Commit d886bb1b authored by Jehan's avatar Jehan

libgimpwidgets: improve/fix more of GimpMemSizeEntry.

Looking further at this widget, many things are not right. Here are the
- Use binary prefixes (i.e. kibibyte, mebibyte and gibibyte) instead of
  decimal ones. We are making binary shifts so we were actually showing
  the wrong units.
- Round the value to the closest integer when showing it, not towards 0.
  Otherwise I had cases where it was showing 7GiB for an actual value of
  7.69GiB (default as computed by GIMP from my actual physical memory).
  Note that I am actually unsure even rounding makes sense. Shouldn't we
  rather show double values with a few digits after the decimal points?
  For such values, I think it would make sense.
- Do not edit the internally saved accurate value when the entry is
  edited to the same less accurate value as our saved value would be
  shown too. In particular when changing the display unit to a bigger
  one, we don't want to lose accuracy. This is especially true for low
  values. Say you don't have a lot of memory and you set the Tile cache
  size to 1.5GiB (1536MiB), you certainly don't want it to become either
  1 or 2GiB when switching display unit to GiB. Now even if the number
  will still display with less accuracy, the internal value will stay
parent 0be4e5c1
Pipeline #201872 passed with stages
in 55 minutes and 9 seconds
......@@ -73,6 +73,8 @@ static void gimp_memsize_entry_adj_callback (GtkAdjustment *adj,
static void gimp_memsize_entry_unit_callback (GtkWidget *widget,
GimpMemsizeEntry *entry);
static guint64 gimp_memsize_entry_get_rounded_value (GimpMemsizeEntry *entry,
guint64 value);
G_DEFINE_TYPE_WITH_PRIVATE (GimpMemsizeEntry, gimp_memsize_entry, GTK_TYPE_BOX)
......@@ -113,6 +115,10 @@ gimp_memsize_entry_adj_callback (GtkAdjustment *adj,
GimpMemsizeEntryPrivate *private = GET_PRIVATE (entry);
guint64 size = gtk_adjustment_get_value (adj);
if (gimp_memsize_entry_get_rounded_value (entry, private->value) != size)
/* Do not allow losing accuracy if the converted/displayed value
* stays the same.
private->value = size << private->shift;
g_signal_emit (entry, gimp_memsize_entry_signals[VALUE_CHANGED], 0);
......@@ -138,7 +144,7 @@ gimp_memsize_entry_unit_callback (GtkWidget *widget,
private->shift = shift;
gtk_adjustment_configure (private->adjustment,
CAST private->value >> shift,
gimp_memsize_entry_get_rounded_value (entry, private->value),
CAST private->lower >> shift,
CAST private->upper >> shift,
gtk_adjustment_get_step_increment (private->adjustment),
......@@ -149,6 +155,36 @@ gimp_memsize_entry_unit_callback (GtkWidget *widget,
#undef CAST
* gimp_memsize_entry_get_rounded_value:
* @entry: #GimpMemsizeEntry whose set unit is used.
* @value: value to convert to @entry unit, and rounded.
* Returns: the proper integer value to be displayed for current unit.
* This value has been appropriately rounded to the nearest
* integer, away from zero.
static guint64
gimp_memsize_entry_get_rounded_value (GimpMemsizeEntry *entry,
guint64 value)
GimpMemsizeEntryPrivate *private = GET_PRIVATE (entry);
guint64 converted;
#if _MSC_VER < 1300
# define CAST (gint64)
# define CAST
converted = (CAST value >> private->shift) +
((CAST private->value >> (private->shift - 1)) & 1);
#undef CAST
return converted;
* gimp_memsize_entry_new:
......@@ -194,7 +230,8 @@ gimp_memsize_entry_new (guint64 value,
private->upper = upper;
private->shift = shift;
private->adjustment = gtk_adjustment_new (CAST (value >> shift),
private->adjustment = gtk_adjustment_new (gimp_memsize_entry_get_rounded_value (entry,
CAST (lower >> shift),
CAST (upper >> shift),
1, 8, 0);
......@@ -212,9 +249,9 @@ gimp_memsize_entry_new (guint64 value,
G_CALLBACK (gimp_memsize_entry_adj_callback),
private->menu = gimp_int_combo_box_new (_("Kilobytes"), 10,
_("Megabytes"), 20,
_("Gigabytes"), 30,
private->menu = gimp_int_combo_box_new (_("Kibibyte"), 10,
_("Mebibyte"), 20,
_("Gibibyte"), 30,
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (private->menu), shift);
......@@ -260,13 +297,8 @@ gimp_memsize_entry_set_value (GimpMemsizeEntry *entry,
if (shift != private->shift)
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (private->menu), shift);
#if _MSC_VER < 1300
# define CAST (gint64)
# define CAST
gtk_adjustment_set_value (private->adjustment, CAST (value >> shift));
gtk_adjustment_set_value (private->adjustment,
(gdouble) gimp_memsize_entry_get_rounded_value (entry, value));
#undef CASE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment