GApplication – Part I

#include <gtk/gtk.h>

static void activate ( GtkApplication *application, G_GNUC_UNUSED gpointer data )
{
    GtkWidget *window;

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

    /// ***
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );

    /// *** 3)
    const gchar *const get_id = g_application_get_application_id ( G_APPLICATION ( application ) );

    /// ***
    g_print ( "The application ID is %s\n", get_id );

    /// ***
    gtk_window_present ( GTK_WINDOW ( window ) );
}

static void check_id ( const gchar *const id )
{
    /// *** 1)
    if ( !g_application_id_is_valid ( id ) )
    {
        /// ***
        g_print ( "Invalid ID\n" );

        /// ***
        exit ( EXIT_FAILURE );
    }
}

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

    /// ***
    const gchar *const id = "this.is.my.nice.app";

    /// *** 1)
    check_id ( id );

    /// ***
    application = gtk_application_new ( NULL, G_APPLICATION_FLAGS_NONE );

    /// *** 2)
    g_application_set_application_id ( G_APPLICATION ( application ), "another.id.app" );

    /// ***
    g_signal_connect ( application, "activate", G_CALLBACK ( activate ), 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.