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();
52 static void menuSaveAs();
53 static void menuDelete();
54 static void menuProperties();
55 static void menuQuit();
56 static void menuRefresh();
57 static void menuRefreshAll();
58 static void menuPreferences();
59 static void menuApply();
60 static void menuAbout();
62 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
);
63 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
);
64 static void windowSizeAllocated();
65 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
);
69 static void selectTheme(const gchar
*name
);
70 static gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
);
71 static void load_theme();
72 static void read_config();
73 static void write_config();
74 static void check_theme();
77 // define menubar, toolbar and popup
78 static const char *global_ui
=
80 " <menubar name='MenuBar'>"
81 " <menu action='ThemeMenu'>"
82 " <menuitem action='ThemeAdd'/>"
83 " <menuitem action='ThemeSaveAs'/>"
85 " <menuitem action='ThemeDelete'/>"
87 // " <menuitem action='ThemeProperties'/>"
89 " <menuitem action='ThemeQuit'/>"
91 " <menu action='EditMenu'>"
92 " <menuitem action='EditRefresh'/>"
93 " <menuitem action='EditRefreshAll'/>"
95 // " <menuitem action='EditPreferences'/>"
97 " <menu action='HelpMenu'>"
98 " <menuitem action='HelpAbout'/>"
101 " <toolbar name='ToolBar'>"
102 " <toolitem action='ThemeProperties'/>"
103 " <toolitem action='ViewApply'/>"
105 " <popup name='ThemePopup'>"
106 " <menuitem action='EditRefresh'/>"
107 " <menuitem action='ThemeProperties'/>"
108 " <menuitem action='ViewApply'/>"
110 " <menuitem action='ThemeDelete'/>"
115 // define menubar and toolbar action
116 static GtkActionEntry entries
[] = {
117 {"ThemeMenu", NULL
, "Theme", NULL
, NULL
, NULL
},
118 {"ThemeAdd", GTK_STOCK_ADD
, "_Add...", "<Control>N", "Add theme", G_CALLBACK (menuAdd
)},
119 {"ThemeSaveAs", GTK_STOCK_SAVE_AS
, "_Save as...", NULL
, "Save theme as", G_CALLBACK (menuSaveAs
)},
120 {"ThemeDelete", GTK_STOCK_DELETE
, "_Delete", NULL
, "Delete theme", G_CALLBACK (menuDelete
)},
121 {"ThemeProperties", GTK_STOCK_PROPERTIES
, "_Properties...", NULL
, "Show properties", G_CALLBACK (menuProperties
)},
122 {"ThemeQuit", GTK_STOCK_QUIT
, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit
)},
123 {"EditMenu", NULL
, "Edit", NULL
, NULL
, NULL
},
124 {"EditRefresh", GTK_STOCK_REFRESH
, "Refresh", NULL
, "Refresh", G_CALLBACK (menuRefresh
)},
125 {"EditRefreshAll", GTK_STOCK_REFRESH
, "Refresh all", NULL
, "Refresh all", G_CALLBACK (menuRefreshAll
)},
126 // {"EditPreferences", GTK_STOCK_PREFERENCES, "Preferences", NULL, "Preferences", G_CALLBACK (menuPreferences)},
127 {"ViewApply", GTK_STOCK_APPLY
, "Apply", NULL
, "Apply theme", G_CALLBACK (menuApply
)},
128 {"HelpMenu", NULL
, "Help", NULL
, NULL
, NULL
},
129 {"HelpAbout", GTK_STOCK_ABOUT
, "_About", "<Control>A", "About", G_CALLBACK (menuAbout
)}
133 int main (int argc
, char ** argv
)
135 GtkWidget
*vBox
= NULL
, *scrollbar
= NULL
;
136 GtkActionGroup
*actionGroup
;
138 gtk_init (&argc
, &argv
);
139 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 g_signal_connect(g_window
, "size-allocate", G_CALLBACK(windowSizeAllocated
), NULL
);
149 vBox
= gtk_vbox_new (FALSE
, 0);
150 gtk_container_add (GTK_CONTAINER(g_window
), vBox
);
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);
164 g_theme_view
= create_view();
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
);
172 load_theme(g_theme_view
);
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);
178 gtk_widget_show_all(g_window
);
184 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
186 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
187 gtk_widget_show(p_widget
);
191 static void menuAbout()
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
};
195 gtk_show_about_dialog(g_window
, "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"
203 "translator-credits", _("translator-credits"),
208 static void menuAdd()
211 GtkFileChooser
*chooser
;
212 GtkFileFilter
*filter
;
214 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
);
215 chooser
= GTK_FILE_CHOOSER(dialog
);
217 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
218 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
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
);
225 if (gtk_dialog_run (GTK_DIALOG(dialog
)) != GTK_RESPONSE_ACCEPT
) {
226 gtk_widget_destroy(dialog
);
231 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
232 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
233 gchar
*file
, *pt1
, *name
, *path
, *name_first
=NULL
;
234 for (l
= list
; l
; l
= l
->next
) {
235 file
= (char *)l
->data
;
236 pt1
= strrchr(file
, '/');
237 if (pt1
== NULL
) continue;
239 if (*pt1
== 0) continue;
241 name
= g_strdup(pt1
);
242 path
= g_build_filename (g_get_user_config_dir(), "tint2", name
, NULL
);
245 if (searchTheme(path
, model
, &iter
)) {
247 message
= g_strdup_printf(_("Couldn't add duplicate theme\n\'%s\'."), pt1
);
249 GtkWidget
*w
= gtk_message_dialog_new(g_window
, 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, message
, NULL
);
250 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
257 copy_file(file
, path
);
258 custom_list_append(path
);
259 if (name_first
== NULL
)
260 name_first
= g_strdup(path
);
264 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
266 gtk_widget_destroy(dialog
);
268 selectTheme(name_first
);
273 static void menuSaveAs ()
276 GtkFileChooser
*chooser
;
277 GtkTreeSelection
*sel
;
282 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
283 if (!gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
284 GtkWidget
*w
= gtk_message_dialog_new(g_window
, 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, _("Select the theme to be saved."), NULL
);
285 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
290 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
291 pt1
= strrchr (file
, '/');
294 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
);
295 chooser
= GTK_FILE_CHOOSER(dialog
);
297 gtk_file_chooser_set_do_overwrite_confirmation(chooser
, TRUE
);
298 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
299 gtk_file_chooser_set_current_name(chooser
, pt1
);
301 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
302 char *filename
= gtk_file_chooser_get_filename(chooser
);
303 copy_file(file
, filename
);
307 gtk_widget_destroy (dialog
);
311 static void menuDelete()
313 GtkTreeSelection
*sel
;
318 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
319 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
320 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &filename
, -1);
321 gtk_tree_selection_unselect_all(sel
);
323 // remove (gui and file)
324 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
325 GFile
*file
= g_file_new_for_path(filename
);
326 g_file_trash(file
, NULL
, NULL
);
327 g_object_unref(G_OBJECT(file
));
333 static void menuProperties()
335 GtkTreeSelection
*sel
;
340 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
341 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
342 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
344 cmd
= g_strdup_printf("gedit \'%s\' &", file
);
353 static void menuQuit()
358 g_free(g_path_config
);
362 g_free(g_default_theme
);
368 static void menuRefresh()
370 printf("menuRefresh\n");
374 static void menuRefreshAll()
376 printf("menuRefreshAll\n");
380 static void menuPreferences()
382 printf("menuPreferences\n");
386 static void menuApply()
388 GtkTreeSelection
*sel
;
392 if (g_default_theme
) {
393 g_free(g_default_theme
);
394 g_default_theme
= NULL
;
396 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
397 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
398 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &g_default_theme
, -1);
400 copy_file(g_default_theme
, g_path_config
);
403 system("killall -SIGUSR1 tint2");
408 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
410 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
412 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
416 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
418 // single click with the right mouse button?
419 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
420 GtkTreeSelection
*selection
;
422 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
424 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
427 // Get tree path for row that was clicked
428 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
429 gtk_tree_selection_unselect_all(selection
);
430 gtk_tree_selection_select_path(selection
, path
);
431 gtk_tree_path_free(path
);
435 view_popup_menu(treeview
, event
, userdata
);
442 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
444 view_popup_menu(treeview
, NULL
, userdata
);
449 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
455 static void windowSizeAllocated()
457 const gboolean isMaximized
= g_window
->window
&& (gdk_window_get_state(g_window
->window
) & GDK_WINDOW_STATE_MAXIMIZED
);
460 gtk_window_get_size(GTK_WINDOW(g_window
), &g_width
, &g_height
);
464 static void load_theme(GtkWidget
*list
)
467 gchar
*pt1
, *name
, *file
;
468 gboolean found_theme
= FALSE
;
470 dir
= g_dir_open(g_path_dir
, 0, NULL
);
471 if (dir
== NULL
) return;
472 while ((file
= g_dir_read_name(dir
))) {
473 pt1
= strstr(file
, ".tint2rc");
476 name
= g_build_filename (g_path_dir
, file
, NULL
);
477 custom_list_append(name
);
484 // create default theme file
485 name
= g_build_filename(g_get_user_config_dir(), "tint2", "default.tint2rc", NULL
);
486 copy_file(g_path_config
, name
);
487 custom_list_append(name
);
488 if (g_default_theme
) g_free(g_default_theme
);
489 g_default_theme
= strdup(name
);
493 selectTheme(g_default_theme
);
497 void selectTheme(const gchar
*name_theme
)
499 gboolean have_iter
, found_theme
;
503 if (!name_theme
) return;
505 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
506 found_theme
= searchTheme(name_theme
, model
, &iter
);
508 GtkTreePath
*path
= NULL
;
510 path
= gtk_tree_model_get_path(model
, &iter
);
512 have_iter
= gtk_tree_model_get_iter_first(model
, &iter
);
514 path
= gtk_tree_model_get_path(model
, &iter
);
517 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
)), &iter
);
518 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(g_theme_view
), path
, NULL
, FALSE
, 0, 0);
519 gtk_tree_path_free(path
);
524 gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
527 gboolean have_iter
, found
= FALSE
;
529 have_iter
= gtk_tree_model_get_iter_first(model
, iter
);
531 gtk_tree_model_get(model
, iter
, COL_THEME_FILE
, &name
, -1);
532 found
= (strcmp(name
, name_theme
) == 0);
536 have_iter
= gtk_tree_model_iter_next(model
, iter
);
542 // theme file management
547 if (g_default_theme
!= NULL
) {
548 g_free(g_default_theme
);
549 g_default_theme
= NULL
;
554 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
555 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
559 if ((fp
= fopen(path
, "r")) != NULL
) {
560 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
561 if (parse_line(line
, &key
, &value
)) {
562 if (strcmp (key
, "default_theme") == 0)
563 g_default_theme
= strdup (value
);
564 else if (strcmp (key
, "width") == 0)
565 g_width
= atoi(value
);
566 else if (strcmp (key
, "height") == 0)
567 g_height
= atoi(value
);
584 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
585 fp
= fopen(path
, "w");
587 fputs("#---------------------------------------------\n", fp
);
588 fputs("# TINT2CONF CONFIG FILE\n", fp
);
589 if (g_default_theme
!= NULL
) {
590 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
591 printf("default_theme %s\n", g_default_theme
);
593 fprintf(fp
, "width = %d\n", g_width
);
594 fprintf(fp
, "height = %d\n", g_height
);
604 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
605 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
606 g_mkdir(g_path_dir
, 0777);
608 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);