| Introduction to the GConf library | ||
|---|---|---|
| <<< Previous | Next >>> | |
This section contains two simple example programs; one displays the value of a key (and updates the displayed value when the key changes), and the other allows you to change the value of the same key. These examples should make the MVC architecture more concrete, and give you a general idea how the GConf API works.
Although the examples use GConfClient, remember that it's also possible to use GConfEngine directly, to avoid linking to GTK. Also, in the next version of GTK and glib the object system will be moved to glib, so GConfClient can move to the main GConf library.
This program creates a GtkLabel that displays the current value of the key /extra/test/directory/key. (By the way, directories do have conventional names, described in the GConf documentation; in this case, the directory /extra is similar to the X- prefix used in mail headers and MIME types, that is, it's for nonstandard extensions to the standard. Here our "nonstandard extension" is a trivial little test program.)
I won't explain all the details of this program, since this article is just a general overview of GConf rather than a full tutorial; the GConf documentation goes into detail about the functions and data types you see here.
/* A very simple program that monitors a single key for changes. */
#include <gconf/gconf-client.h>
#include <gtk/gtk.h>
void
key_changed_callback(GConfClient* client,
guint cnxn_id,
const gchar* key,
GConfValue* value,
gboolean is_default,
gpointer user_data)
{
GtkWidget* label;
label = GTK_WIDGET(user_data);
if (value == NULL)
{
gtk_label_set(GTK_LABEL(label), "<unset>");
}
else
{
if (value->type == GCONF_VALUE_STRING)
{
gtk_label_set(GTK_LABEL(label), gconf_value_string(value));
}
else
{
gtk_label_set(GTK_LABEL(label), "<wrong type>");
}
}
}
int
main(int argc, char** argv)
{
GtkWidget* window;
GtkWidget* label;
GConfClient* client;
gchar* str;
gtk_init(&argc, &argv);
gconf_init(argc, argv, NULL);
client = gconf_client_new();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
str = gconf_client_get_string(client, "/extra/test/directory/key",
NULL);
label = gtk_label_new(str ? str : "<unset>");
if (str)
g_free(str);
gtk_container_add(GTK_CONTAINER(window), label);
gconf_client_add_dir(client,
"/extra/test/directory",
GCONF_CLIENT_PRELOAD_NONE,
NULL);
gconf_client_notify_add(client, "/extra/test/directory/key",
key_changed_callback,
label,
NULL, NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
|
This controller pops up a window with an entry box; if you type in the entry box and press return, the box contents will become the new value of the key /extra/test/directory/key. If you have the view program running, then its label should update to reflect the new value.
Again, see the GConf documentation for full details.
/* A very simple program that sets a single key value when you type
it in an entry and press return */
#include <gconf/gconf-client.h>
#include <gtk/gtk.h>
void
entry_activated_callback(GtkWidget* entry, gpointer user_data)
{
GConfClient* client;
gchar* str;
client = GCONF_CLIENT(user_data);
str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
gconf_client_set_string(client, "/extra/test/directory/key",
str, NULL);
g_free(str);
}
int
main(int argc, char** argv)
{
GtkWidget* window;
GtkWidget* entry;
GConfClient* client;
gtk_init(&argc, &argv);
gconf_init(argc, argv, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(window), entry);
client = gconf_client_new();
gconf_client_add_dir(client,
"/extra/test/directory",
GCONF_CLIENT_PRELOAD_NONE,
NULL);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(entry_activated_callback),
client);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
|
| <<< Previous | Home | Next >>> |
| Application Programming | Other Resources |