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
;
45 GtkWidget
*g_theme_view
;
46 GtkCellRenderer
*g_renderer
;
48 static GtkUIManager
*globalUIManager
= NULL
;
50 static void menuAddWidget (GtkUIManager
*, GtkWidget
*, GtkContainer
*);
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
);
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
);
70 static void load_theme();
71 static void read_config();
72 static void write_config();
73 static void check_theme();
76 // define menubar, toolbar and popup
77 static const char *global_ui
=
79 " <menubar name='MenuBar'>"
80 " <menu action='ThemeMenu'>"
81 " <menuitem action='ThemeAdd'/>"
82 " <menuitem action='ThemeSaveAs'/>"
84 " <menuitem action='ThemeDelete'/>"
86 " <menuitem action='ThemeProperties'/>"
88 " <menuitem action='ThemeQuit'/>"
90 " <menu action='EditMenu'>"
91 " <menuitem action='EditRefresh'/>"
92 " <menuitem action='EditRefreshAll'/>"
94 // " <menuitem action='EditPreferences'/>"
96 " <menu action='HelpMenu'>"
97 " <menuitem action='HelpAbout'/>"
100 " <toolbar name='ToolBar'>"
101 " <toolitem action='ViewApply'/>"
102 " <toolitem action='ThemeProperties'/>"
104 " <popup name='ThemePopup'>"
105 " <menuitem action='ViewApply'/>"
106 " <menuitem action='ThemeProperties'/>"
108 " <menuitem action='ThemeDelete'/>"
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
)}
131 int main (int argc
, char ** argv
)
133 GtkWidget
*vBox
= NULL
, *scrollbar
= NULL
;
134 GtkActionGroup
*actionGroup
;
136 gtk_init (&argc
, &argv
);
137 g_thread_init( NULL
);
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
);
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);
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
);
171 load_theme(g_theme_view
);
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);
177 gtk_widget_show_all(g_window
);
183 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
185 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
186 gtk_widget_show(p_widget
);
190 static void menuAbout(GtkWindow
* parent
)
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
};
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"
202 "translator-credits", _("translator-credits"),
207 static void menuAdd (GtkWindow
*parent
)
210 GtkFileChooser
*chooser
;
211 GtkFileFilter
*filter
;
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
);
216 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
217 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
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
);
224 if (gtk_dialog_run (GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
225 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
227 gchar
*file
, *pt1
, *name
, *path
;
228 for (l
= list
; l
; l
= l
->next
) {
229 file
= (char *)l
->data
;
230 pt1
= strrchr (file
, '/');
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
);
244 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
247 gtk_widget_destroy (dialog
);
251 static void menuSaveAs (GtkWindow
*parent
)
254 GtkFileChooser
*chooser
;
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
);
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"));
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);
269 gtk_widget_destroy (dialog
);
273 static void menuDelete (void)
275 GtkTreeSelection
*sel
;
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
);
285 // remove (gui and disk)
286 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
293 static void menuProperties (void)
295 GtkTreeSelection
*sel
;
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);
304 cmd
= g_strdup_printf("tintwizard.py \'%s\'", value
);
313 static void menuQuit (void)
317 g_object_unref(g_store
);
322 static void menuRefresh (void)
324 printf("menuRefresh\n");
328 static void menuRefreshAll (void)
330 printf("menuRefreshAll\n");
334 static void menuPreferences (void)
336 printf("menuPreferences\n");
340 static void menuApply (void)
342 GtkTreeSelection
*sel
;
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);
351 copy_file(value
, g_path_config
);
355 system("killall -SIGUSR1 tint2");
360 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
362 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
364 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
368 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
370 // single click with the right mouse button?
371 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
372 GtkTreeSelection
*selection
;
374 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
376 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
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
);
387 view_popup_menu(treeview
, event
, userdata
);
394 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
396 view_popup_menu(treeview
, NULL
, userdata
);
401 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
407 static void load_theme(GtkWidget
*list
)
410 gchar
*file
, *pt1
, *name
;
411 int found_theme
= FALSE
;
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");
419 name
= g_build_filename (g_path_dir
, file
, NULL
);
420 add_to_list(list
, name
);
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
);
434 // search default theme
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);
444 // theme file management
449 if (g_default_theme
!= NULL
) {
450 g_free(g_default_theme
);
451 g_default_theme
= NULL
;
456 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
457 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
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
);
478 if (g_default_theme
!= NULL
)
479 printf("readConfig : defaultTheme %s\n", g_default_theme
);
488 int x
, y
, width
, height
, depth
;
489 int top
, bottom
, left
, right
;
492 gtk_window_get_size(GTK_WINDOW(g_window
), &width
, &height
);
493 printf("write_config %d, %d\n", width
, height
);
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
);
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);
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 );
509 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
510 fp
= fopen(path
, "w");
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
);
518 fprintf(fp
, "width = %d\n", g_width
);
519 fprintf(fp
, "height = %d\n", g_height
);
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);
533 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);