layers-actions.c 12.8 KB
Newer Older
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 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 <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "actions-types.h"

#include "core/gimpimage.h"
#include "core/gimplayer.h"
#include "core/gimplist.h"

#include "text/gimptextlayer.h"

#include "widgets/gimphelp-ids.h"
#include "widgets/gimpactiongroup.h"
#include "widgets/gimpitemtreeview.h"

#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"

#include "layers-actions.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "layers-commands.h"
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include "gimp-intl.h"


static GimpActionEntry layers_actions[] =
{
  { "layers-menu", NULL,
    N_("_Layer") },

  { "layers-stack-menu", NULL,
    N_("Stac_k") },

  { "layers-colors-menu", NULL,
    N_("_Colors") },

  { "layers-colors-auto-menu", NULL,
    N_("_Auto") },

  { "layers-mask-menu", NULL,
    N_("_Mask") },

  { "layers-transparency-menu", NULL,
    N_("Tr_ansparency") },

  { "layers-transform-menu", NULL,
    N_("_Transform") },

  { "layers-text-tool", GIMP_STOCK_TOOL_TEXT,
    N_("Te_xt Tool"), NULL, NULL,
    G_CALLBACK (layers_text_tool_cmd_callback),
    GIMP_HELP_TOOL_TEXT },

  { "layers-edit-attributes", GIMP_STOCK_EDIT,
    N_("_Edit Layer Attributes..."), NULL, NULL,
    G_CALLBACK (layers_edit_attributes_cmd_callback),
    GIMP_HELP_LAYER_EDIT },

  { "layers-new", GTK_STOCK_NEW,
    N_("_New Layer..."), "", NULL,
    G_CALLBACK (layers_new_cmd_callback),
    GIMP_HELP_LAYER_NEW },

  { "layers-duplicate", GIMP_STOCK_DUPLICATE,
    N_("D_uplicate Layer"), NULL, NULL,
    G_CALLBACK (layers_duplicate_cmd_callback),
    GIMP_HELP_LAYER_DUPLICATE },

  { "layers-delete", GTK_STOCK_DELETE,
    N_("_Delete Layer"), "", NULL,
    G_CALLBACK (layers_delete_cmd_callback),
    GIMP_HELP_LAYER_DELETE },

  { "layers-select-previous", NULL,
    N_("Select _Previous Layer"), "Prior", NULL,
    G_CALLBACK (layers_select_previous_cmd_callback),
    GIMP_HELP_LAYER_PREVIOUS },

  { "layers-select-top", NULL,
    N_("Select _Top Layer"), "Home", NULL,
    G_CALLBACK (layers_select_top_cmd_callback),
    GIMP_HELP_LAYER_TOP },

  { "layers-select-next", NULL,
    N_("Select _Next Layer"), "Next", NULL,
    G_CALLBACK (layers_select_next_cmd_callback),
    GIMP_HELP_LAYER_NEXT },

109
  { "layers-select-bottom", NULL,
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
    N_("Select _Bottom Layer"), "End", NULL,
    G_CALLBACK (layers_select_bottom_cmd_callback),
    GIMP_HELP_LAYER_BOTTOM },

  { "layers-raise", GTK_STOCK_GO_UP,
    N_("_Raise Layer"), "", NULL,
    G_CALLBACK (layers_raise_cmd_callback),
    GIMP_HELP_LAYER_RAISE },

  { "layers-raise-to-top", GTK_STOCK_GOTO_TOP,
    N_("Layer to _Top"), "", NULL,
    G_CALLBACK (layers_raise_to_top_cmd_callback),
    GIMP_HELP_LAYER_RAISE_TO_TOP },

  { "layers-lower", GTK_STOCK_GO_DOWN,
    N_("_Lower Layer"), "", NULL,
    G_CALLBACK (layers_lower_cmd_callback),
    GIMP_HELP_LAYER_LOWER },

  { "layers-lower-to-bottom", GTK_STOCK_GOTO_BOTTOM,
    N_("Layer to _Bottom"), "", NULL,
    G_CALLBACK (layers_lower_to_bottom_cmd_callback),
    GIMP_HELP_LAYER_LOWER_TO_BOTTOM },

  { "layers-anchor", GIMP_STOCK_ANCHOR,
    N_("_Anchor Layer"), NULL, NULL,
    G_CALLBACK (layers_anchor_cmd_callback),
    GIMP_HELP_LAYER_ANCHOR },

  { "layers-merge-down", GIMP_STOCK_MERGE_DOWN,
    N_("Merge Do_wn"), NULL, NULL,
    G_CALLBACK (layers_merge_down_cmd_callback),
    GIMP_HELP_LAYER_MERGE_DOWN },

  { "layers-merge-layers", NULL,
    N_("Merge _Visible Layers..."), NULL, NULL,
    G_CALLBACK (layers_merge_layers_cmd_callback),
    GIMP_HELP_IMAGE_MERGE_LAYERS },

  { "layers-flatten-image", NULL,
    N_("_Flatten Image"), NULL, NULL,
    G_CALLBACK (layers_flatten_image_cmd_callback),
    GIMP_HELP_IMAGE_FLATTEN },

  { "layers-text-discard", GIMP_STOCK_TOOL_TEXT,
    N_("_Discard Text Information"), NULL, NULL,
    G_CALLBACK (layers_text_discard_cmd_callback),
    GIMP_HELP_LAYER_TEXT_DISCARD },

  { "layers-resize", GIMP_STOCK_RESIZE,
    N_("Layer B_oundary Size..."), NULL, NULL,
    G_CALLBACK (layers_resize_cmd_callback),
    GIMP_HELP_LAYER_RESIZE },

  { "layers-resize-to-image", GIMP_STOCK_LAYER_TO_IMAGESIZE,
    N_("Layer to _Image Size"), NULL, NULL,
    G_CALLBACK (layers_resize_to_image_cmd_callback),
    GIMP_HELP_LAYER_RESIZE_TO_IMAGE },

  { "layers-scale", GIMP_STOCK_SCALE,
    N_("_Scale Layer..."), NULL, NULL,
    G_CALLBACK (layers_scale_cmd_callback),
    GIMP_HELP_LAYER_SCALE },

174
175
176
177
178
  { "layers-crop", GIMP_STOCK_TOOL_CROP,
    N_("Cr_op Layer"), NULL, NULL,
    G_CALLBACK (layers_crop_cmd_callback),
    GIMP_HELP_LAYER_CROP },

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  { "layers-mask-add", GIMP_STOCK_LAYER_MASK,
    N_("Add La_yer Mask..."), NULL, NULL,
    G_CALLBACK (layers_mask_add_cmd_callback),
    GIMP_HELP_LAYER_MASK_ADD },

  { "layers-mask-apply", NULL,
    N_("Apply Layer _Mask"), NULL, NULL,
    G_CALLBACK (layers_mask_apply_cmd_callback),
    GIMP_HELP_LAYER_MASK_APPLY },

  { "layers-mask-delete", GTK_STOCK_DELETE,
    N_("Delete Layer Mas_k"), "", NULL,
    G_CALLBACK (layers_mask_delete_cmd_callback),
    GIMP_HELP_LAYER_MASK_DELETE },

  { "layers-alpha-add", GIMP_STOCK_TRANSPARENCY,
    N_("Add Alpha C_hannel"), NULL, NULL,
    G_CALLBACK (layers_alpha_add_cmd_callback),
    GIMP_HELP_LAYER_ALPHA_ADD }
};

static GimpEnumActionEntry layers_mask_to_selection_actions[] =
{
  { "layers-mask-selection-replace", GIMP_STOCK_SELECTION_REPLACE,
    N_("_Mask to Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_REPLACE,
    GIMP_HELP_LAYER_MASK_SELECTION_REPLACE },

207
  { "layers-mask-selection-add", GIMP_STOCK_SELECTION_ADD,
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    N_("_Add to Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_ADD,
    GIMP_HELP_LAYER_MASK_SELECTION_ADD },

  { "layers-mask-selection-subtract", GIMP_STOCK_SELECTION_SUBTRACT,
    N_("_Subtract from Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_SUBTRACT,
    GIMP_HELP_LAYER_MASK_SELECTION_SUBTRACT },

  { "layers-mask-selection-intersect", GIMP_STOCK_SELECTION_INTERSECT,
    N_("_Intersect with Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_INTERSECT,
    GIMP_HELP_LAYER_MASK_SELECTION_INTERSECT }
};

static GimpEnumActionEntry layers_alpha_to_selection_actions[] =
{
  { "layers-alpha-selection-replace", GIMP_STOCK_SELECTION_REPLACE,
    N_("Al_pha to Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_REPLACE,
    GIMP_HELP_LAYER_ALPHA_SELECTION_REPLACE },

  { "layers-alpha-selection-add", GIMP_STOCK_SELECTION_ADD,
    N_("A_dd to Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_ADD,
    GIMP_HELP_LAYER_ALPHA_SELECTION_ADD },

  { "layers-alpha-selection-subtract", GIMP_STOCK_SELECTION_SUBTRACT,
    N_("_Subtract from Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_SUBTRACT,
    GIMP_HELP_LAYER_ALPHA_SELECTION_SUBTRACT },

240
  { "layers-alpha-selection-intersect", GIMP_STOCK_SELECTION_INTERSECT,
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    N_("_Intersect with Selection"), NULL, NULL,
    GIMP_CHANNEL_OP_INTERSECT,
    GIMP_HELP_LAYER_ALPHA_SELECTION_INTERSECT }
};

void
layers_actions_setup (GimpActionGroup *group,
                      gpointer         data)
{
  gimp_action_group_add_actions (group,
                                 layers_actions,
                                 G_N_ELEMENTS (layers_actions),
                                 data);

  gimp_action_group_add_enum_actions (group,
                                      layers_mask_to_selection_actions,
                                      G_N_ELEMENTS (layers_mask_to_selection_actions),
                                      G_CALLBACK (layers_mask_to_selection_cmd_callback),
                                      data);

  gimp_action_group_add_enum_actions (group,
                                      layers_alpha_to_selection_actions,
                                      G_N_ELEMENTS (layers_alpha_to_selection_actions),
                                      G_CALLBACK (layers_alpha_to_selection_cmd_callback),
                                      data);
}

void
layers_actions_update (GimpActionGroup *group,
                       gpointer         data)
{
  GimpImage *gimage     = NULL;
  GimpLayer *layer      = NULL;
  gboolean   fs         = FALSE;    /*  floating sel           */
  gboolean   ac         = FALSE;    /*  active channel         */
  gboolean   lm         = FALSE;    /*  layer mask             */
  gboolean   alpha      = FALSE;    /*  alpha channel present  */
  gboolean   indexed    = FALSE;    /*  is indexed             */
  gboolean   next_alpha = FALSE;
  gboolean   text_layer = FALSE;
  GList     *next       = NULL;
  GList     *prev       = NULL;

  if (GIMP_IS_ITEM_TREE_VIEW (data))
    gimage = GIMP_ITEM_TREE_VIEW (data)->gimage;
  else if (GIMP_IS_DISPLAY_SHELL (data))
    gimage = GIMP_DISPLAY_SHELL (data)->gdisp->gimage;
  else if (GIMP_IS_DISPLAY (data))
    gimage = GIMP_DISPLAY (data)->gimage;

  if (gimage)
    {
      GList *list;

      layer = gimp_image_get_active_layer (gimage);

      if (layer)
        lm = (gimp_layer_get_mask (layer)) ? TRUE : FALSE;

      fs = (gimp_image_floating_sel (gimage) != NULL);
      ac = (gimp_image_get_active_channel (gimage) != NULL);

      alpha = layer && gimp_drawable_has_alpha (GIMP_DRAWABLE (layer));

      indexed = (gimp_image_base_type (gimage) == GIMP_INDEXED);

      for (list = GIMP_LIST (gimage->layers)->list;
           list;
           list = g_list_next (list))
        {
          if (layer == (GimpLayer *) list->data)
            {
              prev = g_list_previous (list);
              next = g_list_next (list);
              break;
            }
        }

      if (next)
        next_alpha = gimp_drawable_has_alpha (GIMP_DRAWABLE (next->data));
      else
        next_alpha = FALSE;

      text_layer = (layer &&
                    gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)));
    }

#define SET_VISIBLE(action,condition) \
        gimp_action_group_set_action_visible (group, action, (condition) != 0)
#define SET_SENSITIVE(action,condition) \
        gimp_action_group_set_action_sensitive (group, action, (condition) != 0)

  SET_VISIBLE   ("layers-text-tool",       text_layer && !ac);
  SET_SENSITIVE ("layers-edit-attributes", layer && !fs && !ac);

  SET_SENSITIVE ("layers-new",             gimage);
  SET_SENSITIVE ("layers-duplicate",    layer && !fs && !ac);
  SET_SENSITIVE ("layers-delete",       layer && !ac);

  SET_SENSITIVE ("layers-select-previous", layer && !fs && !ac && prev);
  SET_SENSITIVE ("layers-select-top",      layer && !fs && !ac && prev);
  SET_SENSITIVE ("layers-select-next",     layer && !fs && !ac && next);
  SET_SENSITIVE ("layers-select-bottom",   layer && !fs && !ac && next);

  SET_SENSITIVE ("layers-raise",           layer && !fs && !ac && alpha && prev);
  SET_SENSITIVE ("layers-raise-to-top",    layer && !fs && !ac && alpha && prev);
  SET_SENSITIVE ("layers-lower",           layer && !fs && !ac && next && next_alpha);
  SET_SENSITIVE ("layers-lower-to-bottom", layer && !fs && !ac && next && next_alpha);

  SET_SENSITIVE ("layers-anchor",        layer &&  fs && !ac);
  SET_SENSITIVE ("layers-merge-down",    layer && !fs && !ac && next);
  SET_SENSITIVE ("layers-merge-layers",  layer && !fs && !ac);
  SET_SENSITIVE ("layers-flatten-image", layer && !fs && !ac);
  SET_VISIBLE   ("layers-text-discard",  text_layer && !ac);

  SET_SENSITIVE ("layers-resize",          layer && !ac);
  SET_SENSITIVE ("layers-resize-to-image", layer && !ac);
  SET_SENSITIVE ("layers-scale",           layer && !ac);

  SET_SENSITIVE ("layers-mask-add",    layer && !fs && !ac && !lm && alpha);
  SET_SENSITIVE ("layers-mask-apply",  layer && !fs && !ac &&  lm);
  SET_SENSITIVE ("layers-mask-delete", layer && !fs && !ac &&  lm);
  SET_SENSITIVE ("layers-alpha-add",   layer && !fs && !alpha);

  SET_SENSITIVE ("layers-mask-selection-replace",   layer && !fs && !ac && lm);
  SET_SENSITIVE ("layers-mask-selection-add",       layer && !fs && !ac && lm);
  SET_SENSITIVE ("layers-mask-selection-subtract",  layer && !fs && !ac && lm);
  SET_SENSITIVE ("layers-mask-selection-intersect", layer && !fs && !ac && lm);

  SET_SENSITIVE ("layers-alpha-selection-replace",   layer && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-add",       layer && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-subtract",  layer && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-intersect", layer && !fs && !ac);

#undef SET_VISIBLE
#undef SET_SENSITIVE
}