[gtk/gtk-3-24] widget-factory: Backport error state examples



commit ebafe781b45f9f7fb770d9c77f44364c19e7ea14
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 15 08:44:44 2020 -0400

    widget-factory: Backport error state examples
    
    Backport changes from master to introduce error
    states in the preference dialog on page 2.

 demos/widget-factory/widget-factory.c  | 70 ++++++++++++++++++++++++++++++++++
 demos/widget-factory/widget-factory.ui | 21 ++++++++++
 2 files changed, 91 insertions(+)
---
diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c
index 99dbdd7edc..eb0f96a3b0 100644
--- a/demos/widget-factory/widget-factory.c
+++ b/demos/widget-factory/widget-factory.c
@@ -1655,6 +1655,66 @@ adjustment3_value_changed (GtkAdjustment *adj, GtkProgressBar *pbar)
   gtk_progress_bar_set_fraction (pbar, fraction);
 }
 
+static void
+validate_more_details (GtkEntry   *entry,
+                       GParamSpec *pspec,
+                       GtkEntry   *details)
+{
+  if (strlen (gtk_entry_get_text (entry)) > 0 &&
+      strlen (gtk_entry_get_text (details)) == 0)
+    {
+      gtk_widget_set_tooltip_text (GTK_WIDGET (entry), "Must have details first");
+      gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (entry)), "error");
+    }
+  else
+    {
+      gtk_widget_set_tooltip_text (GTK_WIDGET (entry), "");
+      gtk_style_context_remove_class (gtk_widget_get_style_context (GTK_WIDGET (entry)), "error");
+    }
+}
+
+static gboolean
+mode_switch_state_set (GtkSwitch *sw, gboolean state)
+{
+  GtkWidget *dialog = gtk_widget_get_ancestor (GTK_WIDGET (sw), GTK_TYPE_DIALOG);
+  GtkWidget *scale = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), "level_scale"));
+  GtkWidget *label = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), "error_label"));
+
+  if (!state ||
+      (gtk_range_get_value (GTK_RANGE (scale)) > 50))
+    {
+      gtk_widget_hide (label);
+      gtk_switch_set_state (sw, state);
+    }
+  else
+    {
+      gtk_widget_show (label);
+    }
+
+  return TRUE;
+}
+
+static void
+level_scale_value_changed (GtkRange *range)
+{
+  GtkWidget *dialog = gtk_widget_get_ancestor (GTK_WIDGET (range), GTK_TYPE_DIALOG);
+  GtkWidget *sw = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), "mode_switch"));
+  GtkWidget *label = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), "error_label"));
+
+  if (gtk_switch_get_active (GTK_SWITCH (sw)) &&
+      !gtk_switch_get_state (GTK_SWITCH (sw)) &&
+      (gtk_range_get_value (range) > 50))
+    {
+      gtk_widget_hide (label);
+      gtk_switch_set_state (GTK_SWITCH (sw), TRUE);
+    }
+  else if (gtk_switch_get_state (GTK_SWITCH (sw)) &&
+          (gtk_range_get_value (range) <= 50))
+    {
+      gtk_switch_set_state (GTK_SWITCH (sw), FALSE);
+    }
+}
+
 static void
 activate (GApplication *app)
 {
@@ -1722,6 +1782,9 @@ activate (GApplication *app)
   gtk_builder_add_callback_symbol (builder, "reset_icon_size", (GCallback)reset_icon_size);
   gtk_builder_add_callback_symbol (builder, "scale_format_value", (GCallback)scale_format_value);
   gtk_builder_add_callback_symbol (builder, "scale_format_value_blank", (GCallback)scale_format_value_blank);
+  gtk_builder_add_callback_symbol (builder, "validate_more_details", (GCallback)validate_more_details);
+  gtk_builder_add_callback_symbol (builder, "mode_switch_state_set", (GCallback)mode_switch_state_set);
+  gtk_builder_add_callback_symbol (builder, "level_scale_value_changed", 
(GCallback)level_scale_value_changed);
 
   gtk_builder_connect_signals (builder, NULL);
 
@@ -1820,6 +1883,13 @@ activate (GApplication *app)
   widget = (GtkWidget *)gtk_builder_get_object (builder, "circular_button");
   g_signal_connect (widget, "clicked", G_CALLBACK (show_dialog), dialog);
 
+  widget = (GtkWidget *)gtk_builder_get_object (builder, "level_scale");
+  g_object_set_data (G_OBJECT (dialog), "level_scale", widget);
+  widget = (GtkWidget *)gtk_builder_get_object (builder, "mode_switch");
+  g_object_set_data (G_OBJECT (dialog), "mode_switch", widget);
+  widget = (GtkWidget *)gtk_builder_get_object (builder, "error_label");
+  g_object_set_data (G_OBJECT (dialog), "error_label", widget);
+
   dialog = (GtkWidget *)gtk_builder_get_object (builder, "selection_dialog");
   g_object_set_data (G_OBJECT (window), "selection_dialog", dialog);
   widget = (GtkWidget *)gtk_builder_get_object (builder, "text3");
diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui
index df5bb617e4..ab70d764d4 100644
--- a/demos/widget-factory/widget-factory.ui
+++ b/demos/widget-factory/widget-factory.ui
@@ -3989,6 +3989,7 @@ bad things might happen.</property>
               <packing>
                 <property name="left-attach">1</property>
                 <property name="top-attach">0</property>
+                <property name="width">2</property>
               </packing>
             </child>
             <child>
@@ -4012,10 +4013,12 @@ bad things might happen.</property>
               <object class="GtkEntry" id="more_details_entry">
                 <property name="visible">1</property>
                 <property name="valign">baseline</property>
+                <signal name="notify::text" handler="validate_more_details" object="details_entry" 
swapped="no"/>
               </object>
               <packing>
                 <property name="left-attach">1</property>
                 <property name="top-attach">1</property>
+                <property name="width">2</property>
               </packing>
             </child>
             <child>
@@ -4041,10 +4044,12 @@ bad things might happen.</property>
                 <property name="valign">baseline</property>
                 <property name="draw-value">0</property>
                 <property name="adjustment">adjustment1</property>
+                <signal name="value-changed" handler="level_scale_value_changed"/>
               </object>
               <packing>
                 <property name="left-attach">1</property>
                 <property name="top-attach">2</property>
+                <property name="width">2</property>
               </packing>
             </child>
             <child>
@@ -4069,12 +4074,28 @@ bad things might happen.</property>
                 <property name="visible">1</property>
                 <property name="halign">start</property>
                 <property name="valign">baseline</property>
+                <signal name="state-set" handler="mode_switch_state_set"/>
               </object>
               <packing>
                 <property name="left-attach">1</property>
                 <property name="top-attach">3</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkLabel" id="error_label">
+                <property name="visible">0</property>
+                <property name="halign">start</property>
+                <property name="valign">baseline</property>
+                <property name="label">Level too low</property>
+                <style>
+                  <class name="error"/>
+                </style>
+              </object>
+              <packing>
+                <property name="left-attach">2</property>
+                <property name="top-attach">3</property>
+              </packing>
+            </child>
           </object>
         </child>
       </object>


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