layers-commands.c 31.4 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 21 22 23 24 25
/* 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"

#include <string.h> 

#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
26
#include "libgimpbase/gimpbase.h"
Michael Natterer's avatar
Michael Natterer committed
27 28
#include "libgimpwidgets/gimpwidgets.h"

29
#include "gui-types.h"
Michael Natterer's avatar
Michael Natterer committed
30

Michael Natterer's avatar
Michael Natterer committed
31
#include "core/gimp.h"
32
#include "core/gimpcontext.h"
33 34
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"
35
#include "core/gimpimage-merge.h"
36
#include "core/gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
37
#include "core/gimplayer-floating-sel.h"
38 39 40
#include "core/gimplayermask.h"
#include "core/gimplist.h"

41 42
#include "pdb/procedural_db.h"

43
#include "widgets/gimpenummenu.h"
44
#include "widgets/gimpitemlistview.h"
45
#include "widgets/gimpviewabledialog.h"
Michael Natterer's avatar
Michael Natterer committed
46

47 48
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
49
#include "layers-commands.h"
Michael Natterer's avatar
Michael Natterer committed
50
#include "resize-dialog.h"
Michael Natterer's avatar
Michael Natterer committed
51 52 53 54 55 56

#include "undo.h"

#include "libgimp/gimpintl.h"


57 58
/*  local function prototypes  */

59 60 61 62 63
static void   layers_add_mask_query       (GimpLayer *layer);
static void   layers_scale_layer_query    (GimpImage *gimage,
					   GimpLayer *layer);
static void   layers_resize_layer_query   (GimpImage *gimage,
					   GimpLayer *layer);
Michael Natterer's avatar
Michael Natterer committed
64 65


66
#define return_if_no_image(gimage,data) \
67 68 69 70 71 72 73 74 75
  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 if (GIMP_IS_ITEM_LIST_VIEW (data)) \
    gimage = ((GimpItemListView *) data)->gimage; \
  else \
    gimage = NULL; \
  \
76 77 78
  if (! gimage) \
    return

79 80
#define return_if_no_layer(gimage,layer,data) \
  return_if_no_image (gimage,data); \
81 82 83 84
  layer = gimp_image_get_active_layer (gimage); \
  if (! layer) \
    return

85

86 87
/*  public functions  */

Michael Natterer's avatar
Michael Natterer committed
88 89 90 91 92 93 94
void
layers_previous_cmd_callback (GtkWidget *widget,
			      gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *new_layer;
  gint       current_layer;
95
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
96 97 98 99 100 101 102 103 104 105 106 107

  current_layer =
    gimp_image_get_layer_index (gimage, gimp_image_get_active_layer (gimage));

  if (current_layer > 0)
    {
      new_layer = (GimpLayer *)
	gimp_container_get_child_by_index (gimage->layers, current_layer - 1);

      if (new_layer)
	{
	  gimp_image_set_active_layer (gimage, new_layer);
108
	  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
109 110 111 112 113 114 115 116 117 118 119
	}
    }
}

void
layers_next_cmd_callback (GtkWidget *widget,
			  gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *new_layer;
  gint       current_layer;
120
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
121 122 123 124 125 126 127 128 129 130 131

  current_layer =
    gimp_image_get_layer_index (gimage, gimp_image_get_active_layer (gimage));

  new_layer = 
    GIMP_LAYER (gimp_container_get_child_by_index (gimage->layers, 
						   current_layer + 1));

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

void
layers_raise_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
141
  GimpLayer *active_layer;
142
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
143

144
  gimp_image_raise_layer (gimage, active_layer);
145
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
146 147 148 149 150 151 152
}

void
layers_lower_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
153
  GimpLayer *active_layer;
154
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
155

156
  gimp_image_lower_layer (gimage, active_layer);
157
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
158 159 160 161 162 163 164
}

void
layers_raise_to_top_cmd_callback (GtkWidget *widget,
				  gpointer   data)
{
  GimpImage *gimage;
165
  GimpLayer *active_layer;
166
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
167

168
  gimp_image_raise_layer_to_top (gimage, active_layer);
169
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
170 171 172 173 174 175 176
}

void
layers_lower_to_bottom_cmd_callback (GtkWidget *widget,
				     gpointer   data)
{
  GimpImage *gimage;
177
  GimpLayer *active_layer;
178
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
179

180
  gimp_image_lower_layer_to_bottom (gimage, active_layer);
181
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
182 183 184 185 186 187 188
}

void
layers_new_cmd_callback (GtkWidget *widget,
			 gpointer   data)
{
  GimpImage *gimage;
189
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
190

Michael Natterer's avatar
Michael Natterer committed
191
  layers_new_layer_query (gimage, NULL, TRUE);
Michael Natterer's avatar
Michael Natterer committed
192 193 194 195 196 197 198 199 200
}

void
layers_duplicate_cmd_callback (GtkWidget *widget,
			       gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *active_layer;
  GimpLayer *new_layer;
201
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
202

203 204 205
  new_layer = gimp_layer_copy (active_layer,
                               G_TYPE_FROM_INSTANCE (active_layer),
                               TRUE);
Michael Natterer's avatar
Michael Natterer committed
206 207
  gimp_image_add_layer (gimage, new_layer, -1);

208
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
209 210 211
}

void
212
layers_anchor_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
213 214 215
			    gpointer   data)
{
  GimpImage *gimage;
216
  GimpLayer *active_layer;
217
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
218

219
  layers_anchor_layer (active_layer);
220
}
Michael Natterer's avatar
Michael Natterer committed
221

222 223 224 225 226
void
layers_merge_down_cmd_callback (GtkWidget *widget,
				gpointer   data)
{
  GimpImage *gimage;
227
  GimpLayer *active_layer;
228
  return_if_no_layer (gimage, active_layer, data);
229

230
  gimp_image_merge_down (gimage, active_layer, GIMP_EXPAND_AS_NECESSARY);
231
  gimp_image_flush (gimage);
232 233
}

234 235 236 237 238
void
layers_delete_cmd_callback (GtkWidget *widget,
			    gpointer   data)
{
  GimpImage *gimage;
239
  GimpLayer *active_layer;
240
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
241

242 243
  if (gimp_layer_is_floating_sel (active_layer))
    floating_sel_remove (active_layer);
Michael Natterer's avatar
Michael Natterer committed
244
  else
245
    gimp_image_remove_layer (gimage, active_layer);
246

247
  gimp_image_flush (gimage);
248 249 250
}

void
251 252
layers_resize_cmd_callback (GtkWidget *widget,
			    gpointer   data)
253 254
{
  GimpImage *gimage;
255
  GimpLayer *active_layer;
256
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
257

258
  layers_resize_layer_query (gimage, active_layer);
Michael Natterer's avatar
Michael Natterer committed
259 260 261
}

void
262 263
layers_resize_to_image_cmd_callback (GtkWidget *widget,
				     gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
264 265
{
  GimpImage *gimage;
266
  GimpLayer *active_layer;
267
  return_if_no_layer (gimage, active_layer, data);
268 269
  
  gimp_layer_resize_to_image (active_layer);
270
  gimp_image_flush (gimage);
271
}
Michael Natterer's avatar
Michael Natterer committed
272

273 274 275 276 277 278
void
layers_scale_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *active_layer;
279
  return_if_no_layer (gimage, active_layer, data);
280 281

  layers_scale_layer_query (gimage, active_layer);
Michael Natterer's avatar
Michael Natterer committed
282 283 284
}

void
285 286
layers_crop_cmd_callback (GtkWidget *widget,
                          gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
287 288
{
  GimpImage *gimage;
289
  GimpLayer *active_layer;
290 291
  gint       x1, y1, x2, y2;
  gint       off_x, off_y;
292
  return_if_no_layer (gimage, active_layer, data);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

  if (! gimp_image_mask_bounds (gimage, &x1, &y1, &x2, &y2))
    {
      g_message (_("Cannot crop because the current selection is empty."));
      return;
    }

  gimp_drawable_offsets (GIMP_DRAWABLE (active_layer), &off_x, &off_y);

  off_x -= x1;
  off_y -= y1;

  undo_push_group_start (gimage, LAYER_RESIZE_UNDO_GROUP);

  if (gimp_layer_is_floating_sel (active_layer))
    floating_sel_relax (active_layer, TRUE);

  gimp_layer_resize (active_layer, x2 - x1, y2 - y1, off_x, off_y);

  if (gimp_layer_is_floating_sel (active_layer))
    floating_sel_rigor (active_layer, TRUE);

  undo_push_group_end (gimage);

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

void
layers_add_layer_mask_cmd_callback (GtkWidget *widget,
				    gpointer   data)
{
  GimpImage *gimage;
325
  GimpLayer *active_layer;
326
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
327

328
  layers_add_mask_query (active_layer);
Michael Natterer's avatar
Michael Natterer committed
329 330 331 332 333 334 335
}

void
layers_apply_layer_mask_cmd_callback (GtkWidget *widget,
				      gpointer   data)
{
  GimpImage *gimage;
336
  GimpLayer *active_layer;
337
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
338

339
  if (gimp_layer_get_mask (active_layer))
Michael Natterer's avatar
Michael Natterer committed
340
    {
341 342 343
      gboolean flush;

      flush = ! active_layer->mask->apply_mask || active_layer->mask->show_mask;
Michael Natterer's avatar
Michael Natterer committed
344

345
      gimp_layer_apply_mask (active_layer, GIMP_MASK_APPLY, TRUE);
Michael Natterer's avatar
Michael Natterer committed
346 347

      if (flush)
348
        gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
349 350 351 352 353 354 355 356
    }
}

void
layers_delete_layer_mask_cmd_callback (GtkWidget *widget,
				       gpointer   data)
{
  GimpImage *gimage;
357
  GimpLayer *active_layer;
358
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
359

360
  if (gimp_layer_get_mask (active_layer))
Michael Natterer's avatar
Michael Natterer committed
361
    {
362
      gboolean flush;
Michael Natterer's avatar
Michael Natterer committed
363

364 365
      flush = active_layer->mask->apply_mask || active_layer->mask->show_mask;

366
      gimp_layer_apply_mask (active_layer, GIMP_MASK_DISCARD, TRUE);
Michael Natterer's avatar
Michael Natterer committed
367 368

      if (flush)
369
        gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
370 371 372 373
    }
}

void
374 375
layers_mask_select_cmd_callback (GtkWidget *widget,
				 gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
376 377
{
  GimpImage *gimage;
378
  GimpLayer *active_layer;
379
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
380

381 382 383
  if (gimp_layer_get_mask (active_layer))
    {
      gimp_image_mask_layer_mask (gimage, active_layer);
384
      gimp_image_flush (gimage);
385
    }
Michael Natterer's avatar
Michael Natterer committed
386 387 388
}

void
389 390
layers_alpha_select_cmd_callback (GtkWidget *widget,
				  gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
391 392
{
  GimpImage *gimage;
393
  GimpLayer *active_layer;
394
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
395

396
  gimp_image_mask_layer_alpha (gimage, active_layer);
397
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
398 399 400 401 402 403 404
}

void
layers_add_alpha_channel_cmd_callback (GtkWidget *widget,
				       gpointer   data)
{
  GimpImage *gimage;
405
  GimpLayer *active_layer;
406
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
407

408 409 410
  if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (active_layer)))
    {
      gimp_layer_add_alpha (active_layer);
411
      gimp_image_flush (gimage);
412
    }
Michael Natterer's avatar
Michael Natterer committed
413 414 415 416 417 418 419
}

void
layers_edit_attributes_cmd_callback (GtkWidget *widget,
				     gpointer   data)
{
  GimpImage *gimage;
420
  GimpLayer *active_layer;
421
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
422

423
  layers_edit_layer_query (active_layer);
Michael Natterer's avatar
Michael Natterer committed
424 425
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
void
layers_remove_layer (GimpImage *gimage,
                     GimpLayer *layer)
{
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
  g_return_if_fail (GIMP_IS_LAYER (layer));

  if (gimp_layer_is_floating_sel (layer))
    floating_sel_remove (layer);
  else
    gimp_image_remove_layer (gimage, layer);
}

void
layers_anchor_layer (GimpLayer *layer)
{
  g_return_if_fail (GIMP_IS_LAYER (layer));

  if (gimp_layer_is_floating_sel (layer))
    {
      floating_sel_anchor (layer);
447
      gimp_image_flush (gimp_item_get_image (GIMP_ITEM (layer)));
448 449 450
    }
}

Michael Natterer's avatar
Michael Natterer committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

/********************************/
/*  The new layer query dialog  */
/********************************/

typedef struct _NewLayerOptions NewLayerOptions;

struct _NewLayerOptions
{
  GtkWidget    *query_box;
  GtkWidget    *name_entry;
  GtkWidget    *size_se;

  GimpFillType  fill_type;
  gint          xsize;
  gint          ysize;

  GimpImage    *gimage;
};

471
static GimpFillType  fill_type  = GIMP_TRANSPARENT_FILL;
Michael Natterer's avatar
Michael Natterer committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
static gchar        *layer_name = NULL;

static void
new_layer_query_ok_callback (GtkWidget *widget,
			     gpointer   data)
{
  NewLayerOptions *options;
  GimpLayer       *layer;
  GimpImage       *gimage;

  options = (NewLayerOptions *) data;

  if (layer_name)
    g_free (layer_name);
  layer_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (options->name_entry)));

  options->xsize = 
    RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (options->size_se), 0));
  options->ysize =
    RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (options->size_se), 1));

  fill_type = options->fill_type;

  if ((gimage = options->gimage))
    {
497 498 499
      layer = gimp_layer_new (gimage,
                              options->xsize,
                              options->ysize,
Michael Natterer's avatar
Michael Natterer committed
500
			      gimp_image_base_type_with_alpha (gimage),
501 502
			      layer_name,
                              GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
Michael Natterer's avatar
Michael Natterer committed
503 504
      if (layer) 
	{
Michael Natterer's avatar
Michael Natterer committed
505 506 507
	  gimp_drawable_fill_by_type (GIMP_DRAWABLE (layer),
				      gimp_get_user_context (gimage->gimp),
				      fill_type);
Michael Natterer's avatar
Michael Natterer committed
508 509
	  gimp_image_add_layer (gimage, layer, -1);
	  
510
          gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
511 512 513 514 515 516 517 518 519 520 521 522
	} 
      else 
	{
	  g_message ("new_layer_query_ok_callback():\n"
		     "could not allocate new layer");
	}
    }

  gtk_widget_destroy (options->query_box);
}

void
523
layers_new_layer_query (GimpImage *gimage,
Michael Natterer's avatar
Michael Natterer committed
524 525
                        GimpLayer *template,
                        gboolean   interactive)
Michael Natterer's avatar
Michael Natterer committed
526 527
{
  NewLayerOptions *options;
528
  GimpLayer       *floating_sel;
Michael Natterer's avatar
Michael Natterer committed
529 530 531 532 533 534
  GtkWidget       *vbox;
  GtkWidget       *table;
  GtkWidget       *label;
  GtkObject       *adjustment;
  GtkWidget       *spinbutton;
  GtkWidget       *frame;
535
  GtkWidget       *button;
Michael Natterer's avatar
Michael Natterer committed
536

537
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
538
  g_return_if_fail (! template || GIMP_IS_LAYER (template));
539 540 541 542 543 544 545

  /*  If there is a floating selection, the new command transforms
   *  the current fs into a new layer
   */
  if ((floating_sel = gimp_image_floating_sel (gimage)))
    {
      floating_sel_to_layer (floating_sel);
546
      gimp_image_flush (gimage);
547 548 549
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
550
  if (template || ! interactive)
551
    {
Michael Natterer's avatar
Michael Natterer committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
      GimpLayer            *new_layer;
      gint                  width, height;
      gint                  off_x, off_y;
      gdouble               opacity;
      GimpLayerModeEffects  mode;

      if (template)
        {
          width  = gimp_drawable_width  (GIMP_DRAWABLE (template));
          height = gimp_drawable_height (GIMP_DRAWABLE (template));
          gimp_drawable_offsets (GIMP_DRAWABLE (template), &off_x, &off_y);
          opacity = template->opacity;
          mode    = template->mode;
        }
      else
        {
          width   = gimp_image_get_width (gimage);
          height  = gimp_image_get_height (gimage);
          off_x   = 0;
          off_y   = 0;
          opacity = 1.0;
          mode    = GIMP_NORMAL_MODE;
        }
575

Michael Natterer's avatar
Michael Natterer committed
576
      undo_push_group_start (gimage, EDIT_PASTE_UNDO_GROUP);
577

578 579 580
      new_layer = gimp_layer_new (gimage,
                                  width,
                                  height,
581 582
                                  gimp_image_base_type_with_alpha (gimage),
                                  _("Empty Layer Copy"),
Michael Natterer's avatar
Michael Natterer committed
583 584 585 586 587 588 589 590 591 592
                                  opacity,
                                  mode);

      if (template)
        {
          gimp_drawable_fill_by_type (GIMP_DRAWABLE (new_layer),
                                      gimp_get_user_context (gimage->gimp),
                                      GIMP_TRANSPARENT_FILL);
          gimp_layer_translate (new_layer, off_x, off_y);
        }
593 594 595 596 597 598 599

      gimp_image_add_layer (gimage, new_layer, -1);

      undo_push_group_end (gimage);
      return;
    }

600 601
  options = g_new0 (NewLayerOptions, 1);

Michael Natterer's avatar
Michael Natterer committed
602 603 604 605 606
  options->fill_type = fill_type;
  options->gimage    = gimage;

  /*  The dialog  */
  options->query_box =
607 608 609
    gimp_viewable_dialog_new (GIMP_VIEWABLE (gimage),
                              _("New Layer"), "new_layer_options",
                              GTK_STOCK_NEW,
610
                              _("Create a New Layer"),
611 612
                              gimp_standard_help_func,
                              "dialogs/layers/new_layer.html",
Michael Natterer's avatar
Michael Natterer committed
613

614 615
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
616

617 618
                              GTK_STOCK_OK, new_layer_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
619

620
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
621

622 623 624
  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);
Michael Natterer's avatar
Michael Natterer committed
625 626

  /*  The main vbox  */
627
  vbox = gtk_vbox_new (FALSE, 4);
Michael Natterer's avatar
Michael Natterer committed
628 629 630 631 632 633 634 635 636 637 638 639 640
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     vbox);

  table = gtk_table_new (3, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 4);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  The name label and entry  */
  options->name_entry = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (options->name_entry),
		      (layer_name ? layer_name : _("New Layer")));
641 642 643
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("Layer _Name:"), 1.0, 0.5,
                             options->name_entry, 1, FALSE);
Michael Natterer's avatar
Michael Natterer committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

  /*  The size labels  */
  label = gtk_label_new (_("Layer Width:"));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  label = gtk_label_new (_("Height:"));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  /*  The size sizeentry  */
659 660 661 662 663
  spinbutton = gimp_spin_button_new (&adjustment,
                                     1, 1, 1, 1, 10, 1,
                                     1, 2);
  gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 10);

Michael Natterer's avatar
Michael Natterer committed
664
  options->size_se = gimp_size_entry_new (1, gimage->unit, "%a",
665
					  TRUE, TRUE, FALSE, 10,
Michael Natterer's avatar
Michael Natterer committed
666
					  GIMP_SIZE_ENTRY_UPDATE_SIZE);
667 668 669
  gtk_table_set_col_spacing (GTK_TABLE (options->size_se), 1, 4);
  gtk_table_set_row_spacing (GTK_TABLE (options->size_se), 0, 2);

Michael Natterer's avatar
Michael Natterer committed
670 671 672 673 674
  gimp_size_entry_add_field (GIMP_SIZE_ENTRY (options->size_se),
                             GTK_SPIN_BUTTON (spinbutton), NULL);
  gtk_table_attach_defaults (GTK_TABLE (options->size_se), spinbutton,
                             1, 2, 0, 1);
  gtk_widget_show (spinbutton);
675

Michael Natterer's avatar
Michael Natterer committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  gtk_table_attach (GTK_TABLE (table), options->size_se, 1, 2, 1, 3,
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (options->size_se);

  gimp_size_entry_set_unit (GIMP_SIZE_ENTRY (options->size_se), 
			    GIMP_UNIT_PIXEL);

  gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (options->size_se), 0,
                                  gimage->xresolution, FALSE);
  gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (options->size_se), 1,
                                  gimage->yresolution, FALSE);

  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (options->size_se), 0,
                                         GIMP_MIN_IMAGE_SIZE,
                                         GIMP_MAX_IMAGE_SIZE);
  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (options->size_se), 1,
                                         GIMP_MIN_IMAGE_SIZE,
                                         GIMP_MAX_IMAGE_SIZE);

  gimp_size_entry_set_size (GIMP_SIZE_ENTRY (options->size_se), 0,
			    0, gimage->width);
  gimp_size_entry_set_size (GIMP_SIZE_ENTRY (options->size_se), 1,
			    0, gimage->height);

  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (options->size_se), 0,
			      gimage->width);
  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (options->size_se), 1,
			      gimage->height);

  gtk_widget_show (table);

  /*  The radio frame  */
708 709 710 711 712 713 714 715
  frame = gimp_enum_radio_frame_new_with_range (GIMP_TYPE_FILL_TYPE,
                                                GIMP_FOREGROUND_FILL,
                                                GIMP_TRANSPARENT_FILL,
                                                gtk_label_new (_("Layer Fill Type")),
                                                2,
                                                G_CALLBACK (gimp_radio_button_update),
                                                &options->fill_type,
                                                &button);
716 717
  gimp_radio_group_set_active (GTK_RADIO_BUTTON (button),
                               GINT_TO_POINTER (options->fill_type));  
Michael Natterer's avatar
Michael Natterer committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  gtk_widget_show (vbox);
  gtk_widget_show (options->query_box);
}

/**************************************/
/*  The edit layer attributes dialog  */
/**************************************/

typedef struct _EditLayerOptions EditLayerOptions;

struct _EditLayerOptions
{
  GtkWidget *query_box;
  GtkWidget *name_entry;
  GimpLayer *layer;
  GimpImage *gimage;
};

static void
edit_layer_query_ok_callback (GtkWidget *widget,
			      gpointer   data)
{
  EditLayerOptions *options;
  GimpLayer        *layer;

  options = (EditLayerOptions *) data;

  if ((layer = options->layer))
    {
751 752 753 754 755
      const gchar *new_name;

      new_name = gtk_entry_get_text (GTK_ENTRY (options->name_entry));

      if (strcmp (new_name, gimp_object_get_name (GIMP_OBJECT (layer))))
Michael Natterer's avatar
Michael Natterer committed
756
        {
757
          undo_push_group_start (options->gimage, LAYER_PROPERTIES_UNDO_GROUP);
Michael Natterer's avatar
Michael Natterer committed
758

759 760 761 762 763 764 765 766 767 768 769 770 771
          if (gimp_layer_is_floating_sel (layer))
            {
              /*  If the layer is a floating selection, make it a layer  */

              floating_sel_to_layer (layer);
            }

          undo_push_item_rename (options->gimage, GIMP_ITEM (layer));

          gimp_object_set_name (GIMP_OBJECT (layer), new_name);

          undo_push_group_end (options->gimage);
        }
Michael Natterer's avatar
Michael Natterer committed
772 773
    }

774
  gimp_image_flush (options->gimage);
Michael Natterer's avatar
Michael Natterer committed
775 776 777 778 779 780 781 782 783

  gtk_widget_destroy (options->query_box); 
}

void
layers_edit_layer_query (GimpLayer *layer)
{
  EditLayerOptions *options;
  GtkWidget        *vbox;
784
  GtkWidget        *table;
Michael Natterer's avatar
Michael Natterer committed
785

786 787
  options = g_new0 (EditLayerOptions, 1);

Michael Natterer's avatar
Michael Natterer committed
788
  options->layer  = layer;
789
  options->gimage = gimp_item_get_image (GIMP_ITEM (layer));
Michael Natterer's avatar
Michael Natterer committed
790 791 792

  /*  The dialog  */
  options->query_box =
793 794 795 796 797 798 799
    gimp_viewable_dialog_new (GIMP_VIEWABLE (layer),
                              _("Layer Attributes"),
                              "edit_layer_attributes",
                              GIMP_STOCK_EDIT,
                              _("Edit Layer Attributes"),
                              gimp_standard_help_func,
                              "dialogs/layers/edit_layer_attributes.html",
Michael Natterer's avatar
Michael Natterer committed
800

801 802
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
803

804 805
                              GTK_STOCK_OK, edit_layer_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
806

807
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
808

809 810 811
  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);
Michael Natterer's avatar
Michael Natterer committed
812 813

  /*  The main vbox  */
814
  vbox = gtk_vbox_new (FALSE, 4);
Michael Natterer's avatar
Michael Natterer committed
815 816 817 818
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     vbox);

819 820 821 822 823
  /*  The name label and entry  */
  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  gtk_widget_show (table);
Michael Natterer's avatar
Michael Natterer committed
824 825 826 827 828 829

  options->name_entry = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (options->name_entry),
		      ((gimp_layer_is_floating_sel (layer) ?
			_("Floating Selection") :
			gimp_object_get_name (GIMP_OBJECT (layer)))));
830 831 832
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("Layer _Name"), 1.0, 0.5,
                             options->name_entry, 1, FALSE);
Michael Natterer's avatar
Michael Natterer committed
833

834
  g_signal_connect (options->name_entry, "activate",
835 836 837
		    G_CALLBACK (edit_layer_query_ok_callback),
		    options);

Michael Natterer's avatar
Michael Natterer committed
838 839 840 841 842 843 844 845 846 847 848 849
  gtk_widget_show (vbox);
  gtk_widget_show (options->query_box);
}

/*******************************/
/*  The add mask query dialog  */
/*******************************/

typedef struct _AddMaskOptions AddMaskOptions;

struct _AddMaskOptions
{
850 851 852
  GtkWidget       *query_box;
  GimpLayer       *layer;
  GimpAddMaskType  add_mask_type;
Michael Natterer's avatar
Michael Natterer committed
853 854 855 856 857 858 859 860 861 862 863 864 865
};

static void
add_mask_query_ok_callback (GtkWidget *widget,
			    gpointer   data)
{
  AddMaskOptions *options;
  GimpImage      *gimage;
  GimpLayerMask  *mask;
  GimpLayer      *layer;

  options = (AddMaskOptions *) data;

866
  if ((layer = (options->layer)) && (gimage = GIMP_ITEM (layer)->gimage))
Michael Natterer's avatar
Michael Natterer committed
867 868 869
    {
      mask = gimp_layer_create_mask (layer, options->add_mask_type);
      gimp_layer_add_mask (layer, mask, TRUE);
870
      g_object_unref (mask);
871

872
      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    }

  gtk_widget_destroy (options->query_box);
}

static void
layers_add_mask_query (GimpLayer *layer)
{
  AddMaskOptions *options;
  GtkWidget      *frame;
  GimpImage      *gimage;
  
  /*  The new options structure  */
  options = g_new (AddMaskOptions, 1);
  options->layer         = layer;
888
  options->add_mask_type = GIMP_ADD_WHITE_MASK;
Michael Natterer's avatar
Michael Natterer committed
889

890
  gimage = gimp_item_get_image (GIMP_ITEM (layer));
Michael Natterer's avatar
Michael Natterer committed
891 892 893
  
  /*  The dialog  */
  options->query_box =
894
    gimp_viewable_dialog_new (GIMP_VIEWABLE (layer),
895
                              _("Add Layer Mask"), "add_mask_options",
896
                              GTK_STOCK_ADD,
897
                              _("Add a Mask to the Layer"),
898 899
                              gimp_standard_help_func,
                              "dialogs/layers/add_layer_mask.html",
Michael Natterer's avatar
Michael Natterer committed
900

901 902
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
903

904 905
                              GTK_STOCK_OK, add_mask_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
906

907
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
908

909 910 911
  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);
Michael Natterer's avatar
Michael Natterer committed
912 913

  /*  The radio frame and box  */
914
  if (! gimp_image_mask_is_empty (gimage))
Michael Natterer's avatar
Michael Natterer committed
915
    {
916
      options->add_mask_type = GIMP_ADD_SELECTION_MASK;
Michael Natterer's avatar
Michael Natterer committed
917

918 919 920 921 922 923 924
      frame =
        gimp_radio_group_new2 (TRUE, _("Initialize Layer Mask to:"),
                               G_CALLBACK (gimp_radio_button_update),
                               &options->add_mask_type,
                               GINT_TO_POINTER (options->add_mask_type),

                               _("Selection"),
925
                               GINT_TO_POINTER (GIMP_ADD_SELECTION_MASK), NULL,
926
                               _("Inverse Selection"),
927
                               GINT_TO_POINTER (GIMP_ADD_INVERSE_SELECTION_MASK), NULL,
928 929

                               _("Grayscale Copy of Layer"),
930
                               GINT_TO_POINTER (GIMP_ADD_COPY_MASK), NULL,
931
                               _("Inverse Grayscale Copy of Layer"),
932
                               GINT_TO_POINTER (GIMP_ADD_INVERSE_COPY_MASK), NULL,
933 934

                               _("White (Full Opacity)"),
935
                               GINT_TO_POINTER (GIMP_ADD_WHITE_MASK), NULL,
936
                               _("Black (Full Transparency)"),
937
                               GINT_TO_POINTER (GIMP_ADD_BLACK_MASK), NULL,
938
                               _("Layer's Alpha Channel"),
939
                               GINT_TO_POINTER (GIMP_ADD_ALPHA_MASK), NULL,
940 941

                               NULL);
Michael Natterer's avatar
Michael Natterer committed
942 943 944
    }
  else
    {
945 946 947 948 949 950 951
      frame =
        gimp_radio_group_new2 (TRUE, _("Initialize Layer Mask to:"),
                               G_CALLBACK (gimp_radio_button_update),
                               &options->add_mask_type,
                               GINT_TO_POINTER (options->add_mask_type),

                               _("Grayscale Copy of Layer"),
952
                               GINT_TO_POINTER (GIMP_ADD_COPY_MASK), NULL,
953
                               _("Inverse Grayscale Copy of Layer"),
954
                               GINT_TO_POINTER (GIMP_ADD_INVERSE_COPY_MASK), NULL,
955 956

                               _("White (Full Opacity)"),
957
                               GINT_TO_POINTER (GIMP_ADD_WHITE_MASK), NULL,
958
                               _("Black (Full Transparency)"),
959
                               GINT_TO_POINTER (GIMP_ADD_BLACK_MASK), NULL,
960
                               _("Layer's Alpha Channel"),
961
                               GINT_TO_POINTER (GIMP_ADD_ALPHA_MASK), NULL,
962 963

                               NULL);
Michael Natterer's avatar
Michael Natterer committed
964
    }
965

Michael Natterer's avatar
Michael Natterer committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
  gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     frame);
  gtk_widget_show (frame);

  gtk_widget_show (options->query_box);
}


/****************************/
/*  The scale layer dialog  */
/****************************/

typedef struct _ScaleLayerOptions ScaleLayerOptions;

struct _ScaleLayerOptions
{
  GimpLayer *layer;
  Resize    *resize;
};

static void
scale_layer_query_ok_callback (GtkWidget *widget,
			       gpointer   data)
{
  ScaleLayerOptions *options;
  GimpLayer         *layer;

  options = (ScaleLayerOptions *) data;

  if (options->resize->width > 0 && options->resize->height > 0 &&
      (layer =  (options->layer)))
    {
999 1000
      GimpImage *gimage;

Michael Natterer's avatar
Michael Natterer committed
1001 1002
      gtk_widget_set_sensitive (options->resize->resize_shell, FALSE);

1003 1004 1005
      gimage = gimp_item_get_image (GIMP_ITEM (layer));

      if (gimage)
Michael Natterer's avatar
Michael Natterer committed
1006
	{
Michael Natterer's avatar
Michael Natterer committed
1007
	  undo_push_group_start (gimage, LAYER_SCALE_UNDO_GROUP);
Michael Natterer's avatar
Michael Natterer committed
1008 1009 1010 1011 1012

	  if (gimp_layer_is_floating_sel (layer))
	    floating_sel_relax (layer, TRUE);

	  gimp_layer_scale (layer, 
1013 1014 1015
			    options->resize->width,
                            options->resize->height,
                            options->resize->interpolation,
Michael Natterer's avatar
Michael Natterer committed
1016 1017 1018 1019 1020 1021 1022
			    TRUE);

	  if (gimp_layer_is_floating_sel (layer))
	    floating_sel_rigor (layer, TRUE);

	  undo_push_group_end (gimage);

1023
          gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	}

      gtk_widget_destroy (options->resize->resize_shell);
    }
  else
    {
      g_message (_("Invalid width or height.\n"
		   "Both must be positive."));
    }
}

static void
layers_scale_layer_query (GimpImage *gimage,
Michael Natterer's avatar
Michael Natterer committed
1037
                          GimpLayer *layer)