]> Pileus Git - ~andy/gtk/blob - examples/action-namespace.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / examples / action-namespace.c
1 #include <gtk/gtk.h>
2
3 static void
4 action_activated (GSimpleAction *action,
5                   GVariant      *parameter,
6                   gpointer       user_data)
7 {
8   GtkWindow *parent = user_data;
9   GtkWidget *dialog;
10
11   dialog = gtk_message_dialog_new (parent,
12                                    GTK_DIALOG_DESTROY_WITH_PARENT,
13                                    GTK_MESSAGE_INFO,
14                                    GTK_BUTTONS_CLOSE,
15                                    "Activated action `%s`",
16                                    g_action_get_name (G_ACTION (action)));
17
18   g_signal_connect_swapped (dialog, "response",
19                             G_CALLBACK (gtk_widget_destroy), dialog);
20
21   gtk_widget_show_all (dialog);
22 }
23
24 static GActionEntry doc_entries[] = {
25   { "save", action_activated },
26   { "print", action_activated },
27   { "share", action_activated }
28 };
29
30 static GActionEntry win_entries[] = {
31   { "fullscreen", action_activated },
32   { "close", action_activated },
33 };
34
35 const gchar *menu_ui =
36   "<interface>"
37   "  <menu id='doc-menu'>"
38   "    <section>"
39   "      <item>"
40   "        <attribute name='label'>_Save</attribute>"
41   "        <attribute name='action'>save</attribute>"
42   "      </item>"
43   "      <item>"
44   "        <attribute name='label'>_Print</attribute>"
45   "        <attribute name='action'>print</attribute>"
46   "      </item>"
47   "      <item>"
48   "        <attribute name='label'>_Share</attribute>"
49   "        <attribute name='action'>share</attribute>"
50   "      </item>"
51   "    </section>"
52   "  </menu>"
53   "  <menu id='win-menu'>"
54   "    <section>"
55   "      <item>"
56   "        <attribute name='label'>_Fullscreen</attribute>"
57   "        <attribute name='action'>fullscreen</attribute>"
58   "      </item>"
59   "      <item>"
60   "        <attribute name='label'>_Close</attribute>"
61   "        <attribute name='action'>close</attribute>"
62   "      </item>"
63   "    </section>"
64   "  </menu>"
65   "</interface>";
66
67 static void
68 activate (GApplication *app,
69           gpointer      user_data)
70 {
71   GtkWidget *win;
72   GtkWidget *button;
73   GSimpleActionGroup *doc_actions;
74   GtkBuilder *builder;
75   GMenuModel *doc_menu;
76   GMenuModel *win_menu;
77   GMenu *button_menu;
78   GMenuItem *section;
79
80   if (gtk_application_get_windows (GTK_APPLICATION (app)) != NULL)
81     return;
82
83   win = gtk_application_window_new (GTK_APPLICATION (app));
84
85   doc_actions = g_simple_action_group_new ();
86   g_simple_action_group_add_entries (doc_actions, doc_entries,
87                                      G_N_ELEMENTS (doc_entries), win);
88
89   g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries,
90                                    G_N_ELEMENTS (win_entries), win);
91
92   builder = gtk_builder_new ();
93   gtk_builder_add_from_string (builder, menu_ui, -1, NULL);
94
95   doc_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "doc-menu"));
96   win_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "win-menu"));
97
98   button_menu = g_menu_new ();
99
100   section = g_menu_item_new_section (NULL, doc_menu);
101   g_menu_item_set_attribute (section, "action-namespace", "s", "doc");
102   g_menu_append_item (button_menu, section);
103   g_object_unref (section);
104
105   section = g_menu_item_new_section (NULL, win_menu);
106   g_menu_item_set_attribute (section, "action-namespace", "s", "win");
107   g_menu_append_item (button_menu, section);
108   g_object_unref (section);
109
110   button = gtk_menu_button_new ();
111   gtk_button_set_label (GTK_BUTTON (button), "Menu");
112   gtk_widget_insert_action_group (button, "doc", G_ACTION_GROUP (doc_actions));
113   gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (button_menu));
114
115   gtk_container_add (GTK_CONTAINER (win), button);
116   gtk_container_set_border_width (GTK_CONTAINER (win), 12);
117   gtk_widget_show_all (win);
118
119   g_object_unref (button_menu);
120   g_object_unref (doc_actions);
121   g_object_unref (builder);
122 }
123
124 int
125 main(int    argc,
126      char **argv)
127 {
128   GtkApplication *app;
129
130   app = gtk_application_new ("org.gtk.Example", 0);
131   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
132
133   return g_application_run (G_APPLICATION (app), argc, argv);
134 }