]> Pileus Git - ~andy/gtk/blob - examples/plugman.c
stylecontext: Do invalidation on first resize container
[~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   return (GMenuModel*) g_object_get_data (G_OBJECT (g_application_get_default ()), "plugin-menu");
191 }
192
193 static void
194 plugin_action (GAction  *action,
195                GVariant *parameter,
196                gpointer  data)
197 {
198   GApplication *app;
199   GList *list;
200   GtkWindow *window;
201   GtkWidget *text;
202   GdkRGBA color;
203
204   app = g_application_get_default ();
205   list = gtk_application_get_windows (GTK_APPLICATION (app));
206   window = GTK_WINDOW (list->data);
207   text = g_object_get_data ((GObject*)window, "plugman-text");
208
209   gdk_rgba_parse (&color, g_action_get_name (action));
210
211   gtk_widget_override_color (text, 0, &color);
212 }
213
214 static void
215 enable_plugin (const gchar *name)
216 {
217   GMenuModel *plugin_menu;
218   GAction *action;
219
220   g_print ("Enabling '%s' plugin\n", name);
221
222   action = (GAction *)g_simple_action_new (name, NULL);
223   g_signal_connect (action, "activate", G_CALLBACK (plugin_action), (gpointer)name);
224   g_action_map_add_action (G_ACTION_MAP (g_application_get_default ()), action);
225   g_print ("Actions of '%s' plugin added\n", name);
226   g_object_unref (action);
227
228   plugin_menu = find_plugin_menu ();
229   if (plugin_menu)
230     {
231       GMenu *section;
232       GMenuItem *item;
233       gchar *label;
234       gchar *action_name;
235
236       section = g_menu_new ();
237       label = g_strdup_printf ("Turn text %s", name);
238       action_name = g_strconcat ("app.", name, NULL);
239       g_menu_insert (section, 0, label, action_name);
240       g_free (label);
241       g_free (action_name);
242       item = g_menu_item_new_section (NULL, (GMenuModel*)section);
243       g_menu_item_set_attribute (item, "id", "s", name);
244       g_menu_append_item (G_MENU (plugin_menu), item);
245       g_object_unref (item);
246       g_object_unref (section);
247       g_print ("Menus of '%s' plugin added\n", name);
248     }
249   else
250     g_warning ("Plugin menu not found\n");
251
252   if (g_strcmp0 (name, "red") == 0)
253     is_red_plugin_enabled = TRUE;
254   else
255     is_black_plugin_enabled = TRUE;
256 }
257
258 static void
259 disable_plugin (const gchar *name)
260 {
261   GMenuModel *plugin_menu;
262
263   g_print ("Disabling '%s' plugin\n", name);
264
265   plugin_menu = find_plugin_menu ();
266   if (plugin_menu)
267     {
268       const gchar *id;
269       gint i;
270
271       for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
272         {
273            if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id) &&
274                g_strcmp0 (id, name) == 0)
275              {
276                g_menu_remove (G_MENU (plugin_menu), i);
277                g_print ("Menus of '%s' plugin removed\n", name);
278              }
279         }
280     }
281   else
282     g_warning ("Plugin menu not found\n");
283
284   g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), name);
285   g_print ("Actions of '%s' plugin removed\n", name);
286
287   if (g_strcmp0 (name, "red") == 0)
288     is_red_plugin_enabled = FALSE;
289   else
290     is_black_plugin_enabled = FALSE;
291 }
292
293 static void
294 enable_or_disable_plugin (GtkToggleButton *button,
295                           const gchar     *name)
296 {
297   if (plugin_enabled (name))
298     disable_plugin (name);
299   else
300     enable_plugin (name);
301 }
302
303
304 static void
305 configure_plugins (GSimpleAction *action,
306                    GVariant      *parameter,
307                    gpointer       user_data)
308 {
309   GtkBuilder *builder;
310   GtkWidget *dialog;
311   GtkWidget *check;
312   GError *error = NULL;
313
314   builder = gtk_builder_new ();
315   gtk_builder_add_from_string (builder,
316                                "<interface>"
317                                "  <object class='GtkDialog' id='plugin-dialog'>"
318                                "    <property name='border-width'>12</property>"
319                                "    <property name='title'>Plugins</property>"
320                                "    <child internal-child='vbox'>"
321                                "      <object class='GtkBox' id='content-area'>"
322                                "        <property name='visible'>True</property>"
323                                "        <child>"
324                                "          <object class='GtkCheckButton' id='red-plugin'>"
325                                "            <property name='label' translatable='yes'>Red Plugin - turn your text red</property>"
326                                "            <property name='visible'>True</property>"
327                                "          </object>"
328                                "        </child>"
329                                "        <child>"
330                                "          <object class='GtkCheckButton' id='black-plugin'>"
331                                "            <property name='label' translatable='yes'>Black Plugin - turn your text black</property>"
332                                "            <property name='visible'>True</property>"
333                                "          </object>"
334                                "        </child>"
335                                "      </object>"
336                                "    </child>"
337                                "    <child internal-child='action_area'>"
338                                "      <object class='GtkButtonBox' id='action-area'>"
339                                "        <property name='visible'>True</property>"
340                                "        <child>"
341                                "          <object class='GtkButton' id='close-button'>"
342                                "            <property name='label' translatable='yes'>Close</property>"
343                                "            <property name='visible'>True</property>"
344                                "          </object>"
345                                "        </child>"
346                                "      </object>"
347                                "    </child>"
348                                "    <action-widgets>"
349                                "      <action-widget response='-5'>close-button</action-widget>"
350                                "    </action-widgets>"
351                                "  </object>"
352                                "</interface>", -1, &error);
353   if (error)
354     {
355       g_warning ("%s", error->message);
356       g_error_free (error);
357       return;
358     }
359
360   dialog = (GtkWidget *)gtk_builder_get_object (builder, "plugin-dialog");
361   check = (GtkWidget *)gtk_builder_get_object (builder, "red-plugin");
362   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("red"));
363   g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "red");
364   check = (GtkWidget *)gtk_builder_get_object (builder, "black-plugin");
365   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("black"));
366   g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "black");
367
368   g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
369
370   gtk_window_present (GTK_WINDOW (dialog));
371 }
372
373 static GActionEntry app_entries[] = {
374   { "about", show_about, NULL, NULL, NULL },
375   { "quit", quit_app, NULL, NULL, NULL },
376   { "plugins", configure_plugins, NULL, NULL, NULL },
377 };
378
379 static void
380 plug_man_startup (GApplication *application)
381 {
382   GtkBuilder *builder;
383
384   G_APPLICATION_CLASS (plug_man_parent_class)
385     ->startup (application);
386
387   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
388
389   builder = gtk_builder_new ();
390   gtk_builder_add_from_string (builder,
391                                "<interface>"
392                                "  <menu id='app-menu'>"
393                                "    <section>"
394                                "      <item>"
395                                "        <attribute name='label' translatable='yes'>_About Plugman</attribute>"
396                                "        <attribute name='action'>app.about</attribute>"
397                                "      </item>"
398                                "    </section>"
399                                "    <section>"
400                                "      <item>"
401                                "        <attribute name='label' translatable='yes'>_Quit</attribute>"
402                                "        <attribute name='action'>app.quit</attribute>"
403                                "        <attribute name='accel'>&lt;Primary&gt;q</attribute>"
404                                "      </item>"
405                                "    </section>"
406                                "  </menu>"
407                                "  <menu id='menubar'>"
408                                "    <submenu>"
409                                "      <attribute name='label' translatable='yes'>_Edit</attribute>"
410                                "      <section>"
411                                "        <item>"
412                                "          <attribute name='label' translatable='yes'>_Copy</attribute>"
413                                "          <attribute name='action'>win.copy</attribute>"
414                                "        </item>"
415                                "        <item>"
416                                "          <attribute name='label' translatable='yes'>_Paste</attribute>"
417                                "          <attribute name='action'>win.paste</attribute>"
418                                "        </item>"
419                                "      </section>"
420                                "      <item><link name='section' id='plugins'>"
421                                "      </link></item>"
422                                "      <section>"
423                                "        <item>"
424                                "          <attribute name='label' translatable='yes'>Plugins</attribute>"
425                                "          <attribute name='action'>app.plugins</attribute>"
426                                "        </item>"
427                                "      </section>"
428                                "    </submenu>"
429                                "    <submenu>"
430                                "      <attribute name='label' translatable='yes'>_View</attribute>"
431                                "      <section>"
432                                "        <item>"
433                                "          <attribute name='label' translatable='yes'>_Fullscreen</attribute>"
434                                "          <attribute name='action'>win.fullscreen</attribute>"
435                                "        </item>"
436                                "      </section>"
437                                "    </submenu>"
438                                "  </menu>"
439                                "</interface>", -1, NULL);
440   gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
441   gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
442   g_object_set_data_full (G_OBJECT (application), "plugin-menu", gtk_builder_get_object (builder, "plugins"), g_object_unref);
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   return g_object_new (plug_man_get_type (),
469                        "application-id", "org.gtk.Test.plugman",
470                        "flags", G_APPLICATION_HANDLES_OPEN,
471                        NULL);
472 }
473
474 int
475 main (int argc, char **argv)
476 {
477   PlugMan *plug_man;
478   int status;
479
480   plug_man = plug_man_new ();
481   gtk_application_add_accelerator (GTK_APPLICATION (plug_man),
482                                    "F11", "win.fullscreen", NULL);
483   status = g_application_run (G_APPLICATION (plug_man), argc, argv);
484   g_object_unref (plug_man);
485
486   return status;
487 }