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