gimpsizeentry.c 53.8 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3
 *
4 5
 * gimpsizeentry.c
 * Copyright (C) 1999-2000 Sven Neumann <sven@gimp.org>
6
 *                         Michael Natterer <mitch@gimp.org>
7
 *
8
 * This library is free software: you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or (at your option) any later version.
12 13 14 15
 *
 * 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
16 17
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
20
 * <https://www.gnu.org/licenses/>.
21
 */
22

23 24
#include "config.h"

25 26
#include <string.h>

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

30
#include "libgimpbase/gimpbase.h"
31

32
#include "gimpwidgets.h"
33

34
#include "gimpeevl.h"
35
#include "gimpsizeentry.h"
36

37

38 39 40 41
/**
 * SECTION: gimpsizeentry
 * @title: GimpSizeEntry
 * @short_description: Widget for entering pixel values and resolutions.
42
 * @see_also: #GimpUnit, #GimpUnitComboBox, gimp_coordinates_new()
43 44 45 46 47 48 49
 *
 * This widget is used to enter pixel distances/sizes and resolutions.
 *
 * You can specify the number of fields the widget should provide. For
 * each field automatic mappings are performed between the field's
 * "reference value" and its "value".
 *
50 51
 * There is a #GimpUnitComboBox right of the entry fields which lets
 * you specify the #GimpUnit of the displayed values.
52 53 54 55
 *
 * For each field, there can be one or two #GtkSpinButton's to enter
 * "value" and "reference value". If you specify @show_refval as
 * #FALSE in gimp_size_entry_new() there will be only one
56
 * #GtkSpinButton and the #GimpUnitComboBox will contain an item for
57 58 59 60 61 62 63 64 65 66
 * selecting GIMP_UNIT_PIXEL.
 *
 * The "reference value" is either of GIMP_UNIT_PIXEL or dpi,
 * depending on which #GimpSizeEntryUpdatePolicy you specify in
 * gimp_size_entry_new().  The "value" is either the size in pixels
 * mapped to the size in a real-world-unit (see #GimpUnit) or the dpi
 * value mapped to pixels per real-world-unit.
 **/


Michael Natterer's avatar
Michael Natterer committed
67
#define SIZE_MAX_VALUE 500000.0
68

69
#define GIMP_SIZE_ENTRY_DIGITS(unit) (MIN (gimp_unit_get_digits (unit), 5) + 1)
70

71

72 73
enum
{
74 75 76
  VALUE_CHANGED,
  REFVAL_CHANGED,
  UNIT_CHANGED,
77 78 79
  LAST_SIGNAL
};

80

81 82 83 84
struct _GimpSizeEntryField
{
  GimpSizeEntry *gse;

85 86 87
  gdouble        resolution;
  gdouble        lower;
  gdouble        upper;
88

89
  GtkAdjustment *value_adjustment;
90
  GtkWidget     *value_spinbutton;
91 92 93
  gdouble        value;
  gdouble        min_value;
  gdouble        max_value;
94

95
  GtkAdjustment *refval_adjustment;
96
  GtkWidget     *refval_spinbutton;
97 98 99
  gdouble        refval;
  gdouble        min_refval;
  gdouble        max_refval;
100
  gint           refval_digits;
101 102

  gint           stop_recursion;
103 104
};

105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
struct _GimpSizeEntryPrivate
{
  GSList                    *fields;
  gint                       number_of_fields;

  GtkWidget                 *unit_combo;
  GimpUnit                   unit;
  gboolean                   menu_show_pixels;
  gboolean                   menu_show_percent;

  gboolean                   show_refval;
  GimpSizeEntryUpdatePolicy  update_policy;
};

#define GET_PRIVATE(obj) (((GimpSizeEntry *) (obj))->priv)


123 124 125
static void      gimp_size_entry_finalize            (GObject            *object);
static void      gimp_size_entry_update_value        (GimpSizeEntryField *gsef,
                                                      gdouble             value);
126
static void      gimp_size_entry_value_callback      (GtkAdjustment      *adjustment,
127 128 129
                                                      gpointer            data);
static void      gimp_size_entry_update_refval       (GimpSizeEntryField *gsef,
                                                      gdouble             refval);
130
static void      gimp_size_entry_refval_callback     (GtkAdjustment      *adjustment,
131 132 133 134 135 136 137 138 139 140 141 142 143
                                                      gpointer            data);
static void      gimp_size_entry_update_unit         (GimpSizeEntry      *gse,
                                                      GimpUnit            unit);
static void      gimp_size_entry_unit_callback       (GtkWidget          *widget,
                                                      GimpSizeEntry      *sizeentry);
static void      gimp_size_entry_attach_eevl         (GtkSpinButton      *spin_button,
                                                      GimpSizeEntryField *gsef);
static gint      gimp_size_entry_eevl_input_callback (GtkSpinButton      *spinner,
                                                      gdouble            *return_val,
                                                      gpointer           *data);
static gboolean  gimp_size_entry_eevl_unit_resolver  (const gchar        *ident,
                                                      GimpEevlQuantity   *result,
                                                      gpointer            data);
144 145


146
G_DEFINE_TYPE_WITH_PRIVATE (GimpSizeEntry, gimp_size_entry, GTK_TYPE_GRID)
147

148
#define parent_class gimp_size_entry_parent_class
149

150
static guint gimp_size_entry_signals[LAST_SIGNAL] = { 0 };
151

152

153
static void
154
gimp_size_entry_class_init (GimpSizeEntryClass *klass)
155
{
156
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
157 158

  gimp_size_entry_signals[VALUE_CHANGED] =
159
    g_signal_new ("value-changed",
Sven Neumann's avatar
Sven Neumann committed
160 161 162 163 164 165
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpSizeEntryClass, value_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
166 167

  gimp_size_entry_signals[REFVAL_CHANGED] =
168
    g_signal_new ("refval-changed",
Sven Neumann's avatar
Sven Neumann committed
169 170 171 172 173 174
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpSizeEntryClass, refval_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
175 176

  gimp_size_entry_signals[UNIT_CHANGED] =
177
    g_signal_new ("unit-changed",
Sven Neumann's avatar
Sven Neumann committed
178 179 180 181 182 183
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpSizeEntryClass, unit_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
184

185
  object_class->finalize = gimp_size_entry_finalize;
186

187 188 189
  klass->value_changed   = NULL;
  klass->refval_changed  = NULL;
  klass->unit_changed    = NULL;
190 191 192 193 194
}

static void
gimp_size_entry_init (GimpSizeEntry *gse)
{
195 196
  GimpSizeEntryPrivate *priv;

197
  gse->priv = gimp_size_entry_get_instance_private (gse);
198 199 200 201 202 203 204 205

  priv = gse->priv;

  priv->unit              = GIMP_UNIT_PIXEL;
  priv->menu_show_pixels  = TRUE;
  priv->menu_show_percent = TRUE;
  priv->show_refval       = FALSE;
  priv->update_policy     = GIMP_SIZE_ENTRY_UPDATE_NONE;
206 207
}

208
static void
209
gimp_size_entry_finalize (GObject *object)
210
{
211
  GimpSizeEntryPrivate *priv = GET_PRIVATE (object);
212

213
  if (priv->fields)
214
    {
215 216
      GSList *list;

217
      for (list = priv->fields; list; list = list->next)
218 219
        g_slice_free (GimpSizeEntryField, list->data);

220 221
      g_slist_free (priv->fields);
      priv->fields = NULL;
222
    }
223

224
  G_OBJECT_CLASS (parent_class)->finalize (object);
225 226
}

227 228
/**
 * gimp_size_entry_new:
229 230
 * @number_of_fields:  The number of input fields.
 * @unit:              The initial unit.
231 232
 * @unit_format:       A printf-like unit-format string as is used with
 *                     gimp_unit_menu_new().
233
 * @menu_show_pixels:  %TRUE if the unit menu should contain an item for
234 235
 *                     GIMP_UNIT_PIXEL (ignored if the @update_policy is not
 *                     GIMP_SIZE_ENTRY_UPDATE_NONE).
236
 * @menu_show_percent: %TRUE if the unit menu should contain an item for
237
 *                     GIMP_UNIT_PERCENT.
Marc Lehmann's avatar
Marc Lehmann committed
238
 * @show_refval:       %TRUE if you want an extra "reference value"
239
 *                     spinbutton per input field.
240
 * @spinbutton_width:  The minimal horizontal size of the #GtkSpinButton's.
241 242
 * @update_policy:     How the automatic pixel <-> real-world-unit
 *                     calculations should be done.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
 *
 * Creates a new #GimpSizeEntry widget.
 *
 * To have all automatic calculations performed correctly, set up the
 * widget in the following order:
 *
 * 1. gimp_size_entry_new()
 *
 * 2. (for each additional input field) gimp_size_entry_add_field()
 *
 * 3. gimp_size_entry_set_unit()
 *
 * For each input field:
 *
 * 4. gimp_size_entry_set_resolution()
 *
 * 5. gimp_size_entry_set_refval_boundaries()
 *    (or gimp_size_entry_set_value_boundaries())
 *
 * 6. gimp_size_entry_set_size()
 *
 * 7. gimp_size_entry_set_refval() (or gimp_size_entry_set_value())
 *
266
 * The #GimpSizeEntry is derived from #GtkGrid and will have
267
 * an empty border of one cell width on each side plus an empty column left
268
 * of the #GimpUnitComboBox to allow the caller to add labels or a
269
 * #GimpChainButton.
270 271
 *
 * Returns: A Pointer to the new #GimpSizeEntry widget.
272
 **/
273 274
GtkWidget *
gimp_size_entry_new (gint                       number_of_fields,
Sven Neumann's avatar
Sven Neumann committed
275 276 277 278 279 280 281
                     GimpUnit                   unit,
                     const gchar               *unit_format,
                     gboolean                   menu_show_pixels,
                     gboolean                   menu_show_percent,
                     gboolean                   show_refval,
                     gint                       spinbutton_width,
                     GimpSizeEntryUpdatePolicy  update_policy)
282
{
283 284 285 286
  GimpSizeEntry        *gse;
  GimpSizeEntryPrivate *priv;
  GimpUnitStore        *store;
  gint                  i;
287

288
  g_return_val_if_fail ((number_of_fields >= 0) && (number_of_fields <= 16),
Sven Neumann's avatar
Sven Neumann committed
289
                        NULL);
290

Michael Natterer's avatar
Michael Natterer committed
291
  gse = g_object_new (GIMP_TYPE_SIZE_ENTRY, NULL);
292

293 294 295 296 297 298
  priv = GET_PRIVATE (gse);

  priv->number_of_fields = number_of_fields;
  priv->unit             = unit;
  priv->show_refval      = show_refval;
  priv->update_policy    = update_policy;
299

300 301 302
  /*  show the 'pixels' menu entry only if we are a 'size' sizeentry and
   *  don't have the reference value spinbutton
   */
Michael Natterer's avatar
Michael Natterer committed
303 304
  if ((update_policy == GIMP_SIZE_ENTRY_UPDATE_RESOLUTION) ||
      (show_refval == TRUE))
305
    priv->menu_show_pixels = FALSE;
306
  else
307
    priv->menu_show_pixels = menu_show_pixels;
308

Michael Natterer's avatar
Michael Natterer committed
309 310 311
  /*  show the 'percent' menu entry only if we are a 'size' sizeentry
   */
  if (update_policy == GIMP_SIZE_ENTRY_UPDATE_RESOLUTION)
312
    priv->menu_show_percent = FALSE;
Michael Natterer's avatar
Michael Natterer committed
313
  else
314
    priv->menu_show_percent = menu_show_percent;
Michael Natterer's avatar
Michael Natterer committed
315

316
  for (i = 0; i < number_of_fields; i++)
317
    {
318
      GimpSizeEntryField *gsef = g_slice_new0 (GimpSizeEntryField);
319
      gint                digits;
320

321
      priv->fields = g_slist_append (priv->fields, gsef);
322

323 324 325 326 327 328 329
      gsef->gse               = gse;
      gsef->resolution        = 1.0; /*  just to avoid division by zero  */
      gsef->lower             = 0.0;
      gsef->upper             = 100.0;
      gsef->value             = 0;
      gsef->min_value         = 0;
      gsef->max_value         = SIZE_MAX_VALUE;
330
      gsef->refval_adjustment = NULL;
331 332 333 334
      gsef->value_adjustment  = NULL;
      gsef->refval            = 0;
      gsef->min_refval        = 0;
      gsef->max_refval        = SIZE_MAX_VALUE;
335
      gsef->refval_digits     =
Sven Neumann's avatar
Sven Neumann committed
336
        (update_policy == GIMP_SIZE_ENTRY_UPDATE_SIZE) ? 0 : 3;
337
      gsef->stop_recursion    = 0;
338

339 340 341 342
      digits = ((unit == GIMP_UNIT_PIXEL) ?
                gsef->refval_digits : ((unit == GIMP_UNIT_PERCENT) ?
                                       2 : GIMP_SIZE_ENTRY_DIGITS (unit)));

343 344 345 346
      gsef->value_adjustment = gtk_adjustment_new (gsef->value,
                                                   gsef->min_value,
                                                   gsef->max_value,
                                                   1.0, 10.0, 0.0);
347 348
      gsef->value_spinbutton = gimp_spin_button_new (gsef->value_adjustment,
                                                     1.0, digits);
349 350
      gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (gsef->value_spinbutton),
                                   TRUE);
351

352 353 354
      gimp_size_entry_attach_eevl (GTK_SPIN_BUTTON (gsef->value_spinbutton),
                                   gsef);

355 356 357 358 359 360 361 362 363 364
      if (spinbutton_width > 0)
        {
          if (spinbutton_width < 17)
            gtk_entry_set_width_chars (GTK_ENTRY (gsef->value_spinbutton),
                                       spinbutton_width);
          else
            gtk_widget_set_size_request (gsef->value_spinbutton,
                                         spinbutton_width, -1);
        }

365
      gtk_grid_attach (GTK_GRID (gse), gsef->value_spinbutton,
366
                       i+1, priv->show_refval+1, 1, 1);
367
      g_signal_connect (gsef->value_adjustment, "value-changed",
368 369
                        G_CALLBACK (gimp_size_entry_value_callback),
                        gsef);
370

371 372
      gtk_widget_show (gsef->value_spinbutton);

373
      if (priv->show_refval)
Sven Neumann's avatar
Sven Neumann committed
374
        {
375 376 377 378
          gsef->refval_adjustment = gtk_adjustment_new (gsef->refval,
                                                        gsef->min_refval,
                                                        gsef->max_refval,
                                                        1.0, 10.0, 0.0);
379 380 381
          gsef->refval_spinbutton = gimp_spin_button_new (gsef->refval_adjustment,
                                                          1.0,
                                                          gsef->refval_digits);
382 383
          gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (gsef->refval_spinbutton),
                                       TRUE);
384

Sven Neumann's avatar
Sven Neumann committed
385 386
          gtk_widget_set_size_request (gsef->refval_spinbutton,
                                       spinbutton_width, -1);
387 388
          gtk_grid_attach (GTK_GRID (gse), gsef->refval_spinbutton,
                           i + 1, 1, 1, 1);
Sven Neumann's avatar
Sven Neumann committed
389
          g_signal_connect (gsef->refval_adjustment,
390
                            "value-changed",
391 392
                            G_CALLBACK (gimp_size_entry_refval_callback),
                            gsef);
393

Sven Neumann's avatar
Sven Neumann committed
394 395
          gtk_widget_show (gsef->refval_spinbutton);
        }
396

397 398
      if (priv->menu_show_pixels && (unit == GIMP_UNIT_PIXEL) &&
          ! priv->show_refval)
Sven Neumann's avatar
Sven Neumann committed
399 400
        gtk_spin_button_set_digits (GTK_SPIN_BUTTON (gsef->value_spinbutton),
                                    gsef->refval_digits);
401
    }
402

403 404 405
  store = gimp_unit_store_new (priv->number_of_fields);
  gimp_unit_store_set_has_pixels (store, priv->menu_show_pixels);
  gimp_unit_store_set_has_percent (store, priv->menu_show_percent);
406

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  if (unit_format)
    {
      gchar *short_format = g_strdup (unit_format);
      gchar *p;

      p = strstr (short_format, "%s");
      if (p)
        strcpy (p, "%a");

      p = strstr (short_format, "%p");
      if (p)
        strcpy (p, "%a");

      g_object_set (store,
                    "short-format", short_format,
                    "long-format",  unit_format,
                    NULL);

      g_free (short_format);
    }

428
  priv->unit_combo = gimp_unit_combo_box_new_with_model (store);
429 430
  g_object_unref (store);

431
  gimp_unit_combo_box_set_active (GIMP_UNIT_COMBO_BOX (priv->unit_combo), unit);
432

433 434 435
  gtk_grid_attach (GTK_GRID (gse), priv->unit_combo,
                    i+2, priv->show_refval+1, 1, 1);
  g_signal_connect (priv->unit_combo, "changed",
436 437
                    G_CALLBACK (gimp_size_entry_unit_callback),
                    gse);
438
  gtk_widget_show (priv->unit_combo);
439

440 441 442
  return GTK_WIDGET (gse);
}

443

444 445
/**
 * gimp_size_entry_add_field:
446 447
 * @gse:               The sizeentry you want to add a field to.
 * @value_spinbutton:  The spinbutton to display the field's value.
448 449 450 451 452
 * @refval_spinbutton: The spinbutton to display the field's reference value.
 *
 * Adds an input field to the #GimpSizeEntry.
 *
 * The new input field will have the index 0. If you specified @show_refval
453 454
 * as %TRUE in gimp_size_entry_new() you have to pass an additional
 * #GtkSpinButton to hold the reference value. If @show_refval was %FALSE,
455
 * @refval_spinbutton will be ignored.
456
 **/
457
void
458
gimp_size_entry_add_field  (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
459 460
                            GtkSpinButton *value_spinbutton,
                            GtkSpinButton *refval_spinbutton)
461
{
462 463 464
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
  gint                  digits;
465 466 467

  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
  g_return_if_fail (GTK_IS_SPIN_BUTTON (value_spinbutton));
468

469 470 471
  priv = GET_PRIVATE (gse);

  if (priv->show_refval)
472 473 474 475
    {
      g_return_if_fail (GTK_IS_SPIN_BUTTON (refval_spinbutton));
    }

476
  gsef = g_slice_new0 (GimpSizeEntryField);
477

478 479
  priv->fields = g_slist_prepend (priv->fields, gsef);
  priv->number_of_fields++;
480

481 482 483 484 485 486 487 488 489 490 491
  gsef->gse            = gse;
  gsef->resolution     = 1.0; /*  just to avoid division by zero  */
  gsef->lower          = 0.0;
  gsef->upper          = 100.0;
  gsef->value          = 0;
  gsef->min_value      = 0;
  gsef->max_value      = SIZE_MAX_VALUE;
  gsef->refval         = 0;
  gsef->min_refval     = 0;
  gsef->max_refval     = SIZE_MAX_VALUE;
  gsef->refval_digits  =
492
    (priv->update_policy == GIMP_SIZE_ENTRY_UPDATE_SIZE) ? 0 : 3;
493
  gsef->stop_recursion = 0;
494

495
  gsef->value_adjustment = gtk_spin_button_get_adjustment (value_spinbutton);
496
  gsef->value_spinbutton = GTK_WIDGET (value_spinbutton);
497
  g_signal_connect (gsef->value_adjustment, "value-changed",
498 499
                    G_CALLBACK (gimp_size_entry_value_callback),
                    gsef);
500

501 502 503
  gimp_size_entry_attach_eevl (GTK_SPIN_BUTTON (gsef->value_spinbutton),
                               gsef);

504
  if (priv->show_refval)
505
    {
506
      gsef->refval_adjustment = gtk_spin_button_get_adjustment (refval_spinbutton);
507
      gsef->refval_spinbutton = GTK_WIDGET (refval_spinbutton);
508
      g_signal_connect (gsef->refval_adjustment, "value-changed",
509 510
                        G_CALLBACK (gimp_size_entry_refval_callback),
                        gsef);
511 512
    }

513 514 515
  digits = ((priv->unit == GIMP_UNIT_PIXEL) ? gsef->refval_digits :
            (priv->unit == GIMP_UNIT_PERCENT) ? 2 :
            GIMP_SIZE_ENTRY_DIGITS (priv->unit));
516 517

  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (value_spinbutton), digits);
518

519 520 521
  if (priv->menu_show_pixels &&
      !priv->show_refval &&
      (priv->unit == GIMP_UNIT_PIXEL))
522 523
    {
      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (gsef->value_spinbutton),
Sven Neumann's avatar
Sven Neumann committed
524
                                  gsef->refval_digits);
525
    }
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
GimpSizeEntryUpdatePolicy
gimp_size_entry_get_update_policy (GimpSizeEntry *gse)
{
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), GIMP_SIZE_ENTRY_UPDATE_SIZE);

  return GET_PRIVATE (gse)->update_policy;
}

gint
gimp_size_entry_get_n_fields (GimpSizeEntry *gse)
{
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), 0);

  return GET_PRIVATE (gse)->number_of_fields;
}

GtkWidget *
gimp_size_entry_get_unit_combo (GimpSizeEntry *gse)
{
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), NULL);

  return GET_PRIVATE (gse)->unit_combo;
}

552 553
/**
 * gimp_size_entry_attach_label:
554 555 556 557
 * @gse:       The sizeentry you want to add a label to.
 * @text:      The text of the label.
 * @row:       The row where the label will be attached.
 * @column:    The column where the label will be attached.
558 559
 * @alignment: The horizontal alignment of the label.
 *
560
 * Attaches a #GtkLabel to the #GimpSizeEntry (which is a #GtkGrid).
561 562
 *
 * Returns: A pointer to the new #GtkLabel widget.
563
 **/
564
GtkWidget *
565
gimp_size_entry_attach_label (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
566 567 568 569
                              const gchar   *text,
                              gint           row,
                              gint           column,
                              gfloat         alignment)
570
{
571
  GtkWidget *label;
572

573 574
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), NULL);
  g_return_val_if_fail (text != NULL, NULL);
575

576
  label = gtk_label_new_with_mnemonic (text);
577 578 579

  if (column == 0)
    {
580 581
      GList *children;
      GList *list;
582

583 584 585
      children = gtk_container_get_children (GTK_CONTAINER (gse));

      for (list = children; list; list = g_list_next (list))
586
        {
587 588 589 590 591 592 593 594
          GtkWidget *child = list->data;
          gint       left_attach;
          gint       top_attach;

          gtk_container_child_get (GTK_CONTAINER (gse), child,
                                   "left-attach", &left_attach,
                                   "top-attach",  &top_attach,
                                   NULL);
595

596
          if (left_attach == 1 && top_attach == row)
597
            {
598
              gtk_label_set_mnemonic_widget (GTK_LABEL (label), child);
599 600 601
              break;
            }
        }
602 603

      g_list_free (children);
604
    }
605

606
  gtk_label_set_xalign (GTK_LABEL (label), alignment);
607

608
  gtk_grid_attach (GTK_GRID (gse), label, column, row, 1, 1);
609
  gtk_widget_show (label);
610 611

  return label;
612
}
613 614


615 616
/**
 * gimp_size_entry_set_resolution:
617 618
 * @gse:        The sizeentry you want to set a resolution for.
 * @field:      The index of the field you want to set the resolution for.
619
 * @resolution: The new resolution (in dpi) for the chosen @field.
620 621
 * @keep_size:  %TRUE if the @field's size in pixels should stay the same.
 *              %FALSE if the @field's size in units should stay the same.
622 623 624 625 626 627 628
 *
 * Sets the resolution (in dpi) for field # @field of the #GimpSizeEntry.
 *
 * The @resolution passed will be clamped to fit in
 * [#GIMP_MIN_RESOLUTION..#GIMP_MAX_RESOLUTION].
 *
 * This function does nothing if the #GimpSizeEntryUpdatePolicy specified in
629 630
 * gimp_size_entry_new() doesn't equal to #GIMP_SIZE_ENTRY_UPDATE_SIZE.
 **/
631
void
632
gimp_size_entry_set_resolution (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
633 634 635
                                gint           field,
                                gdouble        resolution,
                                gboolean       keep_size)
636
{
637 638 639
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
  gfloat                val;
640

641
  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
642 643 644 645

  priv = GET_PRIVATE (gse);

  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));
646 647

  resolution = CLAMP (resolution, GIMP_MIN_RESOLUTION, GIMP_MAX_RESOLUTION);
648

649
  gsef = (GimpSizeEntryField*) g_slist_nth_data (priv->fields, field);
650
  gsef->resolution = resolution;
651

652 653 654 655
  val = gsef->value;

  gsef->stop_recursion = 0;
  gimp_size_entry_set_refval_boundaries (gse, field,
Sven Neumann's avatar
Sven Neumann committed
656
                                         gsef->min_refval, gsef->max_refval);
657 658 659

  if (! keep_size)
    gimp_size_entry_set_value (gse, field, val);
660 661 662
}


663 664
/**
 * gimp_size_entry_set_size:
665
 * @gse:   The sizeentry you want to set a size for.
666 667 668 669 670 671 672
 * @field: The index of the field you want to set the size for.
 * @lower: The reference value which will be treated as 0%.
 * @upper: The reference value which will be treated as 100%.
 *
 * Sets the pixel values for field # @field of the #GimpSizeEntry
 * which will be treated as 0% and 100%.
 *
673
 * These values will be used if you specified @menu_show_percent as %TRUE
674
 * in gimp_size_entry_new() and the user has selected GIMP_UNIT_PERCENT in
675
 * the #GimpSizeEntry's #GimpUnitComboBox.
676 677 678
 *
 * This function does nothing if the #GimpSizeEntryUpdatePolicy specified in
 * gimp_size_entry_new() doesn't equal to GIMP_SIZE_ENTRY_UPDATE_SIZE.
679
 **/
680 681
void
gimp_size_entry_set_size (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
682 683 684
                          gint           field,
                          gdouble        lower,
                          gdouble        upper)
685
{
686 687
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
688 689

  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
690 691 692 693

  priv = GET_PRIVATE (gse);

  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));
694
  g_return_if_fail (lower <= upper);
695

696
  gsef = (GimpSizeEntryField*) g_slist_nth_data (priv->fields, field);
697 698 699 700 701 702 703
  gsef->lower = lower;
  gsef->upper = upper;

  gimp_size_entry_set_refval (gse, field, gsef->refval);
}


704 705
/**
 * gimp_size_entry_set_value_boundaries:
706
 * @gse:   The sizeentry you want to set value boundaries for.
707 708 709 710 711 712 713 714 715 716
 * @field: The index of the field you want to set value boundaries for.
 * @lower: The new lower boundary of the value of the chosen @field.
 * @upper: The new upper boundary of the value of the chosen @field.
 *
 * Limits the range of possible values which can be entered in field # @field
 * of the #GimpSizeEntry.
 *
 * The current value of the @field will be clamped to fit in the @field's
 * new boundaries.
 *
Sven Neumann's avatar
Sven Neumann committed
717
 * NOTE: In most cases you won't be interested in this function because the
718 719
 *       #GimpSizeEntry's purpose is to shield the programmer from unit
 *       calculations. Use gimp_size_entry_set_refval_boundaries() instead.
720 721
 *       Whatever you do, don't mix these calls. A size entry should either
 *       be clamped by the value or the reference value.
722
 **/
723
void
724
gimp_size_entry_set_value_boundaries (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
725 726 727
                                      gint           field,
                                      gdouble        lower,
                                      gdouble        upper)
728
{
729 730
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
731

732
  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
733 734 735 736

  priv = GET_PRIVATE (gse);

  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));
737
  g_return_if_fail (lower <= upper);
738

739
  gsef = (GimpSizeEntryField*) g_slist_nth_data (priv->fields, field);
740 741
  gsef->min_value        = lower;
  gsef->max_value        = upper;
742

743 744 745 746
  g_object_freeze_notify (G_OBJECT (gsef->value_adjustment));

  gtk_adjustment_set_lower (gsef->value_adjustment, gsef->min_value);
  gtk_adjustment_set_upper (gsef->value_adjustment, gsef->max_value);
747

748
  if (gsef->stop_recursion) /* this is a hack (but useful ;-) */
749 750 751 752
    {
      g_object_thaw_notify (G_OBJECT (gsef->value_adjustment));
      return;
    }
753

754
  gsef->stop_recursion++;
755
  switch (priv->update_policy)
756 757 758
    {
    case GIMP_SIZE_ENTRY_UPDATE_NONE:
      break;
759

760
    case GIMP_SIZE_ENTRY_UPDATE_SIZE:
761
      switch (priv->unit)
Sven Neumann's avatar
Sven Neumann committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
        {
        case GIMP_UNIT_PIXEL:
          gimp_size_entry_set_refval_boundaries (gse, field,
                                                 gsef->min_value,
                                                 gsef->max_value);
          break;
        case GIMP_UNIT_PERCENT:
          gimp_size_entry_set_refval_boundaries (gse, field,
                                                 gsef->lower +
                                                 (gsef->upper - gsef->lower) *
                                                 gsef->min_value / 100,
                                                 gsef->lower +
                                                 (gsef->upper - gsef->lower) *
                                                 gsef->max_value / 100);
          break;
        default:
          gimp_size_entry_set_refval_boundaries (gse, field,
                                                 gsef->min_value *
                                                 gsef->resolution /
781
                                                 gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
782 783
                                                 gsef->max_value *
                                                 gsef->resolution /
784
                                                 gimp_unit_get_factor (priv->unit));
Sven Neumann's avatar
Sven Neumann committed
785 786
          break;
        }
787
      break;
788

789 790
    case GIMP_SIZE_ENTRY_UPDATE_RESOLUTION:
      gimp_size_entry_set_refval_boundaries (gse, field,
Sven Neumann's avatar
Sven Neumann committed
791
                                             gsef->min_value *
792
                                             gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
793
                                             gsef->max_value *
794
                                             gimp_unit_get_factor (priv->unit));
795
      break;
796

797 798 799
    default:
      break;
    }
800 801 802
  gsef->stop_recursion--;

  gimp_size_entry_set_value (gse, field, gsef->value);
803 804

  g_object_thaw_notify (G_OBJECT (gsef->value_adjustment));
805 806
}

807
/**
808
 * gimp_size_entry_get_value:
809
 * @gse:   The sizeentry you want to know a value of.
810
 * @field: The index of the field you want to know the value of.
811 812 813 814 815
 *
 * Returns the value of field # @field of the #GimpSizeEntry.
 *
 * The @value returned is a distance or resolution
 * in the #GimpUnit the user has selected in the #GimpSizeEntry's
816
 * #GimpUnitComboBox.
817 818 819 820 821 822
 *
 * NOTE: In most cases you won't be interested in this value because the
 *       #GimpSizeEntry's purpose is to shield the programmer from unit
 *       calculations. Use gimp_size_entry_get_refval() instead.
 *
 * Returns: The value of the chosen @field.
823
 **/
824
gdouble
825
gimp_size_entry_get_value (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
826
                           gint           field)
827
{
828 829
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
830

831
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), 0);
832

833 834 835 836 837 838
  priv = GET_PRIVATE (gse);

  g_return_val_if_fail ((field >= 0) && (field < priv->number_of_fields), 0);

  gsef = (GimpSizeEntryField *) g_slist_nth_data (priv->fields, field);

839
  return gsef->value;
840 841
}

842
static void
843
gimp_size_entry_update_value (GimpSizeEntryField *gsef,
Sven Neumann's avatar
Sven Neumann committed
844
                              gdouble             value)
845
{
846 847
  GimpSizeEntryPrivate *priv = gsef->gse->priv;

848
  if (gsef->stop_recursion > 1)
849
    return;
850

851
  gsef->value = value;
852

853
  switch (priv->update_policy)
854 855 856 857 858
    {
    case GIMP_SIZE_ENTRY_UPDATE_NONE:
      break;

    case GIMP_SIZE_ENTRY_UPDATE_SIZE:
859
      switch (priv->unit)
Sven Neumann's avatar
Sven Neumann committed
860 861 862 863 864 865 866 867 868 869 870 871
        {
        case GIMP_UNIT_PIXEL:
          gsef->refval = value;
          break;
        case GIMP_UNIT_PERCENT:
          gsef->refval =
            CLAMP (gsef->lower + (gsef->upper - gsef->lower) * value / 100,
                   gsef->min_refval, gsef->max_refval);
          break;
        default:
          gsef->refval =
            CLAMP (value * gsef->resolution /
872
                   gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
873 874 875
                   gsef->min_refval, gsef->max_refval);
          break;
        }
876
      if (priv->show_refval)
877
        gtk_adjustment_set_value (gsef->refval_adjustment, gsef->refval);
878
      break;
879

880
    case GIMP_SIZE_ENTRY_UPDATE_RESOLUTION:
881
      gsef->refval =
882
        CLAMP (value * gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
883
               gsef->min_refval, gsef->max_refval);
884
      if (priv->show_refval)
885
        gtk_adjustment_set_value (gsef->refval_adjustment, gsef->refval);
886
      break;
887

888 889 890
    default:
      break;
    }
891 892

  g_signal_emit (gsef->gse, gimp_size_entry_signals[VALUE_CHANGED], 0);
893 894
}

895
/**
896
 * gimp_size_entry_set_value:
897
 * @gse:   The sizeentry you want to set a value for.
898 899 900 901 902 903 904
 * @field: The index of the field you want to set a value for.
 * @value: The new value for @field.
 *
 * Sets the value for field # @field of the #GimpSizeEntry.
 *
 * The @value passed is treated to be a distance or resolution
 * in the #GimpUnit the user has selected in the #GimpSizeEntry's
905
 * #GimpUnitComboBox.
906 907 908 909
 *
 * NOTE: In most cases you won't be interested in this value because the
 *       #GimpSizeEntry's purpose is to shield the programmer from unit
 *       calculations. Use gimp_size_entry_set_refval() instead.
910
 **/
911
void
912
gimp_size_entry_set_value (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
913 914
                           gint           field,
                           gdouble        value)
915
{
916 917
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
918

919
  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
920

921
  priv = GET_PRIVATE (gse);
922

923 924 925
  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));

  gsef = (GimpSizeEntryField *) g_slist_nth_data (priv->fields, field);
926

927
  value = CLAMP (value, gsef->min_value, gsef->max_value);
928
  gtk_adjustment_set_value (gsef->value_adjustment, value);
929 930 931 932 933
  gimp_size_entry_update_value (gsef, value);
}


static void
934 935
gimp_size_entry_value_callback (GtkAdjustment *adjustment,
                                gpointer       data)
936 937
{
  GimpSizeEntryField *gsef;
938
  gdouble             new_value;
939

940
  gsef = (GimpSizeEntryField *) data;
941

942
  new_value = gtk_adjustment_get_value (adjustment);
943 944

  if (gsef->value != new_value)
945
    gimp_size_entry_update_value (gsef, new_value);
946 947 948
}


949 950
/**
 * gimp_size_entry_set_refval_boundaries:
951
 * @gse:   The sizeentry you want to set the reference value boundaries for.
952 953 954 955 956 957 958 959 960 961
 * @field: The index of the field you want to set the reference value
 *         boundaries for.
 * @lower: The new lower boundary of the reference value of the chosen @field.
 * @upper: The new upper boundary of the reference value of the chosen @field.
 *
 * Limits the range of possible reference values which can be entered in
 * field # @field of the #GimpSizeEntry.
 *
 * The current reference value of the @field will be clamped to fit in the
 * @field's new boundaries.
962
 **/
963
void
964 965 966 967
gimp_size_entry_set_refval_boundaries (GimpSizeEntry *gse,
                                       gint           field,
                                       gdouble        lower,
                                       gdouble        upper)
968
{
969 970
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
971

972
  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
973 974 975 976

  priv = GET_PRIVATE (gse);

  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));
977
  g_return_if_fail (lower <= upper);
978

979
  gsef = (GimpSizeEntryField *) g_slist_nth_data (priv->fields, field);
980 981 982
  gsef->min_refval = lower;
  gsef->max_refval = upper;

983
  if (priv->show_refval)
984
    {
985 986 987 988
      g_object_freeze_notify (G_OBJECT (gsef->refval_adjustment));

      gtk_adjustment_set_lower (gsef->refval_adjustment, gsef->min_refval);
      gtk_adjustment_set_upper (gsef->refval_adjustment, gsef->max_refval);
989
    }
990

991
  if (gsef->stop_recursion) /* this is a hack (but useful ;-) */
992
    {
993
      if (priv->show_refval)
994 995 996 997
        g_object_thaw_notify (G_OBJECT (gsef->refval_adjustment));

      return;
    }
998

999
  gsef->stop_recursion++;
1000
  switch (priv->update_policy)
1001 1002 1003 1004 1005
    {
    case GIMP_SIZE_ENTRY_UPDATE_NONE:
      break;

    case GIMP_SIZE_ENTRY_UPDATE_SIZE:
1006
      switch (priv->unit)
Sven Neumann's avatar
Sven Neumann committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
        {
        case GIMP_UNIT_PIXEL:
          gimp_size_entry_set_value_boundaries (gse, field,
                                                gsef->min_refval,
                                                gsef->max_refval);
          break;
        case GIMP_UNIT_PERCENT:
          gimp_size_entry_set_value_boundaries (gse, field,
                                                100 * (gsef->min_refval -
                                                       gsef->lower) /
                                                (gsef->upper - gsef->lower),
                                                100 * (gsef->max_refval -
                                                       gsef->lower) /
                                                (gsef->upper - gsef->lower));
          break;
        default:
          gimp_size_entry_set_value_boundaries (gse, field,
                                                gsef->min_refval *
1025
                                                gimp_unit_get_factor (priv->unit) /
Sven Neumann's avatar
Sven Neumann committed
1026 1027
                                                gsef->resolution,
                                                gsef->max_refval *
1028
                                                gimp_unit_get_factor (priv->unit) /
Sven Neumann's avatar
Sven Neumann committed
1029 1030 1031
                                                gsef->resolution);
          break;
        }
1032
      break;
1033

1034 1035
    case GIMP_SIZE_ENTRY_UPDATE_RESOLUTION:
      gimp_size_entry_set_value_boundaries (gse, field,
Sven Neumann's avatar
Sven Neumann committed
1036
                                            gsef->min_refval /
1037
                                            gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
1038
                                            gsef->max_refval /
1039
                                            gimp_unit_get_factor (priv->unit));
1040 1041 1042 1043 1044
      break;

    default:
      break;
    }
1045 1046 1047
  gsef->stop_recursion--;

  gimp_size_entry_set_refval (gse, field, gsef->refval);
1048

1049
  if (priv->show_refval)
1050
    g_object_thaw_notify (G_OBJECT (gsef->refval_adjustment));
1051 1052
}

1053 1054
/**
 * gimp_size_entry_set_refval_digits:
1055 1056
 * @gse:    The sizeentry you want to set the reference value digits for.
 * @field:  The index of the field you want to set the reference value for.
1057 1058 1059 1060 1061 1062 1063
 * @digits: The new number of decimal digits for the #GtkSpinButton which
 *          displays @field's reference value.
 *
 * Sets the decimal digits of field # @field of the #GimpSizeEntry to
 * @digits.
 *
 * If you don't specify this value explicitly, the reference value's number
1064 1065 1066
 * of digits will equal to 0 for #GIMP_SIZE_ENTRY_UPDATE_SIZE and to 2 for
 * #GIMP_SIZE_ENTRY_UPDATE_RESOLUTION.
 **/
1067 1068
void
gimp_size_entry_set_refval_digits (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
1069 1070
                                   gint           field,
                                   gint           digits)
1071
{
1072 1073
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
1074 1075

  g_return_if_fail (GIMP_IS_SIZE_ENTRY (gse));
1076 1077 1078 1079

  priv = GET_PRIVATE (gse);

  g_return_if_fail ((field >= 0) && (field < priv->number_of_fields));
1080 1081
  g_return_if_fail ((digits >= 0) && (digits <= 6));

1082
  gsef = (GimpSizeEntryField*) g_slist_nth_data (priv->fields, field);
1083 1084
  gsef->refval_digits = digits;

1085
  if (priv->update_policy == GIMP_SIZE_ENTRY_UPDATE_SIZE)
1086
    {
1087
      if (priv->show_refval)
Sven Neumann's avatar
Sven Neumann committed
1088 1089
        gtk_spin_button_set_digits (GTK_SPIN_BUTTON (gsef->refval_spinbutton),
                                    gsef->refval_digits);
1090
      else if (priv->unit == GIMP_UNIT_PIXEL)
Sven Neumann's avatar
Sven Neumann committed
1091 1092
        gtk_spin_button_set_digits (GTK_SPIN_BUTTON (gsef->value_spinbutton),
                                    gsef->refval_digits);
1093 1094 1095
    }
}

1096
/**
1097
 * gimp_size_entry_get_refval:
1098
 * @gse:   The sizeentry you want to know a reference value of.
1099 1100 1101 1102 1103 1104 1105 1106 1107
 * @field: The index of the field you want to know the reference value of.
 *
 * Returns the reference value for field # @field of the #GimpSizeEntry.
 *
 * The reference value is either a distance in pixels or a resolution
 * in dpi, depending on which #GimpSizeEntryUpdatePolicy you chose in
 * gimp_size_entry_new().
 *
 * Returns: The reference value of the chosen @field.
1108
 **/
1109
gdouble
1110
gimp_size_entry_get_refval (GimpSizeEntry *gse,
Sven Neumann's avatar
Sven Neumann committed
1111
                            gint           field)
1112
{
1113 1114
  GimpSizeEntryPrivate *priv;
  GimpSizeEntryField   *gsef;
1115

1116 1117
  /*  return 1.0 to avoid division by zero  */
  g_return_val_if_fail (GIMP_IS_SIZE_ENTRY (gse), 1.0);
1118

1119 1120 1121 1122 1123 1124
  priv = GET_PRIVATE (gse);

  g_return_val_if_fail ((field >= 0) && (field < priv->number_of_fields), 1.0);

  gsef = (GimpSizeEntryField*) g_slist_nth_data (priv->fields, field);

1125
  return gsef->refval;
1126 1127
}

1128
static void
1129
gimp_size_entry_update_refval (GimpSizeEntryField *gsef,
Sven Neumann's avatar
Sven Neumann committed
1130
                               gdouble             refval)
1131
{
1132 1133
  GimpSizeEntryPrivate *priv = GET_PRIVATE (gsef->gse);

1134
  if (gsef->stop_recursion > 1)
1135 1136
    return;

1137 1138
  gsef->refval = refval;

1139
  switch (priv->update_policy)
1140 1141 1142 1143 1144
    {
    case GIMP_SIZE_ENTRY_UPDATE_NONE:
      break;

    case GIMP_SIZE_ENTRY_UPDATE_SIZE:
1145
      switch (priv->unit)
Sven Neumann's avatar
Sven Neumann committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        {
        case GIMP_UNIT_PIXEL:
          gsef->value = refval;
          break;
        case GIMP_UNIT_PERCENT:
          gsef->value =
            CLAMP (100 * (refval - gsef->lower) / (gsef->upper - gsef->lower),
                   gsef->min_value, gsef->max_value);
          break;
        default:
          gsef->value =
1157
            CLAMP (refval * gimp_unit_get_factor (priv->unit) /
Sven Neumann's avatar
Sven Neumann committed
1158 1159 1160 1161
                   gsef->resolution,
                   gsef->min_value, gsef->max_value);
          break;
        }
1162
      gtk_adjustment_set_value (gsef->value_adjustment, gsef->value);
1163
      break;
1164

1165
    case GIMP_SIZE_ENTRY_UPDATE_RESOLUTION:
1166
      gsef->value =
1167
        CLAMP (refval / gimp_unit_get_factor (priv->unit),
Sven Neumann's avatar
Sven Neumann committed
1168
               gsef->min_value, gsef->max_value);
1169
      gtk_adjustment_set_value (gsef->value_adjustment, gsef->value);
1170 1171 1172 1173 1174
      break;

    default:
      break;
    }
1175 1176