~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/actions/image-commands.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "libgimpwidgets/gimpwidgets.h"
 
24
 
 
25
#include "actions-types.h"
 
26
 
 
27
#include "core/core-enums.h"
 
28
#include "core/gimp.h"
 
29
#include "core/gimpchannel.h"
 
30
#include "core/gimpcontext.h"
 
31
#include "core/gimpimage.h"
 
32
#include "core/gimpimage-convert.h"
 
33
#include "core/gimpimage-crop.h"
 
34
#include "core/gimpimage-duplicate.h"
 
35
#include "core/gimpimage-flip.h"
 
36
#include "core/gimpimage-merge.h"
 
37
#include "core/gimpimage-resize.h"
 
38
#include "core/gimpimage-rotate.h"
 
39
#include "core/gimpimage-scale.h"
 
40
#include "core/gimpimage-undo.h"
 
41
#include "core/gimpprogress.h"
 
42
 
 
43
#include "widgets/gimpdialogfactory.h"
 
44
#include "widgets/gimpdock.h"
 
45
#include "widgets/gimphelp-ids.h"
 
46
 
 
47
#include "display/gimpdisplay.h"
 
48
#include "display/gimpdisplayshell.h"
 
49
 
 
50
#include "dialogs/convert-dialog.h"
 
51
#include "dialogs/dialogs.h"
 
52
#include "dialogs/grid-dialog.h"
 
53
#include "dialogs/image-merge-layers-dialog.h"
 
54
#include "dialogs/image-new-dialog.h"
 
55
#include "dialogs/image-scale-dialog.h"
 
56
#include "dialogs/print-size-dialog.h"
 
57
#include "dialogs/resize-dialog.h"
 
58
 
 
59
#include "actions.h"
 
60
#include "image-commands.h"
 
61
 
 
62
#include "gimp-intl.h"
 
63
 
 
64
 
 
65
typedef struct _ImageResizeOptions ImageResizeOptions;
 
66
 
 
67
struct _ImageResizeOptions
 
68
{
 
69
  GimpContext  *context;
 
70
  GimpDisplay  *gdisp;
 
71
};
 
72
 
 
73
 
 
74
/*  local function prototypes  */
 
75
 
 
76
static void   image_resize_callback        (GtkWidget              *dialog,
 
77
                                            GimpViewable           *viewable,
 
78
                                            gint                    width,
 
79
                                            gint                    height,
 
80
                                            gint                    offset_x,
 
81
                                            gint                    offset_y,
 
82
                                            gpointer                data);
 
83
static void   image_print_size_callback    (GtkWidget              *dialog,
 
84
                                            GimpImage              *image,
 
85
                                            gdouble                 xresolution,
 
86
                                            gdouble                 yresolution,
 
87
                                            GimpUnit                resolution_unit,
 
88
                                            gpointer                data);
 
89
static void   image_scale_callback         (ImageScaleDialog       *dialog);
 
90
 
 
91
static void   image_merge_layers_response  (GtkWidget              *widget,
 
92
                                            gint                    response_id,
 
93
                                            ImageMergeLayersDialog *dialog);
 
94
 
 
95
 
 
96
/*  private variables  */
 
97
 
 
98
static GimpMergeType image_merge_layers_type = GIMP_EXPAND_AS_NECESSARY;
 
99
 
 
100
 
 
101
/*  public functions  */
 
102
 
 
103
void
 
104
image_new_cmd_callback (GtkAction *action,
 
105
                        gpointer   data)
 
106
{
 
107
  GtkWidget *widget;
 
108
  GtkWidget *dialog;
 
109
  return_if_no_widget (widget, data);
 
110
 
 
111
  dialog = gimp_dialog_factory_dialog_new (global_dialog_factory,
 
112
                                           gtk_widget_get_screen (widget),
 
113
                                           "gimp-image-new-dialog", -1, FALSE);
 
114
 
 
115
  if (dialog)
 
116
    {
 
117
      image_new_dialog_set (dialog, NULL, NULL);
 
118
 
 
119
      gtk_window_present (GTK_WINDOW (dialog));
 
120
    }
 
121
}
 
122
 
 
123
void
 
124
image_new_from_image_cmd_callback (GtkAction *action,
 
125
                                   gpointer   data)
 
126
{
 
127
  GtkWidget *widget;
 
128
  GtkWidget *dialog;
 
129
  return_if_no_widget (widget, data);
 
130
 
 
131
  dialog = gimp_dialog_factory_dialog_new (global_dialog_factory,
 
132
                                           gtk_widget_get_screen (widget),
 
133
                                           "gimp-image-new-dialog", -1, FALSE);
 
134
 
 
135
  if (dialog)
 
136
    {
 
137
      GimpImage *gimage = action_data_get_image (data);
 
138
 
 
139
      image_new_dialog_set (dialog, gimage, NULL);
 
140
 
 
141
      gtk_window_present (GTK_WINDOW (dialog));
 
142
    }
 
143
}
 
144
 
 
145
void
 
146
image_convert_cmd_callback (GtkAction *action,
 
147
                            gint       value,
 
148
                            gpointer   data)
 
149
{
 
150
  GimpImage   *gimage;
 
151
  GtkWidget   *widget;
 
152
  GimpDisplay *gdisp;
 
153
  return_if_no_image (gimage, data);
 
154
  return_if_no_widget (widget, data);
 
155
  return_if_no_display (gdisp, data);
 
156
 
 
157
  switch ((GimpImageBaseType) value)
 
158
    {
 
159
    case GIMP_RGB:
 
160
    case GIMP_GRAY:
 
161
      gimp_image_convert (gimage, (GimpImageBaseType) value,
 
162
                          0, 0, FALSE, FALSE, 0, NULL, NULL);
 
163
      gimp_image_flush (gimage);
 
164
      break;
 
165
 
 
166
    case GIMP_INDEXED:
 
167
      gtk_widget_show (convert_dialog_new (gimage, widget,
 
168
                                           GIMP_PROGRESS (gdisp)));
 
169
      break;
 
170
    }
 
171
}
 
172
 
 
173
void
 
174
image_resize_cmd_callback (GtkAction *action,
 
175
                           gpointer   data)
 
176
{
 
177
  ImageResizeOptions *options;
 
178
  GimpImage          *gimage;
 
179
  GtkWidget          *widget;
 
180
  GimpDisplay        *gdisp;
 
181
  GtkWidget          *dialog;
 
182
  return_if_no_image (gimage, data);
 
183
  return_if_no_widget (widget, data);
 
184
  return_if_no_display (gdisp, data);
 
185
 
 
186
  options = g_new0 (ImageResizeOptions, 1);
 
187
 
 
188
  options->gdisp   = gdisp;
 
189
  options->context = action_data_get_context (data);
 
190
 
 
191
  dialog = resize_dialog_new (GIMP_VIEWABLE (gimage),
 
192
                              _("Set Image Canvas Size"), "gimp-image-resize",
 
193
                              widget,
 
194
                              gimp_standard_help_func, GIMP_HELP_IMAGE_RESIZE,
 
195
                              GIMP_DISPLAY_SHELL (gdisp->shell)->unit,
 
196
                              image_resize_callback,
 
197
                              options);
 
198
 
 
199
  g_signal_connect_object (gdisp, "disconnect",
 
200
                           G_CALLBACK (gtk_widget_destroy),
 
201
                           dialog, G_CONNECT_SWAPPED);
 
202
 
 
203
  g_object_weak_ref (G_OBJECT (dialog),
 
204
                     (GWeakNotify) g_free, options);
 
205
 
 
206
  gtk_widget_show (dialog);
 
207
}
 
208
 
 
209
void
 
210
image_resize_to_layers_cmd_callback (GtkAction *action,
 
211
                                     gpointer   data)
 
212
{
 
213
  GimpDisplay  *gdisp;
 
214
  GimpProgress *progress;
 
215
 
 
216
  return_if_no_display (gdisp, data);
 
217
 
 
218
  progress = gimp_progress_start (GIMP_PROGRESS (gdisp),
 
219
                                  _("Resizing..."), FALSE);
 
220
 
 
221
  gimp_image_resize_to_layers (gdisp->gimage,
 
222
                               action_data_get_context (data),
 
223
                               progress);
 
224
 
 
225
  if (progress)
 
226
    gimp_progress_end (progress);
 
227
 
 
228
  gimp_image_flush (gdisp->gimage);
 
229
}
 
230
 
 
231
void
 
232
image_print_size_cmd_callback (GtkAction *action,
 
233
                               gpointer   data)
 
234
{
 
235
  GtkWidget   *dialog;
 
236
  GimpDisplay *gdisp;
 
237
  GtkWidget   *widget;
 
238
  return_if_no_display (gdisp, data);
 
239
  return_if_no_widget (widget, data);
 
240
 
 
241
  dialog = print_size_dialog_new (gdisp->gimage,
 
242
                                  _("Set Image Print Resolution"),
 
243
                                  "gimp-image-print-size",
 
244
                                  widget,
 
245
                                  gimp_standard_help_func,
 
246
                                  GIMP_HELP_IMAGE_PRINT_SIZE,
 
247
                                  image_print_size_callback,
 
248
                                  NULL);
 
249
 
 
250
  g_signal_connect_object (gdisp, "disconnect",
 
251
                           G_CALLBACK (gtk_widget_destroy),
 
252
                           dialog, G_CONNECT_SWAPPED);
 
253
 
 
254
  gtk_widget_show (dialog);
 
255
}
 
256
 
 
257
void
 
258
image_scale_cmd_callback (GtkAction *action,
 
259
                          gpointer   data)
 
260
{
 
261
  ImageScaleDialog *dialog;
 
262
  GimpDisplay      *gdisp;
 
263
  GtkWidget        *widget;
 
264
  return_if_no_display (gdisp, data);
 
265
  return_if_no_widget (widget, data);
 
266
 
 
267
  dialog = image_scale_dialog_new (gdisp->gimage, gdisp,
 
268
                                   action_data_get_context (data),
 
269
                                   widget,
 
270
                                   image_scale_callback);
 
271
 
 
272
  g_signal_connect_object (gdisp, "disconnect",
 
273
                           G_CALLBACK (gtk_widget_destroy),
 
274
                           dialog->dialog, G_CONNECT_SWAPPED);
 
275
 
 
276
  gtk_widget_show (dialog->dialog);
 
277
}
 
278
 
 
279
void
 
280
image_flip_cmd_callback (GtkAction *action,
 
281
                         gint       value,
 
282
                         gpointer   data)
 
283
{
 
284
  GimpDisplay  *gdisp;
 
285
  GimpProgress *progress;
 
286
  return_if_no_display (gdisp, data);
 
287
 
 
288
  progress = gimp_progress_start (GIMP_PROGRESS (gdisp),
 
289
                                  _("Flipping..."), FALSE);
 
290
 
 
291
  gimp_image_flip (gdisp->gimage, action_data_get_context (data),
 
292
                   (GimpOrientationType) value, progress);
 
293
 
 
294
  if (progress)
 
295
    gimp_progress_end (progress);
 
296
 
 
297
  gimp_image_flush (gdisp->gimage);
 
298
}
 
299
 
 
300
void
 
301
image_rotate_cmd_callback (GtkAction *action,
 
302
                           gint       value,
 
303
                           gpointer   data)
 
304
{
 
305
  GimpDisplay  *gdisp;
 
306
  GimpProgress *progress;
 
307
  return_if_no_display (gdisp, data);
 
308
 
 
309
  progress = gimp_progress_start (GIMP_PROGRESS (gdisp),
 
310
                                  _("Rotating..."), FALSE);
 
311
 
 
312
  gimp_image_rotate (gdisp->gimage, action_data_get_context (data),
 
313
                     (GimpRotationType) value, progress);
 
314
 
 
315
  if (progress)
 
316
    gimp_progress_end (progress);
 
317
 
 
318
  gimp_image_flush (gdisp->gimage);
 
319
}
 
320
 
 
321
void
 
322
image_crop_cmd_callback (GtkAction *action,
 
323
                         gpointer   data)
 
324
{
 
325
  GimpImage *gimage;
 
326
  gint       x1, y1, x2, y2;
 
327
  return_if_no_image (gimage, data);
 
328
 
 
329
  if (! gimp_channel_bounds (gimp_image_get_mask (gimage),
 
330
                             &x1, &y1, &x2, &y2))
 
331
    {
 
332
      g_message (_("Cannot crop because the current selection is empty."));
 
333
      return;
 
334
    }
 
335
 
 
336
  gimp_image_crop (gimage, action_data_get_context (data),
 
337
                   x1, y1, x2, y2, FALSE, TRUE);
 
338
  gimp_image_flush (gimage);
 
339
}
 
340
 
 
341
void
 
342
image_duplicate_cmd_callback (GtkAction *action,
 
343
                              gpointer   data)
 
344
{
 
345
  GimpDisplay      *gdisp;
 
346
  GimpDisplayShell *shell;
 
347
  GimpImage        *new_gimage;
 
348
  return_if_no_display (gdisp, data);
 
349
 
 
350
  shell = GIMP_DISPLAY_SHELL (gdisp->shell);
 
351
 
 
352
  new_gimage = gimp_image_duplicate (gdisp->gimage);
 
353
 
 
354
  gimp_create_display (new_gimage->gimp,
 
355
                       new_gimage,
 
356
                       shell->unit, shell->scale);
 
357
 
 
358
  g_object_unref (new_gimage);
 
359
}
 
360
 
 
361
void
 
362
image_merge_layers_cmd_callback (GtkAction *action,
 
363
                                 gpointer   data)
 
364
{
 
365
  ImageMergeLayersDialog *dialog;
 
366
  GimpImage              *gimage;
 
367
  GtkWidget              *widget;
 
368
  return_if_no_image (gimage, data);
 
369
  return_if_no_widget (widget, data);
 
370
 
 
371
  dialog = image_merge_layers_dialog_new (gimage,
 
372
                                          action_data_get_context (data),
 
373
                                          widget,
 
374
                                          image_merge_layers_type);
 
375
 
 
376
  g_signal_connect (dialog->dialog, "response",
 
377
                    G_CALLBACK (image_merge_layers_response),
 
378
                    dialog);
 
379
 
 
380
  gtk_widget_show (dialog->dialog);
 
381
}
 
382
 
 
383
void
 
384
image_flatten_image_cmd_callback (GtkAction *action,
 
385
                                  gpointer   data)
 
386
{
 
387
  GimpImage *gimage;
 
388
  return_if_no_image (gimage, data);
 
389
 
 
390
  gimp_image_flatten (gimage, action_data_get_context (data));
 
391
  gimp_image_flush (gimage);
 
392
}
 
393
 
 
394
void
 
395
image_configure_grid_cmd_callback (GtkAction *action,
 
396
                                   gpointer   data)
 
397
{
 
398
  GimpDisplay      *gdisp;
 
399
  GimpDisplayShell *shell;
 
400
  GimpImage        *gimage;
 
401
  return_if_no_display (gdisp, data);
 
402
 
 
403
  shell  = GIMP_DISPLAY_SHELL (gdisp->shell);
 
404
  gimage = gdisp->gimage;
 
405
 
 
406
  if (! shell->grid_dialog)
 
407
    {
 
408
      shell->grid_dialog = grid_dialog_new (gdisp->gimage, gdisp->shell);
 
409
 
 
410
      gtk_window_set_transient_for (GTK_WINDOW (shell->grid_dialog),
 
411
                                    GTK_WINDOW (gdisp->shell));
 
412
      gtk_window_set_destroy_with_parent (GTK_WINDOW (shell->grid_dialog),
 
413
                                          TRUE);
 
414
 
 
415
      g_object_add_weak_pointer (G_OBJECT (shell->grid_dialog),
 
416
                                 (gpointer *) &shell->grid_dialog);
 
417
    }
 
418
 
 
419
  gtk_window_present (GTK_WINDOW (shell->grid_dialog));
 
420
}
 
421
 
 
422
 
 
423
/*  private functions  */
 
424
 
 
425
static void
 
426
image_resize_callback (GtkWidget    *dialog,
 
427
                       GimpViewable *viewable,
 
428
                       gint          width,
 
429
                       gint          height,
 
430
                       gint          offset_x,
 
431
                       gint          offset_y,
 
432
                       gpointer      data)
 
433
{
 
434
  ImageResizeOptions *options = data;
 
435
 
 
436
  if (width > 0 && height > 0)
 
437
    {
 
438
      GimpImage    *image   = GIMP_IMAGE (viewable);
 
439
      GimpDisplay  *gdisp   = options->gdisp;
 
440
      GimpContext  *context = options->context;
 
441
      GimpProgress *progress;
 
442
 
 
443
      gtk_widget_destroy (dialog);
 
444
 
 
445
      if (width == image->width && height == image->height)
 
446
        return;
 
447
 
 
448
      progress = gimp_progress_start (GIMP_PROGRESS (gdisp),
 
449
                                      _("Resizing..."), FALSE);
 
450
 
 
451
      gimp_image_resize (image,
 
452
                         context,
 
453
                         width, height, offset_x, offset_y,
 
454
                         progress);
 
455
 
 
456
      if (progress)
 
457
        gimp_progress_end (progress);
 
458
 
 
459
      gimp_image_flush (image);
 
460
    }
 
461
  else
 
462
    {
 
463
      g_warning ("Resize Error: "
 
464
                 "Both width and height must be greater than zero.");
 
465
    }
 
466
}
 
467
 
 
468
static void
 
469
image_print_size_callback (GtkWidget *dialog,
 
470
                           GimpImage *image,
 
471
                           gdouble    xresolution,
 
472
                           gdouble    yresolution,
 
473
                           GimpUnit   resolution_unit,
 
474
                           gpointer   data)
 
475
{
 
476
  gtk_widget_destroy (dialog);
 
477
 
 
478
  if (xresolution     == image->xresolution     &&
 
479
      yresolution     == image->yresolution     &&
 
480
      resolution_unit == image->resolution_unit)
 
481
    return;
 
482
 
 
483
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_SCALE,
 
484
                               _("Change Print Size"));
 
485
 
 
486
  gimp_image_set_resolution (image, xresolution, yresolution);
 
487
  gimp_image_set_unit (image, resolution_unit);
 
488
 
 
489
  gimp_image_undo_group_end (image);
 
490
 
 
491
  gimp_image_flush (image);
 
492
}
 
493
 
 
494
 
 
495
static void
 
496
image_scale_callback (ImageScaleDialog  *dialog)
 
497
{
 
498
  GimpImage *image = dialog->gimage;
 
499
 
 
500
  if (dialog->width           == image->width           &&
 
501
      dialog->height          == image->height          &&
 
502
      dialog->xresolution     == image->xresolution     &&
 
503
      dialog->yresolution     == image->yresolution     &&
 
504
      dialog->resolution_unit == image->resolution_unit)
 
505
    return;
 
506
 
 
507
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_SCALE,
 
508
                               _("Scale Image"));
 
509
 
 
510
  gimp_image_set_resolution (image,
 
511
                             dialog->xresolution, dialog->yresolution);
 
512
  gimp_image_set_unit (image, dialog->resolution_unit);
 
513
 
 
514
  if (dialog->width != image->width || dialog->height != image->height)
 
515
    {
 
516
      if (dialog->width > 0 && dialog->height > 0)
 
517
        {
 
518
          GimpProgress *progress;
 
519
 
 
520
          progress = gimp_progress_start (GIMP_PROGRESS (dialog->gdisp),
 
521
                                          _("Scaling..."), FALSE);
 
522
 
 
523
          gimp_image_scale (image,
 
524
                            dialog->width,
 
525
                            dialog->height,
 
526
                            dialog->interpolation,
 
527
                            progress);
 
528
 
 
529
          if (progress)
 
530
            gimp_progress_end (progress);
 
531
        }
 
532
      else
 
533
        {
 
534
          g_warning ("Scale Error: "
 
535
                     "Both width and height must be greater than zero.");
 
536
        }
 
537
    }
 
538
 
 
539
  gimp_image_undo_group_end (image);
 
540
 
 
541
  gimp_image_flush (image);
 
542
}
 
543
 
 
544
static void
 
545
image_merge_layers_response (GtkWidget              *widget,
 
546
                             gint                    response_id,
 
547
                             ImageMergeLayersDialog *dialog)
 
548
{
 
549
  if (response_id == GTK_RESPONSE_OK)
 
550
    {
 
551
      image_merge_layers_type = dialog->merge_type;
 
552
 
 
553
      gimp_image_merge_visible_layers (dialog->gimage,
 
554
                                       dialog->context,
 
555
                                       image_merge_layers_type);
 
556
      gimp_image_flush (dialog->gimage);
 
557
    }
 
558
 
 
559
  gtk_widget_destroy (widget);
 
560
}