edit-commands.c 7.86 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"
34
#include "core/gimpdrawable-bucket-fill.h"
Michael Natterer's avatar
Michael Natterer committed
35
#include "core/gimpedit.h"
Michael Natterer's avatar
Michael Natterer committed
36
#include "core/gimpimage.h"
37
#include "core/gimpimage-undo.h"
38
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
39

40
41
#include "display/gimpdisplay.h"

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

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

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


Michael Natterer's avatar
Michael Natterer committed
51
#define return_if_no_display(gdisp,data) \
52
53
54
55
56
57
  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
58
59
60
61
  if (! gdisp) \
    return

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


/*  local function prototypes  */

74
75
76
77
78
79
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
80
81
82
83
84
85
86
87


/*  public functions  */

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

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

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

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

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

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

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

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

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

140
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
141
    {
Michael Natterer's avatar
Michael Natterer committed
142
143
      if (gimp_edit_paste (gdisp->gimage,
			   gimp_image_active_drawable (gdisp->gimage), 
144
			   gdisp->gimage->gimp->global_buffer,
Michael Natterer's avatar
Michael Natterer committed
145
			   FALSE))
Michael Natterer's avatar
Michael Natterer committed
146
	{
147
          gimp_image_flush (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
148
149
150
151
152
153
154
155
	}
    }
}

void
edit_paste_into_cmd_callback (GtkWidget *widget,
			      gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
156
157
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
158

159
  if (gdisp->gimage->gimp->global_buffer)
Michael Natterer's avatar
Michael Natterer committed
160
    {
Michael Natterer's avatar
Michael Natterer committed
161
162
      if (gimp_edit_paste (gdisp->gimage,
			   gimp_image_active_drawable (gdisp->gimage), 
163
			   gdisp->gimage->gimp->global_buffer,
Michael Natterer's avatar
Michael Natterer committed
164
			   TRUE))
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
172
173
174
	}
    }
}

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

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

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

  qbox = gimp_query_string_box (_("Cut Named"),
				gimp_standard_help_func,
196
				GIMP_HELP_BUFFER_CUT,
Michael Natterer's avatar
Michael Natterer committed
197
198
				_("Enter a name for this buffer"),
				NULL,
199
				G_OBJECT (gdisp->gimage), "disconnect",
Michael Natterer's avatar
Michael Natterer committed
200
201
202
203
204
205
206
207
				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
208
  GimpImage *gimage;
Michael Natterer's avatar
Michael Natterer committed
209
  GtkWidget *qbox;
Michael Natterer's avatar
Michael Natterer committed
210
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
211
212
213

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

void
edit_named_paste_cmd_callback (GtkWidget *widget,
			       gpointer   data)
{
226
  gimp_dialog_factory_dialog_raise (global_dock_factory, "gimp-buffer-list", -1);
Michael Natterer's avatar
Michael Natterer committed
227
228
229
230
231
232
}

void
edit_clear_cmd_callback (GtkWidget *widget,
			 gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
233
234
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
235

Michael Natterer's avatar
Michael Natterer committed
236
  gimp_edit_clear (gimage, gimp_image_active_drawable (gimage));
237
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
238
239
240
241
242
243
244
}

void
edit_fill_cmd_callback (GtkWidget *widget,
			gpointer   data,
			guint      action)
{
245
246
  GimpImage          *gimage;
  GimpBucketFillMode  fill_mode;
Michael Natterer's avatar
Michael Natterer committed
247
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
248

249
  fill_mode = (GimpBucketFillMode) action;
Michael Natterer's avatar
Michael Natterer committed
250

251
252
253
254
255
  gimp_drawable_bucket_fill (gimp_image_active_drawable (gimage),
                             fill_mode,
                             GIMP_NORMAL_MODE, 1.0,
                             FALSE /* no seed fill */,
                             FALSE, 0.0, FALSE, 0.0, 0.0);
256
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
257
258
259
260
261
262
}

void
edit_stroke_cmd_callback (GtkWidget *widget,
			  gpointer   data)
{
263
264
265
  GimpImage    *gimage;
  GimpDrawable *active_drawable;
  GimpToolInfo *tool_info;
Michael Natterer's avatar
Michael Natterer committed
266
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
267

268
269
270
271
272
273
274
275
276
277
  active_drawable = gimp_image_active_drawable (gimage);

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

  tool_info = gimp_context_get_tool (gimp_get_current_context (gimage->gimp));

278
279
  gimp_item_stroke (GIMP_ITEM (gimp_image_get_mask (gimage)),
                    active_drawable, tool_info->paint_info);
280
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
281
282
283
284
285
286
}


/*  private functions  */

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

  gimage = (GimpImage *) data;

296
297
298
299
  cut_buffer = gimp_edit_cut (gimage,
                              gimp_image_active_drawable (gimage));

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
300
    {
301
302
      GimpBuffer *new_buffer;

303
304
305
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

308
309
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
310
311
    }

312
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
313
314
315
}

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

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

325
326
  copy_buffer = gimp_edit_copy (gimage,
                                gimp_image_active_drawable (gimage));
Michael Natterer's avatar
Michael Natterer committed
327

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

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

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

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