From 42acaac7d694619a7acd29665701f619be1b31b8 Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Mon, 4 Nov 2013 16:49:05 -0500 Subject: [PATCH] widget-factory: port to GtkApplication and GtkHeaderBar --- demos/widget-factory/widget-factory.c | 151 ++++--- demos/widget-factory/widget-factory.ui | 545 +++---------------------- 2 files changed, 166 insertions(+), 530 deletions(-) diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c index 1dd75a4427..ef3f1d403f 100644 --- a/demos/widget-factory/widget-factory.c +++ b/demos/widget-factory/widget-factory.c @@ -24,19 +24,38 @@ #include static void -dark_toggled (GtkCheckMenuItem *item, gpointer data) +activate_toggle (GSimpleAction *action, + GVariant *parameter, + gpointer user_data) { - gboolean dark; + GVariant *state; - dark = gtk_check_menu_item_get_active (item); - g_object_set (gtk_settings_get_default (), - "gtk-application-prefer-dark-theme", dark, - NULL); + state = g_action_get_state (G_ACTION (action)); + g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state))); + g_variant_unref (state); } static void -show_about (GtkMenuItem *item, GtkWidget *window) +change_theme_state (GSimpleAction *action, + GVariant *state, + gpointer user_data) { + GtkSettings *settings = gtk_settings_get_default (); + + g_object_set (G_OBJECT (settings), + "gtk-application-prefer-dark-theme", + g_variant_get_boolean (state), + NULL); + + g_simple_action_set_state (action, state); +} + +static void +activate_about (GSimpleAction *action, + GVariant *parameter, + gpointer user_data) +{ + GtkWidget *window = user_data; GdkPixbuf *pixbuf; const gchar *authors[] = { "Andrea Cimitan", @@ -66,16 +85,24 @@ show_about (GtkMenuItem *item, GtkWidget *window) } static void -on_page_toggled (GtkToggleButton *button, - GtkNotebook *pages) +activate_quit (GSimpleAction *action, + GVariant *parameter, + gpointer user_data) { - gint page; + GtkApplication *app = user_data; + GtkWidget *win; + GList *list, *next; - if (!gtk_toggle_button_get_active (button)) - return; + list = gtk_application_get_windows (app); + while (list) + { + win = list->data; + next = list->next; - page = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "page")); - gtk_notebook_set_current_page (pages, page); + gtk_widget_destroy (GTK_WIDGET (win)); + + list = next; + } } static void @@ -114,58 +141,78 @@ pulse_it (GtkWidget *widget) return TRUE; } -int -main (int argc, char *argv[]) +static void +startup (GApplication *app) { GtkBuilder *builder; - GtkWidget *window; - GtkWidget *widget; - GtkWidget *notebook; - gboolean dark = FALSE; - GtkAdjustment *adj; - - gtk_init (&argc, &argv); - - if (argc > 1 && (g_strcmp0 (argv[1], "--dark") == 0)) - dark = TRUE; + GMenuModel *appmenu; builder = gtk_builder_new (); gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL); - window = GTK_WIDGET (gtk_builder_get_object (builder, "window")); - gtk_builder_connect_signals (builder, NULL); + appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu"); - widget = (GtkWidget*) gtk_builder_get_object (builder, "progressbar3"); + gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu); + + g_object_unref (builder); +} + +static void +activate (GApplication *app) +{ + GtkBuilder *builder; + GtkWindow *window; + GtkWidget *widget; + GtkAdjustment *adj; + static GActionEntry win_entries[] = { + { "dark", activate_toggle, NULL, "false", change_theme_state } + }; + + builder = gtk_builder_new (); + gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL); + + window = (GtkWindow *)gtk_builder_get_object (builder, "window"); + gtk_application_add_window (GTK_APPLICATION (app), window); + g_action_map_add_action_entries (G_ACTION_MAP (window), + win_entries, G_N_ELEMENTS (win_entries), + window); + + widget = (GtkWidget *)gtk_builder_get_object (builder, "progressbar3"); g_timeout_add (250, (GSourceFunc)pulse_it, widget); - widget = (GtkWidget*) gtk_builder_get_object (builder, "darkmenuitem"); - g_signal_connect (widget, "toggled", G_CALLBACK (dark_toggled), NULL); - gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), dark); - - notebook = (GtkWidget*) gtk_builder_get_object (builder, "toplevel_notebook"); - widget = (GtkWidget*) gtk_builder_get_object (builder, "togglepage1"); - g_object_set_data (G_OBJECT (widget), "page", GINT_TO_POINTER (0)); - g_signal_connect (widget, "toggled", G_CALLBACK (on_page_toggled), notebook); - - widget = (GtkWidget*) gtk_builder_get_object (builder, "togglepage2"); - g_object_set_data (G_OBJECT (widget), "page", GINT_TO_POINTER (1)); - g_signal_connect (widget, "toggled", G_CALLBACK (on_page_toggled), notebook); - - widget = (GtkWidget*) gtk_builder_get_object (builder, "aboutmenuitem"); - g_signal_connect (widget, "activate", G_CALLBACK (show_about), window); - - widget = (GtkWidget*) gtk_builder_get_object (builder, "page2dismiss"); + widget = (GtkWidget *)gtk_builder_get_object (builder, "page2dismiss"); g_signal_connect (widget, "clicked", G_CALLBACK (dismiss), NULL); - widget = (GtkWidget*) gtk_builder_get_object (builder, "page2note"); + widget = (GtkWidget *)gtk_builder_get_object (builder, "page2note"); adj = (GtkAdjustment *) gtk_builder_get_object (builder, "adjustment2"); - g_signal_connect (adj, "value-changed", - G_CALLBACK (spin_value_changed), widget); + g_signal_connect (adj, "value-changed", G_CALLBACK (spin_value_changed), widget); - g_object_unref (G_OBJECT (builder)); + gtk_widget_show_all (GTK_WIDGET (window)); - gtk_widget_show (window); - gtk_main (); + g_object_unref (builder); +} + +int +main (int argc, char *argv[]) +{ + GtkApplication *app; + static GActionEntry app_entries[] = { + { "about", activate_about, NULL, NULL, NULL }, + { "quit", activate_quit, NULL, NULL, NULL }, + }; + + gtk_init (&argc, &argv); + + app = gtk_application_new ("org.gtk.WidgetFactory", 0); + + g_action_map_add_action_entries (G_ACTION_MAP (app), + app_entries, G_N_ELEMENTS (app_entries), + app); + + g_signal_connect (app, "startup", G_CALLBACK (startup), NULL); + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + + g_application_run (G_APPLICATION (app), argc, argv); return 0; } diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui index 3360b4fb4a..d1242c05f8 100644 --- a/demos/widget-factory/widget-factory.ui +++ b/demos/widget-factory/widget-factory.ui @@ -1,6 +1,29 @@ + +
+ + About + app.about + +
+
+ + _Quit + app.quit + <Primary>q + +
+
+ +
+ + Dark Theme + win.dark + +
+
100 50 @@ -72,495 +95,53 @@ Suspendisse feugiat quam quis dolor accumsan cursus. Spanish Uyghur - + False GTK+ Widget Factory + + + True + False + True + + + True + toplevel_stack + + + + + True + center + False + gear_menu + + + + True + 1 + emblem-system-symbolic + + + + + end + + + + True False vertical - + True - False - - - True - False - False - _File - True - - - True - False - - - _New - True - False - False - True - - - - - _Open - True - False - False - True - - - - - _Save - True - False - False - True - - - - - Save _As - True - False - False - True - - - - - True - False - False - - - - - _Quit - True - False - False - True - - - - - - - - - - True - False - False - _Edit - True - - - True - False - - - Cu_t - True - True - False - True - - - - - _Copy - True - False - False - True - - - - - _Paste - True - False - False - True - - - - - _Delete - True - False - False - True - - - - - Checks & Radios - True - False - False - True - False - - - True - False - - - _Check - True - True - True - False - False - True - - - - - _Check - True - True - False - False - False - True - - - - - _Check - False - True - True - True - False - False - True - - - - - _Check - False - True - True - False - False - True - - - - - _Check - False - True - False - False - False - True - - - - - _Check - False - True - True - False - False - False - True - - - - - True - - - - - _Radio - True - True - True - False - False - True - - - - - _Radio - True - True - False - False - False - True - - - - - _Radio - False - True - True - True - False - False - True - - - - - _Radio - False - True - True - False - False - True - - - - - _Radio - False - True - False - False - False - True - - - - - _Radio - False - True - True - False - False - False - True - - - - - - - - - - - - - True - False - False - _View - True - - - True - False - - - _Dark theme - True - False - False - True - - - - - - - - - True - False - False - _Help - True - - - True - False - - - _About - True - False - False - True - - - - - - - - - False - True - 0 - - - - - True - False - - - - True - False - False - _Save - True - document-save - - - False - True - - - - - True - False - False - Open - True - document-open - - - False - True - - - - - True - False - False - - - False - True - - - - - True - False - False - Find - True - edit-find - - - False - True - - - - - True - False - False - - - - True - horizontal - True - center - True - - - - True - Page 1 - False - - - - - True - Page 2 - False - togglepage1 - - - - - - - True - True - - - - - True - False - False - - - - - - False - True - - - - - True - False - False - - - True - True - - search... - - - - - False - - - - - False - True - 1 - - - - - True - False True @@ -2159,6 +1740,10 @@ Suspendisse feugiat quam quis dolor accumsan cursus. + + page1 + Page 1 + @@ -2317,6 +1902,10 @@ Suspendisse feugiat quam quis dolor accumsan cursus. + + page2 + Page 2 +