~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

1 by Daniel Holbach
Import upstream version 2.2.9
1
/* LIBGIMP - The GIMP Library
2
 * Copyright (C) 1995-1999 Peter Mattis and Spencer Kimball
3
 *
4
 * gimpunitmenu.c
5
 * Copyright (C) 1999 Michael Natterer <mitch@gimp.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Library General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 * Boston, MA 02111-1307, USA.
21
 */
22
23
#include "config.h"
24
25
#include <gtk/gtk.h>
26
27
#include "libgimpbase/gimpbase.h"
28
29
#include "gimpwidgetstypes.h"
30
31
#include "gimpdialog.h"
32
#include "gimphelpui.h"
33
#include "gimpunitmenu.h"
34
#include "gimpwidgets.h"
35
36
#include "libgimp/libgimp-intl.h"
37
38
39
enum
40
{
41
  UNIT_CHANGED,
42
  LAST_SIGNAL
43
};
44
45
enum
46
{
47
  UNIT_COLUMN,
48
  FACTOR_COLUMN,
49
  DATA_COLUMN,
50
  NUM_COLUMNS
51
};
52
53
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
54
static void          gimp_unit_menu_finalize     (GObject     *object);
55
56
static const gchar * gimp_unit_menu_build_string (const gchar *format,
57
                                                  GimpUnit     unit);
58
static void          gimp_unit_menu_callback     (GtkWidget   *widget,
59
                                                  gpointer     data);
60
61
62
G_DEFINE_TYPE (GimpUnitMenu, gimp_unit_menu, GTK_TYPE_OPTION_MENU)
63
64
#define parent_class gimp_unit_menu_parent_class
1 by Daniel Holbach
Import upstream version 2.2.9
65
66
static guint gimp_unit_menu_signals[LAST_SIGNAL] = { 0 };
67
68
69
static void
70
gimp_unit_menu_class_init (GimpUnitMenuClass *klass)
71
{
72
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74
  /**
75
   * GimpUnitMenu::unit-changed:
76
   *
77
   * This signal is emitted whenever the user selects a #GimpUnit from
78
   * the #GimpUnitMenu.
79
   **/
80
  gimp_unit_menu_signals[UNIT_CHANGED] =
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
81
    g_signal_new ("unit-changed",
82
                  G_TYPE_FROM_CLASS (klass),
83
                  G_SIGNAL_RUN_FIRST,
84
                  G_STRUCT_OFFSET (GimpUnitMenuClass, unit_changed),
85
                  NULL, NULL,
86
                  g_cclosure_marshal_VOID__VOID,
87
                  G_TYPE_NONE, 0);
1 by Daniel Holbach
Import upstream version 2.2.9
88
89
  object_class->finalize = gimp_unit_menu_finalize;
90
91
  klass->unit_changed    = NULL;
92
}
93
94
static void
95
gimp_unit_menu_init (GimpUnitMenu *menu)
96
{
97
  menu->format       = NULL;
98
  menu->unit         = GIMP_UNIT_PIXEL;
99
  menu->show_pixels  = FALSE;
100
  menu->show_percent = FALSE;
101
  menu->selection    = NULL;
102
  menu->tv           = NULL;
103
}
104
105
static void
106
gimp_unit_menu_finalize (GObject *object)
107
{
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
108
  GimpUnitMenu *menu = GIMP_UNIT_MENU (object);
1 by Daniel Holbach
Import upstream version 2.2.9
109
110
  if (menu->format)
111
    {
112
      g_free (menu->format);
113
      menu->format = NULL;
114
    }
115
116
  G_OBJECT_CLASS (parent_class)->finalize (object);
117
}
118
119
/**
120
 * gimp_unit_menu_new:
121
 * @format:       A printf-like format string which is used to create the unit
122
 *                strings.
123
 * @unit:         The initially selected unit.
124
 * @show_pixels:  %TRUE if the unit menu should contain an item for
125
 *                GIMP_UNIT_PIXEL.
126
 * @show_percent: %TRUE in the unit menu should contain an item for
127
 *                GIMP_UNIT_PERCENT.
128
 * @show_custom:  %TRUE if the unit menu should contain a "More..." item for
129
 *                opening the user-defined-unit selection dialog.
130
 *
131
 * Creates a new #GimpUnitMenu widget.
132
 *
133
 * The @format string supports the following percent expansions:
134
 *
135
 * <informaltable pgwide="1" frame="none" role="enum">
136
 *   <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/>
137
 *     <tbody>
138
 *       <row>
139
 *         <entry>% f</entry>
140
 *         <entry>Factor (how many units make up an inch)</entry>
141
 *        </row>
142
 *       <row>
143
 *         <entry>% y</entry>
144
 *         <entry>Symbol (e.g. "''" for GIMP_UNIT_INCH)</entry>
145
 *       </row>
146
 *       <row>
147
 *         <entry>% a</entry>
148
 *         <entry>Abbreviation</entry>
149
 *       </row>
150
 *       <row>
151
 *         <entry>% s</entry>
152
 *         <entry>Singular</entry>
153
 *       </row>
154
 *       <row>
155
 *         <entry>% p</entry>
156
 *         <entry>Plural</entry>
157
 *       </row>
158
 *       <row>
159
 *         <entry>%%</entry>
160
 *         <entry>Literal percent</entry>
161
 *       </row>
162
 *     </tbody>
163
 *   </tgroup>
164
 * </informaltable>
165
 *
166
 * Returns: A pointer to the new #GimpUnitMenu widget.
167
 **/
168
GtkWidget *
169
gimp_unit_menu_new (const gchar *format,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
170
                    GimpUnit     unit,
171
                    gboolean     show_pixels,
172
                    gboolean     show_percent,
173
                    gboolean     show_custom)
1 by Daniel Holbach
Import upstream version 2.2.9
174
{
175
  GimpUnitMenu *unit_menu;
176
  GtkWidget    *menu;
177
  GtkWidget    *menuitem;
178
  GimpUnit      u;
179
180
  g_return_val_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
181
                         (unit < gimp_unit_get_number_of_units ())) ||
182
                        (unit == GIMP_UNIT_PERCENT), NULL);
1 by Daniel Holbach
Import upstream version 2.2.9
183
184
  if ((unit >= gimp_unit_get_number_of_built_in_units ()) &&
185
      (unit != GIMP_UNIT_PERCENT))
186
    show_custom = TRUE;
187
188
  unit_menu = g_object_new (GIMP_TYPE_UNIT_MENU, NULL);
189
190
  unit_menu->format       = g_strdup (format);
191
  unit_menu->show_pixels  = show_pixels;
192
  unit_menu->show_percent = show_percent;
193
194
  menu = gtk_menu_new ();
195
  for (u = show_pixels ? GIMP_UNIT_PIXEL : GIMP_UNIT_INCH;
196
       u < gimp_unit_get_number_of_built_in_units ();
197
       u++)
198
    {
199
      /*  special cases "pixels" and "percent"  */
200
      if (u == GIMP_UNIT_INCH)
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
201
        {
202
          if (show_percent)
203
            {
204
              menuitem =
205
                gtk_menu_item_new_with_label
206
                (gimp_unit_menu_build_string (format, GIMP_UNIT_PERCENT));
207
              gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
208
              g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
1 by Daniel Holbach
Import upstream version 2.2.9
209
                                 GINT_TO_POINTER (GIMP_UNIT_PERCENT));
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
210
              gtk_widget_show (menuitem);
1 by Daniel Holbach
Import upstream version 2.2.9
211
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
212
              g_signal_connect (menuitem, "activate",
1 by Daniel Holbach
Import upstream version 2.2.9
213
                                G_CALLBACK (gimp_unit_menu_callback),
214
                                unit_menu);
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
215
            }
1 by Daniel Holbach
Import upstream version 2.2.9
216
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
217
          if (show_pixels || show_percent)
218
            {
219
              menuitem = gtk_menu_item_new ();
220
              gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
221
              gtk_widget_set_sensitive (menuitem, FALSE);
222
              gtk_widget_show (menuitem);
223
            }
224
        }
1 by Daniel Holbach
Import upstream version 2.2.9
225
226
      menuitem =
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
227
        gtk_menu_item_new_with_label (gimp_unit_menu_build_string (format, u));
1 by Daniel Holbach
Import upstream version 2.2.9
228
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
229
      g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
230
                         GINT_TO_POINTER (u));
231
      gtk_widget_show (menuitem);
232
233
      g_signal_connect (menuitem, "activate",
234
                        G_CALLBACK (gimp_unit_menu_callback),
235
                        unit_menu);
236
    }
237
238
  if ((unit >= gimp_unit_get_number_of_built_in_units ()) &&
239
      (unit != GIMP_UNIT_PERCENT))
240
    {
241
      menuitem = gtk_menu_item_new ();
242
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
243
      gtk_widget_set_sensitive (menuitem, FALSE);
244
      gtk_widget_show (menuitem);
245
246
      menuitem =
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
247
        gtk_menu_item_new_with_label (gimp_unit_menu_build_string (format,
1 by Daniel Holbach
Import upstream version 2.2.9
248
                                                                   unit));
249
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
250
      g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
251
                         GINT_TO_POINTER (unit));
252
      gtk_widget_show (menuitem);
253
254
      g_signal_connect (menuitem, "activate",
255
                        G_CALLBACK (gimp_unit_menu_callback),
256
                        unit_menu);
257
    }
258
259
  if (show_custom)
260
    {
261
      menuitem = gtk_menu_item_new ();
262
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
263
      gtk_widget_set_sensitive (menuitem, FALSE);
264
      gtk_widget_show (menuitem);
265
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
266
      menuitem = gtk_menu_item_new_with_label (_("More..."));
1 by Daniel Holbach
Import upstream version 2.2.9
267
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
268
      g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
269
                         GINT_TO_POINTER (GIMP_UNIT_PERCENT + 1));
270
      gtk_widget_show (menuitem);
271
272
      g_signal_connect (menuitem, "activate",
273
                        G_CALLBACK (gimp_unit_menu_callback),
274
                        unit_menu);
275
    }
276
277
  gtk_option_menu_set_menu (GTK_OPTION_MENU (unit_menu), menu);
278
279
  unit_menu->unit = unit;
280
  gtk_option_menu_set_history (GTK_OPTION_MENU (unit_menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
281
                               (unit == GIMP_UNIT_PIXEL) ? 0 :
282
                               ((unit == GIMP_UNIT_PERCENT) ?
283
                                (show_pixels ? 1 : 0) :
284
                                (((show_pixels || show_percent) ? 2 : 0) +
285
                                 ((show_pixels && show_percent) ? 1 : 0) +
286
                                 ((unit < GIMP_UNIT_END) ?
287
                                  (unit - 1) : GIMP_UNIT_END))));
1 by Daniel Holbach
Import upstream version 2.2.9
288
289
  return GTK_WIDGET (unit_menu);
290
}
291
292
/**
293
 * gimp_unit_menu_set_unit:
294
 * @menu:  The unit menu you want to set the unit for.
295
 * @unit: The new unit.
296
 *
297
 * Sets a new #GimpUnit for the specified #GimpUnitMenu.
298
 **/
299
void
300
gimp_unit_menu_set_unit (GimpUnitMenu *menu,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
301
                         GimpUnit      unit)
1 by Daniel Holbach
Import upstream version 2.2.9
302
{
303
  GtkWidget *menuitem = NULL;
304
  GList     *items;
305
  gint       user_unit;
306
307
  g_return_if_fail (GIMP_IS_UNIT_MENU (menu));
308
  g_return_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
309
                     ((unit > GIMP_UNIT_PIXEL) || menu->show_pixels) &&
310
                     (unit < gimp_unit_get_number_of_units ())) ||
311
                    ((unit == GIMP_UNIT_PERCENT) && menu->show_percent));
1 by Daniel Holbach
Import upstream version 2.2.9
312
313
  if (unit == menu->unit)
314
    return;
315
316
  items = GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu)->children;
317
  user_unit = (GIMP_UNIT_END +
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
318
               (((menu->show_pixels || menu->show_percent) ? 2 : 0) +
319
                ((menu->show_pixels && menu->show_percent) ? 1 : 0)));
1 by Daniel Holbach
Import upstream version 2.2.9
320
321
  if ((unit >= GIMP_UNIT_END) && (unit != GIMP_UNIT_PERCENT))
322
    {
323
      if ((g_list_length (items) - 3) >= user_unit)
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
324
        {
325
          gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
326
                                                           user_unit - 1)));
327
          gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
328
                                                           user_unit - 1)));
329
        }
1 by Daniel Holbach
Import upstream version 2.2.9
330
331
      menuitem = gtk_menu_item_new ();
332
      gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
333
                             menuitem);
1 by Daniel Holbach
Import upstream version 2.2.9
334
      gtk_widget_set_sensitive (menuitem, FALSE);
335
      gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
336
                              menuitem, user_unit - 1);
1 by Daniel Holbach
Import upstream version 2.2.9
337
      gtk_widget_show (menuitem);
338
339
      menuitem =
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
340
        gtk_menu_item_new_with_label (gimp_unit_menu_build_string (menu->format,
341
                                                                   unit));
1 by Daniel Holbach
Import upstream version 2.2.9
342
      gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
343
                             menuitem);
1 by Daniel Holbach
Import upstream version 2.2.9
344
      g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
345
                         GINT_TO_POINTER (unit));
346
      gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
347
                              menuitem, user_unit);
1 by Daniel Holbach
Import upstream version 2.2.9
348
      gtk_widget_show (menuitem);
349
350
      g_signal_connect (menuitem, "activate",
351
                        G_CALLBACK (gimp_unit_menu_callback),
352
                        menu);
353
    }
354
355
  menu->unit = unit;
356
  gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
357
                               (unit == GIMP_UNIT_PIXEL) ? 0 :
358
                               ((unit == GIMP_UNIT_PERCENT) ?
359
                                (menu->show_pixels ? 1 : 0) :
360
                                (((menu->show_pixels ||
361
                                   menu->show_percent) ? 2 : 0) +
362
                                 ((menu->show_pixels &&
363
                                   menu->show_percent) ? 1 : 0) +
364
                                 ((unit < GIMP_UNIT_END) ?
365
                                  (unit - 1) : GIMP_UNIT_END))));
1 by Daniel Holbach
Import upstream version 2.2.9
366
367
  g_signal_emit (menu, gimp_unit_menu_signals[UNIT_CHANGED], 0);
368
}
369
370
/**
371
 * gimp_unit_menu_get_unit:
372
 * @menu: The unit menu you want to know the unit of.
373
 *
374
 * Returns the #GimpUnit the user has selected from the #GimpUnitMenu.
375
 *
376
 * Returns: The unit the user has selected.
377
 **/
378
GimpUnit
379
gimp_unit_menu_get_unit (GimpUnitMenu *menu)
380
{
381
  g_return_val_if_fail (GIMP_IS_UNIT_MENU (menu), GIMP_UNIT_INCH);
382
383
  return menu->unit;
384
}
385
386
387
/**
388
 * gimp_unit_menu_set_pixel_digits:
389
 * @menu: a #GimpUnitMenu
390
 * @digits: the number of digits to display for a pixel size
391
 *
392
 * A GimpUnitMenu can be setup to control the number of digits shown
393
 * by attached spinbuttons. Please refer to the documentation of
394
 * gimp_unit_menu_update() to see how this is done.
395
 *
396
 * This function allows to specify the number of digits shown for a
397
 * size in pixels. Usually this is 0 (only full pixels). If you want
398
 * to allow the user to specify sub-pixel sizes using the attached
399
 * spinbuttons, specify the number of digits after the decimal point
400
 * here. You should do this after attaching your spinbuttons.
401
 **/
402
void
403
gimp_unit_menu_set_pixel_digits (GimpUnitMenu *menu,
404
                                 gint          digits)
405
{
406
  GimpUnit unit;
407
408
  g_return_if_fail (GIMP_IS_UNIT_MENU (menu));
409
410
  menu->pixel_digits = digits;
411
412
  gimp_unit_menu_update (GTK_WIDGET (menu), &unit);
413
}
414
415
/**
416
 * gimp_unit_menu_get_pixel_digits:
417
 * @menu: a #GimpUnitMenu
418
 *
419
 * Retrieve the number of digits for a pixel size as set by
420
 * gimp_unit_set_pixel_digits().
421
 *
422
 * Return value: the configured number of digits for a pixel size
423
 **/
424
gint
425
gimp_unit_menu_get_pixel_digits (GimpUnitMenu *menu)
426
{
427
  g_return_val_if_fail (GIMP_IS_UNIT_MENU (menu), 0);
428
429
  return menu->pixel_digits;
430
}
431
432
433
/*  most of the next two functions is stolen from app/gdisplay.c  */
434
static gint
435
print (gchar       *buf,
436
       gint         len,
437
       gint         start,
438
       const gchar *fmt,
439
       ...)
440
{
441
  va_list args;
442
  gint printed;
443
444
  va_start (args, fmt);
445
446
  printed = g_vsnprintf (buf + start, len - start, fmt, args);
447
  if (printed < 0)
448
    printed = len - start;
449
450
  va_end (args);
451
452
  return printed;
453
}
454
455
static const gchar *
456
gimp_unit_menu_build_string (const gchar *format,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
457
                             GimpUnit     unit)
1 by Daniel Holbach
Import upstream version 2.2.9
458
{
459
  static gchar buffer[64];
460
  gint i = 0;
461
462
  while (i < (sizeof (buffer) - 1) && *format)
463
    {
464
      switch (*format)
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
465
        {
466
        case '%':
467
          format++;
468
          switch (*format)
469
            {
470
            case 0:
471
              g_warning ("%s: unit-menu-format string ended within %%-sequence",
1 by Daniel Holbach
Import upstream version 2.2.9
472
                         G_STRFUNC);
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
473
              break;
474
475
            case '%':
476
              buffer[i++] = '%';
477
              break;
478
479
            case 'f': /* factor (how many units make up an inch) */
480
              i += print (buffer, sizeof (buffer), i, "%f",
481
                          gimp_unit_get_factor (unit));
482
              break;
483
484
            case 'y': /* symbol ("''" for inch) */
485
              i += print (buffer, sizeof (buffer), i, "%s",
486
                          gimp_unit_get_symbol (unit));
487
              break;
488
489
            case 'a': /* abbreviation */
490
              i += print (buffer, sizeof (buffer), i, "%s",
491
                          gimp_unit_get_abbreviation (unit));
492
              break;
493
494
            case 's': /* singular */
495
              i += print (buffer, sizeof (buffer), i, "%s",
496
                          gimp_unit_get_singular (unit));
497
              break;
498
499
            case 'p': /* plural */
500
              i += print (buffer, sizeof (buffer), i, "%s",
501
                          gimp_unit_get_plural (unit));
502
              break;
503
504
            default:
505
              g_warning ("%s: unit-menu-format contains unknown format "
1 by Daniel Holbach
Import upstream version 2.2.9
506
                         "sequence '%%%c'", G_STRFUNC, *format);
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
507
              break;
508
            }
509
          break;
1 by Daniel Holbach
Import upstream version 2.2.9
510
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
511
        default:
512
          buffer[i++] = *format;
513
          break;
514
        }
1 by Daniel Holbach
Import upstream version 2.2.9
515
516
      format++;
517
    }
518
519
  buffer[MIN (i, sizeof (buffer) - 1)] = 0;
520
521
  return buffer;
522
}
523
524
/*  private callback of gimp_unit_menu_create_selection ()  */
525
static void
526
gimp_unit_menu_selection_response (GtkWidget    *widget,
527
                                   gint          response_id,
528
                                   GimpUnitMenu *menu)
529
{
530
  if (response_id == GTK_RESPONSE_OK)
531
    {
532
      GtkTreeSelection *sel;
533
      GtkTreeModel     *model;
534
      GtkTreeIter       iter;
535
536
      sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv));
537
      if (menu->selection && gtk_tree_selection_get_selected (sel, &model,
538
                                                              &iter))
539
        {
540
          GValue   val = { 0, };
541
          GimpUnit unit;
542
543
          gtk_tree_model_get_value (model, &iter, 2, &val);
544
          unit = (GimpUnit) g_value_get_int (&val);
545
          g_value_unset (&val);
546
547
          gimp_unit_menu_set_unit (menu, unit);
548
        }
549
    }
550
551
  gtk_widget_destroy (menu->selection);
552
}
553
554
static void
555
gimp_unit_menu_selection_row_activated_callback (GtkTreeView       *tv,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
556
                                                 GtkTreePath       *path,
557
                                                 GtkTreeViewColumn *column,
558
                                                 GimpUnitMenu      *menu)
1 by Daniel Holbach
Import upstream version 2.2.9
559
{
560
  gtk_dialog_response (GTK_DIALOG (menu->selection), GTK_RESPONSE_OK);
561
}
562
563
/*  private function of gimp_unit_menu_callback ()  */
564
static void
565
gimp_unit_menu_create_selection (GimpUnitMenu *menu)
566
{
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
567
  GtkWidget        *parent = gtk_widget_get_toplevel (GTK_WIDGET (menu));
1 by Daniel Holbach
Import upstream version 2.2.9
568
  GtkWidget        *vbox;
569
  GtkWidget        *scrolled_win;
570
  GtkListStore     *list;
571
  GtkTreeSelection *sel;
572
  GtkTreeIter       iter;
573
  GtkTreePath      *path;
1.1.7 by Steve Kowalik
Import upstream version 2.4.0~rc3
574
  GtkDialogFlags    flags  = GTK_DIALOG_DESTROY_WITH_PARENT;
1 by Daniel Holbach
Import upstream version 2.2.9
575
  GimpUnit          unit;
576
  gint              num_units;
577
1.1.7 by Steve Kowalik
Import upstream version 2.4.0~rc3
578
  if (gtk_window_get_modal (GTK_WINDOW (parent)))
579
    flags |= GTK_DIALOG_MODAL;
580
1 by Daniel Holbach
Import upstream version 2.2.9
581
  menu->selection = gimp_dialog_new (_("Unit Selection"), "gimp-unit-selection",
1.1.7 by Steve Kowalik
Import upstream version 2.4.0~rc3
582
                                     parent, flags,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
583
                                     gimp_standard_help_func,
584
                                     "gimp-unit-dialog",
1 by Daniel Holbach
Import upstream version 2.2.9
585
586
                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
587
                                     GTK_STOCK_OK,     GTK_RESPONSE_OK,
588
589
                                     NULL);
590
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
591
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (menu->selection),
592
                                           GTK_RESPONSE_OK,
593
                                           GTK_RESPONSE_CANCEL,
594
                                           -1);
595
1 by Daniel Holbach
Import upstream version 2.2.9
596
  g_object_add_weak_pointer (G_OBJECT (menu->selection),
597
                             (gpointer) &menu->selection);
598
599
  g_signal_connect (menu->selection, "response",
600
                    G_CALLBACK (gimp_unit_menu_selection_response),
601
                    menu);
602
603
  g_signal_connect_object (menu, "unmap",
604
                           G_CALLBACK (gtk_widget_destroy),
605
                           menu->selection, G_CONNECT_SWAPPED);
606
607
  /*  the main vbox  */
608
  vbox = gtk_vbox_new (FALSE, 0);
609
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
610
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (menu->selection)->vbox), vbox);
611
  gtk_widget_show (vbox);
612
613
  /*  the selection list  */
614
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
615
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
616
                                       GTK_SHADOW_ETCHED_IN);
1 by Daniel Holbach
Import upstream version 2.2.9
617
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
618
                                  GTK_POLICY_NEVER,
619
                                  GTK_POLICY_ALWAYS);
1 by Daniel Holbach
Import upstream version 2.2.9
620
  gtk_container_add (GTK_CONTAINER (vbox), scrolled_win);
621
  gtk_widget_show (scrolled_win);
622
623
  list = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
624
                             G_TYPE_INT);
1 by Daniel Holbach
Import upstream version 2.2.9
625
  menu->tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list));
626
  g_object_unref (list);
627
628
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
629
                                               -1, _("Unit"),
630
                                               gtk_cell_renderer_text_new (),
631
                                               "text", UNIT_COLUMN, NULL);
1 by Daniel Holbach
Import upstream version 2.2.9
632
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
633
                                               -1, _("Factor"),
634
                                               gtk_cell_renderer_text_new (),
635
                                               "text", FACTOR_COLUMN, NULL);
1 by Daniel Holbach
Import upstream version 2.2.9
636
637
  /*  the unit lines  */
638
  num_units = gimp_unit_get_number_of_units ();
639
  for (unit = GIMP_UNIT_END; unit < num_units; unit++)
640
    {
641
      gtk_list_store_append (list, &iter);
642
      gtk_list_store_set (list, &iter,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
643
                          UNIT_COLUMN,
644
                          gimp_unit_menu_build_string (menu->format, unit),
645
                          -1);
1 by Daniel Holbach
Import upstream version 2.2.9
646
      gtk_list_store_set (list, &iter,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
647
                          FACTOR_COLUMN,
648
                          gimp_unit_menu_build_string ("(%f)", unit),
649
                          -1);
1 by Daniel Holbach
Import upstream version 2.2.9
650
      gtk_list_store_set (list, &iter, DATA_COLUMN, unit, -1);
651
    }
652
653
  gtk_widget_set_size_request (menu->tv, -1, 150);
654
655
  gtk_container_add (GTK_CONTAINER (scrolled_win), menu->tv);
656
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
657
  g_signal_connect (menu->tv, "row-activated",
1 by Daniel Holbach
Import upstream version 2.2.9
658
                    G_CALLBACK (gimp_unit_menu_selection_row_activated_callback),
659
                    menu);
660
661
  gtk_widget_show (menu->tv);
662
663
  g_signal_connect (menu->tv, "destroy",
664
                    G_CALLBACK (gtk_widget_destroyed),
665
                    &menu->tv);
666
667
  gtk_widget_show (vbox);
668
  gtk_widget_show (menu->selection);
669
670
  if (menu->unit >= GIMP_UNIT_END)
671
    {
672
      path = gtk_tree_path_new ();
673
      gtk_tree_path_append_index (path, menu->unit - GIMP_UNIT_END);
674
675
      sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv));
676
      gtk_tree_selection_select_path (sel, path);
677
678
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (menu->tv), path, NULL,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
679
                                    FALSE, 0.0, 0.0);
1 by Daniel Holbach
Import upstream version 2.2.9
680
    }
681
}
682
683
static void
684
gimp_unit_menu_callback (GtkWidget *widget,
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
685
                         gpointer   data)
1 by Daniel Holbach
Import upstream version 2.2.9
686
{
687
  GimpUnitMenu *menu = data;
688
  GimpUnit      new_unit;
689
690
  new_unit = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget),
691
                                                  "gimp_unit_menu"));
692
693
  if (menu->unit == new_unit)
694
    return;
695
696
  /*  was "More..." selected?  */
697
  if (new_unit == (GIMP_UNIT_PERCENT + 1))
698
    {
699
      gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
700
                                   (menu->unit == GIMP_UNIT_PIXEL) ? 0 :
701
                                   ((menu->unit == GIMP_UNIT_PERCENT) ?
702
                                    (menu->show_pixels ? 1 : 0) :
703
                                    ((menu->show_pixels ||
704
                                      menu->show_percent ? 2 : 0) +
705
                                     (menu->show_pixels &&
706
                                      menu->show_percent ? 1 : 0) +
707
                                     ((menu->unit < GIMP_UNIT_END) ?
708
                                      menu->unit - 1 : GIMP_UNIT_END))));
1 by Daniel Holbach
Import upstream version 2.2.9
709
      if (! menu->selection)
1.1.4 by Daniel Holbach
Import upstream version 2.3.16
710
        gimp_unit_menu_create_selection (menu);
1 by Daniel Holbach
Import upstream version 2.2.9
711
      return;
712
    }
713
  else if (menu->selection)
714
    {
715
      gtk_widget_destroy (menu->selection);
716
    }
717
718
  gimp_unit_menu_set_unit (menu, new_unit);
719
}