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