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