gimpmemsizeentry.c 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmemsizeentry.c
 * Copyright (C) 2000-2003  Sven Neumann <sven@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "gimpwidgetstypes.h"

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

#include "libgimp/libgimp-intl.h"

34

35 36 37 38 39 40 41
enum
{
  VALUE_CHANGED,
  LAST_SIGNAL
};


42
static void  gimp_memsize_entry_finalize      (GObject          *object);
43

44
static void  gimp_memsize_entry_adj_callback  (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
45
                                               GimpMemsizeEntry *entry);
46
static void  gimp_memsize_entry_unit_callback (GtkWidget        *widget,
Sven Neumann's avatar
Sven Neumann committed
47
                                               GimpMemsizeEntry *entry);
48 49


50
G_DEFINE_TYPE (GimpMemsizeEntry, gimp_memsize_entry, GTK_TYPE_HBOX);
51

52
#define parent_class gimp_memsize_entry_parent_class
53

54
static guint gimp_memsize_entry_signals[LAST_SIGNAL] = { 0 };
55 56 57 58 59


static void
gimp_memsize_entry_class_init (GimpMemsizeEntryClass *klass)
{
60
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
61 62 63

  object_class->finalize = gimp_memsize_entry_finalize;

64 65
  klass->value_changed   = NULL;

Sven Neumann's avatar
Sven Neumann committed
66
  gimp_memsize_entry_signals[VALUE_CHANGED] =
67
    g_signal_new ("value-changed",
Sven Neumann's avatar
Sven Neumann committed
68 69 70 71 72 73
                  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);
74 75 76 77 78
}

static void
gimp_memsize_entry_init (GimpMemsizeEntry *entry)
{
Sven Neumann's avatar
Sven Neumann committed
79
  gtk_box_set_spacing (GTK_BOX (entry), 4);
80

81 82 83 84 85 86 87 88 89 90 91 92
  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;
93

94 95 96 97 98
  if (entry->adjustment)
    {
      g_object_unref (entry->adjustment);
      entry->adjustment = NULL;
    }
99

100 101 102
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

103 104
static void
gimp_memsize_entry_adj_callback (GtkAdjustment    *adj,
Sven Neumann's avatar
Sven Neumann committed
105
                                 GimpMemsizeEntry *entry)
106
{
107
  guint64 size = gtk_adjustment_get_value (adj);
108 109 110 111 112 113 114 115

  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
116
                                  GimpMemsizeEntry *entry)
117
{
118 119 120
  guint  shift;

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

122
#if _MSC_VER < 1300
123 124 125 126 127
#  define CAST (gint64)
#else
#  define CAST
#endif

128 129 130 131
  if (shift != entry->shift)
    {
      entry->shift = shift;

132 133 134
      entry->adjustment->value = CAST entry->value >> shift;
      entry->adjustment->lower = CAST entry->lower >> shift;
      entry->adjustment->upper = CAST entry->upper >> shift;
135 136 137 138

      gtk_adjustment_value_changed (entry->adjustment);
      gtk_adjustment_changed (entry->adjustment);
    }
139
#undef CAST
140 141 142
}


143 144 145 146 147 148 149 150 151 152 153 154
/**
 * 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 *
155
gimp_memsize_entry_new (guint64  value,
Sven Neumann's avatar
Sven Neumann committed
156 157
                        guint64  lower,
                        guint64  upper)
158 159 160 161
{
  GimpMemsizeEntry *entry;
  guint             shift;

162
#if _MSC_VER < 1300
163 164 165 166 167
#  define CAST (gint64)
#else
#  define CAST
#endif

168 169
  g_return_val_if_fail (value >= lower && value <= upper, NULL);

170
  entry = g_object_new (GIMP_TYPE_MEMSIZE_ENTRY, NULL);
171 172 173

  for (shift = 30; shift > 10; shift -= 10)
    {
174 175
      if (value > ((guint64) 1 << shift) &&
          value % ((guint64) 1 << shift) == 0)
176 177 178 179 180 181 182
        break;
    }

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

184
  entry->spinbutton = gimp_spin_button_new ((GtkObject **) &entry->adjustment,
185 186
                                            CAST (value >> shift),
                                            CAST (lower >> shift),
187 188
                                            CAST (upper >> shift),
                                            1, 8, 0, 1, 0);
189 190

#undef CAST
191 192 193 194

  g_object_ref (entry->adjustment);
  gtk_object_sink (GTK_OBJECT (entry->adjustment));

195 196 197
  gtk_entry_set_width_chars (GTK_ENTRY (entry->spinbutton), 10);
  gtk_box_pack_start (GTK_BOX (entry), entry->spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (entry->spinbutton);
198

199
  g_signal_connect (entry->adjustment, "value-changed",
200 201
                    G_CALLBACK (gimp_memsize_entry_adj_callback),
                    entry);
202

203 204 205
  entry->menu = gimp_int_combo_box_new (_("Kilobytes"), 10,
                                        _("Megabytes"), 20,
                                        _("Gigabytes"), 30,
206 207 208 209 210 211 212
                                        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);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  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
230
                              guint64           value)
231 232 233 234 235 236 237 238
{
  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)
    {
239 240
      if (value > ((guint64) 1 << shift) &&
          value % ((guint64) 1 << shift) == 0)
241 242 243 244 245 246
        break;
    }

  if (shift != entry->shift)
    {
      entry->shift = shift;
247 248
      entry->value = value;

249
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (entry->menu), shift);
250 251
    }

252
#if _MSC_VER < 1300
253 254 255 256 257 258 259 260
#  define CAST (gint64)
#else
#  define CAST
#endif

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

#undef CASE
261 262 263 264 265 266 267 268 269 270
}

/**
 * gimp_memsize_entry_get_value:
 * @entry: a #GimpMemsizeEntry
 *
 * Retrieves the current value from a #GimpMemsizeEntry.
 *
 * Returns: the current value of @entry (in Bytes).
 **/
271
guint64
272 273 274
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
275

276 277
  return entry->value;
}