1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 251 252 253 254 255 256 257 258 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 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 440 441 442 443 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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
|
/*
* 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include "gtkapplicationwindow.h"
#include "gtkapplicationprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkwindowprivate.h"
#include "gtkpopovermenubar.h"
#include "gtksettings.h"
#include "deprecated/gtkshortcutswindowprivate.h"
#include "gtktooltipprivate.h"
#include <glib/gi18n-lib.h>
/**
* GtkApplicationWindow:
*
* A `GtkWindow` subclass that integrates with `GtkApplication`.
*
* Notably, `GtkApplicationWindow` can handle an application menubar.
*
* This class implements the [iface@Gio.ActionGroup] and [iface@Gio.ActionMap]
* interfaces, to let you add window-specific actions that will be exported
* by the associated [class@Gtk.Application], 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 menu model.
*
* Note that widgets that are placed inside a `GtkApplicationWindow`
* can also activate these actions, if they implement the
* [iface@Gtk.Actionable] interface.
*
* The settings [property@Gtk.Settings:gtk-shell-shows-app-menu] and
* [property@Gtk.Settings: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.
*
* If the desktop environment does not display the menubar, it can be shown in
* the `GtkApplicationWindow` by setting the
* [property@Gtk.ApplicationWindow:show-menubar] property to true. If the
* desktop environment does not display the application menu, then it will
* automatically be included in the menubar or in the window’s client-side
* decorations.
*
* See [class@Gtk.PopoverMenu] for information about the XML language
* used by `GtkBuilder` for menu models.
*
* See also: [method@Gtk.Application.set_menubar].
*
* ## A GtkApplicationWindow with a menubar
*
* The code sample below shows how to set up a `GtkApplicationWindow`
* with a menu bar defined on the [class@Gtk.Application]:
*
* ```c
* GtkApplication *app = gtk_application_new ("org.gtk.test", 0);
*
* GtkBuilder *builder = gtk_builder_new_from_string (
* "<interface>"
* " <menu id='menubar'>"
* " <submenu>"
* " <attribute name='label' translatable='yes'>_Edit</attribute>"
* " <item>"
* " <attribute name='label' translatable='yes'>_Copy</attribute>"
* " <attribute name='action'>win.copy</attribute>"
* " </item>"
* " <item>"
* " <attribute name='label' translatable='yes'>_Paste</attribute>"
* " <attribute name='action'>win.paste</attribute>"
* " </item>"
* " </submenu>"
* " </menu>"
* "</interface>",
* -1);
*
* GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"));
* gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
* g_object_unref (builder);
*
* // ...
*
* GtkWidget *window = gtk_application_window_new (app);
* ```
*/
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 char *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 char *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... */
typedef struct _GtkApplicationWindowPrivate GtkApplicationWindowPrivate;
struct _GtkApplicationWindowPrivate
{
GSimpleActionGroup *actions;
GtkWidget *menubar;
gboolean show_menubar;
guint id;
GMenu *menubar_section;
GtkShortcutsWindow *help_overlay;
};
static void gtk_application_window_group_iface_init (GActionGroupInterface *iface);
static void gtk_application_window_map_iface_init (GActionMapInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
G_ADD_PRIVATE (GtkApplicationWindow)
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))
static void
gtk_application_window_update_menubar (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
gboolean should_have_menubar;
gboolean have_menubar;
have_menubar = priv->menubar != NULL;
should_have_menubar = priv->show_menubar &&
g_menu_model_get_n_items (G_MENU_MODEL (priv->menubar_section));
if (have_menubar && !should_have_menubar)
{
gtk_widget_unparent (priv->menubar);
priv->menubar = NULL;
}
if (!have_menubar && should_have_menubar)
{
GMenu *combined;
combined = g_menu_new ();
g_menu_append_section (combined, NULL, G_MENU_MODEL (priv->menubar_section));
priv->menubar = gtk_popover_menu_bar_new_from_model (G_MENU_MODEL (combined));
gtk_accessible_update_property (GTK_ACCESSIBLE (priv->menubar),
GTK_ACCESSIBLE_PROPERTY_LABEL, _("Menu bar"),
-1);
gtk_widget_set_parent (priv->menubar, GTK_WIDGET (window));
g_object_unref (combined);
}
}
static void
gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
GtkSettings *settings)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
gboolean shown_by_shell;
g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
if (shown_by_shell)
{
/* the shell shows it, so don't show it locally */
if (g_menu_model_get_n_items (G_MENU_MODEL (priv->menubar_section)) != 0)
g_menu_remove (priv->menubar_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 (priv->menubar_section)) == 0)
{
GMenuModel *menubar = NULL;
if (gtk_window_get_application (GTK_WINDOW (window)) != NULL)
menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
if (menubar != NULL)
g_menu_append_section (priv->menubar_section, NULL, menubar);
}
}
}
static void
gtk_application_window_shell_shows_menubar_changed (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
GtkApplicationWindow *window = user_data;
gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
gtk_application_window_update_menubar (window);
}
static char **
gtk_application_window_list_actions (GActionGroup *group)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
/* may be NULL after dispose has run */
if (!priv->actions)
return g_new0 (char *, 0 + 1);
return g_action_group_list_actions (G_ACTION_GROUP (priv->actions));
}
static gboolean
gtk_application_window_query_action (GActionGroup *group,
const char *action_name,
gboolean *enabled,
const GVariantType **parameter_type,
const GVariantType **state_type,
GVariant **state_hint,
GVariant **state)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return FALSE;
return g_action_group_query_action (G_ACTION_GROUP (priv->actions),
action_name, enabled, parameter_type, state_type, state_hint, state);
}
static void
gtk_application_window_activate_action (GActionGroup *group,
const char *action_name,
GVariant *parameter)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return;
g_action_group_activate_action (G_ACTION_GROUP (priv->actions), action_name, parameter);
}
static void
gtk_application_window_change_action_state (GActionGroup *group,
const char *action_name,
GVariant *state)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return;
g_action_group_change_action_state (G_ACTION_GROUP (priv->actions), action_name, state);
}
static GAction *
gtk_application_window_lookup_action (GActionMap *action_map,
const char *action_name)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return NULL;
return g_action_map_lookup_action (G_ACTION_MAP (priv->actions), action_name);
}
static void
gtk_application_window_add_action (GActionMap *action_map,
GAction *action)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return;
g_action_map_add_action (G_ACTION_MAP (priv->actions), action);
}
static void
gtk_application_window_remove_action (GActionMap *action_map,
const char *action_name)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (!priv->actions)
return;
g_action_map_remove_action (G_ACTION_MAP (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;
iface->activate_action = gtk_application_window_activate_action;
iface->change_action_state = gtk_application_window_change_action_state;
}
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;
}
enum {
PROP_0,
PROP_SHOW_MENUBAR,
N_PROPS
};
static GParamSpec *gtk_application_window_properties[N_PROPS];
static void
gtk_application_window_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->measure (widget,
orientation,
for_size,
minimum, natural,
minimum_baseline, natural_baseline);
if (priv->menubar != NULL)
{
int menubar_min, menubar_nat;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
int menubar_height = 0;
gtk_widget_measure (priv->menubar, GTK_ORIENTATION_VERTICAL,
-1, &menubar_height, NULL, NULL, NULL);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->measure (widget,
orientation,
for_size > -1 ?
for_size - menubar_height : -1,
minimum, natural,
minimum_baseline, natural_baseline);
gtk_widget_measure (priv->menubar, orientation, menubar_height, &menubar_min, &menubar_nat, NULL, NULL);
*minimum = MAX (*minimum, menubar_min);
*natural = MAX (*natural, menubar_nat);
}
else /* VERTICAL */
{
gtk_widget_measure (priv->menubar, orientation, for_size, &menubar_min, &menubar_nat, NULL, NULL);
*minimum += menubar_min;
*natural += menubar_nat;
}
}
}
static void
gtk_application_window_real_size_allocate (GtkWidget *widget,
int width,
int height,
int baseline)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (priv->menubar != NULL)
{
GtkAllocation menubar_allocation;
GtkAllocation child_allocation;
int menubar_height;
GtkWidget *child;
_gtk_window_set_allocation (GTK_WINDOW (widget), width, height, &child_allocation);
menubar_allocation = child_allocation;
gtk_widget_measure (priv->menubar, GTK_ORIENTATION_VERTICAL,
menubar_allocation.width,
&menubar_height, NULL, NULL, NULL);
menubar_allocation.height = menubar_height;
gtk_widget_size_allocate (priv->menubar, &menubar_allocation, -1);
child_allocation.y += menubar_height;
child_allocation.height -= menubar_height;
if (baseline != -1)
baseline -= child_allocation.y;
child = gtk_window_get_child (GTK_WINDOW (window));
if (child != NULL && gtk_widget_get_visible (child))
gtk_widget_size_allocate (child, &child_allocation, baseline);
gtk_tooltip_maybe_allocate (GTK_NATIVE (widget));
}
else
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->size_allocate (widget,
width,
height,
baseline);
}
static void
gtk_application_window_real_realize (GtkWidget *widget)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
GtkSettings *settings;
settings = gtk_widget_get_settings (widget);
g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->realize (widget);
gtk_application_window_update_shell_shows_menubar (window, settings);
gtk_application_window_update_menubar (window);
}
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_menubar_changed, widget);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->unrealize (widget);
}
GActionGroup *
gtk_application_window_get_action_group (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
return G_ACTION_GROUP (priv->actions);
}
static void
gtk_application_window_real_map (GtkWidget *widget)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
/* XXX could eliminate this by tweaking gtk_window_map */
if (priv->menubar)
gtk_widget_map (priv->menubar);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->map (widget);
}
static void
gtk_application_window_real_unmap (GtkWidget *widget)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
/* XXX could eliminate this by tweaking gtk_window_unmap */
if (priv->menubar)
gtk_widget_unmap (priv->menubar);
GTK_WIDGET_CLASS (gtk_application_window_parent_class)->unmap (widget);
}
static void
gtk_application_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
switch (prop_id)
{
case PROP_SHOW_MENUBAR:
g_value_set_boolean (value, priv->show_menubar);
break;
default:
g_assert_not_reached ();
}
}
static void
gtk_application_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
switch (prop_id)
{
case PROP_SHOW_MENUBAR:
gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
break;
default:
g_assert_not_reached ();
}
}
static void
gtk_application_window_dispose (GObject *object)
{
GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (priv->menubar)
{
gtk_widget_unparent (priv->menubar);
priv->menubar = NULL;
}
g_clear_object (&priv->menubar_section);
if (priv->help_overlay)
{
gtk_window_destroy (GTK_WINDOW (priv->help_overlay));
g_clear_object (&priv->help_overlay);
}
G_OBJECT_CLASS (gtk_application_window_parent_class)->dispose (object);
/* We do this below the chain-up above to give us a chance to be
* removed from the GtkApplication (which is done in the dispose
* handler of GtkWindow).
*
* That reduces our chances of being watched as a GActionGroup from a
* muxer constructed by GtkApplication.
*/
g_clear_object (&priv->actions);
}
static void
gtk_application_window_init (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
priv->actions = gtk_application_window_actions_new (window);
priv->menubar_section = g_menu_new ();
gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (priv->actions));
/* priv->actions is the one and only ref on the group, so when
* we dispose, the action group will die, disconnecting all signals.
*/
g_signal_connect_swapped (priv->actions, "action-added",
G_CALLBACK (g_action_group_action_added), window);
g_signal_connect_swapped (priv->actions, "action-enabled-changed",
G_CALLBACK (g_action_group_action_enabled_changed), window);
g_signal_connect_swapped (priv->actions, "action-state-changed",
G_CALLBACK (g_action_group_action_state_changed), window);
g_signal_connect_swapped (priv->actions, "action-removed",
G_CALLBACK (g_action_group_action_removed), window);
}
static void
gtk_application_window_keys_changed (GtkWindow *window)
{
GtkApplicationWindow *self = GTK_APPLICATION_WINDOW (window);
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (self);
GTK_WINDOW_CLASS (gtk_application_window_parent_class)->keys_changed (window);
/* Notify key changes on the help overlay */
if (priv->help_overlay != NULL)
_gtk_window_notify_keys_changed (GTK_WINDOW (priv->help_overlay));
}
static void
gtk_application_window_class_init (GtkApplicationWindowClass *class)
{
GtkWindowClass *window_class = GTK_WINDOW_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
GObjectClass *object_class = G_OBJECT_CLASS (class);
window_class->keys_changed = gtk_application_window_keys_changed;
widget_class->measure = gtk_application_window_measure;
widget_class->size_allocate = gtk_application_window_real_size_allocate;
widget_class->realize = gtk_application_window_real_realize;
widget_class->unrealize = gtk_application_window_real_unrealize;
widget_class->map = gtk_application_window_real_map;
widget_class->unmap = gtk_application_window_real_unmap;
object_class->get_property = gtk_application_window_get_property;
object_class->set_property = gtk_application_window_set_property;
object_class->dispose = gtk_application_window_dispose;
/**
* GtkApplicationWindow:show-menubar:
*
* If this property is true, the window will display a menubar
* unless it is shown by the desktop shell.
*
* See [method@Gtk.Application.set_menubar].
*
* If false, the window will not display a menubar, regardless
* of whether the desktop shell is showing it or not.
*/
gtk_application_window_properties[PROP_SHOW_MENUBAR] =
g_param_spec_boolean ("show-menubar", NULL, NULL,
FALSE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
}
/**
* gtk_application_window_new:
* @application: an application
*
* Creates a new `GtkApplicationWindow`.
*
* Returns: a newly created `GtkApplicationWindow`
*/
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);
}
/**
* gtk_application_window_get_show_menubar:
* @window: an application window
*
* Returns whether the window will display a menubar for the app menu
* and menubar as needed.
*
* Returns: True if the window will display a menubar when needed
*/
gboolean
gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
return priv->show_menubar;
}
/**
* gtk_application_window_set_show_menubar:
* @window: an application window
* @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.
*/
void
gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
gboolean show_menubar)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
show_menubar = !!show_menubar;
if (priv->show_menubar != show_menubar)
{
priv->show_menubar = show_menubar;
gtk_application_window_update_menubar (window);
g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
}
}
/**
* gtk_application_window_get_id:
* @window: an application window
*
* Returns the unique ID of the window.
*
* If the window has not yet been added to a `GtkApplication`, returns `0`.
*
* Returns: the unique ID for the window, or `0` if the window
* has not yet been added to an application
*/
guint
gtk_application_window_get_id (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
return priv->id;
}
void
gtk_application_window_set_id (GtkApplicationWindow *window,
guint id)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
priv->id = id;
}
static void
show_help_overlay (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkApplicationWindow *window = user_data;
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
if (priv->help_overlay)
gtk_window_present (GTK_WINDOW (priv->help_overlay));
}
/**
* gtk_application_window_set_help_overlay:
* @window: an application window
* @help_overlay: (nullable): a shortcuts window
*
* Associates a shortcuts window with the application window.
*
* Additionally, sets up an action with the name
* `win.show-help-overlay` to present it.
*
* The window takes responsibility for destroying the help overlay.
*
* Deprecated: 4.18: `GtkShortcutsWindow` will be removed in GTK 5
*/
void
gtk_application_window_set_help_overlay (GtkApplicationWindow *window,
GtkShortcutsWindow *help_overlay)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
g_return_if_fail (help_overlay == NULL || GTK_IS_SHORTCUTS_WINDOW (help_overlay));
if (priv->help_overlay)
gtk_window_destroy (GTK_WINDOW (priv->help_overlay));
g_set_object (&priv->help_overlay, help_overlay);
if (!priv->help_overlay)
return;
gtk_window_set_modal (GTK_WINDOW (help_overlay), TRUE);
gtk_window_set_hide_on_close (GTK_WINDOW (help_overlay), TRUE);
gtk_window_set_transient_for (GTK_WINDOW (help_overlay), GTK_WINDOW (window));
gtk_shortcuts_window_set_window (help_overlay, GTK_WINDOW (window));
if (!g_action_map_lookup_action (G_ACTION_MAP (priv->actions), "show-help-overlay"))
{
GSimpleAction *action;
action = g_simple_action_new ("show-help-overlay", NULL);
g_signal_connect (action, "activate", G_CALLBACK (show_help_overlay), window);
g_action_map_add_action (G_ACTION_MAP (priv->actions), G_ACTION (action));
g_object_unref (G_OBJECT (action));
}
}
/**
* gtk_application_window_get_help_overlay:
* @window: an application window
*
* Gets the `GtkShortcutsWindow` that is associated with @window.
*
* See [method@Gtk.ApplicationWindow.set_help_overlay].
*
* Returns: (transfer none) (nullable): the help overlay associated
* with the window
*
* Deprecated: 4.18: `GtkShortcutsWindow` will be removed in GTK 5
*/
GtkShortcutsWindow *
gtk_application_window_get_help_overlay (GtkApplicationWindow *window)
{
GtkApplicationWindowPrivate *priv = gtk_application_window_get_instance_private (window);
g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
return priv->help_overlay;
}
|