[ekiga/ds-gtk-application] GmEntry: Only emit the activated signal when content is not empty.



commit 2ac6bd98356348775402b5a7115082635aff7847
Author: Damien Sandras <dsandras seconix com>
Date:   Sun Oct 26 16:12:32 2014 +0100

    GmEntry: Only emit the activated signal when content is not empty.
    
    + Code cleanups.

 lib/gui/gm-entry.c |   43 ++++++++++++++++++++++++++++---------------
 lib/gui/gm-entry.h |   16 ++++++++++------
 2 files changed, 38 insertions(+), 21 deletions(-)
---
diff --git a/lib/gui/gm-entry.c b/lib/gui/gm-entry.c
index 71ac7d9..0c2a737 100644
--- a/lib/gui/gm-entry.c
+++ b/lib/gui/gm-entry.c
@@ -48,6 +48,7 @@ struct _GmEntryPrivate {
   gchar *regex_string;
   gchar *activate_icon;
   gboolean is_valid;
+  gboolean is_empty;
   gboolean allow_empty;
 };
 
@@ -86,6 +87,8 @@ static void gm_entry_update_clear_icon (GmEntry *self);
 
 static void gm_entry_update_activate_icon (GmEntry *self);
 
+static gboolean gm_entry_text_is_empty (GmEntry *self);
+
 
 /* Static GObject functions and declarations */
 static void gm_entry_class_init (GmEntryClass *);
@@ -112,6 +115,7 @@ gm_entry_changed_cb (GmEntry *self,
 {
   g_return_if_fail (GM_IS_ENTRY (self));
   gboolean is_valid = gm_entry_text_is_valid (self);
+  self->priv->is_empty = gm_entry_text_is_empty (self);
 
   if (is_valid != self->priv->is_valid) {
     self->priv->is_valid = is_valid;
@@ -128,7 +132,7 @@ gm_entry_activated_cb (GmEntry *self,
                        G_GNUC_UNUSED gpointer data)
 {
   g_return_if_fail (GM_IS_ENTRY (self));
-  if (self->priv->is_valid)
+  if (self->priv->is_valid && !self->priv->is_empty)
     g_signal_emit (self, signals[ACTIVATED_SIGNAL], 0);
 }
 
@@ -322,6 +326,7 @@ gm_entry_init (GmEntry* self)
   self->priv->regex_string = NULL;
   self->priv->activate_icon = NULL;
   self->priv->is_valid = gm_entry_text_is_valid (self);
+  self->priv->is_empty = gm_entry_text_is_empty (self);
 
   gm_entry_update_activate_icon (self);
   gm_entry_update_clear_icon (self);
@@ -341,17 +346,13 @@ gm_entry_update_clear_icon (GmEntry *self)
 {
   g_return_if_fail (GM_IS_ENTRY (self));
 
-  gboolean empty = (gtk_entry_get_text_length (GTK_ENTRY (self)) == 0);
+  gboolean has_content = (gtk_entry_get_text_length (GTK_ENTRY (self)) == 0);
   gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL);
 
-  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
-                                            GM_TYPE_ENTRY,
-                                           GmEntryPrivate);
-
   g_object_set (self,
-                "secondary-icon-name", !empty ? (rtl ? "edit-clear-rtl-symbolic" : "edit-clear-symbolic") : 
NULL,
-                "secondary-icon-activatable", !empty,
-                "secondary-icon-sensitive", !empty,
+                "secondary-icon-name", !has_content ? (rtl ? "edit-clear-rtl-symbolic" : 
"edit-clear-symbolic") : NULL,
+                "secondary-icon-activatable", !has_content,
+                "secondary-icon-sensitive", !has_content,
                 NULL);
 }
 
@@ -359,20 +360,32 @@ gm_entry_update_clear_icon (GmEntry *self)
 static void
 gm_entry_update_activate_icon (GmEntry *self)
 {
-  const gchar *content = gtk_entry_get_text (GTK_ENTRY (self));
-  gchar *value = g_strdup (content);
-  value = g_strstrip (value);
-
-  gboolean empty = (!g_strcmp0 (value, ""));
-  gboolean ok = (!empty && self->priv->is_valid);
+  gboolean ok = (!self->priv->is_empty && self->priv->is_valid);
 
   g_object_set (self,
                 "primary-icon-name", ok ? self->priv->activate_icon : NULL,
                 "primary-icon-activatable", ok,
                 "primary-icon-sensitive", ok,
                 NULL);
+}
+
+
+static gboolean
+gm_entry_text_is_empty (GmEntry *self)
+{
+  gboolean empty = FALSE;
+
+  g_return_val_if_fail (GM_IS_ENTRY (self), TRUE);
+
+  const gchar *content = gtk_entry_get_text (GTK_ENTRY (self));
+  gchar *value = g_strdup (content);
+  value = g_strstrip (value);
+
+  empty = (!g_strcmp0 (value, ""));
 
   g_free (value);
+
+  return empty;
 }
 
 
diff --git a/lib/gui/gm-entry.h b/lib/gui/gm-entry.h
index 9229c6b..c0dc2ab 100644
--- a/lib/gui/gm-entry.h
+++ b/lib/gui/gm-entry.h
@@ -110,18 +110,22 @@ const gchar *gm_entry_get_activate_icon (GmEntry *self);
 /* Signals emitted by that widget :
  *
  * - "validity-changed": Emitted when the GmEntry validity changes.
- * - "activated"       : Emitted when the entry is activated and its content is
- *                       valid. This is similar to the native "activate" signal.
+ * - "activated"       : Emitted when the entry is activated and:
+ *                        - its content matches the regex (is valid).
+ *                        - its content is not empty (independantly of the
+ *                          "allow-empty" property value. There must be
+ *                          something to activate.
+ *                       This signal is similar to the native "activate" signal.
  *                       However, the native signal will be emitted even if the
- *                       GmEntry content is invalid.
+ *                       GmEntry content is invalid or empty.
  *
  */
 
 /* Properties of that widget :
  *
- * - "allow-empty"  :   Defaults to TRUE.
- * - "activate-icon":   Icon that appears when the entry content is not empty
- *                      and valid. Clicking on it emits the activated signal.
+ * - "allow-empty"  :  Defaults to TRUE.
+ * - "activate-icon":  Icon that appears when the entry content is not empty
+ *                     and valid. Clicking on it emits the activated signal.
  * - "regex"        :  Set the regex string to use for validity checking.
  *
  */


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