Top | Description | Object Hierarchy | Implemented Interfaces | Properties |
#include <gtk/gtk.h> struct GtkApplicationWindow; GtkWidget * gtk_application_window_new (GtkApplication *application
); void gtk_application_window_set_show_menubar (GtkApplicationWindow *window
,gboolean show_menubar
); gboolean gtk_application_window_get_show_menubar (GtkApplicationWindow *window
); guint gtk_application_window_get_id (GtkApplicationWindow *window
);
GObject +----GInitiallyUnowned +----GtkWidget +----GtkContainer +----GtkBin +----GtkWindow +----GtkApplicationWindow
GtkApplicationWindow implements AtkImplementorIface, GtkBuildable, GActionGroup and GActionMap.
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
as the menubar. See gtk_application_set_app_menu()
and
gtk_application_set_menubar()
.
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.
Note that widgets that are placed inside a GtkApplicationWindow can also activate these actions, if they implement the GtkActionable interface.
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).
The settings "gtk-shell-shows-app-menu" and "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 GtkApplicationWindow will automatically show a GtkMenuBar for it. (see the GtkApplication docs for some screenshots of how this looks on different platforms). This behaviour can be overridden with the "show-menubar" property. If the desktop environment does not display the application menu, then it will automatically be included in the menubar.
Example 7. A GtkApplicationWindow with a menubar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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>"); gtk_application_set_menubar (G_APPLICATION (app), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"))); g_object_unref (builder); ... window = gtk_application_window_new (app); |
Example 8. Handling fallback yourself
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 |
#include <stdlib.h> #include <gtk/gtk.h> static void new_window (GApplication *app, GFile *file) { GtkWidget *window, *scrolled, *view, *overlay; GtkSettings *settings; gboolean appmenu; window = gtk_application_window_new (GTK_APPLICATION (app)); gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (window), FALSE); gtk_window_set_default_size ((GtkWindow*)window, 640, 480); gtk_window_set_title (GTK_WINDOW (window), "Sunny"); overlay = gtk_overlay_new (); gtk_container_add (GTK_CONTAINER (window), overlay); settings = gtk_settings_get_default (); g_object_get (settings, "gtk-shell-shows-app-menu", &appmenu, NULL); if (!appmenu) { GMenuModel *model; GtkWidget *menu; GtkWidget *image; model = gtk_application_get_app_menu (GTK_APPLICATION (app)); menu = gtk_menu_button_new (); gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menu), model); gtk_widget_set_halign (menu, GTK_ALIGN_END); gtk_widget_set_valign (menu, GTK_ALIGN_START); image = gtk_image_new (); gtk_image_set_from_icon_name (GTK_IMAGE (image), "sunny", GTK_ICON_SIZE_MENU); gtk_button_set_image (GTK_BUTTON (menu), image); gtk_overlay_add_overlay (GTK_OVERLAY (overlay), menu); } scrolled = gtk_scrolled_window_new (NULL, NULL); gtk_widget_set_hexpand (scrolled, TRUE); gtk_widget_set_vexpand (scrolled, TRUE); view = gtk_text_view_new (); gtk_container_add (GTK_CONTAINER (scrolled), view); gtk_container_add (GTK_CONTAINER (overlay), scrolled); if (file != NULL) { gchar *contents; gsize length; if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) { GtkTextBuffer *buffer; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); gtk_text_buffer_set_text (buffer, contents, length); g_free (contents); } } gtk_widget_show_all (GTK_WIDGET (window)); } static void activate (GApplication *application) { new_window (application, NULL); } static void open (GApplication *application, GFile **files, gint n_files, const gchar *hint) { gint i; for (i = 0; i < n_files; i++) new_window (application, files[i]); } typedef GtkApplication MenuButton; typedef GtkApplicationClass MenuButtonClass; G_DEFINE_TYPE (MenuButton, menu_button, GTK_TYPE_APPLICATION) static void show_about (GSimpleAction *action, GVariant *parameter, gpointer user_data) { gtk_show_about_dialog (NULL, "program-name", "Sunny", "title", "About Sunny", "logo-icon-name", "sunny", "comments", "A cheap Bloatpad clone.", NULL); } static void quit_app (GSimpleAction *action, GVariant *parameter, gpointer user_data) { GList *list, *next; GtkWindow *win; g_print ("Going down...\n"); list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ())); while (list) { win = list->data; next = list->next; gtk_widget_destroy (GTK_WIDGET (win)); list = next; } } static void new_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) { GApplication *app = user_data; g_application_activate (app); } static GActionEntry app_entries[] = { { "about", show_about, NULL, NULL, NULL }, { "quit", quit_app, NULL, NULL, NULL }, { "new", new_activated, NULL, NULL, NULL } }; static void startup (GApplication *application) { GtkBuilder *builder; G_APPLICATION_CLASS (menu_button_parent_class)->startup (application); g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application); builder = gtk_builder_new (); gtk_builder_add_from_string (builder, "<interface>" " <menu id='app-menu'>" " <section>" " <item>" " <attribute name='label' translatable='yes'>_New Window</attribute>" " <attribute name='action'>app.new</attribute>" " </item>" " <item>" " <attribute name='label' translatable='yes'>_About Sunny</attribute>" " <attribute name='action'>app.about</attribute>" " </item>" " <item>" " <attribute name='label' translatable='yes'>_Quit</attribute>" " <attribute name='action'>app.quit</attribute>" " <attribute name='accel'><Primary>q</attribute>" " </item>" " </section>" " </menu>" "</interface>", -1, NULL); gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"))); g_object_unref (builder); } static void menu_button_init (MenuButton *app) { } static void menu_button_class_init (MenuButtonClass *class) { GApplicationClass *application_class = G_APPLICATION_CLASS (class); application_class->startup = startup; application_class->activate = activate; application_class->open = open; } MenuButton * menu_button_new (void) { return g_object_new (menu_button_get_type (), "application-id", "org.gtk.Test.Sunny", "flags", G_APPLICATION_HANDLES_OPEN, NULL); } int main (int argc, char **argv) { MenuButton *menu_button; int status; menu_button = menu_button_new (); status = g_application_run (G_APPLICATION (menu_button), argc, argv); g_object_unref (menu_button); return status; } |
The XML format understood by GtkBuilder for GMenuModel consists
of a toplevel <menu>
element, which contains
one or more <item>
elements. Each
<item>
element contains
<attribute>
and <link>
elements with a mandatory name attribute.
<link>
elements have the same content
model as <menu>
.
Attribute values can be translated using gettext, like other GtkBuilder
content. <attribute>
elements can be marked for
translation with a translatable="yes"
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.
GtkWidget * gtk_application_window_new (GtkApplication *application
);
Creates a new GtkApplicationWindow.
|
a GtkApplication |
Returns : |
a newly created GtkApplicationWindow |
Since 3.4
void gtk_application_window_set_show_menubar (GtkApplicationWindow *window
,gboolean show_menubar
);
Sets whether the window will display a menubar for the app menu and menubar as needed.
|
a GtkApplicationWindow |
|
whether to show a menubar when needed |
Since 3.4
gboolean gtk_application_window_get_show_menubar
(GtkApplicationWindow *window
);
Returns whether the window will display a menubar for the app menu and menubar as needed.
|
a GtkApplicationWindow |
Returns : |
TRUE if window will display a menubar when needed |
Since 3.4
guint gtk_application_window_get_id (GtkApplicationWindow *window
);
Returns the unique ID of the window. If the window has not yet been added to
a GtkApplication, returns 0
.
|
a GtkApplicationWindow |
Returns : |
the unique ID for window , or 0 if the window
has not yet been added to a GtkApplication
|
Since 3.6
"show-menubar"
property"show-menubar" gboolean : Read / Write / Construct
If this property is TRUE
, the window will display a menubar
that includes the app menu and menubar, unless these are
shown by the desktop shell. See gtk_application_set_app_menu()
and gtk_application_set_menubar()
.
If FALSE
, the window will not display a menubar, regardless
of whether the desktop shell is showing the menus or not.
Default value: TRUE