gimpunitmenu.c 20.7 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * 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
Marc Lehmann's avatar
Marc Lehmann committed
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 13
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 16
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20
 */
21

22 23
#include "config.h"

24 25
#undef GSEAL_ENABLE

26
#include <gegl.h>
27 28
/* FIXME: #undef GTK_DISABLE_DEPRECATED */
#undef GTK_DISABLE_DEPRECATED
29 30
#include <gtk/gtk.h>

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 42
#include "gimpunitmenu.h"

43
#include "libgimp/libgimp-intl.h"
44

45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/**
 * SECTION: gimpunitmenu
 * @title: GimpUnitMenu
 * @short_description: Widget for selecting a #GimpUnit.
 * @see_also: #GimpUnit, #GimpSizeEntry, gimp_coordinates_new()
 *
 * This widget provides a #GtkOptionMenu which contains a list of
 * #GimpUnit's.
 *
 * You can specify the string that will be displayed for each unit by
 * passing a printf-like @format string to gimp_unit_menu_new().
 *
 * The constructor also lets you choose if the menu should contain
 * items for GIMP_UNIT_PIXEL, GIMP_UNIT_PERCENT and a "More..." item
 * which will pop up a dialog for selecting user-defined units.
 *
 * Whenever the user selects a unit from the menu or the dialog, the
 * "unit_changed" signal will be emitted.
 **/


67 68
enum
{
69
  UNIT_CHANGED,
70 71 72
  LAST_SIGNAL
};

73 74 75 76 77 78 79 80
enum
{
  UNIT_COLUMN,
  FACTOR_COLUMN,
  DATA_COLUMN,
  NUM_COLUMNS
};

81

82 83 84
static void   gimp_unit_menu_finalize (GObject   *object);
static void   gimp_unit_menu_callback (GtkWidget *widget,
                                       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
Michael Natterer's avatar
Michael Natterer committed
90

91
static guint gimp_unit_menu_signals[LAST_SIGNAL] = { 0 };
92

93 94

static void
Michael Natterer's avatar
Michael Natterer committed
95
gimp_unit_menu_class_init (GimpUnitMenuClass *klass)
96
{
97
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
98

99 100 101 102 103 104
  /**
   * GimpUnitMenu::unit-changed:
   *
   * This signal is emitted whenever the user selects a #GimpUnit from
   * the #GimpUnitMenu.
   **/
105
  gimp_unit_menu_signals[UNIT_CHANGED] =
106
    g_signal_new ("unit-changed",
Sven Neumann's avatar
Sven Neumann committed
107 108 109 110 111 112
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpUnitMenuClass, unit_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
113

114
  object_class->finalize = gimp_unit_menu_finalize;
115

116
  klass->unit_changed    = NULL;
117
}
118 119

static void
120
gimp_unit_menu_init (GimpUnitMenu *menu)
121
{
122 123 124 125 126 127
  menu->format       = NULL;
  menu->unit         = GIMP_UNIT_PIXEL;
  menu->show_pixels  = FALSE;
  menu->show_percent = FALSE;
  menu->selection    = NULL;
  menu->tv           = NULL;
128 129
}

130
static void
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 143 144
 * @format:       A printf-like format string which is used to create the unit
 *                strings.
 * @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 153
 *
 * Creates a new #GimpUnitMenu widget.
 *
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 *
159
gimp_unit_menu_new (const gchar *format,
Sven Neumann's avatar
Sven Neumann committed
160 161 162 163
                    GimpUnit     unit,
                    gboolean     show_pixels,
                    gboolean     show_percent,
                    gboolean     show_custom)
164
{
165
  GimpUnitMenu *unit_menu;
166 167
  GtkWidget    *menu;
  GtkWidget    *menuitem;
168
  gchar        *string;
169
  GimpUnit      u;
170

171
  g_return_val_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
Sven Neumann's avatar
Sven Neumann committed
172 173
                         (unit < gimp_unit_get_number_of_units ())) ||
                        (unit == GIMP_UNIT_PERCENT), NULL);
174

175
  if ((unit >= gimp_unit_get_number_of_built_in_units ()) &&
176
      (unit != GIMP_UNIT_PERCENT))
Michael Natterer's avatar
Michael Natterer committed
177
    show_custom = TRUE;
178

179
  unit_menu = g_object_new (GIMP_TYPE_UNIT_MENU, NULL);
180

181 182 183
  unit_menu->format       = g_strdup (format);
  unit_menu->show_pixels  = show_pixels;
  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 ();
Michael Natterer's avatar
Michael Natterer committed
188
       u++)
189
    {
190
      /*  special cases "pixels" and "percent"  */
191
      if (u == GIMP_UNIT_INCH)
Sven Neumann's avatar
Sven Neumann committed
192 193 194
        {
          if (show_percent)
            {
195 196 197 198
              string = gimp_unit_format_string (format, GIMP_UNIT_PERCENT);
              menuitem = gtk_menu_item_new_with_label (string);
              g_free (string);

Sven Neumann's avatar
Sven Neumann committed
199 200
              gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
              g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu",
201
                                 GINT_TO_POINTER (GIMP_UNIT_PERCENT));
Sven Neumann's avatar
Sven Neumann committed
202
              gtk_widget_show (menuitem);
203

Sven Neumann's avatar
Sven Neumann committed
204
              g_signal_connect (menuitem, "activate",
205
                                G_CALLBACK (gimp_unit_menu_callback),
206
                                unit_menu);
Sven Neumann's avatar
Sven Neumann committed
207 208 209 210 211 212 213 214 215 216
            }

          if (show_pixels || show_percent)
            {
              menuitem = gtk_menu_item_new ();
              gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
              gtk_widget_set_sensitive (menuitem, FALSE);
              gtk_widget_show (menuitem);
            }
        }
217

218 219 220 221
      string = gimp_unit_format_string (format, u);
      menuitem = gtk_menu_item_new_with_label (string);
      g_free (string);

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));
Michael Natterer's avatar
Michael Natterer committed
225
      gtk_widget_show (menuitem);
226

227
      g_signal_connect (menuitem, "activate",
228
                        G_CALLBACK (gimp_unit_menu_callback),
229
                        unit_menu);
230
    }
Michael Natterer's avatar
Michael Natterer committed
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 241 242 243
      string = gimp_unit_format_string (format, unit);
      menuitem = gtk_menu_item_new_with_label (string);
      g_free (string);

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

Michael Natterer's avatar
Michael Natterer committed
254
  if (show_custom)
Michael Natterer's avatar
Michael Natterer committed
255 256
    {
      menuitem = gtk_menu_item_new ();
257
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
258
      gtk_widget_set_sensitive (menuitem, FALSE);
Michael Natterer's avatar
Michael Natterer committed
259
      gtk_widget_show (menuitem);
260

Sven Neumann's avatar
Sven Neumann committed
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);
Michael Natterer's avatar
Michael Natterer committed
270
    }
271

272
  gtk_option_menu_set_menu (GTK_OPTION_MENU (unit_menu), menu);
273

274 275
  unit_menu->unit = unit;
  gtk_option_menu_set_history (GTK_OPTION_MENU (unit_menu),
Sven Neumann's avatar
Sven Neumann committed
276 277 278 279 280 281 282
                               (unit == GIMP_UNIT_PIXEL) ? 0 :
                               ((unit == GIMP_UNIT_PERCENT) ?
                                (show_pixels ? 1 : 0) :
                                (((show_pixels || show_percent) ? 2 : 0) +
                                 ((show_pixels && show_percent) ? 1 : 0) +
                                 ((unit < GIMP_UNIT_END) ?
                                  (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 291 292
 * @unit: The new unit.
 *
 * Sets a new #GimpUnit for the specified #GimpUnitMenu.
293
 **/
294
void
295
gimp_unit_menu_set_unit (GimpUnitMenu *menu,
Sven Neumann's avatar
Sven Neumann committed
296
                         GimpUnit      unit)
297
{
298 299 300 301
  GtkWidget *menuitem = NULL;
  GList     *items;
  gint       user_unit;

302
  g_return_if_fail (GIMP_IS_UNIT_MENU (menu));
303
  g_return_if_fail (((unit >= GIMP_UNIT_PIXEL) &&
Sven Neumann's avatar
Sven Neumann committed
304 305 306
                     ((unit > GIMP_UNIT_PIXEL) || menu->show_pixels) &&
                     (unit < gimp_unit_get_number_of_units ())) ||
                    ((unit == GIMP_UNIT_PERCENT) && menu->show_percent));
307

308
  if (unit == menu->unit)
309 310
    return;

311
  items = GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu)->children;
312
  user_unit = (GIMP_UNIT_END +
Sven Neumann's avatar
Sven Neumann committed
313 314
               (((menu->show_pixels || menu->show_percent) ? 2 : 0) +
                ((menu->show_pixels && menu->show_percent) ? 1 : 0)));
315

316
  if ((unit >= GIMP_UNIT_END) && (unit != GIMP_UNIT_PERCENT))
317
    {
318 319
      gchar *string;

320
      if ((g_list_length (items) - 3) >= user_unit)
Sven Neumann's avatar
Sven Neumann committed
321 322 323 324 325 326
        {
          gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
                                                           user_unit - 1)));
          gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items,
                                                           user_unit - 1)));
        }
327 328

      menuitem = gtk_menu_item_new ();
329
      gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
Sven Neumann's avatar
Sven Neumann committed
330
                             menuitem);
331
      gtk_widget_set_sensitive (menuitem, FALSE);
332
      gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu),
Sven Neumann's avatar
Sven Neumann committed
333
                              menuitem, user_unit - 1);
334
      gtk_widget_show (menuitem);
335

336 337 338 339
      string = gimp_unit_format_string (menu->format, unit);
      menuitem = gtk_menu_item_new_with_label (string);
      g_free (string);

340
      gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu),
Sven Neumann's avatar
Sven Neumann committed
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),
Sven Neumann's avatar
Sven Neumann committed
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 354
  menu->unit = unit;
  gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
Sven Neumann's avatar
Sven Neumann committed
355 356 357 358 359 360 361 362 363
                               (unit == GIMP_UNIT_PIXEL) ? 0 :
                               ((unit == GIMP_UNIT_PERCENT) ?
                                (menu->show_pixels ? 1 : 0) :
                                (((menu->show_pixels ||
                                   menu->show_percent) ? 2 : 0) +
                                 ((menu->show_pixels &&
                                   menu->show_percent) ? 1 : 0) +
                                 ((unit < GIMP_UNIT_END) ?
                                  (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 373 374
 *
 * Returns the #GimpUnit the user has selected from the #GimpUnitMenu.
 *
 * Returns: The unit the user has selected.
375
 **/
376
GimpUnit
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 387 388 389 390 391 392 393

/**
 * gimp_unit_menu_set_pixel_digits:
 * @menu: a #GimpUnitMenu
 * @digits: the number of digits to display for a pixel size
 *
 * A GimpUnitMenu can be setup to control the number of digits shown
 * by attached spinbuttons. Please refer to the documentation of
 * gimp_unit_menu_update() to see how this is done.
 *
394 395 396 397 398
 * This function specifies the number of digits shown for a size in
 * pixels. Usually this is 0 (only full pixels). If you want to allow
 * the user to specify sub-pixel sizes using the attached spinbuttons,
 * specify the number of digits after the decimal point here. You
 * should do this after attaching your spinbuttons.
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
 **/
void
gimp_unit_menu_set_pixel_digits (GimpUnitMenu *menu,
                                 gint          digits)
{
  GimpUnit unit;

  g_return_if_fail (GIMP_IS_UNIT_MENU (menu));

  menu->pixel_digits = digits;

  gimp_unit_menu_update (GTK_WIDGET (menu), &unit);
}

/**
 * gimp_unit_menu_get_pixel_digits:
 * @menu: a #GimpUnitMenu
 *
 * Retrieve the number of digits for a pixel size as set by
418
 * gimp_unit_menu_set_pixel_digits().
419 420 421 422 423 424 425 426 427 428 429
 *
 * Return value: the configured number of digits for a pixel size
 **/
gint
gimp_unit_menu_get_pixel_digits (GimpUnitMenu *menu)
{
  g_return_val_if_fail (GIMP_IS_UNIT_MENU (menu), 0);

  return menu->pixel_digits;
}

430
/*  private callback of gimp_unit_menu_create_selection ()  */
431
static void
432 433 434
gimp_unit_menu_selection_response (GtkWidget    *widget,
                                   gint          response_id,
                                   GimpUnitMenu *menu)
435
{
436
  if (response_id == GTK_RESPONSE_OK)
437
    {
438 439 440 441 442 443 444 445
      GtkTreeSelection *sel;
      GtkTreeModel     *model;
      GtkTreeIter       iter;

      sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv));
      if (menu->selection && gtk_tree_selection_get_selected (sel, &model,
                                                              &iter))
        {
446
          GValue   val = G_VALUE_INIT;
447 448 449 450 451 452 453 454
          GimpUnit unit;

          gtk_tree_model_get_value (model, &iter, 2, &val);
          unit = (GimpUnit) g_value_get_int (&val);
          g_value_unset (&val);

          gimp_unit_menu_set_unit (menu, unit);
        }
455
    }
456 457

  gtk_widget_destroy (menu->selection);
458 459
}

460
static void
461
gimp_unit_menu_selection_row_activated_callback (GtkTreeView       *tv,
Sven Neumann's avatar
Sven Neumann committed
462 463 464
                                                 GtkTreePath       *path,
                                                 GtkTreeViewColumn *column,
                                                 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
471
gimp_unit_menu_create_selection (GimpUnitMenu *menu)
472
{
473
  GtkWidget        *parent = gtk_widget_get_toplevel (GTK_WIDGET (menu));
474 475 476 477 478 479
  GtkWidget        *vbox;
  GtkWidget        *scrolled_win;
  GtkListStore     *list;
  GtkTreeSelection *sel;
  GtkTreeIter       iter;
  GtkTreePath      *path;
480
  GtkDialogFlags    flags  = GTK_DIALOG_DESTROY_WITH_PARENT;
481 482
  GimpUnit          unit;
  gint              num_units;
483

484 485 486
  if (gtk_window_get_modal (GTK_WINDOW (parent)))
    flags |= GTK_DIALOG_MODAL;

487
  menu->selection = gimp_dialog_new (_("Unit Selection"), "gimp-unit-selection",
488
                                     parent, flags,
Sven Neumann's avatar
Sven Neumann committed
489 490
                                     gimp_standard_help_func,
                                     "gimp-unit-dialog",
491

492 493
                                     _("_Cancel"), GTK_RESPONSE_CANCEL,
                                     _("_OK"),     GTK_RESPONSE_OK,
494

495
                                     NULL);
496

497 498 499 500 501
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (menu->selection),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

502 503
  g_object_add_weak_pointer (G_OBJECT (menu->selection),
                             (gpointer) &menu->selection);
504

505 506 507 508
  g_signal_connect (menu->selection, "response",
                    G_CALLBACK (gimp_unit_menu_selection_response),
                    menu);

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 517
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (menu->selection))),
                      vbox, TRUE, TRUE, 0);
518 519 520
  gtk_widget_show (vbox);

  /*  the selection list  */
521
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
522
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
Sven Neumann's avatar
Sven Neumann committed
523
                                       GTK_SHADOW_ETCHED_IN);
524
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
Sven Neumann's avatar
Sven Neumann committed
525 526
                                  GTK_POLICY_NEVER,
                                  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,
Sven Neumann's avatar
Sven Neumann committed
531
                             G_TYPE_INT);
532
  menu->tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list));
533 534
  g_object_unref (list);

535
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
Sven Neumann's avatar
Sven Neumann committed
536 537 538
                                               -1, _("Unit"),
                                               gtk_cell_renderer_text_new (),
                                               "text", UNIT_COLUMN, NULL);
539
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv),
Sven Neumann's avatar
Sven Neumann committed
540 541 542
                                               -1, _("Factor"),
                                               gtk_cell_renderer_text_new (),
                                               "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 549
      gchar *string;

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,
Sven Neumann's avatar
Sven Neumann committed
555
                          -1);
556 557 558
      g_free (string);

      string = gimp_unit_format_string ("(%f)", unit);
559
      gtk_list_store_set (list, &iter,
560
                          FACTOR_COLUMN, string,
Sven Neumann's avatar
Sven Neumann committed
561
                          -1);
562 563
      g_free (string);

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 591
      gtk_tree_selection_select_path (sel, path);

592
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (menu->tv), path, NULL,
Sven Neumann's avatar
Sven Neumann committed
593
                                    FALSE, 0.0, 0.0);
594 595 596
    }
}

597 598
static void
gimp_unit_menu_callback (GtkWidget *widget,
Sven Neumann's avatar
Sven Neumann committed
599
                         gpointer   data)
600
{
601
  GimpUnitMenu *menu = data;
602
  GimpUnit      new_unit;
603

604 605
  new_unit = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget),
                                                  "gimp_unit_menu"));
606

607
  if (menu->unit == new_unit)
608 609
    return;

Michael Natterer's avatar
Michael Natterer committed
610
  /*  was "More..." selected?  */
611
  if (new_unit == (GIMP_UNIT_PERCENT + 1))
612
    {
613
      gtk_option_menu_set_history (GTK_OPTION_MENU (menu),
Sven Neumann's avatar
Sven Neumann committed
614 615 616 617 618 619 620 621 622
                                   (menu->unit == GIMP_UNIT_PIXEL) ? 0 :
                                   ((menu->unit == GIMP_UNIT_PERCENT) ?
                                    (menu->show_pixels ? 1 : 0) :
                                    ((menu->show_pixels ||
                                      menu->show_percent ? 2 : 0) +
                                     (menu->show_pixels &&
                                      menu->show_percent ? 1 : 0) +
                                     ((menu->unit < GIMP_UNIT_END) ?
                                      menu->unit - 1 : GIMP_UNIT_END))));
623
      if (! menu->selection)
Sven Neumann's avatar
Sven Neumann committed
624
        gimp_unit_menu_create_selection (menu);
625 626
      return;
    }
627
  else if (menu->selection)
628
    {
629
      gtk_widget_destroy (menu->selection);
630 631
    }

632
  gimp_unit_menu_set_unit (menu, new_unit);
633
}