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 **************************************************************************/
25 #include "theme_view.h"
26 #include "properties.h"
28 #define SNAPSHOT_TICK 190
31 // default config file and directory
32 char *g_path_config
= NULL
;
33 char *g_path_dir
= NULL
;
34 char *g_default_theme
= NULL
;
35 char *g_cmd_property
= NULL
;
36 int g_width
, g_height
;
40 static GtkUIManager
*globalUIManager
= NULL
;
42 static void menuAddWidget (GtkUIManager
*, GtkWidget
*, GtkContainer
*);
45 static void menuAdd();
46 static void menuSaveAs();
47 static void menuDelete();
48 static void menuProperties();
49 static void menuQuit();
50 static void menuRefresh();
51 static void menuRefreshAll();
52 static void menuApply();
53 static void menuAbout();
55 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
);
56 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
);
57 static void windowSizeAllocated();
58 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
);
62 static void selectTheme(const gchar
*name
);
63 static gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
);
64 static void load_theme();
65 static void initTheme();
66 static void read_config();
67 static void write_config();
70 // define menubar, toolbar and popup
71 static const char *global_ui
=
73 " <menubar name='MenuBar'>"
74 " <menu action='ThemeMenu'>"
75 " <menuitem action='ThemeAdd'/>"
76 " <menuitem action='ThemeSaveAs'/>"
78 " <menuitem action='ThemeDelete'/>"
80 // " <menuitem action='ThemeProperties'/>"
82 " <menuitem action='ThemeQuit'/>"
84 " <menu action='EditMenu'>"
85 " <menuitem action='EditRefresh'/>"
86 " <menuitem action='EditRefreshAll'/>"
88 // " <menuitem action='EditPreferences'/>"
90 " <menu action='HelpMenu'>"
91 " <menuitem action='HelpAbout'/>"
94 " <toolbar name='ToolBar'>"
95 " <toolitem action='ThemeProperties'/>"
96 " <toolitem action='ViewApply'/>"
98 " <popup name='ThemePopup'>"
99 " <menuitem action='ThemeProperties'/>"
100 " <menuitem action='EditRefresh'/>"
101 " <menuitem action='ViewApply'/>"
103 " <menuitem action='ThemeDelete'/>"
108 // define menubar and toolbar action
109 static GtkActionEntry entries
[] = {
110 {"ThemeMenu", NULL
, _("Theme"), NULL
, NULL
, NULL
},
111 {"ThemeAdd", GTK_STOCK_ADD
, _("_Add..."), "<Control>N", _("Add theme"), G_CALLBACK (menuAdd
)},
112 {"ThemeSaveAs", GTK_STOCK_SAVE_AS
, _("_Save as..."), NULL
, _("Save theme as"), G_CALLBACK (menuSaveAs
)},
113 {"ThemeDelete", GTK_STOCK_DELETE
, _("_Delete"), NULL
, _("Delete theme"), G_CALLBACK (menuDelete
)},
114 {"ThemeProperties", GTK_STOCK_PROPERTIES
, _("_Properties..."), NULL
, _("Show properties"), G_CALLBACK (menuProperties
)},
115 {"ThemeQuit", GTK_STOCK_QUIT
, _("_Quit"), "<control>Q", _("Quit"), G_CALLBACK (menuQuit
)},
116 {"EditMenu", NULL
, "Edit", NULL
, NULL
, NULL
},
117 {"EditRefresh", GTK_STOCK_REFRESH
, _("Refresh"), NULL
, _("Refresh"), G_CALLBACK (menuRefresh
)},
118 {"EditRefreshAll", GTK_STOCK_REFRESH
, _("Refresh all"), NULL
, _("Refresh all"), G_CALLBACK (menuRefreshAll
)},
119 // {"EditPreferences", GTK_STOCK_PREFERENCES, "Preferences", NULL, "Preferences", G_CALLBACK (menuPreferences)},
120 {"ViewApply", GTK_STOCK_APPLY
, _("Apply"), NULL
, _("Apply theme"), G_CALLBACK (menuApply
)},
121 {"HelpMenu", NULL
, _("Help"), NULL
, NULL
, NULL
},
122 {"HelpAbout", GTK_STOCK_ABOUT
, _("_About"), "<Control>A", _("About"), G_CALLBACK (menuAbout
)}
126 int main (int argc
, char ** argv
)
128 GtkWidget
*vBox
= NULL
, *scrollbar
= NULL
;
129 GtkActionGroup
*actionGroup
;
131 gtk_init (&argc
, &argv
);
132 g_thread_init( NULL
);
136 // define main layout : container, menubar, toolbar
137 g_window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
138 gtk_window_set_title(GTK_WINDOW(g_window
), _("Panel theming"));
139 gtk_window_resize(GTK_WINDOW(g_window
), g_width
, g_height
);
140 g_signal_connect(G_OBJECT(g_window
), "destroy", G_CALLBACK (menuQuit
), NULL
);
141 g_signal_connect(g_window
, "size-allocate", G_CALLBACK(windowSizeAllocated
), NULL
);
142 vBox
= gtk_vbox_new (FALSE
, 0);
143 gtk_container_add (GTK_CONTAINER(g_window
), vBox
);
145 actionGroup
= gtk_action_group_new ("menuActionGroup");
146 gtk_action_group_add_actions (actionGroup
, entries
, G_N_ELEMENTS (entries
), NULL
);
147 globalUIManager
= gtk_ui_manager_new();
148 gtk_ui_manager_insert_action_group (globalUIManager
, actionGroup
, 0);
149 gtk_ui_manager_add_ui_from_string (globalUIManager
, global_ui
, -1, NULL
);
150 g_signal_connect(globalUIManager
, "add_widget", G_CALLBACK (menuAddWidget
), vBox
);
151 gtk_ui_manager_ensure_update(globalUIManager
);
152 scrollbar
= gtk_scrolled_window_new(NULL
, NULL
);
153 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
154 gtk_box_pack_start(GTK_BOX(vBox
), scrollbar
, TRUE
, TRUE
, 0);
157 g_theme_view
= create_view();
158 gtk_container_add(GTK_CONTAINER(scrollbar
), g_theme_view
);
159 gtk_widget_show(g_theme_view
);
160 g_signal_connect(g_theme_view
, "button-press-event", (GCallback
)view_onButtonPressed
, NULL
);
161 g_signal_connect(g_theme_view
, "popup-menu", (GCallback
)view_onPopupMenu
, NULL
);
162 g_signal_connect(g_theme_view
, "row-activated", G_CALLBACK(viewRowActivated
), NULL
);
165 load_theme(g_theme_view
);
167 /* temporaire !!!!!!!!!!!!!!
168 GtkWidget *prop = create_properties();
169 gtk_window_present(GTK_WINDOW(prop));
171 gtk_widget_show_all(g_window
);
177 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
179 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
180 gtk_widget_show(p_widget
);
184 static void menuAbout()
186 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
};
188 gtk_show_about_dialog(GTK_WINDOW(g_window
), "name", g_get_application_name( ),
189 "comments", _("Theming tool for tint2 panel"),
190 "version", VERSION_STRING
,
191 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
192 "logo-icon-name", NULL
, "authors", authors
,
193 /* Translators: translate "translator-credits" as
194 your name to have it appear in the credits in the "About"
196 "translator-credits", _("translator-credits"),
201 static void menuAdd()
204 GtkFileChooser
*chooser
;
205 GtkFileFilter
*filter
;
207 dialog
= gtk_file_chooser_dialog_new(_("Add a theme"), GTK_WINDOW(g_window
), GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_ADD
, GTK_RESPONSE_ACCEPT
, NULL
);
208 chooser
= GTK_FILE_CHOOSER(dialog
);
210 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
211 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
213 filter
= gtk_file_filter_new();
214 gtk_file_filter_set_name(filter
, _("Tint2 theme files"));
215 gtk_file_filter_add_pattern(filter
, "*.tint2rc");
216 gtk_file_chooser_add_filter(chooser
, filter
);
218 if (gtk_dialog_run (GTK_DIALOG(dialog
)) != GTK_RESPONSE_ACCEPT
) {
219 gtk_widget_destroy(dialog
);
224 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
225 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
226 gchar
*file
, *pt1
, *name
, *path
, *name_first
=NULL
;
227 for (l
= list
; l
; l
= l
->next
) {
228 file
= (char *)l
->data
;
229 pt1
= strrchr(file
, '/');
230 if (pt1
== NULL
) continue;
232 if (*pt1
== 0) continue;
234 name
= g_strdup(pt1
);
235 path
= g_build_filename (g_get_user_config_dir(), "tint2", name
, NULL
);
238 if (searchTheme(path
, model
, &iter
)) {
240 message
= g_strdup_printf(_("Couldn't add duplicate theme\n\'%s\'."), pt1
);
242 GtkWidget
*w
= gtk_message_dialog_new(GTK_WINDOW(g_window
), 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, message
, NULL
);
243 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
250 copy_file(file
, path
);
251 custom_list_append(path
);
252 if (name_first
== NULL
)
253 name_first
= g_strdup(path
);
257 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
259 gtk_widget_destroy(dialog
);
261 selectTheme(name_first
);
263 g_timeout_add(SNAPSHOT_TICK
, (GSourceFunc
)update_snapshot
, NULL
);
267 static void menuSaveAs ()
270 GtkFileChooser
*chooser
;
271 GtkTreeSelection
*sel
;
276 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
277 if (!gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
278 GtkWidget
*w
= gtk_message_dialog_new(GTK_WINDOW(g_window
), 0, GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, _("Select the theme to be saved."));
279 g_signal_connect_swapped(w
, "response", G_CALLBACK(gtk_widget_destroy
), w
);
284 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
285 pt1
= strrchr (file
, '/');
288 dialog
= gtk_file_chooser_dialog_new(_("Save theme as"), GTK_WINDOW(g_window
), GTK_FILE_CHOOSER_ACTION_SAVE
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
289 chooser
= GTK_FILE_CHOOSER(dialog
);
291 gtk_file_chooser_set_do_overwrite_confirmation(chooser
, TRUE
);
292 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
293 gtk_file_chooser_set_current_name(chooser
, pt1
);
295 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
296 char *filename
= gtk_file_chooser_get_filename(chooser
);
297 copy_file(file
, filename
);
301 gtk_widget_destroy (dialog
);
305 static void menuDelete()
307 GtkTreeSelection
*sel
;
312 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
313 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
314 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &filename
, -1);
315 gtk_tree_selection_unselect_all(sel
);
317 // remove (gui and file)
318 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
319 GFile
*file
= g_file_new_for_path(filename
);
320 g_file_trash(file
, NULL
, NULL
);
321 g_object_unref(G_OBJECT(file
));
327 static void menuProperties()
329 GtkTreeSelection
*sel
;
334 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
335 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
336 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &file
, -1);
339 prop = create_properties();
340 gtk_window_present(GTK_WINDOW(prop));
341 //printf("menuProperties : fin\n");
344 cmd
= g_strdup_printf("%s \'%s\' &", g_cmd_property
, file
);
345 printf("cmd %s\n", cmd
);
354 static void menuQuit()
359 g_free(g_path_config
);
363 g_free(g_default_theme
);
365 g_free(g_cmd_property
);
371 static void menuRefresh()
373 GtkTreeSelection
*sel
;
377 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
378 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
379 gtk_list_store_set(g_store
, &iter
, COL_SNAPSHOT
, NULL
, -1);
382 g_timeout_add(SNAPSHOT_TICK
, (GSourceFunc
)update_snapshot
, NULL
);
386 static void menuRefreshAll()
392 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
393 have_iter
= gtk_tree_model_get_iter_first(model
, &iter
);
395 gtk_list_store_set(g_store
, &iter
, COL_SNAPSHOT
, NULL
, -1);
396 have_iter
= gtk_tree_model_iter_next(model
, &iter
);
399 g_timeout_add(SNAPSHOT_TICK
, (GSourceFunc
)update_snapshot
, NULL
);
403 static void menuApply()
405 GtkTreeSelection
*sel
;
409 if (g_default_theme
) {
410 g_free(g_default_theme
);
411 g_default_theme
= NULL
;
413 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
414 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
415 gtk_tree_model_get(model
, &iter
, COL_THEME_FILE
, &g_default_theme
, -1);
417 copy_file(g_default_theme
, g_path_config
);
420 system("killall -SIGUSR1 tint2");
425 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
427 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
429 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
433 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
435 // single click with the right mouse button?
436 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
437 GtkTreeSelection
*selection
;
439 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
441 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
444 // Get tree path for row that was clicked
445 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
446 gtk_tree_selection_unselect_all(selection
);
447 gtk_tree_selection_select_path(selection
, path
);
448 gtk_tree_path_free(path
);
452 view_popup_menu(treeview
, event
, userdata
);
459 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
461 view_popup_menu(treeview
, NULL
, userdata
);
466 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
472 static void windowSizeAllocated()
474 const gboolean isMaximized
= g_window
->window
&& (gdk_window_get_state(g_window
->window
) & GDK_WINDOW_STATE_MAXIMIZED
);
477 gtk_window_get_size(GTK_WINDOW(g_window
), &g_width
, &g_height
);
481 static void load_theme(GtkWidget
*list
)
486 gboolean found_theme
= FALSE
;
488 dir
= g_dir_open(g_path_dir
, 0, NULL
);
489 if (dir
== NULL
) return;
490 while ((file
= g_dir_read_name(dir
))) {
491 pt1
= strstr(file
, ".tint2rc");
494 name
= g_build_filename (g_path_dir
, file
, NULL
);
495 custom_list_append(name
);
502 // create default theme file
503 name
= g_build_filename(g_get_user_config_dir(), "tint2", "default.tint2rc", NULL
);
504 copy_file(g_path_config
, name
);
505 custom_list_append(name
);
506 if (g_default_theme
) g_free(g_default_theme
);
507 g_default_theme
= strdup(name
);
511 selectTheme(g_default_theme
);
513 g_timeout_add(SNAPSHOT_TICK
, (GSourceFunc
)update_snapshot
, NULL
);
517 void selectTheme(const gchar
*name_theme
)
519 gboolean have_iter
, found_theme
;
523 if (!name_theme
) return;
525 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view
));
526 found_theme
= searchTheme(name_theme
, model
, &iter
);
528 GtkTreePath
*path
= NULL
;
530 path
= gtk_tree_model_get_path(model
, &iter
);
532 have_iter
= gtk_tree_model_get_iter_first(model
, &iter
);
534 path
= gtk_tree_model_get_path(model
, &iter
);
537 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
)), &iter
);
538 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(g_theme_view
), path
, NULL
, FALSE
, 0, 0);
539 gtk_tree_path_free(path
);
544 gboolean
searchTheme(const gchar
*name_theme
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
547 gboolean have_iter
, found
= FALSE
;
549 have_iter
= gtk_tree_model_get_iter_first(model
, iter
);
551 gtk_tree_model_get(model
, iter
, COL_THEME_FILE
, &name
, -1);
552 found
= (strcmp(name
, name_theme
) == 0);
556 have_iter
= gtk_tree_model_iter_next(model
, iter
);
564 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
565 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
566 g_mkdir(g_path_dir
, 0777);
568 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);
577 if (g_default_theme
!= NULL
) {
578 g_free(g_default_theme
);
579 g_default_theme
= NULL
;
583 g_cmd_property
= g_strconcat( "python ", INSTALL_PREFIX
, "/bin/tintwizard.py", (void*)0 );
586 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
587 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
591 if ((fp
= fopen(path
, "r")) != NULL
) {
592 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
593 if (parse_line(line
, &key
, &value
)) {
594 if (strcmp (key
, "default_theme") == 0)
595 g_default_theme
= strdup(value
);
596 else if (strcmp (key
, "cmd_property") == 0) {
597 g_free(g_cmd_property
);
598 g_cmd_property
= strdup(value
);
600 else if (strcmp (key
, "width") == 0)
601 g_width
= atoi(value
);
602 else if (strcmp (key
, "height") == 0)
603 g_height
= atoi(value
);
620 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
621 fp
= fopen(path
, "w");
623 fputs("#---------------------------------------------\n", fp
);
624 fputs("# TINT2CONF CONFIG FILE\n", fp
);
625 if (g_default_theme
!= NULL
) {
626 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
628 if (g_cmd_property
!= NULL
) {
629 fprintf(fp
, "cmd_property = %s\n", g_cmd_property
);
631 fprintf(fp
, "width = %d\n", g_width
);
632 fprintf(fp
, "height = %d\n", g_height
);