/
BaseTool.cs
276 lines (227 loc) · 8.58 KB
/
BaseTool.cs
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
41
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
109
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
174
175
176
177
178
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
207
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
240
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
//
// BaseTool.cs
//
// Author:
// Jonathan Pobst <monkey@jpobst.com>
//
// Copyright (c) 2010 Jonathan Pobst
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Cairo;
using Gtk;
using System.IO;
using Mono.Unix;
using Mono.Addins;
namespace Pinta.Core
{
public delegate void MouseHandler (double x, double y, Gdk.ModifierType state);
[TypeExtensionPoint]
public abstract class BaseTool
{
protected const int DEFAULT_BRUSH_WIDTH = 2;
protected static Point point_empty = new Point (-500, -500);
protected ToggleToolButton tool_item;
protected ToolItem tool_label;
protected ToolItem tool_image;
protected ToolItem tool_sep;
protected ToolBarDropDownButton antialiasing_button;
protected ToolBarDropDownButton alphablending_button;
public event MouseHandler MouseMoved;
public event MouseHandler MousePressed;
public event MouseHandler MouseReleased;
protected BaseTool ()
{
}
static BaseTool ()
{
Gtk.IconFactory fact = new Gtk.IconFactory ();
fact.Add ("Toolbar.AntiAliasingEnabledIcon.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Toolbar.AntiAliasingEnabledIcon.png")));
fact.Add ("Toolbar.AntiAliasingDisabledIcon.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Toolbar.AntiAliasingDisabledIcon.png")));
fact.Add ("Toolbar.BlendingEnabledIcon.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Toolbar.BlendingEnabledIcon.png")));
fact.Add ("Toolbar.BlendingOverwriteIcon.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Toolbar.BlendingOverwriteIcon.png")));
fact.Add ("Tools.FreeformShape.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("Tools.FreeformShape.png")));
fact.AddDefault ();
}
public virtual string Name { get { throw new ApplicationException ("Tool didn't override Name"); } }
public virtual string Icon { get { throw new ApplicationException ("Tool didn't override Icon"); } }
public virtual string ToolTip { get { throw new ApplicationException ("Tool didn't override ToolTip"); } }
public virtual string StatusBarText { get { return string.Empty; } }
public virtual ToggleToolButton ToolItem { get { if (tool_item == null) tool_item = CreateToolButton (); return tool_item; } }
public virtual bool Enabled { get { return true; } }
public virtual Gdk.Cursor DefaultCursor { get { return null; } }
public virtual Gdk.Key ShortcutKey { get { return (Gdk.Key)0; } }
public virtual bool UseAntialiasing { get { return ShowAntialiasingButton && (bool)antialiasing_button.SelectedItem.Tag; } }
public virtual bool UseAlphaBlending { get { return ShowAlphaBlendingButton && (bool)alphablending_button.SelectedItem.Tag; } }
public virtual int Priority { get { return 75; } }
protected virtual bool ShowAntialiasingButton { get { return false; } }
protected virtual bool ShowAlphaBlendingButton { get { return false; } }
#region Public Methods
public void DoMouseMove (object o, MotionNotifyEventArgs args, Cairo.PointD point)
{
if (MouseMoved != null)
MouseMoved (point.X, point.Y, args.Event.State);
OnMouseMove (o, args, point);
}
public void DoBuildToolBar (Toolbar tb)
{
OnBuildToolBar (tb);
BuildRasterizationToolItems (tb);
}
public void DoClearToolBar (Toolbar tb)
{
OnClearToolBar (tb);
}
public void DoMouseDown (DrawingArea canvas, ButtonPressEventArgs args, Cairo.PointD point)
{
if (MousePressed != null)
MousePressed (point.X, point.Y, args.Event.State);
OnMouseDown (canvas, args, point);
}
public void DoMouseUp (DrawingArea canvas, ButtonReleaseEventArgs args, Cairo.PointD point)
{
if (MouseReleased != null)
MouseReleased (point.X, point.Y, args.Event.State);
OnMouseUp (canvas, args, point);
}
public void DoCommit ()
{
OnCommit ();
}
public void DoActivated ()
{
OnActivated ();
}
public void DoDeactivated ()
{
OnDeactivated ();
}
// Return true if the key was consumed.
public void DoKeyPress (DrawingArea canvas, KeyPressEventArgs args)
{
OnKeyDown (canvas, args);
}
public void DoKeyRelease (DrawingArea canvas, KeyReleaseEventArgs args)
{
OnKeyUp (canvas, args);
}
#endregion
#region Protected Methods
protected virtual void OnMouseMove (object o, Gtk.MotionNotifyEventArgs args, Cairo.PointD point)
{
}
protected virtual void BuildRasterizationToolItems (Toolbar tb)
{
if (ShowAlphaBlendingButton || ShowAntialiasingButton)
tb.AppendItem (new SeparatorToolItem ());
if (ShowAntialiasingButton)
BuildAntialiasingTool (tb);
if (ShowAlphaBlendingButton)
BuildAlphaBlending (tb);
}
protected virtual void OnBuildToolBar (Toolbar tb)
{
if (tool_label == null)
tool_label = new ToolBarLabel (string.Format (" {0}: ", Catalog.GetString ("Tool")));
tb.AppendItem (tool_label);
if (tool_image == null)
tool_image = new ToolBarImage (Icon);
tb.AppendItem (tool_image);
if (tool_sep == null)
tool_sep = new SeparatorToolItem ();
tb.AppendItem (tool_sep);
}
protected virtual void OnClearToolBar (Toolbar tb)
{
while (tb.NItems > 0)
tb.Remove (tb.Children[tb.NItems - 1]);
}
protected virtual void OnMouseDown (DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
{
}
protected virtual void OnMouseUp (DrawingArea canvas, Gtk.ButtonReleaseEventArgs args, Cairo.PointD point)
{
}
protected virtual void OnKeyDown (DrawingArea canvas, Gtk.KeyPressEventArgs args)
{
}
protected virtual void OnKeyUp (DrawingArea canvas, Gtk.KeyReleaseEventArgs args)
{
}
/// <summary>
/// This is called whenever a menu option is called, for
/// tools that are in a temporary state while being used, and
/// need to commit their work when another option is selected.
/// </summary>
protected virtual void OnCommit ()
{
}
protected virtual void OnActivated ()
{
SetCursor (DefaultCursor);
}
protected virtual void OnDeactivated ()
{
SetCursor (null);
}
protected virtual ToggleToolButton CreateToolButton ()
{
Image i2 = new Image (PintaCore.Resources.GetIcon (Icon));
i2.Show ();
ToggleToolButton tool_item = new ToggleToolButton ();
tool_item.IconWidget = i2;
tool_item.Show ();
tool_item.Label = Name;
if (ShortcutKey != (Gdk.Key)0)
tool_item.TooltipText = string.Format ("{0}\n{2}: {1}\n\n{3}", Name, ShortcutKey.ToString ().ToUpperInvariant (), Catalog.GetString ("Shortcut key"), StatusBarText);
else
tool_item.TooltipText = Name;
return tool_item;
}
protected void SetCursor (Gdk.Cursor cursor)
{
PintaCore.Chrome.Canvas.GdkWindow.Cursor = cursor;
}
#endregion
#region Private Methods
private void BuildAlphaBlending (Toolbar tb)
{
if (alphablending_button != null) {
tb.AppendItem (alphablending_button);
return;
}
alphablending_button = new ToolBarDropDownButton ();
alphablending_button.AddItem (Catalog.GetString ("Normal Blending"), "Toolbar.BlendingEnabledIcon.png", true);
alphablending_button.AddItem (Catalog.GetString ("Overwrite"), "Toolbar.BlendingOverwriteIcon.png", false);
tb.AppendItem (alphablending_button);
}
private void BuildAntialiasingTool (Toolbar tb)
{
if (antialiasing_button != null) {
tb.AppendItem (antialiasing_button);
return;
}
antialiasing_button = new ToolBarDropDownButton ();
antialiasing_button.AddItem (Catalog.GetString ("Antialiasing On"), "Toolbar.AntiAliasingEnabledIcon.png", true);
antialiasing_button.AddItem (Catalog.GetString ("Antialiasing Off"), "Toolbar.AntiAliasingDisabledIcon.png", false);
tb.AppendItem (antialiasing_button);
}
#endregion
}
}