edit-commands.c 10.1 KB
Newer Older
Michael Natterer's avatar
Michael Natterer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 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"

Hans Breuer's avatar
Hans Breuer committed
21
22
#include <string.h>

Michael Natterer's avatar
Michael Natterer committed
23
24
25
26
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

27
#include "gui-types.h"
Michael Natterer's avatar
Michael Natterer committed
28

29
#include "core/gimp.h"
30
#include "core/gimp-edit.h"
Michael Natterer's avatar
Michael Natterer committed
31
32
33
34
35
#include "core/gimpbuffer.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
36
#include "core/gimpimage-undo.h"
37
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
38

39
#include "display/gimpdisplay.h"
40
41
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-transform.h"
42

43
#include "widgets/gimpdock.h"
44
#include "widgets/gimphelp-ids.h"
Michael Natterer's avatar
Michael Natterer committed
45
46
47
#include "widgets/gimpdialogfactory.h"

#include "dialogs.h"
Michael Natterer's avatar
Michael Natterer committed
48
#include "edit-commands.h"
Michael Natterer's avatar
Michael Natterer committed
49
#include "stroke-dialog.h"
Michael Natterer's avatar
Michael Natterer committed
50

51
#include "gimp-intl.h"
Michael Natterer's avatar
Michael Natterer committed
52
53


Michael Natterer's avatar
Michael Natterer committed
54
#define return_if_no_display(gdisp,data) \
55
56
57
58
  if (GIMP_IS_DISPLAY (data)) \
    gdisp = data; \
  else if (GIMP_IS_GIMP (data)) \
    gdisp = gimp_context_get_display (gimp_get_user_context (GIMP (data))); \
59
60
  else if (GIMP_IS_DOCK (data)) \
    gdisp = gimp_context_get_display (((GimpDock *) data)->context); \
61
62
  else \
    gdisp = NULL; \
Michael Natterer's avatar
Michael Natterer committed
63
64
65
66
  if (! gdisp) \
    return

#define return_if_no_image(gimage,data) \
67
68
69
70
  if (GIMP_IS_DISPLAY (data)) \
    gimage = ((GimpDisplay *) data)->gimage; \
  else if (GIMP_IS_GIMP (data)) \
    gimage = gimp_context_get_image (gimp_get_user_context (GIMP (data))); \
71
72
  else if (GIMP_IS_DOCK (data)) \
    gimage = gimp_context_get_image (((GimpDock *) data)->context); \
73
74
  else \
    gimage = NULL; \
Michael Natterer's avatar
Michael Natterer committed
75
76
  if (! gimage) \
    return
Michael Natterer's avatar
Michael Natterer committed
77

78
79
80
81
82
83
#define return_if_no_drawable(gimage,drawable,data) \
  return_if_no_image (gimage, data); \
  drawable = gimp_image_active_drawable (gimage); \
  if (! drawable) \
    return;

Michael Natterer's avatar
Michael Natterer committed
84
85
86

/*  local function prototypes  */

87
static void   cut_named_buffer_callback  (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
88
89
                                          const gchar *name,
                                          gpointer     data);
90
static void   copy_named_buffer_callback (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
91
92
                                          const gchar *name,
                                          gpointer     data);
Michael Natterer's avatar
Michael Natterer committed
93
94
95
96
97
98


/*  public functions  */

void
edit_undo_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
99
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
100
{
Michael Natterer's avatar
Michael Natterer committed
101
102
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
103

104
  if (gimp_image_undo (gimage))
105
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
106
107
108
109
}

void
edit_redo_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
110
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
111
{
Michael Natterer's avatar
Michael Natterer committed
112
113
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
114

115
  if (gimp_image_redo (gimage))
116
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
117
118
119
120
}

void
edit_cut_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
121
                       gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
122
{
123
124
125
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
126

127
  if (gimp_edit_cut (gimage, drawable, gimp_get_user_context (gimage->gimp)))
128
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
129
130
131
132
}

void
edit_copy_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
133
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
134
{
135
136
137
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
138

139
  if (gimp_edit_copy (gimage, drawable, gimp_get_user_context (gimage->gimp)))
140
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
141
142
143
144
}

void
edit_paste_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
145
146
                         gpointer   data,
                         guint      action)
Michael Natterer's avatar
Michael Natterer committed
147
{
Michael Natterer's avatar
Michael Natterer committed
148
  GimpDisplay *gdisp;
Michael Natterer's avatar
Michael Natterer committed
149
  gboolean     paste_into;
Michael Natterer's avatar
Michael Natterer committed
150
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
151

Michael Natterer's avatar
Michael Natterer committed
152
  paste_into = (gboolean) action;
Michael Natterer's avatar
Michael Natterer committed
153

154
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
155
    {
156
157
158
159
160
161
162
      GimpDisplayShell *shell;
      gint              x, y, width, height;

      shell = GIMP_DISPLAY_SHELL (gdisp->shell);

      gimp_display_shell_untransform_viewport (shell, &x, &y, &width, &height);

Michael Natterer's avatar
Michael Natterer committed
163
      if (gimp_edit_paste (gdisp->gimage,
Michael Natterer's avatar
Michael Natterer committed
164
165
166
                           gimp_image_active_drawable (gdisp->gimage),
                           gdisp->gimage->gimp->global_buffer,
                           paste_into, x, y, width, height))
Michael Natterer's avatar
Michael Natterer committed
167
	{
168
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
169
170
171
172
173
174
	}
    }
}

void
edit_paste_as_new_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
175
                                gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
176
{
Michael Natterer's avatar
Michael Natterer committed
177
178
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
179

180
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
181
    {
182
      gimp_edit_paste_as_new (gdisp->gimage->gimp,
Michael Natterer's avatar
Michael Natterer committed
183
184
                              gdisp->gimage,
                              gdisp->gimage->gimp->global_buffer);
Michael Natterer's avatar
Michael Natterer committed
185
186
187
188
189
    }
}

void
edit_named_cut_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
190
                             gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
191
{
Michael Natterer's avatar
Michael Natterer committed
192
193
194
  GimpDisplay *gdisp;
  GtkWidget   *qbox;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
195
196

  qbox = gimp_query_string_box (_("Cut Named"),
197
                                gdisp->shell,
Michael Natterer's avatar
Michael Natterer committed
198
199
200
201
202
203
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_CUT,
                                _("Enter a name for this buffer"),
                                NULL,
                                G_OBJECT (gdisp->gimage), "disconnect",
                                cut_named_buffer_callback, gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
204
205
206
207
208
  gtk_widget_show (qbox);
}

void
edit_named_copy_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
209
                              gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
210
{
211
212
213
  GimpDisplay *gdisp;
  GtkWidget   *qbox;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
214
215

  qbox = gimp_query_string_box (_("Copy Named"),
216
                                gdisp->shell,
Michael Natterer's avatar
Michael Natterer committed
217
218
219
220
221
222
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_COPY,
                                _("Enter a name for this buffer"),
                                NULL,
                                G_OBJECT (gdisp->gimage), "disconnect",
                                copy_named_buffer_callback, gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
223
224
225
226
227
  gtk_widget_show (qbox);
}

void
edit_named_paste_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
228
                               gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
229
{
230
231
  gimp_dialog_factory_dialog_raise (global_dock_factory,
                                    gtk_widget_get_screen (widget),
232
                                    "gimp-buffer-list|gimp-buffer-grid", -1);
Michael Natterer's avatar
Michael Natterer committed
233
234
235
236
}

void
edit_clear_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
237
                         gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
238
{
239
240
241
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
242

243
  gimp_edit_clear (gimage, drawable, gimp_get_user_context (gimage->gimp));
244
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
245
246
247
248
}

void
edit_fill_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
249
250
                        gpointer   data,
                        guint      action)
Michael Natterer's avatar
Michael Natterer committed
251
{
252
  GimpImage    *gimage;
253
  GimpDrawable *drawable;
254
  GimpFillType  fill_type;
255
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
256

257
  fill_type = (GimpFillType) action;
Michael Natterer's avatar
Michael Natterer committed
258

259
260
  gimp_edit_fill (gimage, drawable, gimp_get_user_context (gimage->gimp),
                  fill_type);
261
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
262
263
264
265
}

void
edit_stroke_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
266
                          gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
267
{
268
269
270
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
271

272
  edit_stroke_selection (GIMP_ITEM (gimp_image_get_mask (gimage)), widget);
Michael Natterer's avatar
Michael Natterer committed
273
274
275
}

void
276
277
edit_stroke_selection (GimpItem  *item,
                       GtkWidget *parent)
Michael Natterer's avatar
Michael Natterer committed
278
{
279
280
  GimpImage    *gimage;
  GimpDrawable *active_drawable;
Michael Natterer's avatar
Michael Natterer committed
281
282
283
284
285
  GtkWidget    *dialog;

  g_return_if_fail (GIMP_IS_ITEM (item));

  gimage = gimp_item_get_image (item);
Michael Natterer's avatar
Michael Natterer committed
286

287
288
289
290
  active_drawable = gimp_image_active_drawable (gimage);

  if (! active_drawable)
    {
291
      g_message (_("There is no active layer or channel to stroke to."));
292
293
294
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
295
  dialog = stroke_dialog_new (item, GIMP_STOCK_SELECTION_STROKE,
296
297
                              GIMP_HELP_SELECTION_STROKE,
                              parent);
Michael Natterer's avatar
Michael Natterer committed
298
  gtk_widget_show (dialog);
Michael Natterer's avatar
Michael Natterer committed
299
300
301
302
303
304
}


/*  private functions  */

static void
Sven Neumann's avatar
Sven Neumann committed
305
cut_named_buffer_callback (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
306
307
                           const gchar *name,
                           gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
308
{
Michael Natterer's avatar
Michael Natterer committed
309
  GimpImage        *gimage = GIMP_IMAGE (data);
310
  const GimpBuffer *cut_buffer;
311
  GimpDrawable     *active_drawable;
Michael Natterer's avatar
Michael Natterer committed
312

313
314
315
316
317
318
319
320
  active_drawable = gimp_image_active_drawable (gimage);

  if (! active_drawable)
    {
      g_message (_("There is no active layer or channel to cut from."));
      return;
    }

321
322
  cut_buffer = gimp_edit_cut (gimage, active_drawable,
                              gimp_get_user_context (gimage->gimp));
323
324

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
325
    {
326
327
      GimpBuffer *new_buffer;

328
329
330
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

331
      new_buffer = gimp_buffer_new (cut_buffer->tiles, name, TRUE);
Michael Natterer's avatar
Michael Natterer committed
332

333
334
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
335
      g_object_unref (new_buffer);
Michael Natterer's avatar
Michael Natterer committed
336

337
338
      gimp_image_flush (gimage);
    }
Michael Natterer's avatar
Michael Natterer committed
339
340
341
}

static void
342
343
344
copy_named_buffer_callback (GtkWidget   *widget,
			    const gchar *name,
			    gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
345
{
Michael Natterer's avatar
Michael Natterer committed
346
  GimpImage        *gimage = GIMP_IMAGE (data);
347
  const GimpBuffer *copy_buffer;
348
349
350
351
352
353
354
355
356
  GimpDrawable     *active_drawable;

  active_drawable = gimp_image_active_drawable (gimage);

  if (! active_drawable)
    {
      g_message (_("There is no active layer or channel to copy from."));
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
357

358
359
  copy_buffer = gimp_edit_copy (gimage, active_drawable,
                                gimp_get_user_context (gimage->gimp));
Michael Natterer's avatar
Michael Natterer committed
360

361
  if (copy_buffer)
Michael Natterer's avatar
Michael Natterer committed
362
    {
363
364
      GimpBuffer *new_buffer;

365
366
367
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

368
      new_buffer = gimp_buffer_new (copy_buffer->tiles, name, TRUE);
Michael Natterer's avatar
Michael Natterer committed
369

370
371
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
372
      g_object_unref (new_buffer);
373
374

      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
375
376
    }
}