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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
// 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) 2004 Novell, Inc.
//
// Authors:
// Peter Bartok pbartok@novell.com
//
// NOT COMPLETE
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
namespace System.Windows.Forms {
[Designer ("System.Windows.Forms.Design.ScrollableControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
public class ScrollableControl : Control {
#region Local Variables
private bool auto_vscroll;
private bool auto_hscroll;
private bool hscroll_visible;
private bool vscroll_visible;
private bool force_hscroll_visible;
private bool force_vscroll_visible;
private bool auto_scroll;
private Size auto_scroll_margin;
private Size auto_scroll_min_size;
private Point scroll_position;
private DockPaddingEdges dock_padding;
private SizeGrip sizegrip;
private HScrollBar hscrollbar;
private VScrollBar vscrollbar;
#endregion // Local Variables
[MonoTODO("Need to use the edge values when performing the layout")]
[TypeConverter(typeof(ScrollableControl.DockPaddingEdgesConverter))]
#region Subclass DockPaddingEdges
public class DockPaddingEdges : ICloneable {
#region DockPaddingEdges Local Variables
private int all;
private int left;
private int right;
private int top;
private int bottom;
private Control owner;
#endregion // DockPaddingEdges Local Variables
#region DockPaddingEdges Constructor
internal DockPaddingEdges(Control owner) {
all = 0;
left = 0;
right = 0;
top = 0;
bottom = 0;
this.owner = owner;
}
#endregion // DockPaddingEdges Constructor
#region DockPaddingEdges Public Instance Properties
[RefreshProperties(RefreshProperties.All)]
public int All {
get {
return all;
}
set {
all = value;
left = value;
right = value;
top = value;
bottom = value;
owner.PerformLayout();
}
}
[RefreshProperties(RefreshProperties.All)]
public int Bottom {
get {
return bottom;
}
set {
bottom = value;
all = 0;
owner.PerformLayout();
}
}
[RefreshProperties(RefreshProperties.All)]
public int Left {
get {
return left;
}
set {
left=value;
all = 0;
owner.PerformLayout();
}
}
[RefreshProperties(RefreshProperties.All)]
public int Right {
get {
return right;
}
set {
right=value;
all = 0;
owner.PerformLayout();
}
}
[RefreshProperties(RefreshProperties.All)]
public int Top {
get {
return top;
}
set {
top=value;
all = 0;
owner.PerformLayout();
}
}
#endregion // DockPaddingEdges Public Instance Properties
// Public Instance Methods
public override bool Equals(object other) {
if (! (other is DockPaddingEdges)) {
return false;
}
if ( (this.all == ((DockPaddingEdges)other).all) && (this.left == ((DockPaddingEdges)other).left) &&
(this.right == ((DockPaddingEdges)other).right) && (this.top == ((DockPaddingEdges)other).top) &&
(this.bottom == ((DockPaddingEdges)other).bottom)) {
return true;
}
return false;
}
public override int GetHashCode() {
return all*top*bottom*right*left;
}
public override string ToString() {
return "All = "+all.ToString()+" Top = "+top.ToString()+" Left = "+left.ToString()+" Bottom = "+bottom.ToString()+" Right = "+right.ToString();
}
object ICloneable.Clone() {
DockPaddingEdges padding_edge;
padding_edge=new DockPaddingEdges(owner);
padding_edge.all=all;
padding_edge.left=left;
padding_edge.right=right;
padding_edge.top=top;
padding_edge.bottom=bottom;
return padding_edge;
}
}
#endregion // Subclass DockPaddingEdges
#region Subclass DockPaddingEdgesConverter
public class DockPaddingEdgesConverter : System.ComponentModel.TypeConverter {
// Public Constructors
public DockPaddingEdgesConverter() {
}
// Public Instance Methods
public override PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, Attribute[] attributes) {
return TypeDescriptor.GetProperties(typeof(DockPaddingEdges), attributes);
}
public override bool GetPropertiesSupported(System.ComponentModel.ITypeDescriptorContext context) {
return true;
}
}
#endregion // Subclass DockPaddingEdgesConverter
#region Public Constructors
public ScrollableControl() {
SetStyle(ControlStyles.ContainerControl, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, false);
auto_scroll = false;
auto_hscroll = false;
auto_vscroll = false;
hscroll_visible = false;
vscroll_visible = false;
force_hscroll_visible = false;
force_vscroll_visible = false;
auto_scroll_margin = new Size(0, 0);
auto_scroll_min_size = new Size(0, 0);
scroll_position = new Point(0, 0);
dock_padding = new DockPaddingEdges(this);
SizeChanged +=new EventHandler(Recalculate);
VisibleChanged += new EventHandler(Recalculate);
}
#endregion // Public Constructors
#region Protected Static Fields
protected const int ScrollStateAutoScrolling = 1;
protected const int ScrollStateFullDrag = 16;
protected const int ScrollStateHScrollVisible = 2;
protected const int ScrollStateUserHasScrolled = 8;
protected const int ScrollStateVScrollVisible = 4;
#endregion // Protected Static Fields
#region Public Instance Properties
[DefaultValue(false)]
[Localizable(true)]
public virtual bool AutoScroll {
get {
return auto_scroll;
}
set {
if (auto_scroll == value) {
return;
}
auto_scroll = value;
if (!auto_scroll) {
SuspendLayout ();
Controls.RemoveImplicit (hscrollbar);
hscrollbar.Dispose();
hscrollbar = null;
hscroll_visible = false;
Controls.RemoveImplicit (vscrollbar);
vscrollbar.Dispose();
vscrollbar = null;
vscroll_visible = false;
Controls.RemoveImplicit (sizegrip);
sizegrip.Dispose();
sizegrip = null;
ResumeLayout ();
} else {
SuspendLayout ();
hscrollbar = new HScrollBar();
hscrollbar.Visible = false;
hscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
this.Controls.AddImplicit (hscrollbar);
vscrollbar = new VScrollBar();
vscrollbar.Visible = false;
vscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
this.Controls.AddImplicit (vscrollbar);
sizegrip = new SizeGrip();
sizegrip.Visible = false;
this.Controls.AddImplicit (sizegrip);
ResumeLayout ();
}
}
}
[Localizable(true)]
public Size AutoScrollMargin {
get {
return auto_scroll_margin;
}
set {
if (value.Width < 0) {
throw new ArgumentException("Width is assigned less than 0", "value.Width");
}
if (value.Height < 0) {
throw new ArgumentException("Height is assigned less than 0", "value.Height");
}
auto_scroll_margin = value;
}
}
[Localizable(true)]
public Size AutoScrollMinSize {
get {
return auto_scroll_min_size;
}
set {
auto_scroll_min_size = value;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Point AutoScrollPosition {
get {
return new Point(-scroll_position.X, -scroll_position.Y);
}
set {
if ((value.X != scroll_position.X) || (value.Y != scroll_position.Y)) {
int shift_x;
int shift_y;
shift_x = 0;
shift_y = 0;
if (hscroll_visible) {
shift_x = value.X - scroll_position.X;
}
if (vscroll_visible) {
shift_y = value.Y - scroll_position.Y;
}
ScrollWindow(shift_x, shift_y);
if (hscroll_visible) {
hscrollbar.Value = scroll_position.X;
}
if (vscroll_visible) {
vscrollbar.Value = scroll_position.Y;
}
}
}
}
public override Rectangle DisplayRectangle {
get {
Rectangle rect;
rect = base.DisplayRectangle;
if (vscroll_visible) {
rect.Width -= vscrollbar.Width;
if (rect.Width < 0) {
rect.Width = 0;
}
}
if (hscroll_visible) {
rect.Height -= hscrollbar.Height;
if (rect.Height < 0) {
rect.Height = 0;
}
}
rect.X += dock_padding.Left;
rect.Y += dock_padding.Top;
rect.Width -= dock_padding.Right + dock_padding.Left;
rect.Height -= dock_padding.Bottom + dock_padding.Top;
return rect;
//return new Rectangle(-scroll_position.X, -scroll_position.Y, auto_scroll_min_size.Width, auto_scroll_min_size.Height);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
public DockPaddingEdges DockPadding {
get {
return dock_padding;
}
}
#endregion // Public Instance Properties
#region Protected Instance Methods
protected override CreateParams CreateParams {
get {
return base.CreateParams;
}
}
protected bool HScroll {
get {
return hscroll_visible;
}
set {
if (hscroll_visible != value) {
force_hscroll_visible = value;
Recalculate(this, EventArgs.Empty);
}
}
}
protected bool VScroll {
get {
return vscroll_visible;
}
set {
if (vscroll_visible != value) {
force_vscroll_visible = value;
Recalculate(this, EventArgs.Empty);
}
}
}
#endregion // Protected Instance Methods
#region Public Instance Methods
public void ScrollControlIntoView(Control activeControl) {
}
public void SetAutoScrollMargin(int x, int y) {
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
auto_scroll_margin = new Size(x, y);
Recalculate(this, EventArgs.Empty);
}
#endregion // Public Instance Methods
#region Protected Instance Methods
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void AdjustFormScrollbars(bool displayScrollbars) {
// Internal MS
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected bool GetScrollState(int bit) {
return false;
// Internal MS
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnLayout(LayoutEventArgs levent) {
base.OnLayout(levent);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnMouseWheel(MouseEventArgs e) {
if (vscroll_visible) {
if (e.Delta > 0) {
if (vscrollbar.Minimum < (vscrollbar.Value - vscrollbar.LargeChange)) {
vscrollbar.Value -= vscrollbar.LargeChange;
} else {
vscrollbar.Value = vscrollbar.Minimum;
}
} else {
if (vscrollbar.Maximum > (vscrollbar.Value + vscrollbar.LargeChange)) {
vscrollbar.Value += vscrollbar.LargeChange;
} else {
vscrollbar.Value = vscrollbar.Maximum;
}
}
}
base.OnMouseWheel(e);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnVisibleChanged(EventArgs e) {
base.OnVisibleChanged(e);
}
protected override void ScaleCore(float dx, float dy) {
base.ScaleCore(dx, dy);
}
protected void SetDisplayRectLocation(int x, int y) {
throw new NotImplementedException();
}
protected void SetScrollState(int bit, bool value) {
throw new NotImplementedException();
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
}
#endregion // Protected Instance Methods
#region Internal & Private Methods
private Size Canvas {
get {
int num_of_children;
int width;
int height;
num_of_children = child_controls.Count;
width = 0;
height = 0;
for (int i = 0; i < num_of_children; i++) {
if ((child_controls[i].Visible == false) || (child_controls[i] == hscrollbar) || (child_controls[i] == vscrollbar) || (child_controls[i] == sizegrip)) {
continue;
}
if (child_controls[i].Right > width) {
width = child_controls[i].Right;
}
if (child_controls[i].Bottom > height) {
height = child_controls[i].Bottom;
}
}
return new Size(width, height);
}
}
private void Recalculate (object sender, EventArgs e)
{
if (!this.auto_scroll && !force_hscroll_visible && !force_vscroll_visible) {
return;
}
Size canvas = Canvas;
Size client = ClientRectangle.Size;
canvas.Width += auto_scroll_margin.Width;
canvas.Height += auto_scroll_margin.Height;
int right_edge = client.Width;
int bottom_edge = client.Height;
int prev_right_edge;
int prev_bottom_edge;
do {
prev_right_edge = right_edge;
prev_bottom_edge = bottom_edge;
if ((force_hscroll_visible || canvas.Width > right_edge) && client.Width != 0) {
hscroll_visible = true;
bottom_edge = client.Height - SystemInformation.HorizontalScrollBarHeight;
} else {
hscroll_visible = false;
bottom_edge = client.Height;
}
if ((force_vscroll_visible || canvas.Height > bottom_edge) && client.Height != 0) {
vscroll_visible = true;
right_edge = client.Width - SystemInformation.VerticalScrollBarWidth;
} else {
vscroll_visible = false;
right_edge = client.Width;
}
} while (right_edge != prev_right_edge || bottom_edge != prev_bottom_edge);
if (hscroll_visible) {
hscrollbar.Left = 0;
hscrollbar.Top = client.Height - SystemInformation.HorizontalScrollBarHeight;
hscrollbar.LargeChange = DisplayRectangle.Width;
hscrollbar.SmallChange = DisplayRectangle.Width / 10;
hscrollbar.Maximum = canvas.Width - 1;
} else {
scroll_position.X = 0;
}
if (vscroll_visible) {
vscrollbar.Left = client.Width - SystemInformation.VerticalScrollBarWidth;
vscrollbar.Top = 0;
vscrollbar.LargeChange = DisplayRectangle.Height;
vscrollbar.SmallChange = DisplayRectangle.Height / 10;
vscrollbar.Maximum = canvas.Height - 1;
} else {
scroll_position.Y = 0;
}
if (hscroll_visible && vscroll_visible) {
hscrollbar.Width = ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth;
vscrollbar.Height = ClientRectangle.Height - SystemInformation.HorizontalScrollBarHeight;
sizegrip.Left = hscrollbar.Right;
sizegrip.Top = vscrollbar.Bottom;
sizegrip.Width = SystemInformation.VerticalScrollBarWidth;
sizegrip.Height = SystemInformation.HorizontalScrollBarHeight;
hscrollbar.Visible = true;
vscrollbar.Visible = true;
sizegrip.Visible = true;
} else {
sizegrip.Visible = false;
if (hscroll_visible) {
hscrollbar.Width = ClientRectangle.Width;
hscrollbar.Visible = true;
} else {
hscrollbar.Visible = false;
}
if (vscroll_visible) {
vscrollbar.Height = ClientRectangle.Height;
vscrollbar.Visible = true;
} else {
vscrollbar.Visible = false;
}
}
}
private void HandleScrollBar(object sender, EventArgs e) {
if (sender == vscrollbar) {
ScrollWindow(0, vscrollbar.Value- scroll_position.Y);
} else {
ScrollWindow(hscrollbar.Value - scroll_position.X, 0);
}
}
private void ScrollWindow(int XOffset, int YOffset) {
int num_of_children;
SuspendLayout();
num_of_children = child_controls.Count;
for (int i = 0; i < num_of_children; i++) {
if (child_controls[i] == hscrollbar || child_controls[i] == vscrollbar || child_controls[i] == sizegrip) {
continue;
}
child_controls[i].Left -= XOffset;
child_controls[i].Top -= YOffset;
// Is this faster? child_controls[i].Location -= new Size(XOffset, YOffset);
}
scroll_position.X += XOffset;
scroll_position.Y += YOffset;
// Should we call XplatUI.ScrollWindow???
Invalidate();
ResumeLayout();
}
#endregion // Internal & Private Methods
}
}
|