[gtk+/popover-menu: 10/17] GtkModelButton: Don't require an explicit role



commit 0918e5acde339d192a4f4474187a3ffa15759681
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Oct 26 01:07:59 2014 -0400

    GtkModelButton: Don't require an explicit role
    
    When we have an action-name, we can deduce the role from the
    action that is looked up by the action helper. This lets use
    remove all explicit role setting from the testpopover example.
    
    Also, rename the enumeration to GtkButtonRole.

 gtk/gtkactionhelper.c |   41 +++++++++++++++++++++++++++--
 gtk/gtkmodelbutton.c  |   69 +++++++++++++++++++++++++-----------------------
 gtk/gtkmodelbutton.h  |    8 +++---
 tests/popover2.ui     |   10 -------
 4 files changed, 78 insertions(+), 50 deletions(-)
---
diff --git a/gtk/gtkactionhelper.c b/gtk/gtkactionhelper.c
index 6ba5985..a9276d5 100644
--- a/gtk/gtkactionhelper.c
+++ b/gtk/gtkactionhelper.c
@@ -23,6 +23,8 @@
 #include "gtkwidget.h"
 #include "gtkwidgetprivate.h"
 #include "gtkdebug.h"
+#include "gtkmodelbutton.h"
+#include "gtktypebuiltins.h"
 
 #include <string.h>
 
@@ -66,6 +68,8 @@ struct _GtkActionHelper
   gboolean enabled;
   gboolean active;
 
+  GtkButtonRole role;
+
   gint reporting;
 };
 
@@ -74,6 +78,7 @@ enum
   PROP_0,
   PROP_ENABLED,
   PROP_ACTIVE,
+  PROP_ROLE,
   N_PROPS
 };
 
@@ -107,6 +112,17 @@ gtk_action_helper_report_change (GtkActionHelper *helper,
       }
       break;
 
+    case PROP_ROLE:
+      {
+        GParamSpec *pspec;
+
+        pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (helper->widget), "role");
+
+        if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == GTK_TYPE_BUTTON_ROLE)
+          g_object_set (G_OBJECT (helper->widget), "role", helper->role, NULL);
+      }
+      break;
+
     default:
       g_assert_not_reached ();
     }
@@ -146,10 +162,19 @@ gtk_action_helper_action_added (GtkActionHelper    *helper,
     GTK_NOTE(ACTIONS, g_message("actionhelper: %s found and enabled", helper->action_name));
 
   if (helper->target != NULL && state != NULL)
-    helper->active = g_variant_equal (state, helper->target);
-
+    {
+      helper->active = g_variant_equal (state, helper->target);
+      helper->role = GTK_BUTTON_ROLE_RADIO;
+    }
   else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
-    helper->active = g_variant_get_boolean (state);
+    {
+      helper->active = g_variant_get_boolean (state);
+      helper->role = GTK_BUTTON_ROLE_CHECK;
+    }
+  else
+    {
+      helper->role = GTK_BUTTON_ROLE_NORMAL;
+    }
 
   if (should_emit_signals)
     {
@@ -158,6 +183,8 @@ gtk_action_helper_action_added (GtkActionHelper    *helper,
 
       if (helper->active)
         gtk_action_helper_report_change (helper, PROP_ACTIVE);
+
+      gtk_action_helper_report_change (helper, PROP_ROLE);
     }
 }
 
@@ -242,6 +269,10 @@ gtk_action_helper_get_property (GObject *object, guint prop_id,
       g_value_set_boolean (value, helper->active);
       break;
 
+    case PROP_ROLE:
+      g_value_set_enum (value, helper->role);
+      break;
+
     default:
       g_assert_not_reached ();
     }
@@ -313,6 +344,10 @@ gtk_action_helper_class_init (GtkActionHelperClass *class)
                                                                  G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
   gtk_action_helper_pspecs[PROP_ACTIVE] = g_param_spec_boolean ("active", "active", "active", FALSE,
                                                                 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  gtk_action_helper_pspecs[PROP_ROLE] = g_param_spec_enum ("role", "role", "role",
+                                                           GTK_TYPE_BUTTON_ROLE,
+                                                           GTK_BUTTON_ROLE_NORMAL,
+                                                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_properties (class, N_PROPS, gtk_action_helper_pspecs);
 }
 
diff --git a/gtk/gtkmodelbutton.c b/gtk/gtkmodelbutton.c
index e31cdba..0d55b4e 100644
--- a/gtk/gtkmodelbutton.c
+++ b/gtk/gtkmodelbutton.c
@@ -46,7 +46,7 @@ struct _GtkModelButton
   gboolean inverted;
   gboolean iconic;
   gchar *menu_name;
-  GtkModelButtonRole role;
+  GtkButtonRole role;
 };
 
 typedef GtkButtonClass GtkModelButtonClass;
@@ -70,8 +70,27 @@ enum
 static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
 
 static void
-gtk_model_button_set_role (GtkModelButton     *button,
-                           GtkModelButtonRole  role)
+gtk_model_button_update_state (GtkModelButton *button)
+{
+  GtkStateFlags state;
+
+  if (button->role == GTK_BUTTON_ROLE_NORMAL)
+    return;
+
+  state = gtk_widget_get_state_flags (GTK_WIDGET (button));
+
+  state &= ~GTK_STATE_FLAG_CHECKED;
+
+  if (button->active && !button->menu_name)
+    state |= GTK_STATE_FLAG_CHECKED;
+
+  gtk_widget_set_state_flags (GTK_WIDGET (button), state, TRUE);
+}
+
+
+static void
+gtk_model_button_set_role (GtkModelButton *button,
+                           GtkButtonRole   role)
 {
   AtkObject *accessible;
   AtkRole a11y_role;
@@ -80,20 +99,19 @@ gtk_model_button_set_role (GtkModelButton     *button,
     return;
 
   button->role = role;
-  gtk_widget_queue_draw (GTK_WIDGET (button));
 
   accessible = gtk_widget_get_accessible (GTK_WIDGET (button));
   switch (role)
     {
-    case GTK_MODEL_BUTTON_ROLE_NORMAL:
+    case GTK_BUTTON_ROLE_NORMAL:
       a11y_role = ATK_ROLE_PUSH_BUTTON;
       break;
 
-    case GTK_MODEL_BUTTON_ROLE_CHECK:
+    case GTK_BUTTON_ROLE_CHECK:
       a11y_role = ATK_ROLE_CHECK_BOX;
       break;
 
-    case GTK_MODEL_BUTTON_ROLE_RADIO:
+    case GTK_BUTTON_ROLE_RADIO:
       a11y_role = ATK_ROLE_RADIO_BUTTON;
       break;
 
@@ -102,6 +120,10 @@ gtk_model_button_set_role (GtkModelButton     *button,
     }
 
   atk_object_set_role (accessible, a11y_role);
+
+  gtk_model_button_update_state (button);
+  gtk_widget_queue_draw (GTK_WIDGET (button));
+  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ROLE]);
 }
 
 static void
@@ -136,25 +158,6 @@ gtk_model_button_set_text (GtkModelButton *button,
 }
 
 static void
-gtk_model_button_update_state (GtkModelButton *button)
-{
-  GtkStateFlags state;
-
-  if (button->role == GTK_MODEL_BUTTON_ROLE_NORMAL)
-    return;
-
-  state = gtk_widget_get_state_flags (GTK_WIDGET (button));
-
-  state &= ~GTK_STATE_FLAG_CHECKED;
-
-  if (button->active && !button->menu_name)
-    state |= GTK_STATE_FLAG_CHECKED;
-
-  gtk_widget_set_state_flags (GTK_WIDGET (button), state, TRUE);
-}
-
-
-static void
 gtk_model_button_set_active (GtkModelButton *button,
                              gboolean        active)
 {
@@ -379,7 +382,7 @@ has_sibling_with_indicator (GtkWidget *button)
         continue;
 
       if (!sibling->centered &&
-          (sibling->menu_name || sibling->role != GTK_MODEL_BUTTON_ROLE_NORMAL))
+          (sibling->menu_name || sibling->role != GTK_BUTTON_ROLE_NORMAL))
         {
           has_indicator = TRUE;
           break;
@@ -394,7 +397,7 @@ has_sibling_with_indicator (GtkWidget *button)
 static gboolean
 needs_indicator (GtkModelButton *button)
 {
-  if (button->role != GTK_MODEL_BUTTON_ROLE_NORMAL)
+  if (button->role != GTK_BUTTON_ROLE_NORMAL)
     return TRUE;
 
   return has_sibling_with_indicator (GTK_WIDGET (button));
@@ -688,14 +691,14 @@ gtk_model_button_draw (GtkWidget *widget,
       gtk_render_expander (context, cr, x, y, indicator_size, indicator_size);
       gtk_style_context_restore (context);
     }
-  else if (model_button->role == GTK_MODEL_BUTTON_ROLE_CHECK)
+  else if (model_button->role == GTK_BUTTON_ROLE_CHECK)
     {
       gtk_style_context_save (context);
       gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
       gtk_render_check (context, cr, x, y, indicator_size, indicator_size);
       gtk_style_context_restore (context);
     }
-  else if (model_button->role == GTK_MODEL_BUTTON_ROLE_RADIO)
+  else if (model_button->role == GTK_BUTTON_ROLE_RADIO)
     {
       gtk_style_context_save (context);
       gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
@@ -735,7 +738,7 @@ gtk_model_button_clicked (GtkButton *button)
       if (stack)
         gtk_stack_set_visible_child_name (GTK_STACK (stack), model_button->menu_name);
     }
-  else if (model_button->role == GTK_MODEL_BUTTON_ROLE_NORMAL)
+  else if (model_button->role == GTK_BUTTON_ROLE_NORMAL)
     {
       GtkWidget *popover;
 
@@ -767,8 +770,8 @@ gtk_model_button_class_init (GtkModelButtonClass *class)
 
   properties[PROP_ROLE] =
     g_param_spec_enum ("role", P_("Role"), P_("The role of this button"),
-                       GTK_TYPE_MODEL_BUTTON_ROLE,
-                       GTK_MODEL_BUTTON_ROLE_NORMAL,
+                       GTK_TYPE_BUTTON_ROLE,
+                       GTK_BUTTON_ROLE_NORMAL,
                        G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
   properties[PROP_ICON] = 
     g_param_spec_object ("icon", P_("Icon"), P_("The icon"),
diff --git a/gtk/gtkmodelbutton.h b/gtk/gtkmodelbutton.h
index ce1d2e9..154c932 100644
--- a/gtk/gtkmodelbutton.h
+++ b/gtk/gtkmodelbutton.h
@@ -37,10 +37,10 @@ G_BEGIN_DECLS
 typedef struct _GtkModelButton        GtkModelButton;
 
 typedef enum {
-  GTK_MODEL_BUTTON_ROLE_NORMAL,
-  GTK_MODEL_BUTTON_ROLE_CHECK,
-  GTK_MODEL_BUTTON_ROLE_RADIO
-} GtkModelButtonRole;
+  GTK_BUTTON_ROLE_NORMAL,
+  GTK_BUTTON_ROLE_CHECK,
+  GTK_BUTTON_ROLE_RADIO
+} GtkButtonRole;
 
 GDK_AVAILABLE_IN_3_16
 GType       gtk_model_button_get_type (void) G_GNUC_CONST;
diff --git a/tests/popover2.ui b/tests/popover2.ui
index 22e7eb9..4b85bc3 100644
--- a/tests/popover2.ui
+++ b/tests/popover2.ui
@@ -69,7 +69,6 @@
       <object class="GtkModelButton">
         <property name="visible">True</property>
         <property name="action-name">top.action2</property>
-        <property name="role">check</property>
         <property name="text">Toggle</property>
       </object>
     </child>
@@ -77,7 +76,6 @@
       <object class="GtkModelButton">
         <property name="visible">True</property>
         <property name="action-name">top.action2a</property>
-        <property name="role">check</property>
         <property name="text">Another Toggle</property>
       </object>
     </child>
@@ -112,7 +110,6 @@
         <property name="visible">True</property>
         <property name="action-name">top.action3</property>
         <property name="action-target">'three'</property>
-        <property name="role">radio</property>
         <property name="text">Radio 1</property>
       </object>
     </child>
@@ -121,7 +118,6 @@
         <property name="visible">True</property>
         <property name="action-name">top.action3</property>
         <property name="action-target">'four'</property>
-        <property name="role">radio</property>
         <property name="text">Radio 2</property>
       </object>
     </child>
@@ -266,7 +262,6 @@
             <property name="text">List</property>
             <property name="action-name">top.set-view</property>
             <property name="action-target">'list'</property>
-            <property name="role">radio</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
             <property name="icon">icon-list</property>
@@ -281,7 +276,6 @@
             <property name="text">Grid</property>
             <property name="action-name">top.set-view</property>
             <property name="action-target">'grid'</property>
-            <property name="role">radio</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
             <property name="icon">icon-grid</property>
@@ -358,7 +352,6 @@
             <property name="visible">True</property>
             <property name="text">Bold</property>
             <property name="action-name">top.bold</property>
-            <property name="role">check</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
           </object>
@@ -371,7 +364,6 @@
             <property name="visible">True</property>
             <property name="text">Italic</property>
             <property name="action-name">top.italic</property>
-            <property name="role">check</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
             <property name="icon">icon-italic</property>
@@ -385,7 +377,6 @@
             <property name="visible">True</property>
             <property name="text">Strikethrough</property>
             <property name="action-name">top.strikethrough</property>
-            <property name="role">check</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
             <property name="icon">icon-strikethrough</property>
@@ -399,7 +390,6 @@
             <property name="visible">True</property>
             <property name="text">Underline</property>
             <property name="action-name">top.underline</property>
-            <property name="role">check</property>
             <property name="iconic">True</property>
             <property name="centered">True</property>
             <property name="icon">icon-underline</property>


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