edit-commands.c 7.78 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"
Michael Natterer's avatar
Michael Natterer committed
30
31
32
33
#include "core/gimpbuffer.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdrawable.h"
Michael Natterer's avatar
Michael Natterer committed
34
#include "core/gimpedit.h"
Michael Natterer's avatar
Michael Natterer committed
35
36
37
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"

38
39
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
40
41
#include "tools/tool_manager.h"

Michael Natterer's avatar
Michael Natterer committed
42
43
44
#include "widgets/gimpdialogfactory.h"

#include "dialogs.h"
Michael Natterer's avatar
Michael Natterer committed
45
46
47
48
49
50
51
#include "edit-commands.h"

#include "undo.h"

#include "libgimp/gimpintl.h"


Michael Natterer's avatar
Michael Natterer committed
52
#define return_if_no_display(gdisp,data) \
53
54
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))); \
  else \
    gdisp = NULL; \
Michael Natterer's avatar
Michael Natterer committed
59
60
61
62
  if (! gdisp) \
    return

#define return_if_no_image(gimage,data) \
63
64
65
66
67
68
  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
69
70
  if (! gimage) \
    return
Michael Natterer's avatar
Michael Natterer committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88


/*  local function prototypes  */

static void   cut_named_buffer_callback  (GtkWidget *widget,
					  gchar     *name,
					  gpointer   data);
static void   copy_named_buffer_callback (GtkWidget *widget,
					  gchar     *name,
					  gpointer   data);


/*  public functions  */

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

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

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

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

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

  /*  stop any active tool  */
115
  tool_manager_control_active (gdisp->gimage->gimp, HALT, gdisp);
Michael Natterer's avatar
Michael Natterer committed
116

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

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

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

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

144
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
145
146
    {
      /*  stop any active tool  */
147
      tool_manager_control_active (gdisp->gimage->gimp, HALT, gdisp);
Michael Natterer's avatar
Michael Natterer committed
148

Michael Natterer's avatar
Michael Natterer committed
149
150
      if (gimp_edit_paste (gdisp->gimage,
			   gimp_image_active_drawable (gdisp->gimage), 
151
			   gdisp->gimage->gimp->global_buffer,
Michael Natterer's avatar
Michael Natterer committed
152
			   FALSE))
Michael Natterer's avatar
Michael Natterer committed
153
	{
154
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
155
156
157
158
159
160
161
162
	}
    }
}

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

166
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
167
168
    {
      /*  stop any active tool  */
169
      tool_manager_control_active (gdisp->gimage->gimp, HALT, gdisp);
Michael Natterer's avatar
Michael Natterer committed
170

Michael Natterer's avatar
Michael Natterer committed
171
172
      if (gimp_edit_paste (gdisp->gimage,
			   gimp_image_active_drawable (gdisp->gimage), 
173
			   gdisp->gimage->gimp->global_buffer,
Michael Natterer's avatar
Michael Natterer committed
174
			   TRUE))
Michael Natterer's avatar
Michael Natterer committed
175
	{
176
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
177
178
179
180
181
182
183
184
	}
    }
}

void
edit_paste_as_new_cmd_callback (GtkWidget *widget,
				gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
185
186
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
187

188
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
189
190
    {
      /*  stop any active tool  */
191
      tool_manager_control_active (gdisp->gimage->gimp, HALT, gdisp);
Michael Natterer's avatar
Michael Natterer committed
192

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

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

  /*  stop any active tool  */
208
  tool_manager_control_active (gdisp->gimage->gimp, HALT, gdisp);
Michael Natterer's avatar
Michael Natterer committed
209
210
211
212
213
214

  qbox = gimp_query_string_box (_("Cut Named"),
				gimp_standard_help_func,
				"dialogs/cut_named.html",
				_("Enter a name for this buffer"),
				NULL,
215
				G_OBJECT (gdisp->gimage), "disconnect",
Michael Natterer's avatar
Michael Natterer committed
216
217
218
219
220
221
222
223
				cut_named_buffer_callback, gdisp->gimage);
  gtk_widget_show (qbox);
}

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

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

void
edit_named_paste_cmd_callback (GtkWidget *widget,
			       gpointer   data)
{
242
  gimp_dialog_factory_dialog_raise (global_dock_factory, "gimp-buffer-list", -1);
Michael Natterer's avatar
Michael Natterer committed
243
244
245
246
247
248
}

void
edit_clear_cmd_callback (GtkWidget *widget,
			 gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
249
250
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
251

Michael Natterer's avatar
Michael Natterer committed
252
  gimp_edit_clear (gimage, gimp_image_active_drawable (gimage));
253
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
254
255
256
257
258
259
260
}

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

  fill_type = (GimpFillType) action;

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

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

280
281
282
  gimp_image_mask_stroke (gimage,
                          gimp_image_active_drawable (gimage),
                          gimp_get_current_context (gimage->gimp));
283
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
284
285
286
287
288
289
290
291
292
293
}


/*  private functions  */

static void
cut_named_buffer_callback (GtkWidget *widget,
			   gchar     *name,
			   gpointer   data)
{
294
295
  GimpBuffer *cut_buffer;
  GimpImage  *gimage;
Michael Natterer's avatar
Michael Natterer committed
296
297
298

  gimage = (GimpImage *) data;

299
300
301
302
  cut_buffer = gimp_edit_cut (gimage,
                              gimp_image_active_drawable (gimage));

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
303
    {
304
305
      GimpBuffer *new_buffer;

306
307
308
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

311
312
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
313
314
    }

315
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
316
317
318
319
320
321
322
}

static void
copy_named_buffer_callback (GtkWidget *widget,
			    gchar     *name,
			    gpointer   data)
{
323
324
  GimpBuffer *copy_buffer;
  GimpImage  *gimage;
Michael Natterer's avatar
Michael Natterer committed
325
326

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

328
329
  copy_buffer = gimp_edit_copy (gimage,
                                gimp_image_active_drawable (gimage));
Michael Natterer's avatar
Michael Natterer committed
330

331
  if (copy_buffer)
Michael Natterer's avatar
Michael Natterer committed
332
    {
333
334
      GimpBuffer *new_buffer;

335
336
337
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

340
341
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
342
343
    }
}