GtkApplication – Part IV

#include <gtk/gtk.h>

static void find_win_by_id ( GtkApplication *application, const guint id )
{
    /// ***
    g_return_if_fail ( GTK_IS_APPLICATION ( application ) );

    /// ***
    if ( id == 0 )
    {
        g_print ( "No id Found\n" );
        return;
    }

    /// ***
    GtkWindow *window = NULL;

    /// ***
    window = gtk_application_get_window_by_id ( application, id );
    GtkWindow *tmp_window = gtk_application_get_active_window ( application );

    /// ***
    if ( GTK_IS_WINDOW ( window ) )
    {
        /// ***
        const gchar *const found_window  = gtk_widget_get_name ( GTK_WIDGET ( window ) );
        const gchar *const active_window = gtk_widget_get_name ( GTK_WIDGET ( tmp_window ) );

        /// ***
        g_print ( "The window found is:  %s\n", found_window );
        g_print ( "The active window is: %s\n", active_window );
    }
}

static void activate_clbk ( GtkApplication *application )
{
    GtkWidget *first_window;
    GtkWidget *second_window;

    /// ***
    guint id = 0;

    /// ***
    first_window = gtk_application_window_new   ( application );

    /// ***
    gtk_window_set_title        ( GTK_WINDOW ( first_window ), "GtkApplicationWindow" );
    gtk_window_set_default_size ( GTK_WINDOW ( first_window ), 300, 200 );

    /// ***
    second_window = gtk_window_new();

    /// ***
    gtk_window_set_title        ( GTK_WINDOW ( second_window ), "GtkWindow" );
    gtk_window_set_default_size ( GTK_WINDOW ( first_window ), 300, 200 );

    /// *** important is, which window is first
    gtk_application_add_window ( application, GTK_WINDOW ( second_window ) );

    /// *** Based of which window is passed
    id = gtk_application_window_get_id ( GTK_APPLICATION_WINDOW ( first_window ) );

    /// ***
    find_win_by_id ( application, id );

    /// *** Depends if is  after or before find_win by id
    ///gtk_application_add_window ( application, GTK_WINDOW ( second_window ) );

    /// ***
    gtk_window_present ( GTK_WINDOW ( first_window ) );

    /// ***
    gtk_window_present ( GTK_WINDOW ( second_window ) );

    /// ***
}

int main ( void )
{
    GtkApplication *application;
    gint status;

    /// ***
    application = gtk_application_new ( "this.is.my-nice.app", G_APPLICATION_FLAGS_NONE );

    /// ***
    g_signal_connect ( application, "activate", G_CALLBACK ( activate_clbk ), NULL );

    /// ***
    status = g_application_run ( G_APPLICATION ( application ), FALSE, NULL );

    /// ***
    g_object_unref ( application );
    return status;
}
Author: MichaelB

Leave a Reply

Your email address will not be published. Required fields are marked *