gimpmemsizeentry.c 7.94 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmemsizeentry.c
 * Copyright (C) 2000-2003  Sven Neumann <sven@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <gegl.h>
25 26 27 28 29
#include <gtk/gtk.h>

#include "gimpwidgetstypes.h"

#include "gimpmemsizeentry.h"
30
#include "gimpspinbutton.h"
31 32 33 34
#include "gimpwidgets.h"

#include "libgimp/libgimp-intl.h"

35

36 37 38
/**
 * SECTION: gimpmemsizeentry
 * @title: GimpMemSizeEntry
39
 * @short_description: A composite widget to enter a memory size.
40 41
 *
 * Similar to a #GimpSizeEntry but instead of lengths, this widget is
42
 * used to let the user enter memory sizes. A combo box allows one to
43 44 45 46 47
 * switch between Kilobytes, Megabytes and Gigabytes. Used in the GIMP
 * preferences dialog.
 **/


48 49 50 51 52 53 54
enum
{
  VALUE_CHANGED,
  LAST_SIGNAL
};


55
static void  gimp_memsize_entry_finalize      (GObject          *object);
56

57
static void  gimp_memsize_entry_adj_callback  (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
58
                                               GimpMemsizeEntry *entry);
59
static void  gimp_memsize_entry_unit_callback (GtkWidget        *widget,
Sven Neumann's avatar
Sven Neumann committed
60
                                               GimpMemsizeEntry *entry);
61 62


63
G_DEFINE_TYPE (GimpMemsizeEntry, gimp_memsize_entry, GTK_TYPE_BOX)
64

65
#define parent_class gimp_memsize_entry_parent_class
66

67
static guint gimp_memsize_entry_signals[LAST_SIGNAL] = { 0 };
68 69 70 71 72


static void
gimp_memsize_entry_class_init (GimpMemsizeEntryClass *klass)
{
73
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 75 76

  object_class->finalize = gimp_memsize_entry_finalize;

77 78
  klass->value_changed   = NULL;

Sven Neumann's avatar
Sven Neumann committed
79
  gimp_memsize_entry_signals[VALUE_CHANGED] =
80
    g_signal_new ("value-changed",
Sven Neumann's avatar
Sven Neumann committed
81 82 83 84 85 86
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpMemsizeEntryClass, value_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
87 88 89 90 91
}

static void
gimp_memsize_entry_init (GimpMemsizeEntry *entry)
{
92 93 94
  gtk_orientable_set_orientation (GTK_ORIENTABLE (entry),
                                  GTK_ORIENTATION_HORIZONTAL);

Sven Neumann's avatar
Sven Neumann committed
95
  gtk_box_set_spacing (GTK_BOX (entry), 4);
96

97 98 99 100 101 102 103 104 105 106 107 108
  entry->value      = 0;
  entry->lower      = 0;
  entry->upper      = 0;
  entry->shift      = 0;
  entry->adjustment = NULL;
  entry->menu       = NULL;
}

static void
gimp_memsize_entry_finalize (GObject *object)
{
  GimpMemsizeEntry *entry = (GimpMemsizeEntry *) object;
109

110
  g_clear_object (&entry->adjustment);
111

112 113 114
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

115 116
static void
gimp_memsize_entry_adj_callback (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
117
                                 GimpMemsizeEntry *entry)
118
{
119
  guint64 size = gtk_adjustment_get_value (adj);
120 121 122 123 124 125 126 127

  entry->value = size << entry->shift;

  g_signal_emit (entry, gimp_memsize_entry_signals[VALUE_CHANGED], 0);
}

static void
gimp_memsize_entry_unit_callback (GtkWidget        *widget,
Sven Neumann's avatar
Sven Neumann committed
128
                                  GimpMemsizeEntry *entry)
129
{
130 131 132
  guint  shift;

  gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), (gint *) &shift);
133

134
#if _MSC_VER < 1300
135 136 137 138 139
#  define CAST (gint64)
#else
#  define CAST
#endif

140 141 142 143
  if (shift != entry->shift)
    {
      entry->shift = shift;

144 145 146 147 148 149 150
      gtk_adjustment_configure (entry->adjustment,
                                CAST entry->value >> shift,
                                CAST entry->lower >> shift,
                                CAST entry->upper >> shift,
                                gtk_adjustment_get_step_increment (entry->adjustment),
                                gtk_adjustment_get_page_increment (entry->adjustment),
                                gtk_adjustment_get_page_size (entry->adjustment));
151
    }
152

153
#undef CAST
154 155 156
}


157 158 159 160 161 162 163 164 165 166 167 168
/**
 * gimp_memsize_entry_new:
 * @value: the initial value (in Bytes)
 * @lower: the lower limit for the value (in Bytes)
 * @upper: the upper limit for the value (in Bytes)
 *
 * Creates a new #GimpMemsizeEntry which is a #GtkHBox with a #GtkSpinButton
 * and a #GtkOptionMenu all setup to allow the user to enter memory sizes.
 *
 * Returns: Pointer to the new #GimpMemsizeEntry.
 **/
GtkWidget *
169
gimp_memsize_entry_new (guint64  value,
Sven Neumann's avatar
Sven Neumann committed
170 171
                        guint64  lower,
                        guint64  upper)
172 173
{
  GimpMemsizeEntry *entry;
174
  GtkAdjustment    *adj;
175 176
  guint             shift;

177
#if _MSC_VER < 1300
178 179 180 181 182
#  define CAST (gint64)
#else
#  define CAST
#endif

183 184
  g_return_val_if_fail (value >= lower && value <= upper, NULL);

185
  entry = g_object_new (GIMP_TYPE_MEMSIZE_ENTRY, NULL);
186 187 188

  for (shift = 30; shift > 10; shift -= 10)
    {
189 190
      if (value > (G_GUINT64_CONSTANT (1) << shift) &&
          value % (G_GUINT64_CONSTANT (1) << shift) == 0)
191 192 193 194 195 196 197
        break;
    }

  entry->value = value;
  entry->lower = lower;
  entry->upper = upper;
  entry->shift = shift;
198

199 200 201 202 203
  adj = (GtkAdjustment *) gtk_adjustment_new (CAST (value >> shift),
                                              CAST (lower >> shift),
                                              CAST (upper >> shift),
                                              1, 8, 0);

204
  entry->spinbutton = gimp_spin_button_new (adj, 1.0, 0);
205
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (entry->spinbutton), TRUE);
206 207

#undef CAST
208

209
  entry->adjustment = GTK_ADJUSTMENT (adj);
210
  g_object_ref_sink (entry->adjustment);
211

212
  gtk_entry_set_width_chars (GTK_ENTRY (entry->spinbutton), 7);
213 214
  gtk_box_pack_start (GTK_BOX (entry), entry->spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (entry->spinbutton);
215

216
  g_signal_connect (entry->adjustment, "value-changed",
217 218
                    G_CALLBACK (gimp_memsize_entry_adj_callback),
                    entry);
219

220 221 222
  entry->menu = gimp_int_combo_box_new (_("Kilobytes"), 10,
                                        _("Megabytes"), 20,
                                        _("Gigabytes"), 30,
223 224 225 226 227 228 229
                                        NULL);

  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (entry->menu), shift);

  g_signal_connect (entry->menu, "changed",
                    G_CALLBACK (gimp_memsize_entry_unit_callback),
                    entry);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

  gtk_box_pack_start (GTK_BOX (entry), entry->menu, FALSE, FALSE, 0);
  gtk_widget_show (entry->menu);

  return GTK_WIDGET (entry);
}

/**
 * gimp_memsize_entry_set_value:
 * @entry: a #GimpMemsizeEntry
 * @value: the new value (in Bytes)
 *
 * Sets the @entry's value. Please note that the #GimpMemsizeEntry rounds
 * the value to full Kilobytes.
 **/
void
gimp_memsize_entry_set_value (GimpMemsizeEntry *entry,
Sven Neumann's avatar
Sven Neumann committed
247
                              guint64           value)
248 249 250 251 252 253 254 255
{
  guint shift;

  g_return_if_fail (GIMP_IS_MEMSIZE_ENTRY (entry));
  g_return_if_fail (value >= entry->lower && value <= entry->upper);

  for (shift = 30; shift > 10; shift -= 10)
    {
256 257
      if (value > (G_GUINT64_CONSTANT (1) << shift) &&
          value % (G_GUINT64_CONSTANT (1) << shift) == 0)
258 259 260 261
        break;
    }

  if (shift != entry->shift)
262
    gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (entry->menu), shift);
263

264
#if _MSC_VER < 1300
265 266 267 268 269 270 271 272
#  define CAST (gint64)
#else
#  define CAST
#endif

  gtk_adjustment_set_value (entry->adjustment, CAST (value >> shift));

#undef CASE
273 274 275 276 277 278 279 280 281 282
}

/**
 * gimp_memsize_entry_get_value:
 * @entry: a #GimpMemsizeEntry
 *
 * Retrieves the current value from a #GimpMemsizeEntry.
 *
 * Returns: the current value of @entry (in Bytes).
 **/
283
guint64
284 285 286
gimp_memsize_entry_get_value (GimpMemsizeEntry *entry)
{
  g_return_val_if_fail (GIMP_IS_MEMSIZE_ENTRY (entry), 0);
Sven Neumann's avatar
Sven Neumann committed
287

288 289
  return entry->value;
}