File: SizeGrip.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (293 lines) | stat: -rw-r--r-- 8,185 bytes parent folder | download | duplicates (11)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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.
//
// Copyright (c) 2005 Novell, Inc.
//
// Authors:
//	Jackson Harper (jackson@ximian.com)

using System;
using System.Drawing;

namespace System.Windows.Forms {

	internal class SizeGrip : Control {
		#region Local Variables
		private Point	capture_point;
		private Control captured_control;
		private int	window_w;
		private int	window_h;
		private bool	hide_pending;
		private bool	captured;
		private bool	is_virtual; // If virtual the size grip is painted directly on the captured controls' surface.
		private bool	enabled;
		private bool	fill_background;
		private Rectangle	last_painted_area; // The last area that was painted (to know which area to invalidate when resizing).
		#endregion	// Local Variables

		#region Constructors
		public SizeGrip (Control CapturedControl)
		{
			this.Cursor = Cursors.SizeNWSE;
			enabled = true;
			fill_background = true;
			this.Size = GetDefaultSize ();
			this.CapturedControl = CapturedControl;
		}
		#endregion	// Constructors

		#region Properties
		public bool FillBackground {
			get {
				return fill_background;
			}
			set {
				fill_background = value;
			}
		}
		
		public bool Virtual {
			get { 
				return is_virtual;
			}
			set {
				if (is_virtual == value)	
					return;
					
				is_virtual = value;
				if (is_virtual) {
					CapturedControl.MouseMove += new MouseEventHandler(HandleMouseMove);
					CapturedControl.MouseUp += new MouseEventHandler(HandleMouseUp);
					CapturedControl.MouseDown += new MouseEventHandler(HandleMouseDown);
					CapturedControl.EnabledChanged += new EventHandler(HandleEnabledChanged);
					CapturedControl.Resize += new EventHandler(HandleResize);
				} else {
					CapturedControl.MouseMove -= new MouseEventHandler (HandleMouseMove);
					CapturedControl.MouseUp -= new MouseEventHandler (HandleMouseUp);
					CapturedControl.MouseDown -= new MouseEventHandler (HandleMouseDown);
					CapturedControl.EnabledChanged -= new EventHandler (HandleEnabledChanged);
					CapturedControl.Resize -= new EventHandler (HandleResize);
				}
			}
		}
		
		public Control CapturedControl {
			get {
				return captured_control;
			}
			set {
				captured_control = value;
			}
		}
		
		#endregion	// Properties

		#region Methods
		static internal Size GetDefaultSize () {
			return new Size (SystemInformation.VerticalScrollBarWidth, SystemInformation.HorizontalScrollBarHeight);
		}
		
		static internal Rectangle GetDefaultRectangle (Control Parent)
		{
			Size size = GetDefaultSize ();
			return new Rectangle (Parent.ClientSize.Width - size.Width, Parent.ClientSize.Height - size.Height, size.Width, size.Height);
		}
		
		private void HandleResize (object sender, EventArgs e)
		{
			Control ctrl = (Control) sender;
			ctrl.Invalidate (last_painted_area);
		}

		private void HandleEnabledChanged (object sender, EventArgs e)
		{
			Control ctrl = (Control) sender;
			enabled = ctrl.Enabled;
			Cursor cursor;
			if (enabled) {
				cursor = Cursors.SizeNWSE;
			} else {
				cursor = Cursors.Default;
			}
			if (is_virtual) {
				if (CapturedControl != null)
					CapturedControl.Cursor = cursor;
			} else {
				this.Cursor = cursor;
			}
			ctrl.Invalidate (GetDefaultRectangle (ctrl));
			
		}

		// This method needs to be internal, since the captured control must be able to call
		// it. We can't use events to hook it up, since then the paint ordering won't be correct
		internal void HandlePaint (object sender, PaintEventArgs e)
		{
			if (Visible) {
				Control destination = (Control) sender;
				Graphics gr = e.Graphics;
				Rectangle rect = GetDefaultRectangle (destination);
			
				if (!is_virtual || fill_background) {
					gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorControl), rect);
				}
				if (enabled) {
					ControlPaint.DrawSizeGrip (gr, BackColor, rect);
				}
				last_painted_area = rect;
			}
		}

		private void HandleMouseCaptureChanged (object sender, EventArgs e)
		{
			Control ctrl = (Control) sender;
			if (captured && !ctrl.Capture) {
				captured = false;
				CapturedControl.Size = new Size (window_w, window_h);
			}
		}

		internal void HandleMouseDown (object sender, MouseEventArgs e)
		{
			if (enabled) {
				Control ctrl = (Control)sender;
				if (!GetDefaultRectangle (ctrl).Contains (e.X, e.Y)) {
					return;
				}
				
				ctrl.Capture = true;
				captured = true;
				capture_point = Control.MousePosition;

				window_w = CapturedControl.Width;
				window_h = CapturedControl.Height;
			}
		}

		internal void HandleMouseMove (object sender, MouseEventArgs e)
		{
			Control ctrl = (Control) sender;
			Rectangle rect = GetDefaultRectangle (ctrl);
			
			if (rect.Contains (e.X, e.Y)) {
				ctrl.Cursor = Cursors.SizeNWSE;
			} else {
				ctrl.Cursor = Cursors.Default;
			}
			
			if (captured) {
				int	delta_x;
				int	delta_y;
				Point	current_point;

				current_point = Control.MousePosition;

				delta_x = current_point.X - capture_point.X;
				delta_y = current_point.Y - capture_point.Y;

				Control parent = CapturedControl;
				Form form_parent = parent as Form;
				Size new_size = new Size (window_w + delta_x, window_h + delta_y);
				Size max_size = form_parent != null ? form_parent.MaximumSize : Size.Empty;
				Size min_size = form_parent != null ? form_parent.MinimumSize : Size.Empty;
				
				if (new_size.Width > max_size.Width && max_size.Width > 0)
					new_size.Width = max_size.Width;
				else if (new_size.Width < min_size.Width)
					new_size.Width = min_size.Width;
				
				if (new_size.Height > max_size.Height && max_size.Height > 0)
					new_size.Height = max_size.Height;
				else if (new_size.Height < min_size.Height)
					new_size.Height = min_size.Height;

				if (new_size != parent.Size) {
					parent.Size = new_size;
				}
			}
		}

		internal void HandleMouseUp (object sender, MouseEventArgs e)
		{
			if (captured) {
				Control ctrl = (Control) sender;
				captured = false;
				ctrl.Capture = false;
				ctrl.Invalidate (last_painted_area);
				
				if (Parent is ScrollableControl) {
					((ScrollableControl)Parent).UpdateSizeGripVisible ();
				}
				if (hide_pending) {
					Hide();
					hide_pending = false;
				}
			}
		}


		protected override void SetVisibleCore(bool value) {
			if (Capture) {
				if (value == false) {
					hide_pending = true;
				} else {
					hide_pending = false;
				}
				return;
			}
			base.SetVisibleCore (value);
		}

		protected override void OnPaint (PaintEventArgs pe)
		{
			HandlePaint (this, pe);
			base.OnPaint (pe);
		}

		protected override void OnMouseCaptureChanged (EventArgs e)
		{
			base.OnMouseCaptureChanged (e);
			HandleMouseCaptureChanged (this, e);
		}

		protected override void OnEnabledChanged (EventArgs e)
		{
			base.OnEnabledChanged (e);
			HandleEnabledChanged (this, e);
		}
		
		protected override void OnMouseDown (MouseEventArgs e)
		{
			HandleMouseDown (this, e);
		}
		
		protected override void OnMouseMove(MouseEventArgs e)
		{
			HandleMouseMove (this, e);
		}

		protected override void OnMouseUp (MouseEventArgs e)
		{
			HandleMouseUp (this, e);
		}
		#endregion	// Methods
	}
}