5
// Jonathan Pobst <monkey@jpobst.com>
7
// Copyright (c) 2010 Jonathan Pobst
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
public class LayerActions
35
public Gtk.Action AddNewLayer { get; private set; }
36
public Gtk.Action DeleteLayer { get; private set; }
37
public Gtk.Action DuplicateLayer { get; private set; }
38
public Gtk.Action MergeLayerDown { get; private set; }
39
public Gtk.Action ImportFromFile { get; private set; }
40
public Gtk.Action FlipHorizontal { get; private set; }
41
public Gtk.Action FlipVertical { get; private set; }
42
public Gtk.Action RotateZoom { get; private set; }
43
public Gtk.Action MoveLayerUp { get; private set; }
44
public Gtk.Action MoveLayerDown { get; private set; }
45
public Gtk.Action Properties { get; private set; }
47
public LayerActions ()
49
Gtk.IconFactory fact = new Gtk.IconFactory ();
50
fact.Add ("Menu.Layers.AddNewLayer.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.AddNewLayer.png")));
51
fact.Add ("Menu.Layers.DeleteLayer.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.DeleteLayer.png")));
52
fact.Add ("Menu.Layers.DuplicateLayer.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.DuplicateLayer.png")));
53
fact.Add ("Menu.Layers.MergeLayerDown.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.MergeLayerDown.png")));
54
fact.Add ("Menu.Layers.MoveLayerDown.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.MoveLayerDown.png")));
55
fact.Add ("Menu.Layers.MoveLayerUp.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.MoveLayerUp.png")));
56
fact.Add ("Menu.Layers.FlipHorizontal.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.FlipHorizontal.png")));
57
fact.Add ("Menu.Layers.FlipVertical.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.FlipVertical.png")));
58
fact.Add ("Menu.Layers.ImportFromFile.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.ImportFromFile.png")));
59
fact.Add ("Menu.Layers.LayerProperties.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.LayerProperties.png")));
60
fact.Add ("Menu.Layers.RotateZoom.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Layers.RotateZoom.png")));
63
AddNewLayer = new Gtk.Action ("AddNewLayer", Mono.Unix.Catalog.GetString ("Add New Layer"), null, "Menu.Layers.AddNewLayer.png");
64
DeleteLayer = new Gtk.Action ("DeleteLayer", Mono.Unix.Catalog.GetString ("Delete Layer"), null, "Menu.Layers.DeleteLayer.png");
65
DuplicateLayer = new Gtk.Action ("DuplicateLayer", Mono.Unix.Catalog.GetString ("Duplicate Layer"), null, "Menu.Layers.DuplicateLayer.png");
66
MergeLayerDown = new Gtk.Action ("MergeLayerDown", Mono.Unix.Catalog.GetString ("Merge Layer Down"), null, "Menu.Layers.MergeLayerDown.png");
67
ImportFromFile = new Gtk.Action ("ImportFromFile", Mono.Unix.Catalog.GetString ("Import From File"), null, "Menu.Layers.ImportFromFile.png");
68
FlipHorizontal = new Gtk.Action ("FlipHorizontal", Mono.Unix.Catalog.GetString ("Flip Horizontal"), null, "Menu.Layers.FlipHorizontal.png");
69
FlipVertical = new Gtk.Action ("FlipVertical", Mono.Unix.Catalog.GetString ("Flip Vertical"), null, "Menu.Layers.FlipVertical.png");
70
RotateZoom = new Gtk.Action ("RotateZoom", Mono.Unix.Catalog.GetString ("Rotate / Zoom"), null, "Menu.Layers.RotateZoom.png");
71
MoveLayerUp = new Gtk.Action ("MoveLayerUp", Mono.Unix.Catalog.GetString ("Move Layer Up"), null, "Menu.Layers.MoveLayerUp.png");
72
MoveLayerDown = new Gtk.Action ("MoveLayerDown", Mono.Unix.Catalog.GetString ("Move Layer Down"), null, "Menu.Layers.MoveLayerDown.png");
73
Properties = new Gtk.Action ("Properties", Mono.Unix.Catalog.GetString ("Layer Properties"), null, "Menu.Layers.LayerProperties.png");
75
RotateZoom.Sensitive = false;
78
#region Initialization
79
public void CreateMainMenu (Gtk.Menu menu)
81
menu.Remove (menu.Children[1]);
83
menu.Append (AddNewLayer.CreateAcceleratedMenuItem (Gdk.Key.N, Gdk.ModifierType.ControlMask | Gdk.ModifierType.ShiftMask));
84
menu.Append (DeleteLayer.CreateAcceleratedMenuItem (Gdk.Key.Delete, Gdk.ModifierType.ControlMask | Gdk.ModifierType.ShiftMask));
85
menu.Append (DuplicateLayer.CreateAcceleratedMenuItem (Gdk.Key.D, Gdk.ModifierType.ControlMask | Gdk.ModifierType.ShiftMask));
86
menu.Append (MergeLayerDown.CreateAcceleratedMenuItem (Gdk.Key.M, Gdk.ModifierType.ControlMask));
87
menu.Append (ImportFromFile.CreateMenuItem ());
88
menu.AppendSeparator ();
89
menu.Append (FlipHorizontal.CreateMenuItem ());
90
menu.Append (FlipVertical.CreateMenuItem ());
91
//menu.Append (RotateZoom.CreateAcceleratedMenuItem (Gdk.Key.Z, Gdk.ModifierType.ControlMask | Gdk.ModifierType.ShiftMask));
92
menu.AppendSeparator ();
93
menu.Append (Properties.CreateAcceleratedMenuItem (Gdk.Key.F4, Gdk.ModifierType.None));
96
public void CreateLayerWindowToolBar (Gtk.Toolbar toolbar)
98
toolbar.AppendItem (AddNewLayer.CreateToolBarItem ());
99
toolbar.AppendItem (DeleteLayer.CreateToolBarItem ());
100
toolbar.AppendItem (DuplicateLayer.CreateToolBarItem ());
101
toolbar.AppendItem (MergeLayerDown.CreateToolBarItem ());
102
toolbar.AppendItem (MoveLayerUp.CreateToolBarItem ());
103
toolbar.AppendItem (MoveLayerDown.CreateToolBarItem ());
104
toolbar.AppendItem (Properties.CreateToolBarItem ());
107
public void RegisterHandlers ()
109
AddNewLayer.Activated += HandlePintaCoreActionsLayersAddNewLayerActivated;
110
DeleteLayer.Activated += HandlePintaCoreActionsLayersDeleteLayerActivated;
111
DuplicateLayer.Activated += HandlePintaCoreActionsLayersDuplicateLayerActivated;
112
MergeLayerDown.Activated += HandlePintaCoreActionsLayersMergeLayerDownActivated;
113
MoveLayerDown.Activated += HandlePintaCoreActionsLayersMoveLayerDownActivated;
114
MoveLayerUp.Activated += HandlePintaCoreActionsLayersMoveLayerUpActivated;
115
FlipHorizontal.Activated += HandlePintaCoreActionsLayersFlipHorizontalActivated;
116
FlipVertical.Activated += HandlePintaCoreActionsLayersFlipVerticalActivated;
117
ImportFromFile.Activated += HandlePintaCoreActionsLayersImportFromFileActivated;
119
PintaCore.Layers.LayerAdded += EnableOrDisableLayerActions;
120
PintaCore.Layers.LayerRemoved += EnableOrDisableLayerActions;
121
PintaCore.Layers.SelectedLayerChanged += EnableOrDisableLayerActions;
123
EnableOrDisableLayerActions (null, EventArgs.Empty);
127
#region Action Handlers
128
private void EnableOrDisableLayerActions (object sender, EventArgs e)
130
if (PintaCore.Layers.Count > 1) {
131
PintaCore.Actions.Layers.DeleteLayer.Sensitive = true;
132
PintaCore.Actions.Image.Flatten.Sensitive = true;
134
PintaCore.Actions.Layers.DeleteLayer.Sensitive = false;
135
PintaCore.Actions.Image.Flatten.Sensitive = false;
138
if (PintaCore.Layers.CurrentLayerIndex > 0) {
139
PintaCore.Actions.Layers.MergeLayerDown.Sensitive = true;
140
PintaCore.Actions.Layers.MoveLayerDown.Sensitive = true;
142
PintaCore.Actions.Layers.MergeLayerDown.Sensitive = false;
143
PintaCore.Actions.Layers.MoveLayerDown.Sensitive = false;
146
if (PintaCore.Layers.CurrentLayerIndex < PintaCore.Layers.Count - 1)
147
PintaCore.Actions.Layers.MoveLayerUp.Sensitive = true;
149
PintaCore.Actions.Layers.MoveLayerUp.Sensitive = false;
152
private void HandlePintaCoreActionsLayersImportFromFileActivated (object sender, EventArgs e)
154
PintaCore.Layers.FinishSelection ();
156
Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog ("Open Image File", null, FileChooserAction.Open, Gtk.Stock.Cancel, Gtk.ResponseType.Cancel, Gtk.Stock.Open, Gtk.ResponseType.Ok);
158
int response = fcd.Run ();
160
if (response == (int)Gtk.ResponseType.Ok) {
162
string file = fcd.Filename;
164
// Open the image and add it to the layers
165
Layer layer = PintaCore.Layers.AddNewLayer (System.IO.Path.GetFileName (file));
167
Pixbuf bg = new Pixbuf (file, (int)PintaCore.Workspace.ImageSize.X, (int)PintaCore.Workspace.ImageSize.Y, true);
169
using (Cairo.Context g = new Cairo.Context (layer.Surface)) {
170
CairoHelper.SetSourcePixbuf (g, bg, 0, 0);
176
PintaCore.Layers.SetCurrentLayer (layer);
178
AddLayerHistoryItem hist = new AddLayerHistoryItem ("Menu.Layers.ImportFromFile.png", Mono.Unix.Catalog.GetString ("Import From File"), PintaCore.Layers.IndexOf (layer));
179
PintaCore.History.PushNewItem (hist);
181
PintaCore.Workspace.Invalidate ();
187
private void HandlePintaCoreActionsLayersFlipVerticalActivated (object sender, EventArgs e)
189
PintaCore.Layers.FinishSelection ();
191
PintaCore.Layers.FlipCurrentLayerVertical ();
192
PintaCore.History.PushNewItem (new InvertHistoryItem (InvertType.FlipLayerVertical, PintaCore.Layers.CurrentLayerIndex));
195
private void HandlePintaCoreActionsLayersFlipHorizontalActivated (object sender, EventArgs e)
197
PintaCore.Layers.FinishSelection ();
199
PintaCore.Layers.FlipCurrentLayerHorizontal ();
200
PintaCore.History.PushNewItem (new InvertHistoryItem (InvertType.FlipLayerHorizontal, PintaCore.Layers.CurrentLayerIndex));
203
private void HandlePintaCoreActionsLayersMoveLayerUpActivated (object sender, EventArgs e)
205
PintaCore.Layers.FinishSelection ();
207
SwapLayersHistoryItem hist = new SwapLayersHistoryItem ("Menu.Layers.MoveLayerUp.png", Mono.Unix.Catalog.GetString ("Move Layer Up"), PintaCore.Layers.CurrentLayerIndex, PintaCore.Layers.CurrentLayerIndex + 1);
209
PintaCore.Layers.MoveCurrentLayerUp ();
210
PintaCore.History.PushNewItem (hist);
213
private void HandlePintaCoreActionsLayersMoveLayerDownActivated (object sender, EventArgs e)
215
PintaCore.Layers.FinishSelection ();
217
SwapLayersHistoryItem hist = new SwapLayersHistoryItem ("Menu.Layers.MoveLayerDown.png", Mono.Unix.Catalog.GetString ("Move Layer Down"), PintaCore.Layers.CurrentLayerIndex, PintaCore.Layers.CurrentLayerIndex - 1);
219
PintaCore.Layers.MoveCurrentLayerDown ();
220
PintaCore.History.PushNewItem (hist);
223
private void HandlePintaCoreActionsLayersMergeLayerDownActivated (object sender, EventArgs e)
225
PintaCore.Layers.FinishSelection ();
227
CompoundHistoryItem hist = new CompoundHistoryItem ("Menu.Layers.MergeLayerDown.png", Mono.Unix.Catalog.GetString ("Merge Layer Down"));
228
DeleteLayerHistoryItem h1 = new DeleteLayerHistoryItem (string.Empty, string.Empty, PintaCore.Layers.CurrentLayer, PintaCore.Layers.CurrentLayerIndex);
229
SimpleHistoryItem h2 = new SimpleHistoryItem (string.Empty, string.Empty, PintaCore.Layers[PintaCore.Layers.CurrentLayerIndex - 1].Surface.Clone (), PintaCore.Layers.CurrentLayerIndex - 1);
234
PintaCore.Layers.MergeCurrentLayerDown ();
236
PintaCore.History.PushNewItem (hist);
239
private void HandlePintaCoreActionsLayersDuplicateLayerActivated (object sender, EventArgs e)
241
PintaCore.Layers.FinishSelection ();
243
Layer l = PintaCore.Layers.DuplicateCurrentLayer ();
245
// Make new layer the current layer
246
PintaCore.Layers.SetCurrentLayer (l);
248
AddLayerHistoryItem hist = new AddLayerHistoryItem ("Menu.Layers.DuplicateLayer.png", Mono.Unix.Catalog.GetString ("Duplicate Layer"), PintaCore.Layers.IndexOf (l));
249
PintaCore.History.PushNewItem (hist);
252
private void HandlePintaCoreActionsLayersDeleteLayerActivated (object sender, EventArgs e)
254
PintaCore.Layers.FinishSelection ();
256
DeleteLayerHistoryItem hist = new DeleteLayerHistoryItem ("Menu.Layers.DeleteLayer.png", Mono.Unix.Catalog.GetString ("Delete Layer"), PintaCore.Layers.CurrentLayer, PintaCore.Layers.CurrentLayerIndex);
258
PintaCore.Layers.DeleteLayer (PintaCore.Layers.CurrentLayerIndex, false);
260
PintaCore.History.PushNewItem (hist);
263
private void HandlePintaCoreActionsLayersAddNewLayerActivated (object sender, EventArgs e)
265
PintaCore.Layers.FinishSelection ();
267
Layer l = PintaCore.Layers.AddNewLayer (string.Empty);
269
// Make new layer the current layer
270
PintaCore.Layers.SetCurrentLayer (l);
272
AddLayerHistoryItem hist = new AddLayerHistoryItem ("Menu.Layers.AddNewLayer.png", Mono.Unix.Catalog.GetString ("Add New Layer"), PintaCore.Layers.IndexOf (l));
273
PintaCore.History.PushNewItem (hist);