[gimp/soc-2011-gimpunitentry: 17/68] GimpUnitAdjustment: add function for setting boundaries



commit bd353ddaaf95f8211b18035ba9f6566a992dace4
Author: Enrico Schröder <enni schroeder gmail com>
Date:   Sat Jun 11 13:16:27 2011 +0200

    GimpUnitAdjustment: add function for setting boundaries
    
    Adds a function for setting the upper and lower bounds of a UnitAdjustment

 app/dialogs/layer-options-dialog.c  |   13 +++++----
 libgimpwidgets/gimpunitadjustment.c |   45 +++++++++++++++++++++++++++++++++-
 libgimpwidgets/gimpunitadjustment.h |    1 +
 libgimpwidgets/gimpunitentry.c      |    6 ++++
 libgimpwidgets/gimpunitentry.h      |    1 +
 5 files changed, 58 insertions(+), 8 deletions(-)
---
diff --git a/app/dialogs/layer-options-dialog.c b/app/dialogs/layer-options-dialog.c
index 774c669..0bbb8f0 100644
--- a/app/dialogs/layer-options-dialog.c
+++ b/app/dialogs/layer-options-dialog.c
@@ -72,7 +72,6 @@ layer_options_dialog_new (GimpImage    *image,
   GtkWidget          *spinbutton;
   GtkWidget          *frame;
   GtkWidget          *button;
-  GimpUnitEntryTable *unitEntryTable;
   GtkWidget          *entry1;
   GtkWidget          *entry2;
 
@@ -207,11 +206,13 @@ layer_options_dialog_new (GimpImage    *image,
       entry2 = gimp_unit_entry_table_add_entry (options->size_se, "height", _("Height:"));
       gimp_unit_entry_table_add_label (options->size_se, GIMP_UNIT_PIXEL, "width", "height");
       
-      gimp_unit_entry_set_unit (GIMP_UNIT_ENTRY (entry1), GIMP_UNIT_PIXEL);
-      gimp_unit_entry_set_resolution (GIMP_UNIT_ENTRY (entry1), xres);
-      gimp_unit_entry_set_resolution (GIMP_UNIT_ENTRY (entry2), yres);
-      gimp_unit_entry_set_value (GIMP_UNIT_ENTRY (entry1), gimp_image_get_width  (image));
-      gimp_unit_entry_set_value (GIMP_UNIT_ENTRY (entry2), gimp_image_get_height  (image));
+      gimp_unit_entry_set_unit        (GIMP_UNIT_ENTRY (entry1), GIMP_UNIT_PIXEL);
+      gimp_unit_entry_set_resolution  (GIMP_UNIT_ENTRY (entry1), xres);
+      gimp_unit_entry_set_resolution  (GIMP_UNIT_ENTRY (entry2), yres);
+      gimp_unit_entry_set_value       (GIMP_UNIT_ENTRY (entry1), gimp_image_get_width  (image));
+      gimp_unit_entry_set_value       (GIMP_UNIT_ENTRY (entry2), gimp_image_get_height  (image));
+      gimp_unit_entry_set_bounds      (GIMP_UNIT_ENTRY (entry1), GIMP_UNIT_PIXEL, GIMP_MAX_IMAGE_SIZE, 
GIMP_MIN_IMAGE_SIZE);
+      gimp_unit_entry_set_bounds      (GIMP_UNIT_ENTRY (entry2), GIMP_UNIT_PIXEL, GIMP_MAX_IMAGE_SIZE, 
GIMP_MIN_IMAGE_SIZE);
 
       gtk_table_attach (GTK_TABLE (table), options->size_se->table, 0, 2, 1, 3,
                         GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
diff --git a/libgimpwidgets/gimpunitadjustment.c b/libgimpwidgets/gimpunitadjustment.c
index 9309d1e..a762c4f 100644
--- a/libgimpwidgets/gimpunitadjustment.c
+++ b/libgimpwidgets/gimpunitadjustment.c
@@ -121,11 +121,11 @@ gimp_unit_adjustment_connect (GimpUnitAdjustment *adj, GimpUnitAdjustment *targe
                     (gpointer*) adj); 
 }
 
-/* converts from one current unit to another */
+/* converts value from one unit to another */
 static void
 gimp_unit_adjustment_convert_unit (GimpUnitAdjustment *adj, GimpUnit unit)
 {
-  gdouble newValue = 0;
+  gdouble newValue = 0, lower, upper;
   if (adj->unit != unit)
   {
     DEBUG   (("GimpUnitAdjustment: changing unit from %s to %s\n",
@@ -140,6 +140,25 @@ gimp_unit_adjustment_convert_unit (GimpUnitAdjustment *adj, GimpUnit unit)
                                      unit, 
                                      adj->resolution);
 
+    /* also convert bounds */
+    upper = gimp_units_to_pixels (gtk_adjustment_get_upper (GTK_ADJUSTMENT (adj)),
+                                  adj->unit,
+                                  adj->resolution);
+    lower = gimp_units_to_pixels (gtk_adjustment_get_lower (GTK_ADJUSTMENT (adj)),
+                                  adj->unit,
+                                  adj->resolution);
+
+    upper = gimp_pixels_to_units (upper,
+                                  unit, 
+                                  adj->resolution);
+    lower = gimp_pixels_to_units (lower,
+                                  unit, 
+                                  adj->resolution);
+
+    gtk_adjustment_set_upper (GTK_ADJUSTMENT (adj), upper);
+    gtk_adjustment_set_lower (GTK_ADJUSTMENT (adj), lower);
+
+    /* set the new unit */
     adj->unit  = unit;
 
     gimp_unit_adjustment_set_value (adj, newValue);
@@ -228,4 +247,26 @@ gimp_unit_adjustment_get_unit (GimpUnitAdjustment *adj)
 {
   return adj->unit;
 }
+void
+gimp_unit_adjustment_set_bounds (GimpUnitAdjustment *adj, GimpUnit unit, gdouble upper, gdouble lower)
+{
+  /* convert bounds from given unit to current unit */
+  upper = gimp_units_to_pixels (upper,
+                                unit,
+                                adj->resolution);
+  lower = gimp_units_to_pixels (lower,
+                                unit,
+                                adj->resolution);
+
+  upper = gimp_pixels_to_units (upper,
+                                adj->unit, 
+                                adj->resolution);
+  lower = gimp_pixels_to_units (lower,
+                                adj->unit, 
+                                adj->resolution);
+
+  /* set bounds */
+  gtk_adjustment_set_upper (GTK_ADJUSTMENT (adj), upper);
+  gtk_adjustment_set_lower (GTK_ADJUSTMENT (adj), lower);
+}
 
diff --git a/libgimpwidgets/gimpunitadjustment.h b/libgimpwidgets/gimpunitadjustment.h
index 27b91af..728afb0 100644
--- a/libgimpwidgets/gimpunitadjustment.h
+++ b/libgimpwidgets/gimpunitadjustment.h
@@ -76,6 +76,7 @@ gdouble gimp_unit_adjustment_get_value_in_unit    (GimpUnitAdjustment *adj, Gimp
 void    gimp_unit_adjustment_set_resolution (GimpUnitAdjustment *adj, gdouble res);
 gdouble gimp_unit_adjustment_get_resolution (GimpUnitAdjustment *adj);
 GimpUnit gimp_unit_adjustment_get_unit (GimpUnitAdjustment *adj);
+void    gimp_unit_adjustment_set_bounds (GimpUnitAdjustment *adj, GimpUnit unit, gdouble upper, gdouble 
lower);
 /* get string in format "value unit" */
 gchar*  gimp_unit_adjustment_to_string (GimpUnitAdjustment *adj);
 gchar*  gimp_unit_adjustment_to_string_in_unit (GimpUnitAdjustment *adj, GimpUnit unit);
diff --git a/libgimpwidgets/gimpunitentry.c b/libgimpwidgets/gimpunitentry.c
index 6e2a640..5359c64 100644
--- a/libgimpwidgets/gimpunitentry.c
+++ b/libgimpwidgets/gimpunitentry.c
@@ -476,3 +476,9 @@ gimp_unit_entry_get_unit (GimpUnitEntry *entry)
   GimpUnitAdjustment *adj = gimp_unit_entry_get_adjustment (entry);
   return gimp_unit_adjustment_get_unit (adj);
 }
+void
+gimp_unit_entry_set_bounds (GimpUnitEntry *entry, GimpUnit unit, gdouble upper, gdouble lower)
+{
+  GimpUnitAdjustment *adj = gimp_unit_entry_get_adjustment (entry);
+  gimp_unit_adjustment_set_bounds (adj, unit, upper, lower);
+}
diff --git a/libgimpwidgets/gimpunitentry.h b/libgimpwidgets/gimpunitentry.h
index 9330747..81358ab 100644
--- a/libgimpwidgets/gimpunitentry.h
+++ b/libgimpwidgets/gimpunitentry.h
@@ -77,6 +77,7 @@ void gimp_unit_entry_set_value (GimpUnitEntry *entry, gdouble resolution);
 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);
+void gimp_unit_entry_set_bounds (GimpUnitEntry *entry, GimpUnit unit, gdouble upper, gdouble lower);
 
 G_END_DECLS
 


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