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)
190 return (GMenuModel*) g_object_get_data (G_OBJECT (g_application_get_default ()), "plugin-menu");
194 plugin_action (GAction *action,
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");
209 gdk_rgba_parse (&color, g_action_get_name (action));
211 gtk_widget_override_color (text, 0, &color);
215 enable_plugin (const gchar *name)
217 GMenuModel *plugin_menu;
220 g_print ("Enabling '%s' plugin\n", name);
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);
228 plugin_menu = find_plugin_menu ();
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);
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);
250 g_warning ("Plugin menu not found\n");
252 if (g_strcmp0 (name, "red") == 0)
253 is_red_plugin_enabled = TRUE;
255 is_black_plugin_enabled = TRUE;
259 disable_plugin (const gchar *name)
261 GMenuModel *plugin_menu;
263 g_print ("Disabling '%s' plugin\n", name);
265 plugin_menu = find_plugin_menu ();
271 for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
273 if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id) &&
274 g_strcmp0 (id, name) == 0)
276 g_menu_remove (G_MENU (plugin_menu), i);
277 g_print ("Menus of '%s' plugin removed\n", name);
282 g_warning ("Plugin menu not found\n");
284 g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), name);
285 g_print ("Actions of '%s' plugin removed\n", name);
287 if (g_strcmp0 (name, "red") == 0)
288 is_red_plugin_enabled = FALSE;
290 is_black_plugin_enabled = FALSE;
294 enable_or_disable_plugin (GtkToggleButton *button,
297 if (plugin_enabled (name))
298 disable_plugin (name);
300 enable_plugin (name);
305 configure_plugins (GSimpleAction *action,
312 GError *error = NULL;
314 builder = gtk_builder_new ();
315 gtk_builder_add_from_string (builder,
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>"
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>"
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>"
337 " <child internal-child='action_area'>"
338 " <object class='GtkButtonBox' id='action-area'>"
339 " <property name='visible'>True</property>"
341 " <object class='GtkButton' id='close-button'>"
342 " <property name='label' translatable='yes'>Close</property>"
343 " <property name='visible'>True</property>"
349 " <action-widget response='-5'>close-button</action-widget>"
352 "</interface>", -1, &error);
355 g_warning ("%s", error->message);
356 g_error_free (error);
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");
368 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
370 gtk_window_present (GTK_WINDOW (dialog));
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 },
380 plug_man_startup (GApplication *application)
384 G_APPLICATION_CLASS (plug_man_parent_class)
385 ->startup (application);
387 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
389 builder = gtk_builder_new ();
390 gtk_builder_add_from_string (builder,
392 " <menu id='app-menu'>"
395 " <attribute name='label' translatable='yes'>_About Plugman</attribute>"
396 " <attribute name='action'>app.about</attribute>"
401 " <attribute name='label' translatable='yes'>_Quit</attribute>"
402 " <attribute name='action'>app.quit</attribute>"
403 " <attribute name='accel'><Primary>q</attribute>"
407 " <menu id='menubar'>"
409 " <attribute name='label' translatable='yes'>_Edit</attribute>"
412 " <attribute name='label' translatable='yes'>_Copy</attribute>"
413 " <attribute name='action'>win.copy</attribute>"
416 " <attribute name='label' translatable='yes'>_Paste</attribute>"
417 " <attribute name='action'>win.paste</attribute>"
420 " <item><link name='section' id='plugins'>"
424 " <attribute name='label' translatable='yes'>Plugins</attribute>"
425 " <attribute name='action'>app.plugins</attribute>"
430 " <attribute name='label' translatable='yes'>_View</attribute>"
433 " <attribute name='label' translatable='yes'>_Fullscreen</attribute>"
434 " <attribute name='action'>win.fullscreen</attribute>"
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);
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;
468 return g_object_new (plug_man_get_type (),
469 "application-id", "org.gtk.Test.plugman",
470 "flags", G_APPLICATION_HANDLES_OPEN,
475 main (int argc, char **argv)
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);