]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
GtkApplicationWindow: deal with remote actions
[~andy/gtk] / gtk / gtkapplicationwindow.c
1 /*
2  * Copyright © 2011 Canonical Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gtkapplicationwindow.h"
25
26 #include "gtkapplicationprivate.h"
27 #include "gtkmodelmenu.h"
28 #include "gactionmuxer.h"
29 #include "gtkaccelgroup.h"
30 #include "gtkaccelmap.h"
31 #include "gtkintl.h"
32
33 #include <gdk/gdk.h>
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
36 #endif
37
38 /**
39  * SECTION:gtkapplicationwindow
40  * @title: GtkApplicationWindow
41  * @short_description: GtkWindow subclass with GtkApplication support
42  *
43  * GtkApplicationWindow is a #GtkWindow subclass that offers some
44  * extra functionality for better integration with #GtkApplication
45  * features.  Notably, it can handle both the application menu as well
46  * as the menubar. See g_application_set_app_menu() and
47  * g_application_set_menubar().
48  *
49  * This class implements the #GActionGroup and #GActionMap interfaces,
50  * to let you add window-specific actions that will be exported by the
51  * associated #GtkApplication, together with its application-wide
52  * actions.  Window-specific actions are prefixed with the "win."
53  * prefix and application-wide actions are prefixed with the "app."
54  * prefix.  Actions must be addressed with the prefixed name when
55  * referring to them from a #GMenuModel.
56  *
57  * The settings #GtkSettings:gtk-shell-shows-app-menu and
58  * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
59  * desktop environment is showing the application menu and menubar
60  * models outside the application as part of the desktop shell.
61  * For instance, on OS X, both menus will be displayed remotely;
62  * on Windows neither will be. gnome-shell (starting with version 3.4)
63  * will display the application menu, but not the menubar.
64  *
65  * If the desktop environment does not display the menubar, then
66  * #GApplicationWindow will automatically show a #GtkMenubar for it.
67  * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
68  * property. If the desktop environment does not display the application
69  * menu, then it will automatilly be included in the menubar.
70  *
71  * <example><title>A GtkApplicationWindow with a menubar</title>
72  * <programlisting><![CDATA[
73  * app = gtk_application_new ();
74  *
75  * builder = gtk_builder_new ();
76  * gtk_builder_add_from_string (builder,
77  *     "<interface>"
78  *     "  <menu id='menubar'>"
79  *     "    <submenu label='_Edit'>"
80  *     "      <item label='_Copy' action='win.copy'/>"
81  *     "      <item label='_Paste' action='win.paste'/>"
82  *     "    </submenu>"
83  *     "  </menu>"
84  *     "</interface>");
85  * g_application_set_menubar (G_APPLICATION (app),
86  *                            G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
87  * g_object_unref (builder);
88  *
89  * ...
90  *
91  * window = gtk_application_window_new (app);
92  * ]]>
93  * </programlisting>
94  * </example>
95  */
96
97 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
98 typedef struct
99 {
100   GSimpleActionGroup parent_instance;
101   GtkWindow *window;
102 } GtkApplicationWindowActions;
103
104 static GType gtk_application_window_actions_get_type   (void);
105 static void  gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
106 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
107                          G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
108
109 static void
110 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
111                                                      const gchar        *action_name,
112                                                      GVariant           *parameter,
113                                                      GVariant           *platform_data)
114 {
115   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
116   GApplication *application;
117   GApplicationClass *class;
118
119   application = G_APPLICATION (gtk_window_get_application (actions->window));
120   class = G_APPLICATION_GET_CLASS (application);
121
122   class->before_emit (application, platform_data);
123   g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
124   class->after_emit (application, platform_data);
125 }
126
127 static void
128 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
129                                                          const gchar        *action_name,
130                                                          GVariant           *value,
131                                                          GVariant           *platform_data)
132 {
133   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
134   GApplication *application;
135   GApplicationClass *class;
136
137   application = G_APPLICATION (gtk_window_get_application (actions->window));
138   class = G_APPLICATION_GET_CLASS (application);
139
140   class->before_emit (application, platform_data);
141   g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
142   class->after_emit (application, platform_data);
143 }
144
145 static void
146 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
147 {
148 }
149
150 static void
151 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
152 {
153   iface->activate_action_full = gtk_application_window_actions_activate_action_full;
154   iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
155 }
156
157 static void
158 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
159 {
160 }
161
162 static GSimpleActionGroup *
163 gtk_application_window_actions_new (GtkApplicationWindow *window)
164 {
165   GtkApplicationWindowActions *actions;
166
167   actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
168   actions->window = GTK_WINDOW (window);
169
170   return G_SIMPLE_ACTION_GROUP (actions);
171 }
172
173 /* Now onto GtkApplicationWindow... */
174
175 struct _GtkApplicationWindowPrivate
176 {
177   GSimpleActionGroup *actions;
178   GActionObservable *muxer;
179   GtkWidget *menubar;
180   GtkAccelGroup *accels;
181   GSList *accel_closures;
182
183   GMenu *app_menu_section;
184   GMenu *menubar_section;
185   gboolean show_menubar;
186
187   GDBusConnection *session;
188   gchar           *object_path;
189   guint            export_id;
190 };
191
192 static void
193 gtk_application_window_update_menubar (GtkApplicationWindow *window)
194 {
195   gboolean should_have_menubar;
196   gboolean have_menubar;
197
198   have_menubar = window->priv->menubar != NULL;
199
200   should_have_menubar = window->priv->show_menubar &&
201                         (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
202                          g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
203
204   if (have_menubar && !should_have_menubar)
205     {
206       gtk_widget_unparent (window->priv->menubar);
207       window->priv->menubar = NULL;
208
209       gtk_widget_queue_resize (GTK_WIDGET (window));
210     }
211
212   if (!have_menubar && should_have_menubar)
213     {
214       GMenu *combined;
215
216       combined = g_menu_new ();
217       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
218       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
219
220       window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
221       gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
222       gtk_widget_show_all (window->priv->menubar);
223       g_object_unref (combined);
224
225       gtk_widget_queue_resize (GTK_WIDGET (window));
226     }
227 }
228
229 static void
230 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
231                                                     GtkSettings          *settings)
232 {
233   gboolean shown_by_shell;
234
235   g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
236
237   if (shown_by_shell)
238     {
239       /* the shell shows it, so don't show it locally */
240       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
241         g_menu_remove (window->priv->app_menu_section, 0);
242     }
243   else
244     {
245       /* the shell does not show it, so make sure we show it */
246       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
247         {
248           GMenuModel *app_menu;
249
250           app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
251
252           if (app_menu != NULL)
253             g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
254         }
255     }
256 }
257
258 static void
259 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
260                                                    GtkSettings          *settings)
261 {
262   gboolean shown_by_shell;
263
264   g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
265
266   if (shown_by_shell)
267     {
268       /* the shell shows it, so don't show it locally */
269       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
270         g_menu_remove (window->priv->menubar_section, 0);
271     }
272   else
273     {
274       /* the shell does not show it, so make sure we show it */
275       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
276         {
277           GMenuModel *menubar;
278
279           menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
280
281           if (menubar != NULL)
282             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
283         }
284     }
285 }
286
287 typedef struct {
288   GClosure closure;
289   gchar *action_name;
290   GVariant *parameter;
291 } AccelClosure;
292
293 static void
294 accel_activate (GClosure     *closure,
295                 GValue       *return_value,
296                 guint         n_param_values,
297                 const GValue *param_values,
298                 gpointer      invocation_hint,
299                 gpointer      marshal_data)
300 {
301   AccelClosure *aclosure = (AccelClosure*)closure;
302   GActionGroup *actions;
303
304   actions = G_ACTION_GROUP (closure->data);
305   if (g_action_group_get_action_enabled (actions, aclosure->action_name))
306     {
307        g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
308
309       /* we handled the accelerator */
310       g_value_set_boolean (return_value, TRUE);
311     }
312 }
313
314 static void
315 free_accel_closures (GtkApplicationWindow *window)
316 {
317   GSList *l;
318
319   for (l = window->priv->accel_closures; l; l = l->next)
320     {
321        AccelClosure *closure = l->data;
322
323        gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
324
325        g_object_unref (closure->closure.data);
326        if (closure->parameter)
327          g_variant_unref (closure->parameter);
328        g_free (closure->action_name);
329        g_closure_invalidate (&closure->closure);
330        g_closure_unref (&closure->closure);
331     }
332   g_slist_free (window->priv->accel_closures);
333   window->priv->accel_closures = NULL;
334 }
335
336 typedef struct {
337   GtkApplicationWindow *window;
338   GActionGroup *actions;
339 } AccelData;
340
341 /* Hack. We iterate over the accel map instead of the actions,
342  * in order to pull the parameters out of accel map entries
343  */
344 static void
345 add_accel_closure (gpointer         data,
346                    const gchar     *accel_path,
347                    guint            accel_key,
348                    GdkModifierType  accel_mods,
349                    gboolean         changed)
350 {
351   AccelData *d = data;
352   GtkApplicationWindow *window = d->window;
353   GActionGroup *actions = d->actions;
354   const gchar *path;
355   const gchar *p;
356   gchar *action_name;
357   GVariant *parameter;
358   AccelClosure *closure;
359
360   if (accel_key == 0)
361     return;
362
363   if (!g_str_has_prefix (accel_path, "<Actions>/"))
364     return;
365
366   path = accel_path + strlen ("<Actions>/");
367   p = strchr (path, '/');
368   if (p)
369     {
370       action_name = g_strndup (path, p - path);
371       parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
372       if (!parameter)
373         g_warning ("Failed to parse parameter from '%s'\n", accel_path);
374     }
375   else
376     {
377       action_name = g_strdup (path);
378       parameter = NULL;
379     }
380
381   if (g_action_group_has_action (actions, action_name))
382     {
383       closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
384       g_closure_set_marshal (&closure->closure, accel_activate);
385
386       closure->action_name = g_strdup (action_name);
387       closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
388
389       window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
390       g_closure_sink (&closure->closure);
391
392       gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
393     }
394
395   g_free (action_name);
396 }
397
398 static void
399 gtk_application_window_update_accels (GtkApplicationWindow *window)
400 {
401   AccelData data;
402
403   free_accel_closures (window);
404
405   data.window = window;
406   data.actions = G_ACTION_GROUP (window->priv->muxer);
407
408   gtk_accel_map_foreach (&data, add_accel_closure);
409 }
410
411 static void
412 gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
413                                                      GParamSpec *pspec,
414                                                      gpointer    user_data)
415 {
416   GtkApplicationWindow *window = user_data;
417
418   gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
419   gtk_application_window_update_menubar (window);
420 }
421
422 static void
423 gtk_application_window_shell_shows_menubar_changed (GObject    *object,
424                                                     GParamSpec *pspec,
425                                                     gpointer    user_data)
426 {
427   GtkApplicationWindow *window = user_data;
428
429   gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
430   gtk_application_window_update_menubar (window);
431 }
432
433 static gchar **
434 gtk_application_window_list_actions (GActionGroup *group)
435 {
436   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
437
438   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
439 }
440
441 static gboolean
442 gtk_application_window_query_action (GActionGroup        *group,
443                                      const gchar         *action_name,
444                                      gboolean            *enabled,
445                                      const GVariantType **parameter_type,
446                                      const GVariantType **state_type,
447                                      GVariant           **state_hint,
448                                      GVariant           **state)
449 {
450   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
451
452   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
453                                       action_name, enabled, parameter_type, state_type, state_hint, state);
454 }
455
456 static void
457 gtk_application_window_activate_action (GActionGroup *group,
458                                         const gchar  *action_name,
459                                         GVariant     *parameter)
460 {
461   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
462
463   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
464 }
465
466 static void
467 gtk_application_window_change_action_state (GActionGroup *group,
468                                             const gchar  *action_name,
469                                             GVariant     *state)
470 {
471   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
472
473   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
474 }
475
476 static GAction *
477 gtk_application_window_lookup_action (GActionMap  *action_map,
478                                       const gchar *action_name)
479 {
480   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
481
482   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
483 }
484
485 static void
486 gtk_application_window_add_action (GActionMap *action_map,
487                                    GAction    *action)
488 {
489   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
490
491   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
492 }
493
494 static void
495 gtk_application_window_remove_action (GActionMap  *action_map,
496                                       const gchar *action_name)
497 {
498   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
499
500   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
501 }
502
503 static void
504 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
505 {
506   iface->list_actions = gtk_application_window_list_actions;
507   iface->query_action = gtk_application_window_query_action;
508   iface->activate_action = gtk_application_window_activate_action;
509   iface->change_action_state = gtk_application_window_change_action_state;
510 }
511
512 static void
513 gtk_application_window_map_iface_init (GActionMapInterface *iface)
514 {
515   iface->lookup_action = gtk_application_window_lookup_action;
516   iface->add_action = gtk_application_window_add_action;
517   iface->remove_action = gtk_application_window_remove_action;
518 }
519
520 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
521                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
522                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
523
524 enum {
525   PROP_0,
526   PROP_SHOW_MENUBAR,
527   N_PROPS
528 };
529 static GParamSpec *gtk_application_window_properties[N_PROPS];
530
531 static void
532 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
533                                                   gint      *minimum_height,
534                                                   gint      *natural_height)
535 {
536   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
537
538   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
539     ->get_preferred_height (widget, minimum_height, natural_height);
540
541   if (window->priv->menubar != NULL)
542     {
543       gint menubar_min_height, menubar_nat_height;
544
545       gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
546       *minimum_height += menubar_min_height;
547       *natural_height += menubar_nat_height;
548     }
549 }
550
551 static void
552 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
553                                                             gint       width,
554                                                             gint      *minimum_height,
555                                                             gint      *natural_height)
556 {
557   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
558
559   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
560     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
561
562   if (window->priv->menubar != NULL)
563     {
564       gint menubar_min_height, menubar_nat_height;
565
566       gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
567       *minimum_height += menubar_min_height;
568       *natural_height += menubar_nat_height;
569     }
570 }
571
572 static void
573 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
574                                                  gint      *minimum_width,
575                                                  gint      *natural_width)
576 {
577   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
578
579   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
580     ->get_preferred_width (widget, minimum_width, natural_width);
581
582   if (window->priv->menubar != NULL)
583     {
584       gint menubar_min_width, menubar_nat_width;
585
586       gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
587       *minimum_width = MAX (*minimum_width, menubar_min_width);
588       *natural_width = MAX (*natural_width, menubar_nat_width);
589     }
590 }
591
592 static void
593 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
594                                                             gint       height,
595                                                             gint      *minimum_width,
596                                                             gint      *natural_width)
597 {
598   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
599
600   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
601     ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
602
603   if (window->priv->menubar != NULL)
604     {
605       gint menubar_min_width, menubar_nat_width;
606
607       gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
608       *minimum_width = MAX (*minimum_width, menubar_min_width);
609       *natural_width = MAX (*natural_width, menubar_nat_width);
610     }
611 }
612
613 static void
614 gtk_application_window_real_size_allocate (GtkWidget     *widget,
615                                            GtkAllocation *allocation)
616 {
617   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
618
619   if (window->priv->menubar != NULL)
620     {
621       GtkAllocation menubar_allocation = *allocation;
622       gint menubar_min_height, menubar_nat_height;
623       GtkWidget *child;
624
625       gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
626
627       menubar_allocation.height = menubar_min_height;
628       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
629
630       child = gtk_bin_get_child (GTK_BIN (window));
631       if (child != NULL && gtk_widget_get_visible (child))
632         {
633           GtkAllocation child_allocation = *allocation;
634           gint border_width;
635
636           child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
637
638           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
639           child_allocation.x += border_width;
640           child_allocation.y += border_width + menubar_min_height;
641           child_allocation.width -= border_width * 2;
642           child_allocation.height -= border_width * 2 - menubar_min_height;
643           gtk_widget_size_allocate (child, &child_allocation);
644         }
645
646       gtk_widget_set_allocation (widget, allocation);
647     }
648   else
649     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
650       ->size_allocate (widget, allocation);
651 }
652
653 static void
654 gtk_application_window_real_realize (GtkWidget *widget)
655 {
656   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
657   GtkApplication *application;
658   GtkSettings *settings;
659
660   application = gtk_window_get_application (GTK_WINDOW (window));
661   settings = gtk_widget_get_settings (widget);
662
663   g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
664                     G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
665   g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
666                     G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
667
668   if (window->priv->muxer == NULL)
669     {
670       GActionMuxer *muxer;
671
672       muxer = g_action_muxer_new ();
673       g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
674       g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
675       window->priv->muxer = G_ACTION_OBSERVABLE (muxer);
676     }
677
678   gtk_application_window_update_shell_shows_app_menu (window, settings);
679   gtk_application_window_update_shell_shows_menubar (window, settings);
680   gtk_application_window_update_menubar (window);
681   gtk_application_window_update_accels (window);
682
683   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
684     ->realize (widget);
685
686 #ifdef GDK_WINDOWING_X11
687   {
688     GdkWindow *gdkwindow;
689
690     gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
691
692     if (GDK_IS_X11_WINDOW (gdkwindow))
693       {
694         const gchar *unique_id;
695         const gchar *app_id;
696
697         gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_OBJECT_PATH", window->priv->object_path);
698
699         unique_id = g_dbus_connection_get_unique_name (window->priv->session);
700         gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_UNIQUE_NAME", unique_id);
701
702         app_id = g_application_get_application_id (G_APPLICATION (application));
703         gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_APPLICATION_ID", app_id);
704       }
705   }
706 #endif
707 }
708
709 static void
710 gtk_application_window_real_unrealize (GtkWidget *widget)
711 {
712   GtkSettings *settings;
713
714   settings = gtk_widget_get_settings (widget);
715
716   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
717   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
718
719   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
720     ->unrealize (widget);
721 }
722
723 gboolean
724 gtk_application_window_publish (GtkApplicationWindow *window,
725                                 GDBusConnection      *session,
726                                 const gchar          *object_path)
727 {
728   g_assert (window->priv->session == NULL);
729   g_assert (window->priv->export_id == 0);
730   g_assert (window->priv->object_path == NULL);
731
732   window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
733                                                                    G_ACTION_GROUP (window->priv->actions),
734                                                                    NULL);
735
736   if (window->priv->export_id == 0)
737     return FALSE;
738
739   window->priv->session = session;
740   window->priv->object_path = g_strdup (object_path);
741
742   return TRUE;
743 }
744
745 void
746 gtk_application_window_unpublish (GtkApplicationWindow *window)
747 {
748   g_assert (window->priv->session != NULL);
749   g_assert (window->priv->export_id != 0);
750   g_assert (window->priv->object_path != NULL);
751
752   g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
753   window->priv->session = NULL;
754   window->priv->export_id = 0;
755
756   g_free (window->priv->object_path);
757   window->priv->object_path = NULL;
758 }
759
760 static void
761 gtk_application_window_real_map (GtkWidget *widget)
762 {
763   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
764
765   /* XXX could eliminate this by tweaking gtk_window_map */
766   if (window->priv->menubar)
767     gtk_widget_map (window->priv->menubar);
768
769   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
770     ->map (widget);
771 }
772
773 static void
774 gtk_application_window_real_forall_internal (GtkContainer *container,
775                                              gboolean      include_internal,
776                                              GtkCallback   callback,
777                                              gpointer      user_data)
778 {
779   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
780
781   if (window->priv->menubar)
782     callback (window->priv->menubar, user_data);
783
784   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
785     ->forall (container, include_internal, callback, user_data);
786 }
787
788
789 static void
790 gtk_application_window_get_property (GObject    *object,
791                                      guint       prop_id,
792                                      GValue     *value,
793                                      GParamSpec *pspec)
794 {
795   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
796
797   switch (prop_id)
798     {
799     case PROP_SHOW_MENUBAR:
800       g_value_set_boolean (value, window->priv->show_menubar);
801       break;
802
803     default:
804       g_assert_not_reached ();
805     }
806 }
807
808 static void
809 gtk_application_window_set_property (GObject      *object,
810                                      guint         prop_id,
811                                      const GValue *value,
812                                      GParamSpec   *pspec)
813 {
814   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
815
816   switch (prop_id)
817     {
818     case PROP_SHOW_MENUBAR:
819       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
820       break;
821
822     default:
823       g_assert_not_reached ();
824     }
825 }
826
827 static void
828 gtk_application_window_dispose (GObject *object)
829 {
830   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
831
832   if (window->priv->menubar)
833     {
834       gtk_widget_unparent (window->priv->menubar);
835       window->priv->menubar = NULL;
836     }
837
838   free_accel_closures (window);
839
840   g_clear_object (&window->priv->app_menu_section);
841   g_clear_object (&window->priv->menubar_section);
842   g_clear_object (&window->priv->actions);
843   g_clear_object (&window->priv->accels);
844   g_clear_object (&window->priv->muxer);
845
846   G_OBJECT_CLASS (gtk_application_window_parent_class)
847     ->dispose (object);
848 }
849
850 static void
851 gtk_application_window_init (GtkApplicationWindow *window)
852 {
853   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
854
855   window->priv->actions = gtk_application_window_actions_new (window);
856   window->priv->app_menu_section = g_menu_new ();
857   window->priv->menubar_section = g_menu_new ();
858   window->priv->accels = gtk_accel_group_new ();
859   gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
860
861   /* window->priv->actions is the one and only ref on the group, so when
862    * we dispose, the action group will die, disconnecting all signals.
863    */
864   g_signal_connect_swapped (window->priv->actions, "action-added",
865                             G_CALLBACK (g_action_group_action_added), window);
866   g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
867                             G_CALLBACK (g_action_group_action_enabled_changed), window);
868   g_signal_connect_swapped (window->priv->actions, "action-state-changed",
869                             G_CALLBACK (g_action_group_action_state_changed), window);
870   g_signal_connect_swapped (window->priv->actions, "action-removed",
871                             G_CALLBACK (g_action_group_action_removed), window);
872 }
873
874 static void
875 gtk_application_window_class_init (GtkApplicationWindowClass *class)
876 {
877   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
878   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
879   GObjectClass *object_class = G_OBJECT_CLASS (class);
880
881   container_class->forall = gtk_application_window_real_forall_internal;
882   widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
883   widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
884   widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
885   widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
886   widget_class->size_allocate = gtk_application_window_real_size_allocate;
887   widget_class->realize = gtk_application_window_real_realize;
888   widget_class->unrealize = gtk_application_window_real_unrealize;
889   widget_class->map = gtk_application_window_real_map;
890   object_class->get_property = gtk_application_window_get_property;
891   object_class->set_property = gtk_application_window_set_property;
892   object_class->dispose = gtk_application_window_dispose;
893
894   /**
895    * GtkApplicationWindow:show-menubar:
896    *
897    * If this property is %TRUE, the window will display a menubar
898    * that includes the app menu and menubar, unless these are
899    * shown by the desktop shell. See g_application_set_app_menu()
900    * and g_application_set_menubar().
901    *
902    * If %FALSE, the window will not display a menubar, regardless
903    * of whether the desktop shell is showing the menus or not.
904    */
905   gtk_application_window_properties[PROP_SHOW_MENUBAR] =
906     g_param_spec_boolean ("show-menubar",
907                           P_("Show a menubar"),
908                           P_("TRUE if the window should show a "
909                              "menubar at the top of the window"),
910                           TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
911   g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
912   g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
913 }
914
915 /**
916  * gtk_application_window_new:
917  * @application: a #GtkApplication
918  *
919  * Creates a new #GtkApplicationWindow.
920  *
921  * Returns: a newly created #GtkApplicationWindow
922  *
923  * Since: 3.4
924  */
925 GtkWidget *
926 gtk_application_window_new (GtkApplication *application)
927 {
928   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
929
930   return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
931                        "application", application,
932                        NULL);
933 }
934
935 /**
936  * gtk_application_window_get_show_menubar:
937  * @window: a #GtkApplicationWindow
938  *
939  * Returns whether the window will display a menubar for the app menu
940  * and menubar as needed.
941  *
942  * Returns: %TRUE if @window will display a menubar when needed
943  *
944  * Since: 3.4
945  */
946 gboolean
947 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
948 {
949   return window->priv->show_menubar;
950 }
951
952 /**
953  * gtk_application_window_set_show_menubar:
954  * @window: a #GtkApplicationWindow
955  * @show_menubar: whether to show a menubar when needed
956  *
957  * Sets whether the window will display a menubar for the app menu
958  * and menubar as needed.
959  *
960  * Since: 3.4
961  */
962 void
963 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
964                                          gboolean              show_menubar)
965 {
966   g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
967
968   show_menubar = !!show_menubar;
969
970   if (window->priv->show_menubar != show_menubar)
971     {
972       window->priv->show_menubar = show_menubar;
973
974       gtk_application_window_update_menubar (window);
975
976       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
977     }
978 }