Add some checks and special dirty hacks to make lxde-settings and
authorHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Sun, 6 Apr 2008 14:47:50 +0000 (14:47 +0000)
committerHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Sun, 6 Apr 2008 14:47:50 +0000 (14:47 +0000)
lxappearance work together.

src/demo.c
src/main-dlg.c
src/main.c

index ba27cef..3f07f3d 100644 (file)
@@ -8,6 +8,8 @@
 #include "demo-ui.h"
 #include "glade-support.h"
 
+extern gboolean under_lxde;    /* wether lxde-xsettings daemon is active */
+
 static GtkIconView* icon_view = NULL;
 
 static void load_demo_icons()
@@ -71,7 +73,7 @@ static void load_demo_tree_view( GtkTreeView* view )
 void show_demo( GdkNativeWindow wid )
 {
     GtkWidget* demo = create_demo_window();
-    GtkWidget* plug = gtk_plug_new( wid );
+    GtkWidget* plug;
     GtkWidget* top_vbox;
     GtkToolbarStyle tb_style;
     GtkWidget* tree_view;
@@ -82,10 +84,6 @@ void show_demo( GdkNativeWindow wid )
     icon_view = lookup_widget( demo, "icon_view" );
     tree_view = lookup_widget( demo, "demo_treeview" );
 
-    gtk_widget_show_all( demo );
-    gtk_container_add( (GtkContainer*)plug, demo );
-
-    gtk_widget_show( plug );
     gtk_icon_view_set_pixbuf_column( icon_view, 0 );
     gtk_icon_view_set_text_column( icon_view, 1 );
     gtk_icon_view_set_item_width( icon_view, 64 );
@@ -94,4 +92,21 @@ void show_demo( GdkNativeWindow wid )
 
     load_demo_icons();
     load_demo_tree_view( (GtkTreeView*)tree_view );
+
+    gtk_widget_show_all( demo );
+    
+    /* LXDE settings daemon is active and we don't use gtkrc files. */
+    if( under_lxde )
+    {
+       /* The demo window and the main dialog are in the same process */
+       GtkWidget* demo_box = (GtkWidget*)wid;
+       gtk_container_add( demo_box, demo );
+    }
+    else
+    {
+       /* we are in another process with a different gtkrc file. */
+               plug = gtk_plug_new( wid );     /* plug our demo window into the main process */
+               gtk_container_add( (GtkContainer*)plug, demo );
+               gtk_widget_show( plug );        
+    }
 }
index 08ba112..a95ee04 100644 (file)
@@ -29,6 +29,8 @@
 #define enable_apply()      gtk_dialog_set_response_sensitive( main_dlg, GTK_RESPONSE_APPLY, TRUE )
 #define disable_apply()      gtk_dialog_set_response_sensitive( main_dlg, GTK_RESPONSE_APPLY, FALSE )
 
+extern gboolean under_lxde;    /* wether lxde-xsettings daemon is active */
+
 extern GtkWidget* main_dlg; /* defined in main.c */
 
 static GtkTreeView* gtk_theme_view = NULL;
@@ -105,6 +107,59 @@ static void write_rc_file( const char* path )
     }
 }
 
+static void create_lxde_config_dir()
+{
+       char* dir = g_build_filename( g_get_user_config_dir(), "lxde", NULL );
+#if GTK_CHECK_VERSION( 2, 8, 0 )
+       g_mkdir_with_parents( dir, 0755 );
+#else
+       /* FIXME: implement mkdir -p for gtk+ < 2.8 */
+#endif
+       g_free( dir );
+}
+
+static void write_lxde_config()
+{
+    FILE* f;
+       char* file, *data;
+       gsize len;
+       GKeyFile* kf = g_key_file_new();
+       gboolean ret;
+
+       file = g_build_filename( g_get_user_config_dir(), "lxde/config", NULL );
+       ret = g_key_file_load_from_file( kf, file, G_KEY_FILE_KEEP_COMMENTS, NULL );
+
+       if( ! ret )
+       {
+               const char** dir, **dirs = g_get_system_data_dirs();
+               create_lxde_config_dir();
+               /* load system-wide config file */
+               for( dir = dirs; *dir; ++dir )
+               {
+                       char* path = g_build_filename( *dir, "lxde/config", NULL );
+                       ret = g_key_file_load_from_file( kf, path, 0, NULL );
+                       g_free( path );
+                       if( ret )
+                               break;
+               }
+       }
+
+       g_key_file_set_string( kf, "GTK", "sNet/ThemeName", gtk_theme_name );
+       g_key_file_set_string( kf, "GTK", "sNet/IconThemeName", icon_theme_name );
+       g_key_file_set_string( kf, "GTK", "sGtk/FontName", font_name );
+       g_key_file_set_integer( kf, "GTK", "iGtk/ToolbarStyle", tb_style );
+
+       data = g_key_file_to_data( kf, &len, NULL );
+       g_key_file_free( kf );
+
+    if( f = fopen( file, "w" ) )
+    {
+               fwrite( data, sizeof(char), len, f );
+        fclose( f );
+    }
+    g_free( data );
+}
+
 static void reload_theme()
 {
 
@@ -126,6 +181,9 @@ static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
                 goto out;
             g_free( gtk_theme_name );
             gtk_theme_name = name;
+
+            if( under_lxde )
+                               g_object_set( gtk_settings_get_default(), "gtk-theme-name", name, NULL );
         }
         else if( model == icon_theme_list )   /* icon theme */
         {
@@ -133,10 +191,22 @@ static void on_list_sel_changed( GtkTreeSelection* sel, const char* prop )
                 goto out;
             g_free( icon_theme_name );
             icon_theme_name = name;
+
+            if( under_lxde )
+                               g_object_set( gtk_settings_get_default(), "gtk-icon-theme-name", name, NULL );
         }
-        write_rc_file( tmp_rc_file );
-        //gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
-        reload_demo_process();
+
+               if( under_lxde )
+               {
+                       enable_apply();
+               }
+               else
+               {
+                       write_rc_file( tmp_rc_file );
+                       //gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
+                       reload_demo_process();
+               }
+
         return;
     out:
         g_free( name );
@@ -261,15 +331,19 @@ void main_dlg_init( GtkWidget* dlg )
     char** def_files = gtk_rc_get_default_files();
     char** file;
 
-    for( file = def_files; *file; ++file )
-    {
-        if( 0 == access( *file, W_OK ) )
-            rc_file = *file;
-    }
-    if( rc_file )
-        rc_file = g_strdup( rc_file );
-    else
-        rc_file = g_build_filename( g_get_home_dir(), ".gtkrc-2.0", NULL );
+       /* no lxde-settings daemon, use gtkrc-2.0 */
+       if( ! under_lxde )
+       {
+               for( file = def_files; *file; ++file )
+               {
+                       if( 0 == access( *file, W_OK ) )
+                               rc_file = *file;
+               }
+               if( rc_file )
+                       rc_file = g_strdup( rc_file );
+               else
+                       rc_file = g_build_filename( g_get_home_dir(), ".gtkrc-2.0", NULL );
+       }
 
     g_object_get( gtk_settings_get_default(),
                         "gtk-theme-name", &gtk_theme_name,
@@ -285,7 +359,9 @@ void main_dlg_init( GtkWidget* dlg )
     if( ! font_name )
         font_name = g_strdup( "Sans 10" );
 
-    write_rc_file( tmp_rc_file );
+       /* no lxde-settings daemon, use gtkrc-2.0 */
+       if( ! under_lxde )
+               write_rc_file( tmp_rc_file );
 
     INIT_LIST( gtk_theme, "gtk-theme-name" )
     INIT_LIST( icon_theme, "gtk-icon-theme-name" )
@@ -296,14 +372,28 @@ void main_dlg_init( GtkWidget* dlg )
     GET_WIDGET( demo_box );
     gtk_widget_show( demo_box );
 
-    demo_socket = gtk_socket_new();
-    g_signal_connect( demo_socket, "plug-added", G_CALLBACK(on_demo_loaded), dlg );
-    g_signal_connect( demo_socket, "plug-removed", G_CALLBACK(gtk_true), NULL );
-    gtk_widget_show( demo_socket );
-    gtk_container_add( (GtkContainer*)demo_box, demo_socket );
+       if( under_lxde )
+       {
+               /* XSettings daemon of LXDE is running, gtkrc is useless.
+                *      We should set properties of GtkSettings object on the fly.
+                *      This will cause problems with some themes, but we have no choice.
+                */
+               show_demo( (GdkNativeWindow)demo_box );
+               gtk_widget_show_all( dlg );
+       }
+       else
+       {
+               /* no lxde-settings daemon, use gtkrc-2.0 and load preview in another process */
+               demo_socket = gtk_socket_new();
+               g_signal_connect( demo_socket, "plug-added", G_CALLBACK(on_demo_loaded), dlg );
+               g_signal_connect( demo_socket, "plug-removed", G_CALLBACK(gtk_true), NULL );
+               gtk_widget_show( demo_socket );
+               gtk_container_add( (GtkContainer*)demo_box, demo_socket );
+
+               gtk_widget_realize( dlg );
+               reload_demo_process();
+       }
 
-    gtk_widget_realize( dlg );
-    reload_demo_process();
     disable_apply();
 }
 
@@ -314,21 +404,32 @@ static void reload_all_programs( gboolean icon_only )
     event.send_event = TRUE;
     event.window = NULL;
 
-    if( icon_only )
-        event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
-    else
-        event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
-
-    event.data_format = 8;
-    gdk_event_send_clientmessage_toall((GdkEvent *)&event);
+       if( under_lxde )
+       {
+               event.message_type = gdk_atom_intern("LXDE_SETTINGS", FALSE);
+               event.data.b[0] = 0;    /* LXS_RELOAD */
+       }
+       else
+       {
+               if( icon_only )
+                       event.message_type = gdk_atom_intern("_GTK_LOAD_ICONTHEMES", FALSE);
+               else
+                       event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
+       }
+       event.data_format = 8;
+       gdk_event_send_clientmessage_toall((GdkEvent *)&event);
 }
 
 void
 on_apply_clicked                       (GtkButton       *button,
                                         gpointer         user_data)
 {
-    write_rc_file( rc_file );
-    reload_all_programs( FALSE );
+       if( under_lxde )
+               write_lxde_config();
+       else
+               write_rc_file( rc_file );
+
+       reload_all_programs( FALSE );
     disable_apply();
 }
 
@@ -342,8 +443,18 @@ on_font_changed                        (GtkFontButton   *fontbutton,
         return;
     g_free( font_name );
     font_name = g_strdup( name );
-    write_rc_file( tmp_rc_file );
-    reload_demo_process();
+
+       if( under_lxde )
+       {
+               g_object_set( gtk_settings_get_default(), "gtk-font-name", font_name, NULL );
+               
+               enable_apply();
+       }
+       else
+       {
+               write_rc_file( tmp_rc_file );
+               reload_demo_process();
+       }
 }
 
 void
@@ -404,7 +515,16 @@ on_tb_style_changed                    (GtkComboBox     *combobox,
     if( sel == tb_style || sel < 0 )
         return;
     tb_style = sel;
-    write_rc_file( tmp_rc_file );
-    reload_demo_process();
+
+       if( under_lxde )
+       {
+               g_object_set( gtk_settings_get_default(), "gtk-toolbar-style", tb_style, NULL );
+               enable_apply();
+       }
+       else
+       {
+               write_rc_file( tmp_rc_file );
+               reload_demo_process();
+       }
 }
 
index c7822fb..3541d64 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
+#include <X11/X.h>
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#include <gdk/gdkx.h>
+
 #include "main-dlg-ui.h"
 #include "demo.h"
 #include "glade-support.h"
 char tmp_rc_file[] = "/tmp/gtkrc-2.0-XXXXXX";
 GtkWidget* main_dlg = NULL;
 
+gboolean under_lxde = FALSE;   /* wether lxde-xsettings daemon is active */
+
+static void check_lxde()
+{
+       Atom atom = XInternAtom( GDK_DISPLAY(), "LXDE_SETTINGS", True );
+       if( atom != None )
+       {
+               XGrabServer( GDK_DISPLAY() );
+               if( XGetSelectionOwner( GDK_DISPLAY(), atom ) )
+                       under_lxde = TRUE;
+               XUngrabServer( GDK_DISPLAY() );
+       }
+}
+
 int main (int argc, char *argv[])
 {
 #ifdef ENABLE_NLS
@@ -33,16 +52,25 @@ int main (int argc, char *argv[])
 
         gtk_set_locale ();
         gtk_init (&argc, &argv);
+
+        check_lxde();
+        if( under_lxde )
+               return 1;
+
         show_demo( (GdkNativeWindow)atoi( argv[2] ) );
         gtk_main();
         return 0;
     }
 
-    mkstemp( tmp_rc_file );
-
     gtk_set_locale ();
     gtk_init (&argc, &argv);
 
+       /* Dirty hack: check if lxde-xsettings daemon is active */
+       check_lxde();
+
+       if( ! under_lxde )
+               mkstemp( tmp_rc_file );
+
     /* Dirty hack: "gtk-toolbar-style" is installed in class_init of GtkToolbar */
     gtk_widget_destroy( gtk_toolbar_new() );
 
@@ -52,7 +80,9 @@ int main (int argc, char *argv[])
 
     gtk_main ();
 
-    unlink( tmp_rc_file );
+       if( ! under_lxde )
+               unlink( tmp_rc_file );
+
     return 0;
 }