5 activate_toggle (GSimpleAction *action,
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);
17 change_fullscreen_state (GSimpleAction *action,
21 if (g_variant_get_boolean (state))
22 gtk_window_fullscreen (user_data);
24 gtk_window_unfullscreen (user_data);
26 g_simple_action_set_state (action, state);
30 get_clipboard (GtkWidget *widget)
32 return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
36 window_copy (GSimpleAction *action,
40 GtkWindow *window = GTK_WINDOW (user_data);
41 GtkTextView *text = g_object_get_data ((GObject*)window, "plugman-text");
43 gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (text),
44 get_clipboard ((GtkWidget*) text));
48 window_paste (GSimpleAction *action,
52 GtkWindow *window = GTK_WINDOW (user_data);
53 GtkTextView *text = g_object_get_data ((GObject*)window, "plugman-text");
55 gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (text),
56 get_clipboard ((GtkWidget*) text),
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 }
69 new_window (GApplication *app,
72 GtkWidget *window, *grid, *scrolled, *view;
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");
79 grid = gtk_grid_new ();
80 gtk_container_add (GTK_CONTAINER (window), grid);
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 ();
87 g_object_set_data ((GObject*)window, "plugman-text", view);
89 gtk_container_add (GTK_CONTAINER (scrolled), view);
91 gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 0, 1, 1);
98 if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
100 GtkTextBuffer *buffer;
102 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
103 gtk_text_buffer_set_text (buffer, contents, length);
108 gtk_widget_show_all (GTK_WIDGET (window));
112 plug_man_activate (GApplication *application)
114 new_window (application, NULL);
118 plug_man_open (GApplication *application,
125 for (i = 0; i < n_files; i++)
126 new_window (application, files[i]);
129 typedef GtkApplication PlugMan;
130 typedef GtkApplicationClass PlugManClass;
132 G_DEFINE_TYPE (PlugMan, plug_man, GTK_TYPE_APPLICATION)
135 plug_man_finalize (GObject *object)
137 G_OBJECT_CLASS (plug_man_parent_class)->finalize (object);
141 show_about (GSimpleAction *action,
145 gtk_show_about_dialog (NULL,
146 "program-name", "Plugman",
147 "title", "About Plugman",
148 "comments", "A cheap Bloatpad clone.",
154 quit_app (GSimpleAction *action,
161 g_print ("Going down...\n");
163 list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
169 gtk_widget_destroy (GTK_WIDGET (win));
175 static gboolean is_red_plugin_enabled;
176 static gboolean is_black_plugin_enabled;
179 plugin_enabled (const gchar *name)
181 if (g_strcmp0 (name, "red") == 0)
182 return is_red_plugin_enabled;
184 return is_black_plugin_enabled;
188 find_plugin_menu (void)
193 const gchar *label, *id;
195 menubar = g_application_get_menubar (g_application_get_default ());
196 for (i = 0; i < g_menu_model_get_n_items (menubar); i++)
198 if (g_menu_model_get_item_attribute (menubar, i, "label", "s", &label) &&
199 g_strcmp0 (label, "_Edit") == 0)
201 menu = g_menu_model_get_item_link (menubar, i, "submenu");
202 for (j = 0; j < g_menu_model_get_n_items (menu); j++)
204 if (g_menu_model_get_item_attribute (menu, j, "id", "s", &id) &&
205 g_strcmp0 (id, "plugins") == 0)
207 return g_menu_model_get_item_link (menu, j, "section");
217 plugin_action (GAction *action,
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");
232 gdk_rgba_parse (&color, g_action_get_name (action));
234 gtk_widget_override_color (text, 0, &color);
238 enable_plugin (const gchar *name)
240 GMenuModel *plugin_menu;
243 g_print ("Enabling '%s' plugin\n", name);
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);
250 plugin_menu = find_plugin_menu ();
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);
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);
272 g_warning ("Plugin menu not found\n");
274 if (g_strcmp0 (name, "red") == 0)
275 is_red_plugin_enabled = TRUE;
277 is_black_plugin_enabled = TRUE;
281 disable_plugin (const gchar *name)
283 GMenuModel *plugin_menu;
285 g_print ("Disabling '%s' plugin\n", name);
287 plugin_menu = find_plugin_menu ();
293 for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
295 if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id) &&
296 g_strcmp0 (id, name) == 0)
298 g_menu_remove (G_MENU (plugin_menu), i);
299 g_print ("Menus of '%s' plugin removed\n", name);
304 g_warning ("Plugin menu not found\n");
306 g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), name);
307 g_print ("Actions of '%s' plugin removed\n", name);
309 if (g_strcmp0 (name, "red") == 0)
310 is_red_plugin_enabled = FALSE;
312 is_black_plugin_enabled = FALSE;
316 enable_or_disable_plugin (GtkToggleButton *button,
319 if (plugin_enabled (name))
320 disable_plugin (name);
322 enable_plugin (name);
327 configure_plugins (GSimpleAction *action,
334 GError *error = NULL;
336 builder = gtk_builder_new ();
337 gtk_builder_add_from_string (builder,
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>"
346 " <object class='GtkCheckButton' id='red-plugin'>"
347 " <property name='label'>Red Plugin - turn your text red</property>"
348 " <property name='visible'>True</property>"
352 " <object class='GtkCheckButton' id='black-plugin'>"
353 " <property name='label'>Black Plugin - turn your text black</property>"
354 " <property name='visible'>True</property>"
359 " <child internal-child='action_area'>"
360 " <object class='GtkButtonBox' id='action-area'>"
361 " <property name='visible'>True</property>"
363 " <object class='GtkButton' id='close-button'>"
364 " <property name='label'>Close</property>"
365 " <property name='visible'>True</property>"
371 " <action-widget response='-5'>close-button</action-widget>"
374 "</interface>", -1, &error);
377 g_warning ("%s", error->message);
378 g_error_free (error);
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");
390 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
392 gtk_window_present (GTK_WINDOW (dialog));
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 },
402 plug_man_startup (GApplication *application)
406 G_APPLICATION_CLASS (plug_man_parent_class)
407 ->startup (application);
409 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
411 builder = gtk_builder_new ();
412 gtk_builder_add_from_string (builder,
414 " <menu id='app-menu'>"
416 " <item label='_About Plugman' action='app.about'/>"
419 " <item label='_Quit' action='app.quit' accel='<Primary>q'/>"
422 " <menu id='menubar'>"
423 " <submenu label='_Edit'>"
425 " <item label='_Copy' action='win.copy'/>"
426 " <item label='_Paste' action='win.paste'/>"
428 " <section id='plugins'>"
431 " <item label='Plugins' action='app.plugins'/>"
434 " <submenu label='_View'>"
436 " <item label='_Fullscreen' action='win.fullscreen'/>"
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);
447 plug_man_init (PlugMan *app)
452 plug_man_class_init (PlugManClass *class)
454 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
455 GObjectClass *object_class = G_OBJECT_CLASS (class);
457 application_class->startup = plug_man_startup;
458 application_class->activate = plug_man_activate;
459 application_class->open = plug_man_open;
461 object_class->finalize = plug_man_finalize;
470 return g_object_new (plug_man_get_type (),
471 "application-id", "org.gtk.Test.plugman",
472 "flags", G_APPLICATION_HANDLES_OPEN,
477 main (int argc, char **argv)
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);