layers-commands.c 30.8 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/gimpimage-undo.h"
37
#include "core/gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
38
#include "core/gimplayer-floating-sel.h"
39 40 41
#include "core/gimplayermask.h"
#include "core/gimplist.h"

42 43
#include "pdb/procedural_db.h"

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

48 49
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
50
#include "layers-commands.h"
Michael Natterer's avatar
Michael Natterer committed
51
#include "resize-dialog.h"
Michael Natterer's avatar
Michael Natterer committed
52

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


56 57
/*  local function prototypes  */

58 59 60 61 62
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
63 64


65
#define return_if_no_image(gimage,data) \
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))); \
70 71
  else if (GIMP_IS_ITEM_TREE_VIEW (data)) \
    gimage = ((GimpItemTreeView *) data)->gimage; \
72 73 74
  else \
    gimage = NULL; \
  \
75 76 77
  if (! gimage) \
    return

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

84

85 86
/*  public functions  */

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

  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);
107
	  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
108 109 110 111 112
	}
    }
}

void
113 114
layers_select_next_cmd_callback (GtkWidget *widget,
                                 gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
115 116 117 118
{
  GimpImage *gimage;
  GimpLayer *new_layer;
  gint       current_layer;
119
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
120 121 122 123 124 125 126 127 128 129 130

  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);
131
      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
132 133 134
    }
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
void
layers_select_top_cmd_callback (GtkWidget *widget,
                                gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *new_layer;
  return_if_no_image (gimage, data);

  new_layer = (GimpLayer *)
    gimp_container_get_child_by_index (gimage->layers, 0);

  if (new_layer)
    {
      gimp_image_set_active_layer (gimage, new_layer);
      gimp_image_flush (gimage);
    }
}

void
layers_select_bottom_cmd_callback (GtkWidget *widget,
                                   gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *new_layer;
  gint       num_layers;
  return_if_no_image (gimage, data);

  num_layers = gimp_container_num_children (gimage->layers);

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

      if (new_layer)
        {
          gimp_image_set_active_layer (gimage, new_layer);
          gimp_image_flush (gimage);
        }
    }
}

Michael Natterer's avatar
Michael Natterer committed
177 178 179 180 181
void
layers_raise_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
182
  GimpLayer *active_layer;
183
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
184

185
  gimp_image_raise_layer (gimage, active_layer);
186
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
187 188 189 190 191 192 193
}

void
layers_lower_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
194
  GimpLayer *active_layer;
195
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
196

197
  gimp_image_lower_layer (gimage, active_layer);
198
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
199 200 201 202 203 204 205
}

void
layers_raise_to_top_cmd_callback (GtkWidget *widget,
				  gpointer   data)
{
  GimpImage *gimage;
206
  GimpLayer *active_layer;
207
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
208

209
  gimp_image_raise_layer_to_top (gimage, active_layer);
210
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
211 212 213 214 215 216 217
}

void
layers_lower_to_bottom_cmd_callback (GtkWidget *widget,
				     gpointer   data)
{
  GimpImage *gimage;
218
  GimpLayer *active_layer;
219
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
220

221
  gimp_image_lower_layer_to_bottom (gimage, active_layer);
222
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
223 224 225 226 227 228 229
}

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

Michael Natterer's avatar
Michael Natterer committed
232
  layers_new_layer_query (gimage, NULL, TRUE);
Michael Natterer's avatar
Michael Natterer committed
233 234 235 236 237 238 239 240 241
}

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

244 245 246 247
  new_layer =
    GIMP_LAYER (gimp_item_duplicate (GIMP_ITEM (active_layer),
                                     G_TYPE_FROM_INSTANCE (active_layer),
                                     TRUE));
Michael Natterer's avatar
Michael Natterer committed
248 249
  gimp_image_add_layer (gimage, new_layer, -1);

250
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
251 252 253
}

void
254
layers_anchor_cmd_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
255 256 257
			    gpointer   data)
{
  GimpImage *gimage;
258
  GimpLayer *active_layer;
259
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
260

261 262 263 264 265
  if (gimp_layer_is_floating_sel (active_layer))
    {
      floating_sel_anchor (active_layer);
      gimp_image_flush (gimage);
    }
266
}
Michael Natterer's avatar
Michael Natterer committed
267

268 269 270 271 272
void
layers_merge_down_cmd_callback (GtkWidget *widget,
				gpointer   data)
{
  GimpImage *gimage;
273
  GimpLayer *active_layer;
274
  return_if_no_layer (gimage, active_layer, data);
275

276
  gimp_image_merge_down (gimage, active_layer, GIMP_EXPAND_AS_NECESSARY);
277
  gimp_image_flush (gimage);
278 279
}

280 281 282 283 284
void
layers_delete_cmd_callback (GtkWidget *widget,
			    gpointer   data)
{
  GimpImage *gimage;
285
  GimpLayer *active_layer;
286
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
287

288 289
  if (gimp_layer_is_floating_sel (active_layer))
    floating_sel_remove (active_layer);
Michael Natterer's avatar
Michael Natterer committed
290
  else
291
    gimp_image_remove_layer (gimage, active_layer);
292

293
  gimp_image_flush (gimage);
294 295 296
}

void
297 298
layers_resize_cmd_callback (GtkWidget *widget,
			    gpointer   data)
299 300
{
  GimpImage *gimage;
301
  GimpLayer *active_layer;
302
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
303

304
  layers_resize_layer_query (gimage, active_layer);
Michael Natterer's avatar
Michael Natterer committed
305 306 307
}

void
308 309
layers_resize_to_image_cmd_callback (GtkWidget *widget,
				     gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
310 311
{
  GimpImage *gimage;
312
  GimpLayer *active_layer;
313
  return_if_no_layer (gimage, active_layer, data);
314 315
  
  gimp_layer_resize_to_image (active_layer);
316
  gimp_image_flush (gimage);
317
}
Michael Natterer's avatar
Michael Natterer committed
318

319 320 321 322 323 324
void
layers_scale_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
  GimpImage *gimage;
  GimpLayer *active_layer;
325
  return_if_no_layer (gimage, active_layer, data);
326 327

  layers_scale_layer_query (gimage, active_layer);
Michael Natterer's avatar
Michael Natterer committed
328 329 330
}

void
331 332
layers_crop_cmd_callback (GtkWidget *widget,
                          gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
333 334
{
  GimpImage *gimage;
335
  GimpLayer *active_layer;
336 337
  gint       x1, y1, x2, y2;
  gint       off_x, off_y;
338
  return_if_no_layer (gimage, active_layer, data);
339 340 341 342 343 344 345

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

346
  gimp_item_offsets (GIMP_ITEM (active_layer), &off_x, &off_y);
347 348 349 350

  off_x -= x1;
  off_y -= y1;

351 352
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_LAYER_RESIZE,
                               _("Crop Layer"));
353 354 355 356

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

357
  gimp_item_resize (GIMP_ITEM (active_layer), x2 - x1, y2 - y1, off_x, off_y);
358 359 360 361

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

362
  gimp_image_undo_group_end (gimage);
363

364
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
365 366 367 368 369 370 371
}

void
layers_add_layer_mask_cmd_callback (GtkWidget *widget,
				    gpointer   data)
{
  GimpImage *gimage;
372
  GimpLayer *active_layer;
373
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
374

375
  layers_add_mask_query (active_layer);
Michael Natterer's avatar
Michael Natterer committed
376 377 378 379 380 381 382
}

void
layers_apply_layer_mask_cmd_callback (GtkWidget *widget,
				      gpointer   data)
{
  GimpImage *gimage;
383
  GimpLayer *active_layer;
384
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
385

386
  if (gimp_layer_get_mask (active_layer))
Michael Natterer's avatar
Michael Natterer committed
387
    {
388
      gimp_layer_apply_mask (active_layer, GIMP_MASK_APPLY, TRUE);
389
      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
390 391 392 393 394 395 396 397
    }
}

void
layers_delete_layer_mask_cmd_callback (GtkWidget *widget,
				       gpointer   data)
{
  GimpImage *gimage;
398
  GimpLayer *active_layer;
399
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
400

401
  if (gimp_layer_get_mask (active_layer))
Michael Natterer's avatar
Michael Natterer committed
402
    {
403
      gimp_layer_apply_mask (active_layer, GIMP_MASK_DISCARD, TRUE);
404
      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
405 406 407 408
    }
}

void
409 410
layers_mask_select_cmd_callback (GtkWidget *widget,
				 gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
411 412
{
  GimpImage *gimage;
413
  GimpLayer *active_layer;
414
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
415

416 417 418
  if (gimp_layer_get_mask (active_layer))
    {
      gimp_image_mask_layer_mask (gimage, active_layer);
419
      gimp_image_flush (gimage);
420
    }
Michael Natterer's avatar
Michael Natterer committed
421 422 423
}

void
424 425
layers_alpha_select_cmd_callback (GtkWidget *widget,
				  gpointer   data)
Michael Natterer's avatar
Michael Natterer committed
426 427
{
  GimpImage *gimage;
428
  GimpLayer *active_layer;
429
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
430

431
  gimp_image_mask_layer_alpha (gimage, active_layer);
432
  gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
433 434 435 436 437 438 439
}

void
layers_add_alpha_channel_cmd_callback (GtkWidget *widget,
				       gpointer   data)
{
  GimpImage *gimage;
440
  GimpLayer *active_layer;
441
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
442

443 444 445
  if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (active_layer)))
    {
      gimp_layer_add_alpha (active_layer);
446
      gimp_image_flush (gimage);
447
    }
Michael Natterer's avatar
Michael Natterer committed
448 449 450 451 452 453 454
}

void
layers_edit_attributes_cmd_callback (GtkWidget *widget,
				     gpointer   data)
{
  GimpImage *gimage;
455
  GimpLayer *active_layer;
456
  return_if_no_layer (gimage, active_layer, data);
Michael Natterer's avatar
Michael Natterer committed
457

458
  layers_edit_layer_query (active_layer);
Michael Natterer's avatar
Michael Natterer committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
}


/********************************/
/*  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;
};

481
static GimpFillType  fill_type  = GIMP_TRANSPARENT_FILL;
Michael Natterer's avatar
Michael Natterer committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
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))
    {
507 508 509
      layer = gimp_layer_new (gimage,
                              options->xsize,
                              options->ysize,
Michael Natterer's avatar
Michael Natterer committed
510
			      gimp_image_base_type_with_alpha (gimage),
511 512
			      layer_name,
                              GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
Michael Natterer's avatar
Michael Natterer committed
513 514
      if (layer) 
	{
Michael Natterer's avatar
Michael Natterer committed
515 516 517
	  gimp_drawable_fill_by_type (GIMP_DRAWABLE (layer),
				      gimp_get_user_context (gimage->gimp),
				      fill_type);
Michael Natterer's avatar
Michael Natterer committed
518 519
	  gimp_image_add_layer (gimage, layer, -1);
	  
520
          gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
521 522 523 524 525 526 527 528 529 530 531 532
	} 
      else 
	{
	  g_message ("new_layer_query_ok_callback():\n"
		     "could not allocate new layer");
	}
    }

  gtk_widget_destroy (options->query_box);
}

void
533
layers_new_layer_query (GimpImage *gimage,
Michael Natterer's avatar
Michael Natterer committed
534 535
                        GimpLayer *template,
                        gboolean   interactive)
Michael Natterer's avatar
Michael Natterer committed
536 537
{
  NewLayerOptions *options;
538
  GimpLayer       *floating_sel;
Michael Natterer's avatar
Michael Natterer committed
539 540 541 542 543 544
  GtkWidget       *vbox;
  GtkWidget       *table;
  GtkWidget       *label;
  GtkObject       *adjustment;
  GtkWidget       *spinbutton;
  GtkWidget       *frame;
545
  GtkWidget       *button;
Michael Natterer's avatar
Michael Natterer committed
546

547
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
548
  g_return_if_fail (template == NULL || GIMP_IS_LAYER (template));
549 550 551 552 553 554 555

  /*  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);
556
      gimp_image_flush (gimage);
557 558 559
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
560
  if (template || ! interactive)
561
    {
Michael Natterer's avatar
Michael Natterer committed
562 563 564 565 566 567 568 569
      GimpLayer            *new_layer;
      gint                  width, height;
      gint                  off_x, off_y;
      gdouble               opacity;
      GimpLayerModeEffects  mode;

      if (template)
        {
570 571 572
          gimp_item_offsets (GIMP_ITEM (template), &off_x, &off_y);
          width   = gimp_item_width  (GIMP_ITEM (template));
          height  = gimp_item_height (GIMP_ITEM (template));
Michael Natterer's avatar
Michael Natterer committed
573 574 575 576 577 578 579 580 581 582 583 584
          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;
        }
585

586 587
      gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_PASTE,
                                   _("New Layer"));
588

589 590 591
      new_layer = gimp_layer_new (gimage,
                                  width,
                                  height,
592 593
                                  gimp_image_base_type_with_alpha (gimage),
                                  _("Empty Layer Copy"),
Michael Natterer's avatar
Michael Natterer committed
594 595 596 597 598 599 600 601
                                  opacity,
                                  mode);

      if (template)
        {
          gimp_drawable_fill_by_type (GIMP_DRAWABLE (new_layer),
                                      gimp_get_user_context (gimage->gimp),
                                      GIMP_TRANSPARENT_FILL);
602
          gimp_item_translate (GIMP_ITEM (new_layer), off_x, off_y, FALSE);
Michael Natterer's avatar
Michael Natterer committed
603
        }
604 605 606

      gimp_image_add_layer (gimage, new_layer, -1);

607
      gimp_image_undo_group_end (gimage);
608 609 610
      return;
    }

611 612
  options = g_new0 (NewLayerOptions, 1);

Michael Natterer's avatar
Michael Natterer committed
613 614 615 616 617
  options->fill_type = fill_type;
  options->gimage    = gimage;

  /*  The dialog  */
  options->query_box =
618 619
    gimp_viewable_dialog_new (GIMP_VIEWABLE (gimage),
                              _("New Layer"), "new_layer_options",
620
                              GIMP_STOCK_LAYER,
621
                              _("Create a New Layer"),
622 623
                              gimp_standard_help_func,
                              "dialogs/layers/new_layer.html",
Michael Natterer's avatar
Michael Natterer committed
624

625 626
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
627

628 629
                              GTK_STOCK_OK, new_layer_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
630

631
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
632

633 634 635
  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);
Michael Natterer's avatar
Michael Natterer committed
636 637

  /*  The main vbox  */
638
  vbox = gtk_vbox_new (FALSE, 4);
Michael Natterer's avatar
Michael Natterer committed
639 640 641 642 643 644 645 646 647 648 649 650 651
  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")));
652 653 654
  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
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

  /*  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  */
670 671 672 673 674
  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
675
  options->size_se = gimp_size_entry_new (1, gimage->unit, "%a",
676
					  TRUE, TRUE, FALSE, 10,
Michael Natterer's avatar
Michael Natterer committed
677
					  GIMP_SIZE_ENTRY_UPDATE_SIZE);
678 679 680
  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
681 682 683 684 685
  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);
686

Michael Natterer's avatar
Michael Natterer committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  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  */
719 720 721 722 723 724 725 726
  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);
727 728
  gimp_radio_group_set_active (GTK_RADIO_BUTTON (button),
                               GINT_TO_POINTER (options->fill_type));  
Michael Natterer's avatar
Michael Natterer committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

  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;
759
  layer   = options->layer;
Michael Natterer's avatar
Michael Natterer committed
760

761
  if (options->gimage)
Michael Natterer's avatar
Michael Natterer committed
762
    {
763 764 765 766 767
      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
768
        {
769 770
          gimp_item_rename (GIMP_ITEM (layer), new_name);
          gimp_image_flush (options->gimage);
771
        }
Michael Natterer's avatar
Michael Natterer committed
772 773 774 775 776 777 778 779 780 781
    }

  gtk_widget_destroy (options->query_box); 
}

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

784 785
  options = g_new0 (EditLayerOptions, 1);

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

  /*  The dialog  */
  options->query_box =
791 792 793 794 795 796 797
    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
798

799 800
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
801

802 803
                              GTK_STOCK_OK, edit_layer_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
804

805
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
806

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

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

817 818 819 820 821
  /*  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
822 823 824 825 826 827

  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)))));
828 829 830
  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
831

832
  g_signal_connect (options->name_entry, "activate",
833 834 835
		    G_CALLBACK (edit_layer_query_ok_callback),
		    options);

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

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

typedef struct _AddMaskOptions AddMaskOptions;

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

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

  options = (AddMaskOptions *) data;

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

      if (options->invert)
        gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);

Michael Natterer's avatar
Michael Natterer committed
872
      gimp_layer_add_mask (layer, mask, TRUE);
873
      g_object_unref (mask);
874

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

  gtk_widget_destroy (options->query_box);
}

static void
layers_add_mask_query (GimpLayer *layer)
{
  AddMaskOptions *options;
  GtkWidget      *frame;
886 887
  GtkWidget      *button;
  GtkWidget      *sep;
Michael Natterer's avatar
Michael Natterer committed
888 889 890 891 892
  GimpImage      *gimage;
  
  /*  The new options structure  */
  options = g_new (AddMaskOptions, 1);
  options->layer         = layer;
893
  options->add_mask_type = GIMP_ADD_WHITE_MASK;
894
  options->invert        = FALSE;
Michael Natterer's avatar
Michael Natterer committed
895

896
  gimage = gimp_item_get_image (GIMP_ITEM (layer));
Michael Natterer's avatar
Michael Natterer committed
897 898 899
  
  /*  The dialog  */
  options->query_box =
900
    gimp_viewable_dialog_new (GIMP_VIEWABLE (layer),
901
                              _("Add Layer Mask"), "add_mask_options",
902
                              GTK_STOCK_ADD,
903
                              _("Add a Mask to the Layer"),
904 905
                              gimp_standard_help_func,
                              "dialogs/layers/add_layer_mask.html",
Michael Natterer's avatar
Michael Natterer committed
906

907 908
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
Michael Natterer's avatar
Michael Natterer committed
909

910 911
                              GTK_STOCK_OK, add_mask_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
912

913
                              NULL);
Michael Natterer's avatar
Michael Natterer committed
914

915
  g_object_weak_ref (G_OBJECT (options->query_box),
916 917 918 919 920 921 922 923 924 925
		     (GWeakNotify) g_free, options);

  frame = gimp_enum_radio_frame_new (GIMP_TYPE_ADD_MASK_TYPE,
                                     gtk_label_new (_("Initialize Layer Mask to:")),
                                     6,
                                     G_CALLBACK (gimp_radio_button_update),
                                     &options->add_mask_type,
                                     &button);
  gimp_radio_group_set_active (GTK_RADIO_BUTTON (button),
                               GINT_TO_POINTER (options->add_mask_type));  
926

Michael Natterer's avatar
Michael Natterer committed
927 928 929 930
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     frame);
  gtk_widget_show (frame);

931 932 933 934 935 936 937 938 939 940 941 942 943
  sep = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (GTK_BIN (frame)->child), sep, FALSE, FALSE, 0);
  gtk_widget_show (sep);

  button = gtk_check_button_new_with_mnemonic (_("In_vert Mask"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), options->invert);
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &options->invert);

  gtk_box_pack_end (GTK_BOX (GTK_BIN (frame)->child), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

Michael Natterer's avatar
Michael Natterer committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  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)))
    {
972 973
      GimpImage *gimage;

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

976 977 978
      gimage = gimp_item_get_image (GIMP_ITEM (layer));

      if (gimage)
Michael Natterer's avatar
Michael Natterer committed
979 980
	{
	  if (gimp_layer_is_floating_sel (layer))
981 982 983 984 985 986
            {
              gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_LAYER_SCALE,
                                           _("Scale Layer"));

              floating_sel_relax (layer, TRUE);
            }
Michael Natterer's avatar
Michael Natterer committed
987

988 989 990 991 992
	  gimp_item_scale_by_origin (GIMP_ITEM (layer),
                                     options->resize->width,
                                     options->resize->height,
                                     options->resize->interpolation,
                                     TRUE);
Michael Natterer's avatar
Michael Natterer committed
993 994

	  if (gimp_layer_is_floating_sel (layer))
995 996
            {
              floating_sel_rigor (layer, TRUE);
Michael Natterer's avatar
Michael Natterer committed
997

998 999
              gimp_image_undo_group_end (gimage);
            }
Michael Natterer's avatar
Michael Natterer committed
1000

1001
          gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	}

      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
1015
                          GimpLayer *layer)
Michael Natterer's avatar
Michael Natterer committed
1016 1017 1018
{
  ScaleLayerOptions *options;

1019 1020 1021 1022
  options = g_new0 (ScaleLayerOptions, 1);

  options->layer = layer;

Michael Natterer's avatar
Michael Natterer committed
1023
  options->resize =
1024
    resize_widget_new (GIMP_VIEWABLE (layer),
Michael Natterer's avatar
Michael Natterer committed
1025
                       ScaleWidget,
1026 1027
		       gimp_item_width  (GIMP_ITEM (layer)),
		       gimp_item_height (GIMP_ITEM (layer)),
Michael Natterer's avatar
Michael Natterer committed
1028 1029 1030 1031
		       gimage->xresolution,
		       gimage->yresolution,
		       gimage->unit,
		       TRUE,
1032
		       G_CALLBACK (scale_layer_query_ok_callback),
1033
                       options);
Michael Natterer's avatar
Michael Natterer committed
1034

1035 1036 1037
  g_object_weak_ref (G_OBJECT (options->resize->resize_shell),
		     (GWeakNotify) g_free,
		     options);
Michael Natterer's avatar
Michael Natterer committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

  gtk_widget_show (options->resize->resize_shell);
}

/*****************************/
/*  The resize layer dialog  */
/*****************************/

typedef struct _ResizeLayerOptions ResizeLayerOptions;

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

static void
resize_layer_query_ok_callback (GtkWidget *widget,
				gpointer   data)
{
  ResizeLayerOptions *options;
  GimpLayer          *layer;

  options = (ResizeLayerOptions *) data;

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

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

1070 1071 1072
      gimage = gimp_item_get_image (GIMP_ITEM (layer));

      if (gimage)
Michael Natterer's avatar
Michael Natterer committed
1073 1074
	{
	  if (gimp_layer_is_floating_sel (layer))
1075 1076 1077 1078 1079 1080
            {
              gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_LAYER_RESIZE,
                                           _("Resize Layer"));

              floating_sel_relax (layer, TRUE);
            }
Michael Natterer's avatar
Michael Natterer committed
1081

1082 1083 1084 1085 1086
	  gimp_item_resize (GIMP_ITEM (layer),
                            options->resize->width,
                            options->resize->height,
                            options->resize->offset_x,
                            options->resize->offset_y);