[gimp/gimp-2-10] libgimpwidgets: control GimpSpinButton scroll increments using modifiers



commit 6b9a6da4a7c7ac162d58020be37867d700cc2eb7
Author: Ell <ell_se yahoo com>
Date:   Wed Jan 8 20:48:30 2020 +0200

    libgimpwidgets: control GimpSpinButton scroll increments using modifiers
    
    In GimpSpinButton, adjust the scroll step in response to modifiers:
    normal scrolling uses the step increment, Ctrl uses the page
    increment, and Shift scales the step increment down by the ratio
    between the page and step increments (up to the minimal precision
    of the spin button).
    
    This applies to all spin buttons used in GIMP, including spin
    scales.
    
    (cherry picked from commit ac8bf47fa6fc26fa081ae117d61206e36fe4fdf3)

 libgimpwidgets/gimpspinbutton.c | 67 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 59 insertions(+), 8 deletions(-)
---
diff --git a/libgimpwidgets/gimpspinbutton.c b/libgimpwidgets/gimpspinbutton.c
index cf14afc9d6..34cbe8fa41 100644
--- a/libgimpwidgets/gimpspinbutton.c
+++ b/libgimpwidgets/gimpspinbutton.c
@@ -45,6 +45,8 @@
  *
  *   - When the spin-button's "wrap" property is TRUE, values input through the
  *     entry are wrapped around.
+ *
+ *   - Modifiers can be used during scrolling for smaller/bigger increments.
  **/
 
 
@@ -59,16 +61,18 @@ struct _GimpSpinButtonPrivate
 
 /*  local function prototypes  */
 
-static gboolean   gimp_spin_button_focus_in  (GtkWidget     *widget,
-                                              GdkEventFocus *event);
-static gboolean   gimp_spin_button_focus_out (GtkWidget     *widget,
-                                              GdkEventFocus *event);
+static gboolean   gimp_spin_button_scroll    (GtkWidget      *widget,
+                                              GdkEventScroll *event);
+static gboolean   gimp_spin_button_focus_in  (GtkWidget      *widget,
+                                              GdkEventFocus  *event);
+static gboolean   gimp_spin_button_focus_out (GtkWidget      *widget,
+                                              GdkEventFocus  *event);
 
-static gint       gimp_spin_button_input     (GtkSpinButton *spin_button,
-                                              gdouble       *new_value);
+static gint       gimp_spin_button_input     (GtkSpinButton  *spin_button,
+                                              gdouble        *new_value);
 
-static void       gimp_spin_button_changed   (GtkEditable   *editable,
-                                              gpointer       data);
+static void       gimp_spin_button_changed   (GtkEditable    *editable,
+                                              gpointer        data);
 
 
 G_DEFINE_TYPE_WITH_PRIVATE (GimpSpinButton, gimp_spin_button,
@@ -86,6 +90,7 @@ gimp_spin_button_class_init (GimpSpinButtonClass *klass)
   GtkWidgetClass     *widget_class      = GTK_WIDGET_CLASS (klass);
   GtkSpinButtonClass *spin_button_class = GTK_SPIN_BUTTON_CLASS (klass);
 
+  widget_class->scroll_event    = gimp_spin_button_scroll;
   widget_class->focus_in_event  = gimp_spin_button_focus_in;
   widget_class->focus_out_event = gimp_spin_button_focus_out;
 
@@ -102,6 +107,52 @@ gimp_spin_button_init (GimpSpinButton *spin_button)
                     NULL);
 }
 
+static gboolean
+gimp_spin_button_scroll (GtkWidget      *widget,
+                         GdkEventScroll *event)
+{
+  if (event->direction == GDK_SCROLL_UP ||
+      event->direction == GDK_SCROLL_DOWN)
+    {
+      GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
+      GtkAdjustment *adjustment  = gtk_spin_button_get_adjustment (spin_button);
+      gdouble        step_inc;
+      gdouble        page_inc;
+      gint           digits;
+      gdouble        step;
+
+      step_inc = gtk_adjustment_get_step_increment (adjustment);
+      page_inc = gtk_adjustment_get_page_increment (adjustment);
+      digits   = gtk_spin_button_get_digits (spin_button);
+
+      if (event->state & GDK_SHIFT_MASK)
+        {
+          step = step_inc * step_inc / page_inc;
+          step = MAX (step, pow (10.0, -digits));
+        }
+      else if (event->state & GDK_CONTROL_MASK)
+        {
+          step = page_inc;
+        }
+      else
+        {
+          step = step_inc;
+        }
+
+      if (event->direction == GDK_SCROLL_DOWN)
+        step = -step;
+
+      if (! gtk_widget_has_focus (widget))
+        gtk_widget_grab_focus (widget);
+
+      gtk_spin_button_spin (spin_button, GTK_SPIN_USER_DEFINED, step);
+
+      return TRUE;
+    }
+
+  return GTK_WIDGET_CLASS (parent_class)->scroll_event (widget, event);
+}
+
 static gboolean
 gimp_spin_button_focus_in (GtkWidget     *widget,
                            GdkEventFocus *event)


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