]> Dogcows Code - chaz/tint2/blob - src/tint2conf/main.c
tint2conf : add theme menu
[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();
52 static void menuSaveAs();
53 static void menuDelete();
54 static void menuProperties();
55 static void menuQuit();
56 static void menuRefresh();
57 static void menuRefreshAll();
58 static void menuPreferences();
59 static void menuApply();
60 static void menuAbout();
61
62 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata);
63 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata);
64 static void windowSizeAllocated();
65 static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
66
67
68 // theme files
69 static void selectTheme(const gchar *name);
70 static gboolean searchTheme(const gchar *name_theme, GtkTreeModel *model, GtkTreeIter *iter);
71 static void load_theme();
72 static void read_config();
73 static void write_config();
74 static void check_theme();
75
76
77 // define menubar, toolbar and popup
78 static const char *global_ui =
79 "<ui>"
80 " <menubar name='MenuBar'>"
81 " <menu action='ThemeMenu'>"
82 " <menuitem action='ThemeAdd'/>"
83 " <menuitem action='ThemeSaveAs'/>"
84 " <separator/>"
85 " <menuitem action='ThemeDelete'/>"
86 " <separator/>"
87 // " <menuitem action='ThemeProperties'/>"
88 // " <separator/>"
89 " <menuitem action='ThemeQuit'/>"
90 " </menu>"
91 " <menu action='EditMenu'>"
92 " <menuitem action='EditRefresh'/>"
93 " <menuitem action='EditRefreshAll'/>"
94 // " <separator/>"
95 // " <menuitem action='EditPreferences'/>"
96 " </menu>"
97 " <menu action='HelpMenu'>"
98 " <menuitem action='HelpAbout'/>"
99 " </menu>"
100 " </menubar>"
101 " <toolbar name='ToolBar'>"
102 " <toolitem action='ThemeProperties'/>"
103 " <toolitem action='ViewApply'/>"
104 " </toolbar>"
105 " <popup name='ThemePopup'>"
106 " <menuitem action='EditRefresh'/>"
107 " <menuitem action='ThemeProperties'/>"
108 " <menuitem action='ViewApply'/>"
109 " <separator/>"
110 " <menuitem action='ThemeDelete'/>"
111 " </popup>"
112 "</ui>";
113
114
115 // define menubar and toolbar action
116 static GtkActionEntry entries[] = {
117 {"ThemeMenu", NULL, "Theme", NULL, NULL, NULL},
118 {"ThemeAdd", GTK_STOCK_ADD, "_Add...", "<Control>N", "Add theme", G_CALLBACK (menuAdd)},
119 {"ThemeSaveAs", GTK_STOCK_SAVE_AS, "_Save as...", NULL, "Save theme as", G_CALLBACK (menuSaveAs)},
120 {"ThemeDelete", GTK_STOCK_DELETE, "_Delete", NULL, "Delete theme", G_CALLBACK (menuDelete)},
121 {"ThemeProperties", GTK_STOCK_PROPERTIES, "_Properties...", NULL, "Show properties", G_CALLBACK (menuProperties)},
122 {"ThemeQuit", GTK_STOCK_QUIT, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit)},
123 {"EditMenu", NULL, "Edit", NULL, NULL, NULL},
124 {"EditRefresh", GTK_STOCK_REFRESH, "Refresh", NULL, "Refresh", G_CALLBACK (menuRefresh)},
125 {"EditRefreshAll", GTK_STOCK_REFRESH, "Refresh all", NULL, "Refresh all", G_CALLBACK (menuRefreshAll)},
126 // {"EditPreferences", GTK_STOCK_PREFERENCES, "Preferences", NULL, "Preferences", G_CALLBACK (menuPreferences)},
127 {"ViewApply", GTK_STOCK_APPLY, "Apply", NULL, "Apply theme", G_CALLBACK (menuApply)},
128 {"HelpMenu", NULL, "Help", NULL, NULL, NULL},
129 {"HelpAbout", GTK_STOCK_ABOUT, "_About", "<Control>A", "About", G_CALLBACK (menuAbout)}
130 };
131
132
133 int main (int argc, char ** argv)
134 {
135 GtkWidget *vBox = NULL, *scrollbar = NULL;
136 GtkActionGroup *actionGroup;
137
138 gtk_init (&argc, &argv);
139 g_thread_init( NULL );
140 read_config();
141 check_theme();
142
143 // define main layout : container, menubar, toolbar
144 g_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
145 gtk_window_set_title(GTK_WINDOW(g_window), _("Panel theming"));
146 gtk_window_resize(GTK_WINDOW(g_window), g_width, g_height);
147 g_signal_connect(G_OBJECT(g_window), "destroy", G_CALLBACK (menuQuit), NULL);
148 g_signal_connect(g_window, "size-allocate", G_CALLBACK(windowSizeAllocated), NULL);
149 vBox = gtk_vbox_new (FALSE, 0);
150 gtk_container_add (GTK_CONTAINER(g_window), vBox);
151
152 actionGroup = gtk_action_group_new ("menuActionGroup");
153 gtk_action_group_add_actions (actionGroup, entries, G_N_ELEMENTS (entries), NULL);
154 globalUIManager = gtk_ui_manager_new();
155 gtk_ui_manager_insert_action_group (globalUIManager, actionGroup, 0);
156 gtk_ui_manager_add_ui_from_string (globalUIManager, global_ui, -1, NULL );
157 g_signal_connect(globalUIManager, "add_widget", G_CALLBACK (menuAddWidget), vBox);
158 gtk_ui_manager_ensure_update(globalUIManager);
159 scrollbar = gtk_scrolled_window_new(NULL, NULL);
160 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
161 gtk_box_pack_start(GTK_BOX(vBox), scrollbar, TRUE, TRUE, 0);
162
163 // define theme view
164 g_theme_view = create_view();
165 gtk_container_add(GTK_CONTAINER(scrollbar), g_theme_view);
166 gtk_widget_show(g_theme_view);
167 g_signal_connect(g_theme_view, "button-press-event", (GCallback)view_onButtonPressed, NULL);
168 g_signal_connect(g_theme_view, "popup-menu", (GCallback)view_onPopupMenu, NULL);
169 g_signal_connect(g_theme_view, "row-activated", G_CALLBACK(viewRowActivated), NULL);
170
171 // load themes
172 load_theme(g_theme_view);
173
174 // rig up idle/thread routines
175 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::load_cache_images), true);
176 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::create_cache_images), true);
177
178 gtk_widget_show_all(g_window);
179 gtk_main ();
180 return 0;
181 }
182
183
184 static void menuAddWidget (GtkUIManager * p_uiManager, GtkWidget * p_widget, GtkContainer * p_box)
185 {
186 gtk_box_pack_start(GTK_BOX(p_box), p_widget, FALSE, FALSE, 0);
187 gtk_widget_show(p_widget);
188 }
189
190
191 static void menuAbout()
192 {
193 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 };
194
195 gtk_show_about_dialog(g_window, "name", g_get_application_name( ),
196 "comments", _("Theming tool for tint2 panel"),
197 "version", VERSION_STRING,
198 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
199 "logo-icon-name", NULL, "authors", authors,
200 /* Translators: translate "translator-credits" as
201 your name to have it appear in the credits in the "About"
202 dialog */
203 "translator-credits", _("translator-credits"),
204 NULL);
205 }
206
207
208 static void menuAdd()
209 {
210 GtkWidget *dialog;
211 GtkFileChooser *chooser;
212 GtkFileFilter *filter;
213
214 dialog = gtk_file_chooser_dialog_new(_("Add a theme"), g_window, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT, NULL);
215 chooser = GTK_FILE_CHOOSER(dialog);
216
217 gtk_file_chooser_set_current_folder(chooser, g_get_home_dir());
218 gtk_file_chooser_set_select_multiple(chooser, TRUE);
219
220 filter = gtk_file_filter_new();
221 gtk_file_filter_set_name(filter, _("Tint2 theme files"));
222 gtk_file_filter_add_pattern(filter, "*.tint2rc");
223 gtk_file_chooser_add_filter(chooser, filter);
224
225 if (gtk_dialog_run (GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
226 gtk_widget_destroy(dialog);
227 return;
228 }
229
230 GtkTreeIter iter;
231 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view));
232 GSList *l, *list = gtk_file_chooser_get_filenames(chooser);
233 gchar *file, *pt1, *name, *path, *name_first=NULL;
234 for (l = list; l ; l = l->next) {
235 file = (char *)l->data;
236 pt1 = strrchr(file, '/');
237 if (pt1 == NULL) continue;
238 pt1++;
239 if (*pt1 == 0) continue;
240
241 name = g_strdup(pt1);
242 path = g_build_filename (g_get_user_config_dir(), "tint2", name, NULL);
243
244 // check existing
245 if (searchTheme(path, model, &iter)) {
246 gchar *message;
247 message = g_strdup_printf(_("Couldn't add duplicate theme\n\'%s\'."), pt1);
248
249 GtkWidget *w = gtk_message_dialog_new(g_window, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, message, NULL);
250 g_signal_connect_swapped(w, "response", G_CALLBACK(gtk_widget_destroy), w);
251 gtk_widget_show(w);
252 g_free(message);
253 continue;
254 }
255
256 // append theme
257 copy_file(file, path);
258 custom_list_append(path);
259 if (name_first == NULL)
260 name_first = g_strdup(path);
261 g_free(path);
262 g_free(name);
263 }
264 g_slist_foreach(list, (GFunc)g_free, NULL);
265 g_slist_free(list);
266 gtk_widget_destroy(dialog);
267
268 selectTheme(name_first);
269 g_free(name_first);
270 }
271
272
273 static void menuSaveAs ()
274 {
275 GtkWidget *dialog;
276 GtkFileChooser *chooser;
277 GtkTreeSelection *sel;
278 GtkTreeIter iter;
279 GtkTreeModel *model;
280 gchar *file, *pt1;
281
282 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
283 if (!gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
284 GtkWidget *w = gtk_message_dialog_new(g_window, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("Select the theme to be saved."), NULL);
285 g_signal_connect_swapped(w, "response", G_CALLBACK(gtk_widget_destroy), w);
286 gtk_widget_show(w);
287 return;
288 }
289
290 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1);
291 pt1 = strrchr (file, '/');
292 if (pt1) pt1++;
293
294 dialog = gtk_file_chooser_dialog_new(_("Save theme as"), g_window, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
295 chooser = GTK_FILE_CHOOSER(dialog);
296
297 gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
298 gtk_file_chooser_set_current_folder(chooser, g_get_home_dir());
299 gtk_file_chooser_set_current_name(chooser, pt1);
300
301 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
302 char *filename = gtk_file_chooser_get_filename(chooser);
303 copy_file(file, filename);
304 g_free (filename);
305 }
306 g_free(file);
307 gtk_widget_destroy (dialog);
308 }
309
310
311 static void menuDelete()
312 {
313 GtkTreeSelection *sel;
314 GtkTreeIter iter;
315 GtkTreeModel *model;
316 gchar *filename;
317
318 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
319 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
320 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &filename, -1);
321 gtk_tree_selection_unselect_all(sel);
322
323 // remove (gui and file)
324 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
325 GFile *file = g_file_new_for_path(filename);
326 g_file_trash(file, NULL, NULL);
327 g_object_unref(G_OBJECT(file));
328 g_free(filename);
329 }
330 }
331
332
333 static void menuProperties()
334 {
335 GtkTreeSelection *sel;
336 GtkTreeIter iter;
337 GtkTreeModel *model;
338 char *file, *cmd;
339
340 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
341 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
342 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1);
343
344 cmd = g_strdup_printf("gedit \'%s\' &", file);
345 system(cmd);
346
347 g_free(cmd);
348 g_free(file);
349 }
350 }
351
352
353 static void menuQuit()
354 {
355 write_config();
356
357 if (g_path_config)
358 g_free(g_path_config);
359 if (g_path_dir)
360 g_free(g_path_dir);
361 if (g_default_theme)
362 g_free(g_default_theme);
363
364 gtk_main_quit ();
365 }
366
367
368 static void menuRefresh()
369 {
370 printf("menuRefresh\n");
371 }
372
373
374 static void menuRefreshAll()
375 {
376 printf("menuRefreshAll\n");
377 }
378
379
380 static void menuPreferences()
381 {
382 printf("menuPreferences\n");
383 }
384
385
386 static void menuApply()
387 {
388 GtkTreeSelection *sel;
389 GtkTreeIter iter;
390 GtkTreeModel *model;
391
392 if (g_default_theme) {
393 g_free(g_default_theme);
394 g_default_theme = NULL;
395 }
396 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
397 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
398 gtk_tree_model_get(model, &iter, COL_THEME_FILE, &g_default_theme, -1);
399 // overwrite tint2rc
400 copy_file(g_default_theme, g_path_config);
401
402 // restart panel
403 system("killall -SIGUSR1 tint2");
404 }
405 }
406
407
408 static void view_popup_menu(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
409 {
410 GtkWidget *w = gtk_ui_manager_get_widget(globalUIManager, "/ThemePopup");
411
412 gtk_menu_popup(GTK_MENU(w), NULL, NULL, NULL, NULL, (event != NULL) ? event->button : 0, gdk_event_get_time((GdkEvent*)event));
413 }
414
415
416 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
417 {
418 // single click with the right mouse button?
419 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
420 GtkTreeSelection *selection;
421
422 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
423
424 if (gtk_tree_selection_count_selected_rows(selection) <= 1) {
425 GtkTreePath *path;
426
427 // Get tree path for row that was clicked
428 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview), (gint) event->x, (gint) event->y, &path, NULL, NULL, NULL)) {
429 gtk_tree_selection_unselect_all(selection);
430 gtk_tree_selection_select_path(selection, path);
431 gtk_tree_path_free(path);
432 }
433 }
434
435 view_popup_menu(treeview, event, userdata);
436 return TRUE;
437 }
438 return FALSE;
439 }
440
441
442 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata)
443 {
444 view_popup_menu(treeview, NULL, userdata);
445 return TRUE;
446 }
447
448
449 static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
450 {
451 menuApply();
452 }
453
454
455 static void windowSizeAllocated()
456 {
457 const gboolean isMaximized = g_window->window && (gdk_window_get_state(g_window->window) & GDK_WINDOW_STATE_MAXIMIZED);
458
459 if(!isMaximized)
460 gtk_window_get_size(GTK_WINDOW(g_window), &g_width, &g_height);
461 }
462
463
464 static void load_theme(GtkWidget *list)
465 {
466 GDir *dir;
467 gchar *pt1, *name, *file;
468 gboolean found_theme = FALSE;
469
470 dir = g_dir_open(g_path_dir, 0, NULL);
471 if (dir == NULL) return;
472 while ((file = g_dir_read_name(dir))) {
473 pt1 = strstr(file, ".tint2rc");
474 if (pt1) {
475 found_theme = TRUE;
476 name = g_build_filename (g_path_dir, file, NULL);
477 custom_list_append(name);
478 g_free(name);
479 }
480 }
481 g_dir_close(dir);
482
483 if (!found_theme) {
484 // create default theme file
485 name = g_build_filename(g_get_user_config_dir(), "tint2", "default.tint2rc", NULL);
486 copy_file(g_path_config, name);
487 custom_list_append(name);
488 if (g_default_theme) g_free(g_default_theme);
489 g_default_theme = strdup(name);
490 g_free(name);
491 }
492
493 selectTheme(g_default_theme);
494 }
495
496
497 void selectTheme(const gchar *name_theme)
498 {
499 gboolean have_iter, found_theme;
500 GtkTreeIter iter;
501 GtkTreeModel *model;
502
503 if (!name_theme) return;
504
505 model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view));
506 found_theme = searchTheme(name_theme, model, &iter);
507
508 GtkTreePath *path = NULL;
509 if (found_theme)
510 path = gtk_tree_model_get_path(model, &iter);
511 else {
512 have_iter = gtk_tree_model_get_iter_first(model, &iter);
513 if (have_iter)
514 path = gtk_tree_model_get_path(model, &iter);
515 }
516 if (path) {
517 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view)), &iter);
518 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(g_theme_view), path, NULL, FALSE, 0, 0);
519 gtk_tree_path_free(path);
520 }
521 }
522
523
524 gboolean searchTheme(const gchar *name_theme, GtkTreeModel *model, GtkTreeIter *iter)
525 {
526 gchar *name;
527 gboolean have_iter, found = FALSE;
528
529 have_iter = gtk_tree_model_get_iter_first(model, iter);
530 while (have_iter) {
531 gtk_tree_model_get(model, iter, COL_THEME_FILE, &name, -1);
532 found = (strcmp(name, name_theme) == 0);
533 g_free(name);
534 if (found)
535 break;
536 have_iter = gtk_tree_model_iter_next(model, iter);
537 }
538 return found;
539 }
540
541
542 // theme file management
543 void read_config()
544 {
545 char *path;
546
547 if (g_default_theme != NULL) {
548 g_free(g_default_theme);
549 g_default_theme = NULL;
550 }
551
552 g_width = 500;
553 g_height = 350;
554 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
555 if (g_file_test (path, G_FILE_TEST_EXISTS)) {
556 FILE *fp;
557 char line[80];
558 char *key, *value;
559 if ((fp = fopen(path, "r")) != NULL) {
560 while (fgets(line, sizeof(line), fp) != NULL) {
561 if (parse_line(line, &key, &value)) {
562 if (strcmp (key, "default_theme") == 0)
563 g_default_theme = strdup (value);
564 else if (strcmp (key, "width") == 0)
565 g_width = atoi(value);
566 else if (strcmp (key, "height") == 0)
567 g_height = atoi(value);
568 free (key);
569 free (value);
570 }
571 }
572 fclose (fp);
573 }
574 }
575 g_free(path);
576 }
577
578
579 void write_config()
580 {
581 char *path;
582 FILE *fp;
583
584 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
585 fp = fopen(path, "w");
586 if (fp != NULL) {
587 fputs("#---------------------------------------------\n", fp);
588 fputs("# TINT2CONF CONFIG FILE\n", fp);
589 if (g_default_theme != NULL) {
590 fprintf(fp, "default_theme = %s\n", g_default_theme);
591 printf("default_theme %s\n", g_default_theme);
592 }
593 fprintf(fp, "width = %d\n", g_width);
594 fprintf(fp, "height = %d\n", g_height);
595 fputs("\n", fp);
596 fclose (fp);
597 }
598 g_free(path);
599 }
600
601
602 void check_theme()
603 {
604 g_path_dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
605 if (!g_file_test (g_path_dir, G_FILE_TEST_IS_DIR))
606 g_mkdir(g_path_dir, 0777);
607
608 g_path_config = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
609
610 }
611
612
613
This page took 0.07056 seconds and 5 git commands to generate.