1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
23 // Peter Dennis Bartok (pbartok@novell.com)
32 using System.ComponentModel;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
38 [DefaultEvent("SplitterMoved")]
39 [Designer("System.Windows.Forms.Design.SplitterDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 [DefaultProperty("Dock")]
41 public class Splitter : Control
47 private enum DrawType {
54 #region Local Variables
55 static private Cursor splitter_ns;
56 static private Cursor splitter_we;
57 // XXX this "new" shouldn't be here. Control shouldn't define border_style as internal.
58 new private BorderStyle border_style;
59 private int min_extra;
61 private int split_position; // Current splitter position
62 private int prev_split_position; // Previous splitter position, only valid during drag
63 private int click_offset; // Click offset from border of splitter control
64 private int splitter_size; // Size (width or height) of our splitter control
65 private bool horizontal; // true if we've got a horizontal splitter
66 private Control affected; // The control that the splitter resizes
67 private Control filler; // The control that MinExtra prevents from being shrunk to 0 size
68 private SplitterEventArgs sevent; // We cache the object, prevents fragmentation
69 private int limit_min; // The max we're allowed to move the splitter left/up
70 private int limit_max; // The max we're allowed to move the splitter right/down
71 private int split_requested; // If the user requests a position before we have ever laid out the doc
72 #endregion // Local Variables
76 splitter_ns = Cursors.HSplit;
77 splitter_we = Cursors.VSplit;
88 sevent = new SplitterEventArgs(0, 0, 0, 0);
90 SetStyle(ControlStyles.Selectable, false);
91 Anchor = AnchorStyles.None;
92 Dock = DockStyle.Left;
94 Layout += new LayoutEventHandler(LayoutSplitter);
95 this.ParentChanged += new EventHandler(ReparentSplitter);
98 #endregion // Constructors
100 #region Public Instance Properties
102 [EditorBrowsable(EditorBrowsableState.Never)]
103 public override bool AllowDrop {
105 return base.AllowDrop;
109 base.AllowDrop = value;
114 [DefaultValue(AnchorStyles.None)]
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public override AnchorStyles Anchor {
118 return AnchorStyles.None;
122 ; // MS doesn't set it
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 public override Image BackgroundImage {
130 return base.BackgroundImage;
134 base.BackgroundImage = value;
139 [DefaultValue (BorderStyle.None)]
140 [MWFDescription("Sets the border style for the splitter")]
141 [MWFCategory("Appearance")]
142 public BorderStyle BorderStyle {
148 border_style = value;
151 case BorderStyle.FixedSingle: {
152 splitter_size = 4; // We don't get motion events for 1px wide windows on X11. sigh.
156 case BorderStyle.Fixed3D: {
157 value = BorderStyle.None;
162 case BorderStyle.None: {
168 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
172 base.InternalBorderStyle = value;
176 [DefaultValue(DockStyle.Left)]
178 public override DockStyle Dock {
184 if (!Enum.IsDefined (typeof (DockStyle), value) || (value == DockStyle.None) || (value == DockStyle.Fill)) {
185 throw new ArgumentException("Splitter must be docked left, top, bottom or right");
188 if ((value == DockStyle.Top) || (value == DockStyle.Bottom)) {
190 Cursor = splitter_ns;
193 Cursor = splitter_we;
200 [EditorBrowsable(EditorBrowsableState.Never)]
201 public override Font Font {
212 [EditorBrowsable(EditorBrowsableState.Never)]
213 public override Color ForeColor {
215 return base.ForeColor;
219 base.ForeColor = value;
224 [EditorBrowsable(EditorBrowsableState.Never)]
225 public new ImeMode ImeMode {
231 base.ImeMode = value;
237 [MWFDescription("Sets minimum size of undocked window")]
238 [MWFCategory("Behaviour")]
239 public int MinExtra {
251 [MWFDescription("Sets minimum size of the resized control")]
252 [MWFCategory("Behaviour")]
265 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
266 [MWFDescription("Current splitter position")]
267 [MWFCategory("Layout")]
268 public int SplitPosition {
270 affected = AffectedControl;
271 if (affected == null) {
276 return CalculateSplitPosition();
280 return affected.Height;
282 return affected.Width;
287 affected = AffectedControl;
289 if (affected == null) {
290 split_requested = value;
293 if (Capture || (affected == null)) {
298 affected.Height = value;
300 affected.Width = value;
306 [EditorBrowsable(EditorBrowsableState.Never)]
307 public new bool TabStop {
308 get { return base.TabStop; }
309 set { base.TabStop = value; }
314 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 public override string Text {
326 #endregion // Public Instance Properties
328 #region Protected Instance Properties
329 protected override CreateParams CreateParams {
331 return base.CreateParams;
335 protected override ImeMode DefaultImeMode {
337 return ImeMode.Disable;
341 protected override Size DefaultSize {
343 return new Size (3, 3);
346 #endregion // Protected Instance Properties
348 #region Public Instance Methods
350 public bool PreFilterMessage(ref Message m) {
355 public override string ToString() {
356 return base.ToString () + String.Format(", MinExtra: {0}, MinSize: {1}", min_extra, min_size);
358 #endregion // Public Instance Methods
360 #region Protected Instance Methods
361 protected override void OnKeyDown(KeyEventArgs e) {
363 if (Capture && (e.KeyCode == Keys.Escape)) {
365 DrawDragHandle(DrawType.Finish);
369 protected override void OnMouseDown(MouseEventArgs e) {
372 base.OnMouseDown (e);
374 // Only allow if we are set up properly
375 if ((affected == null) || (filler == null)) {
376 affected = AffectedControl;
377 filler = FillerControl;
380 if (affected == null || e.Button != MouseButtons.Left) {
388 if (filler != null) {
390 if (dock_style == DockStyle.Top) {
391 limit_min = affected.Bounds.Top + min_size;
392 limit_max = filler.Bounds.Bottom - min_extra + this.bounds.Top - filler.Bounds.Top;
394 limit_min = filler.Bounds.Top + min_extra + this.bounds.Top - filler.Bounds.Bottom;
395 limit_max = affected.Bounds.Bottom - min_size - this.Height;
398 if (dock_style == DockStyle.Left) {
399 limit_min = affected.Bounds.Left + min_size;
400 limit_max = filler.Bounds.Right - min_extra + this.bounds.Left - filler.Bounds.Left;
402 limit_min = filler.Bounds.Left + min_extra + this.bounds.Left - filler.Bounds.Right;
403 limit_max = affected.Bounds.Right - min_size - this.Width;
409 limit_max = affected.Parent.Height;
411 limit_max = affected.Parent.Width;
416 Console.WriteLine("Sizing limits: Min:{0}, Max:{1}", limit_min, limit_max);
419 pt = PointToScreen(Parent.PointToClient(new Point(e.X, e.Y)));
422 split_position = pt.Y;
423 if (dock_style == DockStyle.Top) {
429 split_position = pt.X;
430 if (dock_style == DockStyle.Left) {
437 // We need to set this, in case we never get a mouse move
438 prev_split_position = split_position;
441 Console.WriteLine("Click-offset: {0} MouseDown split position: {1}", click_offset, split_position);
444 // Draw our initial handle
445 DrawDragHandle(DrawType.Initial);
448 protected override void OnMouseMove(MouseEventArgs e) {
451 base.OnMouseMove (e);
453 if (!Capture || e.Button != MouseButtons.Left || affected == null) {
457 // We need our mouse coordinates relative to our parent
458 pt = PointToScreen(Parent.PointToClient(new Point(e.X, e.Y)));
460 // Grab our new coordinates
461 prev_split_position = split_position;
463 int candidate = horizontal ? pt.Y : pt.X;
465 // Enforce limit on what we send to the event
466 if (candidate < limit_min)
467 candidate = limit_min;
468 else if (candidate > limit_max)
469 candidate = limit_max;
473 sevent.split_x = horizontal ? 0 : candidate;
474 sevent.split_y = horizontal ? candidate : 0;
477 OnSplitterMoving(sevent);
479 split_position = horizontal ? sevent.split_y : sevent.split_x;
482 if (split_position < limit_min) {
484 Console.WriteLine("SplitPosition {0} less than minimum {1}, setting to minimum", split_position, limit_min);
486 split_position = limit_min;
487 } else if (split_position > limit_max) {
489 Console.WriteLine("SplitPosition {0} more than maximum {1}, setting to maximum", split_position, limit_max);
491 split_position = limit_max;
494 // Don't waste cycles
495 if (prev_split_position != split_position) {
496 // Update our handle location
497 DrawDragHandle(DrawType.Redraw);
501 protected override void OnMouseUp(MouseEventArgs e) {
502 if (!Capture || e.Button != MouseButtons.Left || affected == null) {
508 DrawDragHandle(DrawType.Finish);
510 // Resize the affected window
512 affected.Height = CalculateSplitPosition() - click_offset;
514 Console.WriteLine("Setting height of affected control to {0}", CalculateSplitPosition() - click_offset);
517 affected.Width = CalculateSplitPosition() - click_offset;
519 Console.WriteLine("Setting width of affected control to {0}", CalculateSplitPosition() - click_offset);
525 // It seems that MS is sending some data that doesn't quite make sense
526 // In this event. It tried to match their stuff.., not sure about split_x...
531 sevent.y = split_position;
532 sevent.split_x = 200;
533 sevent.split_y = split_position;
535 sevent.x = split_position;
537 sevent.split_x = split_position;
538 sevent.split_y = 200;
543 OnSplitterMoved(sevent);
546 protected virtual void OnSplitterMoved(SplitterEventArgs sevent) {
547 SplitterEventHandler eh = (SplitterEventHandler)(Events [SplitterMovedEvent]);
552 protected virtual void OnSplitterMoving(SplitterEventArgs sevent) {
553 SplitterEventHandler eh = (SplitterEventHandler)(Events [SplitterMovingEvent]);
558 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
559 // enforce our width / height
561 splitter_size = height;
562 if (splitter_size < 1) {
565 base.SetBoundsCore (x, y, width, splitter_size, specified);
567 splitter_size = width;
568 if (splitter_size < 1) {
571 base.SetBoundsCore (x, y, splitter_size, height, specified);
574 #endregion // Protected Instance Methods
576 #region Private Properties and Methods
577 private Control AffectedControl {
579 if (Parent == null) {
583 // Doc says the first control preceeding us in the zorder
584 for (int i = Parent.Controls.GetChildIndex(this) + 1; i < Parent.Controls.Count; i++) {
586 case DockStyle.Top: {
587 if (Top == Parent.Controls[i].Bottom) {
588 return Parent.Controls[i];
593 case DockStyle.Bottom: {
594 if (Bottom == Parent.Controls[i].Top) {
595 return Parent.Controls[i];
600 case DockStyle.Left: {
601 if (Left == Parent.Controls[i].Right) {
602 return Parent.Controls[i];
607 case DockStyle.Right: {
608 if (Right == Parent.Controls[i].Left) {
609 return Parent.Controls[i];
619 private Control FillerControl {
621 if (Parent == null) {
625 // Doc says the first control preceeding us in the zorder
626 for (int i = Parent.Controls.GetChildIndex(this) - 1; i >= 0; i--) {
627 if (Parent.Controls[i].Dock == DockStyle.Fill) {
628 return Parent.Controls[i];
635 private int CalculateSplitPosition() {
637 if (dock_style == DockStyle.Top) {
638 return split_position - affected.Top;
640 return affected.Bottom - split_position - splitter_size;
643 if (dock_style == DockStyle.Left) {
644 return split_position - affected.Left;
646 return affected.Right - split_position - splitter_size;
651 internal override void OnPaintInternal (PaintEventArgs e) {
652 e.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(this.BackColor), e.ClipRectangle);
655 private void LayoutSplitter(object sender, LayoutEventArgs e) {
656 affected = AffectedControl;
657 filler = FillerControl;
658 if (split_requested != -1) {
659 SplitPosition = split_requested;
660 split_requested = -1;
664 private void ReparentSplitter(object sender, EventArgs e) {
669 private void DrawDragHandle(DrawType type) {
674 prev = new Rectangle(Location.X, prev_split_position - click_offset + 1, Width, 0);
675 current = new Rectangle(Location.X, split_position - click_offset + 1, Width, 0);
677 prev = new Rectangle(prev_split_position - click_offset + 1, Location.Y, 0, Height);
678 current = new Rectangle(split_position - click_offset + 1, Location.Y, 0, Height);
682 case DrawType.Initial: {
683 XplatUI.DrawReversibleRectangle(Parent.window.Handle, current, 3);
687 case DrawType.Redraw: {
688 if (prev.X == current.X && prev.Y == current.Y) {
692 XplatUI.DrawReversibleRectangle(Parent.window.Handle, prev, 3);
693 XplatUI.DrawReversibleRectangle(Parent.window.Handle, current, 3);
697 case DrawType.Finish: {
698 XplatUI.DrawReversibleRectangle(Parent.window.Handle, current, 3);
703 #endregion // Private Properties and Methods
707 [EditorBrowsable(EditorBrowsableState.Never)]
708 public new event EventHandler BackgroundImageChanged {
709 add { base.BackgroundImageChanged += value; }
710 remove { base.BackgroundImageChanged -= value; }
714 [EditorBrowsable(EditorBrowsableState.Never)]
715 public new event EventHandler Enter {
716 add { base.Enter += value; }
717 remove { base.Enter -= value; }
721 [EditorBrowsable(EditorBrowsableState.Never)]
722 public new event EventHandler FontChanged {
723 add { base.FontChanged += value; }
724 remove { base.FontChanged -= value; }
728 [EditorBrowsable(EditorBrowsableState.Never)]
729 public new event EventHandler ForeColorChanged {
730 add { base.ForeColorChanged += value; }
731 remove { base.ForeColorChanged -= value; }
735 [EditorBrowsable(EditorBrowsableState.Never)]
736 public new event EventHandler ImeModeChanged {
737 add { base.ImeModeChanged += value; }
738 remove { base.ImeModeChanged -= value; }
742 [EditorBrowsable(EditorBrowsableState.Never)]
743 public new event KeyEventHandler KeyDown {
744 add { base.KeyDown += value; }
745 remove { base.KeyDown -= value; }
749 [EditorBrowsable(EditorBrowsableState.Never)]
750 public new event KeyPressEventHandler KeyPress {
751 add { base.KeyPress += value; }
752 remove { base.KeyPress -= value; }
756 [EditorBrowsable(EditorBrowsableState.Never)]
757 public new event KeyEventHandler KeyUp {
758 add { base.KeyUp += value; }
759 remove { base.KeyUp -= value; }
763 [EditorBrowsable(EditorBrowsableState.Never)]
764 public new event EventHandler Leave {
765 add { base.Leave += value; }
766 remove { base.Leave -= value; }
770 [EditorBrowsable(EditorBrowsableState.Never)]
771 public new event EventHandler TabStopChanged {
772 add { base.TabStopChanged += value; }
773 remove { base.TabStopChanged -= value; }
777 [EditorBrowsable(EditorBrowsableState.Never)]
778 public new event EventHandler TextChanged {
779 add { base.TextChanged += value; }
780 remove { base.TextChanged -= value; }
783 static object SplitterMovedEvent = new object ();
784 static object SplitterMovingEvent = new object ();
786 public event SplitterEventHandler SplitterMoved {
787 add { Events.AddHandler (SplitterMovedEvent, value); }
788 remove { Events.RemoveHandler (SplitterMovedEvent, value); }
791 public event SplitterEventHandler SplitterMoving {
792 add { Events.AddHandler (SplitterMovingEvent, value); }
793 remove { Events.RemoveHandler (SplitterMovingEvent, value); }