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
;
45 static GtkUIManager
*globalUIManager
= NULL
;
47 static void menuAddWidget (GtkUIManager
*, GtkWidget
*, GtkContainer
*);
50 static void menuAdd();
51 static void menuSaveAs();
52 static void menuDelete();
53 static void menuProperties();
54 static void menuQuit();
55 static void menuRefresh();
56 static void menuRefreshAll();
57 static void menuApply();
58 static void menuAbout();
60 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
);
61 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
);
62 static void windowSizeAllocated();
63 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
);
67 static void selectTheme(const gchar
*name
);
68 static gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
);
69 static void load_theme();
70 static void initTheme();
71 static void read_config();
72 static void write_config();
75 // define menubar, toolbar and popup
76 static const char *global_ui
=
78 " <menubar name='MenuBar'>"
79 " <menu action='ThemeMenu'>"
80 " <menuitem action='ThemeAdd'/>"
81 " <menuitem action='ThemeSaveAs'/>"
83 " <menuitem action='ThemeDelete'/>"
85 // " <menuitem action='ThemeProperties'/>"
87 " <menuitem action='ThemeQuit'/>"
89 " <menu action='EditMenu'>"
90 " <menuitem action='EditRefresh'/>"
91 " <menuitem action='EditRefreshAll'/>"
93 // " <menuitem action='EditPreferences'/>"
95 " <menu action='HelpMenu'>"
96 " <menuitem action='HelpAbout'/>"
99 " <toolbar name='ToolBar'>"
100 " <toolitem action='ThemeProperties'/>"
101 " <toolitem action='ViewApply'/>"
103 " <popup name='ThemePopup'>"
104 " <menuitem action='EditRefresh'/>"
105 " <menuitem action='ThemeProperties'/>"
106 " <menuitem action='ViewApply'/>"
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
);
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 g_signal_connect(g_window
, "size-allocate", G_CALLBACK(windowSizeAllocated
), NULL
);
147 vBox
= gtk_vbox_new (FALSE
, 0);
148 gtk_container_add (GTK_CONTAINER(g_window
), vBox
);
150 actionGroup
= gtk_action_group_new ("menuActionGroup");
151 gtk_action_group_add_actions (actionGroup
, entries
, G_N_ELEMENTS (entries
), NULL
);
152 globalUIManager
= gtk_ui_manager_new();
153 gtk_ui_manager_insert_action_group (globalUIManager
, actionGroup
, 0);
154 gtk_ui_manager_add_ui_from_string (globalUIManager
, global_ui
, -1, NULL
);
155 g_signal_connect(globalUIManager
, "add_widget", G_CALLBACK (menuAddWidget
), vBox
);
156 gtk_ui_manager_ensure_update(globalUIManager
);
157 scrollbar
= gtk_scrolled_window_new(NULL
, NULL
);
158 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
159 gtk_box_pack_start(GTK_BOX(vBox
), scrollbar
, TRUE
, TRUE
, 0);
162 g_theme_view
= create_view();
163 gtk_container_add(GTK_CONTAINER(scrollbar
), g_theme_view
);
164 gtk_widget_show(g_theme_view
);
165 g_signal_connect(g_theme_view
, "button-press-event", (GCallback
)view_onButtonPressed
, NULL
);
166 g_signal_connect(g_theme_view
, "popup-menu", (GCallback
)view_onPopupMenu
, NULL
);
167 g_signal_connect(g_theme_view
, "row-activated", G_CALLBACK(viewRowActivated
), NULL
);
170 load_theme(g_theme_view
);
172 gtk_widget_show_all(g_window
);
178 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
180 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
181 gtk_widget_show(p_widget
);
185 static void menuAbout()
187 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
};
189 gtk_show_about_dialog(g_window
, "name", g_get_application_name( ),
190 "comments", _("Theming tool for tint2 panel"),
191 "version", VERSION_STRING
,
192 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
193 "logo-icon-name", NULL
, "authors", authors
,
194 /* Translators: translate "translator-credits" as
195 your name to have it appear in the credits in the "About"
197 "translator-credits", _("translator-credits"),
202 static void menuAdd()
205 GtkFileChooser
*chooser
;
206 GtkFileFilter
*filter
;
208 dialog
= gtk_file_chooser_dialog_new(_("Add a theme"), g_window
, GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_ADD
, GTK_RESPONSE_ACCEPT
, NULL
);
209 chooser
= GTK_FILE_CHOOSER(dialog
);
211 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
212 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
214 filter
= gtk_file_filter_new();
215 gtk_file_filter_set_name(filter
, _("Tint2 theme files"));
216 gtk_file_filter_add_pattern(filter
, "*.tint2rc");
217 gtk_file_chooser_add_filter(chooser
, filter
);
219 if (gtk_dialog_run (GTK_DIALOG(dialog
)) != GTK_RESPONSE_ACCEPT
) {
220 gtk_widget_destroy(dialog
);
225 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
226 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
227 gchar
*file
, *pt1
, *name
, *path
, *name_first
=NULL
;
228 for (l
= list
; l
; l
= l
->next
) {
229 file
= (char *)l
->data
;
230 pt1
= strrchr(file
, '/');
231 if (pt1
== NULL
) continue;
233 if (*pt1
== 0) continue;
235 name
= g_strdup(pt1
);
236 path
= g_build_filename (g_get_user_config_dir(), "tint2", name
, NULL
);
239 if (searchTheme(path
, model
, &iter
)) {
241 message
= g_strdup_printf(_("Couldn't add duplicate theme\n\'%s\'."), pt1
);
243 GtkWidget
*w
= gtk_message_dialog_new(g_window
, 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, message
, NULL
);
244 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
251 copy_file(file
, path
);
252 custom_list_append(path
);
253 if (name_first
== NULL
)
254 name_first
= g_strdup(path
);
258 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
260 gtk_widget_destroy(dialog
);
262 selectTheme(name_first
);
264 g_timeout_add(100, (GSourceFunc
)update_snapshot
, NULL
);
268 static void menuSaveAs ()
271 GtkFileChooser
*chooser
;
272 GtkTreeSelection
*sel
;
277 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
278 if (!gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
279 GtkWidget
*w
= gtk_message_dialog_new(g_window
, 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, _("Select the theme to be saved."), NULL
);
280 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
285 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
286 pt1
= strrchr (file
, '/');
289 dialog
= gtk_file_chooser_dialog_new(_("Save theme as"), g_window
, GTK_FILE_CHOOSER_ACTION_SAVE
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
290 chooser
= GTK_FILE_CHOOSER(dialog
);
292 gtk_file_chooser_set_do_overwrite_confirmation(chooser
, TRUE
);
293 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
294 gtk_file_chooser_set_current_name(chooser
, pt1
);
296 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
297 char *filename
= gtk_file_chooser_get_filename(chooser
);
298 copy_file(file
, filename
);
302 gtk_widget_destroy (dialog
);
306 static void menuDelete()
308 GtkTreeSelection
*sel
;
313 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
314 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
315 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &filename
, -1);
316 gtk_tree_selection_unselect_all(sel
);
318 // remove (gui and file)
319 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
320 GFile
*file
= g_file_new_for_path(filename
);
321 g_file_trash(file
, NULL
, NULL
);
322 g_object_unref(G_OBJECT(file
));
328 static void menuProperties()
330 GtkTreeSelection
*sel
;
335 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
336 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
337 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
339 cmd
= g_strdup_printf("gedit \'%s\' &", file
);
348 static void menuQuit()
353 g_free(g_path_config
);
357 g_free(g_default_theme
);
363 static void menuRefresh()
365 GtkTreeSelection
*sel
;
369 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
370 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
371 gtk_list_store_set(g_store
, &iter
, COL_SNAPSHOT
, NULL
, -1);
374 g_timeout_add(100, (GSourceFunc
)update_snapshot
, NULL
);
378 static void menuRefreshAll()
384 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
385 have_iter
= gtk_tree_model_get_iter_first(model
, &iter
);
387 gtk_list_store_set(g_store
, &iter
, COL_SNAPSHOT
, NULL
, -1);
388 have_iter
= gtk_tree_model_iter_next(model
, &iter
);
391 g_timeout_add(100, (GSourceFunc
)update_snapshot
, NULL
);
395 static void menuApply()
397 GtkTreeSelection
*sel
;
401 if (g_default_theme
) {
402 g_free(g_default_theme
);
403 g_default_theme
= NULL
;
405 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
406 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
407 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &g_default_theme
, -1);
409 copy_file(g_default_theme
, g_path_config
);
412 system("killall -SIGUSR1 tint2");
417 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
419 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
421 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
425 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
427 // single click with the right mouse button?
428 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
429 GtkTreeSelection
*selection
;
431 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
433 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
436 // Get tree path for row that was clicked
437 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
438 gtk_tree_selection_unselect_all(selection
);
439 gtk_tree_selection_select_path(selection
, path
);
440 gtk_tree_path_free(path
);
444 view_popup_menu(treeview
, event
, userdata
);
451 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
453 view_popup_menu(treeview
, NULL
, userdata
);
458 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
464 static void windowSizeAllocated()
466 const gboolean isMaximized
= g_window
->window
&& (gdk_window_get_state(g_window
->window
) & GDK_WINDOW_STATE_MAXIMIZED
);
469 gtk_window_get_size(GTK_WINDOW(g_window
), &g_width
, &g_height
);
473 static void load_theme(GtkWidget
*list
)
478 gboolean found_theme
= FALSE
;
480 dir
= g_dir_open(g_path_dir
, 0, NULL
);
481 if (dir
== NULL
) return;
482 while ((file
= g_dir_read_name(dir
))) {
483 pt1
= strstr(file
, ".tint2rc");
486 name
= g_build_filename (g_path_dir
, file
, NULL
);
487 custom_list_append(name
);
494 // create default theme file
495 name
= g_build_filename(g_get_user_config_dir(), "tint2", "default.tint2rc", NULL
);
496 copy_file(g_path_config
, name
);
497 custom_list_append(name
);
498 if (g_default_theme
) g_free(g_default_theme
);
499 g_default_theme
= strdup(name
);
503 selectTheme(g_default_theme
);
505 g_timeout_add(100, (GSourceFunc
)update_snapshot
, NULL
);
509 void selectTheme(const gchar
*name_theme
)
511 gboolean have_iter
, found_theme
;
515 if (!name_theme
) return;
517 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
518 found_theme
= searchTheme(name_theme
, model
, &iter
);
520 GtkTreePath
*path
= NULL
;
522 path
= gtk_tree_model_get_path(model
, &iter
);
524 have_iter
= gtk_tree_model_get_iter_first(model
, &iter
);
526 path
= gtk_tree_model_get_path(model
, &iter
);
529 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
)), &iter
);
530 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(g_theme_view
), path
, NULL
, FALSE
, 0, 0);
531 gtk_tree_path_free(path
);
536 gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
539 gboolean have_iter
, found
= FALSE
;
541 have_iter
= gtk_tree_model_get_iter_first(model
, iter
);
543 gtk_tree_model_get(model
, iter
, COL_THEME_FILE
, &name
, -1);
544 found
= (strcmp(name
, name_theme
) == 0);
548 have_iter
= gtk_tree_model_iter_next(model
, iter
);
556 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
557 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
558 g_mkdir(g_path_dir
, 0777);
560 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
568 if (g_default_theme
!= NULL
) {
569 g_free(g_default_theme
);
570 g_default_theme
= NULL
;
575 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
576 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
580 if ((fp
= fopen(path
, "r")) != NULL
) {
581 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
582 if (parse_line(line
, &key
, &value
)) {
583 if (strcmp (key
, "default_theme") == 0)
584 g_default_theme
= strdup (value
);
585 else if (strcmp (key
, "width") == 0)
586 g_width
= atoi(value
);
587 else if (strcmp (key
, "height") == 0)
588 g_height
= atoi(value
);
605 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
606 fp
= fopen(path
, "w");
608 fputs("#---------------------------------------------\n", fp
);
609 fputs("# TINT2CONF CONFIG FILE\n", fp
);
610 if (g_default_theme
!= NULL
) {
611 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
612 printf("default_theme %s\n", g_default_theme
);
614 fprintf(fp
, "width = %d\n", g_width
);
615 fprintf(fp
, "height = %d\n", g_height
);