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