File: info-dialog.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (497 lines) | stat: -rw-r--r-- 14,766 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "dialogs-types.h"

#include "widgets/gimpviewabledialog.h"

#include "info-dialog.h"

#include "gimp-intl.h"


#define SB_WIDTH 10


/*  local function prototypes  */

static InfoDialog * info_dialog_new_extended    (GimpViewable  *viewable,
                                                 const gchar   *title,
                                                 const gchar   *role,
                                                 const gchar   *stock_id,
                                                 const gchar   *desc,
                                                 GtkWidget     *parent,
                                                 GimpHelpFunc   help_func,
                                                 gpointer       help_data,
                                                 gboolean       in_notebook);
static void         info_dialog_field_new       (InfoDialog    *idialog,
                                                 InfoFieldType  field_type,
                                                 gchar         *title,
                                                 GtkWidget     *widget,
                                                 GtkObject     *object,
                                                 gpointer       value_ptr,
                                                 GCallback      callback,
                                                 gpointer       callback_data);
static void         info_dialog_update_field    (InfoField     *info_field);
static void         info_dialog_field_free      (gpointer       data,
                                                 gpointer       user_data);


/*  public functions  */

InfoDialog *
info_dialog_new (GimpViewable *viewable,
                 const gchar  *title,
                 const gchar  *role,
                 const gchar  *stock_id,
                 const gchar  *desc,
                 GtkWidget    *parent,
		 GimpHelpFunc  help_func,
		 gpointer      help_data)
{
  return info_dialog_new_extended (viewable, title, role,
                                   stock_id, desc,
                                   parent,
                                   help_func, help_data, FALSE);
}

InfoDialog *
info_dialog_notebook_new (GimpViewable *viewable,
                          const gchar  *title,
                          const gchar  *role,
                          const gchar  *stock_id,
                          const gchar  *desc,
                          GtkWidget    *parent,
                          GimpHelpFunc  help_func,
			  gpointer      help_data)
{
  return info_dialog_new_extended (viewable, title, role,
                                   stock_id, desc,
                                   parent,
                                   help_func, help_data, TRUE);
}

static void
info_dialog_field_free (gpointer data,
                        gpointer user_data)
{
  InfoField *field = data;

  g_signal_handlers_disconnect_by_func (field->obj,
                                        field->callback,
                                        field->callback_data);
  g_free (field);
}

void
info_dialog_free (InfoDialog *idialog)
{
  g_return_if_fail (idialog != NULL);

  g_slist_foreach (idialog->field_list, (GFunc) info_dialog_field_free, NULL);
  g_slist_free (idialog->field_list);

  gtk_widget_destroy (idialog->shell);

  g_free (idialog);
}

void
info_dialog_show (InfoDialog *idialog)
{
  g_return_if_fail (idialog != NULL);

  gtk_widget_show (idialog->shell);
}

void
info_dialog_present (InfoDialog *idialog)
{
  g_return_if_fail (idialog != NULL);

  gtk_window_present (GTK_WINDOW (idialog->shell));
}

void
info_dialog_hide (InfoDialog *idialog)
{
  g_return_if_fail (idialog != NULL);

  gtk_widget_hide (idialog->shell);
}

void
info_dialog_update (InfoDialog *idialog)
{
  GSList *list;

  g_return_if_fail (idialog != NULL);

  for (list = idialog->field_list; list; list = g_slist_next (list))
    info_dialog_update_field ((InfoField *) list->data);
}

GtkWidget *
info_dialog_add_label (InfoDialog *idialog,
		       gchar      *title,
		       gchar      *text_ptr)
{
  GtkWidget *label;

  g_return_val_if_fail (idialog != NULL, NULL);

  label = gtk_label_new (text_ptr);
  gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);

  info_dialog_field_new (idialog, INFO_LABEL, title, label, NULL,
                         text_ptr,
                         NULL, NULL);

  return label;
}

GtkWidget *
info_dialog_add_entry (InfoDialog    *idialog,
		       gchar         *title,
		       gchar         *text_ptr,
		       GCallback      callback,
		       gpointer       callback_data)
{
  GtkWidget *entry;

  g_return_val_if_fail (idialog != NULL, NULL);

  entry = gtk_entry_new ();
  gtk_widget_set_size_request (entry, 50, -1);
  gtk_entry_set_text (GTK_ENTRY (entry), text_ptr ? text_ptr : "");

  if (callback)
    g_signal_connect (entry, "changed",
		      callback,
		      callback_data);

  info_dialog_field_new (idialog, INFO_ENTRY, title, entry, NULL,
                         text_ptr,
                         callback, callback_data);

  return entry;
}

GtkWidget *
info_dialog_add_scale   (InfoDialog    *idialog,
			 gchar         *title,
			 gdouble       *value_ptr,
			 gfloat         lower,
			 gfloat         upper,
			 gfloat         step_increment,
			 gfloat         page_increment,
			 gfloat         page_size,
			 gint           digits,
			 GCallback      callback,
			 gpointer       callback_data)
{
  GtkObject *adjustment;
  GtkWidget *scale;

  g_return_val_if_fail (idialog != NULL, NULL);

  adjustment = gtk_adjustment_new (value_ptr ? *value_ptr : 0, lower, upper,
				   step_increment, page_increment, page_size);
  scale = gtk_hscale_new (GTK_ADJUSTMENT (adjustment));

  if (digits >= 0)
    gtk_scale_set_digits (GTK_SCALE (scale), MAX (digits, 6));
  else
    gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);

  if (callback)
    g_signal_connect (adjustment, "value_changed",
		      callback,
		      callback_data);

  info_dialog_field_new (idialog, INFO_SCALE, title, scale, adjustment,
                         value_ptr,
                         callback, callback_data);

  return scale;
}

GtkWidget *
info_dialog_add_spinbutton (InfoDialog    *idialog,
			    gchar         *title,
			    gdouble       *value_ptr,
			    gfloat         lower,
			    gfloat         upper,
			    gfloat         step_increment,
			    gfloat         page_increment,
			    gfloat         page_size,
			    gfloat         climb_rate,
			    gint           digits,
			    GCallback      callback,
			    gpointer       callback_data)
{
  GtkWidget *alignment;
  GtkObject *adjustment;
  GtkWidget *spinbutton;

  g_return_val_if_fail (idialog != NULL, NULL);

  alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);

  spinbutton = gimp_spin_button_new (&adjustment,
                                     value_ptr ? *value_ptr : 0,
                                     lower, upper,
                                     step_increment, page_increment, page_size,
                                     climb_rate, MAX (MIN (digits, 6), 0));
  gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), SB_WIDTH);

  if (callback)
    g_signal_connect (adjustment, "value_changed",
		      callback,
		      callback_data);

  gtk_container_add (GTK_CONTAINER (alignment), spinbutton);
  gtk_widget_show (spinbutton);

  info_dialog_field_new (idialog, INFO_SPINBUTTON, title, alignment,
                         adjustment,
                         value_ptr,
                         callback, callback_data);

  return spinbutton;
}

GtkWidget *
info_dialog_add_sizeentry (InfoDialog                *idialog,
			   gchar                     *title,
			   gdouble                   *value_ptr,
			   gint                       nfields,
			   GimpUnit                   unit,
			   gchar                     *unit_format,
			   gboolean                   menu_show_pixels,
			   gboolean                   menu_show_percent,
			   gboolean                   show_refval,
			   GimpSizeEntryUpdatePolicy  update_policy,
			   GCallback                  callback,
			   gpointer                   callback_data)
{
  GtkWidget *alignment;
  GtkWidget *sizeentry;
  gint       i;

  g_return_val_if_fail (idialog != NULL, NULL);

  alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);

  sizeentry = gimp_size_entry_new (nfields, unit, unit_format,
				   menu_show_pixels, menu_show_percent,
				   show_refval, SB_WIDTH,
				   update_policy);
  if (value_ptr)
    for (i = 0; i < nfields; i++)
      gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (sizeentry), i, value_ptr[i]);

  if (callback)
    g_signal_connect (sizeentry, "value_changed",
		      callback,
		      callback_data);

  gtk_container_add (GTK_CONTAINER (alignment), sizeentry);
  gtk_widget_show (sizeentry);

  info_dialog_field_new (idialog, INFO_SIZEENTRY, title, alignment,
                         GTK_OBJECT (sizeentry),
                         value_ptr,
                         callback, callback_data);

  return sizeentry;
}


/*  private functions  */

static InfoDialog *
info_dialog_new_extended (GimpViewable *viewable,
                          const gchar  *title,
                          const gchar  *role,
                          const gchar  *stock_id,
                          const gchar  *desc,
                          GtkWidget    *parent,
			  GimpHelpFunc  help_func,
			  gpointer      help_data,
			  gboolean      in_notebook)
{
  InfoDialog *idialog;
  GtkWidget  *shell;
  GtkWidget  *vbox;
  GtkWidget  *info_table;
  GtkWidget  *info_notebook;

  idialog = g_new (InfoDialog, 1);
  idialog->field_list = NULL;
  idialog->nfields    = 0;

  shell = gimp_viewable_dialog_new (viewable,
                                    title, role,
                                    stock_id, desc,
                                    parent,
                                    help_func, help_data,
                                    NULL);

  vbox = gtk_vbox_new (FALSE, 6);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (shell)->vbox), vbox);

  info_table = gtk_table_new (2, 0, FALSE);

  if (in_notebook)
    {
      info_notebook = gtk_notebook_new ();
      gtk_container_set_border_width (GTK_CONTAINER (info_table), 6);
      gtk_notebook_append_page (GTK_NOTEBOOK (info_notebook),
				info_table,
				gtk_label_new (_("General")));
      gtk_box_pack_start (GTK_BOX (vbox), info_notebook, FALSE, FALSE, 0);
    }
  else
    {
      info_notebook = NULL;
      gtk_box_pack_start (GTK_BOX (vbox), info_table, FALSE, FALSE, 0);
    }

  idialog->shell         = shell;
  idialog->vbox          = vbox;
  idialog->info_table    = info_table;
  idialog->info_notebook = info_notebook;

  if (in_notebook)
    gtk_widget_show (idialog->info_notebook);

  gtk_widget_show (idialog->info_table);
  gtk_widget_show (idialog->vbox);

  return idialog;
}

static void
info_dialog_field_new (InfoDialog    *idialog,
                       InfoFieldType  field_type,
                       gchar         *title,
                       GtkWidget     *widget,
                       GtkObject     *obj,
                       gpointer       value_ptr,
                       GCallback      callback,
                       gpointer       callback_data)
{
  GtkWidget *label;
  InfoField *field;
  gint       row;

  field = g_new (InfoField, 1);

  row = idialog->nfields + 1;
  gtk_table_resize (GTK_TABLE (idialog->info_table), 2, row);

  label = gtk_label_new (title);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (idialog->info_table), label,
		    0, 1, row - 1, row,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  gtk_table_attach_defaults (GTK_TABLE (idialog->info_table), widget,
			     1, 2, row - 1, row);
  gtk_widget_show (widget);

  gtk_table_set_col_spacing (GTK_TABLE (idialog->info_table), 0, 6);
  gtk_table_set_row_spacings (GTK_TABLE (idialog->info_table), 2);

  field->field_type = field_type;

  if (obj == NULL)
    field->obj = GTK_OBJECT (widget);
  else
    field->obj = obj;

  field->value_ptr     = value_ptr;
  field->callback      = callback;
  field->callback_data = callback_data;

  idialog->field_list = g_slist_prepend (idialog->field_list, field);
  idialog->nfields++;
}

static void
info_dialog_update_field (InfoField *field)
{
  const gchar *old_text;
  gint         num;
  gint         i;

  if (field->value_ptr == NULL)
    return;

  if (field->field_type != INFO_LABEL)
    g_signal_handlers_block_by_func (field->obj,
				     field->callback,
				     field->callback_data);

  switch (field->field_type)
    {
    case INFO_LABEL:
      gtk_label_set_text (GTK_LABEL (field->obj), (gchar *) field->value_ptr);
      break;

    case INFO_ENTRY:
      old_text = gtk_entry_get_text (GTK_ENTRY (field->obj));
      if (strcmp (old_text, (gchar *) field->value_ptr))
	gtk_entry_set_text (GTK_ENTRY (field->obj), (gchar *) field->value_ptr);
      break;

    case INFO_SCALE:
    case INFO_SPINBUTTON:
      gtk_adjustment_set_value (GTK_ADJUSTMENT (field->obj),
				*((gdouble *) field->value_ptr));
      break;

    case INFO_SIZEENTRY:
      num = GIMP_SIZE_ENTRY (field->obj)->number_of_fields;
      for (i = 0; i < num; i++)
	gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (field->obj), i,
				    ((gdouble *) field->value_ptr)[i]);
      break;

    default:
      g_warning ("%s: Unknown info_dialog field type.", G_STRFUNC);
      break;
    }

  if (field->field_type != INFO_LABEL)
    g_signal_handlers_unblock_by_func (field->obj,
				       field->callback,
				       field->callback_data);
}