GtkApplication – Part V

#include <gtk/gtk.h>

static void clbk ( G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer application )
{
    /// ***
    g_print ( "The action %s\n", g_action_get_name ( G_ACTION ( action ) ) );

    /// ***
    g_application_quit ( G_APPLICATION ( application ) );
}

static const GActionEntry app_entries[] =
{
    /// ***
    { "open", clbk, NULL, NULL, NULL, { 0, 0, 0 } },
    { "quit", clbk, NULL, NULL, NULL, { 0, 0, 0 } }
};

static void create_accel ( GtkApplication *application )
{
    /// ***
    g_return_if_fail ( GTK_IS_APPLICATION ( application ) );

    /// ***
    const char * const open_accel[] = { "<Shift>O", NULL };
    const char * const quit_accel[] = { "<Control>Q", NULL };

    /// ***
    g_action_map_add_action_entries ( G_ACTION_MAP ( application ), app_entries, G_N_ELEMENTS ( app_entries ), application );

    /// ***
    gtk_application_set_accels_for_action ( GTK_APPLICATION ( application ), "app.open", open_accel );
    gtk_application_set_accels_for_action ( GTK_APPLICATION ( application ), "app.quit", quit_accel );
}

static gchar **get_description ( GtkApplication *app )
{
    gchar **action_descript;
    gint i;

    /// ***
    action_descript = gtk_application_list_action_descriptions ( app );

    /// ***
    for ( i = 0; action_descript[i]; i++ )
    {
        /// ***
        g_print ( "%s\n", action_descript[i] );
    }

    return action_descript;
}

static void get_action ( GtkApplication *app, gchar **action_descript )
{
    /// ***
    gchar **accels;
    gint i;

    /// ***
    for ( i = 0; action_descript[i]; i++ )
    {
        /// ***
        accels = gtk_application_get_accels_for_action ( app, action_descript[i] );

        /// ***
        g_print ( "%s\n", *accels );
    }

    /// ***
    g_strfreev ( accels );
}

static void activate_clbk ( GtkApplication *application )
{
    GtkWidget *window;

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

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

    /// ***
    create_accel ( application );

    /// ***
    gchar **descrpt;
    descrpt = get_description ( application );

    /// ***
    get_action ( application, descrpt );

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

    /// ***
    g_strfreev ( descrpt );
}

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.