gtkapplicationwindow.c 37.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2011 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gtkapplicationwindow.h"

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 34 35 36 37
#include <gdk/gdk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#endif

38 39 40 41
#ifdef HAVE_GIO_UNIX
#include <gio/gdesktopappinfo.h>
#endif

Matthias Clasen's avatar
Matthias Clasen committed
42 43 44 45 46
/**
 * SECTION:gtkapplicationwindow
 * @title: GtkApplicationWindow
 * @short_description: GtkWindow subclass with GtkApplication support
 *
47 48 49
 * GtkApplicationWindow is a #GtkWindow subclass that offers some
 * extra functionality for better integration with #GtkApplication
 * features.  Notably, it can handle both the application menu as well
50 51
 * as the menubar. See gtk_application_set_app_menu() and
 * gtk_application_set_menubar().
52 53 54 55 56 57 58 59
 *
 * This class implements the #GActionGroup and #GActionMap interfaces,
 * to let you add window-specific actions that will be exported by the
 * associated #GtkApplication, together with its application-wide
 * actions.  Window-specific actions are prefixed with the "win."
 * prefix and application-wide actions are prefixed with the "app."
 * prefix.  Actions must be addressed with the prefixed name when
 * referring to them from a #GMenuModel.
Matthias Clasen's avatar
Matthias Clasen committed
60
 *
61 62 63 64
 * Note that widgets that are placed inside a GtkApplicationWindow
 * can also activate these actions, if they implement the
 * GtkActionable interface.
 *
65 66 67 68
 * As with #GtkApplication, the GDK lock will be acquired when
 * processing actions arriving from other processes and should therefore
 * be held when activating actions locally (if GDK threads are enabled).
 *
69 70 71 72 73 74 75 76 77 78
 * The settings #GtkSettings:gtk-shell-shows-app-menu and
 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
 * desktop environment is showing the application menu and menubar
 * models outside the application as part of the desktop shell.
 * For instance, on OS X, both menus will be displayed remotely;
 * on Windows neither will be. gnome-shell (starting with version 3.4)
 * will display the application menu, but not the menubar.
 *
 * If the desktop environment does not display the menubar, then
 * #GApplicationWindow will automatically show a #GtkMenubar for it.
79 80
 * (see the #GtkApplication docs for some screenshots of how this
 * looks on different platforms).
81 82
 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
 * property. If the desktop environment does not display the application
83
 * menu, then it will automatically be included in the menubar.
Matthias Clasen's avatar
Matthias Clasen committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 *
 * <example><title>A GtkApplicationWindow with a menubar</title>
 * <programlisting><![CDATA[
 * app = gtk_application_new ();
 *
 * builder = gtk_builder_new ();
 * gtk_builder_add_from_string (builder,
 *     "<interface>"
 *     "  <menu id='menubar'>"
 *     "    <submenu label='_Edit'>"
 *     "      <item label='_Copy' action='win.copy'/>"
 *     "      <item label='_Paste' action='win.paste'/>"
 *     "    </submenu>"
 *     "  </menu>"
 *     "</interface>");
99 100
 * gtk_application_set_menubar (G_APPLICATION (app),
 *                              G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
Matthias Clasen's avatar
Matthias Clasen committed
101 102 103 104 105 106 107 108
 * g_object_unref (builder);
 *
 * ...
 *
 * window = gtk_application_window_new (app);
 * ]]>
 * </programlisting>
 * </example>
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 *
 * The XML format understood by #GtkBuilder for #GMenuModel consists
 * of a toplevel <tag class="starttag">menu</tag> element, which contains
 * one or more <tag class="starttag">item</tag> elements. Each
 * <tag class="starttag">item</tag> element contains
 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
 * elements with a mandatory name attribute.
 * <tag class="starttag">link</tag> elements have the same content
 * model as <tag class="starttag">menu</tag>.
 *
 * Attribute values can be translated using gettext, like other #GtkBuilder
 * content. <tag class="starttag">attribute</tag> elements can be marked for
 * translation with a <literal>translatable="yes"</literal> attribute.
 * It is also possible to specify message context and translator comments,
 * using the context and comments attributes. To make use of this, the
 * #GtkBuilder must have been given the gettext domain to use.
Matthias Clasen's avatar
Matthias Clasen committed
125
 */
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
typedef struct
{
  GSimpleActionGroup parent_instance;
  GtkWindow *window;
} GtkApplicationWindowActions;

static GType gtk_application_window_actions_get_type   (void);
static void  gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
                         G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))

static void
gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
                                                     const gchar        *action_name,
                                                     GVariant           *parameter,
                                                     GVariant           *platform_data)
{
  GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
  GApplication *application;
  GApplicationClass *class;

  application = G_APPLICATION (gtk_window_get_application (actions->window));
  class = G_APPLICATION_GET_CLASS (application);

  class->before_emit (application, platform_data);
  g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
  class->after_emit (application, platform_data);
}

static void
gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
                                                         const gchar        *action_name,
                                                         GVariant           *value,
                                                         GVariant           *platform_data)
{
  GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
  GApplication *application;
  GApplicationClass *class;

  application = G_APPLICATION (gtk_window_get_application (actions->window));
  class = G_APPLICATION_GET_CLASS (application);

  class->before_emit (application, platform_data);
  g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
  class->after_emit (application, platform_data);
}

static void
gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
{
}

static void
gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
{
  iface->activate_action_full = gtk_application_window_actions_activate_action_full;
  iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
}

static void
gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
{
}

static GSimpleActionGroup *
gtk_application_window_actions_new (GtkApplicationWindow *window)
{
  GtkApplicationWindowActions *actions;

  actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
  actions->window = GTK_WINDOW (window);

  return G_SIMPLE_ACTION_GROUP (actions);
}

/* Now onto GtkApplicationWindow... */

205 206
struct _GtkApplicationWindowPrivate
{
207
  GSimpleActionGroup *actions;
208
  GActionObservable *muxer;
209
  gboolean muxer_initialised;
210
  GtkWidget *menubar;
211
  GtkAccelGroup *accels;
212
  GSList *accel_closures;
213

214 215 216
  GMenu *app_menu_section;
  GMenu *menubar_section;
  gboolean show_menubar;
217 218 219 220

  GDBusConnection *session;
  gchar           *object_path;
  guint            export_id;
221 222
};

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static void
gtk_application_window_update_menubar (GtkApplicationWindow *window)
{
  gboolean should_have_menubar;
  gboolean have_menubar;

  have_menubar = window->priv->menubar != NULL;

  should_have_menubar = window->priv->show_menubar &&
                        (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
                         g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));

  if (have_menubar && !should_have_menubar)
    {
      gtk_widget_unparent (window->priv->menubar);
      window->priv->menubar = NULL;

      gtk_widget_queue_resize (GTK_WIDGET (window));
    }

  if (!have_menubar && should_have_menubar)
    {
      GMenu *combined;

      combined = g_menu_new ();
      g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
      g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));

251
      window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
252 253 254 255 256 257 258
      gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
      gtk_widget_show_all (window->priv->menubar);
      g_object_unref (combined);

      gtk_widget_queue_resize (GTK_WIDGET (window));
    }
}
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static gchar *
gtk_application_window_get_app_desktop_name ()
{
  gchar *retval = NULL;

#ifdef HAVE_GIO_UNIX
  GDesktopAppInfo *app_info;
  const gchar *app_name = NULL;
  gchar *desktop_file;

  desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
  app_info = g_desktop_app_info_new (desktop_file);
  g_free (desktop_file);

  if (app_info != NULL)
    app_name = g_app_info_get_name (G_APP_INFO (app_info));

  if (app_name != NULL)
    retval = g_strdup (app_name);

  g_clear_object (&app_info);
#endif /* HAVE_GIO_UNIX */

  return retval;
}

286
static void
287 288
gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
                                                    GtkSettings          *settings)
289
{
290
  gboolean shown_by_shell;
291

292
  g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
293 294 295 296 297 298 299 300 301 302 303 304 305 306

  if (shown_by_shell)
    {
      /* the shell shows it, so don't show it locally */
      if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
        g_menu_remove (window->priv->app_menu_section, 0);
    }
  else
    {
      /* the shell does not show it, so make sure we show it */
      if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
        {
          GMenuModel *app_menu;

307
          app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
308 309

          if (app_menu != NULL)
310
            {
311 312
              const gchar *app_name;
              gchar *name;
313

314 315
              app_name = g_get_application_name ();
              if (app_name != g_get_prgname ())
316
                {
317 318
                  /* the app has set its application name, use it */
                  name = g_strdup (app_name);
319
                }
320 321 322 323 324 325 326 327
              else
                {
                  /* get the name from .desktop file */
                  name = gtk_application_window_get_app_desktop_name ();
                  if (name == NULL)
                    name = g_strdup (_("Application"));
                }

328
              g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
329
              g_free (name);
330
            }
331 332 333 334 335
        }
    }
}

static void
336 337
gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
                                                   GtkSettings          *settings)
338 339 340
{
  gboolean shown_by_shell;

341
  g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
342 343 344 345 346 347 348 349

  if (shown_by_shell)
    {
      /* the shell shows it, so don't show it locally */
      if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
        g_menu_remove (window->priv->menubar_section, 0);
    }
  else
350
    {
351 352 353 354 355
      /* the shell does not show it, so make sure we show it */
      if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
        {
          GMenuModel *menubar;

356
          menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
357 358 359 360

          if (menubar != NULL)
            g_menu_append_section (window->priv->menubar_section, NULL, menubar);
        }
361
    }
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
typedef struct {
  GClosure closure;
  gchar *action_name;
  GVariant *parameter;
} AccelClosure;

static void
accel_activate (GClosure     *closure,
                GValue       *return_value,
                guint         n_param_values,
                const GValue *param_values,
                gpointer      invocation_hint,
                gpointer      marshal_data)
{
  AccelClosure *aclosure = (AccelClosure*)closure;
  GActionGroup *actions;

  actions = G_ACTION_GROUP (closure->data);
  if (g_action_group_get_action_enabled (actions, aclosure->action_name))
    {
       g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);

      /* we handled the accelerator */
      g_value_set_boolean (return_value, TRUE);
    }
}

static void
free_accel_closures (GtkApplicationWindow *window)
{
  GSList *l;

  for (l = window->priv->accel_closures; l; l = l->next)
    {
       AccelClosure *closure = l->data;

       gtk_accel_group_disconnect (window->priv->accels, &closure->closure);

       g_object_unref (closure->closure.data);
       if (closure->parameter)
         g_variant_unref (closure->parameter);
       g_free (closure->action_name);
       g_closure_invalidate (&closure->closure);
       g_closure_unref (&closure->closure);
    }
  g_slist_free (window->priv->accel_closures);
  window->priv->accel_closures = NULL;
}

typedef struct {
  GtkApplicationWindow *window;
  GActionGroup *actions;
} AccelData;

/* Hack. We iterate over the accel map instead of the actions,
 * in order to pull the parameters out of accel map entries
 */
static void
add_accel_closure (gpointer         data,
                   const gchar     *accel_path,
                   guint            accel_key,
                   GdkModifierType  accel_mods,
                   gboolean         changed)
{
  AccelData *d = data;
  GtkApplicationWindow *window = d->window;
  GActionGroup *actions = d->actions;
  const gchar *path;
  const gchar *p;
  gchar *action_name;
  GVariant *parameter;
  AccelClosure *closure;

  if (accel_key == 0)
    return;

440
  if (!g_str_has_prefix (accel_path, "<GAction>/"))
441 442
    return;

443
  path = accel_path + strlen ("<GAction>/");
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
  p = strchr (path, '/');
  if (p)
    {
      action_name = g_strndup (path, p - path);
      parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
      if (!parameter)
        g_warning ("Failed to parse parameter from '%s'\n", accel_path);
    }
  else
    {
      action_name = g_strdup (path);
      parameter = NULL;
    }

  if (g_action_group_has_action (actions, action_name))
    {
      closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
      g_closure_set_marshal (&closure->closure, accel_activate);

      closure->action_name = g_strdup (action_name);
      closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;

      window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
      g_closure_sink (&closure->closure);

      gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
    }

  g_free (action_name);
}

static void
gtk_application_window_update_accels (GtkApplicationWindow *window)
{
  AccelData data;

  free_accel_closures (window);

  data.window = window;
483
  data.actions = G_ACTION_GROUP (window->priv->muxer);
484 485 486 487

  gtk_accel_map_foreach (&data, add_accel_closure);
}

488 489 490 491 492 493 494
static void
gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
                                                     GParamSpec *pspec,
                                                     gpointer    user_data)
{
  GtkApplicationWindow *window = user_data;

495
  gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
496 497 498 499 500 501 502 503 504 505
  gtk_application_window_update_menubar (window);
}

static void
gtk_application_window_shell_shows_menubar_changed (GObject    *object,
                                                    GParamSpec *pspec,
                                                    gpointer    user_data)
{
  GtkApplicationWindow *window = user_data;

506
  gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
507 508 509
  gtk_application_window_update_menubar (window);
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static gchar **
gtk_application_window_list_actions (GActionGroup *group)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);

  return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
}

static gboolean
gtk_application_window_query_action (GActionGroup        *group,
                                     const gchar         *action_name,
                                     gboolean            *enabled,
                                     const GVariantType **parameter_type,
                                     const GVariantType **state_type,
                                     GVariant           **state_hint,
                                     GVariant           **state)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);

  return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
                                      action_name, enabled, parameter_type, state_type, state_hint, state);
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
static void
gtk_application_window_activate_action (GActionGroup *group,
                                        const gchar  *action_name,
                                        GVariant     *parameter)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);

  return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
}

static void
gtk_application_window_change_action_state (GActionGroup *group,
                                            const gchar  *action_name,
                                            GVariant     *state)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);

  return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
}

553
static GAction *
Matthias Clasen's avatar
Matthias Clasen committed
554
gtk_application_window_lookup_action (GActionMap  *action_map,
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
                                      const gchar *action_name)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);

  return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
}

static void
gtk_application_window_add_action (GActionMap *action_map,
                                   GAction    *action)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);

  g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
}

static void
gtk_application_window_remove_action (GActionMap  *action_map,
                                      const gchar *action_name)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);

  g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
}

static void
gtk_application_window_group_iface_init (GActionGroupInterface *iface)
{
  iface->list_actions = gtk_application_window_list_actions;
  iface->query_action = gtk_application_window_query_action;
585 586
  iface->activate_action = gtk_application_window_activate_action;
  iface->change_action_state = gtk_application_window_change_action_state;
587 588 589 590 591 592 593 594 595 596 597 598 599
}

static void
gtk_application_window_map_iface_init (GActionMapInterface *iface)
{
  iface->lookup_action = gtk_application_window_lookup_action;
  iface->add_action = gtk_application_window_add_action;
  iface->remove_action = gtk_application_window_remove_action;
}

G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
600 601 602

enum {
  PROP_0,
603
  PROP_SHOW_MENUBAR,
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  N_PROPS
};
static GParamSpec *gtk_application_window_properties[N_PROPS];

static void
gtk_application_window_real_get_preferred_height (GtkWidget *widget,
                                                  gint      *minimum_height,
                                                  gint      *natural_height)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->get_preferred_height (widget, minimum_height, natural_height);

  if (window->priv->menubar != NULL)
    {
      gint menubar_min_height, menubar_nat_height;

622
      gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
      *minimum_height += menubar_min_height;
      *natural_height += menubar_nat_height;
    }
}

static void
gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
                                                            gint       width,
                                                            gint      *minimum_height,
                                                            gint      *natural_height)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);

  if (window->priv->menubar != NULL)
    {
      gint menubar_min_height, menubar_nat_height;

643
      gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
      *minimum_height += menubar_min_height;
      *natural_height += menubar_nat_height;
    }
}

static void
gtk_application_window_real_get_preferred_width (GtkWidget *widget,
                                                 gint      *minimum_width,
                                                 gint      *natural_width)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->get_preferred_width (widget, minimum_width, natural_width);

  if (window->priv->menubar != NULL)
    {
      gint menubar_min_width, menubar_nat_width;

663
      gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
664 665 666 667 668 669 670 671 672 673 674 675
      *minimum_width = MAX (*minimum_width, menubar_min_width);
      *natural_width = MAX (*natural_width, menubar_nat_width);
    }
}

static void
gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
                                                            gint       height,
                                                            gint      *minimum_width,
                                                            gint      *natural_width)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
676 677 678 679 680 681
  gint menubar_height;

  if (window->priv->menubar != NULL)
    gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
  else
    menubar_height = 0;
682 683

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
684
    ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
685 686 687 688 689

  if (window->priv->menubar != NULL)
    {
      gint menubar_min_width, menubar_nat_width;

690
      gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
691 692 693 694 695 696 697 698 699 700 701 702 703 704
      *minimum_width = MAX (*minimum_width, menubar_min_width);
      *natural_width = MAX (*natural_width, menubar_nat_width);
    }
}

static void
gtk_application_window_real_size_allocate (GtkWidget     *widget,
                                           GtkAllocation *allocation)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);

  if (window->priv->menubar != NULL)
    {
      GtkAllocation menubar_allocation = *allocation;
705
      gint menubar_height;
706 707
      GtkWidget *child;

708
      _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
709

710
      gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
711

712
      menubar_allocation.height = menubar_height;
713
      gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
714 715 716 717 718 719 720 721 722

      child = gtk_bin_get_child (GTK_BIN (window));
      if (child != NULL && gtk_widget_get_visible (child))
        {
          GtkAllocation child_allocation = *allocation;
          gint border_width;

          border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
          child_allocation.x += border_width;
723 724 725 726
          child_allocation.y += border_width + menubar_height;
          child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
          child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);

727 728 729 730 731 732 733 734
          gtk_widget_size_allocate (child, &child_allocation);
        }
    }
  else
    GTK_WIDGET_CLASS (gtk_application_window_parent_class)
      ->size_allocate (widget, allocation);
}

735 736 737 738
static void
gtk_application_window_real_realize (GtkWidget *widget)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
739
  GtkApplication *application;
740
  GtkSettings *settings;
741

742
  application = gtk_window_get_application (GTK_WINDOW (window));
743 744 745 746 747 748 749
  settings = gtk_widget_get_settings (widget);

  g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
                    G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
  g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
                    G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);

750
  if (!window->priv->muxer_initialised)
751
    {
752 753 754
      g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
      g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
      window->priv->muxer_initialised = TRUE;
755 756
    }

757 758
  gtk_application_window_update_shell_shows_app_menu (window, settings);
  gtk_application_window_update_shell_shows_menubar (window, settings);
759
  gtk_application_window_update_menubar (window);
760
  gtk_application_window_update_accels (window);
761 762 763

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->realize (widget);
764 765 766 767 768 769 770 771 772

#ifdef GDK_WINDOWING_X11
  {
    GdkWindow *gdkwindow;

    gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));

    if (GDK_IS_X11_WINDOW (gdkwindow))
      {
773 774
        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
                                          g_application_get_application_id (G_APPLICATION (application)));
775

776 777
        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
                                          g_dbus_connection_get_unique_name (window->priv->session));
778

779 780
        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
                                          gtk_application_get_dbus_object_path (application));
781

782 783 784 785 786 787 788 789
        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
                                          window->priv->object_path);

        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
                                          gtk_application_get_app_menu_object_path (application));

        gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
                                          gtk_application_get_menubar_object_path (application));
790 791 792
      }
  }
#endif
793 794
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808
static void
gtk_application_window_real_unrealize (GtkWidget *widget)
{
  GtkSettings *settings;

  settings = gtk_widget_get_settings (widget);

  g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
  g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->unrealize (widget);
}

809 810 811 812 813 814 815 816 817
gboolean
gtk_application_window_publish (GtkApplicationWindow *window,
                                GDBusConnection      *session,
                                const gchar          *object_path)
{
  g_assert (window->priv->session == NULL);
  g_assert (window->priv->export_id == 0);
  g_assert (window->priv->object_path == NULL);

818 819 820
  window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
                                                                   G_ACTION_GROUP (window->priv->actions),
                                                                   NULL);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

  if (window->priv->export_id == 0)
    return FALSE;

  window->priv->session = session;
  window->priv->object_path = g_strdup (object_path);

  return TRUE;
}

void
gtk_application_window_unpublish (GtkApplicationWindow *window)
{
  g_assert (window->priv->session != NULL);
  g_assert (window->priv->export_id != 0);
  g_assert (window->priv->object_path != NULL);

  g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
  window->priv->session = NULL;
  window->priv->export_id = 0;

  g_free (window->priv->object_path);
  window->priv->object_path = NULL;
}

846 847 848 849 850
static void
gtk_application_window_real_map (GtkWidget *widget)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);

851
  /* XXX could eliminate this by tweaking gtk_window_map */
852
  if (window->priv->menubar)
853
    gtk_widget_map (window->priv->menubar);
854 855 856 857 858 859 860 861 862 863 864 865 866 867

  GTK_WIDGET_CLASS (gtk_application_window_parent_class)
    ->map (widget);
}

static void
gtk_application_window_real_forall_internal (GtkContainer *container,
                                             gboolean      include_internal,
                                             GtkCallback   callback,
                                             gpointer      user_data)
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);

  if (window->priv->menubar)
868
    callback (window->priv->menubar, user_data);
869 870 871 872 873 874 875

  GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
    ->forall (container, include_internal, callback, user_data);
}


static void
876 877 878 879
gtk_application_window_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
880 881 882 883 884
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);

  switch (prop_id)
    {
885
    case PROP_SHOW_MENUBAR:
886
      g_value_set_boolean (value, window->priv->show_menubar);
887 888 889 890 891 892 893 894
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
895 896 897 898
gtk_application_window_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
899 900 901 902 903
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);

  switch (prop_id)
    {
904 905
    case PROP_SHOW_MENUBAR:
      gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
906 907 908 909 910 911 912 913
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
914
gtk_application_window_dispose (GObject *object)
915 916 917
{
  GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);

918 919 920 921 922
  if (window->priv->menubar)
    {
      gtk_widget_unparent (window->priv->menubar);
      window->priv->menubar = NULL;
    }
923

924 925
  free_accel_closures (window);

926 927
  g_clear_object (&window->priv->app_menu_section);
  g_clear_object (&window->priv->menubar_section);
928
  g_clear_object (&window->priv->actions);
929
  g_clear_object (&window->priv->accels);
930
  g_clear_object (&window->priv->muxer);
931 932

  G_OBJECT_CLASS (gtk_application_window_parent_class)
933
    ->dispose (object);
934 935 936 937 938 939
}

static void
gtk_application_window_init (GtkApplicationWindow *window)
{
  window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
940

941
  window->priv->actions = gtk_application_window_actions_new (window);
942 943
  window->priv->app_menu_section = g_menu_new ();
  window->priv->menubar_section = g_menu_new ();
944 945
  window->priv->accels = gtk_accel_group_new ();
  gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
946 947

  /* window->priv->actions is the one and only ref on the group, so when
948
   * we dispose, the action group will die, disconnecting all signals.
949 950 951 952 953 954 955 956 957
   */
  g_signal_connect_swapped (window->priv->actions, "action-added",
                            G_CALLBACK (g_action_group_action_added), window);
  g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
                            G_CALLBACK (g_action_group_action_enabled_changed), window);
  g_signal_connect_swapped (window->priv->actions, "action-state-changed",
                            G_CALLBACK (g_action_group_action_state_changed), window);
  g_signal_connect_swapped (window->priv->actions, "action-removed",
                            G_CALLBACK (g_action_group_action_removed), window);
958 959

  window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
}

static void
gtk_application_window_class_init (GtkApplicationWindowClass *class)
{
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  container_class->forall = gtk_application_window_real_forall_internal;
  widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
  widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
  widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
  widget_class->size_allocate = gtk_application_window_real_size_allocate;
975
  widget_class->realize = gtk_application_window_real_realize;
976
  widget_class->unrealize = gtk_application_window_real_unrealize;
977 978 979
  widget_class->map = gtk_application_window_real_map;
  object_class->get_property = gtk_application_window_get_property;
  object_class->set_property = gtk_application_window_set_property;
980
  object_class->dispose = gtk_application_window_dispose;
981

982 983 984 985 986
  /**
   * GtkApplicationWindow:show-menubar:
   *
   * If this property is %TRUE, the window will display a menubar
   * that includes the app menu and menubar, unless these are
987 988
   * shown by the desktop shell. See gtk_application_set_app_menu()
   * and gtk_application_set_menubar().
989 990 991 992
   *
   * If %FALSE, the window will not display a menubar, regardless
   * of whether the desktop shell is showing the menus or not.
   */
993 994 995
  gtk_application_window_properties[PROP_SHOW_MENUBAR] =
    g_param_spec_boolean ("show-menubar",
                          P_("Show a menubar"),
996 997
                          P_("TRUE if the window should show a "
                             "menubar at the top of the window"),
998
                          TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
999 1000 1001 1002
  g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
  g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
/**
 * gtk_application_window_new:
 * @application: a #GtkApplication
 *
 * Creates a new #GtkApplicationWindow.
 *
 * Returns: a newly created #GtkApplicationWindow
 *
 * Since: 3.4
 */
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
GtkWidget *
gtk_application_window_new (GtkApplication *application)
{
  g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);

  return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
                       "application", application,
                       NULL);
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/**
 * gtk_application_window_get_show_menubar:
 * @window: a #GtkApplicationWindow
 *
 * Returns whether the window will display a menubar for the app menu
 * and menubar as needed.
 *
 * Returns: %TRUE if @window will display a menubar when needed
 *
 * Since: 3.4
 */
1034
gboolean
1035
gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1036
{
1037
  return window->priv->show_menubar;
1038 1039
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
/**
 * gtk_application_window_set_show_menubar:
 * @window: a #GtkApplicationWindow
 * @show_menubar: whether to show a menubar when needed
 *
 * Sets whether the window will display a menubar for the app menu
 * and menubar as needed.
 *
 * Since: 3.4
 */
1050 1051 1052
void
gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
                                         gboolean              show_menubar)
1053
{
1054
  g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1055

1056
  show_menubar = !!show_menubar;
1057

1058
  if (window->priv->show_menubar != show_menubar)
1059
    {
1060 1061 1062
      window->priv->show_menubar = show_menubar;

      gtk_application_window_update_menubar (window);
1063

1064
      g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1065 1066
    }
}
1067 1068

GSimpleActionObserver *
1069 1070 1071
gtk_application_window_create_observer (GtkApplicationWindow *window,
                                        const gchar          *action_name,
                                        GVariant             *target)
1072 1073 1074 1075 1076
{
  g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);

  return g_simple_action_observer_new (window->priv->muxer, action_name, target);
}
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

GActionObservable *
gtk_application_window_get_observable (GtkApplicationWindow *window)
{
  return G_ACTION_OBSERVABLE (window->priv->muxer);
}

GtkAccelGroup *
gtk_application_window_get_accel_group (GtkApplicationWindow *window)
{
  return window->priv->accels;
}