[frogr] Add option to allow using or not the dark theme for GTK >= 3.2



commit d363d0e648da21aae43efcb6792fe95a88217769
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Sun Apr 8 03:58:40 2012 +0200

    Add option to allow using or not the dark theme for GTK >= 3.2

 data/gtkbuilder/frogr-main-view.xml |   12 +++++---
 src/frogr-config.c                  |   35 ++++++++++++++++++++++++
 src/frogr-config.h                  |    4 +++
 src/frogr-main-view.c               |   51 ++++++++++++++++++++++++++++++----
 src/main.c                          |    5 ---
 5 files changed, 92 insertions(+), 15 deletions(-)
---
diff --git a/data/gtkbuilder/frogr-main-view.xml b/data/gtkbuilder/frogr-main-view.xml
index 37f29c8..a46907c 100644
--- a/data/gtkbuilder/frogr-main-view.xml
+++ b/data/gtkbuilder/frogr-main-view.xml
@@ -35,6 +35,10 @@
     <property name="label" translatable="yes">Disable _Tooltips</property>
     <signal name="toggled" handler="_on_toggle_action_changed" swapped="no"/>
   </object>
+  <object class="GtkToggleAction" id="use_dark_theme_action">
+    <property name="label" translatable="yes">Use _Dark Theme</property>
+    <signal name="toggled" handler="_on_toggle_action_changed" swapped="no"/>
+  </object>
   <object class="GtkAction" id="edit_details_action">
     <property name="label" translatable="yes">Edit _Detailsâ</property>
     <signal name="activate" handler="_on_action_activated" swapped="no"/>
@@ -533,17 +537,17 @@
               </object>
             </child>
             <child>
-              <object class="GtkSeparatorMenuItem" id="separator7">
+              <object class="GtkCheckMenuItem" id="disable_tooltips_menu_item">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="use_action_appearance">False</property>
+                <property name="related_action">disable_tooltips_action</property>
               </object>
             </child>
             <child>
-              <object class="GtkCheckMenuItem" id="disable_tooltips_menu_item">
+              <object class="GtkCheckMenuItem" id="use_dark_theme_menu_item">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="related_action">disable_tooltips_action</property>
+                <property name="related_action">use_dark_theme_action</property>
               </object>
             </child>
           </object>
diff --git a/src/frogr-config.c b/src/frogr-config.c
index 614bbdf..026baa8 100644
--- a/src/frogr-config.c
+++ b/src/frogr-config.c
@@ -68,6 +68,7 @@ struct _FrogrConfigPrivate
   SortingCriteria mainview_sorting_criteria;
   gboolean mainview_sorting_reversed;
   gboolean mainview_enable_tooltips;
+  gboolean use_dark_theme;
 
   gboolean use_proxy;
   gboolean use_gnome_proxy;
@@ -295,6 +296,16 @@ _load_settings (FrogrConfig *self)
 
           if (!xmlStrcmp (node->name, (const xmlChar*) "http-proxy"))
             _load_proxy_data_xml (self, xml, node);
+
+          if (!xmlStrcmp (node->name, (const xmlChar*) "use-dark-theme"))
+            {
+              xmlChar *content = NULL;
+
+              content = xmlNodeGetContent (node);
+              priv->use_dark_theme = !xmlStrcmp (content, (const xmlChar*) "1");
+
+              xmlFree (content);
+            }
         }
     }
   else if (node && node->name)
@@ -698,6 +709,7 @@ _save_settings (FrogrConfig *self)
   _xml_add_bool_child (root, "tags-autocompletion", priv->tags_autocompletion);
   _xml_add_bool_child (root, "keep-file-extensions", priv->keep_file_extensions);
   _xml_add_bool_child (root, "import-tags-from-metadata", priv->import_tags_from_metadata);
+  _xml_add_bool_child (root, "use-dark-theme", priv->use_dark_theme);
   node = xmlNewNode (NULL, (const xmlChar*) "mainview-options");
   _xml_add_bool_child (node, "enable-tooltips", priv->mainview_enable_tooltips);
   _xml_add_int_child (node, "sorting-criteria", priv->mainview_sorting_criteria);
@@ -973,6 +985,7 @@ frogr_config_init (FrogrConfig *self)
   priv->mainview_sorting_criteria = SORT_AS_LOADED;
   priv->mainview_sorting_reversed = FALSE;
   priv->mainview_enable_tooltips = TRUE;
+  priv->use_dark_theme = FALSE;
   priv->use_proxy = FALSE;
   priv->use_gnome_proxy = FALSE;
   priv->proxy_host = NULL;
@@ -1413,6 +1426,28 @@ frogr_config_get_mainview_enable_tooltips (FrogrConfig *self)
 }
 
 void
+frogr_config_set_use_dark_theme (FrogrConfig *self, gboolean value)
+{
+  FrogrConfigPrivate * priv = NULL;
+
+  g_return_if_fail (FROGR_IS_CONFIG (self));
+
+  priv = FROGR_CONFIG_GET_PRIVATE (self);
+  priv->use_dark_theme = value;
+}
+
+gboolean
+frogr_config_get_use_dark_theme (FrogrConfig *self)
+{
+  FrogrConfigPrivate *priv = NULL;
+
+  g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
+
+  priv = FROGR_CONFIG_GET_PRIVATE (self);
+  return priv->use_dark_theme;
+}
+
+void
 frogr_config_set_mainview_sorting_criteria (FrogrConfig *self,
                                             SortingCriteria criteria)
 {
diff --git a/src/frogr-config.h b/src/frogr-config.h
index d5f91e0..2808b70 100644
--- a/src/frogr-config.h
+++ b/src/frogr-config.h
@@ -136,6 +136,10 @@ void frogr_config_set_mainview_enable_tooltips (FrogrConfig *self, gboolean valu
 
 gboolean frogr_config_get_mainview_enable_tooltips (FrogrConfig *self);
 
+void frogr_config_set_use_dark_theme (FrogrConfig *self, gboolean value);
+
+gboolean frogr_config_get_use_dark_theme (FrogrConfig *self);
+
 void frogr_config_set_mainview_sorting_criteria (FrogrConfig *self,
                                                  SortingCriteria criteria);
 
diff --git a/src/frogr-main-view.c b/src/frogr-main-view.c
index 8d6fd9c..03141c6 100644
--- a/src/frogr-main-view.c
+++ b/src/frogr-main-view.c
@@ -70,6 +70,7 @@ typedef struct _FrogrMainViewPrivate {
   SortingCriteria sorting_criteria;
   gboolean sorting_reversed;
   gboolean tooltips_enabled;
+  gboolean using_dark_theme;
   gint n_selected_pictures;
 
   GtkWindow *window;
@@ -108,6 +109,7 @@ typedef struct _FrogrMainViewPrivate {
   GtkAction *about_action;
   GtkToggleAction *disable_tooltips_action;
   GtkToggleAction *reversed_order_action;
+  GtkToggleAction *use_dark_theme_action;
   GtkToggleAction *sort_as_loaded_action;
   GtkToggleAction *sort_by_title_action;
   GtkToggleAction *sort_by_date_taken_action;
@@ -129,6 +131,7 @@ static gboolean _maybe_show_auth_dialog_on_idle (FrogrMainView *self);
 #ifdef MAC_INTEGRATION
 static void _tweak_menu_bar_for_mac (FrogrMainView *self);
 #endif
+
 static void _populate_accounts_submenu (FrogrMainView *self);
 
 static void _initialize_drag_n_drop (FrogrMainView *self);
@@ -185,6 +188,7 @@ static void _load_pictures (FrogrMainView *self, GSList *fileuris);
 static void _upload_pictures (FrogrMainView *self);
 static void _show_help_contents (FrogrMainView *self);
 static void _reorder_pictures (FrogrMainView *self, SortingCriteria criteria, gboolean reversed);
+static void _use_dark_theme (FrogrMainView *mainview, gboolean enabled);
 
 static void _progress_dialog_response (GtkDialog *dialog,
                                        gint response_id,
@@ -440,8 +444,6 @@ _on_toggle_action_changed (GtkToggleAction *action,
   checked = gtk_toggle_action_get_active (action);
   if (action == priv->disable_tooltips_action)
     {
-      gboolean checked =
-        gtk_toggle_action_get_active (action);
       frogr_config_set_mainview_enable_tooltips (priv->config, !checked);
       priv->tooltips_enabled = !checked;
     }
@@ -450,6 +452,11 @@ _on_toggle_action_changed (GtkToggleAction *action,
       _reorder_pictures (mainview, priv->sorting_criteria, checked);
       frogr_config_set_mainview_sorting_reversed (priv->config, checked);
     }
+  else if (action == priv->use_dark_theme_action)
+    {
+      frogr_config_set_use_dark_theme (priv->config, checked);
+      _use_dark_theme (mainview, checked);
+    }
   else if (checked)
     {
       /* Radio buttons handling here (only care about 'em when checked) */
@@ -1091,6 +1098,19 @@ _reorder_pictures (FrogrMainView *self, SortingCriteria criteria, gboolean rever
 }
 
 static void
+_use_dark_theme (FrogrMainView *mainview, gboolean enabled)
+{
+  FrogrMainViewPrivate *priv = NULL;
+  GtkSettings *gtk_settings = NULL;
+
+  gtk_settings = gtk_settings_get_default ();
+  g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme", enabled, NULL);
+
+  priv = FROGR_MAIN_VIEW_GET_PRIVATE (mainview);
+  priv->using_dark_theme = enabled;
+}
+
+static void
 _progress_dialog_response (GtkDialog *dialog,
                            gint response_id,
                            gpointer data)
@@ -1480,6 +1500,10 @@ frogr_main_view_init (FrogrMainView *self)
   GtkWidget *toolbar;
 #endif
 
+#if !GTK_CHECK_VERSION (3,2,0)
+  GtkWidget *dark_theme_menu_item = NULL;
+#endif
+
   /* Init model, controller and configuration */
   priv->model = frogr_main_view_model_new ();
   priv->controller = g_object_ref (frogr_controller_get_instance ());
@@ -1594,6 +1618,9 @@ frogr_main_view_init (FrogrMainView *self)
   priv->reversed_order_action =
     GTK_TOGGLE_ACTION (gtk_builder_get_object (builder,
                                                "reversed_order_action"));
+  priv->use_dark_theme_action =
+    GTK_TOGGLE_ACTION (gtk_builder_get_object (builder,
+                                               "use_dark_theme_action"));
 #ifndef MAC_INTEGRATION
   priv->quit_action =
     GTK_ACTION (gtk_builder_get_object (builder, "quit_action"));
@@ -1612,15 +1639,21 @@ frogr_main_view_init (FrogrMainView *self)
     gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->sort_as_loaded_action), TRUE);
 
   priv->sorting_reversed = frogr_config_get_mainview_sorting_reversed (priv->config);
-  gtk_toggle_action_set_active (priv->reversed_order_action,
-                                priv->sorting_reversed);
+  gtk_toggle_action_set_active (priv->reversed_order_action, priv->sorting_reversed);
 
   /* Read value for 'tooltips enabled' */
   priv->tooltips_enabled = frogr_config_get_mainview_enable_tooltips (priv->config);
-
   gtk_toggle_action_set_active (priv->disable_tooltips_action,
                                 !priv->tooltips_enabled);
 
+#if GTK_CHECK_VERSION (3,2,0)
+  /* Read value for 'use dark theme' (for GTK >= 3.2 only) */
+  priv->using_dark_theme = frogr_config_get_use_dark_theme (priv->config);
+  _use_dark_theme (self, priv->using_dark_theme);
+  gtk_toggle_action_set_active (priv->use_dark_theme_action,
+                                priv->using_dark_theme);
+#endif
+
   /* No selected pictures at the beginning */
   priv->n_selected_pictures = 0;
 
@@ -1755,9 +1788,15 @@ frogr_main_view_init (FrogrMainView *self)
 
   gtk_builder_connect_signals (builder, self);
 
-  /* Show the UI, but hiding some widgets */
+  /* Show the UI */
   gtk_widget_show_all (GTK_WIDGET(priv->window));
 
+#if !GTK_CHECK_VERSION (3,2,0)
+  /* Hide the option to select the dark theme if GTK < 3.2 */
+  dark_theme_menu_item = GTK_WIDGET (gtk_builder_get_object (priv->builder, "use_dark_theme_menu_item"));
+  gtk_widget_hide (dark_theme_menu_item);
+#endif
+
   /* Update UI */
   _update_ui (FROGR_MAIN_VIEW (self));
 
diff --git a/src/main.c b/src/main.c
index b516d33..6719e0b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -80,7 +80,6 @@ main (int argc, char **argv)
 {
   FrogrController *fcontroller = NULL;
   GSList *fileuris = NULL;
-  GtkSettings *gtk_settings;
 
   /* Check optional command line parameters */
   if (argc > 1)
@@ -100,10 +99,6 @@ main (int argc, char **argv)
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
 
-  gtk_settings = gtk_settings_get_default ();
-  g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme",
-                TRUE, NULL);
-
   /* Init libxml2 library */
   xmlInitParser ();
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]