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

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

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

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


/*  local function prototypes  */

55 56 57 58 59
static void   edit_paste                 (GimpDisplay *gdisp,
                                          gboolean     paste_into);
static void   edit_undo_clear_callback   (GtkWidget   *widget,
                                          gboolean     clear,
                                          gpointer     data);
60
static void   cut_named_buffer_callback  (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
61 62
                                          const gchar *name,
                                          gpointer     data);
63
static void   copy_named_buffer_callback (GtkWidget   *widget,
Michael Natterer's avatar
Michael Natterer committed
64 65
                                          const gchar *name,
                                          gpointer     data);
Michael Natterer's avatar
Michael Natterer committed
66 67 68 69 70


/*  public functions  */

void
71
edit_undo_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
72
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
73
{
Michael Natterer's avatar
Michael Natterer committed
74 75
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
76

77
  if (gimp_image_undo (gimage))
78
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
79 80 81
}

void
82
edit_redo_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
83
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
84
{
Michael Natterer's avatar
Michael Natterer committed
85 86
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
87

88
  if (gimp_image_redo (gimage))
89
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
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
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
115
void
116
edit_cut_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
117
                       gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
118
{
119 120 121
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
122

123
  if (gimp_edit_cut (gimage, drawable, action_data_get_context (data)))
124
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
125 126 127
}

void
128
edit_copy_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
129
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
130
{
131 132 133
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
134

135
  if (gimp_edit_copy (gimage, drawable, action_data_get_context (data)))
136
    gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
137 138
}

139
void
140
edit_paste_cmd_callback (GtkAction *action,
141 142 143 144 145 146 147 148 149
                         gpointer   data)
{
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);

  edit_paste (gdisp, FALSE);
}

void
150
edit_paste_into_cmd_callback (GtkAction *action,
151 152 153 154 155 156 157 158
                              gpointer   data)
{
  GimpDisplay *gdisp;
  return_if_no_display (gdisp, data);

  edit_paste (gdisp, TRUE);
}

Michael Natterer's avatar
Michael Natterer committed
159
void
160
edit_paste_as_new_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
161
                                gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
162
{
163 164
  Gimp       *gimp;
  GimpBuffer *buffer;
165
  return_if_no_gimp (gimp, data);
Michael Natterer's avatar
Michael Natterer committed
166

167
  buffer = gimp_clipboard_get_buffer (gimp);
168 169 170 171 172 173 174

  if (buffer)
    {
      gimp_edit_paste_as_new (gimp, action_data_get_image (data), buffer);

      g_object_unref (buffer);
    }
Michael Natterer's avatar
Michael Natterer committed
175 176 177
}

void
178
edit_named_cut_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
179
                             gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
180
{
181 182 183 184 185
  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
186

187
  qbox = gimp_query_string_box (_("Cut Named"), widget,
Michael Natterer's avatar
Michael Natterer committed
188 189 190 191
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_CUT,
                                _("Enter a name for this buffer"),
                                NULL,
192 193
                                G_OBJECT (gimage), "disconnect",
                                cut_named_buffer_callback, gimage);
Michael Natterer's avatar
Michael Natterer committed
194 195 196 197
  gtk_widget_show (qbox);
}

void
198
edit_named_copy_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
199
                              gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
200
{
201 202 203 204 205
  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
206

207
  qbox = gimp_query_string_box (_("Copy Named"), widget,
Michael Natterer's avatar
Michael Natterer committed
208 209 210 211
                                gimp_standard_help_func,
                                GIMP_HELP_BUFFER_COPY,
                                _("Enter a name for this buffer"),
                                NULL,
212 213
                                G_OBJECT (gimage), "disconnect",
                                copy_named_buffer_callback, gimage);
Michael Natterer's avatar
Michael Natterer committed
214 215 216 217
  gtk_widget_show (qbox);
}

void
218
edit_named_paste_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
219
                               gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
220
{
221 222
  GtkWidget *widget;
  return_if_no_widget (widget, data);
223

224
  gimp_dialog_factory_dialog_raise (global_dock_factory,
225
                                    gtk_widget_get_screen (widget),
226
                                    "gimp-buffer-list|gimp-buffer-grid", -1);
Michael Natterer's avatar
Michael Natterer committed
227 228 229
}

void
230
edit_clear_cmd_callback (GtkAction *action,
Michael Natterer's avatar
Michael Natterer committed
231
                         gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
232
{
233 234 235
  GimpImage    *gimage;
  GimpDrawable *drawable;
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
236

237
  gimp_edit_clear (gimage, drawable, action_data_get_context (data));
238
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
239 240 241
}

void
242 243 244
edit_fill_cmd_callback (GtkAction *action,
                        gint       value,
                        gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
245
{
246
  GimpImage    *gimage;
247
  GimpDrawable *drawable;
248
  GimpFillType  fill_type;
249
  return_if_no_drawable (gimage, drawable, data);
Michael Natterer's avatar
Michael Natterer committed
250

251
  fill_type = (GimpFillType) value;
Michael Natterer's avatar
Michael Natterer committed
252

253
  gimp_edit_fill (gimage, drawable, action_data_get_context (data),
254
                  fill_type);
255
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
256 257 258 259 260
}


/*  private functions  */

261 262 263 264
static void
edit_paste (GimpDisplay *gdisp,
            gboolean     paste_into)
{
265
  GimpBuffer *buffer = gimp_clipboard_get_buffer (gdisp->gimage->gimp);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

  if (buffer)
    {
      GimpDisplayShell *shell = GIMP_DISPLAY_SHELL (gdisp->shell);
      gint              x, y;
      gint              width, height;

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

      if (gimp_edit_paste (gdisp->gimage,
                           gimp_image_active_drawable (gdisp->gimage),
                           buffer, paste_into, x, y, width, height))
	{
          gimp_image_flush (gdisp->gimage);
	}

      g_object_unref (buffer);
    }
}

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);
    }
}

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

310
  drawable = gimp_image_active_drawable (gimage);
311

312
  if (! drawable)
313 314 315 316 317
    {
      g_message (_("There is no active layer or channel to cut from."));
      return;
    }

318
  cut_buffer = gimp_edit_cut (gimage, drawable,
319
                              gimp_get_user_context (gimage->gimp));
320 321

  if (cut_buffer)
Michael Natterer's avatar
Michael Natterer committed
322
    {
323 324
      GimpBuffer *new_buffer;

325 326 327
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

330 331
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
332
      g_object_unref (new_buffer);
Michael Natterer's avatar
Michael Natterer committed
333

334 335
      gimp_image_flush (gimage);
    }
Michael Natterer's avatar
Michael Natterer committed
336 337 338
}

static void
339 340 341
copy_named_buffer_callback (GtkWidget   *widget,
			    const gchar *name,
			    gpointer     data)
Michael Natterer's avatar
Michael Natterer committed
342
{
Michael Natterer's avatar
Michael Natterer committed
343
  GimpImage        *gimage = GIMP_IMAGE (data);
344
  const GimpBuffer *copy_buffer;
345
  GimpDrawable     *drawable;
346

347
  drawable = gimp_image_active_drawable (gimage);
348

349
  if (! drawable)
350 351 352 353
    {
      g_message (_("There is no active layer or channel to copy from."));
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
354

355
  copy_buffer = gimp_edit_copy (gimage, drawable,
356
                                gimp_get_user_context (gimage->gimp));
Michael Natterer's avatar
Michael Natterer committed
357

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

362 363 364
      if (! (name && strlen (name)))
        name = _("(Unnamed Buffer)");

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

367 368
      gimp_container_add (gimage->gimp->named_buffers,
                          GIMP_OBJECT (new_buffer));
Michael Natterer's avatar
Michael Natterer committed
369
      g_object_unref (new_buffer);
370 371

      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
372 373
    }
}