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;
178 red_plugin_enabled (void)
180 return is_red_plugin_enabled;
184 find_plugin_menu (void)
189 const gchar *label, *id;
191 menubar = g_application_get_menubar (g_application_get_default ());
192 for (i = 0; i < g_menu_model_get_n_items (menubar); i++)
194 if (g_menu_model_get_item_attribute (menubar, i, "label", "s", &label) &&
195 g_strcmp0 (label, "_Edit") == 0)
197 menu = g_menu_model_get_item_link (menubar, i, "submenu");
198 for (j = 0; j < g_menu_model_get_n_items (menu); j++)
200 if (g_menu_model_get_item_attribute (menu, j, "id", "s", &id) &&
201 g_strcmp0 (id, "plugins") == 0)
203 return g_menu_model_get_item_link (menu, j, "section");
213 red_action (GAction *action,
217 g_print ("Here is where we turn the text red\n");
221 enable_red_plugin (void)
226 g_print ("Enabling 'Red' plugin\n");
228 action = (GAction *)g_simple_action_new ("red-action", NULL);
229 g_signal_connect (action, "activate", G_CALLBACK (red_action), NULL);
230 g_action_map_add_action (G_ACTION_MAP (g_application_get_default ()), action);
231 g_print ("Actions of 'Red' plugin added\n");
233 plugin_menu = find_plugin_menu ();
239 section = g_menu_new ();
240 g_menu_insert (section, 0, "Turn text red", "app.red-action");
241 item = g_menu_item_new_section (NULL, (GMenuModel*)section);
242 g_menu_item_set_attribute (item, "id", "s", "red");
243 g_menu_append_item (plugin_menu, item);
244 g_object_unref (item);
245 g_object_unref (section);
246 g_print ("Menus of 'Red' plugin added\n");
249 g_warning ("Plugin menu not found\n");
251 is_red_plugin_enabled = TRUE;
255 disable_red_plugin (void)
257 GMenuModel *plugin_menu;
259 g_print ("Disabling 'Red' plugin\n");
261 g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()), "app.red-action");
263 plugin_menu = find_plugin_menu ();
269 for (i = 0; i < g_menu_model_get_n_items (plugin_menu); i++)
271 if (g_menu_model_get_item_attribute (plugin_menu, i, "id", "s", &id) &&
272 g_strcmp0 (id, "red") == 0)
274 g_menu_remove (plugin_menu, i);
275 g_print ("Menus of 'Red' plugin removed\n");
280 g_warning ("Plugin menu not found\n");
282 is_red_plugin_enabled = FALSE;
286 enable_or_disable_red_plugin (void)
288 if (red_plugin_enabled ())
289 disable_red_plugin ();
291 enable_red_plugin ();
296 configure_plugins (GSimpleAction *action,
303 GError *error = NULL;
305 builder = gtk_builder_new ();
306 gtk_builder_add_from_string (builder,
308 " <object class='GtkDialog' id='plugin-dialog'>"
309 " <property name='border-width'>12</property>"
310 " <property name='title'>Plugins</property>"
311 " <child internal-child='vbox'>"
312 " <object class='GtkBox' id='content-area'>"
313 " <property name='visible'>True</property>"
315 " <object class='GtkCheckButton' id='red-plugin'>"
316 " <property name='label'>Red Plugin - turn your text red</property>"
317 " <property name='visible'>True</property>"
322 " <child internal-child='action_area'>"
323 " <object class='GtkButtonBox' id='action-area'>"
324 " <property name='visible'>True</property>"
326 " <object class='GtkButton' id='close-button'>"
327 " <property name='label'>Close</property>"
328 " <property name='visible'>True</property>"
334 " <action-widget response='-5'>close-button</action-widget>"
337 "</interface>", -1, &error);
340 g_warning ("%s", error->message);
341 g_error_free (error);
345 dialog = (GtkWidget *)gtk_builder_get_object (builder, "plugin-dialog");
346 red = (GtkWidget *)gtk_builder_get_object (builder, "red-plugin");
347 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (red), red_plugin_enabled ());
348 g_signal_connect (red, "toggled", G_CALLBACK (enable_or_disable_red_plugin), NULL);
350 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
352 gtk_window_present (GTK_WINDOW (dialog));
355 static GActionEntry app_entries[] = {
356 { "about", show_about, NULL, NULL, NULL },
357 { "quit", quit_app, NULL, NULL, NULL },
358 { "plugins", configure_plugins, NULL, NULL, NULL },
362 plug_man_startup (GApplication *application)
366 G_APPLICATION_CLASS (plug_man_parent_class)
367 ->startup (application);
369 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
371 builder = gtk_builder_new ();
372 gtk_builder_add_from_string (builder,
374 " <menu id='app-menu'>"
376 " <item label='_About Plugman' action='app.about'/>"
379 " <item label='_Quit' action='app.quit' accel='<Primary>q'/>"
382 " <menu id='menubar'>"
383 " <submenu label='_Edit'>"
385 " <item label='_Copy' action='win.copy'/>"
386 " <item label='_Paste' action='win.paste'/>"
388 " <section id='plugins'>"
391 " <item label='Plugins' action='app.plugins'/>"
394 " <submenu label='_View'>"
396 " <item label='_Fullscreen' action='win.fullscreen'/>"
400 "</interface>", -1, NULL);
401 g_application_set_app_menu (application, G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
402 g_application_set_menubar (application, G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
403 g_object_unref (builder);
407 plug_man_init (PlugMan *app)
412 plug_man_class_init (PlugManClass *class)
414 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
415 GObjectClass *object_class = G_OBJECT_CLASS (class);
417 application_class->startup = plug_man_startup;
418 application_class->activate = plug_man_activate;
419 application_class->open = plug_man_open;
421 object_class->finalize = plug_man_finalize;
430 return g_object_new (plug_man_get_type (),
431 "application-id", "org.gtk.Test.plugman",
432 "flags", G_APPLICATION_HANDLES_OPEN,
437 main (int argc, char **argv)
442 plug_man = plug_man_new ();
443 gtk_application_add_accelerator (GTK_APPLICATION (plug_man),
444 "F11", "win.fullscreen", NULL);
445 status = g_application_run (G_APPLICATION (plug_man), argc, argv);
446 g_object_unref (plug_man);