1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This is a plug-in for GIMP.
6
* Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
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.
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.
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.
27
#include <libgimp/gimp.h>
28
#include <libgimp/gimpui.h>
30
#include "libgimp/stdplugins-intl.h"
33
#define PLUG_IN_PROC "plug-in-unit-editor"
34
#define PLUG_IN_BINARY "unit-editor"
35
#define RESPONSE_REFRESH 1
61
static void query (void);
62
static void run (const gchar *name,
64
const GimpParam *param,
66
GimpParam **return_vals);
68
static GimpUnit new_unit_dialog (GtkWidget *main_dialog,
70
static void unit_editor_dialog (void);
71
static void unit_editor_response (GtkWidget *widget,
74
static void new_callback (GtkAction *action,
76
static void duplicate_callback (GtkAction *action,
78
static void saved_toggled_callback (GtkCellRendererToggle *celltoggle,
80
GtkListStore *list_store);
81
static void unit_list_init (GtkTreeView *tv);
84
const GimpPlugInInfo PLUG_IN_INFO =
88
query, /* query_proc */
92
static const UnitColumn columns[] =
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 "
109
{ N_("Singular"), N_("The unit's singular form.") },
110
{ N_("Plural"), N_("The unit's plural form.") }
113
static GtkActionEntry actions[] =
115
{ "unit-editor-toolbar", NULL,
116
"Unit Editor Toolbar", NULL, NULL, NULL
119
{ "unit-editor-new", GTK_STOCK_NEW,
121
N_("Create a new unit from scratch"),
122
G_CALLBACK (new_callback)
125
{ "unit-editor-duplicate", GIMP_STOCK_DUPLICATE,
127
N_("Create a new unit using the currently selected unit as template"),
128
G_CALLBACK (duplicate_callback)
139
static const GimpParamDef args[] =
141
{ GIMP_PDB_INT32, "run-mode", "Interactive" }
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>",
153
G_N_ELEMENTS (args), 0,
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);
162
run (const gchar *name,
164
const GimpParam *param,
166
GimpParam **return_vals)
168
static GimpParam values[2];
169
GimpRunMode run_mode;
171
run_mode = param[0].data.d_int32;
176
*return_vals = values;
178
values[0].type = GIMP_PDB_STATUS;
179
values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
181
if (strcmp (name, PLUG_IN_PROC) == 0)
183
values[0].data.d_status = GIMP_PDB_SUCCESS;
185
unit_editor_dialog ();
190
new_unit_dialog (GtkWidget *main_dialog,
196
GtkWidget *spinbutton;
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;
206
GimpUnit unit = GIMP_UNIT_PIXEL;
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,
212
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
213
GTK_STOCK_ADD, GTK_RESPONSE_OK,
217
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
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,
228
gtk_widget_show (table);
230
entry = identifier_entry = gtk_entry_new ();
231
if (template != GIMP_UNIT_PIXEL)
233
gtk_entry_set_text (GTK_ENTRY (entry),
234
gimp_unit_get_identifier (template));
236
gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
240
gimp_help_set_help_data (entry, gettext (columns[IDENTIFIER].help), NULL);
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);
251
gimp_help_set_help_data (spinbutton, gettext (columns[FACTOR].help), NULL);
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);
261
gimp_help_set_help_data (spinbutton, gettext (columns[DIGITS].help), NULL);
263
entry = symbol_entry = gtk_entry_new ();
264
if (template != GIMP_UNIT_PIXEL)
266
gtk_entry_set_text (GTK_ENTRY (entry),
267
gimp_unit_get_symbol (template));
269
gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
270
_("_Symbol:"), 0.0, 0.5,
273
gimp_help_set_help_data (entry, gettext (columns[SYMBOL].help), NULL);
275
entry = abbreviation_entry = gtk_entry_new ();
276
if (template != GIMP_UNIT_PIXEL)
278
gtk_entry_set_text (GTK_ENTRY (entry),
279
gimp_unit_get_abbreviation (template));
281
gimp_table_attach_aligned (GTK_TABLE (table), 0, 4,
282
_("_Abbreviation:"), 0.0, 0.5,
285
gimp_help_set_help_data (entry, gettext (columns[ABBREVIATION].help), NULL);
287
entry = singular_entry = gtk_entry_new ();
288
if (template != GIMP_UNIT_PIXEL)
290
gtk_entry_set_text (GTK_ENTRY (entry),
291
gimp_unit_get_singular (template));
293
gimp_table_attach_aligned (GTK_TABLE (table), 0, 5,
294
_("Si_ngular:"), 0.0, 0.5,
297
gimp_help_set_help_data (entry, gettext (columns[SINGULAR].help), NULL);
299
entry = plural_entry = gtk_entry_new ();
300
if (template != GIMP_UNIT_PIXEL)
302
gtk_entry_set_text (GTK_ENTRY (entry),
303
gimp_unit_get_plural (template));
305
gimp_table_attach_aligned (GTK_TABLE (table), 0, 6,
306
_("_Plural:"), 0.0, 0.5,
309
gimp_help_set_help_data (entry, gettext (columns[PLURAL].help), NULL);
311
gtk_widget_show (dialog);
323
if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK)
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)));
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);
340
if (!strlen (identifier) |
342
!strlen (abbreviation) |
346
GtkWidget *msg = gtk_message_dialog_new (GTK_WINDOW (dialog), 0,
349
_("Incomplete input"));
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);
359
unit = gimp_unit_new (identifier,
361
symbol, abbreviation, singular, plural);
365
g_free (abbreviation);
372
gtk_widget_destroy (dialog);
378
unit_editor_dialog (void)
381
GtkWidget *scrolled_win;
382
GtkUIManager *ui_manager;
383
GtkActionGroup *group;
385
GtkListStore *list_store;
387
GtkTreeViewColumn *col;
388
GtkCellRenderer *rend;
391
gimp_ui_init (PLUG_IN_BINARY, FALSE);
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 */
406
tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
407
g_object_unref (list_store);
409
dialog = gimp_dialog_new (_("Unit Editor"), PLUG_IN_BINARY,
411
gimp_standard_help_func, PLUG_IN_PROC,
413
GTK_STOCK_REFRESH, RESPONSE_REFRESH,
414
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
418
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
420
g_signal_connect (dialog, "response",
421
G_CALLBACK (unit_editor_response),
423
g_signal_connect (dialog, "destroy",
424
G_CALLBACK (gtk_main_quit),
428
ui_manager = gtk_ui_manager_new ();
430
group = gtk_action_group_new ("unit-editor");
432
gtk_action_group_set_translation_domain (group, NULL);
433
gtk_action_group_add_actions (group, actions, G_N_ELEMENTS (actions), tv);
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));
439
gtk_ui_manager_insert_action_group (ui_manager, group, -1);
440
g_object_unref (group);
442
gtk_ui_manager_add_ui_from_string
445
" <toolbar action=\"unit-editor-toolbar\">\n"
446
" <toolitem action=\"unit-editor-new\" />\n"
447
" <toolitem action=\"unit-editor-duplicate\" />\n"
452
toolbar = gtk_ui_manager_get_widget (ui_manager, "/unit-editor-toolbar");
453
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), toolbar,
455
gtk_widget_show (toolbar);
457
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
458
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
460
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
463
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 12);
464
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
466
gtk_widget_show (scrolled_win);
468
gtk_widget_set_size_request (tv, -1, 220);
469
gtk_container_add (GTK_CONTAINER (scrolled_win), tv);
470
gtk_widget_show (tv);
472
rend = gtk_cell_renderer_toggle_new ();
474
gtk_tree_view_column_new_with_attributes (gettext (columns[SAVE].title),
477
"activatable", USER_UNIT,
478
"cell-background-gdk", BG_COLOR,
481
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
483
gimp_help_set_help_data (col->button,
484
gettext (columns[SAVE].help), NULL);
486
g_signal_connect (rend, "toggled",
487
G_CALLBACK (saved_toggled_callback),
490
for (i = 0; i < G_N_ELEMENTS (columns); i++)
496
gtk_tree_view_column_new_with_attributes (gettext (columns[i].title),
497
gtk_cell_renderer_text_new (),
499
"cell-background-gdk", BG_COLOR,
502
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
504
gimp_help_set_help_data (col->button, gettext (columns[i].help), NULL);
507
unit_list_init (GTK_TREE_VIEW (tv));
509
gtk_widget_show (dialog);
515
unit_editor_response (GtkWidget *widget,
521
case RESPONSE_REFRESH:
522
unit_list_init (GTK_TREE_VIEW (data));
526
gtk_widget_destroy (widget);
532
new_callback (GtkAction *action,
537
unit = new_unit_dialog (gtk_widget_get_toplevel (GTK_WIDGET (tv)),
540
if (unit != GIMP_UNIT_PIXEL)
547
model = gtk_tree_view_get_model (tv);
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))
555
gtk_tree_selection_select_iter (gtk_tree_view_get_selection (tv),
558
adj = gtk_tree_view_get_vadjustment (tv);
559
gtk_adjustment_set_value (adj, adj->upper);
565
duplicate_callback (GtkAction *action,
569
GtkTreeSelection *sel;
572
model = gtk_tree_view_get_model (tv);
573
sel = gtk_tree_view_get_selection (tv);
575
if (gtk_tree_selection_get_selected (sel, NULL, &iter))
579
gtk_tree_model_get (model, &iter,
583
unit = new_unit_dialog (gtk_widget_get_toplevel (GTK_WIDGET (tv)),
586
if (unit != GIMP_UNIT_PIXEL)
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))
598
gtk_tree_selection_select_iter (sel, &iter);
600
adj = gtk_tree_view_get_vadjustment (tv);
601
gtk_adjustment_set_value (adj, adj->upper);
608
saved_toggled_callback (GtkCellRendererToggle *celltoggle,
610
GtkListStore *list_store)
617
path = gtk_tree_path_new_from_string (path_string);
619
if (! gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store), &iter, path))
621
g_warning ("%s: bad tree path?", G_STRLOC);
624
gtk_tree_path_free (path);
626
gtk_tree_model_get (GTK_TREE_MODEL (list_store), &iter,
631
if (unit >= gimp_unit_get_number_of_built_in_units ())
633
gimp_unit_set_deletion_flag (unit, saved);
634
gtk_list_store_set (GTK_LIST_STORE (list_store), &iter,
641
unit_list_init (GtkTreeView *tv)
643
GtkListStore *list_store;
649
list_store = GTK_LIST_STORE (gtk_tree_view_get_model (tv));
651
gtk_list_store_clear (list_store);
653
num_units = gimp_unit_get_number_of_units ();
656
color.green = 0xdddd;
659
for (unit = GIMP_UNIT_INCH; unit < num_units; unit++)
661
gboolean user_unit = (unit >= gimp_unit_get_number_of_built_in_units ());
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),
674
USER_UNIT, user_unit,
676
user_unit ? -1 : BG_COLOR, &color,
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);