]> Dogcows Code - chaz/tint2/blob - src/tint2conf/main.c
*fix* include file fix
[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 = 0;
39 char *g_path_dir = 0;
40 char *g_default_theme = 0;
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 menuRename (void);
58 static void menuQuit (void);
59 static void menuRefresh (void);
60 static void menuRefreshAll (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 " <menuitem action='ThemeRename'/>"
88 " <separator/>"
89 " <menuitem action='ThemeQuit'/>"
90 " </menu>"
91 " <menu action='ViewMenu'>"
92 " <menuitem action='ViewRefresh'/>"
93 " <menuitem action='ViewRefreshAll'/>"
94 " </menu>"
95 " <menu action='HelpMenu'>"
96 " <menuitem action='HelpAbout'/>"
97 " </menu>"
98 " </menubar>"
99 " <toolbar name='ToolBar'>"
100 " <toolitem action='ViewRefreshAll'/>"
101 " <separator/>"
102 " <toolitem action='ThemeProperties'/>"
103 " <toolitem action='ViewApply'/>"
104 " </toolbar>"
105 " <popup name='ThemePopup'>"
106 " <menuitem action='ThemeProperties'/>"
107 " <menuitem action='ThemeRename'/>"
108 " <separator/>"
109 " <menuitem action='ThemeDelete'/>"
110 " </popup>"
111 "</ui>";
112
113
114 // define menubar and toolbar action
115 static GtkActionEntry entries[] = {
116 {"ThemeMenu", NULL, "Theme", NULL, NULL, NULL},
117 {"ThemeAdd", GTK_STOCK_ADD, "_Add...", "<Control>N", "Add theme", G_CALLBACK (menuAdd)},
118 // {"ThemeSaveAs", GTK_STOCK_SAVE_AS, "_Save as...", NULL, "Save theme as", G_CALLBACK (menuSaveAs)},
119 {"ThemeDelete", GTK_STOCK_DELETE, "_Delete", NULL, "Delete theme", G_CALLBACK (menuDelete)},
120 {"ThemeProperties", GTK_STOCK_PROPERTIES, "_Properties...", NULL, "Show properties", G_CALLBACK (menuProperties)},
121 {"ThemeRename", NULL, "_Rename...", NULL, "Rename theme", G_CALLBACK (menuRename)},
122 {"ThemeQuit", GTK_STOCK_QUIT, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit)},
123 {"ViewMenu", NULL, "View", NULL, NULL, NULL},
124 {"ViewRefresh", GTK_STOCK_REFRESH, "Refresh", NULL, "Refresh", G_CALLBACK (menuRefresh)},
125 {"ViewRefreshAll", GTK_STOCK_REFRESH, "Refresh all", NULL, "Refresh all", G_CALLBACK (menuRefreshAll)},
126 {"ViewApply", GTK_STOCK_APPLY, "Apply", NULL, "Apply theme", G_CALLBACK (menuApply)},
127 {"HelpMenu", NULL, "Help", NULL, NULL, NULL},
128 {"HelpAbout", GTK_STOCK_ABOUT, "_About", "<Control>A", "About", G_CALLBACK (menuAbout)}
129 };
130
131
132 int main (int argc, char ** argv)
133 {
134 GtkWidget *vBox = NULL, *scrollbar = NULL;
135 GtkActionGroup *actionGroup;
136
137 gtk_init (&argc, &argv);
138 g_thread_init( NULL );
139 read_config();
140 check_theme();
141 g_width = 300;
142 g_height = 400;
143
144 // define main layout : container, menubar, toolbar
145 g_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
146 gtk_window_set_title(GTK_WINDOW(g_window), _("Panel theming"));
147 gtk_window_resize(GTK_WINDOW(g_window), g_width, g_height);
148 g_signal_connect (G_OBJECT (g_window), "destroy", G_CALLBACK (menuQuit), 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_and_model();
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(GtkWindow * parent)
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( parent, "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 (GtkWindow *parent)
209 {
210 GtkWidget *dialog;
211 GtkFileChooser *chooser;
212 GtkFileFilter *filter;
213
214 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);
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 GSList *l, *list = gtk_file_chooser_get_filenames(chooser);
227
228 gchar *file, *pt1, *name, *path;
229 for (l = list; l ; l = l->next) {
230 file = (char *)l->data;
231 pt1 = strrchr (file, '/');
232 if (pt1) {
233 pt1++;
234 if (*pt1) {
235 name = strdup(pt1);
236 path = g_build_filename (g_get_user_config_dir(), "tint2", name, NULL);
237 copy_file(file, path);
238 g_free(path);
239 pt1 = strstr(name, ".tint2rc");
240 if (pt1) {
241 file = strndup(name, pt1-name);
242 add_to_list(g_theme_view, file);
243 g_free(file);
244 }
245 g_free(name);
246 }
247 }
248 }
249
250 g_slist_foreach(list, (GFunc)g_free, NULL);
251 g_slist_free(list);
252 }
253 gtk_widget_destroy (dialog);
254 }
255
256 /*
257 static void menuSaveAs (GtkWindow *parent)
258 {
259 GtkWidget *dialog;
260 GtkFileChooser *chooser;
261
262 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);
263 chooser = GTK_FILE_CHOOSER(dialog);
264
265 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
266 gtk_file_chooser_set_current_folder (chooser, g_get_home_dir());
267 gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
268
269 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
270 char *filename = gtk_file_chooser_get_filename(chooser);
271 printf("fichier %s\n", filename);
272 //save_to_file (filename);
273 g_free (filename);
274 }
275 gtk_widget_destroy (dialog);
276 }
277 */
278
279 static void menuDelete (void)
280 {
281 GtkTreeSelection *sel;
282 GtkTreeIter iter;
283 GtkTreeModel *model;
284 char *value, *name1, *name2;
285
286 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
287 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
288 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
289 gtk_tree_selection_unselect_all(sel);
290 // remove from the gui
291 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
292
293 name1 = g_build_filename (g_get_user_config_dir(), "tint2", value, NULL);
294 name2 = g_strdup_printf("%s.tint2rc", name1);
295 g_remove(name2);
296
297 g_free(name1);
298 g_free(name2);
299 g_free(value);
300 }
301 }
302
303
304 static void menuProperties (void)
305 {
306 GtkTreeSelection *sel;
307 GtkTreeIter iter;
308 GtkTreeModel *model;
309 char *value, *name1, *name2, *cmd;
310
311 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
312 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
313 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
314
315 name1 = g_build_filename ("\'", g_get_user_config_dir(), "tint2", value, NULL);
316 name2 = g_strdup_printf("%s.tint2rc\'", name1);
317
318 cmd = g_strdup_printf("tintwizard.py %s", name2);
319 system(cmd);
320 g_free(cmd);
321
322 g_free(name1);
323 g_free(name2);
324 g_free(value);
325 }
326 }
327
328
329 static void menuRename (void)
330 {
331 printf("menuRename\n");
332 // g_rename
333 }
334
335
336 static void menuQuit (void)
337 {
338 write_config();
339
340 g_object_unref(g_store);
341 gtk_main_quit ();
342 }
343
344
345 static void menuRefresh (void)
346 {
347 printf("menuRefresh\n");
348 }
349
350
351 static void menuRefreshAll (void)
352 {
353 printf("menuRefreshAll\n");
354 }
355
356
357 static void menuApply (void)
358 {
359 GtkTreeSelection *sel;
360 GtkTreeIter iter;
361 GtkTreeModel *model;
362 char *value, *name1, *name2;
363
364 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view));
365 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) {
366 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
367 name1 = g_build_filename (g_get_user_config_dir(), "tint2", value, NULL);
368 name2 = g_strdup_printf("%s.tint2rc", name1);
369 g_free(name1);
370
371 copy_file(name2, g_path_config);
372 write_config();
373 g_free(value);
374 g_free(name2);
375
376 // restart panel
377 system("killall -SIGUSR1 tint2");
378 }
379 }
380
381
382 static void view_popup_menu(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
383 {
384 GtkWidget *w = gtk_ui_manager_get_widget(globalUIManager, "/ThemePopup");
385
386 gtk_menu_popup(GTK_MENU(w), NULL, NULL, NULL, NULL, (event != NULL) ? event->button : 0, gdk_event_get_time((GdkEvent*)event));
387 }
388
389
390 static gboolean view_onButtonPressed (GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
391 {
392 // single click with the right mouse button?
393 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
394 GtkTreeSelection *selection;
395
396 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
397
398 if (gtk_tree_selection_count_selected_rows(selection) <= 1) {
399 GtkTreePath *path;
400
401 // Get tree path for row that was clicked
402 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview), (gint) event->x, (gint) event->y, &path, NULL, NULL, NULL)) {
403 gtk_tree_selection_unselect_all(selection);
404 gtk_tree_selection_select_path(selection, path);
405 gtk_tree_path_free(path);
406 }
407 }
408
409 view_popup_menu(treeview, event, userdata);
410 return TRUE;
411 }
412 return FALSE;
413 }
414
415
416 static gboolean view_onPopupMenu (GtkWidget *treeview, gpointer userdata)
417 {
418 view_popup_menu(treeview, NULL, userdata);
419 return TRUE;
420 }
421
422
423 static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
424 {
425 menuApply();
426 }
427
428
429 static void load_theme(GtkWidget *list)
430 {
431 GDir *dir;
432 gchar *file, *pt1, *name;
433
434 dir = g_dir_open(g_path_dir, 0, NULL);
435 while ((file = g_dir_read_name(dir))) {
436 pt1 = strstr(file, ".tint2rc");
437 if (pt1) {
438 name = strndup(file, pt1-file);
439 add_to_list(list, name);
440 g_free(name);
441 }
442 }
443 g_dir_close(dir);
444
445 // search default theme
446 GtkTreeIter iter;
447 GtkTreeModel *model;
448 if (g_default_theme) {
449 printf("defaultTheme %s\n", g_default_theme);
450 //gtk_tree_selection_select_iter(GtkTreeSelection *selection, GtkTreeIter *iter);
451 }
452 }
453
454
455 // theme file management
456 void read_config()
457 {
458 char *path;
459
460 if (g_default_theme)
461 g_free(g_default_theme);
462
463 g_width = 600;
464 g_height = 350;
465 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
466 if (g_file_test (path, G_FILE_TEST_EXISTS)) {
467 FILE *fp;
468 char line[80];
469 char *key, *value;
470 if ((fp = fopen(path, "r")) != NULL) {
471 while (fgets(line, sizeof(line), fp) != NULL) {
472 if (parse_line(line, &key, &value)) {
473 if (strcmp (key, "default_theme") == 0)
474 g_default_theme = strdup (value);
475 else if (strcmp (key, "width") == 0)
476 g_width = atoi(value);
477 else if (strcmp (key, "height") == 0)
478 g_height = atoi(value);
479 free (key);
480 free (value);
481 }
482 }
483 fclose (fp);
484 }
485 }
486 g_free(path);
487 }
488
489
490 void write_config()
491 {
492 char *path;
493 FILE *fp;
494 GtkRequisition req;
495 int x, y, width, height, depth;
496 int top, bottom, left, right;
497 GdkRectangle rect;
498
499 gtk_window_get_size(GTK_WINDOW(g_window), &width, &height);
500 printf("write_config %d, %d\n", width, height);
501
502 // GTK incapacity to return (width, height) of the window is really annoying
503 gdk_window_get_geometry(GTK_WIDGET(g_window)->window, &x, &y, &width, &height, &depth);
504 printf(" write_config %d, %d, %d, %d\n", x, y, width, height);
505
506 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
507 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
508 //printf(" write_config %d, %d\n", rec.width, rec.height);
509
510 /*
511 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
512 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
513 gtk_tree_selection_select_path( sel, path );
514 gtk_tree_path_free( path );
515 */
516 path = g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL);
517 fp = fopen(path, "w");
518 if (fp != NULL) {
519 fputs("#---------------------------------------------\n", fp);
520 fputs("# TINT2CONF CONFIG FILE\n", fp);
521 fprintf(fp, "default_theme = %s\n", g_default_theme);
522 fprintf(fp, "width = %d\n", g_width);
523 fprintf(fp, "height = %d\n", g_height);
524 fputs("\n", fp);
525 fclose (fp);
526 }
527 g_free(path);
528 }
529
530
531 void check_theme()
532 {
533 g_path_dir = g_build_filename (g_get_user_config_dir(), "tint2", NULL);
534 if (!g_file_test (g_path_dir, G_FILE_TEST_IS_DIR))
535 g_mkdir(g_path_dir, 0777);
536
537 g_path_config = g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL);
538
539 }
540
541
542
This page took 0.058536 seconds and 5 git commands to generate.