[gimp/soc-2011-gimpunitentry] GimpUnitEntry: changed input handling



commit 689f29f72e7dac787d235e5635ec71ab60b20d53
Author: Enrico SchroÌ?der <enni schroeder gmail com>
Date:   Mon Jun 13 14:29:41 2011 +0200

    GimpUnitEntry: changed input handling
    
    Changed the input handling and the events on which to parse and update. The changes make us get rid of the need to parse on the "output" signal of GtkSpinButton and all problems related to that.

 libgimpwidgets/gimpunitadjustment.c |    4 --
 libgimpwidgets/gimpunitadjustment.h |    3 -
 libgimpwidgets/gimpunitentry.c      |   72 ++++++++--------------------------
 libgimpwidgets/gimpunitentry.h      |    3 +-
 4 files changed, 19 insertions(+), 63 deletions(-)
---
diff --git a/libgimpwidgets/gimpunitadjustment.c b/libgimpwidgets/gimpunitadjustment.c
index a762c4f..c9e1b7b 100644
--- a/libgimpwidgets/gimpunitadjustment.c
+++ b/libgimpwidgets/gimpunitadjustment.c
@@ -61,12 +61,10 @@ gimp_unit_adjustment_init (GimpUnitAdjustment *unitAdjustment)
   gtk_adjustment_set_upper (&unitAdjustment->parent_instance, 10000000.0);
   gtk_adjustment_set_step_increment (&unitAdjustment->parent_instance, 1.0);
   gtk_adjustment_set_page_increment (&unitAdjustment->parent_instance, 10.0);
-  unitAdjustment->unitChanged = FALSE;
 
   /* default unit, resolution */
   unitAdjustment->unit        = DEFAULT_UNIT;
   unitAdjustment->resolution  = DEFAULT_RESOLUTION;
-  unitAdjustment->unitChanged = FALSE;
 }
 
 static void
@@ -106,8 +104,6 @@ unit_changed_handler (GimpUnitAdjustment *adj, GimpUnit unit, gpointer userData)
 {
   GimpUnitAdjustment *adjustment = GIMP_UNIT_ADJUSTMENT (userData);
 
-  adjustment->unitChanged = TRUE;
-
   gimp_unit_adjustment_convert_unit (adjustment, unit);
 }
 
diff --git a/libgimpwidgets/gimpunitadjustment.h b/libgimpwidgets/gimpunitadjustment.h
index 728afb0..7514953 100644
--- a/libgimpwidgets/gimpunitadjustment.h
+++ b/libgimpwidgets/gimpunitadjustment.h
@@ -43,9 +43,6 @@ struct _GimpUnitAdjustment
 {
   GtkAdjustment parent_instance;
 
-  /* flag set when unit has been changed externally */
-  gboolean      unitChanged; 
-
   /* private */
   /* TODO move private fields into own struct? */
   GimpUnit  unit;           /* the unit our value is in */
diff --git a/libgimpwidgets/gimpunitentry.c b/libgimpwidgets/gimpunitentry.c
index 5359c64..d14b58c 100644
--- a/libgimpwidgets/gimpunitentry.c
+++ b/libgimpwidgets/gimpunitentry.c
@@ -103,8 +103,7 @@ gimp_unit_entry_init (GimpUnitEntry *unitEntry)
                                   GTK_ADJUSTMENT (adjustment));
 
   /* some default values */
-  unitEntry->buttonPressed = FALSE;        
-  unitEntry->scrolling = FALSE;                                   
+  unitEntry->dontUpdateText = FALSE;                                 
 
   /* connect signals */
   /* we don't need all of them... */
@@ -136,6 +135,7 @@ gimp_unit_entry_class_init (GimpUnitEntryClass *class)
   GtkWidgetClass   *widgetClass = GTK_WIDGET_CLASS (class);
 
   /* some events we need to catch before our parent */
+  /* FIXME: hopefully we don't need them anymore with the latest changes... */
   widgetClass->button_press_event   = gimp_unit_entry_button_press;
   widgetClass->button_release_event = gimp_unit_entry_button_release;
   widgetClass->scroll_event         = gimp_unit_entry_scroll;
@@ -234,31 +234,13 @@ on_output (GtkSpinButton *spin, gpointer data)
   GimpUnitAdjustment *adj   = GIMP_UNIT_ADJUSTMENT (data);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (spin); 
 
-  /* return if widget still has focus => user input must not be overwritten */
-  if (gtk_widget_has_focus (GTK_WIDGET (spin)) && 
-      !entry->buttonPressed &&
-      !entry->scrolling)
+  /* if updating disabled => return (user input must not be overwritten) */
+  if (entry->dontUpdateText)
   {
     return TRUE;
   }
-
-  /* parse once more to prevent value from being overwritten somewhere in GtkSpinButton or 
-     GtkEntry. If we don't do that, the entered text is truncated at the first space.
-     TODO: find out where and why
-     not very elegant, because we have do deactivate parsing in case the value was modified
-     due to a unit change or up/down buttons or keys
-  */
-  if(adj->unitChanged || entry->buttonPressed || entry->scrolling)
-  {
-    /* reset certain flags */
-    if(adj->unitChanged)
-      adj->unitChanged = FALSE;
-    if(entry->scrolling)
-      entry->scrolling = FALSE;
-  }
-  else
-    gimp_unit_entry_parse (GIMP_UNIT_ENTRY (spin));
   
+  /* set text of the entry */
   text = gimp_unit_adjustment_to_string (adj);
 
   DEBUG (("on_output: %s\n", text);)
@@ -288,7 +270,12 @@ void on_text_changed (GtkEditable *editable, gpointer user_data)
 
   DEBUG (("on_text_changed\n");)
 
+  /* disable updating the displayed text (user input must not be overwriten) */
+  entry->dontUpdateText = TRUE;
+  /* parse input */
   gimp_unit_entry_parse (entry);
+  /* reenable updating */
+  entry->dontUpdateText = FALSE;
 }
 
 static 
@@ -296,6 +283,13 @@ gint on_input        (GtkSpinButton *spinButton,
                       gpointer       arg1,
                       gpointer       user_data)
 {
+  /* parse and set value ourselves before GtkSpinButton does so, because
+     GtkSpinButton would truncate our input and ignore parts of it */
+  gimp_unit_entry_parse (GIMP_UNIT_ENTRY (spinButton));
+  on_output (spinButton, (gpointer)GIMP_UNIT_ENTRY(spinButton)->unitAdjustment);
+
+  /* we want GtkSpinButton to handle the input nontheless (there is no problem anymore
+     since we done the parsing), so we return FALSE */
   return FALSE;
 }
 
@@ -360,9 +354,6 @@ gimp_unit_entry_button_press (GtkWidget          *widget,
   GtkSpinButtonClass *class = GTK_SPIN_BUTTON_CLASS (gimp_unit_entry_parent_class);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (widget);
 
-  /* disable output (i.e. parsing and overwriting of our value) while button pressed */
-  entry->buttonPressed = TRUE;
-   
   return GTK_WIDGET_CLASS(class)->button_press_event (widget, event);
 }
 static gint
@@ -371,9 +362,6 @@ gimp_unit_entry_button_release (GtkWidget          *widget,
 {
   GtkSpinButtonClass *class = GTK_SPIN_BUTTON_CLASS (gimp_unit_entry_parent_class);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (widget);
-
-  /* reenable output */
-  entry->buttonPressed = FALSE;
    
   return GTK_WIDGET_CLASS(class)->button_release_event (widget, event);
 }
@@ -385,8 +373,6 @@ gimp_unit_entry_scroll (GtkWidget          *widget,
   GtkSpinButtonClass *class = GTK_SPIN_BUTTON_CLASS (gimp_unit_entry_parent_class);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (widget);
 
-  entry->scrolling = TRUE;  
-
   return GTK_WIDGET_CLASS(class)->scroll_event (widget, event);
 }
 
@@ -396,18 +382,6 @@ gimp_unit_entry_key_press (GtkWidget          *widget,
 {
   GtkSpinButtonClass *class = GTK_SPIN_BUTTON_CLASS (gimp_unit_entry_parent_class);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (widget);
-
-  /* disable output for up/down keys */
-  switch (event->keyval)
-  {
-    case GDK_Up:
-    case GDK_Down:
-      entry->buttonPressed = TRUE;
-      break;
-
-    default:
-      break;
-  }
    
   return GTK_WIDGET_CLASS(class)->key_press_event (widget, event);
 }
@@ -417,18 +391,6 @@ gimp_unit_entry_key_release (GtkWidget          *widget,
 {
   GtkSpinButtonClass *class = GTK_SPIN_BUTTON_CLASS (gimp_unit_entry_parent_class);
   GimpUnitEntry      *entry = GIMP_UNIT_ENTRY (widget);
-
-  /* reenable output */
-  switch (event->keyval)
-  {
-    case GDK_Up:
-    case GDK_Down:
-      entry->buttonPressed = FALSE;
-      break;
-      
-    default:
-      break;
-  }
    
   return GTK_WIDGET_CLASS(class)->key_release_event (widget, event);
 }
diff --git a/libgimpwidgets/gimpunitentry.h b/libgimpwidgets/gimpunitentry.h
index 81358ab..b623543 100644
--- a/libgimpwidgets/gimpunitentry.h
+++ b/libgimpwidgets/gimpunitentry.h
@@ -52,6 +52,7 @@ struct _GimpUnitEntry
      our parsing and display the changed value */
   gboolean          buttonPressed;    
   gboolean          scrolling;
+  gboolean          dontUpdateText;
 
   const gchar *id; /* identifier string of unit entry (used by GimpUnitEntryTable) */
 };
@@ -73,7 +74,7 @@ void gimp_unit_entry_connect (GimpUnitEntry *entry, GimpUnitEntry *target);
 const gchar* gimp_unit_entry_get_id (GimpUnitEntry *entry);
 void gimp_unit_entry_set_unit (GimpUnitEntry *entry, GimpUnit unit);
 void gimp_unit_entry_set_resolution (GimpUnitEntry *entry, gdouble resolution);
-void gimp_unit_entry_set_value (GimpUnitEntry *entry, gdouble resolution);
+void gimp_unit_entry_set_value (GimpUnitEntry *entry, gdouble value);
 gdouble gimp_unit_entry_get_value (GimpUnitEntry *entry);
 gdouble gimp_unit_entry_get_value_in_unit (GimpUnitEntry *entry, GimpUnit unit);
 GimpUnit gimp_unit_entry_get_unit (GimpUnitEntry *entry);



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