1 /* Application class :: menus.ui application.ui
3 * Demonstrates a simple application.
5 * This examples uses GtkApplication, GtkApplicationWindow, GtkBuilder
6 * as well as GMenu and GResource. Due to the way GtkApplication is structured,
7 * it is run as a separate process.
17 show_action_dialog (GSimpleAction *action)
22 name = g_action_get_name (G_ACTION (action));
24 dialog = gtk_message_dialog_new (NULL,
25 GTK_DIALOG_DESTROY_WITH_PARENT,
28 "You activated action: \"%s\"",
31 g_signal_connect (dialog, "response",
32 G_CALLBACK (gtk_widget_destroy), NULL);
34 gtk_widget_show (dialog);
38 show_action_infobar (GSimpleAction *action,
48 name = g_action_get_name (G_ACTION (action));
49 value = g_variant_get_string (parameter, NULL);
51 message = g_object_get_data (G_OBJECT (window), "message");
52 infobar = g_object_get_data (G_OBJECT (window), "infobar");
53 text = g_strdup_printf ("You activated radio action: \"%s\".\n"
54 "Current value: %s", name, value);
55 gtk_label_set_text (GTK_LABEL (message), text);
56 gtk_widget_show (infobar);
61 activate_action (GSimpleAction *action,
65 show_action_dialog (action);
69 activate_toggle (GSimpleAction *action,
75 show_action_dialog (action);
77 state = g_action_get_state (G_ACTION (action));
78 g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
79 g_variant_unref (state);
83 activate_radio (GSimpleAction *action,
87 show_action_infobar (action, parameter, user_data);
89 g_action_change_state (G_ACTION (action), parameter);
93 activate_about (GSimpleAction *action,
97 GtkWidget *window = user_data;
100 const gchar *authors[] = {
108 const gchar *documentors[] = {
111 "Matthias Clasen <mclasen@redhat.com>",
116 pixbuf = gdk_pixbuf_new_from_resource ("/application/logos/gtk-logo-48.png", NULL);
118 gtk_show_about_dialog (GTK_WINDOW (window),
119 "program-name", "GTK+ Code Demos",
120 "version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
122 gtk_get_major_version (),
123 gtk_get_minor_version (),
124 gtk_get_micro_version ()),
125 "copyright", "(C) 1997-2009 The GTK+ Team",
126 "license-type", GTK_LICENSE_LGPL_2_1,
127 "website", "http://www.gtk.org",
128 "comments", "Program to demonstrate GTK+ functions.",
130 "documenters", documentors,
132 "title", "About GTK+ Code Demos",
134 g_object_unref (pixbuf);
138 activate_quit (GSimpleAction *action,
142 GtkApplication *app = user_data;
146 list = gtk_application_get_windows (app);
152 gtk_widget_destroy (GTK_WIDGET (win));
159 register_stock_icons (void)
161 static gboolean registered = FALSE;
166 GtkIconFactory *factory;
167 GtkIconSet *icon_set;
169 static GtkStockItem items[] = {
170 { "demo-gtk-logo", "_GTK!", 0, 0, NULL }
175 gtk_stock_add (items, G_N_ELEMENTS (items));
177 factory = gtk_icon_factory_new ();
178 gtk_icon_factory_add_default (factory);
180 pixbuf = gdk_pixbuf_new_from_resource ("/application/logos/gtk-logo-24.png", NULL);
182 icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
183 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
184 gtk_icon_set_unref (icon_set);
185 g_object_unref (pixbuf);
187 /* Drop our reference to the factory, GTK will hold a reference. */
188 g_object_unref (factory);
193 update_statusbar (GtkTextBuffer *buffer,
194 GtkStatusbar *statusbar)
201 /* clear any previous message, underflow is allowed */
202 gtk_statusbar_pop (statusbar, 0);
204 count = gtk_text_buffer_get_char_count (buffer);
206 gtk_text_buffer_get_iter_at_mark (buffer,
208 gtk_text_buffer_get_insert (buffer));
210 row = gtk_text_iter_get_line (&iter);
211 col = gtk_text_iter_get_line_offset (&iter);
213 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
216 gtk_statusbar_push (statusbar, 0, msg);
222 mark_set_callback (GtkTextBuffer *buffer,
223 const GtkTextIter *new_location,
227 update_statusbar (buffer, GTK_STATUSBAR (data));
231 change_theme_state (GSimpleAction *action,
235 GtkSettings *settings = gtk_settings_get_default ();
237 g_object_set (G_OBJECT (settings),
238 "gtk-application-prefer-dark-theme",
239 g_variant_get_boolean (state),
242 g_simple_action_set_state (action, state);
246 change_titlebar_state (GSimpleAction *action,
250 GtkWindow *window = user_data;
252 gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window),
253 g_variant_get_boolean (state));
255 g_simple_action_set_state (action, state);
259 change_radio_state (GSimpleAction *action,
263 g_simple_action_set_state (action, state);
266 static GActionEntry app_entries[] = {
267 { "new", activate_action, NULL, NULL, NULL },
268 { "open", activate_action, NULL, NULL, NULL },
269 { "save", activate_action, NULL, NULL, NULL },
270 { "save-as", activate_action, NULL, NULL, NULL },
271 { "quit", activate_quit, NULL, NULL, NULL },
272 { "dark", activate_toggle, NULL, "false", change_theme_state }
275 static GActionEntry win_entries[] = {
276 { "titlebar", activate_toggle, NULL, "false", change_titlebar_state },
277 { "shape", activate_radio, "s", "'oval'", change_radio_state },
278 { "bold", activate_toggle, NULL, "false", NULL },
279 { "about", activate_about, NULL, NULL, NULL },
280 { "file1", activate_action, NULL, NULL, NULL },
281 { "logo", activate_action, NULL, NULL, NULL }
285 clicked_cb (GtkWidget *widget, GtkWidget *info)
287 gtk_widget_hide (info);
291 startup (GApplication *app)
297 builder = gtk_builder_new ();
298 gtk_builder_add_from_resource (builder, "/application/ui/menus.ui", NULL);
300 appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
301 menubar = (GMenuModel *)gtk_builder_get_object (builder, "menubar");
303 gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu);
304 gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
306 g_object_unref (builder);
310 activate (GApplication *app)
321 GMenuModel *toolmenu;
322 GtkTextBuffer *buffer;
324 window = gtk_application_window_new (GTK_APPLICATION (app));
325 gtk_window_set_title (GTK_WINDOW (window), "Application Class");
326 gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
327 gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
329 g_action_map_add_action_entries (G_ACTION_MAP (window),
330 win_entries, G_N_ELEMENTS (win_entries),
333 builder = gtk_builder_new ();
334 gtk_builder_add_from_resource (builder, "/application/ui/application.ui", NULL);
336 grid = (GtkWidget *)gtk_builder_get_object (builder, "grid");
337 contents = (GtkWidget *)gtk_builder_get_object (builder, "contents");
338 status = (GtkWidget *)gtk_builder_get_object (builder, "status");
339 message = (GtkWidget *)gtk_builder_get_object (builder, "message");
340 button = (GtkWidget *)gtk_builder_get_object (builder, "button");
341 infobar = (GtkWidget *)gtk_builder_get_object (builder, "infobar");
342 menutool = (GtkWidget *)gtk_builder_get_object (builder, "menutool");
343 toolmenu = (GMenuModel *)gtk_builder_get_object (builder, "toolmenu");
345 g_object_set_data (G_OBJECT (window), "message", message);
346 g_object_set_data (G_OBJECT (window), "infobar", infobar);
348 gtk_container_add (GTK_CONTAINER (window), grid);
350 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (menutool),
351 gtk_menu_new_from_model (toolmenu));
353 gtk_widget_grab_focus (contents);
354 g_signal_connect (button, "clicked", G_CALLBACK (clicked_cb), infobar);
356 /* Show text widget info in the statusbar */
357 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
358 g_signal_connect_object (buffer, "changed",
359 G_CALLBACK (update_statusbar), status, 0);
360 g_signal_connect_object (buffer, "mark-set",
361 G_CALLBACK (mark_set_callback), status, 0);
363 update_statusbar (buffer, GTK_STATUSBAR (status));
365 gtk_widget_show_all (window);
367 g_object_unref (builder);
371 main (int argc, char *argv[])
377 gtk_init (NULL, NULL);
379 register_stock_icons ();
381 app = gtk_application_new ("org.gtk.Demo", 0);
382 settings = g_settings_new ("org.gtk.Demo");
384 g_action_map_add_action_entries (G_ACTION_MAP (app),
385 app_entries, G_N_ELEMENTS (app_entries),
388 action = g_settings_create_action (settings, "color");
390 g_action_map_add_action (G_ACTION_MAP (app), action);
392 g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
393 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
395 g_application_run (G_APPLICATION (app), 0, NULL);
400 #else /* !STANDALONE */
402 static gboolean name_seen;
403 static GtkWidget *placeholder;
406 on_name_appeared (GDBusConnection *connection,
408 const gchar *name_owner,
415 on_name_vanished (GDBusConnection *connection,
424 gtk_widget_destroy (placeholder);
425 g_object_unref (placeholder);
431 #define APP_EXTENSION ".exe"
433 #define APP_EXTENSION
437 do_application (GtkWidget *toplevel)
439 static guint watch = 0;
442 watch = g_bus_watch_name (G_BUS_TYPE_SESSION,
449 if (placeholder == NULL)
451 const gchar *command;
452 GError *error = NULL;
454 if (g_file_test ("./gtk3-demo-application" APP_EXTENSION, G_FILE_TEST_IS_EXECUTABLE))
455 command = "./gtk3-demo-application" APP_EXTENSION;
457 command = "gtk3-demo-application";
459 if (!g_spawn_command_line_async (command, &error))
461 g_warning ("%s", error->message);
462 g_error_free (error);
465 placeholder = gtk_label_new ("");
466 g_object_ref_sink (placeholder);
470 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL),
475 g_variant_new ("(sava{sv})", "quit", NULL, NULL),