GSettings – Part I

#include <gtk/gtk.h>

#define MY_KEY "my-key"
#define SCHEMA_ID "apps.gsettings.c.gschema.xml"
#define PATH "/apps/gsettings.c/"

void set_settings_value ( const gboolean value );
void set_settings_value ( const gboolean value )
{
    /// ***
    GSettings        *settings;

    /// ***
    settings = g_settings_new_with_path ( SCHEMA_ID, PATH );

    /// ***
    g_settings_set_boolean ( settings, MY_KEY, value );

    /// ***
    g_settings_apply ( settings );

    /// ***
    g_print ( "The State is [%s]\n", value ? "Checked" : "Not checked" );

    /// ***
    g_object_unref ( settings );
}

void toggled_clbk ( GtkCheckButton *button );
void toggled_clbk ( GtkCheckButton *button )
{
    g_return_if_fail ( GTK_IS_CHECK_BUTTON ( button ) );

    /// ***
    gboolean active;

    /// ***
    active = gtk_check_button_get_active ( button );

    /// ***
    set_settings_value ( active );
}

GtkWidget *create_check_button ( const gchar *const label );
GtkWidget *create_check_button ( const gchar *const label )
{

    GtkWidget *check_button;
    GSettings *settings;
    gboolean   value;

    /// ***
    ///settings = g_settings_new ( SCHEMA_ID );
    settings = g_settings_new_with_path ( SCHEMA_ID, PATH );

    /// ***
    value = g_settings_get_boolean ( settings, MY_KEY );

    /// ***
    check_button = gtk_check_button_new_with_label ( label );

    /// ***
    gtk_check_button_set_active ( GTK_CHECK_BUTTON ( check_button ), value );

    /// ***
    g_signal_connect_swapped ( check_button, "toggled", G_CALLBACK ( toggled_clbk ), check_button );

    return check_button;
}

static void activate ( GtkApplication *app )
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *check_button;

    /// ***
    window = gtk_application_window_new ( app );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
    gtk_window_set_title ( GTK_WINDOW ( window ), "MyChat" );

    /// ***
    box = gtk_box_new ( GTK_ORIENTATION_HORIZONTAL, 5 );

    /// ***
    gtk_window_set_child ( GTK_WINDOW ( window ), box );

    /// ***
    check_button = create_check_button ( "Check me" );

    /// ***
    gtk_box_append ( GTK_BOX ( box ), check_button );

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

int main ( int argc, char *argv[] )
{
    GtkApplication *app;
    int status;

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

    /// ***
    g_signal_connect_swapped ( app, "activate", G_CALLBACK ( activate ), app );

    /// ***
    status = g_application_run ( G_APPLICATION ( app ), argc, argv );

    return status;
}
Author: MichaelB

Leave a Reply

Your email address will not be published.