~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/common/unit-editor.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This is a plug-in for GIMP.
 
5
 *
 
6
 * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <string.h>
 
26
 
 
27
#include <libgimp/gimp.h>
 
28
#include <libgimp/gimpui.h>
 
29
 
 
30
#include "libgimp/stdplugins-intl.h"
 
31
 
 
32
 
 
33
#define PLUG_IN_PROC     "plug-in-unit-editor"
 
34
#define PLUG_IN_BINARY   "unit-editor"
 
35
#define RESPONSE_REFRESH 1
 
36
 
 
37
enum
 
38
{
 
39
  SAVE,
 
40
  IDENTIFIER,
 
41
  FACTOR,
 
42
  DIGITS,
 
43
  SYMBOL,
 
44
  ABBREVIATION,
 
45
  SINGULAR,
 
46
  PLURAL,
 
47
  UNIT,
 
48
  USER_UNIT,
 
49
  BG_COLOR,
 
50
  NUM_COLUMNS
 
51
};
 
52
 
 
53
typedef struct
 
54
{
 
55
  const gchar *title;
 
56
  const gchar *help;
 
57
 
 
58
} UnitColumn;
 
59
 
 
60
 
 
61
static void     query                  (void);
 
62
static void     run                    (const gchar           *name,
 
63
                                        gint                   n_params,
 
64
                                        const GimpParam       *param,
 
65
                                        gint                  *n_return_vals,
 
66
                                        GimpParam            **return_vals);
 
67
 
 
68
static GimpUnit new_unit_dialog        (GtkWidget             *main_dialog,
 
69
                                        GimpUnit               template);
 
70
static void     unit_editor_dialog     (void);
 
71
static void     unit_editor_response   (GtkWidget             *widget,
 
72
                                        gint                   response_id,
 
73
                                        gpointer               data);
 
74
static void     new_callback           (GtkAction             *action,
 
75
                                        GtkTreeView           *tv);
 
76
static void     duplicate_callback     (GtkAction             *action,
 
77
                                        GtkTreeView           *tv);
 
78
static void     saved_toggled_callback (GtkCellRendererToggle *celltoggle,
 
79
                                        gchar                 *path_string,
 
80
                                        GtkListStore          *list_store);
 
81
static void     unit_list_init         (GtkTreeView           *tv);
 
82
 
 
83
 
 
84
const GimpPlugInInfo PLUG_IN_INFO =
 
85
{
 
86
  NULL,  /* init_proc  */
 
87
  NULL,  /* quit_proc  */
 
88
  query, /* query_proc */
 
89
  run,   /* run_proc   */
 
90
};
 
91
 
 
92
static const UnitColumn columns[] =
 
93
{
 
94
  { N_("Saved"),        N_("A unit definition will only be saved before "
 
95
                           "GIMP exits if this column is checked.")         },
 
96
  { N_("ID"),           N_("This string will be used to identify a "
 
97
                           "unit in GIMP's configuration files.")           },
 
98
  { N_("Factor"),       N_("How many units make up an inch.")               },
 
99
  { N_("Digits"),       N_("This field is a hint for numerical input "
 
100
                           "fields. It specifies how many decimal digits "
 
101
                           "the input field should provide to get "
 
102
                           "approximately the same accuracy as an "
 
103
                           "\"inch\" input field with two decimal digits.") },
 
104
  { N_("Symbol"),       N_("The unit's symbol if it has one (e.g. \"'\" "
 
105
                           "for inches). The unit's abbreviation is used "
 
106
                           "if doesn't have a symbol.")                     },
 
107
  { N_("Abbreviation"), N_("The unit's abbreviation (e.g. \"cm\" for "
 
108
                           "centimeters).")                                 },
 
109
  { N_("Singular"),     N_("The unit's singular form.")                     },
 
110
  { N_("Plural"),       N_("The unit's plural form.")                       }
 
111
};
 
112
 
 
113
static GtkActionEntry actions[] =
 
114
{
 
115
  { "unit-editor-toolbar", NULL,
 
116
    "Unit Editor Toolbar", NULL, NULL, NULL
 
117
  },
 
118
 
 
119
  { "unit-editor-new", GTK_STOCK_NEW,
 
120
    NULL, "<control>N",
 
121
    N_("Create a new unit from scratch"),
 
122
    G_CALLBACK (new_callback)
 
123
  },
 
124
 
 
125
  { "unit-editor-duplicate", GIMP_STOCK_DUPLICATE,
 
126
    NULL,  "<control>D",
 
127
    N_("Create a new unit using the currently selected unit as template"),
 
128
    G_CALLBACK (duplicate_callback)
 
129
  }
 
130
};
 
131
 
 
132
 
 
133
MAIN ()
 
134
 
 
135
 
 
136
static void
 
137
query (void)
 
138
{
 
139
  static const GimpParamDef args[] =
 
140
  {
 
141
    { GIMP_PDB_INT32, "run-mode", "Interactive" }
 
142
  };
 
143
 
 
144
  gimp_install_procedure (PLUG_IN_PROC,
 
145
                          N_("Create or alter units used in GIMP"),
 
146
                          "The GIMP unit editor",
 
147
                          "Michael Natterer <mitch@gimp.org>",
 
148
                          "Michael Natterer <mitch@gimp.org>",
 
149
                          "2000",
 
150
                          N_("U_nits"),
 
151
                          "",
 
152
                          GIMP_PLUGIN,
 
153
                          G_N_ELEMENTS (args), 0,
 
154
                          args, NULL);
 
155
 
 
156
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Edit/Preferences");
 
157
  gimp_plugin_icon_register (PLUG_IN_PROC, GIMP_ICON_TYPE_STOCK_ID,
 
158
                             (const guint8 *) GIMP_STOCK_TOOL_MEASURE);
 
159
}
 
160
 
 
161
static void
 
162
run (const gchar      *name,
 
163
     gint              nparams,
 
164
     const GimpParam  *param,
 
165
     gint             *nreturn_vals,
 
166
     GimpParam       **return_vals)
 
167
{
 
168
  static GimpParam values[2];
 
169
  GimpRunMode      run_mode;
 
170
 
 
171
  run_mode = param[0].data.d_int32;
 
172
 
 
173
  INIT_I18N ();
 
174
 
 
175
  *nreturn_vals = 1;
 
176
  *return_vals  = values;
 
177
 
 
178
  values[0].type          = GIMP_PDB_STATUS;
 
179
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
 
180
 
 
181
  if (strcmp (name, PLUG_IN_PROC) == 0)
 
182
    {
 
183
      values[0].data.d_status = GIMP_PDB_SUCCESS;
 
184
 
 
185
      unit_editor_dialog ();
 
186
    }
 
187
}
 
188
 
 
189
static GimpUnit
 
190
new_unit_dialog (GtkWidget *main_dialog,
 
191
                 GimpUnit   template)
 
192
{
 
193
  GtkWidget *dialog;
 
194
  GtkWidget *table;
 
195
  GtkWidget *entry;
 
196
  GtkWidget *spinbutton;
 
197
 
 
198
  GtkWidget *identifier_entry;
 
199
  GtkObject *factor_adj;
 
200
  GtkObject *digits_adj;
 
201
  GtkWidget *symbol_entry;
 
202
  GtkWidget *abbreviation_entry;
 
203
  GtkWidget *singular_entry;
 
204
  GtkWidget *plural_entry;
 
205
 
 
206
  GimpUnit   unit = GIMP_UNIT_PIXEL;
 
207
 
 
208
  dialog = gimp_dialog_new (_("Add a New Unit"), PLUG_IN_BINARY,
 
209
                            main_dialog, GTK_DIALOG_MODAL,
 
210
                            gimp_standard_help_func, PLUG_IN_PROC,
 
211
 
 
212
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
213
                            GTK_STOCK_ADD,    GTK_RESPONSE_OK,
 
214
 
 
215
                            NULL);
 
216
 
 
217
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
218
                                           GTK_RESPONSE_OK,
 
219
                                           GTK_RESPONSE_CANCEL,
 
220
                                           -1);
 
221
 
 
222
  table = gtk_table_new (7, 2, FALSE);
 
223
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
224
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
 
225
  gtk_container_set_border_width (GTK_CONTAINER (table), 12);
 
226
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), table,
 
227
                      FALSE, FALSE, 0);
 
228
  gtk_widget_show (table);
 
229
 
 
230
  entry = identifier_entry = gtk_entry_new ();
 
231
  if (template != GIMP_UNIT_PIXEL)
 
232
    {
 
233
      gtk_entry_set_text (GTK_ENTRY (entry),
 
234
                          gimp_unit_get_identifier (template));
 
235
    }
 
236
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
 
237
                             _("_ID:"), 0.0, 0.5,
 
238
                             entry, 1, FALSE);
 
239
 
 
240
  gimp_help_set_help_data (entry, gettext (columns[IDENTIFIER].help), NULL);
 
241
 
 
242
  spinbutton = gimp_spin_button_new (&factor_adj,
 
243
                                     (template != GIMP_UNIT_PIXEL) ?
 
244
                                     gimp_unit_get_factor (template) : 1.0,
 
245
                                     GIMP_MIN_RESOLUTION, GIMP_MAX_RESOLUTION,
 
246
                                     0.01, 0.1, 0.0, 0.01, 5);
 
247
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
 
248
                             _("_Factor:"), 0.0, 0.5,
 
249
                             spinbutton, 1, TRUE);
 
250
 
 
251
  gimp_help_set_help_data (spinbutton, gettext (columns[FACTOR].help), NULL);
 
252
 
 
253
  spinbutton = gimp_spin_button_new (&digits_adj,
 
254
                                     (template != GIMP_UNIT_PIXEL) ?
 
255
                                     gimp_unit_get_digits (template) : 2.0,
 
256
                                     0, 5, 1, 1, 0, 1, 0);
 
257
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
 
258
                             _("_Digits:"), 0.0, 0.5,
 
259
                             spinbutton, 1, TRUE);
 
260
 
 
261
  gimp_help_set_help_data (spinbutton, gettext (columns[DIGITS].help), NULL);
 
262
 
 
263
  entry = symbol_entry = gtk_entry_new ();
 
264
  if (template != GIMP_UNIT_PIXEL)
 
265
    {
 
266
      gtk_entry_set_text (GTK_ENTRY (entry),
 
267
                          gimp_unit_get_symbol (template));
 
268
    }
 
269
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
 
270
                             _("_Symbol:"), 0.0, 0.5,
 
271
                             entry, 1, FALSE);
 
272
 
 
273
  gimp_help_set_help_data (entry, gettext (columns[SYMBOL].help), NULL);
 
274
 
 
275
  entry = abbreviation_entry = gtk_entry_new ();
 
276
  if (template != GIMP_UNIT_PIXEL)
 
277
    {
 
278
      gtk_entry_set_text (GTK_ENTRY (entry),
 
279
                          gimp_unit_get_abbreviation (template));
 
280
    }
 
281
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 4,
 
282
                             _("_Abbreviation:"), 0.0, 0.5,
 
283
                             entry, 1, FALSE);
 
284
 
 
285
  gimp_help_set_help_data (entry, gettext (columns[ABBREVIATION].help), NULL);
 
286
 
 
287
  entry = singular_entry = gtk_entry_new ();
 
288
  if (template != GIMP_UNIT_PIXEL)
 
289
    {
 
290
      gtk_entry_set_text (GTK_ENTRY (entry),
 
291
                          gimp_unit_get_singular (template));
 
292
    }
 
293
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 5,
 
294
                             _("Si_ngular:"), 0.0, 0.5,
 
295
                             entry, 1, FALSE);
 
296
 
 
297
  gimp_help_set_help_data (entry, gettext (columns[SINGULAR].help), NULL);
 
298
 
 
299
  entry = plural_entry = gtk_entry_new ();
 
300
  if (template != GIMP_UNIT_PIXEL)
 
301
    {
 
302
      gtk_entry_set_text (GTK_ENTRY (entry),
 
303
                          gimp_unit_get_plural (template));
 
304
    }
 
305
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 6,
 
306
                             _("_Plural:"), 0.0, 0.5,
 
307
                             entry, 1, FALSE);
 
308
 
 
309
  gimp_help_set_help_data (entry, gettext (columns[PLURAL].help), NULL);
 
310
 
 
311
  gtk_widget_show (dialog);
 
312
 
 
313
  while (TRUE)
 
314
    {
 
315
      gchar   *identifier;
 
316
      gdouble  factor;
 
317
      gint     digits;
 
318
      gchar   *symbol;
 
319
      gchar   *abbreviation;
 
320
      gchar   *singular;
 
321
      gchar   *plural;
 
322
 
 
323
      if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK)
 
324
        break;
 
325
 
 
326
      identifier   = g_strdup (gtk_entry_get_text (GTK_ENTRY (identifier_entry)));
 
327
      factor       = gtk_adjustment_get_value (GTK_ADJUSTMENT (factor_adj));
 
328
      digits       = gtk_adjustment_get_value (GTK_ADJUSTMENT (digits_adj));
 
329
      symbol       = g_strdup (gtk_entry_get_text (GTK_ENTRY (symbol_entry)));
 
330
      abbreviation = g_strdup (gtk_entry_get_text (GTK_ENTRY (abbreviation_entry)));
 
331
      singular     = g_strdup (gtk_entry_get_text (GTK_ENTRY (singular_entry)));
 
332
      plural       = g_strdup (gtk_entry_get_text (GTK_ENTRY (plural_entry)));
 
333
 
 
334
      identifier   = g_strstrip (identifier);
 
335
      symbol       = g_strstrip (symbol);
 
336
      abbreviation = g_strstrip (abbreviation);
 
337
      singular     = g_strstrip (singular);
 
338
      plural       = g_strstrip (plural);
 
339
 
 
340
      if (!strlen (identifier) |
 
341
          !strlen (symbol) |
 
342
          !strlen (abbreviation) |
 
343
          !strlen (singular) |
 
344
          !strlen (plural))
 
345
        {
 
346
          GtkWidget *msg = gtk_message_dialog_new (GTK_WINDOW (dialog), 0,
 
347
                                                   GTK_MESSAGE_ERROR,
 
348
                                                   GTK_BUTTONS_OK,
 
349
                                                   _("Incomplete input"));
 
350
 
 
351
          gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (msg),
 
352
                                                    _("Please fill in all text fields."));
 
353
          gtk_dialog_run (GTK_DIALOG (msg));
 
354
          gtk_widget_destroy (msg);
 
355
 
 
356
          continue;
 
357
        }
 
358
 
 
359
      unit = gimp_unit_new (identifier,
 
360
                            factor, digits,
 
361
                            symbol, abbreviation, singular, plural);
 
362
 
 
363
      g_free (identifier);
 
364
      g_free (symbol);
 
365
      g_free (abbreviation);
 
366
      g_free (singular);
 
367
      g_free (plural);
 
368
 
 
369
      break;
 
370
    }
 
371
 
 
372
  gtk_widget_destroy (dialog);
 
373
 
 
374
  return unit;
 
375
}
 
376
 
 
377
static void
 
378
unit_editor_dialog (void)
 
379
{
 
380
  GtkWidget         *dialog;
 
381
  GtkWidget         *scrolled_win;
 
382
  GtkUIManager      *ui_manager;
 
383
  GtkActionGroup    *group;
 
384
  GtkWidget         *toolbar;
 
385
  GtkListStore      *list_store;
 
386
  GtkWidget         *tv;
 
387
  GtkTreeViewColumn *col;
 
388
  GtkCellRenderer   *rend;
 
389
  gint               i;
 
390
 
 
391
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
392
 
 
393
  list_store = gtk_list_store_new (NUM_COLUMNS,
 
394
                                   G_TYPE_BOOLEAN,   /*  SAVE          */
 
395
                                   G_TYPE_STRING,    /*  IDENTIFIER    */
 
396
                                   G_TYPE_DOUBLE,    /*  FACTOR        */
 
397
                                   G_TYPE_INT,       /*  DIGITS        */
 
398
                                   G_TYPE_STRING,    /*  SYMBOL        */
 
399
                                   G_TYPE_STRING,    /*  ABBREVIATION  */
 
400
                                   G_TYPE_STRING,    /*  SINGULAR      */
 
401
                                   G_TYPE_STRING,    /*  PLURAL        */
 
402
                                   GIMP_TYPE_UNIT,   /*  UNIT          */
 
403
                                   G_TYPE_BOOLEAN,   /*  USER_UNIT     */
 
404
                                   GDK_TYPE_COLOR);  /*  BG_COLOR      */
 
405
 
 
406
  tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
 
407
  g_object_unref (list_store);
 
408
 
 
409
  dialog = gimp_dialog_new (_("Unit Editor"), PLUG_IN_BINARY,
 
410
                            NULL, 0,
 
411
                            gimp_standard_help_func, PLUG_IN_PROC,
 
412
 
 
413
                            GTK_STOCK_REFRESH, RESPONSE_REFRESH,
 
414
                            GTK_STOCK_CLOSE,   GTK_RESPONSE_CLOSE,
 
415
 
 
416
                            NULL);
 
417
 
 
418
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
 
419
 
 
420
  g_signal_connect (dialog, "response",
 
421
                    G_CALLBACK (unit_editor_response),
 
422
                    tv);
 
423
  g_signal_connect (dialog, "destroy",
 
424
                    G_CALLBACK (gtk_main_quit),
 
425
                    NULL);
 
426
 
 
427
  /*  the toolbar  */
 
428
  ui_manager = gtk_ui_manager_new ();
 
429
 
 
430
  group = gtk_action_group_new ("unit-editor");
 
431
 
 
432
  gtk_action_group_set_translation_domain (group, NULL);
 
433
  gtk_action_group_add_actions (group, actions, G_N_ELEMENTS (actions), tv);
 
434
 
 
435
  gtk_window_add_accel_group (GTK_WINDOW (dialog),
 
436
                              gtk_ui_manager_get_accel_group (ui_manager));
 
437
  gtk_accel_group_lock (gtk_ui_manager_get_accel_group (ui_manager));
 
438
 
 
439
  gtk_ui_manager_insert_action_group (ui_manager, group, -1);
 
440
  g_object_unref (group);
 
441
 
 
442
  gtk_ui_manager_add_ui_from_string
 
443
    (ui_manager,
 
444
     "<ui>\n"
 
445
     "  <toolbar action=\"unit-editor-toolbar\">\n"
 
446
     "    <toolitem action=\"unit-editor-new\" />\n"
 
447
     "    <toolitem action=\"unit-editor-duplicate\" />\n"
 
448
     "  </toolbar>\n"
 
449
     "</ui>\n",
 
450
     -1, NULL);
 
451
 
 
452
  toolbar = gtk_ui_manager_get_widget (ui_manager, "/unit-editor-toolbar");
 
453
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), toolbar,
 
454
                      FALSE, FALSE, 0);
 
455
  gtk_widget_show (toolbar);
 
456
 
 
457
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
 
458
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
 
459
                                       GTK_SHADOW_IN);
 
460
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
 
461
                                  GTK_POLICY_NEVER,
 
462
                                  GTK_POLICY_ALWAYS);
 
463
  gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 12);
 
464
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
 
465
                     scrolled_win);
 
466
  gtk_widget_show (scrolled_win);
 
467
 
 
468
  gtk_widget_set_size_request (tv, -1, 220);
 
469
  gtk_container_add (GTK_CONTAINER (scrolled_win), tv);
 
470
  gtk_widget_show (tv);
 
471
 
 
472
  rend = gtk_cell_renderer_toggle_new ();
 
473
  col =
 
474
    gtk_tree_view_column_new_with_attributes (gettext (columns[SAVE].title),
 
475
                                              rend,
 
476
                                              "active",              SAVE,
 
477
                                              "activatable",         USER_UNIT,
 
478
                                              "cell-background-gdk", BG_COLOR,
 
479
                                              NULL);
 
480
 
 
481
  gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
 
482
 
 
483
  gimp_help_set_help_data (col->button,
 
484
                           gettext (columns[SAVE].help), NULL);
 
485
 
 
486
  g_signal_connect (rend, "toggled",
 
487
                    G_CALLBACK (saved_toggled_callback),
 
488
                    list_store);
 
489
 
 
490
  for (i = 0; i < G_N_ELEMENTS (columns); i++)
 
491
    {
 
492
      if (i == SAVE)
 
493
        continue;
 
494
 
 
495
      col =
 
496
        gtk_tree_view_column_new_with_attributes (gettext (columns[i].title),
 
497
                                                  gtk_cell_renderer_text_new (),
 
498
                                                  "text",                i,
 
499
                                                  "cell-background-gdk", BG_COLOR,
 
500
                                                  NULL);
 
501
 
 
502
      gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
 
503
 
 
504
      gimp_help_set_help_data (col->button, gettext (columns[i].help), NULL);
 
505
    }
 
506
 
 
507
  unit_list_init (GTK_TREE_VIEW (tv));
 
508
 
 
509
  gtk_widget_show (dialog);
 
510
 
 
511
  gtk_main ();
 
512
}
 
513
 
 
514
static void
 
515
unit_editor_response (GtkWidget *widget,
 
516
                      gint       response_id,
 
517
                      gpointer   data)
 
518
{
 
519
  switch (response_id)
 
520
    {
 
521
    case RESPONSE_REFRESH:
 
522
      unit_list_init (GTK_TREE_VIEW (data));
 
523
      break;
 
524
 
 
525
    default:
 
526
      gtk_widget_destroy (widget);
 
527
      break;
 
528
    }
 
529
}
 
530
 
 
531
static void
 
532
new_callback (GtkAction   *action,
 
533
              GtkTreeView *tv)
 
534
{
 
535
  GimpUnit  unit;
 
536
 
 
537
  unit = new_unit_dialog (gtk_widget_get_toplevel (GTK_WIDGET (tv)),
 
538
                          GIMP_UNIT_PIXEL);
 
539
 
 
540
  if (unit != GIMP_UNIT_PIXEL)
 
541
    {
 
542
      GtkTreeModel *model;
 
543
      GtkTreeIter   iter;
 
544
 
 
545
      unit_list_init (tv);
 
546
 
 
547
      model = gtk_tree_view_get_model (tv);
 
548
 
 
549
      if (gtk_tree_model_get_iter_first (model, &iter) &&
 
550
          gtk_tree_model_iter_nth_child (model, &iter,
 
551
                                         NULL, unit - GIMP_UNIT_INCH))
 
552
        {
 
553
          GtkAdjustment *adj;
 
554
 
 
555
          gtk_tree_selection_select_iter (gtk_tree_view_get_selection (tv),
 
556
                                          &iter);
 
557
 
 
558
          adj = gtk_tree_view_get_vadjustment (tv);
 
559
          gtk_adjustment_set_value (adj, adj->upper);
 
560
        }
 
561
    }
 
562
}
 
563
 
 
564
static void
 
565
duplicate_callback (GtkAction   *action,
 
566
                    GtkTreeView *tv)
 
567
{
 
568
  GtkTreeModel     *model;
 
569
  GtkTreeSelection *sel;
 
570
  GtkTreeIter       iter;
 
571
 
 
572
  model = gtk_tree_view_get_model (tv);
 
573
  sel   = gtk_tree_view_get_selection (tv);
 
574
 
 
575
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
 
576
    {
 
577
      GimpUnit unit;
 
578
 
 
579
      gtk_tree_model_get (model, &iter,
 
580
                          UNIT, &unit,
 
581
                          -1);
 
582
 
 
583
      unit = new_unit_dialog (gtk_widget_get_toplevel (GTK_WIDGET (tv)),
 
584
                              unit);
 
585
 
 
586
      if (unit != GIMP_UNIT_PIXEL)
 
587
        {
 
588
          GtkTreeIter iter;
 
589
 
 
590
          unit_list_init (tv);
 
591
 
 
592
          if (gtk_tree_model_get_iter_first (model, &iter) &&
 
593
              gtk_tree_model_iter_nth_child (model, &iter,
 
594
                                             NULL, unit - GIMP_UNIT_INCH))
 
595
            {
 
596
              GtkAdjustment *adj;
 
597
 
 
598
              gtk_tree_selection_select_iter (sel, &iter);
 
599
 
 
600
              adj = gtk_tree_view_get_vadjustment (tv);
 
601
              gtk_adjustment_set_value (adj, adj->upper);
 
602
            }
 
603
        }
 
604
    }
 
605
}
 
606
 
 
607
static void
 
608
saved_toggled_callback (GtkCellRendererToggle *celltoggle,
 
609
                        gchar                 *path_string,
 
610
                        GtkListStore          *list_store)
 
611
{
 
612
  GtkTreePath *path;
 
613
  GtkTreeIter  iter;
 
614
  gboolean     saved;
 
615
  GimpUnit     unit;
 
616
 
 
617
  path = gtk_tree_path_new_from_string (path_string);
 
618
 
 
619
  if (! gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store), &iter, path))
 
620
    {
 
621
      g_warning ("%s: bad tree path?", G_STRLOC);
 
622
      return;
 
623
    }
 
624
  gtk_tree_path_free (path);
 
625
 
 
626
  gtk_tree_model_get (GTK_TREE_MODEL (list_store), &iter,
 
627
                      SAVE, &saved,
 
628
                      UNIT, &unit,
 
629
                      -1);
 
630
 
 
631
  if (unit >= gimp_unit_get_number_of_built_in_units ())
 
632
    {
 
633
      gimp_unit_set_deletion_flag (unit, saved);
 
634
      gtk_list_store_set (GTK_LIST_STORE (list_store), &iter,
 
635
                          SAVE, ! saved,
 
636
                          -1);
 
637
    }
 
638
}
 
639
 
 
640
static void
 
641
unit_list_init (GtkTreeView *tv)
 
642
{
 
643
  GtkListStore *list_store;
 
644
  GtkTreeIter   iter;
 
645
  gint          num_units;
 
646
  GimpUnit      unit;
 
647
  GdkColor      color;
 
648
 
 
649
  list_store = GTK_LIST_STORE (gtk_tree_view_get_model (tv));
 
650
 
 
651
  gtk_list_store_clear (list_store);
 
652
 
 
653
  num_units = gimp_unit_get_number_of_units ();
 
654
 
 
655
  color.red   = 0xdddd;
 
656
  color.green = 0xdddd;
 
657
  color.blue  = 0xffff;
 
658
 
 
659
  for (unit = GIMP_UNIT_INCH; unit < num_units; unit++)
 
660
    {
 
661
      gboolean user_unit = (unit >= gimp_unit_get_number_of_built_in_units ());
 
662
 
 
663
      gtk_list_store_append (list_store, &iter);
 
664
      gtk_list_store_set (list_store, &iter,
 
665
                          SAVE,         ! gimp_unit_get_deletion_flag (unit),
 
666
                          IDENTIFIER,   gimp_unit_get_identifier (unit),
 
667
                          FACTOR,       gimp_unit_get_factor (unit),
 
668
                          DIGITS,       gimp_unit_get_digits (unit),
 
669
                          SYMBOL,       gimp_unit_get_symbol (unit),
 
670
                          ABBREVIATION, gimp_unit_get_abbreviation (unit),
 
671
                          SINGULAR,     gimp_unit_get_singular (unit),
 
672
                          PLURAL,       gimp_unit_get_plural (unit),
 
673
                          UNIT,         unit,
 
674
                          USER_UNIT,    user_unit,
 
675
 
 
676
                          user_unit ? -1 : BG_COLOR, &color,
 
677
 
 
678
                          -1);
 
679
    }
 
680
 
 
681
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
 
682
    gtk_tree_selection_select_iter (gtk_tree_view_get_selection (tv), &iter);
 
683
}