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 3 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, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 
24 #undef GSEAL_ENABLE
25 
26 #include <gegl.h>
27 /* FIXME: #undef GTK_DISABLE_DEPRECATED */
28 #undef GTK_DISABLE_DEPRECATED
29 #include <gtk/gtk.h>
30 
31 #include "libgimpbase/gimpbase.h"
32 
33 #include "gimpwidgetstypes.h"
34 
35 #include "gimpdialog.h"
36 #include "gimphelpui.h"
37 #include "gimpwidgets.h"
38 
39 #undef GIMP_DISABLE_DEPRECATED
40 #include "gimpoldwidgets.h"
41 #include "gimpunitmenu.h"
42 
43 #include "libgimp/libgimp-intl.h"
44 
45 
46 /**
47  * SECTION: gimpunitmenu
48  * @title: GimpUnitMenu
49  * @short_description: Widget for selecting a #GimpUnit.
50  * @see_also: #GimpUnit, #GimpSizeEntry, gimp_coordinates_new()
51  *
52  * This widget provides a #GtkOptionMenu which contains a list of
53  * #GimpUnit's.
54  *
55  * You can specify the string that will be displayed for each unit by
56  * passing a printf-like @format string to gimp_unit_menu_new().
57  *
58  * The constructor also lets you choose if the menu should contain
59  * items for GIMP_UNIT_PIXEL, GIMP_UNIT_PERCENT and a "More..." item
60  * which will pop up a dialog for selecting user-defined units.
61  *
62  * Whenever the user selects a unit from the menu or the dialog, the
63  * "unit_changed" signal will be emitted.
64  **/
65 
66 
67 enum
68 {
69   UNIT_CHANGED,
70   LAST_SIGNAL
71 };
72 
73 enum
74 {
75   UNIT_COLUMN,
76   FACTOR_COLUMN,
77   DATA_COLUMN,
78   NUM_COLUMNS
79 };
80 
81 
82 static void   gimp_unit_menu_finalize (GObject   *object);
83 static void   gimp_unit_menu_callback (GtkWidget *widget,
84                                        gpointer   data);
85 
86 
87 G_DEFINE_TYPE (GimpUnitMenu, gimp_unit_menu, GTK_TYPE_OPTION_MENU)
88 
89 #define parent_class gimp_unit_menu_parent_class
90 
91 static guint gimp_unit_menu_signals[LAST_SIGNAL] = { 0 };
92 
93 
94 static void
gimp_unit_menu_class_init(GimpUnitMenuClass * klass)95 gimp_unit_menu_class_init (GimpUnitMenuClass *klass)
96 {
97   GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 
99   /**
100    * GimpUnitMenu::unit-changed:
101    *
102    * This signal is emitted whenever the user selects a #GimpUnit from
103    * the #GimpUnitMenu.
104    **/
105   gimp_unit_menu_signals[UNIT_CHANGED] =
106     g_signal_new ("unit-changed",
107                   G_TYPE_FROM_CLASS (klass),
108                   G_SIGNAL_RUN_FIRST,
109                   G_STRUCT_OFFSET (GimpUnitMenuClass, unit_changed),
110                   NULL, NULL,
111                   g_cclosure_marshal_VOID__VOID,
112                   G_TYPE_NONE, 0);
113 
114   object_class->finalize = gimp_unit_menu_finalize;
115 
116   klass->unit_changed    = NULL;
117 }
118 
119 static void
gimp_unit_menu_init(GimpUnitMenu * menu)120 gimp_unit_menu_init (GimpUnitMenu *menu)
121 {
122   menu->format       = NULL;
123   menu->unit         = GIMP_UNIT_PIXEL;
124   menu->show_pixels  = FALSE;
125   menu->show_percent = FALSE;
126   menu->selection    = NULL;
127   menu->tv           = NULL;
128 }
129 
130 static void
gimp_unit_menu_finalize(GObject * object)131 gimp_unit_menu_finalize (GObject *object)
132 {
133   GimpUnitMenu *menu = GIMP_UNIT_MENU (object);
134 
135   g_clear_pointer (&menu->format, g_free);
136 
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139 
140 /**
141  * gimp_unit_menu_new:
142  * @format:       A printf-like format string which is used to create the unit
143  *                strings.
144  * @unit:         The initially selected unit.
145  * @show_pixels:  %TRUE if the unit menu should contain an item for
146  *                GIMP_UNIT_PIXEL.
147  * @show_percent: %TRUE in the unit menu should contain an item for
148  *                GIMP_UNIT_PERCENT.
149  * @show_custom:  %TRUE if the unit menu should contain a "More..." item for
150  *                opening the user-defined-unit selection dialog.
151  *
152  * Creates a new #GimpUnitMenu widget.
153  *
154  * For the @format string's possible expansions, see gimp_unit_format_string().
155  *
156  * Returns: A pointer to the new #GimpUnitMenu widget.
157  **/
158 GtkWidget *
gimp_unit_menu_new(const gchar * format,GimpUnit unit,gboolean show_pixels,gboolean show_percent,gboolean show_custom)159 gimp_unit_menu_new (const gchar *format,
160                     GimpUnit     unit,
161                     gboolean     show_pixels,
162                     gboolean     show_percent,
163                     gboolean     show_custom)
164 {
165   GimpUnitMenu *unit_menu;
166   GtkWidget    *menu;
167   GtkWidget    *menuitem;
168   gchar        *string;
169   GimpUnit      u;
170 
171   g_return_val_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
172                          (unit < gimp_unit_get_number_of_units ())) ||
173                         (unit == GIMP_UNIT_PERCENT), NULL);
174 
175   if ((unit >= gimp_unit_get_number_of_built_in_units ()) &&
176       (unit != GIMP_UNIT_PERCENT))
177     show_custom = TRUE;
178 
179   unit_menu = g_object_new (GIMP_TYPE_UNIT_MENU, NULL);
180 
181   unit_menu->format       = g_strdup (format);
182   unit_menu->show_pixels  = show_pixels;
183   unit_menu->show_percent = show_percent;
184 
185   menu = gtk_menu_new ();
186   for (u = show_pixels ? GIMP_UNIT_PIXEL : GIMP_UNIT_INCH;
187        u < gimp_unit_get_number_of_built_in_units ();
188        u++)
189     {
190       /*  special cases "pixels" and "percent"  */
191       if (u == GIMP_UNIT_INCH)
192         {
193           if (show_percent)
194             {
195               string = gimp_unit_format_string (format, GIMP_UNIT_PERCENT);
196               menuitem = gtk_menu_item_new_with_label (string);
197               g_free (string);
198 
199               gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
200               g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
201                                  GINT_TO_POINTER (GIMP_UNIT_PERCENT));
202               gtk_widget_show (menuitem);
203 
204               g_signal_connect (menuitem, "activate",
205                                 G_CALLBACK (gimp_unit_menu_callback),
206                                 unit_menu);
207             }
208 
209           if (show_pixels || show_percent)
210             {
211               menuitem = gtk_menu_item_new ();
212               gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
213               gtk_widget_set_sensitive (menuitem, FALSE);
214               gtk_widget_show (menuitem);
215             }
216         }
217 
218       string = gimp_unit_format_string (format, u);
219       menuitem = gtk_menu_item_new_with_label (string);
220       g_free (string);
221 
222       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
223       g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
224                          GINT_TO_POINTER (u));
225       gtk_widget_show (menuitem);
226 
227       g_signal_connect (menuitem, "activate",
228                         G_CALLBACK (gimp_unit_menu_callback),
229                         unit_menu);
230     }
231 
232   if ((unit >= gimp_unit_get_number_of_built_in_units ()) &&
233       (unit != GIMP_UNIT_PERCENT))
234     {
235       menuitem = gtk_menu_item_new ();
236       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
237       gtk_widget_set_sensitive (menuitem, FALSE);
238       gtk_widget_show (menuitem);
239 
240       string = gimp_unit_format_string (format, unit);
241       menuitem = gtk_menu_item_new_with_label (string);
242       g_free (string);
243 
244       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
245       g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
246                          GINT_TO_POINTER (unit));
247       gtk_widget_show (menuitem);
248 
249       g_signal_connect (menuitem, "activate",
250                         G_CALLBACK (gimp_unit_menu_callback),
251                         unit_menu);
252     }
253 
254   if (show_custom)
255     {
256       menuitem = gtk_menu_item_new ();
257       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
258       gtk_widget_set_sensitive (menuitem, FALSE);
259       gtk_widget_show (menuitem);
260 
261       menuitem = gtk_menu_item_new_with_label (_("More..."));
262       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
263       g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
264                          GINT_TO_POINTER (GIMP_UNIT_PERCENT + 1));
265       gtk_widget_show (menuitem);
266 
267       g_signal_connect (menuitem, "activate",
268                         G_CALLBACK (gimp_unit_menu_callback),
269                         unit_menu);
270     }
271 
272   gtk_option_menu_set_menu (GTK_OPTION_MENU (unit_menu), menu);
273 
274   unit_menu->unit = unit;
275   gtk_option_menu_set_history (GTK_OPTION_MENU (unit_menu),
276                                (unit == GIMP_UNIT_PIXEL) ? 0 :
277                                ((unit == GIMP_UNIT_PERCENT) ?
278                                 (show_pixels ? 1 : 0) :
279                                 (((show_pixels || show_percent) ? 2 : 0) +
280                                  ((show_pixels && show_percent) ? 1 : 0) +
281                                  ((unit < GIMP_UNIT_END) ?
282                                   (unit - 1) : GIMP_UNIT_END))));
283 
284   return GTK_WIDGET (unit_menu);
285 }
286 
287 /**
288  * gimp_unit_menu_set_unit:
289  * @menu:  The unit menu you want to set the unit for.
290  * @unit: The new unit.
291  *
292  * Sets a new #GimpUnit for the specified #GimpUnitMenu.
293  **/
294 void
gimp_unit_menu_set_unit(GimpUnitMenu * menu,GimpUnit unit)295 gimp_unit_menu_set_unit (GimpUnitMenu *menu,
296                          GimpUnit      unit)
297 {
298   GtkWidget *menuitem = NULL;
299   GList     *items;
300   gint       user_unit;
301 
302   g_return_if_fail (GIMP_IS_UNIT_MENU (menu));
303   g_return_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
304                      ((unit > GIMP_UNIT_PIXEL) || menu->show_pixels) &&
305                      (unit < gimp_unit_get_number_of_units ())) ||
306                     ((unit == GIMP_UNIT_PERCENT) && menu->show_percent));
307 
308   if (unit == menu->unit)
309     return;
310 
311   items = GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu)->children;
312   user_unit = (GIMP_UNIT_END +
313                (((menu->show_pixels || menu->show_percent) ? 2 : 0) +
314                 ((menu->show_pixels && menu->show_percent) ? 1 : 0)));
315 
316   if ((unit >= GIMP_UNIT_END) && (unit != GIMP_UNIT_PERCENT))
317     {
318       gchar *string;
319 
320       if ((g_list_length (items) - 3) >= user_unit)
321         {
322           gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
323                                                            user_unit - 1)));
324           gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
325                                                            user_unit - 1)));
326         }
327 
328       menuitem = gtk_menu_item_new ();
329       gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
330                              menuitem);
331       gtk_widget_set_sensitive (menuitem, FALSE);
332       gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu),
333                               menuitem, user_unit - 1);
334       gtk_widget_show (menuitem);
335 
336       string = gimp_unit_format_string (menu->format, unit);
337       menuitem = gtk_menu_item_new_with_label (string);
338       g_free (string);
339 
340       gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
341                              menuitem);
342       g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
343                          GINT_TO_POINTER (unit));
344       gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu),
345                               menuitem, user_unit);
346       gtk_widget_show (menuitem);
347 
348       g_signal_connect (menuitem, "activate",
349                         G_CALLBACK (gimp_unit_menu_callback),
350                         menu);
351     }
352 
353   menu->unit = unit;
354   gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
355                                (unit == GIMP_UNIT_PIXEL) ? 0 :
356                                ((unit == GIMP_UNIT_PERCENT) ?
357                                 (menu->show_pixels ? 1 : 0) :
358                                 (((menu->show_pixels ||
359                                    menu->show_percent) ? 2 : 0) +
360                                  ((menu->show_pixels &&
361                                    menu->show_percent) ? 1 : 0) +
362                                  ((unit < GIMP_UNIT_END) ?
363                                   (unit - 1) : GIMP_UNIT_END))));
364 
365   g_signal_emit (menu, gimp_unit_menu_signals[UNIT_CHANGED], 0);
366 }
367 
368 /**
369  * gimp_unit_menu_get_unit:
370  * @menu: The unit menu you want to know the unit of.
371  *
372  * Returns the #GimpUnit the user has selected from the #GimpUnitMenu.
373  *
374  * Returns: The unit the user has selected.
375  **/
376 GimpUnit
gimp_unit_menu_get_unit(GimpUnitMenu * menu)377 gimp_unit_menu_get_unit (GimpUnitMenu *menu)
378 {
379   g_return_val_if_fail (GIMP_IS_UNIT_MENU (menu), GIMP_UNIT_INCH);
380 
381   return menu->unit;
382 }
383 
384 
385 /**
386  * gimp_unit_menu_set_pixel_digits:
387  * @menu: a #GimpUnitMenu
388  * @digits: the number of digits to display for a pixel size
389  *
390  * A GimpUnitMenu can be setup to control the number of digits shown
391  * by attached spinbuttons. Please refer to the documentation of
392  * gimp_unit_menu_update() to see how this is done.
393  *
394  * This function specifies the number of digits shown for a size in
395  * pixels. Usually this is 0 (only full pixels). If you want to allow
396  * the user to specify sub-pixel sizes using the attached spinbuttons,
397  * specify the number of digits after the decimal point here. You
398  * should do this after attaching your spinbuttons.
399  **/
400 void
gimp_unit_menu_set_pixel_digits(GimpUnitMenu * menu,gint digits)401 gimp_unit_menu_set_pixel_digits (GimpUnitMenu *menu,
402                                  gint          digits)
403 {
404   GimpUnit unit;
405 
406   g_return_if_fail (GIMP_IS_UNIT_MENU (menu));
407 
408   menu->pixel_digits = digits;
409 
410   gimp_unit_menu_update (GTK_WIDGET (menu), &unit);
411 }
412 
413 /**
414  * gimp_unit_menu_get_pixel_digits:
415  * @menu: a #GimpUnitMenu
416  *
417  * Retrieve the number of digits for a pixel size as set by
418  * gimp_unit_menu_set_pixel_digits().
419  *
420  * Return value: the configured number of digits for a pixel size
421  **/
422 gint
gimp_unit_menu_get_pixel_digits(GimpUnitMenu * menu)423 gimp_unit_menu_get_pixel_digits (GimpUnitMenu *menu)
424 {
425   g_return_val_if_fail (GIMP_IS_UNIT_MENU (menu), 0);
426 
427   return menu->pixel_digits;
428 }
429 
430 /*  private callback of gimp_unit_menu_create_selection ()  */
431 static void
gimp_unit_menu_selection_response(GtkWidget * widget,gint response_id,GimpUnitMenu * menu)432 gimp_unit_menu_selection_response (GtkWidget    *widget,
433                                    gint          response_id,
434                                    GimpUnitMenu *menu)
435 {
436   if (response_id == GTK_RESPONSE_OK)
437     {
438       GtkTreeSelection *sel;
439       GtkTreeModel     *model;
440       GtkTreeIter       iter;
441 
442       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv));
443       if (menu->selection && gtk_tree_selection_get_selected (sel, &model,
444                                                               &iter))
445         {
446           GValue   val = G_VALUE_INIT;
447           GimpUnit unit;
448 
449           gtk_tree_model_get_value (model, &iter, 2, &val);
450           unit = (GimpUnit) g_value_get_int (&val);
451           g_value_unset (&val);
452 
453           gimp_unit_menu_set_unit (menu, unit);
454         }
455     }
456 
457   gtk_widget_destroy (menu->selection);
458 }
459 
460 static void
gimp_unit_menu_selection_row_activated_callback(GtkTreeView * tv,GtkTreePath * path,GtkTreeViewColumn * column,GimpUnitMenu * menu)461 gimp_unit_menu_selection_row_activated_callback (GtkTreeView       *tv,
462                                                  GtkTreePath       *path,
463                                                  GtkTreeViewColumn *column,
464                                                  GimpUnitMenu      *menu)
465 {
466   gtk_dialog_response (GTK_DIALOG (menu->selection), GTK_RESPONSE_OK);
467 }
468 
469 /*  private function of gimp_unit_menu_callback ()  */
470 static void
gimp_unit_menu_create_selection(GimpUnitMenu * menu)471 gimp_unit_menu_create_selection (GimpUnitMenu *menu)
472 {
473   GtkWidget        *parent = gtk_widget_get_toplevel (GTK_WIDGET (menu));
474   GtkWidget        *vbox;
475   GtkWidget        *scrolled_win;
476   GtkListStore     *list;
477   GtkTreeSelection *sel;
478   GtkTreeIter       iter;
479   GtkTreePath      *path;
480   GtkDialogFlags    flags  = GTK_DIALOG_DESTROY_WITH_PARENT;
481   GimpUnit          unit;
482   gint              num_units;
483 
484   if (gtk_window_get_modal (GTK_WINDOW (parent)))
485     flags |= GTK_DIALOG_MODAL;
486 
487   menu->selection = gimp_dialog_new (_("Unit Selection"), "gimp-unit-selection",
488                                      parent, flags,
489                                      gimp_standard_help_func,
490                                      "gimp-unit-dialog",
491 
492                                      _("_Cancel"), GTK_RESPONSE_CANCEL,
493                                      _("_OK"),     GTK_RESPONSE_OK,
494 
495                                      NULL);
496 
497   gtk_dialog_set_alternative_button_order (GTK_DIALOG (menu->selection),
498                                            GTK_RESPONSE_OK,
499                                            GTK_RESPONSE_CANCEL,
500                                            -1);
501 
502   g_object_add_weak_pointer (G_OBJECT (menu->selection),
503                              (gpointer) &menu->selection);
504 
505   g_signal_connect (menu->selection, "response",
506                     G_CALLBACK (gimp_unit_menu_selection_response),
507                     menu);
508 
509   g_signal_connect_object (menu, "unmap",
510                            G_CALLBACK (gtk_widget_destroy),
511                            menu->selection, G_CONNECT_SWAPPED);
512 
513   /*  the main vbox  */
514   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
515   gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
516   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (menu->selection))),
517                       vbox, TRUE, TRUE, 0);
518   gtk_widget_show (vbox);
519 
520   /*  the selection list  */
521   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
522   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
523                                        GTK_SHADOW_ETCHED_IN);
524   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
525                                   GTK_POLICY_NEVER,
526                                   GTK_POLICY_ALWAYS);
527   gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
528   gtk_widget_show (scrolled_win);
529 
530   list = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
531                              G_TYPE_INT);
532   menu->tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list));
533   g_object_unref (list);
534 
535   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
536                                                -1, _("Unit"),
537                                                gtk_cell_renderer_text_new (),
538                                                "text", UNIT_COLUMN, NULL);
539   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
540                                                -1, _("Factor"),
541                                                gtk_cell_renderer_text_new (),
542                                                "text", FACTOR_COLUMN, NULL);
543 
544   /*  the unit lines  */
545   num_units = gimp_unit_get_number_of_units ();
546   for (unit = GIMP_UNIT_END; unit < num_units; unit++)
547     {
548       gchar *string;
549 
550       gtk_list_store_append (list, &iter);
551 
552       string = gimp_unit_format_string (menu->format, unit);
553       gtk_list_store_set (list, &iter,
554                           UNIT_COLUMN, string,
555                           -1);
556       g_free (string);
557 
558       string = gimp_unit_format_string ("(%f)", unit);
559       gtk_list_store_set (list, &iter,
560                           FACTOR_COLUMN, string,
561                           -1);
562       g_free (string);
563 
564       gtk_list_store_set (list, &iter, DATA_COLUMN, unit, -1);
565     }
566 
567   gtk_widget_set_size_request (menu->tv, -1, 150);
568 
569   gtk_container_add (GTK_CONTAINER (scrolled_win), menu->tv);
570 
571   g_signal_connect (menu->tv, "row-activated",
572                     G_CALLBACK (gimp_unit_menu_selection_row_activated_callback),
573                     menu);
574 
575   gtk_widget_show (menu->tv);
576 
577   g_signal_connect (menu->tv, "destroy",
578                     G_CALLBACK (gtk_widget_destroyed),
579                     &menu->tv);
580 
581   gtk_widget_show (vbox);
582   gtk_widget_show (menu->selection);
583 
584   if (menu->unit >= GIMP_UNIT_END)
585     {
586       path = gtk_tree_path_new ();
587       gtk_tree_path_append_index (path, menu->unit - GIMP_UNIT_END);
588 
589       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv));
590       gtk_tree_selection_select_path (sel, path);
591 
592       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (menu->tv), path, NULL,
593                                     FALSE, 0.0, 0.0);
594     }
595 }
596 
597 static void
gimp_unit_menu_callback(GtkWidget * widget,gpointer data)598 gimp_unit_menu_callback (GtkWidget *widget,
599                          gpointer   data)
600 {
601   GimpUnitMenu *menu = data;
602   GimpUnit      new_unit;
603 
604   new_unit = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget),
605                                                   "gimp_unit_menu"));
606 
607   if (menu->unit == new_unit)
608     return;
609 
610   /*  was "More..." selected?  */
611   if (new_unit == (GIMP_UNIT_PERCENT + 1))
612     {
613       gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
614                                    (menu->unit == GIMP_UNIT_PIXEL) ? 0 :
615                                    ((menu->unit == GIMP_UNIT_PERCENT) ?
616                                     (menu->show_pixels ? 1 : 0) :
617                                     ((menu->show_pixels ||
618                                       menu->show_percent ? 2 : 0) +
619                                      (menu->show_pixels &&
620                                       menu->show_percent ? 1 : 0) +
621                                      ((menu->unit < GIMP_UNIT_END) ?
622                                       menu->unit - 1 : GIMP_UNIT_END))));
623       if (! menu->selection)
624         gimp_unit_menu_create_selection (menu);
625       return;
626     }
627   else if (menu->selection)
628     {
629       gtk_widget_destroy (menu->selection);
630     }
631 
632   gimp_unit_menu_set_unit (menu, new_unit);
633 }
634