[gimp/soc-2011-gimpunitentry] GimpUnitEntry: add timeout for error-indication



commit 72b07645aa22ed920699df1e2680b696b0e7613c
Author: Enrico SchroÌder <enni schroeder gmail com>
Date:   Tue Jul 19 13:25:55 2011 +0200

    GimpUnitEntry: add timeout for error-indication

 libgimpwidgets/gimpunitentry.c |   52 ++++++++++++++++++++++++++++++++++-----
 libgimpwidgets/gimpunitentry.h |    5 ++++
 2 files changed, 50 insertions(+), 7 deletions(-)
---
diff --git a/libgimpwidgets/gimpunitentry.c b/libgimpwidgets/gimpunitentry.c
index f897c00..f5ccdda 100644
--- a/libgimpwidgets/gimpunitentry.c
+++ b/libgimpwidgets/gimpunitentry.c
@@ -46,7 +46,9 @@
 #define DEBUG(x) /* nothing */
 #endif
 
-#define UNIT_ENTRY_STRING_LENGTH 30
+#define UNIT_ENTRY_STRING_LENGTH  30
+#define UNIT_ENTRY_ERROR_TIMEOUT  2
+#define UNIT_ENTRY_ERROR_COLOR    "LightSalmon"
 
 G_DEFINE_TYPE (GimpUnitEntry, gimp_unit_entry, GTK_TYPE_SPIN_BUTTON);
 
@@ -64,6 +66,7 @@ static void     gimp_unit_entry_populate_popup  (GtkEntry               *entry,
                                                  gpointer                user_data); 
 static void     gimp_unit_entry_menu_item       (GtkWidget              *menuItem,
                                                  gpointer               *user_data);
+static gboolean gimp_unit_entry_timer_callback  (GtkWidget              *entry);                                               
 
 static void
 gimp_unit_entry_init (GimpUnitEntry *unitEntry)
@@ -77,7 +80,9 @@ gimp_unit_entry_init (GimpUnitEntry *unitEntry)
 
   /* some default values */
   unitEntry->dontUpdateText = FALSE;  
-  unitEntry->mode           = GIMP_UNIT_ENTRY_MODE_UNIT;                               
+  unitEntry->mode           = GIMP_UNIT_ENTRY_MODE_UNIT;  
+  unitEntry->input_valid    = TRUE; 
+  unitEntry->timer          = NULL;                            
 
   /* connect signals */
   /* we don't need all of them... */
@@ -146,6 +151,7 @@ gimp_unit_entry_parse (GimpUnitEntry *entry)
   {
     /* reset color */
     gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, NULL);
+    entry->input_valid = TRUE;
 
     /* set new unit */  
     if (result.unit != entry->unitAdjustment->unit)
@@ -169,11 +175,7 @@ gimp_unit_entry_parse (GimpUnitEntry *entry)
   }
   else
   {
-    /* paint entry red */
-    GdkColor color;
-    gdk_color_parse ("LightSalmon", &color);
-    gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, &color);
-
+    entry->input_valid = FALSE;
     return FALSE;
   }
 
@@ -225,6 +227,23 @@ gimp_unit_entry_text_changed (GtkEditable *editable,
 
   DEBUG (("on_text_changed\n");)
 
+  /* timer for error indication */
+  if (gtk_widget_has_focus (GTK_WIDGET (entry)))
+  {
+    /* if timer exists, reset */
+    if (entry->timer != NULL)
+    {
+      g_source_destroy (entry->timer);
+    }
+    /* create timer */
+    entry->timer = g_timeout_source_new_seconds (UNIT_ENTRY_ERROR_TIMEOUT);
+    g_source_set_callback (entry->timer, 
+                           (GSourceFunc) gimp_unit_entry_timer_callback,
+                           (gpointer) entry,
+                           NULL);
+    g_source_attach (entry->timer, NULL);
+  }
+
   if (!entry->mode == GIMP_UNIT_ENTRY_MODE_RESOLUTION)
   {
     /* disable updating the displayed text (user input must not be overwriten) */
@@ -254,6 +273,25 @@ gimp_unit_entry_input (GtkSpinButton *spinButton,
   return FALSE;
 }
 
+static gboolean 
+gimp_unit_entry_timer_callback  (GtkWidget *entry)                                   
+{
+  GimpUnitEntry *unit_entry = GIMP_UNIT_ENTRY (entry);
+
+  /* paint entry red if input is invalid */
+  if (!unit_entry->input_valid)
+  {
+    GdkColor color;
+    gdk_color_parse (UNIT_ENTRY_ERROR_COLOR, &color);
+    gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, &color);
+  }
+
+  /* delete timer */
+  unit_entry->timer = NULL;
+
+  return FALSE;
+}
+
 static void 
 gimp_unit_entry_populate_popup (GtkEntry *entry,
                                 GtkMenu  *menu,
diff --git a/libgimpwidgets/gimpunitentry.h b/libgimpwidgets/gimpunitentry.h
index 748acf8..7f6a691 100644
--- a/libgimpwidgets/gimpunitentry.h
+++ b/libgimpwidgets/gimpunitentry.h
@@ -60,6 +60,11 @@ struct _GimpUnitEntry
 
   /* input mode */
   GimpUnitEntryMode   mode;
+
+  /* is our input valid? (for error indication) */
+  gboolean            input_valid;
+  /* the timer source which handles the error indication */
+  GSource             *timer;
 };
 
 struct _GimpUnitEntryClass



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