File: gimpresolutionentry-private.c

package info (click to toggle)
gimp 3.0.4-6.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 210,548 kB
  • sloc: ansic: 842,405; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (711 lines) | stat: -rw-r--r-- 26,485 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpresolutionentry.c
 * Copyright (C) 2024 Jehan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"
#include "gimpui.h"
#include "gimpresolutionentry-private.h"

#include "libgimp-intl.h"


enum
{
  PROP_0,
  PROP_WIDTH,
  PROP_HEIGHT,
  PROP_PIXEL_DENSITY,
  PROP_UNIT,
  PROP_KEEP_RATIO,
  N_PROPS
};

struct _GimpResolutionEntry
{
  GtkGrid                   parent_instance;

  gint                      width;
  gint                      height;
  gdouble                   ppi;
  GimpUnit                 *unit;
  gdouble                   ratio;
  gboolean                  keep_ratio;

  GtkWidget                *phy_width_label;
  GtkWidget                *phy_height_label;

  GtkWidget                *chainbutton;
};

G_DEFINE_FINAL_TYPE (GimpResolutionEntry, gimp_resolution_entry, GTK_TYPE_GRID)

#define parent_class gimp_resolution_entry_parent_class


static void        gimp_resolution_entry_class_init      (GimpResolutionEntryClass *class);
static void        gimp_resolution_entry_init            (GimpResolutionEntry      *gre);
static void        gimp_resolution_entry_constructed     (GObject                  *object);
static void        gimp_resolution_entry_set_property    (GObject                  *object,
                                                          guint                     property_id,
                                                          const GValue             *value,
                                                          GParamSpec               *pspec);
static void        gimp_resolution_entry_get_property    (GObject                  *object,
                                                          guint                     property_id,
                                                          GValue                   *value,
                                                          GParamSpec               *pspec);

static GtkWidget * gimp_resolution_entry_attach_label    (GimpResolutionEntry      *entry,
                                                          const gchar              *text,
                                                          gint                      row,
                                                          gint                      column,
                                                          gfloat                    alignment);
static void        gimp_resolution_entry_format_label    (GimpResolutionEntry      *entry,
                                                          GtkWidget                *label,
                                                          gdouble                   size);
static void        gimp_resolution_entry_update_labels   (GimpResolutionEntry      *entry,
                                                          GParamSpec               *param_spec,
                                                          GtkWidget                *label);

static gboolean    gimp_resolution_entry_ppi_to_unit     (GBinding            *binding,
                                                          const GValue        *from_value,
                                                          GValue              *to_value,
                                                          GimpResolutionEntry *entry);
static gboolean    gimp_resolution_entry_unit_to_ppi     (GBinding            *binding,
                                                          const GValue        *from_value,
                                                          GValue              *to_value,
                                                          GimpResolutionEntry *entry);


static GParamSpec *props[N_PROPS] = { NULL, };

static void
gimp_resolution_entry_class_init (GimpResolutionEntryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = gimp_resolution_entry_constructed;
  object_class->set_property = gimp_resolution_entry_set_property;
  object_class->get_property = gimp_resolution_entry_get_property;

  props[PROP_WIDTH] = g_param_spec_int ("width",
                                        "Width in pixel",
                                        NULL,
                                        GIMP_MIN_IMAGE_SIZE, GIMP_MAX_IMAGE_SIZE, GIMP_MIN_IMAGE_SIZE,
                                        GIMP_PARAM_READWRITE |
                                        G_PARAM_EXPLICIT_NOTIFY |
                                        G_PARAM_CONSTRUCT);
  props[PROP_HEIGHT] = g_param_spec_int ("height",
                                         "Height in pixel",
                                         NULL,
                                         GIMP_MIN_IMAGE_SIZE, GIMP_MAX_IMAGE_SIZE, GIMP_MIN_IMAGE_SIZE,
                                         GIMP_PARAM_READWRITE |
                                         G_PARAM_EXPLICIT_NOTIFY |
                                         G_PARAM_CONSTRUCT);
  props[PROP_PIXEL_DENSITY] = g_param_spec_double ("pixel-density",
                                                   "Pixel density in pixel per inch",
                                                   NULL,
                                                   GIMP_MIN_RESOLUTION, GIMP_MAX_RESOLUTION, 300.0,
                                                   GIMP_PARAM_READWRITE |
                                                   G_PARAM_EXPLICIT_NOTIFY |
                                                   G_PARAM_CONSTRUCT);
  props[PROP_UNIT] = gimp_param_spec_unit ("unit",
                                           "Physical unit for the pixel density",
                                           _("This unit is used to select the pixel density "
                                           "and show dimensions in physical unit"),
                                           FALSE, FALSE,
                                           gimp_unit_inch (),
                                           GIMP_PARAM_READWRITE |
                                           G_PARAM_EXPLICIT_NOTIFY |
                                           G_PARAM_CONSTRUCT);
  props[PROP_KEEP_RATIO] = g_param_spec_boolean ("keep-ratio",
                                                 _("_Keep aspect ratio"),
                                                 _("Force dimensions with aspect ratio"),
                                                 TRUE,
                                                 GIMP_PARAM_READWRITE |
                                                 G_PARAM_EXPLICIT_NOTIFY |
                                                 G_PARAM_CONSTRUCT);

  g_object_class_install_properties (object_class, N_PROPS, props);
}

static void
gimp_resolution_entry_init (GimpResolutionEntry *entry)
{
  entry->width      = 0;
  entry->height     = 0;
  entry->ppi        = 300.0;
  entry->unit       = gimp_unit_inch ();
  entry->keep_ratio = TRUE;

  gtk_grid_set_row_spacing (GTK_GRID (entry), 2);
  gtk_grid_set_column_spacing (GTK_GRID (entry), 4);
}

static void
gimp_resolution_entry_constructed (GObject *object)
{
  GimpResolutionEntry *entry = GIMP_RESOLUTION_ENTRY (object);
  GtkTreeModel        *model;
  GtkWidget           *label;
  GtkWidget           *widget;
  GBinding            *binding;
  GtkAdjustment       *adj;

  g_return_if_fail (entry->height != 0);

  /* Initial ratio is from the initial values. */
  entry->ratio = (gdouble) entry->width / (gdouble) entry->height;

  widget = gimp_prop_spin_button_new (object, "pixel-density", 1.0, 10.0, 2);
  adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
  binding = g_object_get_data (G_OBJECT (adj), "gimp-prop-adjustment-binding");
  g_binding_unbind (binding);
  binding = g_object_bind_property_full (object, "pixel-density",
                                         widget, "value",
                                         G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
                                         (GBindingTransformFunc) gimp_resolution_entry_ppi_to_unit,
                                         (GBindingTransformFunc) gimp_resolution_entry_unit_to_ppi,
                                         entry, NULL);
  g_object_set_data (G_OBJECT (adj), "gimp-prop-adjustment-binding", binding);
  gtk_grid_attach (GTK_GRID (entry), widget, 1, 3, 1, 1);
  gtk_widget_show (widget);

  widget = gimp_prop_unit_combo_box_new (object, "unit");
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
  g_object_set (model,
                "short-format", _("pixels/%a"),
                "long-format",  _("pixels/%a"),
                NULL);
  gtk_grid_attach (GTK_GRID (entry), widget, 3, 3, 1, 1);
  gtk_widget_show (widget);

  widget = gimp_prop_spin_button_new (object, "width", 1.0, 10.0, 0);
  gtk_grid_attach (GTK_GRID (entry), widget, 1, 1, 1, 1);
  gtk_widget_show (widget);

  label = g_object_new (GTK_TYPE_LABEL,
                        "xalign", 0.0,
                        "yalign", 0.5,
                        NULL);
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);
  gimp_resolution_entry_format_label (entry, label,
                                      entry->width / entry->ppi);
  entry->phy_width_label = label;
  gtk_grid_attach (GTK_GRID (entry), entry->phy_width_label, 3, 1, 1, 1);
  gtk_widget_show (entry->phy_width_label);

  widget = gimp_prop_spin_button_new (object, "height", 1.0, 10.0, 0);
  gtk_grid_attach (GTK_GRID (entry), widget, 1, 2, 1, 1);
  gtk_widget_show (widget);

  label = g_object_new (GTK_TYPE_LABEL,
                        "xalign", 0.0,
                        "yalign", 0.5,
                        NULL);
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);
  gimp_resolution_entry_format_label (entry, label,
                                      entry->height / entry->ppi);
  entry->phy_height_label = label;
  gtk_grid_attach (GTK_GRID (entry), entry->phy_height_label, 3, 2, 1, 1);
  gtk_widget_show (entry->phy_height_label);

  g_signal_connect (object, "notify::width",
                    G_CALLBACK (gimp_resolution_entry_update_labels),
                    entry->phy_width_label);
  g_signal_connect (object, "notify::height",
                    G_CALLBACK (gimp_resolution_entry_update_labels),
                    entry->phy_height_label);
  g_signal_connect (object, "notify::pixel-density",
                    G_CALLBACK (gimp_resolution_entry_update_labels),
                    NULL);
}

static void
gimp_resolution_entry_set_property (GObject      *object,
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  GimpResolutionEntry *entry = GIMP_RESOLUTION_ENTRY (object);

  switch (property_id)
    {
    case PROP_WIDTH:
      gimp_resolution_entry_set_width (entry, g_value_get_int (value));
      break;
    case PROP_HEIGHT:
      gimp_resolution_entry_set_height (entry, g_value_get_int (value));
      break;
    case PROP_PIXEL_DENSITY:
      gimp_resolution_entry_set_pixel_density (entry, g_value_get_double (value));
      break;
    case PROP_UNIT:
      gimp_resolution_entry_set_unit (entry, g_value_get_object (value));
      break;
    case PROP_KEEP_RATIO:
      gimp_resolution_entry_set_keep_ratio (entry, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_resolution_entry_get_property (GObject    *object,
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GimpResolutionEntry *entry = GIMP_RESOLUTION_ENTRY (object);

  switch (property_id)
    {
    case PROP_WIDTH:
      g_value_set_int (value, entry->width);
      break;
    case PROP_HEIGHT:
      g_value_set_int (value, entry->height);
      break;
    case PROP_PIXEL_DENSITY:
      g_value_set_double (value, entry->ppi);
      break;
    case PROP_UNIT:
      g_value_set_object (value, entry->unit);
      break;
    case PROP_KEEP_RATIO:
      g_value_set_boolean (value, entry->keep_ratio);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

GtkWidget *
gimp_prop_resolution_entry_new (GObject     *config,
                                const gchar *width_prop,
                                const gchar *height_prop,
                                const gchar *ppi_prop,
                                const gchar *unit_prop)
{
  GtkWidget  *widget;
  GParamSpec *w_pspec;
  GParamSpec *h_pspec;
  GParamSpec *d_pspec;
  GParamSpec *u_pspec;
  gint        width  = 0;
  gint        height = 0;
  gdouble     ppi    = 300.0;
  GimpUnit   *unit   = gimp_unit_inch ();

  g_return_val_if_fail (G_IS_OBJECT (config), NULL);
  g_return_val_if_fail (width_prop != NULL, NULL);
  g_return_val_if_fail (height_prop != NULL, NULL);
  g_return_val_if_fail (ppi_prop != NULL, NULL);
  g_return_val_if_fail (unit_prop != NULL, NULL);

  w_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), width_prop);
  h_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), height_prop);
  d_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), ppi_prop);
  u_pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (config), unit_prop);

  g_return_val_if_fail (w_pspec != NULL, NULL);
  g_return_val_if_fail (h_pspec != NULL, NULL);
  g_return_val_if_fail (d_pspec != NULL, NULL);
  g_return_val_if_fail (u_pspec != NULL, NULL);

  g_object_get (config,
                width_prop,  &width,
                height_prop, &height,
                ppi_prop,    &ppi,
                unit_prop,   &unit,
                NULL);

  widget = gimp_resolution_entry_new (g_param_spec_get_nick (w_pspec), width,
                                      g_param_spec_get_nick (h_pspec), height,
                                      g_param_spec_get_nick (d_pspec), ppi,
                                      unit);

  g_object_bind_property (config, width_prop,
                          widget, "width",
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  g_object_bind_property (config, height_prop,
                          widget, "height",
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  g_object_bind_property (config, ppi_prop,
                          widget, "pixel-density",
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
  g_object_bind_property (config, unit_prop,
                          widget, "unit",
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);

  return widget;
}

/**
 * gimp_resolution_entry_new:
 * @width_label:       Optional label for the width control.
 * @width:             Width of the item, specified in pixels.
 * @height_label:      Optional label for the height control.
 * @height:            Height of the item, specified in pixels.
 * @res_label:         Optional label for the resolution entry.
 * @pixel_density:     The initial resolution in pixel per inch.
 * @display_unit:      The display unit.
 *
 * Creates a new #GimpResolutionEntry widget.
 *
 * The #GimpResolutionEntry is derived from #GtkGrid and will have
 * an empty border of one cell width on each side plus an empty column left
 * of the #GimpUnitMenu to allow the caller to add labels or other widgets.
 *
 * A #GimpChainButton is displayed if independent is set to %TRUE.
 *
 * Returns: A pointer to the new #GimpResolutionEntry widget.
 **/
GtkWidget *
gimp_resolution_entry_new (const gchar *width_label,
                           gint         width,
                           const gchar *height_label,
                           gint         height,
                           const gchar *res_label,
                           gdouble      pixel_density,
                           GimpUnit    *display_unit)
{
  GimpResolutionEntry *entry;

  entry = g_object_new (GIMP_TYPE_RESOLUTION_ENTRY,
                        "width",         width,
                        "height",        height,
                        "pixel-density", pixel_density,
                        "unit",          display_unit,
                        NULL);

  if (width_label)
    gimp_resolution_entry_attach_label (entry, width_label,  1, 0, 0.0);

  if (height_label)
    gimp_resolution_entry_attach_label (entry, height_label, 2, 0, 0.0);

  if (res_label)
    gimp_resolution_entry_attach_label (entry, res_label,    3, 0, 0.0);

  return GTK_WIDGET (entry);
}

static gboolean
gimp_resolution_entry_idle_notify (GimpResolutionEntry *entry)
{
  g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_WIDTH]);
  g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_HEIGHT]);

  return G_SOURCE_REMOVE;
}

void
gimp_resolution_entry_set_width (GimpResolutionEntry *entry,
                                 gint                 width)
{
  if (width == 0)
    {
      /* Do nothing, i.e. revert back to value it was. Yet notify after an idle
       * source, otherwise the entry is not updated (the binding likely blocks
       * notifications for this property to avoid infinite loops.
       */
      g_idle_add ((GSourceFunc) gimp_resolution_entry_idle_notify, entry);
    }
  else if (entry->width != width)
    {
      g_object_freeze_notify (G_OBJECT (entry));

      if (entry->keep_ratio && entry->width != 0)
        {
          gint height;

          height = (gint) ((gdouble) width / entry->ratio);

          if (height != entry->height)
            {
              entry->height = height;
              g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_HEIGHT]);
            }
        }

      entry->width = width;
      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_WIDTH]);

      g_object_thaw_notify (G_OBJECT (entry));
    }
}

void
gimp_resolution_entry_set_height (GimpResolutionEntry *entry,
                                  gint                 height)
{
  if (height == 0)
    {
      /* Do nothing, i.e. revert back to value it was. Yet notify after an idle
       * source, otherwise the entry is not updated (the binding likely blocks
       * notifications for this property to avoid infinite loops.
       */
      g_idle_add ((GSourceFunc) gimp_resolution_entry_idle_notify, entry);
    }
  else if (entry->height != height)
    {
      g_object_freeze_notify (G_OBJECT (entry));

      if (entry->keep_ratio && entry->height != 0)
        {
          gint width;

          width = (gint) ((gdouble) height * entry->ratio);

          if (width != entry->width)
            {
              entry->width = width;
              g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_WIDTH]);
            }
        }

      entry->height = height;
      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_HEIGHT]);

      g_object_thaw_notify (G_OBJECT (entry));
    }
}

void
gimp_resolution_entry_set_pixel_density (GimpResolutionEntry *entry,
                                         gdouble              ppi)
{
  if (entry->ppi != ppi)
    {
      entry->ppi = ppi;
      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_PIXEL_DENSITY]);
    }
}

void
gimp_resolution_entry_set_unit (GimpResolutionEntry *entry,
                                GimpUnit            *unit)
{
  g_return_if_fail (unit != gimp_unit_pixel ());
  g_return_if_fail (unit != gimp_unit_percent ());

  if (entry->unit != unit)
    {
      entry->unit = unit;
      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_UNIT]);
      /* Even though the pixel density (in pixel per inch) has not in fact
       * changed, we send a notification to force a refresh of the displayed
       * pixel per other-unit value in the entry field.
       */
      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_PIXEL_DENSITY]);

      if (entry->phy_width_label)
        gimp_resolution_entry_format_label (entry, entry->phy_width_label,
                                            gimp_unit_get_factor (entry->unit) *
                                            entry->width / entry->ppi);
      if (entry->phy_height_label)
        gimp_resolution_entry_format_label (entry, entry->phy_height_label,
                                            gimp_unit_get_factor (entry->unit) *
                                            entry->height / entry->ppi);
    }
}

void
gimp_resolution_entry_set_keep_ratio (GimpResolutionEntry *entry,
                                      gboolean             keep_ratio)
{
  if (keep_ratio != entry->keep_ratio)
    {
      entry->keep_ratio = keep_ratio;

      if (entry->keep_ratio)
        entry->ratio = (gdouble) entry->width / (gdouble) entry->height;

      g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_KEEP_RATIO]);
    }
}

gint
gimp_resolution_entry_get_width (GimpResolutionEntry *entry)
{
  return entry->width;
}

gint
gimp_resolution_entry_get_height (GimpResolutionEntry *entry)
{
  return entry->height;
}

gdouble
gimp_resolution_entry_get_density (GimpResolutionEntry *entry)
{
  return entry->ppi;
}

GimpUnit *
gimp_resolution_entry_get_unit (GimpResolutionEntry *entry)
{
  return entry->unit;
}

gboolean
gimp_resolution_entry_get_keep_ratio (GimpResolutionEntry *entry)
{
  return entry->keep_ratio;
}


/* Private functions. */

/**
 * gimp_resolution_entry_attach_label:
 * @gre:       The #GimpResolutionEntry 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.
 * @alignment: The horizontal alignment of the label.
 *
 * Attaches a #GtkLabel to the #GimpResolutionEntry (which is a #GtkGrid).
 *
 * Returns: A pointer to the new #GtkLabel widget.
 **/
static GtkWidget *
gimp_resolution_entry_attach_label (GimpResolutionEntry *gre,
                                    const gchar         *text,
                                    gint                 row,
                                    gint                 column,
                                    gfloat               alignment)
{
  GtkWidget *label;

  g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), NULL);
  g_return_val_if_fail (text != NULL, NULL);

  label = gtk_label_new_with_mnemonic (text);

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

      children = gtk_container_get_children (GTK_CONTAINER (gre));

      for (list = children; list; list = g_list_next (list))
        {
          GtkWidget *child = list->data;
          gint       left_attach;
          gint       top_attach;

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

          if (left_attach == 1 && top_attach == row)
            {
              gtk_label_set_mnemonic_widget (GTK_LABEL (label), child);
              break;
            }
        }

      g_list_free (children);
    }

  gtk_label_set_xalign (GTK_LABEL (label), alignment);

  gtk_grid_attach (GTK_GRID (gre), label, column, row, 1, 1);
  gtk_widget_show (label);

  return label;
}

static void
gimp_resolution_entry_format_label (GimpResolutionEntry *entry,
                                    GtkWidget           *label,
                                    gdouble              size_inch)
{
  gchar *format = g_strdup_printf ("%%.%df %%s",
                                   gimp_unit_get_digits (entry->unit));
  gchar *text = g_strdup_printf (format,
                                 size_inch * gimp_unit_get_factor (entry->unit),
                                 gimp_unit_get_name (entry->unit));
  g_free (format);

  gtk_label_set_text (GTK_LABEL (label), text);
  g_free (text);
}

static void
gimp_resolution_entry_update_labels (GimpResolutionEntry *entry,
                                     GParamSpec          *param_spec,
                                     GtkWidget           *label)
{
  if ((label == NULL && entry->phy_width_label != NULL) ||
      (label != NULL && label == entry->phy_width_label))
    gimp_resolution_entry_format_label (entry, entry->phy_width_label,
                                        entry->width / entry->ppi);

  if ((label == NULL && entry->phy_height_label != NULL) ||
      (label != NULL && label == entry->phy_height_label))
    gimp_resolution_entry_format_label (entry, entry->phy_height_label,
                                        entry->height / entry->ppi);
}

static gboolean
gimp_resolution_entry_ppi_to_unit (GBinding            *binding,
                                   const GValue        *from_value,
                                   GValue              *to_value,
                                   GimpResolutionEntry *entry)
{
  gdouble ppi = g_value_get_double (from_value);

  g_value_set_double (to_value, ppi / gimp_unit_get_factor (entry->unit));

  return TRUE;
}

static gboolean
gimp_resolution_entry_unit_to_ppi (GBinding            *binding,
                                   const GValue        *from_value,
                                   GValue              *to_value,
                                   GimpResolutionEntry *entry)
{
  gdouble ppu = g_value_get_double (from_value);

  g_value_set_double (to_value, ppu * gimp_unit_get_factor (entry->unit));

  return TRUE;
}