gimpmemsizeentry.c 8.46 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 30 31 32 33
#include <gtk/gtk.h>

#include "gimpwidgetstypes.h"

#include "gimpmemsizeentry.h"
#include "gimpwidgets.h"

#include "libgimp/libgimp-intl.h"

34

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


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


54 55 56 57 58 59 60 61
struct _GimpMemsizeEntryPrivate
{
  guint64        value;
  guint64        lower;
  guint64        upper;

  guint          shift;

luz.paz's avatar
luz.paz committed
62
  /* adjustment is owned by spinbutton. Do not unref() it. */
63 64 65 66 67
  GtkAdjustment *adjustment;
  GtkWidget     *spinbutton;
  GtkWidget     *menu;
};

68
#define GET_PRIVATE(obj) (((GimpMemsizeEntry *) (obj))->priv)
69 70


71
static void  gimp_memsize_entry_adj_callback  (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
72
                                               GimpMemsizeEntry *entry);
73
static void  gimp_memsize_entry_unit_callback (GtkWidget        *widget,
Sven Neumann's avatar
Sven Neumann committed
74
                                               GimpMemsizeEntry *entry);
75 76


77
G_DEFINE_TYPE_WITH_PRIVATE (GimpMemsizeEntry, gimp_memsize_entry, GTK_TYPE_BOX)
78

79
#define parent_class gimp_memsize_entry_parent_class
80

81
static guint gimp_memsize_entry_signals[LAST_SIGNAL] = { 0 };
82 83 84 85 86


static void
gimp_memsize_entry_class_init (GimpMemsizeEntryClass *klass)
{
87
  klass->value_changed = NULL;
88

Sven Neumann's avatar
Sven Neumann committed
89
  gimp_memsize_entry_signals[VALUE_CHANGED] =
90
    g_signal_new ("value-changed",
Sven Neumann's avatar
Sven Neumann committed
91 92 93 94 95 96
                  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);
97 98 99 100 101
}

static void
gimp_memsize_entry_init (GimpMemsizeEntry *entry)
{
102
  entry->priv = gimp_memsize_entry_get_instance_private (entry);
103

104 105 106
  gtk_orientable_set_orientation (GTK_ORIENTABLE (entry),
                                  GTK_ORIENTATION_HORIZONTAL);

Sven Neumann's avatar
Sven Neumann committed
107
  gtk_box_set_spacing (GTK_BOX (entry), 4);
108 109
}

110 111
static void
gimp_memsize_entry_adj_callback (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
112
                                 GimpMemsizeEntry *entry)
113
{
114 115
  GimpMemsizeEntryPrivate *private = GET_PRIVATE (entry);
  guint64                  size    = gtk_adjustment_get_value (adj);
116

117
  private->value = size << private->shift;
118 119 120 121 122 123

  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
124
                                  GimpMemsizeEntry *entry)
125
{
126 127
  GimpMemsizeEntryPrivate *private = GET_PRIVATE (entry);
  guint                    shift;
128 129

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

131
#if _MSC_VER < 1300
132 133 134 135 136
#  define CAST (gint64)
#else
#  define CAST
#endif

137
  if (shift != private->shift)
138
    {
139 140 141 142 143 144 145 146 147
      private->shift = shift;

      gtk_adjustment_configure (private->adjustment,
                                CAST private->value >> shift,
                                CAST private->lower >> shift,
                                CAST private->upper >> shift,
                                gtk_adjustment_get_step_increment (private->adjustment),
                                gtk_adjustment_get_page_increment (private->adjustment),
                                gtk_adjustment_get_page_size (private->adjustment));
148
    }
149

150
#undef CAST
151 152 153
}


154 155 156 157 158 159 160 161 162 163 164 165
/**
 * 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 *
166
gimp_memsize_entry_new (guint64  value,
Sven Neumann's avatar
Sven Neumann committed
167 168
                        guint64  lower,
                        guint64  upper)
169
{
170 171 172
  GimpMemsizeEntry        *entry;
  GimpMemsizeEntryPrivate *private;
  guint                    shift;
173

174
#if _MSC_VER < 1300
175 176 177 178 179
#  define CAST (gint64)
#else
#  define CAST
#endif

180 181
  g_return_val_if_fail (value >= lower && value <= upper, NULL);

182
  entry = g_object_new (GIMP_TYPE_MEMSIZE_ENTRY, NULL);
183

184 185
  private = GET_PRIVATE (entry);

186 187
  for (shift = 30; shift > 10; shift -= 10)
    {
188 189
      if (value > (G_GUINT64_CONSTANT (1) << shift) &&
          value % (G_GUINT64_CONSTANT (1) << shift) == 0)
190 191 192
        break;
    }

193 194 195 196
  private->value = value;
  private->lower = lower;
  private->upper = upper;
  private->shift = shift;
197

198 199 200 201
  private->adjustment = gtk_adjustment_new (CAST (value >> shift),
                                            CAST (lower >> shift),
                                            CAST (upper >> shift),
                                            1, 8, 0);
202

203
  private->spinbutton = gtk_spin_button_new (private->adjustment, 1.0, 0);
204
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (private->spinbutton), TRUE);
205 206

#undef CAST
207

208 209 210
  gtk_entry_set_width_chars (GTK_ENTRY (private->spinbutton), 7);
  gtk_box_pack_start (GTK_BOX (entry), private->spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (private->spinbutton);
211

212
  g_signal_connect (private->adjustment, "value-changed",
213 214
                    G_CALLBACK (gimp_memsize_entry_adj_callback),
                    entry);
215

216
  private->menu = gimp_int_combo_box_new (_("Kilobytes"), 10,
217 218
                                        _("Megabytes"), 20,
                                        _("Gigabytes"), 30,
219 220
                                        NULL);

221
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (private->menu), shift);
222

223
  g_signal_connect (private->menu, "changed",
224 225
                    G_CALLBACK (gimp_memsize_entry_unit_callback),
                    entry);
226

227 228
  gtk_box_pack_start (GTK_BOX (entry), private->menu, FALSE, FALSE, 0);
  gtk_widget_show (private->menu);
229 230 231 232 233 234 235 236 237 238 239 240 241 242

  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
243
                              guint64           value)
244
{
245 246
  GimpMemsizeEntryPrivate *private;
  guint                    shift;
247 248

  g_return_if_fail (GIMP_IS_MEMSIZE_ENTRY (entry));
249 250 251 252

  private = GET_PRIVATE (entry);

  g_return_if_fail (value >= private->lower && value <= private->upper);
253 254 255

  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
        break;
    }

261
  if (shift != private->shift)
262
    {
263 264
      private->shift = shift;
      private->value = value;
265

266
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (private->menu), shift);
267 268
    }

269
#if _MSC_VER < 1300
270 271 272 273 274
#  define CAST (gint64)
#else
#  define CAST
#endif

275
  gtk_adjustment_set_value (private->adjustment, CAST (value >> shift));
276 277

#undef CASE
278 279 280 281 282 283 284 285 286 287
}

/**
 * gimp_memsize_entry_get_value:
 * @entry: a #GimpMemsizeEntry
 *
 * Retrieves the current value from a #GimpMemsizeEntry.
 *
 * Returns: the current value of @entry (in Bytes).
 **/
288
guint64
289 290 291
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
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  return GET_PRIVATE (entry)->value;
}

/**
 * gimp_memsize_entry_get_spinbutton:
 * @entry: a #GimpMemsizeEntry
 *
 * Returns: the entry's #GtkSpinbutton.
 *
 * Since: GIMP 3.0
 **/
GtkWidget *
gimp_memsize_entry_get_spinbutton (GimpMemsizeEntry *entry)
{
  g_return_val_if_fail (GIMP_IS_MEMSIZE_ENTRY (entry), 0);

  return GET_PRIVATE (entry)->spinbutton;
310
}