edit-commands.c 9.59 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"

Michael Natterer's avatar
Michael Natterer committed
27
#include "actions-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
#include "core/gimpbuffer.h"
#include "core/gimpcontainer.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
35
#include "core/gimpimage-undo.h"
Michael Natterer's avatar
Michael Natterer committed
36

37
#include "display/gimpdisplay.h"
38
39
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-transform.h"
40

41
#include "widgets/gimphelp-ids.h"
Michael Natterer's avatar
Michael Natterer committed
42
43
#include "widgets/gimpdialogfactory.h"

Michael Natterer's avatar
Michael Natterer committed
44
45
#include "gui/dialogs.h"

46
#include "actions.h"
Michael Natterer's avatar
Michael Natterer committed
47
48
#include "edit-commands.h"

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


/*  local function prototypes  */

54
static void   cut_named_buffer_callback  (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
55
56
                                          const gchar *name,
                                          gpointer     data);
57
static void   copy_named_buffer_callback (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
58
59
                                          const gchar *name,
                                          gpointer     data);
Michael Natterer's avatar
Michael Natterer committed
60
61
62
63
64


/*  public functions  */

void
65
edit_undo_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
66
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
67
{
Michael Natterer's avatar
Michael Natterer committed
68
69
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
70

71
  if (gimp_image_undo (gimage))
72
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
73
74
75
}

void
76
edit_redo_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
77
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
78
{
Michael Natterer's avatar
Michael Natterer committed
79
80
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
81

82
  if (gimp_image_redo (gimage))
83
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
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
static void
edit_undo_clear_callback (GtkWidget *widget,
                          gboolean   clear,
                          gpointer   data)
{
  if (clear)
    {
      GimpImage *gimage = data;

      gimp_image_undo_disable (gimage);
      gimp_image_undo_enable (gimage);
      gimp_image_flush (gimage);
    }
}

void
edit_undo_clear_cmd_callback (GtkAction *action,
                              gpointer   data)
{
  GimpImage *gimage;
  GtkWidget *widget;
  GtkWidget *dialog;
  return_if_no_image (gimage, data);
  return_if_no_widget (widget, data);

  dialog = gimp_query_boolean_box (_("Clear Undo History"), widget,
                                   gimp_standard_help_func,
                                   GIMP_HELP_EDIT_UNDO_CLEAR,
                                   GIMP_STOCK_QUESTION,
                                   _("Really clear image's undo history?"),
                                   GTK_STOCK_CLEAR, GTK_STOCK_CANCEL,
                                   G_OBJECT (gimage),
                                   "disconnect",
                                   edit_undo_clear_callback,
                                   gimage);
  gtk_widget_show (dialog);
}

Michael Natterer's avatar
Michael Natterer committed
124
void
125
edit_cut_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
126
                       gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
127
{
128
129
130
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
131

132
  if (gimp_edit_cut (gimage, drawable, gimp_get_user_context (gimage->gimp)))
133
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
134
135
136
}

void
137
edit_copy_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
138
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
139
{
140
141
142
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
143

144
  if (gimp_edit_copy (gimage, drawable, gimp_get_user_context (gimage->gimp)))
145
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
146
147
}

148
149
150
static void
edit_paste (GimpDisplay *gdisp,
            gboolean     paste_into)
Michael Natterer's avatar
Michael Natterer committed
151
{
152
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
153
    {
154
155
156
157
158
159
160
      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
161
      if (gimp_edit_paste (gdisp->gimage,
Michael Natterer's avatar
Michael Natterer committed
162
163
164
                           gimp_image_active_drawable (gdisp->gimage),
                           gdisp->gimage->gimp->global_buffer,
                           paste_into, x, y, width, height))
Michael Natterer's avatar
Michael Natterer committed
165
	{
166
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
167
168
169
170
	}
    }
}

171
void
172
edit_paste_cmd_callback (GtkAction *action,
173
174
175
176
177
178
179
180
181
                         gpointer   data)
{
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);

  edit_paste (gdisp, FALSE);
}

void
182
edit_paste_into_cmd_callback (GtkAction *action,
183
184
185
186
187
188
189
190
                              gpointer   data)
{
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);

  edit_paste (gdisp, TRUE);
}

Michael Natterer's avatar
Michael Natterer committed
191
void
192
edit_paste_as_new_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
193
                                gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
194
{
195
196
  Gimp *gimp;
  return_if_no_gimp (gimp, data);
Michael Natterer's avatar
Michael Natterer committed
197

198
199
200
  if (gimp->global_buffer)
    gimp_edit_paste_as_new (gimp, action_data_get_image (data),
                            gimp->global_buffer);
Michael Natterer's avatar
Michael Natterer committed
201
202
203
}

void
204
edit_named_cut_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
205
                             gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
206
{
207
208
209
210
211
  GimpImage *gimage;
  GtkWidget *widget;
  GtkWidget *qbox;
  return_if_no_image (gimage, data);
  return_if_no_widget (widget, data);
Michael Natterer's avatar
Michael Natterer committed
212

213
  qbox = gimp_query_string_box (_("Cut Named"), widget,
Michael Natterer's avatar
Michael Natterer committed
214
215
216
217
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_CUT,
                                _("Enter a name for this buffer"),
                                NULL,
218
219
                                G_OBJECT (gimage), "disconnect",
                                cut_named_buffer_callback, gimage);
Michael Natterer's avatar
Michael Natterer committed
220
221
222
223
  gtk_widget_show (qbox);
}

void
224
edit_named_copy_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
225
                              gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
226
{
227
228
229
230
231
  GimpImage *gimage;
  GtkWidget *widget;
  GtkWidget *qbox;
  return_if_no_image (gimage, data);
  return_if_no_widget (widget, data);
Michael Natterer's avatar
Michael Natterer committed
232

233
  qbox = gimp_query_string_box (_("Copy Named"), widget,
Michael Natterer's avatar
Michael Natterer committed
234
235
236
237
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_COPY,
                                _("Enter a name for this buffer"),
                                NULL,
238
239
                                G_OBJECT (gimage), "disconnect",
                                copy_named_buffer_callback, gimage);
Michael Natterer's avatar
Michael Natterer committed
240
241
242
243
  gtk_widget_show (qbox);
}

void
244
edit_named_paste_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
245
                               gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
246
{
247
248
  GtkWidget *widget;
  return_if_no_widget (widget, data);
249

250
  gimp_dialog_factory_dialog_raise (global_dock_factory,
251
                                    gtk_widget_get_screen (widget),
252
                                    "gimp-buffer-list|gimp-buffer-grid", -1);
Michael Natterer's avatar
Michael Natterer committed
253
254
255
}

void
256
edit_clear_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
257
                         gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
258
{
259
260
261
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
262

263
  gimp_edit_clear (gimage, drawable, gimp_get_user_context (gimage->gimp));
264
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
265
266
267
}

void
268
269
270
edit_fill_cmd_callback (GtkAction *action,
                        gint       value,
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
271
{
272
  GimpImage    *gimage;
273
  GimpDrawable *drawable;
274
  GimpFillType  fill_type;
275
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
276

277
  fill_type = (GimpFillType) value;
Michael Natterer's avatar
Michael Natterer committed
278

279
280
  gimp_edit_fill (gimage, drawable, gimp_get_user_context (gimage->gimp),
                  fill_type);
281
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
282
283
284
285
286
287
}


/*  private functions  */

static void
Sven Neumann's avatar
Sven Neumann committed
288
cut_named_buffer_callback (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
289
290
                           const gchar *name,
                           gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
291
{
Michael Natterer's avatar
Michael Natterer committed
292
  GimpImage        *gimage = GIMP_IMAGE (data);
293
  const GimpBuffer *cut_buffer;
294
  GimpDrawable     *active_drawable;
Michael Natterer's avatar
Michael Natterer committed
295

296
297
298
299
300
301
302
303
  active_drawable = gimp_image_active_drawable (gimage);

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

304
305
  cut_buffer = gimp_edit_cut (gimage, active_drawable,
                              gimp_get_user_context (gimage->gimp));
306
307

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
308
    {
309
310
      GimpBuffer *new_buffer;

311
312
313
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

316
317
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
318
      g_object_unref (new_buffer);
Michael Natterer's avatar
Michael Natterer committed
319

320
321
      gimp_image_flush (gimage);
    }
Michael Natterer's avatar
Michael Natterer committed
322
323
324
}

static void
325
326
327
copy_named_buffer_callback (GtkWidget   *widget,
			    const gchar *name,
			    gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
328
{
Michael Natterer's avatar
Michael Natterer committed
329
  GimpImage        *gimage = GIMP_IMAGE (data);
330
  const GimpBuffer *copy_buffer;
331
332
333
334
335
336
337
338
339
  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
340

341
342
  copy_buffer = gimp_edit_copy (gimage, active_drawable,
                                gimp_get_user_context (gimage->gimp));
Michael Natterer's avatar
Michael Natterer committed
343

344
  if (copy_buffer)
Michael Natterer's avatar
Michael Natterer committed
345
    {
346
347
      GimpBuffer *new_buffer;

348
349
350
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

353
354
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
355
      g_object_unref (new_buffer);
356
357

      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
358
359
    }
}