1 #include <stdlib.h>
2 #include <gtk/gtk.h>
3
4 static void
activate_toggle(GSimpleAction * action,GVariant * parameter,gpointer user_data)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
change_fullscreen_state(GSimpleAction * action,GVariant * state,gpointer user_data)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 *
get_clipboard(GtkWidget * widget)30 get_clipboard (GtkWidget *widget)
31 {
32 return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
33 }
34
35 static void
window_copy(GSimpleAction * action,GVariant * parameter,gpointer user_data)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
window_paste(GSimpleAction * action,GVariant * parameter,gpointer user_data)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
new_window(GApplication * app,GFile * file)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
plug_man_activate(GApplication * application)112 plug_man_activate (GApplication *application)
113 {
114 new_window (application, NULL);
115 }
116
117 static void
plug_man_open(GApplication * application,GFile ** files,gint n_files,const gchar * hint)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
G_DEFINE_TYPE(PlugMan,plug_man,GTK_TYPE_APPLICATION)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
show_about(GSimpleAction * action,GVariant * parameter,gpointer user_data)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
quit_app(GSimpleAction * action,GVariant * parameter,gpointer user_data)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
plugin_enabled(const gchar * name)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 *
find_plugin_menu(void)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
plugin_action(GAction * action,GVariant * parameter,gpointer data)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 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
212 gtk_widget_override_color (text, 0, &color);
213 G_GNUC_END_IGNORE_DEPRECATIONS
214 }
215
216 static void
enable_plugin(const gchar * name)217 enable_plugin (const gchar *name)
218 {
219 GMenuModel *plugin_menu;
220 GAction *action;
221
222 g_print ("Enabling '%s' plugin\n", name);
223
224 action = (GAction *)g_simple_action_new (name, NULL);
225 g_signal_connect (action, "activate", G_CALLBACK (plugin_action), (gpointer)name);
226 g_action_map_add_action (G_ACTION_MAP (g_application_get_default ()), action);
227 g_print ("Actions of '%s' plugin added\n", name);
228 g_object_unref (action);
229
230 plugin_menu = find_plugin_menu ();
231 if (plugin_menu)
232 {
233 GMenu *section;
234 GMenuItem *item;
235 gchar *label;
236 gchar *action_name;
237
238 section = g_menu_new ();
239 label = g_strdup_printf ("Turn text %s", name);
240 action_name = g_strconcat ("app.", name, NULL);
241 g_menu_insert (section, 0, label, action_name);
242 g_free (label);
243 g_free (action_name);
244 item = g_menu_item_new_section (NULL, (GMenuModel*)section);
245 g_menu_item_set_attribute (item, "id", "s", name);
246 g_menu_append_item (G_MENU (plugin_menu), item);
247 g_object_unref (item);
248 g_object_unref (section);
249 g_print ("Menus of '%s' plugin added\n", name);
250 }
251 else
252 g_warning ("Plugin menu not found");
253
254 if (g_strcmp0 (name, "red") == 0)
255 is_red_plugin_enabled = TRUE;
256 else
257 is_black_plugin_enabled = TRUE;
258 }
259
260 static void
disable_plugin(const gchar * name)261 disable_plugin (const gchar *name)
262 {
263 GMenuModel *plugin_menu;
264
265 g_print ("Disabling '%s' plugin\n", name);
266
267 plugin_menu = find_plugin_menu ();
268 if (plugin_menu)
269 {
270 gint i;
271
272 for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
273 {
274 gchar *id;
275 if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id))
276 {
277 if (g_strcmp0 (id, name) == 0)
278 {
279 g_menu_remove (G_MENU (plugin_menu), i);
280 g_print ("Menus of '%s' plugin removed\n", name);
281 }
282 g_free (id);
283 }
284 }
285 }
286 else
287 g_warning ("Plugin menu not found");
288
289 g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), name);
290 g_print ("Actions of '%s' plugin removed\n", name);
291
292 if (g_strcmp0 (name, "red") == 0)
293 is_red_plugin_enabled = FALSE;
294 else
295 is_black_plugin_enabled = FALSE;
296 }
297
298 static void
enable_or_disable_plugin(GtkToggleButton * button,const gchar * name)299 enable_or_disable_plugin (GtkToggleButton *button,
300 const gchar *name)
301 {
302 if (plugin_enabled (name))
303 disable_plugin (name);
304 else
305 enable_plugin (name);
306 }
307
308
309 static void
configure_plugins(GSimpleAction * action,GVariant * parameter,gpointer user_data)310 configure_plugins (GSimpleAction *action,
311 GVariant *parameter,
312 gpointer user_data)
313 {
314 GtkBuilder *builder;
315 GtkWidget *dialog;
316 GtkWidget *check;
317 GError *error = NULL;
318
319 builder = gtk_builder_new ();
320 gtk_builder_add_from_string (builder,
321 "<interface>"
322 " <object class='GtkDialog' id='plugin-dialog'>"
323 " <property name='border-width'>12</property>"
324 " <property name='title'>Plugins</property>"
325 " <child internal-child='vbox'>"
326 " <object class='GtkBox' id='content-area'>"
327 " <property name='visible'>True</property>"
328 " <child>"
329 " <object class='GtkCheckButton' id='red-plugin'>"
330 " <property name='label' translatable='yes'>Red Plugin - turn your text red</property>"
331 " <property name='visible'>True</property>"
332 " </object>"
333 " </child>"
334 " <child>"
335 " <object class='GtkCheckButton' id='black-plugin'>"
336 " <property name='label' translatable='yes'>Black Plugin - turn your text black</property>"
337 " <property name='visible'>True</property>"
338 " </object>"
339 " </child>"
340 " </object>"
341 " </child>"
342 " <child internal-child='action_area'>"
343 " <object class='GtkButtonBox' id='action-area'>"
344 " <property name='visible'>True</property>"
345 " <child>"
346 " <object class='GtkButton' id='close-button'>"
347 " <property name='label' translatable='yes'>Close</property>"
348 " <property name='visible'>True</property>"
349 " </object>"
350 " </child>"
351 " </object>"
352 " </child>"
353 " <action-widgets>"
354 " <action-widget response='-5'>close-button</action-widget>"
355 " </action-widgets>"
356 " </object>"
357 "</interface>", -1, &error);
358 if (error)
359 {
360 g_warning ("%s", error->message);
361 g_error_free (error);
362 goto out;
363 }
364
365 dialog = (GtkWidget *)gtk_builder_get_object (builder, "plugin-dialog");
366 check = (GtkWidget *)gtk_builder_get_object (builder, "red-plugin");
367 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("red"));
368 g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "red");
369 check = (GtkWidget *)gtk_builder_get_object (builder, "black-plugin");
370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), plugin_enabled ("black"));
371 g_signal_connect (check, "toggled", G_CALLBACK (enable_or_disable_plugin), "black");
372
373 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
374
375 gtk_window_present (GTK_WINDOW (dialog));
376
377 out:
378 g_object_unref (builder);
379 }
380
381 static GActionEntry app_entries[] = {
382 { "about", show_about, NULL, NULL, NULL },
383 { "quit", quit_app, NULL, NULL, NULL },
384 { "plugins", configure_plugins, NULL, NULL, NULL },
385 };
386
387 static void
plug_man_startup(GApplication * application)388 plug_man_startup (GApplication *application)
389 {
390 GtkBuilder *builder;
391
392 G_APPLICATION_CLASS (plug_man_parent_class)
393 ->startup (application);
394
395 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
396
397 builder = gtk_builder_new ();
398 gtk_builder_add_from_string (builder,
399 "<interface>"
400 " <menu id='app-menu'>"
401 " <section>"
402 " <item>"
403 " <attribute name='label' translatable='yes'>_About Plugman</attribute>"
404 " <attribute name='action'>app.about</attribute>"
405 " </item>"
406 " </section>"
407 " <section>"
408 " <item>"
409 " <attribute name='label' translatable='yes'>_Quit</attribute>"
410 " <attribute name='action'>app.quit</attribute>"
411 " <attribute name='accel'><Primary>q</attribute>"
412 " </item>"
413 " </section>"
414 " </menu>"
415 " <menu id='menubar'>"
416 " <submenu>"
417 " <attribute name='label' translatable='yes'>_Edit</attribute>"
418 " <section>"
419 " <item>"
420 " <attribute name='label' translatable='yes'>_Copy</attribute>"
421 " <attribute name='action'>win.copy</attribute>"
422 " </item>"
423 " <item>"
424 " <attribute name='label' translatable='yes'>_Paste</attribute>"
425 " <attribute name='action'>win.paste</attribute>"
426 " </item>"
427 " </section>"
428 " <item><link name='section' id='plugins'>"
429 " </link></item>"
430 " <section>"
431 " <item>"
432 " <attribute name='label' translatable='yes'>Plugins</attribute>"
433 " <attribute name='action'>app.plugins</attribute>"
434 " </item>"
435 " </section>"
436 " </submenu>"
437 " <submenu>"
438 " <attribute name='label' translatable='yes'>_View</attribute>"
439 " <section>"
440 " <item>"
441 " <attribute name='label' translatable='yes'>_Fullscreen</attribute>"
442 " <attribute name='action'>win.fullscreen</attribute>"
443 " </item>"
444 " </section>"
445 " </submenu>"
446 " </menu>"
447 "</interface>", -1, NULL);
448 gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
449 gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
450 g_object_set_data_full (G_OBJECT (application), "plugin-menu", gtk_builder_get_object (builder, "plugins"), g_object_unref);
451 g_object_unref (builder);
452 }
453
454 static void
plug_man_init(PlugMan * app)455 plug_man_init (PlugMan *app)
456 {
457 }
458
459 static void
plug_man_class_init(PlugManClass * class)460 plug_man_class_init (PlugManClass *class)
461 {
462 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
463 GObjectClass *object_class = G_OBJECT_CLASS (class);
464
465 application_class->startup = plug_man_startup;
466 application_class->activate = plug_man_activate;
467 application_class->open = plug_man_open;
468
469 object_class->finalize = plug_man_finalize;
470
471 }
472
473 PlugMan *
plug_man_new(void)474 plug_man_new (void)
475 {
476 return g_object_new (plug_man_get_type (),
477 "application-id", "org.gtk.Test.plugman",
478 "flags", G_APPLICATION_HANDLES_OPEN,
479 NULL);
480 }
481
482 int
main(int argc,char ** argv)483 main (int argc, char **argv)
484 {
485 PlugMan *plug_man;
486 int status;
487 const gchar *accels[] = { "F11", NULL };
488
489 plug_man = plug_man_new ();
490 gtk_application_set_accels_for_action (GTK_APPLICATION (plug_man),
491 "win.fullscreen", accels);
492 status = g_application_run (G_APPLICATION (plug_man), argc, argv);
493 g_object_unref (plug_man);
494
495 return status;
496 }
497