📄 example-2.c
字号:
/* This program monitors the * /apps/nautilus/preferences/always_use_browser, * /apps/nautilus/preferences/show_image_thumbnails, and * /apps/nautilus/preferences/thumbnail_limit gconf keys, and allows * the user to change them as well. */#include <gtk/gtk.h>#include <glade/glade.h>#include <gconf/gconf-client.h>static gboolean old_browser_setting;static gchar* old_show_thumbnails_setting;static gint old_thumbnail_limit;/*********** callbacks for always_use_browser **********/voidgconf_browser_key_callback (GConfClient* client, guint cnxn_id, GConfEntry* entry, gpointer user_data){ GtkWidget* button; button = GTK_WIDGET (user_data); /* Make sure the preference has a valid value */ if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_BOOL) { /* Get the new value */ gboolean new_browser_setting = gconf_value_get_bool (gconf_entry_get_value (entry)); /* Try to handle asynchronous-ness of GConf and prevent calling * browser_key_toggled_callback again if this function was * called as a result of the gconf_client_set_bool function * call. */ if (new_browser_setting != old_browser_setting) { old_browser_setting = new_browser_setting; gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), new_browser_setting); } }}voidbrowser_key_toggled_callback (GtkWidget* check_button, gpointer user_data){ GConfClient* client = GCONF_CLIENT (user_data); /* Determine whether the checkbutton is already set or not */ gboolean is_set = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_button)); /* Try to handle asynchronous-ness of GConf and prevent calling * gconf_browser_key_callback again if this function was called as a * result of the gtk_toggle_button_set_active function call. */ if (is_set != old_browser_setting) { old_browser_setting = is_set; gconf_client_set_bool (client, "/apps/nautilus/preferences/always_use_browser", is_set, NULL); }}/*********** callbacks for show_image_thumbnails **********/voidgconf_show_thumbnails_callback (GConfClient* client, guint cnxn_id, GConfEntry* entry, gpointer user_data){ GtkWidget* show_entry; show_entry = GTK_WIDGET (user_data); /* Make sure the preference has a string */ if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_STRING) { /* Get the new value */ const gchar* new_setting = gconf_value_get_string (gconf_entry_get_value (entry)); /* Check if it seems valid, give a warning if it doesn't */ if (strcmp(new_setting, "always") != 0 && strcmp(new_setting, "local_only") != 0 && strcmp(new_setting, "never") != 0) { g_warning("Unrecognized value specified by external program...\n"); } /* Use the new value if it's different than the old */ if (strcmp(new_setting,old_show_thumbnails_setting) != 0) { strcpy(old_show_thumbnails_setting, new_setting); gtk_entry_set_text (GTK_ENTRY (show_entry), new_setting); } }}voidshow_thumbnails_changed_callback (GtkWidget* show_entry, gpointer user_data){ GConfClient* client = GCONF_CLIENT (user_data); /* Get the current value */ const gchar* cur_value = gtk_entry_get_text (GTK_ENTRY (show_entry)); if (strcmp(cur_value, "always") == 0 || strcmp(cur_value, "local_only") == 0 || strcmp(cur_value, "never") == 0) { if (strcmp(cur_value, old_show_thumbnails_setting) != 0) { strcpy(old_show_thumbnails_setting, cur_value); gconf_client_set_string (client, "/apps/nautilus/preferences/show_image_thumbnails", cur_value, NULL); } } else { g_warning("This program needs an error window to warn you that you typed an invalid value.\n"); gtk_entry_set_text (GTK_ENTRY (show_entry), old_show_thumbnails_setting); }}/*********** callbacks for thumbnail_limit **********/voidgconf_thumbnail_limit_callback (GConfClient* client, guint cnxn_id, GConfEntry* entry, gpointer user_data){ GtkWidget* limit_entry; limit_entry = GTK_WIDGET (user_data); /* Make sure the preference has a string */ if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_INT) { /* Get the new value */ gint new_value = gconf_value_get_int (gconf_entry_get_value (entry)); /* Warn if the value seems invalid */ if (new_value < 0) { g_warning("Bad value seems to have been set by external program.\n"); } if (new_value != old_thumbnail_limit) { old_thumbnail_limit = new_value; gchar temp_string[20]; sprintf(temp_string, "%d", old_thumbnail_limit); gtk_entry_set_text (GTK_ENTRY (limit_entry), temp_string); } }}voidthumbnail_limit_changed_callback (GtkWidget* limit_entry, gpointer user_data){ GConfClient* client = GCONF_CLIENT (user_data); /* Get the current value */ const gchar* cur_value = gtk_entry_get_text (GTK_ENTRY (limit_entry)); gboolean is_numeric = TRUE; const gchar* temp_copy = cur_value; while (g_ascii_isdigit (*temp_copy++)) ; temp_copy--; if (*temp_copy) is_numeric = FALSE; gint new_value = 0; if (is_numeric) new_value = atoi(cur_value); if (is_numeric && new_value >= 0) { if (new_value != old_thumbnail_limit) { old_thumbnail_limit = new_value; gconf_client_set_int (client, "/apps/nautilus/preferences/thumbnail_limit", new_value, NULL); } } else { g_warning("This program needs an error window to warn you that you typed an invalid value.\n"); gchar temp_string[20]; sprintf(temp_string, "%d", old_thumbnail_limit); gtk_entry_set_text (GTK_ENTRY (limit_entry), temp_string); }}voidhook_up_callbacks_and_set_defaults (GladeXML * the_widgets){ GtkWidget *widget; /* Get the GConfClient, tell it we want to monitor /apps/nautilus/preferences*/ GConfClient* client = gconf_client_get_default(); gconf_client_add_dir(client, "/apps/nautilus/preferences", GCONF_CLIENT_PRELOAD_NONE, NULL); /* First, setup the checkbutton for the always_use_browser key */ /* Get the check_button */ widget = glade_xml_get_widget (the_widgets, "BrowserMode"); /* Connect the check_button to a callback that can toggle it when the option * is changed by some outside program. */ gconf_client_notify_add(client, "/apps/nautilus/preferences/always_use_browser", gconf_browser_key_callback, widget, NULL, NULL); /* Connect the check_button's clicked callback up so that it can change the * gconf setting. */ g_signal_connect(G_OBJECT (widget), "clicked", G_CALLBACK (browser_key_toggled_callback), client); /* Determine whether button should start activated or not */ old_browser_setting = gconf_client_get_bool (client, "/apps/nautilus/preferences/always_use_browser", NULL); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), old_browser_setting); /* Second, setup the text entry for the show_image_thumbnails key */ /* Get the Entry */ widget = glade_xml_get_widget (the_widgets, "ShowThumbnailsEntry"); /* Connect the show thumbnail entry to a callback that can toggle it * when the option is changed by some outside program. */ gconf_client_notify_add(client, "/apps/nautilus/preferences/show_image_thumbnails", gconf_show_thumbnails_callback, widget, NULL, NULL); /* Connect the show thumbnail entry's activated callback up so that * it can change the gconf setting. */ g_signal_connect(G_OBJECT (widget), "activate", G_CALLBACK (show_thumbnails_changed_callback), client); /* Make space for storing the old value of the old show_thumbnails setting */ old_show_thumbnails_setting = (gchar*)malloc(sizeof(gchar[20])); /* FIXME: Not necessary, but might consider freeing this later... */ /* Determine the starting value of this key */ const gchar* old_value = gconf_client_get_string (client, "/apps/nautilus/preferences/show_image_thumbnails", NULL); strcpy(old_show_thumbnails_setting, old_value); gtk_entry_set_text (GTK_ENTRY (widget), old_show_thumbnails_setting); /* Third, setup the text entry for the thumbnail_limit key */ /* Get the Entry */ widget = glade_xml_get_widget (the_widgets, "ThumbnailLimitEntry"); /* Connect the thumbnail limit entry to a callback that can toggle * it when the option is changed by some outside program. */ gconf_client_notify_add(client, "/apps/nautilus/preferences/thumbnail_limit", gconf_thumbnail_limit_callback, widget, NULL, NULL); /* Connect the thumbnail limit entry's activated callback up so that * it can change the gconf setting. */ g_signal_connect(G_OBJECT (widget), "activate", G_CALLBACK (thumbnail_limit_changed_callback), client); /* Determine the starting value of this key */ old_thumbnail_limit = gconf_client_get_int (client, "/apps/nautilus/preferences/thumbnail_limit", NULL); gchar temp_string[20]; sprintf(temp_string, "%d", old_thumbnail_limit); gtk_entry_set_text (GTK_ENTRY (widget), temp_string); /* Get the main_window and connect the close button to gtk_main_quit */ widget = glade_xml_get_widget (the_widgets, "MainWindow"); g_signal_connect (G_OBJECT (widget), "delete_event", G_CALLBACK (gtk_main_quit), NULL); /* Get the quit button and it to gtk_main_quit */ widget = glade_xml_get_widget (the_widgets, "CloseButton"); g_signal_connect (G_OBJECT (widget), "clicked", G_CALLBACK (gtk_main_quit), NULL);}intmain (int argc, char **argv){ GladeXML *all_da_widgets; gtk_init (&argc, &argv); gconf_init(argc, argv, NULL); /* load the interface */ all_da_widgets = glade_xml_new ("example-2.glade", NULL, NULL); /* Connect all the signals to the appropriate callbacks */ hook_up_callbacks_and_set_defaults (all_da_widgets); /* start the event loop */ gtk_main (); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -