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 EditActions
35
public Gtk.Action Undo { get; private set; }
36
public Gtk.Action Redo { get; private set; }
37
public Gtk.Action Cut { get; private set; }
38
public Gtk.Action Copy { get; private set; }
39
public Gtk.Action Paste { get; private set; }
40
public Gtk.Action PasteIntoNewLayer { get; private set; }
41
public Gtk.Action PasteIntoNewImage { get; private set; }
42
public Gtk.Action EraseSelection { get; private set; }
43
public Gtk.Action FillSelection { get; private set; }
44
public Gtk.Action InvertSelection { get; private set; }
45
public Gtk.Action SelectAll { get; private set; }
46
public Gtk.Action Deselect { get; private set; }
50
Gtk.IconFactory fact = new Gtk.IconFactory ();
51
fact.Add ("Menu.Edit.Deselect.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Edit.Deselect.png")));
52
fact.Add ("Menu.Edit.EraseSelection.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Edit.EraseSelection.png")));
53
fact.Add ("Menu.Edit.FillSelection.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Edit.FillSelection.png")));
54
fact.Add ("Menu.Edit.InvertSelection.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Edit.InvertSelection.png")));
55
fact.Add ("Menu.Edit.SelectAll.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Menu.Edit.SelectAll.png")));
58
Undo = new Gtk.Action ("Undo", Mono.Unix.Catalog.GetString ("Undo"), null, "gtk-undo");
59
Redo = new Gtk.Action ("Redo", Mono.Unix.Catalog.GetString ("Redo"), null, "gtk-redo");
60
Cut = new Gtk.Action ("Cut", Mono.Unix.Catalog.GetString ("Cut"), null, "gtk-cut");
61
Copy = new Gtk.Action ("Copy", Mono.Unix.Catalog.GetString ("Copy"), null, "gtk-copy");
62
Paste = new Gtk.Action ("Paste", Mono.Unix.Catalog.GetString ("Paste"), null, "gtk-paste");
63
PasteIntoNewLayer = new Gtk.Action ("PasteIntoNewLayer", Mono.Unix.Catalog.GetString ("Paste Into New Layer"), null, "gtk-paste");
64
PasteIntoNewImage = new Gtk.Action ("PasteIntoNewImage", Mono.Unix.Catalog.GetString ("Paste Into New Image"), null, "gtk-paste");
65
EraseSelection = new Gtk.Action ("EraseSelection", Mono.Unix.Catalog.GetString ("Erase Selection"), null, "Menu.Edit.EraseSelection.png");
66
FillSelection = new Gtk.Action ("FillSelection", Mono.Unix.Catalog.GetString ("Fill Selection"), null, "Menu.Edit.FillSelection.png");
67
InvertSelection = new Gtk.Action ("InvertSelection", Mono.Unix.Catalog.GetString ("Invert Selection"), null, "Menu.Edit.InvertSelection.png");
68
SelectAll = new Gtk.Action ("SelectAll", Mono.Unix.Catalog.GetString ("Select All"), null, "Menu.Edit.SelectAll.png");
69
Deselect = new Gtk.Action ("Deselect", Mono.Unix.Catalog.GetString ("Deselect"), null, "Menu.Edit.Deselect.png");
71
Undo.Sensitive = false;
72
Redo.Sensitive = false;
73
PasteIntoNewImage.Sensitive = false;
74
InvertSelection.Sensitive = false;
75
Deselect.Sensitive = false;
78
#region Initialization
79
public void CreateMainMenu (Gtk.Menu menu)
81
menu.Remove (menu.Children[1]);
83
menu.Append (Undo.CreateAcceleratedMenuItem (Gdk.Key.Z, Gdk.ModifierType.ControlMask));
84
menu.Append (Redo.CreateAcceleratedMenuItem (Gdk.Key.Y, Gdk.ModifierType.ControlMask));
85
menu.AppendSeparator ();
86
menu.Append (Cut.CreateAcceleratedMenuItem (Gdk.Key.X, Gdk.ModifierType.ControlMask));
87
menu.Append (Copy.CreateAcceleratedMenuItem (Gdk.Key.C, Gdk.ModifierType.ControlMask));
88
menu.Append (Paste.CreateAcceleratedMenuItem (Gdk.Key.V, Gdk.ModifierType.ControlMask));
89
menu.Append (PasteIntoNewLayer.CreateAcceleratedMenuItem (Gdk.Key.V, Gdk.ModifierType.ShiftMask));
90
//menu.Append (PasteIntoNewImage.CreateAcceleratedMenuItem (Gdk.Key.V, Gdk.ModifierType.Mod1Mask));
91
menu.AppendSeparator ();
92
menu.Append (EraseSelection.CreateAcceleratedMenuItem (Gdk.Key.Delete, Gdk.ModifierType.None));
93
menu.Append (FillSelection.CreateAcceleratedMenuItem (Gdk.Key.BackSpace, Gdk.ModifierType.None));
94
//menu.Append (InvertSelection.CreateAcceleratedMenuItem (Gdk.Key.I, Gdk.ModifierType.ControlMask));
95
menu.Append (SelectAll.CreateAcceleratedMenuItem (Gdk.Key.A, Gdk.ModifierType.ControlMask));
96
menu.Append (Deselect.CreateAcceleratedMenuItem (Gdk.Key.D, Gdk.ModifierType.ControlMask));
99
public void CreateHistoryWindowToolBar (Gtk.Toolbar toolbar)
101
toolbar.AppendItem (Undo.CreateToolBarItem ());
102
toolbar.AppendItem (Redo.CreateToolBarItem ());
105
public void RegisterHandlers ()
107
Deselect.Activated += HandlePintaCoreActionsEditDeselectActivated;
108
EraseSelection.Activated += HandlePintaCoreActionsEditEraseSelectionActivated;
109
SelectAll.Activated += HandlePintaCoreActionsEditSelectAllActivated;
110
FillSelection.Activated += HandlePintaCoreActionsEditFillSelectionActivated;
111
PasteIntoNewLayer.Activated += HandlerPintaCoreActionsEditPasteIntoNewLayerActivated;
112
Paste.Activated += HandlerPintaCoreActionsEditPasteActivated;
113
Copy.Activated += HandlerPintaCoreActionsEditCopyActivated;
114
Undo.Activated += HandlerPintaCoreActionsEditUndoActivated;
115
Redo.Activated += HandlerPintaCoreActionsEditRedoActivated;
116
Cut.Activated += HandlerPintaCoreActionsEditCutActivated;
120
#region Action Handlers
121
private void HandlePintaCoreActionsEditFillSelectionActivated (object sender, EventArgs e)
123
PintaCore.Layers.FinishSelection ();
125
Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone ();
127
using (Cairo.Context g = new Cairo.Context (PintaCore.Layers.CurrentLayer.Surface)) {
128
g.AppendPath (PintaCore.Layers.SelectionPath);
129
g.FillRule = Cairo.FillRule.EvenOdd;
130
g.Color = PintaCore.Palette.PrimaryColor;
134
PintaCore.Workspace.Invalidate ();
135
PintaCore.History.PushNewItem (new SimpleHistoryItem ("Menu.Edit.FillSelection.png", Mono.Unix.Catalog.GetString ("Fill Selection"), old, PintaCore.Layers.CurrentLayerIndex));
138
private void HandlePintaCoreActionsEditSelectAllActivated (object sender, EventArgs e)
140
PintaCore.Layers.FinishSelection ();
142
SelectionHistoryItem hist = new SelectionHistoryItem ("Menu.Edit.SelectAll.png", Mono.Unix.Catalog.GetString ("Select All"));
143
hist.TakeSnapshot ();
145
PintaCore.Layers.ResetSelectionPath ();
146
PintaCore.Layers.ShowSelection = true;
148
PintaCore.History.PushNewItem (hist);
149
PintaCore.Workspace.Invalidate ();
152
private void HandlePintaCoreActionsEditEraseSelectionActivated (object sender, EventArgs e)
154
PintaCore.Layers.FinishSelection ();
156
Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone ();
158
using (Cairo.Context g = new Cairo.Context (PintaCore.Layers.CurrentLayer.Surface)) {
159
g.AppendPath (PintaCore.Layers.SelectionPath);
160
g.FillRule = Cairo.FillRule.EvenOdd;
161
g.Operator = Cairo.Operator.Clear;
165
PintaCore.Workspace.Invalidate ();
166
PintaCore.History.PushNewItem (new SimpleHistoryItem ("Menu.Edit.EraseSelection.png", Mono.Unix.Catalog.GetString ("Erase Selection"), old, PintaCore.Layers.CurrentLayerIndex));
169
private void HandlePintaCoreActionsEditDeselectActivated (object sender, EventArgs e)
171
PintaCore.Layers.FinishSelection ();
173
SelectionHistoryItem hist = new SelectionHistoryItem ("Menu.Edit.Deselect.png", Mono.Unix.Catalog.GetString ("Deselect"));
174
hist.TakeSnapshot ();
176
PintaCore.Layers.ResetSelectionPath ();
178
PintaCore.History.PushNewItem (hist);
179
PintaCore.Workspace.Invalidate ();
182
private void HandlerPintaCoreActionsEditPasteIntoNewLayerActivated (object sender, EventArgs e)
184
PintaCore.Layers.FinishSelection ();
186
Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
187
Gdk.Pixbuf image = cb.WaitForImage ();
189
// TODO: Message window saying no image on clipboard
193
Layer l = PintaCore.Layers.AddNewLayer (string.Empty);
195
using (Cairo.Context g = new Cairo.Context (l.Surface))
196
g.DrawPixbuf (image, new Cairo.Point (0, 0));
198
// Make new layer the current layer
199
PintaCore.Layers.SetCurrentLayer (l);
201
PintaCore.Workspace.Invalidate ();
203
// TODO: Need paste icon
204
AddLayerHistoryItem hist = new AddLayerHistoryItem ("Menu.Edit.EraseSelection.png", Mono.Unix.Catalog.GetString ("Paste Into New Layer"), PintaCore.Layers.IndexOf (l));
205
PintaCore.History.PushNewItem (hist);
208
private void HandlerPintaCoreActionsEditPasteActivated (object sender, EventArgs e)
210
PintaCore.Layers.FinishSelection ();
212
Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone ();
214
Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
215
Gdk.Pixbuf image = cb.WaitForImage ();
222
using (Cairo.Context g = new Cairo.Context (PintaCore.Layers.CurrentLayer.Surface)) {
223
g.DrawPixbuf (image, new Cairo.Point (0, 0));
224
p = g.CreateRectanglePath (new Rectangle (0, 0, image.Width, image.Height));
227
PintaCore.Layers.SelectionPath = p;
228
PintaCore.Layers.ShowSelection = true;
230
PintaCore.Workspace.Invalidate ();
232
// TODO: Need paste icon
233
PintaCore.History.PushNewItem (new SimpleHistoryItem ("Menu.Edit.EraseSelection.png", Mono.Unix.Catalog.GetString ("Paste"), old, PintaCore.Layers.CurrentLayerIndex));
236
private void HandlerPintaCoreActionsEditCopyActivated (object sender, EventArgs e)
238
PintaCore.Layers.FinishSelection ();
240
ImageSurface src = PintaCore.Layers.GetClippedLayer (PintaCore.Layers.CurrentLayerIndex);
242
Cairo.Rectangle rect = PintaCore.Layers.SelectionPath.GetBounds ();
244
ImageSurface dest = new ImageSurface (Format.Argb32, (int)rect.Width, (int)rect.Height);
246
using (Context g = new Context (dest)) {
247
g.SetSourceSurface (src, -(int)rect.X, -(int)rect.Y);
251
Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
252
cb.Image = dest.ToPixbuf ();
254
(src as IDisposable).Dispose ();
255
(dest as IDisposable).Dispose ();
258
private void HandlerPintaCoreActionsEditCutActivated (object sender, EventArgs e)
260
PintaCore.Layers.FinishSelection ();
263
HandlerPintaCoreActionsEditCopyActivated (sender, e);
266
Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone ();
268
using (Cairo.Context g = new Cairo.Context (PintaCore.Layers.CurrentLayer.Surface)) {
269
g.AppendPath (PintaCore.Layers.SelectionPath);
270
g.FillRule = Cairo.FillRule.EvenOdd;
271
g.Operator = Cairo.Operator.Clear;
275
PintaCore.Workspace.Invalidate ();
276
PintaCore.History.PushNewItem (new SimpleHistoryItem ("Menu.Edit.EraseSelection.png", Mono.Unix.Catalog.GetString ("Cut"), old, PintaCore.Layers.CurrentLayerIndex));
279
private void HandlerPintaCoreActionsEditUndoActivated (object sender, EventArgs e)
281
PintaCore.History.Undo ();
284
private void HandlerPintaCoreActionsEditRedoActivated (object sender, EventArgs e)
286
PintaCore.History.Redo ();