-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImageWidget.cs
236 lines (210 loc) · 7.06 KB
/
ImageWidget.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
using System;
using System.Collections;
using Gtk;
using Gdk;
using Mono.Unix;
namespace Tomboy.InsertImage
{
public class ImageWidget : EventBox
{
Gtk.Image child;
int width, height;
int difX, difY;
bool resizingX;
bool resizingY;
int oldChildWidth;
int oldChildHeight;
Pixbuf originalPixbuf = null;
Cursor cursorX = new Cursor (CursorType.RightSide);
Cursor cursorY = new Cursor (CursorType.BottomSide);
Cursor cursorXY = new Cursor (CursorType.BottomRightCorner);
Cursor cursorNormal = new Cursor (CursorType.Arrow);
Gtk.Menu contextMenu = null;
Gdk.Size imageSize;
internal bool AllowResize = true;
public const int SelectionBorder = 8;
public const int MinWidth = 12;
public const int MinHeight = 12;
public ImageWidget (Pixbuf pixbuf)
{
resizingX = resizingY = false;
this.CanFocus = true;
this.Events = EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.PointerMotionMask;
this.child = new Gtk.Image ();
this.child.Pixbuf = pixbuf;
originalPixbuf = pixbuf;
Add (child);
imageSize = child.Allocation.Size;
}
public Gtk.Menu ContextMenu
{
get
{
if (contextMenu == null) {
contextMenu = new Menu ();
var accel_group = new Gtk.AccelGroup ();
contextMenu.AccelGroup = accel_group;
Gtk.ImageMenuItem resumeSize = new Gtk.ImageMenuItem (
Catalog.GetString ("Resume Size"));
resumeSize.Image = new Gtk.Image (Gtk.Stock.Zoom100, Gtk.IconSize.Menu);
resumeSize.Activated += (o, e) => ResumeImageSize ();
//resumeSize.AddAccelerator ("activate",
// accel_group,
// (uint) Gdk.Key.r,
// Gdk.ModifierType.ControlMask,
// Gtk.AccelFlags.Visible);
contextMenu.Append (resumeSize);
}
return contextMenu;
}
}
public Gdk.Size ImageSize
{
get
{
return imageSize;
}
}
public event EventHandler<ResizeEventArgs> Resized;
public void ResumeImageSize ()
{
int oldWidth = child.Allocation.Width;
int oldHeight = child.Allocation.Height;
ResizeImage (originalPixbuf.Width, originalPixbuf.Height);
OnResized (oldWidth, oldHeight, originalPixbuf.Width, originalPixbuf.Height);
}
public void ResizeImage (int width, int height)
{
ResizeImage (width, height, InterpType.Bilinear);
}
public void ResizeImage (int width, int height, InterpType interType)
{
if (width <= 0 || height <= 0)
return;
if (width < MinWidth)
width = MinWidth;
if (height < MinHeight)
height = MinHeight;
if (width == originalPixbuf.Width && height == originalPixbuf.Height)
child.Pixbuf = originalPixbuf;
else
child.Pixbuf = originalPixbuf.ScaleSimple (width, height, interType);
child.SetSizeRequest (width, height);
imageSize.Width = width;
imageSize.Height = height;
//QueueDraw ();
}
private void OnResized (int oldWidth, int oldHeight, int newWidth, int newHeight)
{
if (Resized != null && ((oldWidth != newWidth) || (oldHeight != newHeight))) {
ResizeEventArgs arg = new ResizeEventArgs (oldWidth, oldHeight, newWidth, newHeight);
Resized (this, arg);
}
}
protected override void OnDestroyed ()
{
if (cursorX != null) {
cursorX.Dispose ();
cursorXY.Dispose ();
cursorY.Dispose ();
cursorX = cursorXY = cursorY = null;
}
base.OnDestroyed ();
}
protected override bool OnMotionNotifyEvent (Gdk.EventMotion ev)
{
if (resizingX || resizingY) {
int newWidth = resizingX ? (int)(ev.X + difX) : oldChildWidth;
int newHeight = resizingY ? (int)(ev.Y + difY) : oldChildHeight;
if (resizingX && resizingY) {
//Keep the aspect ratio.
double widthRatio = (double)newWidth / (double)oldChildWidth;
double heightRatio = (double)newHeight / (double)oldChildHeight;
if (widthRatio > heightRatio)
newHeight = (int) (oldChildHeight * widthRatio);
else
newWidth = (int) (oldChildWidth * heightRatio);
}
ResizeImage (newWidth, newHeight, InterpType.Nearest);
} else if (AllowResize) {
if (GetAreaResizeXY ().Contains ((int)ev.X, (int)ev.Y))
GdkWindow.Cursor = cursorXY;
else if (GetAreaResizeX ().Contains ((int)ev.X, (int)ev.Y))
GdkWindow.Cursor = cursorX;
else if (GetAreaResizeY ().Contains ((int)ev.X, (int)ev.Y))
GdkWindow.Cursor = cursorY;
else
GdkWindow.Cursor = cursorNormal;
}
return base.OnMotionNotifyEvent (ev);
}
protected override bool OnButtonPressEvent (Gdk.EventButton ev)
{
Gdk.Rectangle rectArea = child.Allocation;
if (ev.Button == 1) {
if (AllowResize) {
// Left button click
Rectangle rect = GetAreaResizeXY ();
if (rect.Contains ((int)ev.X, (int)ev.Y)) {
resizingX = resizingY = true;
difX = (int)(rectArea.Width - ev.X);
difY = (int)(rectArea.Height - ev.Y);
GdkWindow.Cursor = cursorXY;
} else if (GetAreaResizeY ().Contains ((int)ev.X, (int)ev.Y)) {
resizingY = true;
difY = (int)(rectArea.Height - ev.Y);
width = rectArea.Width;
GdkWindow.Cursor = cursorY;
} else if (GetAreaResizeX ().Contains ((int)ev.X, (int)ev.Y)) {
resizingX = true;
difX = (int)(rectArea.Width - ev.X);
height = rectArea.Height;
GdkWindow.Cursor = cursorX;
}
if (resizingX || resizingY) {
oldChildWidth = child.Allocation.Width;
oldChildHeight = child.Allocation.Height;
}
}
}
return base.OnButtonPressEvent (ev);
}
Rectangle GetAreaResizeY ()
{
Gdk.Rectangle rect = child.Allocation;
return new Gdk.Rectangle (rect.X, rect.Y + rect.Height - SelectionBorder, rect.Width - SelectionBorder, SelectionBorder);
}
Rectangle GetAreaResizeX ()
{
Gdk.Rectangle rect = child.Allocation;
return new Gdk.Rectangle (rect.X + rect.Width - SelectionBorder, rect.Y, SelectionBorder, rect.Height - SelectionBorder);
}
Rectangle GetAreaResizeXY ()
{
Gdk.Rectangle rect = child.Allocation;
return new Gdk.Rectangle (rect.X + rect.Width - SelectionBorder, rect.Y + rect.Height - SelectionBorder, SelectionBorder, SelectionBorder);
}
protected override bool OnButtonReleaseEvent (Gdk.EventButton ev)
{
if (AllowResize) {
if (ev.Button == 1 && (resizingX || resizingY)) {
resizingX = resizingY = false;
GdkWindow.Cursor = cursorNormal;
int newWidth = child.Allocation.Width;
int newHeight = child.Allocation.Height;
ResizeImage (newWidth, newHeight);
OnResized (oldChildWidth, oldChildHeight, newWidth, newHeight);
} else if (ev.Button == 3) {
ContextMenu.Popup ();
}
//} else if (ev.Button != 1) {
// int oldWidth = child.Allocation.Width;
// int oldHeight = child.Allocation.Height;
// ResumeImageSize ();
// OnResized (oldWidth, oldHeight, originalPixbuf.Width, originalPixbuf.Height);
//}
}
return base.OnButtonReleaseEvent (ev);
}
}
}