[gimp/wip/nielsdg/propswitch: 15/15] propwidget: Add gimp_prop_switch_new()



commit c77a75a09028e8a4fe919fddb8232d2e10750f42
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat May 30 15:20:06 2020 +0200

    propwidget: Add gimp_prop_switch_new()
    
    Allow developers to add a `GtkSwitch` based on a property. Also start
    using it in the first preferences page.

 app/dialogs/preferences-dialog-utils.c             | 23 +++++++
 app/dialogs/preferences-dialog-utils.h             |  5 ++
 app/dialogs/preferences-dialog.c                   | 21 ++++---
 .../libgimpwidgets/libgimpwidgets3-sections.txt    |  1 +
 libgimpwidgets/gimppropwidgets.c                   | 72 ++++++++++++++++++++++
 libgimpwidgets/gimppropwidgets.h                   |  5 ++
 libgimpwidgets/gimpwidgets.def                     |  1 +
 7 files changed, 119 insertions(+), 9 deletions(-)
---
diff --git a/app/dialogs/preferences-dialog-utils.c b/app/dialogs/preferences-dialog-utils.c
index 4078e3b7bb..8358474103 100644
--- a/app/dialogs/preferences-dialog-utils.c
+++ b/app/dialogs/preferences-dialog-utils.c
@@ -135,6 +135,29 @@ prefs_check_button_add (GObject     *config,
   return button;
 }
 
+GtkWidget *
+prefs_switch_add (GObject      *config,
+                  const gchar  *property_name,
+                  const gchar  *label,
+                  GtkBox       *vbox,
+                  GtkSizeGroup *group)
+{
+  GtkWidget *box;
+  GtkWidget *plabel;
+
+  box = gimp_prop_switch_new (config, property_name, label, &plabel, NULL);
+
+  if (!box)
+    return NULL;
+
+  gtk_box_pack_start (vbox, box, FALSE, FALSE, 0);
+  gtk_label_set_xalign (GTK_LABEL (plabel), 0.0);
+  if (group)
+    gtk_size_group_add_widget (group, plabel);
+
+  return box;
+}
+
 GtkWidget *
 prefs_check_button_add_with_icon (GObject      *config,
                                   const gchar  *property_name,
diff --git a/app/dialogs/preferences-dialog-utils.h b/app/dialogs/preferences-dialog-utils.h
index 3b7fae5a96..8dc6a5bd8a 100644
--- a/app/dialogs/preferences-dialog-utils.h
+++ b/app/dialogs/preferences-dialog-utils.h
@@ -34,6 +34,11 @@ GtkWidget * prefs_hint_box_new               (const gchar  *icon_name,
 GtkWidget * prefs_button_add                 (const gchar  *icon_name,
                                               const gchar  *label,
                                               GtkBox       *box);
+GtkWidget * prefs_switch_add                 (GObject      *config,
+                                              const gchar  *property_name,
+                                              const gchar  *label,
+                                              GtkBox       *vbox,
+                                                                                         GtkSizeGroup 
*group);
 GtkWidget * prefs_check_button_add           (GObject      *config,
                                               const gchar  *property_name,
                                               const gchar  *label,
diff --git a/app/dialogs/preferences-dialog.c b/app/dialogs/preferences-dialog.c
index 0fce63adc2..950fbe63e8 100644
--- a/app/dialogs/preferences-dialog.c
+++ b/app/dialogs/preferences-dialog.c
@@ -1200,9 +1200,10 @@ prefs_dialog_new (Gimp       *gimp,
   vbox2 = prefs_frame_new (_("Hardware Acceleration"), GTK_CONTAINER (vbox),
                            FALSE);
 
-  prefs_check_button_add (object, "use-opencl",
-                          _("Use O_penCL"),
-                          GTK_BOX (vbox2));
+  prefs_switch_add (object, "use-opencl",
+                    _("Use O_penCL"),
+                    GTK_BOX (vbox2),
+                    size_group);
 
   hbox = prefs_hint_box_new (GIMP_ICON_DIALOG_WARNING,
                              _("OpenCL drivers and support are experimental, "
@@ -1215,9 +1216,10 @@ prefs_dialog_new (Gimp       *gimp,
   vbox2 = prefs_frame_new (_("Network access"), GTK_CONTAINER (vbox),
                            FALSE);
 
-  prefs_check_button_add (object, "check-updates",
-                          _("Check for updates (requires internet)"),
-                          GTK_BOX (vbox2));
+  prefs_switch_add (object, "check-updates",
+                    _("Check for updates (requires internet)"),
+                    GTK_BOX (vbox2),
+                    size_group);
 #endif
 
   /*  Image Thumbnails  */
@@ -1236,9 +1238,10 @@ prefs_dialog_new (Gimp       *gimp,
   /*  Document History  */
   vbox2 = prefs_frame_new (_("Document History"), GTK_CONTAINER (vbox), FALSE);
 
-  prefs_check_button_add (object, "save-document-history",
-                          _("_Keep record of used files in the Recent Documents list"),
-                          GTK_BOX (vbox2));
+  prefs_switch_add (object, "save-document-history",
+                    _("_Keep record of used files in the Recent Documents list"),
+                    GTK_BOX (vbox2),
+                    size_group);
 
   g_clear_object (&size_group);
 
diff --git a/devel-docs/libgimpwidgets/libgimpwidgets3-sections.txt 
b/devel-docs/libgimpwidgets/libgimpwidgets3-sections.txt
index 635895c0fb..c787b6e2cf 100644
--- a/devel-docs/libgimpwidgets/libgimpwidgets3-sections.txt
+++ b/devel-docs/libgimpwidgets/libgimpwidgets3-sections.txt
@@ -1219,6 +1219,7 @@ gimp_prop_scale_entry_new
 gimp_prop_size_entry_new
 gimp_prop_spin_button_new
 gimp_prop_string_combo_box_new
+gimp_prop_switch_new
 gimp_prop_text_buffer_new
 gimp_prop_unit_combo_box_new
 </SECTION>
diff --git a/libgimpwidgets/gimppropwidgets.c b/libgimpwidgets/gimppropwidgets.c
index c5b38eb3d3..36758ea394 100644
--- a/libgimpwidgets/gimppropwidgets.c
+++ b/libgimpwidgets/gimppropwidgets.c
@@ -341,6 +341,78 @@ gimp_prop_enum_check_button_notify (GObject    *config,
 }
 
 
+/******************/
+/*     switch     */
+/******************/
+
+
+/**
+ * gimp_prop_switch_new:
+ * @config:        Object to which property is attached.
+ * @property_name: Name of boolean property controlled by checkbutton.
+ * @label:         Label to give checkbutton (including mnemonic).
+ * @label_out: (out) (optional) (transfer none): The generated #GtkLabel
+ * @switch_out: (out) (optional) (transfer none): The generated #GtkSwitch
+ *
+ * Creates a #GtkBox with a switch and a label that displays and sets the
+ * specified boolean property.
+ * If @label is %NULL, the @property_name's nick will be used as label.
+ *
+ * Returns: (transfer full): The newly created box containing a #GtkSwitch.
+ *
+ * Since: 3.0
+ */
+GtkWidget *
+gimp_prop_switch_new (GObject     *config,
+                      const gchar *property_name,
+                      const gchar *label,
+                      GtkWidget  **label_out,
+                      GtkWidget  **switch_out)
+{
+  GParamSpec  *param_spec;
+  const gchar *tooltip;
+  GtkWidget   *plabel;
+  GtkWidget   *pswitch;
+  GtkWidget   *hbox;
+
+  g_return_val_if_fail (G_IS_OBJECT (config), NULL);
+  g_return_val_if_fail (property_name != NULL, NULL);
+
+  param_spec = check_param_spec_w (config, property_name,
+                                   G_TYPE_PARAM_BOOLEAN, G_STRFUNC);
+  if (! param_spec)
+    return NULL;
+
+  if (! label)
+    label = g_param_spec_get_nick (param_spec);
+
+  tooltip = g_param_spec_get_blurb (param_spec);
+
+  pswitch = gtk_switch_new ();
+  g_object_bind_property (config, property_name, pswitch, "active",
+                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+  gimp_help_set_help_data (pswitch, tooltip, NULL);
+  gtk_widget_show (pswitch);
+
+  plabel = gtk_label_new_with_mnemonic (label);
+  gtk_label_set_mnemonic_widget (GTK_LABEL (plabel), pswitch);
+  gimp_help_set_help_data (plabel, tooltip, NULL);
+  gtk_widget_show (plabel);
+
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
+  gtk_box_pack_start (GTK_BOX (hbox), plabel, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox), pswitch, FALSE, FALSE, 0);
+  gtk_widget_show (hbox);
+
+  if (label_out)
+    *label_out = plabel;
+  if (switch_out)
+    *switch_out = pswitch;
+
+  return hbox;
+}
+
+
 /*************************/
 /*  int/enum combo box   */
 /*************************/
diff --git a/libgimpwidgets/gimppropwidgets.h b/libgimpwidgets/gimppropwidgets.h
index 321b79be40..4e0343ecf2 100644
--- a/libgimpwidgets/gimppropwidgets.h
+++ b/libgimpwidgets/gimppropwidgets.h
@@ -34,6 +34,11 @@ G_BEGIN_DECLS
 GtkWidget     * gimp_prop_check_button_new        (GObject      *config,
                                                    const gchar  *property_name,
                                                    const gchar  *label);
+GtkWidget     * gimp_prop_switch_new              (GObject      *config,
+                                                   const gchar  *property_name,
+                                                   const gchar  *label,
+                                                   GtkWidget   **label_out,
+                                                   GtkWidget   **switch_out);
 GtkWidget     * gimp_prop_boolean_combo_box_new   (GObject      *config,
                                                    const gchar  *property_name,
                                                    const gchar  *true_text,
diff --git a/libgimpwidgets/gimpwidgets.def b/libgimpwidgets/gimpwidgets.def
index 67319e4dc7..8cf7ad832f 100644
--- a/libgimpwidgets/gimpwidgets.def
+++ b/libgimpwidgets/gimpwidgets.def
@@ -342,6 +342,7 @@ EXPORTS
        gimp_prop_size_entry_new
        gimp_prop_spin_button_new
        gimp_prop_string_combo_box_new
+       gimp_prop_switch_new
        gimp_prop_text_buffer_new
        gimp_prop_unit_combo_box_new
        gimp_query_boolean_box


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