]> Pileus Git - ~andy/gtk/blob - examples/plugman.c
Add accelerators to bloatpad.
[~andy/gtk] / examples / plugman.c
1 #include <stdlib.h>
2 #include <gtk/gtk.h>
3
4 static void
5 activate_toggle (GSimpleAction *action,
6                  GVariant      *parameter,
7                  gpointer       user_data)
8 {
9   GVariant *state;
10
11   state = g_action_get_state (G_ACTION (action));
12   g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
13   g_variant_unref (state);
14 }
15
16 static void
17 change_fullscreen_state (GSimpleAction *action,
18                          GVariant      *state,
19                          gpointer       user_data)
20 {
21   if (g_variant_get_boolean (state))
22     gtk_window_fullscreen (user_data);
23   else
24     gtk_window_unfullscreen (user_data);
25
26   g_simple_action_set_state (action, state);
27 }
28
29 static GtkClipboard *
30 get_clipboard (GtkWidget *widget)
31 {
32   return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
33 }
34
35 static void
36 window_copy (GSimpleAction *action,
37              GVariant      *parameter,
38              gpointer       user_data)
39 {
40   GtkWindow *window = GTK_WINDOW (user_data);
41   GtkTextView *text = g_object_get_data ((GObject*)window, "plugman-text");
42
43   gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (text),
44                                   get_clipboard ((GtkWidget*) text));
45 }
46
47 static void
48 window_paste (GSimpleAction *action,
49               GVariant      *parameter,
50               gpointer       user_data)
51 {
52   GtkWindow *window = GTK_WINDOW (user_data);
53   GtkTextView *text = g_object_get_data ((GObject*)window, "plugman-text");
54   
55   gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (text),
56                                    get_clipboard ((GtkWidget*) text),
57                                    NULL,
58                                    TRUE);
59
60 }
61
62 static GActionEntry win_entries[] = {
63   { "copy", window_copy, NULL, NULL, NULL },
64   { "paste", window_paste, NULL, NULL, NULL },
65   { "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state }
66 };
67
68 static void
69 new_window (GApplication *app,
70             GFile        *file)
71 {
72   GtkWidget *window, *grid, *scrolled, *view;
73
74   window = gtk_application_window_new (GTK_APPLICATION (app));
75   gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
76   g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window);
77   gtk_window_set_title (GTK_WINDOW (window), "Plugman");
78
79   grid = gtk_grid_new ();
80   gtk_container_add (GTK_CONTAINER (window), grid);
81
82   scrolled = gtk_scrolled_window_new (NULL, NULL);
83   gtk_widget_set_hexpand (scrolled, TRUE);
84   gtk_widget_set_vexpand (scrolled, TRUE);
85   view = gtk_text_view_new ();
86
87   g_object_set_data ((GObject*)window, "plugman-text", view);
88
89   gtk_container_add (GTK_CONTAINER (scrolled), view);
90
91   gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 0, 1, 1);
92
93   if (file != NULL)
94     {
95       gchar *contents;
96       gsize length;
97
98       if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
99         {
100           GtkTextBuffer *buffer;
101
102           buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
103           gtk_text_buffer_set_text (buffer, contents, length);
104           g_free (contents);
105         }
106     }
107
108   gtk_widget_show_all (GTK_WIDGET (window));
109 }
110
111 static void
112 plug_man_activate (GApplication *application)
113 {
114   new_window (application, NULL);
115 }
116
117 static void
118 plug_man_open (GApplication  *application,
119                 GFile        **files,
120                 gint           n_files,
121                 const gchar   *hint)
122 {
123   gint i;
124
125   for (i = 0; i < n_files; i++)
126     new_window (application, files[i]);
127 }
128
129 typedef GtkApplication PlugMan;
130 typedef GtkApplicationClass PlugManClass;
131
132 G_DEFINE_TYPE (PlugMan, plug_man, GTK_TYPE_APPLICATION)
133
134 static void
135 plug_man_finalize (GObject *object)
136 {
137   G_OBJECT_CLASS (plug_man_parent_class)->finalize (object);
138 }
139
140 static void
141 show_about (GSimpleAction *action,
142             GVariant      *parameter,
143             gpointer       user_data)
144 {
145   gtk_show_about_dialog (NULL,
146                          "program-name", "Plugman",
147                          "title", "About Plugman",
148                          "comments", "A cheap Bloatpad clone.",
149                          NULL);
150 }
151
152
153 static void
154 quit_app (GSimpleAction *action,
155           GVariant      *parameter,
156           gpointer       user_data)
157 {
158   GList *list, *next;
159   GtkWindow *win;
160
161   g_print ("Going down...\n");
162
163   list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
164   while (list)
165     {
166       win = list->data;
167       next = list->next;
168
169       gtk_widget_destroy (GTK_WIDGET (win));
170
171       list = next;
172     }
173 }
174
175 static gboolean is_red_plugin_enabled;
176 static gboolean is_black_plugin_enabled;
177
178 static gboolean
179 plugin_enabled (const gchar *name)
180 {
181   if (g_strcmp0 (name, "red") == 0)
182     return is_red_plugin_enabled;
183   else
184     return is_black_plugin_enabled;
185 }
186
187 static GMenuModel *
188 find_plugin_menu (void)
189 {
190   GMenuModel *menubar;
191   GMenuModel *menu;
192   gint i, j;
193   const gchar *label, *id;
194
195   menubar = g_application_get_menubar (g_application_get_default ());
196   for (i = 0; i < g_menu_model_get_n_items (menubar); i++)
197     {
198        if (g_menu_model_get_item_attribute (menubar, i, "label", "s", &label) &&
199            g_strcmp0 (label, "_Edit") == 0)
200          {
201             menu = g_menu_model_get_item_link (menubar, i, "submenu");
202             for (j = 0; j < g_menu_model_get_n_items (menu); j++)
203               {
204                 if (g_menu_model_get_item_attribute (menu, j, "id", "s", &id) &&
205                     g_strcmp0 (id, "plugins") == 0)
206                   {
207                     return g_menu_model_get_item_link (menu, j, "section");
208                   }
209               }
210          }
211     }
212
213   return NULL;
214 }
215
216 static void
217 plugin_action (GAction  *action,
218                GVariant *parameter,
219                gpointer  data)
220 {
221   GApplication *app;
222   GList *list;
223   GtkWindow *window;
224   GtkWidget *text;
225   GdkRGBA color;
226
227   app = g_application_get_default ();
228   list = gtk_application_get_windows (GTK_APPLICATION (app));
229   window = GTK_WINDOW (list->data);
230   text = g_object_get_data ((GObject*)window, "plugman-text");
231
232   gdk_rgba_parse (&color, g_action_get_name (action));
233
234   gtk_widget_override_color (text, 0, &color);
235 }
236
237 static void
238 enable_plugin (const gchar *name)
239 {
240   GMenuModel *plugin_menu;
241   GAction *action;
242
243   g_print ("Enabling '%s' plugin\n", name);
244
245   action = (GAction *)g_simple_action_new (name, NULL);
246   g_signal_connect (action, "activate", G_CALLBACK (plugin_action), (gpointer)name);
247   g_action_map_add_action (G_ACTION_MAP (g_application_get_default ()), action);
248   g_print ("Actions of '%s' plugin added\n", name);
249
250   plugin_menu = find_plugin_menu ();
251   if (plugin_menu)
252     {
253       GMenu *section;
254       GMenuItem *item;
255       gchar *label;
256       gchar *action;
257
258       section = g_menu_new ();
259       label = g_strdup_printf ("Turn text %s", name);
260       action = g_strconcat ("app.", name, NULL);
261       g_menu_insert (section, 0, label, action);
262       g_free (label);
263       g_free (action);
264       item = g_menu_item_new_section (NULL, (GMenuModel*)section);
265       g_menu_item_set_attribute (item, "id", "s", name);
266       g_menu_append_item (G_MENU (plugin_menu), item);
267       g_object_unref (item);
268       g_object_unref (section);
269       g_print ("Menus of '%s' plugin added\n", name);
270     }
271   else
272     g_warning ("Plugin menu not found\n");
273
274   if (g_strcmp0 (name, "red") == 0)
275     is_red_plugin_enabled = TRUE;
276   else
277     is_black_plugin_enabled = TRUE;
278 }
279
280 static void
281 disable_plugin (const gchar *name)
282 {
283   GMenuModel *plugin_menu;
284
285   g_print ("Disabling '%s' plugin\n", name);
286
287   plugin_menu = find_plugin_menu ();
288   if (plugin_menu)
289     {
290       const gchar *id;
291       gint i;
292
293       for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
294         {
295            if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id) &&
296                g_strcmp0 (id, name) == 0)
297              {
298                g_menu_remove (G_MENU (plugin_menu), i);
299                g_print ("Menus of '%s' plugin removed\n", name);
300              }
301         }
302     }
303   else
304     g_warning ("Plugin menu not found\n");
305
306   g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), name);
307   g_print ("Actions of '%s' plugin removed\n", name);
308
309   if (g_strcmp0 (name, "red") == 0)
310     is_red_plugin_enabled = FALSE;
311   else
312     is_black_plugin_enabled = FALSE;
313 }
314
315 static void
316 enable_or_disable_plugin (GtkToggleButton *button,
317                           const gchar     *name)
318 {
319   if (plugin_enabled (name))
320     disable_plugin (name);
321   else
322     enable_plugin (name);
323 }
324
325
326 static void
327 configure_plugins (GSimpleAction *action,
328                    GVariant      *parameter,
329                    gpointer       user_data)
330 {
331   GtkBuilder *builder;
332   GtkWidget *dialog;
333   GtkWidget *check;
334   GError *error = NULL;
335
336   builder = gtk_builder_new ();
337   gtk_builder_add_from_string (builder,
338                                "<interface>"
339                                "  <object class='GtkDialog' id='plugin-dialog'>"
340                                "    <property name='border-width'>12</property>"
341                                "    <property name='title'>Plugins</property>"
342                                "    <child internal-child='vbox'>"
343                                "      <object class='GtkBox' id='content-area'>"
344                                "        <property name='visible'>True</property>"
345                                "        <child>"
346                                "          <object class='GtkCheckButton' id='red-plugin'>"
347                                "            <property name='label'>Red Plugin - turn your text red</property>"
348                                "            <property name='visible'>True</property>"
349                                "          </object>"
350                                "        </child>"
351                                "        <child>"
352                                "          <object class='GtkCheckButton' id='black-plugin'>"
353                                "            <property name='label'>Black Plugin - turn your text black</property>"
354                                "            <property name='visible'>True</property>"
355                                "          </object>"
356                                "        </child>"
357                                "      </object>"
358                                "    </child>"
359                                "    <child internal-child='action_area'>"
360                                "      <object class='GtkButtonBox' id='action-area'>"
361                                "        <property name='visible'>True</property>"
362                                "        <child>"
363                                "          <object class='GtkButton' id='close-button'>"
364                                "            <property name='label'>Close</property>"
365                                "            <property name='visible'>True</property>"
366                                "          </object>"
367                                "        </child>"
368                                "      </object>"
369                                "    </child>"
370                                "    <action-widgets>"
371                                "      <action-widget response='-5'>close-button</action-widget>"
372                                "    </action-widgets>"
373                                "  </object>"
374                                "</interface>", -1, &error);
375   if (error)
376     {
377       g_warning ("%s", error->message);
378       g_error_free (error);
379       return;
380     }
381
382   dialog = (GtkWidget *)gtk_builder_get_object (builder, "plugin-dialog");
383   check = (GtkWidget *)gtk_builder_get_object (builder, "red-plugin");
384   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("red"));
385   g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "red");
386   check = (GtkWidget *)gtk_builder_get_object (builder, "black-plugin");
387   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("black"));
388   g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "black");
389
390   g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
391
392   gtk_window_present (GTK_WINDOW (dialog));
393 }
394
395 static GActionEntry app_entries[] = {
396   { "about", show_about, NULL, NULL, NULL },
397   { "quit", quit_app, NULL, NULL, NULL },
398   { "plugins", configure_plugins, NULL, NULL, NULL },
399 };
400
401 static void
402 plug_man_startup (GApplication *application)
403 {
404   GtkBuilder *builder;
405
406   G_APPLICATION_CLASS (plug_man_parent_class)
407     ->startup (application);
408
409   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
410
411   builder = gtk_builder_new ();
412   gtk_builder_add_from_string (builder,
413                                "<interface>"
414                                "  <menu id='app-menu'>"
415                                "    <section>"
416                                "      <item label='_About Plugman' action='app.about'/>"
417                                "    </section>"
418                                "    <section>"
419                                "      <item label='_Quit' action='app.quit' accel='<Primary>q'/>"
420                                "    </section>"
421                                "  </menu>"
422                                "  <menu id='menubar'>"
423                                "    <submenu label='_Edit'>"
424                                "      <section>"
425                                "        <item label='_Copy' action='win.copy'/>"
426                                "        <item label='_Paste' action='win.paste'/>"
427                                "      </section>"
428                                "      <section id='plugins'>"
429                                "      </section>"
430                                "      <section>"
431                                "        <item label='Plugins' action='app.plugins'/>"
432                                "      </section>"
433                                "    </submenu>"
434                                "    <submenu label='_View'>"
435                                "      <section>"
436                                "        <item label='_Fullscreen' action='win.fullscreen'/>"
437                                "      </section>"
438                                "    </submenu>"
439                                "  </menu>"
440                                "</interface>", -1, NULL);
441   g_application_set_app_menu (application, G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
442   g_application_set_menubar (application, G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
443   g_object_unref (builder);
444 }
445
446 static void
447 plug_man_init (PlugMan *app)
448 {
449 }
450
451 static void
452 plug_man_class_init (PlugManClass *class)
453 {
454   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
455   GObjectClass *object_class = G_OBJECT_CLASS (class);
456
457   application_class->startup = plug_man_startup;
458   application_class->activate = plug_man_activate;
459   application_class->open = plug_man_open;
460
461   object_class->finalize = plug_man_finalize;
462
463 }
464
465 PlugMan *
466 plug_man_new (void)
467 {
468   g_type_init ();
469
470   return g_object_new (plug_man_get_type (),
471                        "application-id", "org.gtk.Test.plugman",
472                        "flags", G_APPLICATION_HANDLES_OPEN,
473                        NULL);
474 }
475
476 int
477 main (int argc, char **argv)
478 {
479   PlugMan *plug_man;
480   int status;
481
482   plug_man = plug_man_new ();
483   gtk_application_add_accelerator (GTK_APPLICATION (plug_man),
484                                    "F11", "win.fullscreen", NULL);
485   status = g_application_run (G_APPLICATION (plug_man), argc, argv);
486   g_object_unref (plug_man);
487
488   return status;
489 }