]> Pileus Git - ~andy/gtk/blob - examples/bloatpad.c
Bloatpad: Add mnemonics to the menu
[~andy/gtk] / examples / bloatpad.c
1 #include <stdlib.h>
2 #include <gtk/gtk.h>
3
4 static void
5 show_about (GSimpleAction *action,
6             GVariant      *parameter,
7             gpointer       user_data)
8 {
9   GtkWindow *window = user_data;
10
11   gtk_show_about_dialog (window,
12                          "program-name", "Bloatpad",
13                          "title", "About Bloatpad",
14                          "comments", "Not much to say, really.",
15                          NULL);
16 }
17
18 static void
19 activate_toggle (GSimpleAction *action,
20                  GVariant      *parameter,
21                  gpointer       user_data)
22 {
23   GVariant *state;
24
25   state = g_action_get_state (G_ACTION (action));
26   g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
27   g_variant_unref (state);
28 }
29
30 static void
31 change_fullscreen_state (GSimpleAction *action,
32                          GVariant      *state,
33                          gpointer       user_data)
34 {
35   if (g_variant_get_boolean (state))
36     gtk_window_fullscreen (user_data);
37   else
38     gtk_window_unfullscreen (user_data);
39
40   g_simple_action_set_state (action, state);
41 }
42
43 static GActionEntry win_entries[] = {
44   { "about", show_about },
45   { "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state }
46 };
47
48 static void
49 new_window (GApplication *app,
50             GFile        *file)
51 {
52   GtkWidget *window, *grid, *scrolled, *view;
53
54   window = gtk_application_window_new (GTK_APPLICATION (app));
55   g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window);
56   gtk_window_set_title (GTK_WINDOW (window), "Bloatpad");
57
58   grid = gtk_grid_new ();
59   gtk_container_add (GTK_CONTAINER (window), grid);
60
61   scrolled = gtk_scrolled_window_new (NULL, NULL);
62   gtk_widget_set_hexpand (scrolled, TRUE);
63   gtk_widget_set_vexpand (scrolled, TRUE);
64   view = gtk_text_view_new ();
65
66   gtk_container_add (GTK_CONTAINER (scrolled), view);
67
68   gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 0, 1, 1);
69
70   if (file != NULL)
71     {
72       gchar *contents;
73       gsize length;
74
75       if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
76         {
77           GtkTextBuffer *buffer;
78
79           buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
80           gtk_text_buffer_set_text (buffer, contents, length);
81           g_free (contents);
82         }
83     }
84
85   gtk_widget_show_all (GTK_WIDGET (window));
86 }
87
88 static void
89 bloat_pad_activate (GApplication *application)
90 {
91   new_window (application, NULL);
92 }
93
94 static void
95 bloat_pad_open (GApplication  *application,
96                 GFile        **files,
97                 gint           n_files,
98                 const gchar   *hint)
99 {
100   gint i;
101
102   for (i = 0; i < n_files; i++)
103     new_window (application, files[i]);
104 }
105
106 typedef GtkApplication BloatPad;
107 typedef GtkApplicationClass BloatPadClass;
108
109 G_DEFINE_TYPE (BloatPad, bloat_pad, GTK_TYPE_APPLICATION)
110
111 static void
112 bloat_pad_finalize (GObject *object)
113 {
114   G_OBJECT_CLASS (bloat_pad_parent_class)->finalize (object);
115 }
116
117 static void
118 show_help (GSimpleAction *action,
119            GVariant      *parameter,
120            gpointer       user_data)
121 {
122   g_print ("Want help, eh ?!\n");
123 }
124
125 static void
126 quit_app (GSimpleAction *action,
127           GVariant      *parameter,
128           gpointer       user_data)
129 {
130   GList *list, *next;
131   GtkWindow *win;
132
133   g_print ("Going down...\n");
134
135   list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
136   while (list)
137     {
138       win = list->data;
139       next = list->next;
140
141       gtk_widget_destroy (GTK_WIDGET (win));
142
143       list = next;
144     }
145 }
146
147 static GSimpleActionGroup *actions = NULL;
148 static GMenu *menu = NULL;
149
150 static void
151 remove_action (GSimpleAction *action,
152                GVariant      *parameter,
153                gpointer       user_data)
154 {
155   GAction *add;
156
157   g_menu_remove (menu, g_menu_model_get_n_items (G_MENU_MODEL (menu)) - 1);
158   g_simple_action_set_enabled (action, FALSE);
159   add = g_simple_action_group_lookup (actions, "add");
160   g_simple_action_set_enabled (G_SIMPLE_ACTION (add), TRUE);
161 }
162
163 static void
164 add_action (GSimpleAction *action,
165             GVariant      *parameter,
166             gpointer       user_data)
167 {
168   GAction *remove;
169
170   g_menu_append (menu, "Remove", "app.remove");
171   g_simple_action_set_enabled (action, FALSE);
172   remove = g_simple_action_group_lookup (actions, "remove");
173   g_simple_action_set_enabled (G_SIMPLE_ACTION (remove), TRUE);
174 }
175
176 static GActionEntry app_entries[] = {
177   { "help", show_help, NULL, NULL, NULL },
178   { "quit", quit_app, NULL, NULL, NULL },
179   { "add", add_action, NULL, NULL, NULL },
180   { "remove", remove_action, NULL, NULL, NULL }
181 };
182
183 static GActionGroup *
184 get_actions (void)
185 {
186   actions = g_simple_action_group_new ();
187   g_simple_action_group_add_entries (actions,
188                                      app_entries, G_N_ELEMENTS (app_entries),
189                                      NULL);
190
191   return G_ACTION_GROUP (actions);
192 }
193
194 static GMenuModel *
195 get_menu (void)
196 {
197   menu = g_menu_new ();
198   g_menu_append (menu, "_Help", "app.help");
199   g_menu_append (menu, "_About Bloatpad", "win.about");
200   g_menu_append (menu, "_Fullscreen", "win.fullscreen");
201   g_menu_append (menu, "_Quit", "app.quit");
202   g_menu_append (menu, "Add", "app.add");
203
204   return G_MENU_MODEL (menu);
205 }
206
207 static void
208 bloat_pad_init (BloatPad *app)
209 {
210   g_application_set_action_group (G_APPLICATION (app), get_actions ());
211   g_application_set_app_menu (G_APPLICATION (app), get_menu ());
212 }
213
214 static void
215 bloat_pad_class_init (BloatPadClass *class)
216 {
217   G_OBJECT_CLASS (class)->finalize = bloat_pad_finalize;
218
219   G_APPLICATION_CLASS (class)->activate = bloat_pad_activate;
220   G_APPLICATION_CLASS (class)->open = bloat_pad_open;
221 }
222
223 BloatPad *
224 bloat_pad_new (void)
225 {
226   g_type_init ();
227
228   return g_object_new (bloat_pad_get_type (),
229                        "application-id", "org.gtk.Test.bloatpad",
230                        "flags", G_APPLICATION_HANDLES_OPEN,
231                        NULL);
232 }
233
234 int
235 main (int argc, char **argv)
236 {
237   BloatPad *bloat_pad;
238   int status;
239
240   bloat_pad = bloat_pad_new ();
241   status = g_application_run (G_APPLICATION (bloat_pad), argc, argv);
242   g_object_unref (bloat_pad);
243
244   return status;
245 }