spinbutton: Tangential refactoring, small fix

* Fix a bug where a zero increment would make the value unsettable,
  when the more natural operation is to allow any value to be set.

* Factorise gtk_spin_button_snap into two parts (snapping + setting),
  and make gtk_spin_button_snap only perform the snapping part.

* Avoid duplicate calls to gtk_adjustment_get_{lower/upper} and
  reinvention of CLAMP macro.
This commit is contained in:
PBS 2023-11-10 18:35:03 +09:00
parent 686d0f8d64
commit 3d898af736

View File

@ -289,7 +289,7 @@ static void gtk_spin_button_activate (GtkText *entry,
static void gtk_spin_button_unset_adjustment (GtkSpinButton *spin_button);
static void gtk_spin_button_set_orientation (GtkSpinButton *spin_button,
GtkOrientation orientation);
static void gtk_spin_button_snap (GtkSpinButton *spin_button,
static double gtk_spin_button_snap (GtkSpinButton *spin_button,
double val);
static void gtk_spin_button_insert_text (GtkEditable *editable,
const char *new_text,
@ -1485,7 +1485,7 @@ gtk_spin_button_real_change_value (GtkSpinButton *spin,
gtk_widget_error_bell (GTK_WIDGET (spin));
}
static void
static double
gtk_spin_button_snap (GtkSpinButton *spin_button,
double val)
{
@ -1494,7 +1494,7 @@ gtk_spin_button_snap (GtkSpinButton *spin_button,
inc = gtk_adjustment_get_step_increment (spin_button->adjustment);
if (inc == 0)
return;
return val;
tmp = (val - gtk_adjustment_get_lower (spin_button->adjustment)) / inc;
if (tmp - floor (tmp) < ceil (tmp) - tmp)
@ -1502,7 +1502,7 @@ gtk_spin_button_snap (GtkSpinButton *spin_button,
else
val = gtk_adjustment_get_lower (spin_button->adjustment) + ceil (tmp) * inc;
gtk_spin_button_set_value (spin_button, val);
return val;
}
static void
@ -2481,26 +2481,22 @@ gtk_spin_button_update (GtkSpinButton *spin_button)
else if (return_val == GTK_INPUT_ERROR)
error = 1;
const double lower = gtk_adjustment_get_lower (spin_button->adjustment);
const double upper = gtk_adjustment_get_upper (spin_button->adjustment);
if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
{
if (val < gtk_adjustment_get_lower (spin_button->adjustment))
val = gtk_adjustment_get_lower (spin_button->adjustment);
else if (val > gtk_adjustment_get_upper (spin_button->adjustment))
val = gtk_adjustment_get_upper (spin_button->adjustment);
val = CLAMP (val, lower, upper);
if (spin_button->snap_to_ticks)
val = gtk_spin_button_snap (spin_button, val);
gtk_spin_button_set_value (spin_button, val);
}
else if ((spin_button->update_policy == GTK_UPDATE_IF_VALID) &&
(error ||
val < gtk_adjustment_get_lower (spin_button->adjustment) ||
val > gtk_adjustment_get_upper (spin_button->adjustment)))
else if (error || val < lower || val > upper)
{
gtk_spin_button_value_changed (spin_button->adjustment, spin_button);
return;
}
if (spin_button->snap_to_ticks)
gtk_spin_button_snap (spin_button, val);
else
gtk_spin_button_set_value (spin_button, val);
}
GtkText *
@ -2508,4 +2504,3 @@ gtk_spin_button_get_text_widget (GtkSpinButton *spin_button)
{
return GTK_TEXT (spin_button->entry);
}