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>
32 #include "theme_view.h"
35 #define LONG_VERSION_STRING "0.7"
38 // default config file and directory
39 char *g_path_config
= 0;
41 char *g_default_theme
= 0;
46 GtkWidget
*g_theme_view
;
47 GtkCellRenderer
*g_renderer
;
49 static GtkUIManager
*globalUIManager
= NULL
;
51 static void menuAddWidget (GtkUIManager
*, GtkWidget
*, GtkContainer
*);
54 static void menuAdd (GtkWindow
* parent
);
55 //static void menuSaveAs (GtkWindow *parent);
56 static void menuDelete (void);
57 static void menuProperties (void);
58 static void menuRename (void);
59 static void menuQuit (void);
60 static void menuRefresh (void);
61 static void menuRefreshAll (void);
62 static void menuApply (void);
63 static void menuAbout(GtkWindow
* parent
);
65 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
);
66 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
);
67 static void viewRowActivated( GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
);
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'/>"
88 " <menuitem action='ThemeRename'/>"
90 " <menuitem action='ThemeQuit'/>"
92 " <menu action='ViewMenu'>"
93 " <menuitem action='ViewRefresh'/>"
94 " <menuitem action='ViewRefreshAll'/>"
96 " <menu action='HelpMenu'>"
97 " <menuitem action='HelpAbout'/>"
100 " <toolbar name='ToolBar'>"
101 " <toolitem action='ViewRefreshAll'/>"
103 " <toolitem action='ThemeProperties'/>"
104 " <toolitem action='ViewApply'/>"
106 " <popup name='ThemePopup'>"
107 " <menuitem action='ThemeProperties'/>"
108 " <menuitem action='ThemeRename'/>"
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 {"ThemeRename", NULL
, "_Rename...", NULL
, "Rename theme", G_CALLBACK (menuRename
)},
123 {"ThemeQuit", GTK_STOCK_QUIT
, "_Quit", "<control>Q", "Quit", G_CALLBACK (menuQuit
)},
124 {"ViewMenu", NULL
, "View", NULL
, NULL
, NULL
},
125 {"ViewRefresh", GTK_STOCK_REFRESH
, "Refresh", NULL
, "Refresh", G_CALLBACK (menuRefresh
)},
126 {"ViewRefreshAll", GTK_STOCK_REFRESH
, "Refresh all", NULL
, "Refresh all", G_CALLBACK (menuRefreshAll
)},
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
);
145 // define main layout : container, menubar, toolbar
146 g_window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
147 gtk_window_set_title(GTK_WINDOW(g_window
), _("Panel theming"));
148 gtk_window_resize(GTK_WINDOW(g_window
), g_width
, g_height
);
149 g_signal_connect (G_OBJECT (g_window
), "destroy", G_CALLBACK (menuQuit
), NULL
);
150 vBox
= gtk_vbox_new (FALSE
, 0);
151 gtk_container_add (GTK_CONTAINER(g_window
), vBox
);
153 actionGroup
= gtk_action_group_new ("menuActionGroup");
154 gtk_action_group_add_actions (actionGroup
, entries
, G_N_ELEMENTS (entries
), NULL
);
155 globalUIManager
= gtk_ui_manager_new();
156 gtk_ui_manager_insert_action_group (globalUIManager
, actionGroup
, 0);
157 gtk_ui_manager_add_ui_from_string (globalUIManager
, global_ui
, -1, NULL
);
158 g_signal_connect(globalUIManager
, "add_widget", G_CALLBACK (menuAddWidget
), vBox
);
159 gtk_ui_manager_ensure_update(globalUIManager
);
160 scrollbar
= gtk_scrolled_window_new(NULL
, NULL
);
161 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
162 gtk_box_pack_start(GTK_BOX(vBox
), scrollbar
, TRUE
, TRUE
, 0);
165 g_theme_view
= create_view_and_model();
166 gtk_container_add(GTK_CONTAINER(scrollbar
), g_theme_view
);
167 gtk_widget_show(g_theme_view
);
168 g_signal_connect(g_theme_view
, "button-press-event", (GCallback
)view_onButtonPressed
, NULL
);
169 g_signal_connect(g_theme_view
, "popup-menu", (GCallback
)view_onPopupMenu
, NULL
);
170 g_signal_connect(g_theme_view
, "row-activated", G_CALLBACK(viewRowActivated
), NULL
);
173 load_theme(g_theme_view
);
175 // rig up idle/thread routines
176 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::load_cache_images), true);
177 //Glib::Thread::create(sigc::mem_fun(window.view, &Thumbview::create_cache_images), true);
179 gtk_widget_show_all(g_window
);
185 static void menuAddWidget (GtkUIManager
* p_uiManager
, GtkWidget
* p_widget
, GtkContainer
* p_box
)
187 gtk_box_pack_start(GTK_BOX(p_box
), p_widget
, FALSE
, FALSE
, 0);
188 gtk_widget_show(p_widget
);
192 static void menuAbout(GtkWindow
* parent
)
194 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
};
196 gtk_show_about_dialog( parent
, "name", g_get_application_name( ),
197 "comments", _("Theming tool for tint2 panel"),
198 "version", LONG_VERSION_STRING
,
199 "copyright", _("Copyright 2009 tint2 team\nTint2 License GNU GPL version 2\nTintwizard License GNU GPL version 3"),
200 "logo-icon-name", NULL
, "authors", authors
,
201 /* Translators: translate "translator-credits" as
202 your name to have it appear in the credits in the "About"
204 "translator-credits", _("translator-credits"),
209 static void menuAdd (GtkWindow
*parent
)
212 GtkFileChooser
*chooser
;
213 GtkFileFilter
*filter
;
215 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
);
216 chooser
= GTK_FILE_CHOOSER(dialog
);
218 gtk_file_chooser_set_current_folder(chooser
, g_get_home_dir());
219 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
221 filter
= gtk_file_filter_new();
222 gtk_file_filter_set_name(filter
, _("Tint2 theme files"));
223 gtk_file_filter_add_pattern(filter
, "*.tint2rc");
224 gtk_file_chooser_add_filter(chooser
, filter
);
226 if (gtk_dialog_run (GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
227 GSList
*l
, *list
= gtk_file_chooser_get_filenames(chooser
);
229 gchar
*file
, *pt1
, *name
, *path
;
230 for (l
= list
; l
; l
= l
->next
) {
231 file
= (char *)l
->data
;
232 pt1
= strrchr (file
, '/');
237 path
= g_build_filename (g_get_user_config_dir(), "tint2", name
, NULL
);
238 copy_file(file
, path
);
240 pt1
= strstr(name
, ".tint2rc");
242 file
= strndup(name
, pt1
-name
);
243 add_to_list(g_theme_view
, file
);
251 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
254 gtk_widget_destroy (dialog
);
258 static void menuSaveAs (GtkWindow *parent)
261 GtkFileChooser *chooser;
263 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);
264 chooser = GTK_FILE_CHOOSER(dialog);
266 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
267 gtk_file_chooser_set_current_folder (chooser, g_get_home_dir());
268 gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
270 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
271 char *filename = gtk_file_chooser_get_filename(chooser);
272 printf("fichier %s\n", filename);
273 //save_to_file (filename);
276 gtk_widget_destroy (dialog);
280 static void menuDelete (void)
282 GtkTreeSelection
*sel
;
285 char *value
, *name1
, *name2
;
287 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
288 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
289 gtk_tree_model_get(model
, &iter
, COL_TEXT
, &value
, -1);
290 gtk_tree_selection_unselect_all(sel
);
291 // remove from the gui
292 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
294 name1
= g_build_filename (g_get_user_config_dir(), "tint2", value
, NULL
);
295 name2
= g_strdup_printf("%s.tint2rc", name1
);
305 static void menuProperties (void)
307 GtkTreeSelection
*sel
;
310 char *value
, *name1
, *name2
, *cmd
;
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_TEXT
, &value
, -1);
316 name1
= g_build_filename ("\'", g_get_user_config_dir(), "tint2", value
, NULL
);
317 name2
= g_strdup_printf("%s.tint2rc\'", name1
);
319 cmd
= g_strdup_printf("tintwizard.py %s", name2
);
330 static void menuRename (void)
332 printf("menuRename\n");
337 static void menuQuit (void)
341 g_object_unref(g_store
);
346 static void menuRefresh (void)
348 printf("menuRefresh\n");
352 static void menuRefreshAll (void)
354 printf("menuRefreshAll\n");
358 static void menuApply (void)
360 GtkTreeSelection
*sel
;
363 char *value
, *name1
, *name2
;
365 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view
));
366 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel
), &model
, &iter
)) {
367 gtk_tree_model_get(model
, &iter
, COL_TEXT
, &value
, -1);
368 name1
= g_build_filename (g_get_user_config_dir(), "tint2", value
, NULL
);
369 name2
= g_strdup_printf("%s.tint2rc", name1
);
372 copy_file(name2
, g_path_config
);
378 system("killall -SIGUSR1 tint2");
383 static void view_popup_menu(GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
385 GtkWidget
*w
= gtk_ui_manager_get_widget(globalUIManager
, "/ThemePopup");
387 gtk_menu_popup(GTK_MENU(w
), NULL
, NULL
, NULL
, NULL
, (event
!= NULL
) ? event
->button
: 0, gdk_event_get_time((GdkEvent
*)event
));
391 static gboolean
view_onButtonPressed (GtkWidget
*treeview
, GdkEventButton
*event
, gpointer userdata
)
393 // single click with the right mouse button?
394 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
395 GtkTreeSelection
*selection
;
397 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
399 if (gtk_tree_selection_count_selected_rows(selection
) <= 1) {
402 // Get tree path for row that was clicked
403 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview
), (gint
) event
->x
, (gint
) event
->y
, &path
, NULL
, NULL
, NULL
)) {
404 gtk_tree_selection_unselect_all(selection
);
405 gtk_tree_selection_select_path(selection
, path
);
406 gtk_tree_path_free(path
);
410 view_popup_menu(treeview
, event
, userdata
);
417 static gboolean
view_onPopupMenu (GtkWidget
*treeview
, gpointer userdata
)
419 view_popup_menu(treeview
, NULL
, userdata
);
424 static void viewRowActivated(GtkTreeView
*tree_view
, GtkTreePath
*path
, GtkTreeViewColumn
*column
, gpointer user_data
)
430 static void load_theme(GtkWidget
*list
)
433 gchar
*file
, *pt1
, *name
;
435 dir
= g_dir_open(g_path_dir
, 0, NULL
);
436 while ((file
= g_dir_read_name(dir
))) {
437 pt1
= strstr(file
, ".tint2rc");
439 name
= strndup(file
, pt1
-file
);
440 add_to_list(list
, name
);
446 // search default theme
449 if (g_default_theme
) {
450 printf("defaultTheme %s\n", g_default_theme
);
451 //gtk_tree_selection_select_iter(GtkTreeSelection *selection, GtkTreeIter *iter);
456 // theme file management
462 g_free(g_default_theme
);
466 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
467 if (g_file_test (path
, G_FILE_TEST_EXISTS
)) {
471 if ((fp
= fopen(path
, "r")) != NULL
) {
472 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
473 if (parse_line(line
, &key
, &value
)) {
474 if (strcmp (key
, "default_theme") == 0)
475 g_default_theme
= strdup (value
);
476 else if (strcmp (key
, "width") == 0)
477 g_width
= atoi(value
);
478 else if (strcmp (key
, "height") == 0)
479 g_height
= atoi(value
);
496 int x
, y
, width
, height
, depth
;
497 int top
, bottom
, left
, right
;
500 gtk_window_get_size(GTK_WINDOW(g_window
), &width
, &height
);
501 printf("write_config %d, %d\n", width
, height
);
503 // GTK incapacity to return (width, height) of the window is really annoying
504 gdk_window_get_geometry(GTK_WIDGET(g_window
)->window
, &x
, &y
, &width
, &height
, &depth
);
505 printf(" write_config %d, %d, %d, %d\n", x
, y
, width
, height
);
507 //Window xid = GDK_WINDOW_XWINDOW(GTK_WIDGET(g_window)->window);
508 //gdk_window_get_frame_extentsGTK_WINDOW(g_window), &rect);
509 //printf(" write_config %d, %d\n", rec.width, rec.height);
512 path = gtk_tree_path_new_from_indices( playlist_pos, -1 );
513 sel = gtk_tree_view_get_selection( (GtkTreeView*)list_view );
514 gtk_tree_selection_select_path( sel, path );
515 gtk_tree_path_free( path );
517 path
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2confrc", NULL
);
518 fp
= fopen(path
, "w");
520 fputs("#---------------------------------------------\n", fp
);
521 fputs("# TINT2CONF CONFIG FILE\n", fp
);
522 fprintf(fp
, "default_theme = %s\n", g_default_theme
);
523 fprintf(fp
, "width = %d\n", g_width
);
524 fprintf(fp
, "height = %d\n", g_height
);
534 g_path_dir
= g_build_filename (g_get_user_config_dir(), "tint2", NULL
);
535 if (!g_file_test (g_path_dir
, G_FILE_TEST_IS_DIR
))
536 g_mkdir(g_path_dir
, 0777);
538 g_path_config
= g_build_filename (g_get_user_config_dir(), "tint2", "tint2rc", NULL
);