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
= 0;
40 char *g_default_theme
= 0;
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 menuRename (void);
58 static void menuQuit (void);
59 static void menuRefresh (void);
60 static void menuRefreshAll (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'/>"
87 " <menuitem action='ThemeRename'/>"
89 " <menuitem action='ThemeQuit'/>"
91 " <menu action='ViewMenu'>"
92 " <menuitem action='ViewRefresh'/>"
93 " <menuitem action='ViewRefreshAll'/>"
95 " <menu action='HelpMenu'>"
96 " <menuitem action='HelpAbout'/>"
99 " <toolbar name='ToolBar'>"
100 " <toolitem action='ViewRefreshAll'/>"
102 " <toolitem action='ThemeProperties'/>"
103 " <toolitem action='ViewApply'/>"
105 " <popup name='ThemePopup'>"
106 " <menuitem action='ThemeProperties'/>"
107 " <menuitem action='ThemeRename'/>"
109 " <menuitem action='ThemeDelete'/>"
114 // define menubar and toolbar action
115 static GtkActionEntry entries
[] = {
116 {"ThemeMenu", NULL
, "Theme", NULL
, NULL
, NULL
},
117 {"ThemeAdd", GTK_STOCK_ADD
, "_Add...", "<Control>N", "Add theme", G_CALLBACK (menuAdd
)},
118 // {"ThemeSaveAs", GTK_STOCK_SAVE_AS, "_Save as...", NULL, "Save theme as", G_CALLBACK (menuSaveAs)},
119 {"ThemeDelete", GTK_STOCK_DELETE
, "_Delete", NULL
, "Delete theme", G_CALLBACK (menuDelete
)},
120 {"ThemeProperties", GTK_STOCK_PROPERTIES
, "_Properties...", NULL
, "Show properties", G_CALLBACK (menuProperties
)},
121 {"ThemeRename", NULL
, "_Rename...", NULL
, "Rename theme", G_CALLBACK (menuRename
)},
122 {"ThemeQuit", GTK_STOCK_QUIT
, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit
)},
123 {"ViewMenu", NULL
, "View", NULL
, NULL
, NULL
},
124 {"ViewRefresh", GTK_STOCK_REFRESH
, "Refresh", NULL
, "Refresh", G_CALLBACK (menuRefresh
)},
125 {"ViewRefreshAll", GTK_STOCK_REFRESH
, "Refresh all", NULL
, "Refresh all", G_CALLBACK (menuRefreshAll
)},
126 {"ViewApply", GTK_STOCK_APPLY
, "Apply", NULL
, "Apply theme", G_CALLBACK (menuApply
)},
127 {"HelpMenu", NULL
, "Help", NULL
, NULL
, NULL
},
128 {"HelpAbout", GTK_STOCK_ABOUT
, "_About", "<Control>A", "About", G_CALLBACK (menuAbout
)}
132 int main (int argc
, char ** argv
)
134 GtkWidget
*vBox
= NULL
, *scrollbar
= NULL
;
135 GtkActionGroup
*actionGroup
;
137 gtk_init (&argc
, &argv
);
138 g_thread_init( NULL
);
144 // define main layout : container, menubar, toolbar
145 g_window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
146 gtk_window_set_title(GTK_WINDOW(g_window
), _("Panel theming"));
147 gtk_window_resize(GTK_WINDOW(g_window
), g_width
, g_height
);
148 g_signal_connect (G_OBJECT (g_window
), "destroy", G_CALLBACK (menuQuit
), 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_and_model();
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(GtkWindow
* parent
)
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( parent
, "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 (GtkWindow
*parent
)
211 GtkFileChooser
*chooser
;
212 GtkFileFilter
*filter
;
214 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
);
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 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
228 gchar
*file
, *pt1
, *name
, *path
;
229 for (l
= list
; l
; l
= l
->next
) {
230 file
= (char *)l
->data
;
231 pt1
= strrchr (file
, '/');
236 path
= g_build_filename (g_get_user_config_dir(), "tint2", name
, NULL
);
237 copy_file(file
, path
);
239 pt1
= strstr(name
, ".tint2rc");
241 file
= strndup(name
, pt1
-name
);
242 add_to_list(g_theme_view
, file
);
250 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
253 gtk_widget_destroy (dialog
);
257 static void menuSaveAs (GtkWindow *parent)
260 GtkFileChooser *chooser;
262 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);
263 chooser = GTK_FILE_CHOOSER(dialog);
265 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
266 gtk_file_chooser_set_current_folder (chooser, g_get_home_dir());
267 gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
269 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
270 char *filename = gtk_file_chooser_get_filename(chooser);
271 printf("fichier %s\n", filename);
272 //save_to_file (filename);
275 gtk_widget_destroy (dialog);
279 static void menuDelete (void)
281 GtkTreeSelection
*sel
;
284 char *value
, *name1
, *name2
;
286 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
287 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
288 gtk_tree_model_get(model
, &iter
, COL_TEXT
, &value
, -1);
289 gtk_tree_selection_unselect_all(sel
);
290 // remove from the gui
291 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
293 name1
= g_build_filename (g_get_user_config_dir(), "tint2", value
, NULL
);
294 name2
= g_strdup_printf("%s.tint2rc", name1
);
304 static void menuProperties (void)
306 GtkTreeSelection
*sel
;
309 char *value
, *name1
, *name2
, *cmd
;
311 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
312 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
313 gtk_tree_model_get(model
, &iter
, COL_TEXT
, &value
, -1);
315 name1
= g_build_filename ("\'", g_get_user_config_dir(), "tint2", value
, NULL
);
316 name2
= g_strdup_printf("%s.tint2rc\'", name1
);
318 cmd
= g_strdup_printf("tintwizard.py %s", name2
);
329 static void menuRename (void)
331 printf("menuRename\n");
336 static void menuQuit (void)
340 g_object_unref(g_store
);
345 static void menuRefresh (void)
347 printf("menuRefresh\n");
351 static void menuRefreshAll (void)
353 printf("menuRefreshAll\n");
357 static void menuApply (void)
359 GtkTreeSelection
*sel
;
362 char *value
, *name1
, *name2
;
364 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
365 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
366 gtk_tree_model_get(model
, &iter
, COL_TEXT
, &value
, -1);
367 name1
= g_build_filename (g_get_user_config_dir(), "tint2", value
, NULL
);
368 name2
= g_strdup_printf("%s.tint2rc", name1
);
371 copy_file(name2
, g_path_config
);
377 system("killall -SIGUSR1 tint2");
382 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
384 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
386 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
390 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
392 // single click with the right mouse button?
393 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
394 GtkTreeSelection
*selection
;
396 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
398 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
401 // Get tree path for row that was clicked
402 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
403 gtk_tree_selection_unselect_all(selection
);
404 gtk_tree_selection_select_path(selection
, path
);
405 gtk_tree_path_free(path
);
409 view_popup_menu(treeview
, event
, userdata
);
416 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
418 view_popup_menu(treeview
, NULL
, userdata
);
423 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
429 static void load_theme(GtkWidget
*list
)
432 gchar
*file
, *pt1
, *name
;
434 dir
= g_dir_open(g_path_dir
, 0, NULL
);
435 while ((file
= g_dir_read_name(dir
))) {
436 pt1
= strstr(file
, ".tint2rc");
438 name
= strndup(file
, pt1
-file
);
439 add_to_list(list
, name
);
445 // search default theme
448 if (g_default_theme
) {
449 printf("defaultTheme %s\n", g_default_theme
);
450 //gtk_tree_selection_select_iter(GtkTreeSelection *selection, GtkTreeIter *iter);
455 // theme file management
461 g_free(g_default_theme
);
465 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
466 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
470 if ((fp
= fopen(path
, "r")) != NULL
) {
471 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
472 if (parse_line(line
, &key
, &value
)) {
473 if (strcmp (key
, "default_theme") == 0)
474 g_default_theme
= strdup (value
);
475 else if (strcmp (key
, "width") == 0)
476 g_width
= atoi(value
);
477 else if (strcmp (key
, "height") == 0)
478 g_height
= atoi(value
);
495 int x
, y
, width
, height
, depth
;
496 int top
, bottom
, left
, right
;
499 gtk_window_get_size(GTK_WINDOW(g_window
), &width
, &height
);
500 printf("write_config %d, %d\n", width
, height
);
502 // GTK incapacity to return (width, height) of the window is really annoying
503 gdk_window_get_geometry(GTK_WIDGET(g_window
)->window
, &x
, &y
, &width
, &height
, &depth
);
504 printf(" write_config %d, %d, %d, %d\n", x
, y
, width
, height
);
506 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
507 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
508 //printf(" write_config %d, %d\n", rec.width, rec.height);
511 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
512 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
513 gtk_tree_selection_select_path( sel, path );
514 gtk_tree_path_free( path );
516 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
517 fp
= fopen(path
, "w");
519 fputs("#---------------------------------------------\n", fp
);
520 fputs("# TINT2CONF CONFIG FILE\n", fp
);
521 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
522 fprintf(fp
, "width = %d\n", g_width
);
523 fprintf(fp
, "height = %d\n", g_height
);
533 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
534 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
535 g_mkdir(g_path_dir
, 0777);
537 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);