]> Dogcows Code - chaz/tint2/blob - src/tint2conf/main.c
tint2conf : store/load selected theme
[chaz/tint2] / src / tint2conf / main.c
1 /**************************************************************************
2 *
3 * Tint2conf
4 *
5 * Copyright (C) 2009 Thierry lorthiois (lorthiois@bbsoft.fr)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <locale.h>
24 #include <X11/Xlib.h>
25 #include <gdk/gdkx.h>
26 #include <gtk/gtk.h>
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <glib/gi18n.h>
30
31 #include "../version.h"
32 #include "common.h"
33 #include "theme_view.h"
34
35
36
37 // default config file and directory
38 char *g_path_config = NULL;
39 char *g_path_dir = NULL;
40 char *g_default_theme = NULL;
41 int g_width, g_height;
42
43 GtkWidget *g_window;
44 GtkWidget *g_theme_view;
45
46 static GtkUIManager *globalUIManager = NULL;
47
48 static void menuAddWidget (GtkUIManager *, GtkWidget *, GtkContainer *);
49
50 // action on menus
51 static void menuAdd (GtkWindow * parent);
52 static void menuSaveAs (GtkWindow *parent);
53 static void menuDelete (void);
54 static void menuProperties (void);
55 static void menuQuit (void);
56 static void menuRefresh (void);
57 static void menuRefreshAll (void);
58 static void menuPreferences (void);
59 static void menuApply (void);
60 static void menuAbout(GtkWindow * parent);
61
62 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata);
63 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata);
64 static void viewRowActivated( GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
65
66
67 // theme files
68 static void load_theme();
69 static void read_config();
70 static void write_config();
71 static void check_theme();
72
73
74 // define menubar, toolbar and popup
75 static const char *global_ui =
76 "<ui>"
77 " <menubar name='MenuBar'>"
78 " <menu action='ThemeMenu'>"
79 " <menuitem action='ThemeAdd'/>"
80 " <menuitem action='ThemeSaveAs'/>"
81 " <separator/>"
82 " <menuitem action='ThemeDelete'/>"
83 " <separator/>"
84 " <menuitem action='ThemeProperties'/>"
85 " <separator/>"
86 " <menuitem action='ThemeQuit'/>"
87 " </menu>"
88 " <menu action='EditMenu'>"
89 " <menuitem action='EditRefresh'/>"
90 " <menuitem action='EditRefreshAll'/>"
91 // " <separator/>"
92 // " <menuitem action='EditPreferences'/>"
93 " </menu>"
94 " <menu action='HelpMenu'>"
95 " <menuitem action='HelpAbout'/>"
96 " </menu>"
97 " </menubar>"
98 " <toolbar name='ToolBar'>"
99 " <toolitem action='ViewApply'/>"
100 " <toolitem action='ThemeProperties'/>"
101 " </toolbar>"
102 " <popup name='ThemePopup'>"
103 " <menuitem action='ViewApply'/>"
104 " <menuitem action='ThemeProperties'/>"
105 " <separator/>"
106 " <menuitem action='ThemeDelete'/>"
107 " </popup>"
108 "</ui>";
109
110
111 // define menubar and toolbar action
112 static GtkActionEntry entries[] = {
113 {"ThemeMenu", NULL, "Theme", NULL, NULL, NULL},
114 {"ThemeAdd", GTK_STOCK_ADD, "_Add...", "<Control>N", "Add theme", G_CALLBACK (menuAdd)},
115 {"ThemeSaveAs", GTK_STOCK_SAVE_AS, "_Save as...", NULL, "Save theme as", G_CALLBACK (menuSaveAs)},
116 {"ThemeDelete", GTK_STOCK_DELETE, "_Delete", NULL, "Delete theme", G_CALLBACK (menuDelete)},
117 {"ThemeProperties", GTK_STOCK_PROPERTIES, "_Properties...", NULL, "Show properties", G_CALLBACK (menuProperties)},
118 {"ThemeQuit", GTK_STOCK_QUIT, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit)},
119 {"EditMenu", NULL, "Edit", NULL, NULL, NULL},
120 {"EditRefresh", GTK_STOCK_REFRESH, "Refresh", NULL, "Refresh", G_CALLBACK (menuRefresh)},
121 {"EditRefreshAll", GTK_STOCK_REFRESH, "Refresh all", NULL, "Refresh all", G_CALLBACK (menuRefreshAll)},
122 // {"EditPreferences", GTK_STOCK_PREFERENCES, "Preferences", NULL, "Preferences", G_CALLBACK (menuPreferences)},
123 {"ViewApply", GTK_STOCK_APPLY, "Apply", NULL, "Apply theme", G_CALLBACK (menuApply)},
124 {"HelpMenu", NULL, "Help", NULL, NULL, NULL},
125 {"HelpAbout", GTK_STOCK_ABOUT, "_About", "<Control>A", "About", G_CALLBACK (menuAbout)}
126 };
127
128
129 int main (int argc, char ** argv)
130 {
131 GtkWidget *vBox = NULL, *scrollbar = NULL;
132 GtkActionGroup *actionGroup;
133
134 gtk_init (&argc, &argv);
135 g_thread_init( NULL );
136 read_config();
137 check_theme();
138 g_width = 300;
139 g_height = 400;
140
141 // define main layout : container, menubar, toolbar
142 g_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
143 gtk_window_set_title(GTK_WINDOW(g_window), _("Panel theming"));
144 gtk_window_resize(GTK_WINDOW(g_window), g_width, g_height);
145 g_signal_connect (G_OBJECT (g_window), "destroy", G_CALLBACK (menuQuit), NULL);
146 vBox = gtk_vbox_new (FALSE, 0);
147 gtk_container_add (GTK_CONTAINER(g_window), vBox);
148
149 actionGroup = gtk_action_group_new ("menuActionGroup");
150 gtk_action_group_add_actions (actionGroup, entries, G_N_ELEMENTS (entries), NULL);
151 globalUIManager = gtk_ui_manager_new();
152 gtk_ui_manager_insert_action_group (globalUIManager, actionGroup, 0);
153 gtk_ui_manager_add_ui_from_string (globalUIManager, global_ui, -1, NULL );
154 g_signal_connect(globalUIManager, "add_widget", G_CALLBACK (menuAddWidget), vBox);
155 gtk_ui_manager_ensure_update(globalUIManager);
156 scrollbar = gtk_scrolled_window_new(NULL, NULL);
157 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
158 gtk_box_pack_start(GTK_BOX(vBox), scrollbar, TRUE, TRUE, 0);
159
160 // define theme view
161 g_theme_view = create_view();
162 gtk_container_add(GTK_CONTAINER(scrollbar), g_theme_view);
163 gtk_widget_show(g_theme_view);
164 g_signal_connect(g_theme_view, "button-press-event", (GCallback)view_onButtonPressed, NULL);
165 g_signal_connect(g_theme_view, "popup-menu", (GCallback)view_onPopupMenu, NULL);
166 g_signal_connect(g_theme_view, "row-activated", G_CALLBACK(viewRowActivated), NULL);
167
168 // load themes
169 load_theme(g_theme_view);
170
171 // rig up idle/thread routines
172 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::load_cache_images), true);
173 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::create_cache_images), true);
174
175 gtk_widget_show_all(g_window);
176 gtk_main ();
177 return 0;
178 }
179
180
181 static void menuAddWidget (GtkUIManager * p_uiManager, GtkWidget * p_widget, GtkContainer * p_box)
182 {
183 gtk_box_pack_start(GTK_BOX(p_box), p_widget, FALSE, FALSE, 0);
184 gtk_widget_show(p_widget);
185 }
186
187
188 static void menuAbout(GtkWindow * parent)
189 {
190 const char *authors[] = { "Thierry Lorthiois <lorthiois@bbsoft.fr>", "Andreas Fink <andreas.fink85@googlemail.com>", "Christian Ruppert <Spooky85@gmail.com> (Build system)", "Euan Freeman <euan04@gmail.com> (tintwizard)\n See http://code.google.com/p/tintwizard/", NULL };
191
192 gtk_show_about_dialog( parent, "name", g_get_application_name( ),
193 "comments", _("Theming tool for tint2 panel"),
194 "version", VERSION_STRING,
195 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
196 "logo-icon-name", NULL, "authors", authors,
197 /* Translators: translate "translator-credits" as
198 your name to have it appear in the credits in the "About"
199 dialog */
200 "translator-credits", _("translator-credits"),
201 NULL );
202 }
203
204
205 static void menuAdd (GtkWindow *parent)
206 {
207 GtkWidget *dialog;
208 GtkFileChooser *chooser;
209 GtkFileFilter *filter;
210
211 dialog = gtk_file_chooser_dialog_new(_("Add a theme"), parent, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT, NULL);
212 chooser = GTK_FILE_CHOOSER(dialog);
213
214 gtk_file_chooser_set_current_folder(chooser, g_get_home_dir());
215 gtk_file_chooser_set_select_multiple(chooser, TRUE);
216
217 filter = gtk_file_filter_new();
218 gtk_file_filter_set_name(filter, _("Tint2 theme files"));
219 gtk_file_filter_add_pattern(filter, "*.tint2rc");
220 gtk_file_chooser_add_filter(chooser, filter);
221
222 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
223 GSList *l, *list = gtk_file_chooser_get_filenames(chooser);
224
225 gchar *file, *pt1, *name, *path;
226 for (l = list; l ; l = l->next) {
227 file = (char *)l->data;
228 pt1 = strrchr (file, '/');
229 if (pt1) {
230 pt1++;
231 if (*pt1) {
232 name = g_strdup(pt1);
233 path = g_build_filename (g_get_user_config_dir(), "tint2", name, NULL);
234 copy_file(file, path);
235 custom_list_append(path);
236 g_free(path);
237 g_free(name);
238 }
239 }
240 }
241
242 g_slist_foreach(list, (GFunc)g_free, NULL);
243 g_slist_free(list);
244 }
245 gtk_widget_destroy (dialog);
246 }
247
248
249 static void menuSaveAs (GtkWindow *parent)
250 {
251 GtkWidget *dialog;
252 GtkFileChooser *chooser;
253
254 dialog = gtk_file_chooser_dialog_new (_("Save theme as"), parent, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
255 chooser = GTK_FILE_CHOOSER(dialog);
256
257 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
258 gtk_file_chooser_set_current_folder (chooser, g_get_home_dir());
259 gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
260
261 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
262 char *filename = gtk_file_chooser_get_filename(chooser);
263 printf("fichier %s\n", filename);
264 //save_to_file (filename);
265 g_free (filename);
266 }
267 gtk_widget_destroy (dialog);
268 }
269
270
271 static void menuDelete (void)
272 {
273 GtkTreeSelection *sel;
274 GtkTreeIter iter;
275 GtkTreeModel *model;
276 char *file;
277
278 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
279 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
280 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1);
281 gtk_tree_selection_unselect_all(sel);
282
283 // remove (gui and disk)
284 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
285 g_remove(file);
286 g_free(file);
287 }
288 }
289
290
291 static void menuProperties (void)
292 {
293 GtkTreeSelection *sel;
294 GtkTreeIter iter;
295 GtkTreeModel *model;
296 char *file, *cmd;
297
298 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
299 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
300 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1);
301
302 cmd = g_strdup_printf("gedit \'%s\' &", file);
303 system(cmd);
304
305 g_free(cmd);
306 g_free(file);
307 }
308 }
309
310
311 static void menuQuit (void)
312 {
313 write_config();
314
315 if (g_path_config)
316 g_free(g_path_config);
317 if (g_path_dir)
318 g_free(g_path_dir);
319 if (g_default_theme)
320 g_free(g_default_theme);
321
322 gtk_main_quit ();
323 }
324
325
326 static void menuRefresh (void)
327 {
328 printf("menuRefresh\n");
329 }
330
331
332 static void menuRefreshAll (void)
333 {
334 printf("menuRefreshAll\n");
335 }
336
337
338 static void menuPreferences (void)
339 {
340 printf("menuPreferences\n");
341 }
342
343
344 static void menuApply (void)
345 {
346 GtkTreeSelection *sel;
347 GtkTreeIter iter;
348 GtkTreeModel *model;
349
350 if (g_default_theme) {
351 g_free(g_default_theme);
352 g_default_theme = NULL;
353 }
354 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
355 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
356 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &g_default_theme, -1);
357 // overwrite tint2rc
358 copy_file(g_default_theme, g_path_config);
359
360 // restart panel
361 system("killall -SIGUSR1 tint2");
362 }
363 }
364
365
366 static void view_popup_menu(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
367 {
368 GtkWidget *w = gtk_ui_manager_get_widget(globalUIManager, "/ThemePopup");
369
370 gtk_menu_popup(GTK_MENU(w), NULL, NULL, NULL, NULL, (event != NULL) ? event->button : 0, gdk_event_get_time((GdkEvent*)event));
371 }
372
373
374 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
375 {
376 // single click with the right mouse button?
377 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
378 GtkTreeSelection *selection;
379
380 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
381
382 if (gtk_tree_selection_count_selected_rows(selection) <= 1) {
383 GtkTreePath *path;
384
385 // Get tree path for row that was clicked
386 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview), (gint) event->x, (gint) event->y, &path, NULL, NULL, NULL)) {
387 gtk_tree_selection_unselect_all(selection);
388 gtk_tree_selection_select_path(selection, path);
389 gtk_tree_path_free(path);
390 }
391 }
392
393 view_popup_menu(treeview, event, userdata);
394 return TRUE;
395 }
396 return FALSE;
397 }
398
399
400 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata)
401 {
402 view_popup_menu(treeview, NULL, userdata);
403 return TRUE;
404 }
405
406
407 static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
408 {
409 menuApply();
410 }
411
412
413 static void load_theme(GtkWidget *list)
414 {
415 GDir *dir;
416 gchar *pt1, *name, *file;
417 gboolean have_iter, found_theme = FALSE;
418 GtkTreeIter iter;
419 GtkTreeModel *model;
420
421 dir = g_dir_open(g_path_dir, 0, NULL);
422 if (dir == NULL) return;
423 while ((file = g_dir_read_name(dir))) {
424 pt1 = strstr(file, ".tint2rc");
425 if (pt1) {
426 found_theme = TRUE;
427 name = g_build_filename (g_path_dir, file, NULL);
428 custom_list_append(name);
429 g_free(name);
430 }
431 }
432 g_dir_close(dir);
433
434 if (!found_theme) {
435 // create default theme file
436 name = g_build_filename (g_get_user_config_dir(), "tint2", "default.tint2rc", NULL);
437 copy_file(g_path_config, name);
438 custom_list_append(name);
439 g_free(name);
440 }
441
442 // search default theme
443 found_theme = FALSE;
444 model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view));
445 have_iter = gtk_tree_model_get_iter_first(model, &iter);
446 while (have_iter) {
447 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &name, -1);
448 found_theme = (strcmp(name, g_default_theme) == 0);
449 g_free(name);
450 if (found_theme)
451 break;
452 have_iter = gtk_tree_model_iter_next(model, &iter);
453 }
454
455 // select theme
456 GtkTreePath *path = NULL;
457 if (found_theme)
458 path = gtk_tree_model_get_path(model, &iter);
459 else {
460 have_iter = gtk_tree_model_get_iter_first(model, &iter);
461 if (have_iter)
462 path = gtk_tree_model_get_path(model, &iter);
463 }
464 if (path) {
465 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view)), &iter);
466 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(g_theme_view), path, NULL, FALSE, 0, 0);
467 gtk_tree_path_free(path);
468 }
469
470 }
471
472
473 // theme file management
474 void read_config()
475 {
476 char *path;
477
478 if (g_default_theme != NULL) {
479 g_free(g_default_theme);
480 g_default_theme = NULL;
481 }
482
483 g_width = 600;
484 g_height = 350;
485 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
486 if (g_file_test (path, G_FILE_TEST_EXISTS)) {
487 FILE *fp;
488 char line[80];
489 char *key, *value;
490 if ((fp = fopen(path, "r")) != NULL) {
491 while (fgets(line, sizeof(line), fp) != NULL) {
492 if (parse_line(line, &key, &value)) {
493 if (strcmp (key, "default_theme") == 0)
494 g_default_theme = strdup (value);
495 else if (strcmp (key, "width") == 0)
496 g_width = atoi(value);
497 else if (strcmp (key, "height") == 0)
498 g_height = atoi(value);
499 free (key);
500 free (value);
501 }
502 }
503 fclose (fp);
504 }
505 }
506 g_free(path);
507 }
508
509
510 void write_config()
511 {
512 char *path;
513 FILE *fp;
514 GtkRequisition req;
515 int x, y, width, height, depth;
516 int top, bottom, left, right;
517 GdkRectangle rect;
518
519 gtk_window_get_size(GTK_WINDOW(g_window), &width, &height);
520 printf("write_config %d, %d\n", width, height);
521
522 // GTK incapacity to return (width, height) of the window is really annoying
523 gdk_window_get_geometry(GTK_WIDGET(g_window)->window, &x, &y, &width, &height, &depth);
524 printf(" write_config %d, %d, %d, %d\n", x, y, width, height);
525
526 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
527 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
528 //printf(" write_config %d, %d\n", rec.width, rec.height);
529
530 /*
531 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
532 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
533 gtk_tree_selection_select_path( sel, path );
534 gtk_tree_path_free( path );
535 */
536 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
537 fp = fopen(path, "w");
538 if (fp != NULL) {
539 fputs("#---------------------------------------------\n", fp);
540 fputs("# TINT2CONF CONFIG FILE\n", fp);
541 if (g_default_theme != NULL) {
542 fprintf(fp, "default_theme = %s\n", g_default_theme);
543 printf("default_theme %s\n", g_default_theme);
544 }
545 fprintf(fp, "width = %d\n", g_width);
546 fprintf(fp, "height = %d\n", g_height);
547 fputs("\n", fp);
548 fclose (fp);
549 }
550 g_free(path);
551 }
552
553
554 void check_theme()
555 {
556 g_path_dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
557 if (!g_file_test (g_path_dir, G_FILE_TEST_IS_DIR))
558 g_mkdir(g_path_dir, 0777);
559
560 g_path_config = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
561
562 }
563
564
565
This page took 0.058337 seconds and 4 git commands to generate.