2009-07-15 22:16:39 +00:00
/**************************************************************************
*
* Tint2conf
*
2010-11-02 11:40:50 +00:00
* Copyright ( C ) 2009 Thierry lorthiois ( lorthiois @ bbsoft . fr ) from Omega distribution
2009-07-15 22:16:39 +00:00
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-01-28 15:31:20 +00:00
# include <time.h>
2010-06-12 21:21:06 +00:00
# ifdef HAVE_VERSION_H
2015-01-20 02:23:28 +00:00
# include "version.h"
2010-06-09 15:31:18 +00:00
# endif
2015-01-28 15:31:20 +00:00
2010-06-14 15:08:51 +00:00
# include "main.h"
2009-10-25 16:36:40 +00:00
# include "common.h"
2009-11-16 09:27:44 +00:00
# include "theme_view.h"
2010-06-12 22:17:45 +00:00
# include "properties.h"
2011-06-06 09:16:51 +00:00
# include "properties_rw.h"
2009-07-15 22:16:39 +00:00
2015-01-28 15:31:20 +00:00
// Default path to the tint2 config file
2010-03-26 23:16:25 +00:00
char * g_path_config = NULL ;
2015-01-28 15:31:20 +00:00
// Default path to the tint2 config directory
2010-03-26 23:16:25 +00:00
char * g_path_dir = NULL ;
char * g_default_theme = NULL ;
2010-03-28 22:50:26 +00:00
int g_width , g_height ;
2009-10-24 23:23:01 +00:00
2009-11-03 15:32:55 +00:00
GtkWidget * g_window ;
2009-10-24 23:23:01 +00:00
2009-10-26 20:17:05 +00:00
static GtkUIManager * globalUIManager = NULL ;
2009-07-15 22:16:39 +00:00
2009-07-28 18:29:36 +00:00
static void menuAddWidget ( GtkUIManager * , GtkWidget * , GtkContainer * ) ;
// action on menus
2010-04-02 20:02:41 +00:00
static void menuAdd ( ) ;
static void menuSaveAs ( ) ;
static void menuDelete ( ) ;
static void menuProperties ( ) ;
static void menuQuit ( ) ;
static void menuRefresh ( ) ;
static void menuRefreshAll ( ) ;
2015-01-28 15:31:20 +00:00
void menuApply ( ) ;
2010-04-02 20:02:41 +00:00
static void menuAbout ( ) ;
2009-07-28 18:29:36 +00:00
2009-10-26 20:17:05 +00:00
static gboolean view_onPopupMenu ( GtkWidget * treeview , gpointer userdata ) ;
static gboolean view_onButtonPressed ( GtkWidget * treeview , GdkEventButton * event , gpointer userdata ) ;
2010-04-02 20:02:41 +00:00
static void windowSizeAllocated ( ) ;
static void viewRowActivated ( GtkTreeView * tree_view , GtkTreePath * path , GtkTreeViewColumn * column , gpointer user_data ) ;
2009-07-28 18:29:36 +00:00
2009-10-24 23:23:01 +00:00
2009-11-16 09:27:44 +00:00
// theme files
2010-04-03 15:56:34 +00:00
static void selectTheme ( const gchar * name ) ;
static gboolean searchTheme ( const gchar * name_theme , GtkTreeModel * model , GtkTreeIter * iter ) ;
2009-11-16 09:27:44 +00:00
static void load_theme ( ) ;
2010-04-03 22:52:52 +00:00
static void initTheme ( ) ;
2009-11-16 09:27:44 +00:00
static void read_config ( ) ;
static void write_config ( ) ;
2009-07-28 18:29:36 +00:00
2009-10-26 20:17:05 +00:00
// define menubar, toolbar and popup
static const char * global_ui =
2009-09-20 20:48:00 +00:00
" <ui> "
" <menubar name='MenuBar'> "
" <menu action='ThemeMenu'> "
" <menuitem action='ThemeAdd'/> "
2010-03-26 23:16:25 +00:00
" <menuitem action='ThemeSaveAs'/> "
2009-10-24 23:23:01 +00:00
" <separator/> "
" <menuitem action='ThemeDelete'/> "
2011-06-06 09:16:51 +00:00
" <separator/> "
" <menuitem action='ThemeProperties'/> "
2011-01-06 23:28:16 +00:00
" <separator/> "
2009-09-20 20:48:00 +00:00
" <menuitem action='ThemeQuit'/> "
" </menu> "
2010-03-26 23:16:25 +00:00
" <menu action='EditMenu'> "
" <menuitem action='EditRefresh'/> "
" <menuitem action='EditRefreshAll'/> "
2011-06-06 09:16:51 +00:00
" <separator/> "
2009-09-20 20:48:00 +00:00
" </menu> "
" <menu action='HelpMenu'> "
" <menuitem action='HelpAbout'/> "
" </menu> "
" </menubar> "
" <toolbar name='ToolBar'> "
2011-06-06 09:16:51 +00:00
" <toolitem action='ThemeProperties'/> "
2010-04-02 20:02:41 +00:00
" <toolitem action='ViewApply'/> "
2009-09-20 20:48:00 +00:00
" </toolbar> "
2009-10-26 20:17:05 +00:00
" <popup name='ThemePopup'> "
2011-06-06 09:16:51 +00:00
" <menuitem action='ThemeProperties'/> "
2010-06-12 22:17:45 +00:00
" <menuitem action='EditRefresh'/> "
2010-04-02 20:02:41 +00:00
" <menuitem action='ViewApply'/> "
2009-10-26 20:17:05 +00:00
" <separator/> "
" <menuitem action='ThemeDelete'/> "
" </popup> "
2009-09-20 20:48:00 +00:00
" </ui> " ;
2009-07-15 22:16:39 +00:00
2009-10-26 20:17:05 +00:00
2009-07-28 18:29:36 +00:00
// define menubar and toolbar action
static GtkActionEntry entries [ ] = {
2010-06-14 15:08:51 +00:00
{ " ThemeMenu " , NULL , _ ( " Theme " ) , NULL , NULL , NULL } ,
{ " ThemeAdd " , GTK_STOCK_ADD , _ ( " _Add... " ) , " <Control>N " , _ ( " Add theme " ) , G_CALLBACK ( menuAdd ) } ,
{ " ThemeSaveAs " , GTK_STOCK_SAVE_AS , _ ( " _Save as... " ) , NULL , _ ( " Save theme as " ) , G_CALLBACK ( menuSaveAs ) } ,
{ " ThemeDelete " , GTK_STOCK_DELETE , _ ( " _Delete " ) , NULL , _ ( " Delete theme " ) , G_CALLBACK ( menuDelete ) } ,
{ " ThemeProperties " , GTK_STOCK_PROPERTIES , _ ( " _Properties... " ) , NULL , _ ( " Show properties " ) , G_CALLBACK ( menuProperties ) } ,
{ " ThemeQuit " , GTK_STOCK_QUIT , _ ( " _Quit " ) , " <control>Q " , _ ( " Quit " ) , G_CALLBACK ( menuQuit ) } ,
2010-03-26 23:16:25 +00:00
{ " EditMenu " , NULL , " Edit " , NULL , NULL , NULL } ,
2010-06-14 15:08:51 +00:00
{ " EditRefresh " , GTK_STOCK_REFRESH , _ ( " Refresh " ) , NULL , _ ( " Refresh " ) , G_CALLBACK ( menuRefresh ) } ,
{ " EditRefreshAll " , GTK_STOCK_REFRESH , _ ( " Refresh all " ) , NULL , _ ( " Refresh all " ) , G_CALLBACK ( menuRefreshAll ) } ,
2010-03-26 23:16:25 +00:00
// {"EditPreferences", GTK_STOCK_PREFERENCES, "Preferences", NULL, "Preferences", G_CALLBACK (menuPreferences)},
2010-06-14 15:08:51 +00:00
{ " ViewApply " , GTK_STOCK_APPLY , _ ( " Apply " ) , NULL , _ ( " Apply theme " ) , G_CALLBACK ( menuApply ) } ,
{ " HelpMenu " , NULL , _ ( " Help " ) , NULL , NULL , NULL } ,
{ " HelpAbout " , GTK_STOCK_ABOUT , _ ( " _About " ) , " <Control>A " , _ ( " About " ) , G_CALLBACK ( menuAbout ) }
2009-07-28 18:29:36 +00:00
} ;
2009-07-15 22:16:39 +00:00
2009-07-28 18:29:36 +00:00
int main ( int argc , char * * argv )
2009-07-15 22:16:39 +00:00
{
2009-11-10 17:07:51 +00:00
GtkWidget * vBox = NULL , * scrollbar = NULL ;
2009-07-28 18:29:36 +00:00
GtkActionGroup * actionGroup ;
gtk_init ( & argc , & argv ) ;
g_thread_init ( NULL ) ;
2009-11-03 15:32:55 +00:00
read_config ( ) ;
2010-04-03 22:52:52 +00:00
initTheme ( ) ;
2010-11-03 14:57:08 +00:00
g_set_application_name ( _ ( " tint2conf " ) ) ;
gtk_window_set_default_icon_name ( " taskbar " ) ;
2011-06-06 09:16:51 +00:00
// config file use '.' as decimal separator
setlocale ( LC_NUMERIC , " POSIX " ) ;
2009-07-28 18:29:36 +00:00
2009-10-24 23:23:01 +00:00
// define main layout : container, menubar, toolbar
2009-11-03 15:32:55 +00:00
g_window = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ;
gtk_window_set_title ( GTK_WINDOW ( g_window ) , _ ( " Panel theming " ) ) ;
gtk_window_resize ( GTK_WINDOW ( g_window ) , g_width , g_height ) ;
2010-04-02 20:02:41 +00:00
g_signal_connect ( G_OBJECT ( g_window ) , " destroy " , G_CALLBACK ( menuQuit ) , NULL ) ;
g_signal_connect ( g_window , " size-allocate " , G_CALLBACK ( windowSizeAllocated ) , NULL ) ;
2009-07-28 18:29:36 +00:00
vBox = gtk_vbox_new ( FALSE , 0 ) ;
2010-06-14 15:08:51 +00:00
gtk_container_add ( GTK_CONTAINER ( g_window ) , vBox ) ;
2009-07-28 18:29:36 +00:00
actionGroup = gtk_action_group_new ( " menuActionGroup " ) ;
2010-06-14 15:08:51 +00:00
gtk_action_group_add_actions ( actionGroup , entries , G_N_ELEMENTS ( entries ) , NULL ) ;
2009-10-26 20:17:05 +00:00
globalUIManager = gtk_ui_manager_new ( ) ;
2010-06-14 15:08:51 +00:00
gtk_ui_manager_insert_action_group ( globalUIManager , actionGroup , 0 ) ;
2009-10-26 20:17:05 +00:00
gtk_ui_manager_add_ui_from_string ( globalUIManager , global_ui , - 1 , NULL ) ;
g_signal_connect ( globalUIManager , " add_widget " , G_CALLBACK ( menuAddWidget ) , vBox ) ;
gtk_ui_manager_ensure_update ( globalUIManager ) ;
2009-11-10 17:07:51 +00:00
scrollbar = gtk_scrolled_window_new ( NULL , NULL ) ;
gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW ( scrollbar ) , GTK_POLICY_AUTOMATIC , GTK_POLICY_AUTOMATIC ) ;
gtk_box_pack_start ( GTK_BOX ( vBox ) , scrollbar , TRUE , TRUE , 0 ) ;
2009-07-28 18:29:36 +00:00
2009-11-16 09:27:44 +00:00
// define theme view
2010-03-26 23:16:25 +00:00
g_theme_view = create_view ( ) ;
2009-11-10 17:07:51 +00:00
gtk_container_add ( GTK_CONTAINER ( scrollbar ) , g_theme_view ) ;
2010-06-14 15:08:51 +00:00
gtk_widget_show ( g_theme_view ) ;
2009-10-26 20:17:05 +00:00
g_signal_connect ( g_theme_view , " button-press-event " , ( GCallback ) view_onButtonPressed , NULL ) ;
g_signal_connect ( g_theme_view , " popup-menu " , ( GCallback ) view_onPopupMenu , NULL ) ;
g_signal_connect ( g_theme_view , " row-activated " , G_CALLBACK ( viewRowActivated ) , NULL ) ;
2009-07-28 18:29:36 +00:00
2010-06-14 15:08:51 +00:00
// load themes
2009-11-16 09:27:44 +00:00
load_theme ( g_theme_view ) ;
2009-07-28 18:29:36 +00:00
2009-11-03 15:32:55 +00:00
gtk_widget_show_all ( g_window ) ;
2009-07-28 18:29:36 +00:00
gtk_main ( ) ;
return 0 ;
}
static void menuAddWidget ( GtkUIManager * p_uiManager , GtkWidget * p_widget , GtkContainer * p_box )
{
2010-06-14 15:08:51 +00:00
gtk_box_pack_start ( GTK_BOX ( p_box ) , p_widget , FALSE , FALSE , 0 ) ;
gtk_widget_show ( p_widget ) ;
2009-07-28 18:29:36 +00:00
}
2010-04-02 20:02:41 +00:00
static void menuAbout ( )
2009-07-28 18:29:36 +00:00
{
2009-11-05 19:48:26 +00:00
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 } ;
2009-07-15 22:16:39 +00:00
2010-06-11 15:37:53 +00:00
gtk_show_about_dialog ( GTK_WINDOW ( g_window ) , " name " , g_get_application_name ( ) ,
2009-07-28 18:29:36 +00:00
" comments " , _ ( " Theming tool for tint2 panel " ) ,
2010-01-15 20:09:35 +00:00
" version " , VERSION_STRING ,
2009-11-03 15:32:55 +00:00
" copyright " , _ ( " Copyright 2009 tint2 team \n Tint2 License GNU GPL version 2 \n Tintwizard License GNU GPL version 3 " ) ,
2010-11-03 14:57:08 +00:00
" logo-icon-name " , " taskbar " , " authors " , authors ,
2009-07-15 22:16:39 +00:00
/* Translators: translate "translator-credits" as
your name to have it appear in the credits in the " About "
dialog */
2009-07-28 18:29:36 +00:00
" translator-credits " , _ ( " translator-credits " ) ,
2010-04-02 20:02:41 +00:00
NULL ) ;
2009-07-15 22:16:39 +00:00
}
2010-04-02 20:02:41 +00:00
static void menuAdd ( )
2009-07-15 22:16:39 +00:00
{
2009-07-28 18:29:36 +00:00
GtkWidget * dialog ;
GtkFileChooser * chooser ;
GtkFileFilter * filter ;
2010-06-11 15:37:53 +00:00
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 ) ;
2009-07-28 18:29:36 +00:00
chooser = GTK_FILE_CHOOSER ( dialog ) ;
2015-01-28 15:31:20 +00:00
gchar * config_dir ;
config_dir = g_build_filename ( g_get_home_dir ( ) , " .config " , " tint2 " , NULL ) ;
gtk_file_chooser_set_current_folder ( chooser , config_dir ) ;
g_free ( config_dir ) ;
2009-07-28 18:29:36 +00:00
gtk_file_chooser_set_select_multiple ( chooser , TRUE ) ;
filter = gtk_file_filter_new ( ) ;
gtk_file_filter_set_name ( filter , _ ( " Tint2 theme files " ) ) ;
2015-01-28 15:31:20 +00:00
gtk_file_filter_add_pattern ( filter , " *tint2rc " ) ;
2009-07-28 18:29:36 +00:00
gtk_file_chooser_add_filter ( chooser , filter ) ;
2010-04-03 15:56:34 +00:00
if ( gtk_dialog_run ( GTK_DIALOG ( dialog ) ) ! = GTK_RESPONSE_ACCEPT ) {
gtk_widget_destroy ( dialog ) ;
return ;
}
GtkTreeIter iter ;
GtkTreeModel * model = gtk_tree_view_get_model ( GTK_TREE_VIEW ( g_theme_view ) ) ;
GSList * l , * list = gtk_file_chooser_get_filenames ( chooser ) ;
gchar * file , * pt1 , * name , * path , * name_first = NULL ;
for ( l = list ; l ; l = l - > next ) {
file = ( char * ) l - > data ;
pt1 = strrchr ( file , ' / ' ) ;
if ( pt1 = = NULL ) continue ;
pt1 + + ;
if ( * pt1 = = 0 ) continue ;
name = g_strdup ( pt1 ) ;
path = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , name , NULL ) ;
// check existing
if ( searchTheme ( path , model , & iter ) ) {
gchar * message ;
message = g_strdup_printf ( _ ( " Couldn't add duplicate theme \n \' %s \' . " ) , pt1 ) ;
2010-06-11 15:37:53 +00:00
GtkWidget * w = gtk_message_dialog_new ( GTK_WINDOW ( g_window ) , 0 , GTK_MESSAGE_ERROR , GTK_BUTTONS_CLOSE , message , NULL ) ;
2010-04-03 15:56:34 +00:00
g_signal_connect_swapped ( w , " response " , G_CALLBACK ( gtk_widget_destroy ) , w ) ;
gtk_widget_show ( w ) ;
g_free ( message ) ;
continue ;
2009-07-28 18:29:36 +00:00
}
2010-04-03 15:56:34 +00:00
// append theme
copy_file ( file , path ) ;
custom_list_append ( path ) ;
if ( name_first = = NULL )
name_first = g_strdup ( path ) ;
g_free ( path ) ;
g_free ( name ) ;
2009-07-28 18:29:36 +00:00
}
2010-04-03 15:56:34 +00:00
g_slist_foreach ( list , ( GFunc ) g_free , NULL ) ;
g_slist_free ( list ) ;
gtk_widget_destroy ( dialog ) ;
selectTheme ( name_first ) ;
g_free ( name_first ) ;
2010-05-04 20:15:46 +00:00
g_timeout_add ( SNAPSHOT_TICK , ( GSourceFunc ) update_snapshot , NULL ) ;
2009-07-15 22:16:39 +00:00
}
2010-03-26 23:16:25 +00:00
2010-04-02 20:02:41 +00:00
static void menuSaveAs ( )
2009-07-15 22:16:39 +00:00
{
2009-07-28 18:29:36 +00:00
GtkWidget * dialog ;
GtkFileChooser * chooser ;
2010-04-02 20:02:41 +00:00
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
gchar * file , * pt1 ;
2009-07-15 22:16:39 +00:00
2010-04-02 20:02:41 +00:00
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
if ( ! gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
2010-06-11 15:37:53 +00:00
GtkWidget * w = gtk_message_dialog_new ( GTK_WINDOW ( g_window ) , 0 , GTK_MESSAGE_ERROR , GTK_BUTTONS_CLOSE , _ ( " Select the theme to be saved. " ) ) ;
2010-04-02 20:02:41 +00:00
g_signal_connect_swapped ( w , " response " , G_CALLBACK ( gtk_widget_destroy ) , w ) ;
gtk_widget_show ( w ) ;
return ;
}
gtk_tree_model_get ( model , & iter , COL_THEME_FILE , & file , - 1 ) ;
pt1 = strrchr ( file , ' / ' ) ;
if ( pt1 ) pt1 + + ;
2010-06-11 15:37:53 +00:00
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 ) ;
2009-07-28 18:29:36 +00:00
chooser = GTK_FILE_CHOOSER ( dialog ) ;
2009-07-15 22:16:39 +00:00
2010-04-02 20:02:41 +00:00
gtk_file_chooser_set_do_overwrite_confirmation ( chooser , TRUE ) ;
2015-01-28 15:31:20 +00:00
gchar * config_dir ;
config_dir = g_build_filename ( g_get_home_dir ( ) , " .config " , " tint2 " , NULL ) ;
gtk_file_chooser_set_current_folder ( chooser , config_dir ) ;
g_free ( config_dir ) ;
2010-04-02 20:02:41 +00:00
gtk_file_chooser_set_current_name ( chooser , pt1 ) ;
2009-07-15 22:16:39 +00:00
2010-04-02 20:02:41 +00:00
if ( gtk_dialog_run ( GTK_DIALOG ( dialog ) ) = = GTK_RESPONSE_ACCEPT ) {
2009-07-28 18:29:36 +00:00
char * filename = gtk_file_chooser_get_filename ( chooser ) ;
2010-04-02 20:02:41 +00:00
copy_file ( file , filename ) ;
2009-07-28 18:29:36 +00:00
g_free ( filename ) ;
}
2010-04-02 20:02:41 +00:00
g_free ( file ) ;
2009-07-28 18:29:36 +00:00
gtk_widget_destroy ( dialog ) ;
}
2010-03-26 23:16:25 +00:00
2009-10-24 23:23:01 +00:00
2010-04-02 20:02:41 +00:00
static void menuDelete ( )
2009-10-24 23:23:01 +00:00
{
2009-10-25 18:59:59 +00:00
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
2010-04-03 15:56:34 +00:00
gchar * filename ;
2009-10-25 18:59:59 +00:00
2009-10-26 20:17:05 +00:00
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
2009-10-25 18:59:59 +00:00
if ( gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
2010-04-03 15:56:34 +00:00
gtk_tree_model_get ( model , & iter , COL_THEME_FILE , & filename , - 1 ) ;
2009-10-26 21:21:54 +00:00
gtk_tree_selection_unselect_all ( sel ) ;
2009-10-25 18:59:59 +00:00
2010-04-03 15:56:34 +00:00
// remove (gui and file)
2010-03-26 23:16:25 +00:00
gtk_list_store_remove ( GTK_LIST_STORE ( model ) , & iter ) ;
2010-04-03 15:56:34 +00:00
GFile * file = g_file_new_for_path ( filename ) ;
g_file_trash ( file , NULL , NULL ) ;
g_object_unref ( G_OBJECT ( file ) ) ;
g_free ( filename ) ;
2009-10-25 18:59:59 +00:00
}
2009-10-24 23:23:01 +00:00
}
2009-07-15 22:16:39 +00:00
2015-01-28 15:31:20 +00:00
char * get_current_theme_file_name ( )
{
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
char * file ;
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
if ( gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
gtk_tree_model_get ( model , & iter , COL_THEME_FILE , & file , - 1 ) ;
return file ;
}
return NULL ;
}
2009-07-15 22:16:39 +00:00
2010-04-02 20:02:41 +00:00
static void menuProperties ( )
2009-07-28 18:29:36 +00:00
{
2009-11-03 15:32:55 +00:00
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
2010-11-07 08:44:27 +00:00
char * file ;
2009-11-03 15:32:55 +00:00
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
if ( gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
2010-03-28 22:50:26 +00:00
gtk_tree_model_get ( model , & iter , COL_THEME_FILE , & file , - 1 ) ;
2010-06-12 22:17:45 +00:00
GtkWidget * prop ;
prop = create_properties ( ) ;
2011-06-06 09:16:51 +00:00
config_read_file ( file ) ;
2010-06-12 22:17:45 +00:00
gtk_window_present ( GTK_WINDOW ( prop ) ) ;
2010-03-28 22:50:26 +00:00
g_free ( file ) ;
2009-11-03 15:32:55 +00:00
}
2009-07-28 18:29:36 +00:00
}
2009-07-15 22:16:39 +00:00
2010-04-02 20:02:41 +00:00
static void menuQuit ( )
2009-07-28 18:29:36 +00:00
{
2009-11-03 15:32:55 +00:00
write_config ( ) ;
2010-04-01 21:25:11 +00:00
if ( g_path_config )
g_free ( g_path_config ) ;
if ( g_path_dir )
g_free ( g_path_dir ) ;
if ( g_default_theme )
g_free ( g_default_theme ) ;
2015-01-20 02:23:28 +00:00
gtk_main_quit ( ) ;
2009-07-28 18:29:36 +00:00
}
2010-04-02 20:02:41 +00:00
static void menuRefresh ( )
2009-07-28 18:29:36 +00:00
{
2010-04-03 22:52:52 +00:00
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
if ( gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
gtk_list_store_set ( g_store , & iter , COL_SNAPSHOT , NULL , - 1 ) ;
}
2010-05-04 20:15:46 +00:00
g_timeout_add ( SNAPSHOT_TICK , ( GSourceFunc ) update_snapshot , NULL ) ;
2009-07-28 18:29:36 +00:00
}
2010-04-02 20:02:41 +00:00
static void menuRefreshAll ( )
2009-07-28 18:29:36 +00:00
{
2010-04-03 22:52:52 +00:00
GtkTreeIter iter ;
GtkTreeModel * model ;
gboolean have_iter ;
2009-07-28 18:29:36 +00:00
2010-04-03 22:52:52 +00:00
model = gtk_tree_view_get_model ( GTK_TREE_VIEW ( g_theme_view ) ) ;
have_iter = gtk_tree_model_get_iter_first ( model , & iter ) ;
while ( have_iter ) {
gtk_list_store_set ( g_store , & iter , COL_SNAPSHOT , NULL , - 1 ) ;
have_iter = gtk_tree_model_iter_next ( model , & iter ) ;
}
2009-07-28 18:29:36 +00:00
2010-05-04 20:15:46 +00:00
g_timeout_add ( SNAPSHOT_TICK , ( GSourceFunc ) update_snapshot , NULL ) ;
2010-03-26 23:16:25 +00:00
}
2015-01-28 15:31:20 +00:00
void menuApply ( )
2009-07-28 18:29:36 +00:00
{
2009-10-24 23:23:01 +00:00
GtkTreeSelection * sel ;
GtkTreeIter iter ;
GtkTreeModel * model ;
2010-04-01 21:25:11 +00:00
if ( g_default_theme ) {
g_free ( g_default_theme ) ;
g_default_theme = NULL ;
}
2009-10-26 20:17:05 +00:00
sel = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) ;
2009-10-24 23:23:01 +00:00
if ( gtk_tree_selection_get_selected ( GTK_TREE_SELECTION ( sel ) , & model , & iter ) ) {
2010-04-01 21:25:11 +00:00
gtk_tree_model_get ( model , & iter , COL_THEME_FILE , & g_default_theme , - 1 ) ;
2015-01-28 15:31:20 +00:00
// Create backup if the config file has been edited manually
if ( config_is_manual ( g_path_config ) ) {
char timestamp [ 64 ] ;
sprintf ( timestamp , " %ld " , time ( NULL ) ) ;
gchar * backup_path = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , " tint2rc.backup. " , timestamp , NULL ) ;
copy_file ( g_path_config , backup_path ) ;
g_free ( backup_path ) ;
}
// Write config file
printf ( " Writing %s to %s \n " , g_default_theme , g_path_config ) ;
2010-04-01 21:25:11 +00:00
copy_file ( g_default_theme , g_path_config ) ;
2015-01-28 15:31:20 +00:00
// Restart panel
int unused = system ( " killall -SIGUSR1 tint2 " ) ;
( void ) unused ;
2009-10-24 23:23:01 +00:00
}
2009-07-28 18:29:36 +00:00
}
2009-10-26 20:17:05 +00:00
static void view_popup_menu ( GtkWidget * treeview , GdkEventButton * event , gpointer userdata )
{
GtkWidget * w = gtk_ui_manager_get_widget ( globalUIManager , " /ThemePopup " ) ;
gtk_menu_popup ( GTK_MENU ( w ) , NULL , NULL , NULL , NULL , ( event ! = NULL ) ? event - > button : 0 , gdk_event_get_time ( ( GdkEvent * ) event ) ) ;
}
static gboolean view_onButtonPressed ( GtkWidget * treeview , GdkEventButton * event , gpointer userdata )
2009-07-28 18:29:36 +00:00
{
2009-10-26 21:21:54 +00:00
// single click with the right mouse button?
2009-10-26 20:17:05 +00:00
if ( event - > type = = GDK_BUTTON_PRESS & & event - > button = = 3 ) {
GtkTreeSelection * selection ;
selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW ( treeview ) ) ;
if ( gtk_tree_selection_count_selected_rows ( selection ) < = 1 ) {
GtkTreePath * path ;
2009-10-26 21:21:54 +00:00
// Get tree path for row that was clicked
2009-10-26 20:17:05 +00:00
if ( gtk_tree_view_get_path_at_pos ( GTK_TREE_VIEW ( treeview ) , ( gint ) event - > x , ( gint ) event - > y , & path , NULL , NULL , NULL ) ) {
gtk_tree_selection_unselect_all ( selection ) ;
gtk_tree_selection_select_path ( selection , path ) ;
gtk_tree_path_free ( path ) ;
}
}
2009-07-28 18:29:36 +00:00
2009-10-26 20:17:05 +00:00
view_popup_menu ( treeview , event , userdata ) ;
return TRUE ;
}
return FALSE ;
}
static gboolean view_onPopupMenu ( GtkWidget * treeview , gpointer userdata )
{
view_popup_menu ( treeview , NULL , userdata ) ;
return TRUE ;
2009-07-28 18:29:36 +00:00
}
static void viewRowActivated ( GtkTreeView * tree_view , GtkTreePath * path , GtkTreeViewColumn * column , gpointer user_data )
{
2009-10-25 18:59:59 +00:00
menuApply ( ) ;
2009-07-28 18:29:36 +00:00
}
2010-04-02 20:02:41 +00:00
static void windowSizeAllocated ( )
{
2015-01-28 15:31:20 +00:00
const gboolean isMaximized = g_window - > window & &
( gdk_window_get_state ( g_window - > window ) & GDK_WINDOW_STATE_MAXIMIZED ) ;
2010-04-02 20:02:41 +00:00
if ( ! isMaximized )
gtk_window_get_size ( GTK_WINDOW ( g_window ) , & g_width , & g_height ) ;
}
2009-11-16 09:27:44 +00:00
static void load_theme ( GtkWidget * list )
2009-07-28 18:29:36 +00:00
{
2009-10-24 23:23:01 +00:00
GDir * dir ;
2015-01-28 15:31:20 +00:00
gchar * name ;
2010-04-03 22:52:52 +00:00
const gchar * file ;
2010-04-03 15:56:34 +00:00
gboolean found_theme = FALSE ;
2009-10-24 23:23:01 +00:00
2009-11-03 15:32:55 +00:00
dir = g_dir_open ( g_path_dir , 0 , NULL ) ;
2015-01-28 15:31:20 +00:00
if ( dir = = NULL )
return ;
2009-10-24 23:23:01 +00:00
while ( ( file = g_dir_read_name ( dir ) ) ) {
2015-01-28 15:31:20 +00:00
if ( g_str_has_suffix ( file , " .tint2rc " ) ) {
2010-03-26 23:16:25 +00:00
found_theme = TRUE ;
name = g_build_filename ( g_path_dir , file , NULL ) ;
2010-04-01 21:25:11 +00:00
custom_list_append ( name ) ;
2009-10-24 23:23:01 +00:00
g_free ( name ) ;
}
2009-07-28 18:29:36 +00:00
}
2009-10-24 23:23:01 +00:00
g_dir_close ( dir ) ;
2009-07-28 18:29:36 +00:00
2010-03-26 23:16:25 +00:00
if ( ! found_theme ) {
// create default theme file
2010-04-03 15:56:34 +00:00
name = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , " default.tint2rc " , NULL ) ;
2010-03-28 22:50:26 +00:00
copy_file ( g_path_config , name ) ;
2010-04-01 21:25:11 +00:00
custom_list_append ( name ) ;
2015-01-28 15:31:20 +00:00
if ( g_default_theme )
g_free ( g_default_theme ) ;
2010-04-03 15:56:34 +00:00
g_default_theme = strdup ( name ) ;
2010-03-28 22:50:26 +00:00
g_free ( name ) ;
2010-03-26 23:16:25 +00:00
}
2010-04-03 15:56:34 +00:00
selectTheme ( g_default_theme ) ;
2010-04-03 22:52:52 +00:00
2010-05-04 20:15:46 +00:00
g_timeout_add ( SNAPSHOT_TICK , ( GSourceFunc ) update_snapshot , NULL ) ;
2010-04-03 15:56:34 +00:00
}
void selectTheme ( const gchar * name_theme )
{
gboolean have_iter , found_theme ;
GtkTreeIter iter ;
GtkTreeModel * model ;
2015-01-28 15:31:20 +00:00
if ( ! name_theme )
return ;
2010-04-03 15:56:34 +00:00
2010-04-01 21:25:11 +00:00
model = gtk_tree_view_get_model ( GTK_TREE_VIEW ( g_theme_view ) ) ;
2010-04-03 15:56:34 +00:00
found_theme = searchTheme ( name_theme , model , & iter ) ;
2010-04-01 21:25:11 +00:00
GtkTreePath * path = NULL ;
2015-01-28 15:31:20 +00:00
if ( found_theme ) {
2010-04-01 21:25:11 +00:00
path = gtk_tree_model_get_path ( model , & iter ) ;
2015-01-28 15:31:20 +00:00
} else {
2010-04-01 21:25:11 +00:00
have_iter = gtk_tree_model_get_iter_first ( model , & iter ) ;
if ( have_iter )
path = gtk_tree_model_get_path ( model , & iter ) ;
2009-10-25 16:36:40 +00:00
}
2010-04-01 21:25:11 +00:00
if ( path ) {
gtk_tree_selection_select_iter ( gtk_tree_view_get_selection ( GTK_TREE_VIEW ( g_theme_view ) ) , & iter ) ;
gtk_tree_view_scroll_to_cell ( GTK_TREE_VIEW ( g_theme_view ) , path , NULL , FALSE , 0 , 0 ) ;
gtk_tree_path_free ( path ) ;
}
2010-04-03 15:56:34 +00:00
}
2010-04-01 21:25:11 +00:00
2010-04-03 15:56:34 +00:00
gboolean searchTheme ( const gchar * name_theme , GtkTreeModel * model , GtkTreeIter * iter )
{
gchar * name ;
gboolean have_iter , found = FALSE ;
have_iter = gtk_tree_model_get_iter_first ( model , iter ) ;
while ( have_iter ) {
gtk_tree_model_get ( model , iter , COL_THEME_FILE , & name , - 1 ) ;
found = ( strcmp ( name , name_theme ) = = 0 ) ;
g_free ( name ) ;
if ( found )
break ;
have_iter = gtk_tree_model_iter_next ( model , iter ) ;
}
return found ;
2009-10-25 16:36:40 +00:00
}
2009-07-28 18:29:36 +00:00
2015-01-28 15:31:20 +00:00
// Initializes the paths to the tint2 config locations.
2010-04-03 22:52:52 +00:00
void initTheme ( )
{
g_path_dir = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , NULL ) ;
if ( ! g_file_test ( g_path_dir , G_FILE_TEST_IS_DIR ) )
g_mkdir ( g_path_dir , 0777 ) ;
g_path_config = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , " tint2rc " , NULL ) ;
}
2015-01-28 15:31:20 +00:00
// Reads the tint2conf config file.
2009-11-03 15:32:55 +00:00
void read_config ( )
2009-10-24 23:23:01 +00:00
{
2009-10-25 16:36:40 +00:00
char * path ;
2010-05-15 14:43:01 +00:00
// default values
2010-03-26 23:16:25 +00:00
if ( g_default_theme ! = NULL ) {
2009-11-03 15:32:55 +00:00
g_free ( g_default_theme ) ;
2010-03-26 23:16:25 +00:00
g_default_theme = NULL ;
}
2010-04-02 20:02:41 +00:00
g_width = 500 ;
2009-11-03 15:32:55 +00:00
g_height = 350 ;
2010-05-15 14:43:01 +00:00
// load config
2009-10-25 16:36:40 +00:00
path = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , " tint2confrc " , NULL ) ;
if ( g_file_test ( path , G_FILE_TEST_EXISTS ) ) {
FILE * fp ;
char line [ 80 ] ;
char * key , * value ;
if ( ( fp = fopen ( path , " r " ) ) ! = NULL ) {
while ( fgets ( line , sizeof ( line ) , fp ) ! = NULL ) {
if ( parse_line ( line , & key , & value ) ) {
2009-11-03 15:32:55 +00:00
if ( strcmp ( key , " default_theme " ) = = 0 )
2010-05-15 14:43:01 +00:00
g_default_theme = strdup ( value ) ;
2009-11-03 15:32:55 +00:00
else if ( strcmp ( key , " width " ) = = 0 )
g_width = atoi ( value ) ;
else if ( strcmp ( key , " height " ) = = 0 )
g_height = atoi ( value ) ;
2009-10-25 16:36:40 +00:00
free ( key ) ;
free ( value ) ;
}
}
fclose ( fp ) ;
}
}
g_free ( path ) ;
2009-10-24 23:23:01 +00:00
}
2015-01-28 15:31:20 +00:00
// Writes the tint2conf config file.
2009-11-03 15:32:55 +00:00
void write_config ( )
2009-10-24 23:23:01 +00:00
{
2009-10-25 16:36:40 +00:00
char * path ;
FILE * fp ;
2010-04-02 20:02:41 +00:00
2009-10-25 16:36:40 +00:00
path = g_build_filename ( g_get_user_config_dir ( ) , " tint2 " , " tint2confrc " , NULL ) ;
fp = fopen ( path , " w " ) ;
if ( fp ! = NULL ) {
fputs ( " #--------------------------------------------- \n " , fp ) ;
fputs ( " # TINT2CONF CONFIG FILE \n " , fp ) ;
2010-03-26 23:16:25 +00:00
if ( g_default_theme ! = NULL ) {
fprintf ( fp , " default_theme = %s \n " , g_default_theme ) ;
2010-05-15 14:43:01 +00:00
}
2009-11-03 15:32:55 +00:00
fprintf ( fp , " width = %d \n " , g_width ) ;
fprintf ( fp , " height = %d \n " , g_height ) ;
fputs ( " \n " , fp ) ;
2009-10-25 16:36:40 +00:00
fclose ( fp ) ;
}
g_free ( path ) ;
}