edit-commands.c 8.61 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/gimphelp-ids.h"
Michael Natterer's avatar
Michael Natterer committed
44
45
46
#include "widgets/gimpdialogfactory.h"

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

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


Michael Natterer's avatar
Michael Natterer committed
53
#define return_if_no_display(gdisp,data) \
54
55
56
57
58
59
  if (GIMP_IS_DISPLAY (data)) \
    gdisp = data; \
  else if (GIMP_IS_GIMP (data)) \
    gdisp = gimp_context_get_display (gimp_get_user_context (GIMP (data))); \
  else \
    gdisp = NULL; \
Michael Natterer's avatar
Michael Natterer committed
60
61
62
63
  if (! gdisp) \
    return

#define return_if_no_image(gimage,data) \
64
65
66
67
68
69
  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))); \
  else \
    gimage = NULL; \
Michael Natterer's avatar
Michael Natterer committed
70
71
  if (! gimage) \
    return
Michael Natterer's avatar
Michael Natterer committed
72
73
74
75


/*  local function prototypes  */

76
77
78
79
80
81
static void   cut_named_buffer_callback  (GtkWidget   *widget,
					  const gchar *name,
					  gpointer     data);
static void   copy_named_buffer_callback (GtkWidget   *widget,
					  const gchar *name,
					  gpointer     data);
Michael Natterer's avatar
Michael Natterer committed
82
83
84
85
86
87
88
89


/*  public functions  */

void
edit_undo_cmd_callback (GtkWidget *widget,
			gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
90
91
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
92

93
  if (gimp_image_undo (gimage))
94
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
95
96
97
98
99
100
}

void
edit_redo_cmd_callback (GtkWidget *widget,
			gpointer   data)
{
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_redo (gimage))
105
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
106
107
108
109
110
111
}

void
edit_cut_cmd_callback (GtkWidget *widget,
		       gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
112
113
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
114

Michael Natterer's avatar
Michael Natterer committed
115
116
  if (gimp_edit_cut (gdisp->gimage,
		     gimp_image_active_drawable (gdisp->gimage)))
Michael Natterer's avatar
Michael Natterer committed
117
    {
118
      gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
119
120
121
122
123
124
125
    }
}

void
edit_copy_cmd_callback (GtkWidget *widget,
			gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
126
127
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
128

129
130
  if (gimp_edit_copy (gimage, gimp_image_active_drawable (gimage)))
    {
131
      gimp_image_flush (gimage);
132
    }
Michael Natterer's avatar
Michael Natterer committed
133
134
135
136
137
138
}

void
edit_paste_cmd_callback (GtkWidget *widget,
			 gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
139
140
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
141

142
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
143
    {
144
145
146
147
148
149
150
      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
151
      if (gimp_edit_paste (gdisp->gimage,
152
			   gimp_image_active_drawable (gdisp->gimage),
153
			   gdisp->gimage->gimp->global_buffer,
154
			   FALSE, x, y, width, height))
Michael Natterer's avatar
Michael Natterer committed
155
	{
156
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
157
158
159
160
161
162
163
164
	}
    }
}

void
edit_paste_into_cmd_callback (GtkWidget *widget,
			      gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
165
166
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
167

168
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
169
    {
170
171
172
173
174
175
176
      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
177
      if (gimp_edit_paste (gdisp->gimage,
178
			   gimp_image_active_drawable (gdisp->gimage),
179
			   gdisp->gimage->gimp->global_buffer,
180
			   TRUE, x, y, width, height))
Michael Natterer's avatar
Michael Natterer committed
181
	{
182
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
183
184
185
186
187
188
189
190
	}
    }
}

void
edit_paste_as_new_cmd_callback (GtkWidget *widget,
				gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
191
192
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
193

194
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
195
    {
196
197
198
      gimp_edit_paste_as_new (gdisp->gimage->gimp,
			      gdisp->gimage,
			      gdisp->gimage->gimp->global_buffer);
Michael Natterer's avatar
Michael Natterer committed
199
200
201
202
203
204
205
    }
}

void
edit_named_cut_cmd_callback (GtkWidget *widget,
			     gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
206
207
208
  GimpDisplay *gdisp;
  GtkWidget   *qbox;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
209
210

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

void
edit_named_copy_cmd_callback (GtkWidget *widget,
			      gpointer   data)
{
225
226
227
  GimpDisplay *gdisp;
  GtkWidget   *qbox;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
228
229

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

void
edit_named_paste_cmd_callback (GtkWidget *widget,
			       gpointer   data)
{
244
245
  gimp_dialog_factory_dialog_raise (global_dock_factory,
                                    gtk_widget_get_screen (widget),
246
                                    "gimp-buffer-list|gimp-buffer-grid", -1);
Michael Natterer's avatar
Michael Natterer committed
247
248
249
250
251
252
}

void
edit_clear_cmd_callback (GtkWidget *widget,
			 gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
253
254
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
255

Michael Natterer's avatar
Michael Natterer committed
256
  gimp_edit_clear (gimage, gimp_image_active_drawable (gimage));
257
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
258
259
260
261
262
263
264
}

void
edit_fill_cmd_callback (GtkWidget *widget,
			gpointer   data,
			guint      action)
{
265
266
  GimpImage    *gimage;
  GimpFillType  fill_type;
Michael Natterer's avatar
Michael Natterer committed
267
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
268

269
  fill_type = (GimpFillType) action;
Michael Natterer's avatar
Michael Natterer committed
270

271
  gimp_edit_fill (gimage, gimp_image_active_drawable (gimage), fill_type);
272
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
273
274
275
276
277
}

void
edit_stroke_cmd_callback (GtkWidget *widget,
			  gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
278
279
280
281
{
  GimpImage *gimage;
  return_if_no_image (gimage, data);

282
  edit_stroke_selection (GIMP_ITEM (gimp_image_get_mask (gimage)), widget);
Michael Natterer's avatar
Michael Natterer committed
283
284
285
}

void
286
287
edit_stroke_selection (GimpItem  *item,
                       GtkWidget *parent)
Michael Natterer's avatar
Michael Natterer committed
288
{
289
290
  GimpImage    *gimage;
  GimpDrawable *active_drawable;
Michael Natterer's avatar
Michael Natterer committed
291
292
293
294
295
  GtkWidget    *dialog;

  g_return_if_fail (GIMP_IS_ITEM (item));

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

297
298
299
300
  active_drawable = gimp_image_active_drawable (gimage);

  if (! active_drawable)
    {
301
      g_message (_("There is no active layer or channel to stroke to."));
302
303
304
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
305
  dialog = stroke_dialog_new (item, GIMP_STOCK_SELECTION_STROKE,
306
307
                              GIMP_HELP_SELECTION_STROKE,
                              parent);
Michael Natterer's avatar
Michael Natterer committed
308
  gtk_widget_show (dialog);
Michael Natterer's avatar
Michael Natterer committed
309
310
311
312
313
314
}


/*  private functions  */

static void
Sven Neumann's avatar
Sven Neumann committed
315
316
317
cut_named_buffer_callback (GtkWidget   *widget,
			   const gchar *name,
			   gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
318
{
319
320
  const GimpBuffer *cut_buffer;
  GimpImage        *gimage;
Michael Natterer's avatar
Michael Natterer committed
321
322
323

  gimage = (GimpImage *) data;

324
325
326
327
  cut_buffer = gimp_edit_cut (gimage,
                              gimp_image_active_drawable (gimage));

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
328
    {
329
330
      GimpBuffer *new_buffer;

331
332
333
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

336
337
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
338
339
    }

340
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
341
342
343
}

static void
344
345
346
copy_named_buffer_callback (GtkWidget   *widget,
			    const gchar *name,
			    gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
347
{
348
349
  const GimpBuffer *copy_buffer;
  GimpImage        *gimage;
Michael Natterer's avatar
Michael Natterer committed
350
351

  gimage = (GimpImage *) data;
Michael Natterer's avatar
Michael Natterer committed
352

353
354
  copy_buffer = gimp_edit_copy (gimage,
                                gimp_image_active_drawable (gimage));
Michael Natterer's avatar
Michael Natterer committed
355

356
  if (copy_buffer)
Michael Natterer's avatar
Michael Natterer committed
357
    {
358
359
      GimpBuffer *new_buffer;

360
361
362
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

365
366
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
367
368
    }
}