]> Dogcows Code - chaz/tint2/blob - src/tint2conf/main.c
fixed issue 188
[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 "common.h"
32 #include "theme_view.h"
33
34
35 #define LONG_VERSION_STRING "0.7"
36
37
38 // default config file and directory
39 char *g_path_config = 0;
40 char *g_path_dir = 0;
41 char *g_default_theme = 0;
42 int g_width;
43 int g_height;
44
45 GtkWidget *g_window;
46 GtkWidget *g_theme_view;
47 GtkCellRenderer *g_renderer;
48
49 static GtkUIManager *globalUIManager = NULL;
50
51 static void menuAddWidget (GtkUIManager *, GtkWidget *, GtkContainer *);
52
53 // action on menus
54 static void menuAdd (GtkWindow * parent);
55 //static void menuSaveAs (GtkWindow *parent);
56 static void menuDelete (void);
57 static void menuProperties (void);
58 static void menuRename (void);
59 static void menuQuit (void);
60 static void menuRefresh (void);
61 static void menuRefreshAll (void);
62 static void menuApply (void);
63 static void menuAbout(GtkWindow * parent);
64
65 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata);
66 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata);
67 static void viewRowActivated( GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
68
69
70 // theme files
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 " <menuitem action='ThemeRename'/>"
89 " <separator/>"
90 " <menuitem action='ThemeQuit'/>"
91 " </menu>"
92 " <menu action='ViewMenu'>"
93 " <menuitem action='ViewRefresh'/>"
94 " <menuitem action='ViewRefreshAll'/>"
95 " </menu>"
96 " <menu action='HelpMenu'>"
97 " <menuitem action='HelpAbout'/>"
98 " </menu>"
99 " </menubar>"
100 " <toolbar name='ToolBar'>"
101 " <toolitem action='ViewRefreshAll'/>"
102 " <separator/>"
103 " <toolitem action='ThemeProperties'/>"
104 " <toolitem action='ViewApply'/>"
105 " </toolbar>"
106 " <popup name='ThemePopup'>"
107 " <menuitem action='ThemeProperties'/>"
108 " <menuitem action='ThemeRename'/>"
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 {"ThemeRename", NULL, "_Rename...", NULL, "Rename theme", G_CALLBACK (menuRename)},
123 {"ThemeQuit", GTK_STOCK_QUIT, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit)},
124 {"ViewMenu", NULL, "View", NULL, NULL, NULL},
125 {"ViewRefresh", GTK_STOCK_REFRESH, "Refresh", NULL, "Refresh", G_CALLBACK (menuRefresh)},
126 {"ViewRefreshAll", GTK_STOCK_REFRESH, "Refresh all", NULL, "Refresh all", G_CALLBACK (menuRefreshAll)},
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 g_width = 300;
143 g_height = 400;
144
145 // define main layout : container, menubar, toolbar
146 g_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
147 gtk_window_set_title(GTK_WINDOW(g_window), _("Panel theming"));
148 gtk_window_resize(GTK_WINDOW(g_window), g_width, g_height);
149 g_signal_connect (G_OBJECT (g_window), "destroy", G_CALLBACK (menuQuit), NULL);
150 vBox = gtk_vbox_new (FALSE, 0);
151 gtk_container_add (GTK_CONTAINER(g_window), vBox);
152
153 actionGroup = gtk_action_group_new ("menuActionGroup");
154 gtk_action_group_add_actions (actionGroup, entries, G_N_ELEMENTS (entries), NULL);
155 globalUIManager = gtk_ui_manager_new();
156 gtk_ui_manager_insert_action_group (globalUIManager, actionGroup, 0);
157 gtk_ui_manager_add_ui_from_string (globalUIManager, global_ui, -1, NULL );
158 g_signal_connect(globalUIManager, "add_widget", G_CALLBACK (menuAddWidget), vBox);
159 gtk_ui_manager_ensure_update(globalUIManager);
160 scrollbar = gtk_scrolled_window_new(NULL, NULL);
161 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
162 gtk_box_pack_start(GTK_BOX(vBox), scrollbar, TRUE, TRUE, 0);
163
164 // define theme view
165 g_theme_view = create_view_and_model();
166 gtk_container_add(GTK_CONTAINER(scrollbar), g_theme_view);
167 gtk_widget_show(g_theme_view);
168 g_signal_connect(g_theme_view, "button-press-event", (GCallback)view_onButtonPressed, NULL);
169 g_signal_connect(g_theme_view, "popup-menu", (GCallback)view_onPopupMenu, NULL);
170 g_signal_connect(g_theme_view, "row-activated", G_CALLBACK(viewRowActivated), NULL);
171
172 // load themes
173 load_theme(g_theme_view);
174
175 // rig up idle/thread routines
176 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::load_cache_images), true);
177 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::create_cache_images), true);
178
179 gtk_widget_show_all(g_window);
180 gtk_main ();
181 return 0;
182 }
183
184
185 static void menuAddWidget (GtkUIManager * p_uiManager, GtkWidget * p_widget, GtkContainer * p_box)
186 {
187 gtk_box_pack_start(GTK_BOX(p_box), p_widget, FALSE, FALSE, 0);
188 gtk_widget_show(p_widget);
189 }
190
191
192 static void menuAbout(GtkWindow * parent)
193 {
194 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 };
195
196 gtk_show_about_dialog( parent, "name", g_get_application_name( ),
197 "comments", _("Theming tool for tint2 panel"),
198 "version", LONG_VERSION_STRING,
199 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
200 "logo-icon-name", NULL, "authors", authors,
201 /* Translators: translate "translator-credits" as
202 your name to have it appear in the credits in the "About"
203 dialog */
204 "translator-credits", _("translator-credits"),
205 NULL );
206 }
207
208
209 static void menuAdd (GtkWindow *parent)
210 {
211 GtkWidget *dialog;
212 GtkFileChooser *chooser;
213 GtkFileFilter *filter;
214
215 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);
216 chooser = GTK_FILE_CHOOSER(dialog);
217
218 gtk_file_chooser_set_current_folder(chooser, g_get_home_dir());
219 gtk_file_chooser_set_select_multiple(chooser, TRUE);
220
221 filter = gtk_file_filter_new();
222 gtk_file_filter_set_name(filter, _("Tint2 theme files"));
223 gtk_file_filter_add_pattern(filter, "*.tint2rc");
224 gtk_file_chooser_add_filter(chooser, filter);
225
226 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
227 GSList *l, *list = gtk_file_chooser_get_filenames(chooser);
228
229 gchar *file, *pt1, *name, *path;
230 for (l = list; l ; l = l->next) {
231 file = (char *)l->data;
232 pt1 = strrchr (file, '/');
233 if (pt1) {
234 pt1++;
235 if (*pt1) {
236 name = strdup(pt1);
237 path = g_build_filename (g_get_user_config_dir(), "tint2", name, NULL);
238 copy_file(file, path);
239 g_free(path);
240 pt1 = strstr(name, ".tint2rc");
241 if (pt1) {
242 file = strndup(name, pt1-name);
243 add_to_list(g_theme_view, file);
244 g_free(file);
245 }
246 g_free(name);
247 }
248 }
249 }
250
251 g_slist_foreach(list, (GFunc)g_free, NULL);
252 g_slist_free(list);
253 }
254 gtk_widget_destroy (dialog);
255 }
256
257 /*
258 static void menuSaveAs (GtkWindow *parent)
259 {
260 GtkWidget *dialog;
261 GtkFileChooser *chooser;
262
263 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);
264 chooser = GTK_FILE_CHOOSER(dialog);
265
266 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
267 gtk_file_chooser_set_current_folder (chooser, g_get_home_dir());
268 gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
269
270 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
271 char *filename = gtk_file_chooser_get_filename(chooser);
272 printf("fichier %s\n", filename);
273 //save_to_file (filename);
274 g_free (filename);
275 }
276 gtk_widget_destroy (dialog);
277 }
278 */
279
280 static void menuDelete (void)
281 {
282 GtkTreeSelection *sel;
283 GtkTreeIter iter;
284 GtkTreeModel *model;
285 char *value, *name1, *name2;
286
287 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
288 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
289 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
290 gtk_tree_selection_unselect_all(sel);
291 // remove from the gui
292 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
293
294 name1 = g_build_filename (g_get_user_config_dir(), "tint2", value, NULL);
295 name2 = g_strdup_printf("%s.tint2rc", name1);
296 g_remove(name2);
297
298 g_free(name1);
299 g_free(name2);
300 g_free(value);
301 }
302 }
303
304
305 static void menuProperties (void)
306 {
307 GtkTreeSelection *sel;
308 GtkTreeIter iter;
309 GtkTreeModel *model;
310 char *value, *name1, *name2, *cmd;
311
312 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
313 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
314 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
315
316 name1 = g_build_filename ("\'", g_get_user_config_dir(), "tint2", value, NULL);
317 name2 = g_strdup_printf("%s.tint2rc\'", name1);
318
319 cmd = g_strdup_printf("tintwizard.py %s", name2);
320 system(cmd);
321 g_free(cmd);
322
323 g_free(name1);
324 g_free(name2);
325 g_free(value);
326 }
327 }
328
329
330 static void menuRename (void)
331 {
332 printf("menuRename\n");
333 // g_rename
334 }
335
336
337 static void menuQuit (void)
338 {
339 write_config();
340
341 g_object_unref(g_store);
342 gtk_main_quit ();
343 }
344
345
346 static void menuRefresh (void)
347 {
348 printf("menuRefresh\n");
349 }
350
351
352 static void menuRefreshAll (void)
353 {
354 printf("menuRefreshAll\n");
355 }
356
357
358 static void menuApply (void)
359 {
360 GtkTreeSelection *sel;
361 GtkTreeIter iter;
362 GtkTreeModel *model;
363 char *value, *name1, *name2;
364
365 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
366 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
367 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
368 name1 = g_build_filename (g_get_user_config_dir(), "tint2", value, NULL);
369 name2 = g_strdup_printf("%s.tint2rc", name1);
370 g_free(name1);
371
372 copy_file(name2, g_path_config);
373 write_config();
374 g_free(value);
375 g_free(name2);
376
377 // restart panel
378 system("killall -SIGUSR1 tint2");
379 }
380 }
381
382
383 static void view_popup_menu(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
384 {
385 GtkWidget *w = gtk_ui_manager_get_widget(globalUIManager, "/ThemePopup");
386
387 gtk_menu_popup(GTK_MENU(w), NULL, NULL, NULL, NULL, (event != NULL) ? event->button : 0, gdk_event_get_time((GdkEvent*)event));
388 }
389
390
391 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
392 {
393 // single click with the right mouse button?
394 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
395 GtkTreeSelection *selection;
396
397 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
398
399 if (gtk_tree_selection_count_selected_rows(selection) <= 1) {
400 GtkTreePath *path;
401
402 // Get tree path for row that was clicked
403 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview), (gint) event->x, (gint) event->y, &path, NULL, NULL, NULL)) {
404 gtk_tree_selection_unselect_all(selection);
405 gtk_tree_selection_select_path(selection, path);
406 gtk_tree_path_free(path);
407 }
408 }
409
410 view_popup_menu(treeview, event, userdata);
411 return TRUE;
412 }
413 return FALSE;
414 }
415
416
417 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata)
418 {
419 view_popup_menu(treeview, NULL, userdata);
420 return TRUE;
421 }
422
423
424 static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
425 {
426 menuApply();
427 }
428
429
430 static void load_theme(GtkWidget *list)
431 {
432 GDir *dir;
433 gchar *file, *pt1, *name;
434
435 dir = g_dir_open(g_path_dir, 0, NULL);
436 while ((file = g_dir_read_name(dir))) {
437 pt1 = strstr(file, ".tint2rc");
438 if (pt1) {
439 name = strndup(file, pt1-file);
440 add_to_list(list, name);
441 g_free(name);
442 }
443 }
444 g_dir_close(dir);
445
446 // search default theme
447 GtkTreeIter iter;
448 GtkTreeModel *model;
449 if (g_default_theme) {
450 printf("defaultTheme %s\n", g_default_theme);
451 //gtk_tree_selection_select_iter(GtkTreeSelection *selection, GtkTreeIter *iter);
452 }
453 }
454
455
456 // theme file management
457 void read_config()
458 {
459 char *path;
460
461 if (g_default_theme)
462 g_free(g_default_theme);
463
464 g_width = 600;
465 g_height = 350;
466 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
467 if (g_file_test (path, G_FILE_TEST_EXISTS)) {
468 FILE *fp;
469 char line[80];
470 char *key, *value;
471 if ((fp = fopen(path, "r")) != NULL) {
472 while (fgets(line, sizeof(line), fp) != NULL) {
473 if (parse_line(line, &key, &value)) {
474 if (strcmp (key, "default_theme") == 0)
475 g_default_theme = strdup (value);
476 else if (strcmp (key, "width") == 0)
477 g_width = atoi(value);
478 else if (strcmp (key, "height") == 0)
479 g_height = atoi(value);
480 free (key);
481 free (value);
482 }
483 }
484 fclose (fp);
485 }
486 }
487 g_free(path);
488 }
489
490
491 void write_config()
492 {
493 char *path;
494 FILE *fp;
495 GtkRequisition req;
496 int x, y, width, height, depth;
497 int top, bottom, left, right;
498 GdkRectangle rect;
499
500 gtk_window_get_size(GTK_WINDOW(g_window), &width, &height);
501 printf("write_config %d, %d\n", width, height);
502
503 // GTK incapacity to return (width, height) of the window is really annoying
504 gdk_window_get_geometry(GTK_WIDGET(g_window)->window, &x, &y, &width, &height, &depth);
505 printf(" write_config %d, %d, %d, %d\n", x, y, width, height);
506
507 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
508 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
509 //printf(" write_config %d, %d\n", rec.width, rec.height);
510
511 /*
512 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
513 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
514 gtk_tree_selection_select_path( sel, path );
515 gtk_tree_path_free( path );
516 */
517 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
518 fp = fopen(path, "w");
519 if (fp != NULL) {
520 fputs("#---------------------------------------------\n", fp);
521 fputs("# TINT2CONF CONFIG FILE\n", fp);
522 fprintf(fp, "default_theme = %s\n", g_default_theme);
523 fprintf(fp, "width = %d\n", g_width);
524 fprintf(fp, "height = %d\n", g_height);
525 fputs("\n", fp);
526 fclose (fp);
527 }
528 g_free(path);
529 }
530
531
532 void check_theme()
533 {
534 g_path_dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
535 if (!g_file_test (g_path_dir, G_FILE_TEST_IS_DIR))
536 g_mkdir(g_path_dir, 0777);
537
538 g_path_config = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
539
540 }
541
542
543
This page took 0.056308 seconds and 4 git commands to generate.