2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gtkapplication.h"
31 #include "gtkmarshalers.h"
33 #include "gtkwindow.h"
34 #include "gtkcheckmenuitem.h"
35 #include "gtkseparatormenuitem.h"
39 #ifdef GDK_WINDOWING_X11
40 #include <gdk/x11/gdkx.h>
44 * SECTION:gtkapplication
45 * @title: GtkApplication
46 * @short_description: Application class
48 * #GtkApplication is a class that handles many important aspects
49 * of a GTK+ application in a convenient fashion, without enforcing
50 * a one-size-fits-all application model.
52 * Currently, GtkApplication handles GTK+ initialization, application
53 * uniqueness, provides some basic scriptability by exporting 'actions',
54 * and manages a list of toplevel windows whose life-cycle is automatically
55 * tied to the life-cycle of your application.
57 * <example id="gtkapplication"><title>A simple application</title>
59 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
60 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
72 static guint gtk_application_signals[LAST_SIGNAL];
74 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
76 struct _GtkApplicationPrivate
82 gtk_application_focus_in_event_cb (GtkWindow *window,
84 GtkApplication *application)
86 GtkApplicationPrivate *priv = application->priv;
89 /* Keep the window list sorted by most-recently-focused. */
90 link = g_list_find (priv->windows, window);
91 if (link != NULL && link != priv->windows)
93 priv->windows = g_list_remove_link (priv->windows, link);
94 priv->windows = g_list_concat (link, priv->windows);
101 gtk_application_startup (GApplication *application)
103 G_APPLICATION_CLASS (gtk_application_parent_class)
104 ->startup (application);
110 gtk_application_add_platform_data (GApplication *application,
111 GVariantBuilder *builder)
113 const gchar *startup_id;
115 startup_id = getenv ("DESKTOP_STARTUP_ID");
117 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
118 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
119 g_variant_new_string (startup_id));
123 gtk_application_before_emit (GApplication *application,
124 GVariant *platform_data)
130 g_variant_iter_init (&iter, platform_data);
131 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
133 #ifdef GDK_WINDOWING_X11
134 if (strcmp (key, "desktop-startup-id") == 0)
139 display = gdk_display_get_default ();
140 id = g_variant_get_string (value, NULL);
141 gdk_x11_display_set_startup_notification_id (display, id);
148 gtk_application_after_emit (GApplication *application,
149 GVariant *platform_data)
151 gdk_notify_startup_complete ();
155 gtk_application_init (GtkApplication *application)
157 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
158 GTK_TYPE_APPLICATION,
159 GtkApplicationPrivate);
163 gtk_application_window_added (GtkApplication *application,
166 GtkApplicationPrivate *priv = application->priv;
168 priv->windows = g_list_prepend (priv->windows, window);
169 gtk_window_set_application (window, application);
170 g_application_hold (G_APPLICATION (application));
172 g_signal_connect (window, "focus-in-event",
173 G_CALLBACK (gtk_application_focus_in_event_cb),
178 gtk_application_window_removed (GtkApplication *application,
181 GtkApplicationPrivate *priv = application->priv;
183 g_signal_handlers_disconnect_by_func (window,
184 gtk_application_focus_in_event_cb,
187 g_application_release (G_APPLICATION (application));
188 priv->windows = g_list_remove (priv->windows, window);
189 gtk_window_set_application (window, NULL);
193 gtk_application_class_init (GtkApplicationClass *class)
195 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
197 application_class->add_platform_data = gtk_application_add_platform_data;
198 application_class->before_emit = gtk_application_before_emit;
199 application_class->after_emit = gtk_application_after_emit;
200 application_class->startup = gtk_application_startup;
202 class->window_added = gtk_application_window_added;
203 class->window_removed = gtk_application_window_removed;
205 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
208 * GtkApplication::window-added:
209 * @application: the #GtkApplication which emitted the signal
210 * @window: the newly-added #GtkWindow
212 * Emitted when a #GtkWindow is added to @application through
213 * gtk_application_add_window().
217 gtk_application_signals[WINDOW_ADDED] =
218 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
219 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
221 g_cclosure_marshal_VOID__OBJECT,
222 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
225 * GtkApplication::window-removed:
226 * @application: the #GtkApplication which emitted the signal
227 * @window: the #GtkWindow that is being removed
229 * Emitted when a #GtkWindow is removed from @application,
230 * either as a side-effect of being destroyed or explicitly
231 * through gtk_application_remove_window().
235 gtk_application_signals[WINDOW_REMOVED] =
236 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
237 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
239 g_cclosure_marshal_VOID__OBJECT,
240 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
244 * gtk_application_new:
245 * @application_id: the application id
246 * @flags: the application flags
248 * Creates a new #GtkApplication instance.
250 * This function calls g_type_init() for you. gtk_init() is called
251 * as soon as the application gets registered as the primary instance.
253 * The application id must be valid. See g_application_id_is_valid().
255 * Returns: a new #GtkApplication instance
258 gtk_application_new (const gchar *application_id,
259 GApplicationFlags flags)
261 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
265 return g_object_new (GTK_TYPE_APPLICATION,
266 "application-id", application_id,
272 * gtk_application_add_window:
273 * @application: a #GtkApplication
274 * @window: a #GtkWindow
276 * Adds a window from @application.
278 * This call is equivalent to setting the #GtkWindow:application
279 * property of @window to @application.
281 * Normally, the connection between the application and the window
282 * will remain until the window is destroyed, but you can explicitly
283 * remove it with gtk_application_remove_window().
285 * GTK+ will keep the application running as long as it has
291 gtk_application_add_window (GtkApplication *application,
294 g_return_if_fail (GTK_IS_APPLICATION (application));
296 if (!g_list_find (application->priv->windows, window))
297 g_signal_emit (application,
298 gtk_application_signals[WINDOW_ADDED], 0, window);
302 * gtk_application_remove_window:
303 * @application: a #GtkApplication
304 * @window: a #GtkWindow
306 * Remove a window from @application.
308 * If @window belongs to @application then this call is equivalent to
309 * setting the #GtkWindow:application property of @window to
312 * The application may stop running as a result of a call to this
318 gtk_application_remove_window (GtkApplication *application,
321 g_return_if_fail (GTK_IS_APPLICATION (application));
323 if (g_list_find (application->priv->windows, window))
324 g_signal_emit (application,
325 gtk_application_signals[WINDOW_REMOVED], 0, window);
329 * gtk_application_get_windows:
330 * @application: a #GtkApplication
332 * Gets a list of the #GtkWindow<!-- -->s associated with @application.
334 * The list is sorted by most recently focused window, such that the first
335 * element is the currently focused window. (Useful for choosing a parent
336 * for a transient window.)
338 * The list that is returned should not be modified in any way. It will
339 * only remain valid until the next focus change or window creation or
342 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
347 gtk_application_get_windows (GtkApplication *application)
349 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
351 return application->priv->windows;
354 /* GtkMenu construction {{{1 */
360 gulong enabled_changed_id;
361 gulong state_changed_id;
362 gulong activate_handler;
366 action_data_free (gpointer data)
368 ActionData *a = data;
370 if (a->enabled_changed_id)
371 g_signal_handler_disconnect (a->group, a->enabled_changed_id);
373 if (a->state_changed_id)
374 g_signal_handler_disconnect (a->group, a->state_changed_id);
376 g_object_unref (a->group);
384 enabled_changed (GActionGroup *group,
385 const gchar *action_name,
389 gtk_widget_set_sensitive (widget, enabled);
393 item_activated (GtkWidget *w,
399 a = g_object_get_data (G_OBJECT (w), "action");
401 parameter = g_variant_ref_sink (g_variant_new_string (a->target));
404 g_action_group_activate_action (a->group, a->name, parameter);
406 g_variant_unref (parameter);
410 toggle_state_changed (GActionGroup *group,
417 a = g_object_get_data (G_OBJECT (w), "action");
418 g_signal_handler_block (w, a->activate_handler);
419 gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
420 g_signal_handler_unblock (w, a->activate_handler);
424 radio_state_changed (GActionGroup *group,
432 a = g_object_get_data (G_OBJECT (w), "action");
433 g_signal_handler_block (w, a->activate_handler);
434 b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
435 gtk_check_menu_item_set_active (w, b);
436 g_signal_handler_unblock (w, a->activate_handler);
440 create_menuitem_from_model (GMenuModel *model,
450 const GVariantType *type;
454 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
457 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
460 type = g_action_group_get_action_state_type (group, action);
465 w = gtk_menu_item_new_with_mnemonic (label);
466 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
467 w = gtk_check_menu_item_new_with_label (label);
468 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
470 w = gtk_check_menu_item_new_with_label (label);
471 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
474 g_assert_not_reached ();
478 a = g_new0 (ActionData, 1);
479 a->group = g_object_ref (group);
480 a->name = g_strdup (action);
481 g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
483 if (!g_action_group_get_action_enabled (group, action))
484 gtk_widget_set_sensitive (w, FALSE);
486 s = g_strconcat ("action-enabled-changed::", action, NULL);
487 a->enabled_changed_id = g_signal_connect (group, s,
488 G_CALLBACK (enabled_changed), w);
490 a->activate_handler = g_signal_connect (w, "activate",
491 G_CALLBACK (item_activated), NULL);
497 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
499 s = g_strconcat ("action-state-changed::", action, NULL);
500 a->state_changed_id = g_signal_connect (group, s,
501 G_CALLBACK (toggle_state_changed), w);
503 v = g_action_group_get_action_state (group, action);
504 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
505 g_variant_get_boolean (v));
508 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
510 s = g_strconcat ("action-state-changed::", action, NULL);
511 a->state_changed_id = g_signal_connect (group, s,
512 G_CALLBACK (radio_state_changed), w);
514 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
515 a->target = g_strdup (target);
516 v = g_action_group_get_action_state (group, action);
517 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
518 g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
523 g_assert_not_reached ();
532 static void populate_menu_from_model (GtkMenuShell *menu,
534 GActionGroup *group);
537 append_items_from_model (GtkMenuShell *menu,
540 gboolean *need_separator,
541 const gchar *heading)
551 n = g_menu_model_get_n_items (model);
553 if (*need_separator && n > 0)
555 w = gtk_separator_menu_item_new ();
557 gtk_menu_shell_append (menu, w);
559 *need_separator = FALSE;
564 w = gtk_menu_item_new_with_label (heading);
566 gtk_widget_set_sensitive (w, FALSE);
567 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
570 for (i = 0; i < n; i++)
572 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
575 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
576 append_items_from_model (menu, m, group, need_separator, label);
582 menuitem = create_menuitem_from_model (model, i, group);
584 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
586 submenu = gtk_menu_new ();
587 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
588 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
592 gtk_widget_show (menuitem);
593 gtk_menu_shell_append (menu, menuitem);
595 *need_separator = TRUE;
600 populate_menu_from_model (GtkMenuShell *menu,
604 gboolean need_separator;
606 need_separator = FALSE;
607 append_items_from_model (menu, model, group, &need_separator, NULL);
611 GtkApplication *application;
614 GHashTable *connected;
618 free_items_changed_data (gpointer data)
620 ItemsChangedData *d = data;
622 g_object_unref (d->application);
624 if (d->update_idle != 0)
625 g_source_remove (d->update_idle);
627 g_hash_table_unref (d->connected);
633 repopulate_menu (gpointer data)
635 ItemsChangedData *d = data;
640 /* remove current children */
641 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
642 for (l = children; l; l = l->next)
645 gtk_container_remove (GTK_CONTAINER (d->menu), child);
647 g_list_free (children);
650 model = g_application_get_menu (G_APPLICATION (d->application));
651 populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
659 connect_to_items_changed (GMenuModel *model,
663 ItemsChangedData *d = data;
668 if (!g_hash_table_lookup (d->connected, model))
670 g_signal_connect (model, "items-changed", callback, data);
671 g_hash_table_insert (d->connected, model, model);
674 for (i = 0; i < g_menu_model_get_n_items (model); i++)
676 iter = g_menu_model_iterate_item_links (model, i);
677 while (g_menu_link_iter_next (iter))
679 m = g_menu_link_iter_get_value (iter);
680 connect_to_items_changed (m, callback, data);
683 g_object_unref (iter);
688 items_changed (GMenuModel *model,
694 ItemsChangedData *d = data;
696 if (d->update_idle == 0)
697 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
698 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
702 * gtk_application_get_menu:
703 * @application: a #GtkApplication
705 * Populates a menu widget from a menu model that is
706 * associated with @application. See g_application_set_menu().
707 * The menu items will be connected to action of @application,
708 * as indicated by the menu model. The menus contents will be
709 * updated automatically in response to menu model changes.
711 * It is the callers responsibility to add the menu at a
712 * suitable place in the widget hierarchy.
714 * This function returns %NULL if @application has no associated
715 * menu model. It also returns %NULL if the menu model is
716 * represented outside the application, e.g. by an application
717 * menu in the desktop shell.
719 * @menu may be a #GtkMenu or a #GtkMenuBar.
721 * Returns: A #GtkMenu that has been populated from the
722 * #GMenuModel that is associated with @application,
726 gtk_application_get_menu (GtkApplication *application)
730 ItemsChangedData *data;
732 model = g_application_get_menu (G_APPLICATION (application));
737 /* FIXME: find out if external menu is available. If yes, return NULL */
739 menu = gtk_menu_new ();
741 populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (application));
743 data = g_new (ItemsChangedData, 1);
744 data->application = g_object_ref (application);
745 data->menu = GTK_MENU_SHELL (menu);
746 data->update_idle = 0;
747 data->connected = g_hash_table_new (NULL, NULL);
749 g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
750 data, free_items_changed_data);
752 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
754 return GTK_MENU (menu);