1 /**************************************************************************
5 * Copyright (C) 2009 Thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
28 #include <glib/gstdio.h>
29 #include <glib/gi18n.h>
31 #include "../version.h"
33 #include "theme_view.h"
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
;
44 GtkWidget
*g_theme_view
;
46 static GtkUIManager
*globalUIManager
= NULL
;
48 static void menuAddWidget (GtkUIManager
*, GtkWidget
*, GtkContainer
*);
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
);
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
);
68 static void load_theme();
69 static void read_config();
70 static void write_config();
71 static void check_theme();
74 // define menubar, toolbar and popup
75 static const char *global_ui
=
77 " <menubar name='MenuBar'>"
78 " <menu action='ThemeMenu'>"
79 " <menuitem action='ThemeAdd'/>"
80 " <menuitem action='ThemeSaveAs'/>"
82 " <menuitem action='ThemeDelete'/>"
84 " <menuitem action='ThemeProperties'/>"
86 " <menuitem action='ThemeQuit'/>"
88 " <menu action='EditMenu'>"
89 " <menuitem action='EditRefresh'/>"
90 " <menuitem action='EditRefreshAll'/>"
92 // " <menuitem action='EditPreferences'/>"
94 " <menu action='HelpMenu'>"
95 " <menuitem action='HelpAbout'/>"
98 " <toolbar name='ToolBar'>"
99 " <toolitem action='ViewApply'/>"
100 " <toolitem action='ThemeProperties'/>"
102 " <popup name='ThemePopup'>"
103 " <menuitem action='ViewApply'/>"
104 " <menuitem action='ThemeProperties'/>"
106 " <menuitem action='ThemeDelete'/>"
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
)}
129 int main (int argc
, char ** argv
)
131 GtkWidget
*vBox
= NULL
, *scrollbar
= NULL
;
132 GtkActionGroup
*actionGroup
;
134 gtk_init (&argc
, &argv
);
135 g_thread_init( NULL
);
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
);
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);
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
);
169 load_theme(g_theme_view
);
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);
175 gtk_widget_show_all(g_window
);
181 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
183 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
184 gtk_widget_show(p_widget
);
188 static void menuAbout(GtkWindow
* parent
)
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
};
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"
200 "translator-credits", _("translator-credits"),
205 static void menuAdd (GtkWindow
*parent
)
208 GtkFileChooser
*chooser
;
209 GtkFileFilter
*filter
;
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
);
214 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
215 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
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
);
222 if (gtk_dialog_run (GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
223 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
225 gchar
*file
, *pt1
, *name
, *path
;
226 for (l
= list
; l
; l
= l
->next
) {
227 file
= (char *)l
->data
;
228 pt1
= strrchr (file
, '/');
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
);
242 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
245 gtk_widget_destroy (dialog
);
249 static void menuSaveAs (GtkWindow
*parent
)
252 GtkFileChooser
*chooser
;
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
);
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"));
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);
267 gtk_widget_destroy (dialog
);
271 static void menuDelete (void)
273 GtkTreeSelection
*sel
;
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
);
283 // remove (gui and disk)
284 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
291 static void menuProperties (void)
293 GtkTreeSelection
*sel
;
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);
302 cmd
= g_strdup_printf("tintwizard.py \'%s\'", file
);
311 static void menuQuit (void)
318 static void menuRefresh (void)
320 printf("menuRefresh\n");
324 static void menuRefreshAll (void)
326 printf("menuRefreshAll\n");
330 static void menuPreferences (void)
332 printf("menuPreferences\n");
336 static void menuApply (void)
338 GtkTreeSelection
*sel
;
343 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
344 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
345 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
348 copy_file(file
, g_path_config
);
352 system("killall -SIGUSR1 tint2");
357 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
359 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
361 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
365 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
367 // single click with the right mouse button?
368 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
369 GtkTreeSelection
*selection
;
371 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
373 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
376 // Get tree path for row that was clicked
377 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
378 gtk_tree_selection_unselect_all(selection
);
379 gtk_tree_selection_select_path(selection
, path
);
380 gtk_tree_path_free(path
);
384 view_popup_menu(treeview
, event
, userdata
);
391 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
393 view_popup_menu(treeview
, NULL
, userdata
);
398 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
404 static void load_theme(GtkWidget
*list
)
407 gchar
*pt1
, *name
, *file
;
408 int found_theme
= FALSE
;
410 dir
= g_dir_open(g_path_dir
, 0, NULL
);
411 if (dir
== NULL
) return;
412 while ((file
= g_dir_read_name(dir
))) {
413 pt1
= strstr(file
, ".tint2rc");
416 name
= g_build_filename (g_path_dir
, file
, NULL
);
417 custom_list_append(name
);
424 // create default theme file
425 name
= g_build_filename (g_get_user_config_dir(), "tint2", "default.tint2rc", NULL
);
426 copy_file(g_path_config
, name
);
427 custom_list_append(name
);
431 // search default theme
434 if (g_default_theme
!= NULL
) {
435 printf("loadTheme : defaultTheme %s\n", g_default_theme
);
436 //gtk_tree_selection_select_iter(GtkTreeSelection *selection, GtkTreeIter *iter);
441 // theme file management
446 if (g_default_theme
!= NULL
) {
447 g_free(g_default_theme
);
448 g_default_theme
= NULL
;
453 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
454 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
458 if ((fp
= fopen(path
, "r")) != NULL
) {
459 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
460 if (parse_line(line
, &key
, &value
)) {
461 if (strcmp (key
, "default_theme") == 0)
462 g_default_theme
= strdup (value
);
463 else if (strcmp (key
, "width") == 0)
464 g_width
= atoi(value
);
465 else if (strcmp (key
, "height") == 0)
466 g_height
= atoi(value
);
475 if (g_default_theme
!= NULL
)
476 printf("readConfig : defaultTheme %s\n", g_default_theme
);
485 int x
, y
, width
, height
, depth
;
486 int top
, bottom
, left
, right
;
489 gtk_window_get_size(GTK_WINDOW(g_window
), &width
, &height
);
490 printf("write_config %d, %d\n", width
, height
);
492 // GTK incapacity to return (width, height) of the window is really annoying
493 gdk_window_get_geometry(GTK_WIDGET(g_window
)->window
, &x
, &y
, &width
, &height
, &depth
);
494 printf(" write_config %d, %d, %d, %d\n", x
, y
, width
, height
);
496 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
497 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
498 //printf(" write_config %d, %d\n", rec.width, rec.height);
501 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
502 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
503 gtk_tree_selection_select_path( sel, path );
504 gtk_tree_path_free( path );
506 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
507 fp
= fopen(path
, "w");
509 fputs("#---------------------------------------------\n", fp
);
510 fputs("# TINT2CONF CONFIG FILE\n", fp
);
511 if (g_default_theme
!= NULL
) {
512 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
513 printf("default_theme %s\n", g_default_theme
);
515 fprintf(fp
, "width = %d\n", g_width
);
516 fprintf(fp
, "height = %d\n", g_height
);
526 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
527 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
528 g_mkdir(g_path_dir
, 0777);
530 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);