~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta.Core/Actions/LayerActions.cs

  • Committer: Iain Lane
  • Date: 2010-02-19 15:33:30 UTC
  • Revision ID: git-v1:92cc09614a4e2801bfe39cd971f6b31e694a500b
ImportedĀ UpstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// LayerActions.cs
 
3
//  
 
4
// Author:
 
5
//       Jonathan Pobst <monkey@jpobst.com>
 
6
// 
 
7
// Copyright (c) 2010 Jonathan Pobst
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Gdk;
 
29
using Gtk;
 
30
 
 
31
namespace Pinta.Core
 
32
{
 
33
        public class LayerActions
 
34
        {
 
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; }
 
46
                
 
47
                public LayerActions ()
 
48
                {
 
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")));
 
61
                        fact.AddDefault ();
 
62
                        
 
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");
 
74
 
 
75
                        RotateZoom.Sensitive = false;
 
76
                }
 
77
 
 
78
                #region Initialization
 
79
                public void CreateMainMenu (Gtk.Menu menu)
 
80
                {
 
81
                        menu.Remove (menu.Children[1]);
 
82
                        
 
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));
 
94
                }
 
95
 
 
96
                public void CreateLayerWindowToolBar (Gtk.Toolbar toolbar)
 
97
                {
 
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 ());
 
105
                }
 
106
                
 
107
                public void RegisterHandlers ()
 
108
                {
 
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;
 
118
                        
 
119
                        PintaCore.Layers.LayerAdded += EnableOrDisableLayerActions;
 
120
                        PintaCore.Layers.LayerRemoved += EnableOrDisableLayerActions;
 
121
                        PintaCore.Layers.SelectedLayerChanged += EnableOrDisableLayerActions;
 
122
                        
 
123
                        EnableOrDisableLayerActions (null, EventArgs.Empty);
 
124
                }
 
125
                #endregion
 
126
 
 
127
                #region Action Handlers
 
128
                private void EnableOrDisableLayerActions (object sender, EventArgs e)
 
129
                {
 
130
                        if (PintaCore.Layers.Count > 1) {
 
131
                                PintaCore.Actions.Layers.DeleteLayer.Sensitive = true;
 
132
                                PintaCore.Actions.Image.Flatten.Sensitive = true;
 
133
                        } else {
 
134
                                PintaCore.Actions.Layers.DeleteLayer.Sensitive = false;
 
135
                                PintaCore.Actions.Image.Flatten.Sensitive = false;
 
136
                        }
 
137
                        
 
138
                        if (PintaCore.Layers.CurrentLayerIndex > 0) {
 
139
                                PintaCore.Actions.Layers.MergeLayerDown.Sensitive = true;
 
140
                                PintaCore.Actions.Layers.MoveLayerDown.Sensitive = true;
 
141
                        } else {
 
142
                                PintaCore.Actions.Layers.MergeLayerDown.Sensitive = false;
 
143
                                PintaCore.Actions.Layers.MoveLayerDown.Sensitive = false;
 
144
                        }
 
145
                        
 
146
                        if (PintaCore.Layers.CurrentLayerIndex < PintaCore.Layers.Count - 1)
 
147
                                PintaCore.Actions.Layers.MoveLayerUp.Sensitive = true;
 
148
                        else
 
149
                                PintaCore.Actions.Layers.MoveLayerUp.Sensitive = false;
 
150
                }
 
151
 
 
152
                private void HandlePintaCoreActionsLayersImportFromFileActivated (object sender, EventArgs e)
 
153
                {
 
154
                        PintaCore.Layers.FinishSelection ();
 
155
 
 
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);
 
157
                        
 
158
                        int response = fcd.Run ();
 
159
                        
 
160
                        if (response == (int)Gtk.ResponseType.Ok) {
 
161
                                
 
162
                                string file = fcd.Filename;
 
163
                                
 
164
                                // Open the image and add it to the layers
 
165
                                Layer layer = PintaCore.Layers.AddNewLayer (System.IO.Path.GetFileName (file));
 
166
                                
 
167
                                Pixbuf bg = new Pixbuf (file, (int)PintaCore.Workspace.ImageSize.X, (int)PintaCore.Workspace.ImageSize.Y, true);
 
168
                                
 
169
                                using (Cairo.Context g = new Cairo.Context (layer.Surface)) {
 
170
                                        CairoHelper.SetSourcePixbuf (g, bg, 0, 0);
 
171
                                        g.Paint ();
 
172
                                }
 
173
                                
 
174
                                bg.Dispose ();
 
175
                                
 
176
                                PintaCore.Layers.SetCurrentLayer (layer);
 
177
 
 
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);
 
180
 
 
181
                                PintaCore.Workspace.Invalidate ();
 
182
                        }
 
183
                        
 
184
                        fcd.Destroy ();
 
185
                }
 
186
 
 
187
                private void HandlePintaCoreActionsLayersFlipVerticalActivated (object sender, EventArgs e)
 
188
                {
 
189
                        PintaCore.Layers.FinishSelection ();
 
190
 
 
191
                        PintaCore.Layers.FlipCurrentLayerVertical ();
 
192
                        PintaCore.History.PushNewItem (new InvertHistoryItem (InvertType.FlipLayerVertical, PintaCore.Layers.CurrentLayerIndex));
 
193
                }
 
194
 
 
195
                private void HandlePintaCoreActionsLayersFlipHorizontalActivated (object sender, EventArgs e)
 
196
                {
 
197
                        PintaCore.Layers.FinishSelection ();
 
198
 
 
199
                        PintaCore.Layers.FlipCurrentLayerHorizontal ();
 
200
                        PintaCore.History.PushNewItem (new InvertHistoryItem (InvertType.FlipLayerHorizontal, PintaCore.Layers.CurrentLayerIndex));
 
201
                }
 
202
 
 
203
                private void HandlePintaCoreActionsLayersMoveLayerUpActivated (object sender, EventArgs e)
 
204
                {
 
205
                        PintaCore.Layers.FinishSelection ();
 
206
 
 
207
                        SwapLayersHistoryItem hist = new SwapLayersHistoryItem ("Menu.Layers.MoveLayerUp.png", Mono.Unix.Catalog.GetString ("Move Layer Up"), PintaCore.Layers.CurrentLayerIndex, PintaCore.Layers.CurrentLayerIndex + 1);
 
208
                        
 
209
                        PintaCore.Layers.MoveCurrentLayerUp ();
 
210
                        PintaCore.History.PushNewItem (hist);
 
211
                }
 
212
 
 
213
                private void HandlePintaCoreActionsLayersMoveLayerDownActivated (object sender, EventArgs e)
 
214
                {
 
215
                        PintaCore.Layers.FinishSelection ();
 
216
 
 
217
                        SwapLayersHistoryItem hist = new SwapLayersHistoryItem ("Menu.Layers.MoveLayerDown.png", Mono.Unix.Catalog.GetString ("Move Layer Down"), PintaCore.Layers.CurrentLayerIndex, PintaCore.Layers.CurrentLayerIndex - 1);
 
218
 
 
219
                        PintaCore.Layers.MoveCurrentLayerDown ();
 
220
                        PintaCore.History.PushNewItem (hist);
 
221
                }
 
222
 
 
223
                private void HandlePintaCoreActionsLayersMergeLayerDownActivated (object sender, EventArgs e)
 
224
                {
 
225
                        PintaCore.Layers.FinishSelection ();
 
226
 
 
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);
 
230
                        
 
231
                        hist.Push (h1);
 
232
                        hist.Push (h2);
 
233
                        
 
234
                        PintaCore.Layers.MergeCurrentLayerDown ();
 
235
                        
 
236
                        PintaCore.History.PushNewItem (hist);
 
237
                }
 
238
 
 
239
                private void HandlePintaCoreActionsLayersDuplicateLayerActivated (object sender, EventArgs e)
 
240
                {
 
241
                        PintaCore.Layers.FinishSelection ();
 
242
 
 
243
                        Layer l = PintaCore.Layers.DuplicateCurrentLayer ();
 
244
                        
 
245
                        // Make new layer the current layer
 
246
                        PintaCore.Layers.SetCurrentLayer (l);
 
247
 
 
248
                        AddLayerHistoryItem hist = new AddLayerHistoryItem ("Menu.Layers.DuplicateLayer.png", Mono.Unix.Catalog.GetString ("Duplicate Layer"), PintaCore.Layers.IndexOf (l));
 
249
                        PintaCore.History.PushNewItem (hist);
 
250
                }
 
251
 
 
252
                private void HandlePintaCoreActionsLayersDeleteLayerActivated (object sender, EventArgs e)
 
253
                {
 
254
                        PintaCore.Layers.FinishSelection ();
 
255
 
 
256
                        DeleteLayerHistoryItem hist = new DeleteLayerHistoryItem ("Menu.Layers.DeleteLayer.png", Mono.Unix.Catalog.GetString ("Delete Layer"), PintaCore.Layers.CurrentLayer, PintaCore.Layers.CurrentLayerIndex);
 
257
 
 
258
                        PintaCore.Layers.DeleteLayer (PintaCore.Layers.CurrentLayerIndex, false);
 
259
 
 
260
                        PintaCore.History.PushNewItem (hist);
 
261
                }
 
262
 
 
263
                private void HandlePintaCoreActionsLayersAddNewLayerActivated (object sender, EventArgs e)
 
264
                {
 
265
                        PintaCore.Layers.FinishSelection ();
 
266
 
 
267
                        Layer l = PintaCore.Layers.AddNewLayer (string.Empty);
 
268
 
 
269
                        // Make new layer the current layer
 
270
                        PintaCore.Layers.SetCurrentLayer (l);
 
271
 
 
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);
 
274
                }
 
275
                #endregion
 
276
        }
 
277
}