]> Pileus Git - grits/blob - src/grits-demo.c
Remove deprecated thread functions
[grits] / src / grits-demo.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gdk/gdkkeysyms.h>
21
22 #include "grits.h"
23
24
25 static GritsPrefs   *prefs;
26 static GritsPlugins *plugins;
27 static GritsViewer  *viewer;
28
29
30 /*************
31  * Callbacks *
32  *************/
33 static gboolean on_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
34 {
35         gtk_main_quit();
36         return TRUE;
37 }
38
39 static void on_offline(GtkToggleAction *action, gpointer _)
40 {
41         gboolean active = gtk_toggle_action_get_active(action);
42         grits_viewer_set_offline(viewer, active);
43 }
44
45 static void on_plugin(GtkToggleAction *action, GtkWidget *notebook)
46 {
47         const gchar *name = gtk_action_get_name(GTK_ACTION(action));
48         gboolean active = gtk_toggle_action_get_active(action);
49         if (active) {
50                 GritsPlugin *plugin = grits_plugins_enable(plugins, name,
51                                 GRITS_VIEWER(viewer), prefs);
52                 GtkWidget *config = grits_plugin_get_config(plugin);
53                 if (config) {
54                         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), config,
55                                         gtk_label_new(name));
56                         gtk_widget_show_all(config);
57                 }
58         } else {
59                 grits_plugins_disable(plugins, name);
60                 guint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
61                 for (int i = 0; i < n_pages; i++) {
62                         GtkWidget *body = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), i);
63                         if (!body) continue;
64                         GtkWidget *tab = gtk_notebook_get_tab_label(GTK_NOTEBOOK(notebook), body);
65                         if (!tab) continue;
66                         const gchar *tab_name = gtk_label_get_text(GTK_LABEL(tab));
67                         if (tab_name && g_str_equal(name, tab_name))
68                                 gtk_notebook_remove_page(GTK_NOTEBOOK(notebook), i);
69                 }
70                 gtk_widget_queue_draw(GTK_WIDGET(viewer));
71         }
72 }
73
74
75 /******************
76  * Static UI Data *
77  ******************/
78 static const gchar menu_xml[] =
79         "<ui>"
80         "  <menubar name='Menu' >"
81         "    <menu name='File'          action='File' >"
82         "      <menuitem name='Offline' action='Offline' />"
83         "      <menuitem name='Quit'    action='Quit' />"
84         "    </menu>"
85         "    <menu name='Plugins'       action='Plugins' >"
86         "      <placeholder name='PluginsItems' />"
87         "    </menu>"
88         "  </menubar>"
89         "</ui>"
90 ;
91
92 static GtkActionEntry action_data[] =
93 {
94         /* name, stock id, label, accel, tooltip, callback */
95         {"File",    NULL, "_File"},
96         {"Plugins", NULL, "_Plugins"},
97         {"Quit", GTK_STOCK_QUIT, "_Quit", "q", NULL,
98                 G_CALLBACK(gtk_main_quit)},
99 };
100 static GtkToggleActionEntry toggle_action_data[] =
101 {
102         /* name, stock id, label, accel, tooltip, callback, is_active */
103         {"Offline", GTK_STOCK_DISCONNECT, "_Offline", NULL, NULL,
104                 G_CALLBACK(on_offline), FALSE},
105 };
106
107
108 /***********
109  * Helpers *
110  ***********/
111 static GtkUIManager *setup_actions()
112 {
113         GtkUIManager   *manager = gtk_ui_manager_new();
114         GtkActionGroup *actions = gtk_action_group_new("Actions");
115         gtk_action_group_add_actions(actions, action_data,
116                         G_N_ELEMENTS(action_data), NULL);
117         gtk_action_group_add_toggle_actions(actions, toggle_action_data,
118                         G_N_ELEMENTS(toggle_action_data), NULL);
119         gtk_ui_manager_insert_action_group(manager, actions, 0);
120         gtk_ui_manager_add_ui_from_string(manager, menu_xml, sizeof(menu_xml)-1, NULL);
121         g_object_unref(actions);
122         return manager;
123 }
124
125 static GtkWidget *setup_window(GtkUIManager *manager, GtkWidget **_notebook)
126 {
127         GtkWidget *window   = gtk_window_new(GTK_WINDOW_TOPLEVEL);
128         GtkWidget *menu     = gtk_ui_manager_get_widget(manager, "/Menu");
129         GtkWidget *notebook = gtk_notebook_new();
130         GtkWidget *vbox     = gtk_vbox_new(FALSE, 0);
131         gtk_container_add(GTK_CONTAINER(window), vbox);
132         gtk_box_pack_start(GTK_BOX(vbox), menu,               FALSE, TRUE, 0);
133         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(viewer), TRUE,  TRUE, 0);
134         gtk_box_pack_start(GTK_BOX(vbox), notebook,           FALSE, TRUE, 0);
135         g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
136         gtk_window_add_accel_group(GTK_WINDOW(window),
137                         gtk_ui_manager_get_accel_group(manager));
138         gtk_widget_set_size_request(GTK_WIDGET(viewer), 400, 300);
139         *_notebook = notebook;
140         return window;
141 }
142
143 static void setup_plugins(GtkUIManager *manager, GtkNotebook *notebook)
144 {
145         GtkActionGroup *actions = gtk_action_group_new("Plugins");
146         gtk_ui_manager_insert_action_group(manager, actions, 1);
147         guint merge_id = gtk_ui_manager_new_merge_id(manager);
148         for (GList *cur = grits_plugins_available(plugins); cur; cur = cur->next) {
149                 gchar *name = cur->data;
150                 GtkToggleAction *action = gtk_toggle_action_new(name, name, NULL, NULL);
151                 g_signal_connect(action, "toggled", G_CALLBACK(on_plugin), notebook);
152                 gtk_action_group_add_action(actions, GTK_ACTION(action));
153                 gtk_ui_manager_add_ui(manager, merge_id, "/Menu/Plugins", name, name,
154                                 GTK_UI_MANAGER_AUTO, TRUE);
155                 if (grits_prefs_get_boolean_v(prefs, "plugins", name, NULL))
156                         gtk_toggle_action_set_active(action, TRUE);
157         }
158 }
159
160 static void restore_states(GtkUIManager *manager)
161 {
162         GtkAction *action = gtk_ui_manager_get_action(manager, "/Menu/File/Offline");
163         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action),
164                         grits_viewer_get_offline(viewer));
165 }
166
167 int main(int argc, char **argv)
168 {
169         gdk_threads_init();
170         gtk_init(&argc, &argv);
171
172         prefs   = grits_prefs_new(NULL, NULL);
173         plugins = grits_plugins_new(g_getenv("GRITS_PLUGIN_PATH"), prefs);
174         viewer  = grits_opengl_new(plugins, prefs);
175
176         gdk_threads_enter();
177
178         GtkWidget    *notebook = NULL;
179         GtkUIManager *manager  = setup_actions();
180         GtkWidget    *window   = setup_window(manager, &notebook);
181         gtk_widget_show_all(window);
182         setup_plugins(manager, GTK_NOTEBOOK(notebook));
183         restore_states(manager);
184         gtk_ui_manager_ensure_update(manager);
185
186         gtk_main();
187
188         grits_plugins_free(plugins);
189         g_object_unref(prefs);
190
191         gdk_threads_leave();
192
193         g_debug("GritsDemo: main - refs=%d,%d",
194                         G_OBJECT(manager)->ref_count,
195                         G_OBJECT(window)->ref_count);
196         g_object_unref(manager);
197         gtk_widget_destroy(window);
198
199         prefs   = NULL;
200         plugins = NULL;
201         viewer  = NULL;
202
203         gdk_display_close(gdk_display_get_default());
204
205         return 0;
206 }