2008-05-05 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStrip.cs
1 //
2 // ToolStrip.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using System.Drawing;
34 using System.Windows.Forms.Layout;
35 using System.Collections.Generic;
36 using System.ComponentModel.Design.Serialization;
37
38 namespace System.Windows.Forms
39 {
40         [ComVisible (true)]
41         [ClassInterface (ClassInterfaceType.AutoDispatch)]
42         [DefaultEvent ("ItemClicked")]
43         [DefaultProperty ("Items")]
44         [Designer ("System.Windows.Forms.Design.ToolStripDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         [DesignerSerializer ("System.Windows.Forms.Design.ToolStripCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
46         public class ToolStrip : ScrollableControl, IComponent, IDisposable, IToolStripData
47         {
48                 #region Private Variables
49                 private bool allow_item_reorder;
50                 private bool allow_merge;
51                 private Color back_color;
52                 private bool can_overflow;
53                 private ToolStrip currently_merged_with;
54                 private ToolStripDropDownDirection default_drop_down_direction;
55                 internal ToolStripItemCollection displayed_items;
56                 private Color fore_color;
57                 private Padding grip_margin;
58                 private ToolStripGripStyle grip_style;
59                 private List<ToolStripItem> hidden_merged_items;
60                 private ImageList image_list;
61                 private Size image_scaling_size;
62                 private bool is_currently_merged;
63                 private ToolStripItemCollection items;
64                 private bool keyboard_active;
65                 private LayoutEngine layout_engine;
66                 private LayoutSettings layout_settings;
67                 private ToolStripLayoutStyle layout_style;
68                 private Orientation orientation;
69                 private ToolStripOverflowButton overflow_button;
70                 private List<ToolStripItem> pre_merge_items;
71                 private ToolStripRenderer renderer;
72                 private ToolStripRenderMode render_mode;
73                 private ToolStripTextDirection text_direction;
74                 private Timer tooltip_timer;
75                 private ToolTip tooltip_window;
76                 private bool show_item_tool_tips;
77                 private bool stretch;
78
79                 private ToolStripItem mouse_currently_over;
80                 internal bool menu_selected;
81                 private ToolStripItem tooltip_currently_showing;
82                 #endregion
83
84                 #region Public Constructors
85                 public ToolStrip () : this (null)
86                 {
87                 }
88
89                 public ToolStrip (params ToolStripItem[] items) : base ()
90                 {
91                         SetStyle (ControlStyles.AllPaintingInWmPaint, true);
92                         SetStyle (ControlStyles.OptimizedDoubleBuffer, true);
93                         SetStyle (ControlStyles.Selectable, false);
94                         SetStyle (ControlStyles.SupportsTransparentBackColor, true);
95
96                         this.SuspendLayout ();
97                         
98                         this.items = new ToolStripItemCollection (this, items, true);
99                         this.allow_merge = true;
100                         base.AutoSize = true;
101                         this.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
102                         this.back_color = Control.DefaultBackColor;
103                         this.can_overflow = true;
104                         base.CausesValidation = false;
105                         this.default_drop_down_direction = ToolStripDropDownDirection.BelowRight;
106                         this.displayed_items = new ToolStripItemCollection (this, null, true);
107                         this.Dock = this.DefaultDock;
108                         base.Font = new Font ("Tahoma", 8.25f);
109                         this.fore_color = Control.DefaultForeColor;
110                         this.grip_margin = this.DefaultGripMargin;
111                         this.grip_style = ToolStripGripStyle.Visible;
112                         this.image_scaling_size = new Size (16, 16);
113                         this.layout_style = ToolStripLayoutStyle.HorizontalStackWithOverflow;
114                         this.orientation = Orientation.Horizontal;
115                         if (!(this is ToolStripDropDown))
116                                 this.overflow_button = new ToolStripOverflowButton (this);
117                         this.renderer = null;
118                         this.render_mode = ToolStripRenderMode.ManagerRenderMode;
119                         this.show_item_tool_tips = this.DefaultShowItemToolTips;
120                         base.TabStop = false;
121                         this.text_direction = ToolStripTextDirection.Horizontal;
122                         this.ResumeLayout ();
123                         
124                         // Register with the ToolStripManager
125                         ToolStripManager.AddToolStrip (this);
126                 }
127                 #endregion
128
129                 #region Public Properties
130                 [MonoTODO ()]
131                 public override bool AllowDrop {
132                         get { return base.AllowDrop; }
133                         set { base.AllowDrop = value; }
134                 }
135                 
136                 [MonoTODO ()]
137                 [DefaultValue (false)]
138                 public bool AllowItemReorder {
139                         get { return this.allow_item_reorder; }
140                         set { this.allow_item_reorder = value; }
141                 }
142                 
143                 [DefaultValue (true)]
144                 public bool AllowMerge {
145                         get { return this.allow_merge; }
146                         set { this.allow_merge = value; }
147                 }
148                 
149                 public override AnchorStyles Anchor {
150                         get { return base.Anchor; }
151                         set { base.Anchor = value; }
152                 }
153
154                 [Browsable (false)]
155                 [EditorBrowsable (EditorBrowsableState.Never)]
156                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157                 public override bool AutoScroll {
158                         get { return base.AutoScroll; }
159                         set { base.AutoScroll = value; }
160                 }
161
162                 [Browsable (false)]
163                 [EditorBrowsable (EditorBrowsableState.Never)]
164                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
165                 public new Size AutoScrollMargin {
166                         get { return base.AutoScrollMargin; }
167                         set { base.AutoScrollMargin = value; }
168                 }
169
170                 [Browsable (false)]
171                 [EditorBrowsable (EditorBrowsableState.Never)]
172                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173                 public new Size AutoScrollMinSize {
174                         get { return base.AutoScrollMinSize; }
175                         set { base.AutoScrollMinSize = value; }
176                 }
177
178                 [Browsable (false)]
179                 [EditorBrowsable (EditorBrowsableState.Never)]
180                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
181                 public new Point AutoScrollPosition {
182                         get { return base.AutoScrollPosition; }
183                         set { base.AutoScrollPosition = value; }
184                 }
185
186                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
187                 [Browsable (true)]
188                 [EditorBrowsable (EditorBrowsableState.Always)]
189                 [DefaultValue (true)]
190                 public override bool AutoSize {
191                         get { return base.AutoSize; }
192                         set { base.AutoSize = value; }
193                 }
194                 
195                 new public Color BackColor {
196                         get { return this.back_color; }
197                         set { this.back_color = value; }
198                 }
199
200                 public override BindingContext BindingContext {
201                         get { return base.BindingContext; }
202                         set { base.BindingContext = value; }
203                 }
204                 
205                 [DefaultValue (true)]
206                 public bool CanOverflow {
207                         get { return this.can_overflow; }
208                         set { this.can_overflow = value; }
209                 }
210                 
211                 [Browsable (false)]
212                 [DefaultValue (false)]
213                 public new bool CausesValidation {
214                         get { return base.CausesValidation; }
215                         set { base.CausesValidation = value; }
216                 }
217                 
218                 [EditorBrowsable (EditorBrowsableState.Never)]
219                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220                 public new ControlCollection Controls {
221                         get { return base.Controls; }
222                 }
223
224                 [Browsable (false)]
225                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
226                 public override Cursor Cursor {
227                         get { return base.Cursor; }
228                         set { base.Cursor = value; }
229                 }
230                 
231                 [Browsable (false)]
232                 public virtual ToolStripDropDownDirection DefaultDropDownDirection {
233                         get { return this.default_drop_down_direction; }
234                         set { 
235                                 if (!Enum.IsDefined (typeof (ToolStripDropDownDirection), value))
236                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripDropDownDirection", value));
237                                         
238                                 this.default_drop_down_direction = value;
239                         }
240                 }
241
242                 public override Rectangle DisplayRectangle {
243                         get {
244                                 if (this.orientation == Orientation.Horizontal)
245                                         if (this.grip_style == ToolStripGripStyle.Hidden || this.layout_style == ToolStripLayoutStyle.Flow || this.layout_style == ToolStripLayoutStyle.Table)
246                                                 return new Rectangle (this.Padding.Left, this.Padding.Top, this.Width - this.Padding.Horizontal, this.Height - this.Padding.Vertical);
247                                         else
248                                                 return new Rectangle (this.GripRectangle.Right + this.GripMargin.Right, this.Padding.Top, this.Width - this.Padding.Horizontal - this.GripRectangle.Right - this.GripMargin.Right, this.Height - this.Padding.Vertical);
249                                 else
250                                         if (this.grip_style == ToolStripGripStyle.Hidden || this.layout_style == ToolStripLayoutStyle.Flow || this.layout_style == ToolStripLayoutStyle.Table)
251                                                 return new Rectangle (this.Padding.Left, this.Padding.Top, this.Width - this.Padding.Horizontal, this.Height - this.Padding.Vertical);
252                                         else
253                                                 return new Rectangle (this.Padding.Left, this.GripRectangle.Bottom + this.GripMargin.Bottom + this.Padding.Top, this.Width - this.Padding.Horizontal, this.Height - this.Padding.Vertical - this.GripRectangle.Bottom - this.GripMargin.Bottom);
254                         }
255                 }
256
257                 [DefaultValue (DockStyle.Top)]
258                 public override DockStyle Dock {
259                         get { return base.Dock; }
260                         set {
261                                 if (base.Dock != value) {
262                                         base.Dock = value;
263                                         
264                                         switch (value) {
265                                                 case DockStyle.Top:
266                                                 case DockStyle.Bottom:
267                                                 case DockStyle.None:
268                                                         this.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
269                                                         break;
270                                                 case DockStyle.Left:
271                                                 case DockStyle.Right:
272                                                         this.LayoutStyle = ToolStripLayoutStyle.VerticalStackWithOverflow;
273                                                         break;
274                                         }
275                                 }
276                         }
277                 }
278
279                 public override Font Font {
280                         get { return base.Font; }
281                         set { 
282                                 if (base.Font != value) {
283                                         base.Font = value;
284                                         
285                                         foreach (ToolStripItem tsi in this.Items)
286                                                 tsi.OnOwnerFontChanged (EventArgs.Empty);
287                                 }
288                          }
289                 }
290                 
291                 [Browsable (false)]
292                 public new Color ForeColor {
293                         get { return this.fore_color; }
294                         set { 
295                                 if (this.fore_color != value) {
296                                         this.fore_color = value; 
297                                         this.OnForeColorChanged (EventArgs.Empty); 
298                                 }
299                         }
300                 }
301
302                 [Browsable (false)]
303                 public ToolStripGripDisplayStyle GripDisplayStyle {
304                         get { return this.orientation == Orientation.Vertical ? ToolStripGripDisplayStyle.Horizontal : ToolStripGripDisplayStyle.Vertical; }
305                 }
306
307                 public Padding GripMargin {
308                         get { return this.grip_margin; }
309                         set { 
310                                 if (this.grip_margin != value) {
311                                         this.grip_margin = value; 
312                                         this.PerformLayout (); 
313                                 }
314                         }
315                 }
316
317                 [Browsable (false)]
318                 public Rectangle GripRectangle {
319                         get {
320                                 if (this.grip_style == ToolStripGripStyle.Hidden)
321                                         return Rectangle.Empty;
322
323                                 if (this.orientation == Orientation.Horizontal)
324                                         return new Rectangle (this.grip_margin.Left + this.Padding.Left, this.Padding.Top, 3, this.Height);
325                                 else
326                                         return new Rectangle (this.Padding.Left, this.grip_margin.Top + this.Padding.Top, this.Width, 3);
327                         }
328                 }
329
330                 [DefaultValue (ToolStripGripStyle.Visible)]
331                 public ToolStripGripStyle GripStyle {
332                         get { return this.grip_style; }
333                         set {
334                                 if (this.grip_style != value) {
335                                         if (!Enum.IsDefined (typeof (ToolStripGripStyle), value))
336                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripGripStyle", value));
337                                         this.grip_style = value;
338                                         this.PerformLayout ();
339                                 }
340                         }
341                 }
342
343                 [Browsable (false)]
344                 [EditorBrowsable (EditorBrowsableState.Never)]
345                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
346                 public new bool HasChildren {
347                         get { return base.HasChildren; }
348                 }
349
350                 [Browsable (false)]
351                 [EditorBrowsable (EditorBrowsableState.Never)]
352                 public new HScrollProperties HorizontalScroll {
353                         get { return base.HorizontalScroll; }
354                 }
355                 
356                 [Browsable (false)]
357                 [DefaultValue (null)]
358                 public ImageList ImageList {
359                         get { return this.image_list; }
360                         set { this.image_list = value; }
361                 }
362
363                 [DefaultValue ("{Width=16, Height=16}")]
364                 public Size ImageScalingSize {
365                         get { return this.image_scaling_size; }
366                         set { this.image_scaling_size = value; }
367                 }
368
369                 [MonoTODO ("Always returns false, dragging not implemented yet.")]
370                 [Browsable (false)]
371                 [EditorBrowsable (EditorBrowsableState.Advanced)]
372                 public bool IsCurrentlyDragging {
373                         get { return false; }
374                 }
375                 
376                 [Browsable (false)]
377                 public bool IsDropDown {
378                         get {
379                                 if (this is ToolStripDropDown)
380                                         return true;
381
382                                 return false;
383                         }
384                 }
385
386                 [MergableProperty (false)]
387                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
388                 public virtual ToolStripItemCollection Items {
389                         get { return this.items; }
390                 }
391
392                 public override LayoutEngine LayoutEngine {
393                         get { 
394                                  if (layout_engine == null)
395                                         this.layout_engine = new ToolStripSplitStackLayout ();
396                                         
397                                  return this.layout_engine;
398                         }
399                 }
400
401                 [Browsable (false)]
402                 [DefaultValue (null)]
403                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
404                 public LayoutSettings LayoutSettings {
405                         get { return this.layout_settings; }
406                         set { this.layout_settings = value; }
407                 }
408                 
409                 [AmbientValue (ToolStripLayoutStyle.StackWithOverflow)]
410                 public ToolStripLayoutStyle LayoutStyle {
411                         get { return layout_style; }
412                         set {
413                                 if (this.layout_style != value) {
414                                         if (!Enum.IsDefined (typeof (ToolStripLayoutStyle), value))
415                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripLayoutStyle", value));
416
417                                         this.layout_style = value;
418
419                                         if (this.layout_style == ToolStripLayoutStyle.Flow)
420                                                 this.layout_engine = new FlowLayout ();
421                                         else
422                                                 this.layout_engine = new ToolStripSplitStackLayout ();
423
424                                         if (this.layout_style == ToolStripLayoutStyle.StackWithOverflow) {
425                                                 if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
426                                                         this.layout_style = ToolStripLayoutStyle.VerticalStackWithOverflow;
427                                                 else
428                                                         this.layout_style = ToolStripLayoutStyle.HorizontalStackWithOverflow;
429                                         }
430
431                                         if (this.layout_style == ToolStripLayoutStyle.HorizontalStackWithOverflow)
432                                                 this.orientation = Orientation.Horizontal;
433                                         else if (this.layout_style == ToolStripLayoutStyle.VerticalStackWithOverflow)
434                                                 this.orientation = Orientation.Vertical;
435                                                 
436                                         this.layout_settings = this.CreateLayoutSettings (value);
437                                         
438                                         this.PerformLayout ();
439                                         this.OnLayoutStyleChanged (EventArgs.Empty);
440                                 }
441                         }
442                 }
443
444                 [Browsable (false)]
445                 public Orientation Orientation {
446                         get { return this.orientation; }
447                 }
448
449                 [Browsable (false)]
450                 [EditorBrowsable (EditorBrowsableState.Advanced)]
451                 public ToolStripOverflowButton OverflowButton {
452                         get { return this.overflow_button; }
453                 }
454                 
455                 [Browsable (false)]
456                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
457                 public ToolStripRenderer Renderer {
458                         get { 
459                                 if (this.render_mode == ToolStripRenderMode.ManagerRenderMode)
460                                         return ToolStripManager.Renderer;
461                                         
462                                 return this.renderer; 
463                         }
464                         set { 
465                                 if (this.renderer != value) {
466                                         this.renderer = value; 
467                                         this.render_mode = ToolStripRenderMode.Custom;
468                                         this.PerformLayout ();
469                                         this.OnRendererChanged (EventArgs.Empty);
470                                 }
471                         }
472                 }
473
474                 public ToolStripRenderMode RenderMode {
475                         get { return this.render_mode; }
476                         set {
477                                 if (!Enum.IsDefined (typeof (ToolStripRenderMode), value))
478                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripRenderMode", value));
479
480                                 if (value == ToolStripRenderMode.Custom && this.renderer == null)
481                                         throw new NotSupportedException ("Must set Renderer property before setting RenderMode to Custom");
482                                 else if (value == ToolStripRenderMode.Professional)
483                                         this.Renderer = new ToolStripProfessionalRenderer ();
484                                 else if (value == ToolStripRenderMode.System)
485                                         this.Renderer = new ToolStripSystemRenderer ();
486                                         
487                                 this.render_mode = value;
488                         }
489                 }
490
491                 [DefaultValue (true)]
492                 public bool ShowItemToolTips {
493                         get { return this.show_item_tool_tips; }
494                         set { this.show_item_tool_tips = value; }
495                 }
496                 
497                 [DefaultValue (false)]
498                 public bool Stretch {
499                         get { return this.stretch; }
500                         set { this.stretch = value; }
501                 }
502                 
503                 [DefaultValue (false)]
504                 [DispId(-516)]
505                 public new bool TabStop {
506                         get { return base.TabStop; }
507                         set { 
508                                 base.TabStop = value;
509                                 SetStyle (ControlStyles.Selectable, value);
510                         }
511                 }
512
513                 [DefaultValue (ToolStripTextDirection.Horizontal)]
514                 public virtual ToolStripTextDirection TextDirection {
515                         get { return this.text_direction; }
516                         set {
517                                 if (!Enum.IsDefined (typeof (ToolStripTextDirection), value))
518                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripTextDirection", value));
519
520                                 if (this.text_direction != value) {
521                                         this.text_direction = value;
522                                         
523                                         this.PerformLayout (this, "TextDirection");
524                                                 
525                                         this.Invalidate ();
526                                 }
527                         }
528                 }
529
530                 [Browsable (false)]
531                 [EditorBrowsable (EditorBrowsableState.Never)]
532                 public new VScrollProperties VerticalScroll {
533                         get { return base.VerticalScroll; }
534                 }
535                 #endregion
536
537                 #region Protected Properties
538                 protected virtual DockStyle DefaultDock { get { return DockStyle.Top; } }
539                 protected virtual Padding DefaultGripMargin { get { return new Padding (2); } }
540                 protected override Padding DefaultMargin { get { return Padding.Empty; } }
541                 protected override Padding DefaultPadding { get { return new Padding (0, 0, 1, 0); } }
542                 protected virtual bool DefaultShowItemToolTips { get { return true; } }
543                 protected override Size DefaultSize { get { return new Size (100, 25); } }
544                 protected internal virtual ToolStripItemCollection DisplayedItems { get { return this.displayed_items; } }
545                 protected internal virtual Size MaxItemSize {
546                         get { return new Size (Width - (GripStyle == ToolStripGripStyle.Hidden ? 1 : 8), Height); }
547                 }
548                 #endregion
549
550                 #region Public Methods
551                 [EditorBrowsable (EditorBrowsableState.Never)]
552                 public new Control GetChildAtPoint (Point point)
553                 {
554                         return base.GetChildAtPoint (point);
555                 }
556
557                 [EditorBrowsable (EditorBrowsableState.Never)]
558                 public new Control GetChildAtPoint (Point pt, GetChildAtPointSkip skipValue)
559                 {
560                         return base.GetChildAtPoint (pt, skipValue);
561                 }
562                 
563                 public ToolStripItem GetItemAt (Point point)
564                 {
565                         foreach (ToolStripItem tsi in this.displayed_items)
566                                 if (tsi.Visible && tsi.Bounds.Contains (point))
567                                         return tsi;
568
569                         return null;
570                 }
571
572                 public ToolStripItem GetItemAt (int x, int y)
573                 {
574                         return GetItemAt (new Point (x, y));
575                 }
576
577                 public virtual ToolStripItem GetNextItem (ToolStripItem start, ArrowDirection direction)
578                 {
579                         if (!Enum.IsDefined (typeof (ArrowDirection), direction))
580                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ArrowDirection", direction));
581
582                         ToolStripItem current_best = null;
583                         int current_best_point;
584                         
585                         switch (direction) {
586                                 case ArrowDirection.Right:
587                                         current_best_point = int.MaxValue;
588
589                                         if (start != null)
590                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
591                                                         if (loop_tsi.Left >= start.Right && loop_tsi.Left < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
592                                                                 current_best = loop_tsi;
593                                                                 current_best_point = loop_tsi.Left;
594                                                         }
595                                                         
596                                         if (current_best == null)
597                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
598                                                         if (loop_tsi.Left < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
599                                                                 current_best = loop_tsi;
600                                                                 current_best_point = loop_tsi.Left;
601                                                         }
602                                                         
603                                         break;
604                                 case ArrowDirection.Up:
605                                         current_best_point = int.MinValue;
606
607                                         if (start != null)
608                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
609                                                         if (loop_tsi.Bottom <= start.Top && loop_tsi.Top > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
610                                                                 current_best = loop_tsi;
611                                                                 current_best_point = loop_tsi.Top;
612                                                         }
613
614                                         if (current_best == null)
615                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
616                                                         if (loop_tsi.Top > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
617                                                                 current_best = loop_tsi;
618                                                                 current_best_point = loop_tsi.Top;
619                                                         }
620
621                                         break;
622                                 case ArrowDirection.Left:
623                                         current_best_point = int.MinValue;
624
625                                         if (start != null)
626                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
627                                                         if (loop_tsi.Right <= start.Left && loop_tsi.Left > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
628                                                                 current_best = loop_tsi;
629                                                                 current_best_point = loop_tsi.Left;
630                                                         }
631
632                                         if (current_best == null)
633                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
634                                                         if (loop_tsi.Left > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
635                                                                 current_best = loop_tsi;
636                                                                 current_best_point = loop_tsi.Left;
637                                                         }
638
639                                         break;
640                                 case ArrowDirection.Down:
641                                         current_best_point = int.MaxValue;
642
643                                         if (start != null) 
644                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
645                                                         if (loop_tsi.Top >= start.Bottom && loop_tsi.Bottom < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
646                                                                 current_best = loop_tsi;
647                                                                 current_best_point = loop_tsi.Top;
648                                                         }
649
650                                         if (current_best == null)
651                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
652                                                         if (loop_tsi.Top < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
653                                                                 current_best = loop_tsi;
654                                                                 current_best_point = loop_tsi.Top;
655                                                         }
656
657                                         break;
658                         }
659
660                         return current_best;
661                 }
662
663                 [EditorBrowsable (EditorBrowsableState.Never)]
664                 public void ResetMinimumSize ()
665                 {
666                         this.MinimumSize = new Size (-1, -1);
667                 }
668
669                 [EditorBrowsable (EditorBrowsableState.Never)]
670                 public new void SetAutoScrollMargin (int x, int y)
671                 {
672                         base.SetAutoScrollMargin (x, y);
673                 }
674                 
675                 public override string ToString ()
676                 {
677                         return String.Format ("{0}, Name: {1}, Items: {2}", base.ToString(), this.Name, this.items.Count.ToString ());
678                 }
679                 #endregion
680
681                 #region Protected Methods
682                 protected override AccessibleObject CreateAccessibilityInstance ()
683                 {
684                         return new ToolStripAccessibleObject (this);
685                 }
686                 
687                 protected override ControlCollection CreateControlsInstance ()
688                 {
689                         return base.CreateControlsInstance ();
690                 }
691
692                 protected internal virtual ToolStripItem CreateDefaultItem (string text, Image image, EventHandler onClick)
693                 {
694                         if (text == "-")
695                                 return new ToolStripSeparator ();
696
697                         if (this is ToolStripDropDown)
698                                 return new ToolStripMenuItem (text, image, onClick);
699                                 
700                         return new ToolStripButton (text, image, onClick);
701                 }
702
703                 protected virtual LayoutSettings CreateLayoutSettings (ToolStripLayoutStyle layoutStyle)
704                 {
705                         switch (layoutStyle) {
706                                 case ToolStripLayoutStyle.Flow:
707                                         return new FlowLayoutSettings ();
708                                 case ToolStripLayoutStyle.Table:
709                                         //return new TableLayoutSettings ();
710                                 case ToolStripLayoutStyle.StackWithOverflow:
711                                 case ToolStripLayoutStyle.HorizontalStackWithOverflow:
712                                 case ToolStripLayoutStyle.VerticalStackWithOverflow:
713                                 default:
714                                         return null;
715                         }
716                 }
717                 
718                 protected override void Dispose (bool disposing)
719                 {
720                         if (!IsDisposed) {
721                                 foreach (ToolStripItem tsi in Items)
722                                         tsi.Dispose ();
723                                         
724                                 if (this.overflow_button != null && this.overflow_button.drop_down != null)
725                                         this.overflow_button.drop_down.Dispose ();
726
727                                 ToolStripManager.RemoveToolStrip (this);
728                                 base.Dispose (disposing);
729                         }
730                 }
731
732                 [MonoTODO ("Not called")]
733                 protected virtual void OnBeginDrag (EventArgs e)
734                 {
735                         EventHandler eh = (EventHandler)(Events[BeginDragEvent]);
736                         if (eh != null)
737                                 eh (this, e);
738                 }
739                 
740                 protected override void OnDockChanged (EventArgs e)
741                 {
742                         base.OnDockChanged (e);
743                 }
744
745                 [MonoTODO ("Not called")]
746                 protected virtual void OnEndDrag (EventArgs e)
747                 {
748                         EventHandler eh = (EventHandler)(Events[EndDragEvent]);
749                         if (eh != null)
750                                 eh (this, e);
751                 }
752
753                 protected override bool IsInputChar (char charCode)
754                 {
755                         return base.IsInputChar (charCode);
756                 }
757
758                 protected override bool IsInputKey (Keys keyData)
759                 {
760                         return base.IsInputKey (keyData);
761                 }
762                 
763                 protected override void OnEnabledChanged (EventArgs e)
764                 {
765                         base.OnEnabledChanged (e);
766                         
767                         foreach (ToolStripItem tsi in this.Items)
768                                 tsi.OnParentEnabledChanged (EventArgs.Empty);
769                 }
770
771                 protected override void OnFontChanged (EventArgs e)
772                 {
773                         base.OnFontChanged (e);
774                 }
775
776                 protected override void OnHandleCreated (EventArgs e)
777                 {
778                         base.OnHandleCreated (e);
779                 }
780
781                 protected override void OnHandleDestroyed (EventArgs e)
782                 {
783                         base.OnHandleDestroyed (e);
784                 }
785
786                 protected override void OnInvalidated (InvalidateEventArgs e)
787                 {
788                         base.OnInvalidated (e);
789                 }
790
791                 protected internal virtual void OnItemAdded (ToolStripItemEventArgs e)
792                 {
793                         if (e.Item.InternalVisible)
794                                 e.Item.Available = true;
795                                 
796                         e.Item.SetPlacement (ToolStripItemPlacement.Main);
797                         
798                         if (this.Created)
799                                 this.PerformLayout ();
800                         
801                         ToolStripItemEventHandler eh = (ToolStripItemEventHandler)(Events [ItemAddedEvent]);
802                         if (eh != null)
803                                 eh (this, e);
804                 }
805
806                 protected virtual void OnItemClicked (ToolStripItemClickedEventArgs e)
807                 {
808                         if (this.KeyboardActive)
809                                 ToolStripManager.SetActiveToolStrip (null, false);
810                         
811                         ToolStripItemClickedEventHandler eh = (ToolStripItemClickedEventHandler)(Events [ItemClickedEvent]);
812                         if (eh != null)
813                                 eh (this, e);
814                 }
815
816                 protected internal virtual void OnItemRemoved (ToolStripItemEventArgs e)
817                 {
818                         ToolStripItemEventHandler eh = (ToolStripItemEventHandler)(Events [ItemRemovedEvent]);
819                         if (eh != null)
820                                 eh (this, e);
821                 }
822
823                 protected override void OnLayout (LayoutEventArgs e)
824                 {
825                         base.OnLayout (e);
826
827                         this.SetDisplayedItems ();
828                         this.OnLayoutCompleted (EventArgs.Empty);
829                         this.Invalidate ();
830                 }
831
832                 protected virtual void OnLayoutCompleted (EventArgs e)
833                 {
834                         EventHandler eh = (EventHandler)(Events [LayoutCompletedEvent]);
835                         if (eh != null)
836                                 eh (this, e);
837                 }
838
839                 protected virtual void OnLayoutStyleChanged (EventArgs e)
840                 {
841                         EventHandler eh = (EventHandler)(Events[LayoutStyleChangedEvent]);
842                         if (eh != null)
843                                 eh (this, e);
844                 }
845
846                 protected override void OnLeave (EventArgs e)
847                 {
848                         base.OnLeave (e);
849                 }
850
851                 protected override void OnLostFocus (EventArgs e)
852                 {
853                         base.OnLostFocus (e);
854                 }
855
856                 protected override void OnMouseCaptureChanged (EventArgs e)
857                 {
858                         base.OnMouseCaptureChanged (e);
859                 }
860                 
861                 protected override void OnMouseDown (MouseEventArgs mea)
862                 {
863                         if (mouse_currently_over != null)
864                         {
865                                 ToolStripItem focused = GetCurrentlyFocusedItem ();
866
867                                 if (focused != null && focused != mouse_currently_over)
868                                         this.FocusInternal (true);
869
870                                 if (this is MenuStrip && !(mouse_currently_over as ToolStripMenuItem).HasDropDownItems) {
871                                         if (!menu_selected)
872                                                 (this as MenuStrip).FireMenuActivate ();
873                                         
874                                         return;
875                                 }
876                                         
877                                 mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseDown);
878                                 
879                                 if (this is MenuStrip && !menu_selected) {
880                                         (this as MenuStrip).FireMenuActivate ();
881                                         menu_selected = true;                           
882                                 }
883                         } else {
884                                 if (this is MenuStrip)
885                                         this.HideMenus (true, ToolStripDropDownCloseReason.AppClicked);
886                         }
887                         
888                         if (this is MenuStrip)
889                                 this.Capture = false;
890
891                         base.OnMouseDown (mea);
892                 }
893
894                 protected override void OnMouseLeave (EventArgs e)
895                 {
896                         if (mouse_currently_over != null) {
897                                 MouseLeftItem (mouse_currently_over);
898                                 mouse_currently_over.FireEvent (e, ToolStripItemEventType.MouseLeave);
899                                 mouse_currently_over = null;
900                         }
901
902                         base.OnMouseLeave (e);
903                 }
904
905                 protected override void OnMouseMove (MouseEventArgs mea)
906                 {
907                         ToolStripItem tsi;
908                         // Find the item we are now 
909                         if (this.overflow_button != null && this.overflow_button.Visible && this.overflow_button.Bounds.Contains (mea.Location))
910                                 tsi = this.overflow_button;
911                         else
912                                 tsi = this.GetItemAt (mea.X, mea.Y);
913
914                         if (tsi != null) {
915                                 // If we were already hovering on this item, just send a mouse move
916                                 if (tsi == mouse_currently_over) 
917                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseMove);
918                                 else {
919                                         // If we were over a different item, fire a mouse leave on it
920                                         if (mouse_currently_over != null) {
921                                                 MouseLeftItem (tsi);
922                                                 mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseLeave);
923                                         }
924                                         
925                                         // Set the new item we are currently over
926                                         mouse_currently_over = tsi;
927                                         
928                                         // Fire mouse enter and mouse move
929                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseEnter);
930                                         MouseEnteredItem (tsi);
931                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseMove);
932
933                                         // If we're over something with a drop down, show it
934                                         if (menu_selected && mouse_currently_over.Enabled && mouse_currently_over is ToolStripDropDownItem && (mouse_currently_over as ToolStripDropDownItem).HasDropDownItems)
935                                                 (mouse_currently_over as ToolStripDropDownItem).ShowDropDown ();
936                                 }
937                         } else {
938                                 // We're not over anything now, just fire the mouse leave on what we used to be over
939                                 if (mouse_currently_over != null) {
940                                         MouseLeftItem (tsi);
941                                         mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseLeave);
942                                         mouse_currently_over = null;
943                                 }
944                         }
945                         
946                         base.OnMouseMove (mea);
947                 }
948
949                 protected override void OnMouseUp (MouseEventArgs mea)
950                 {
951                         // If we're currently over an item (set in MouseMove)
952                         if (mouse_currently_over != null && !(mouse_currently_over is ToolStripControlHost) && mouse_currently_over.Enabled) {
953                                 // Fire our ItemClicked event
954                                 OnItemClicked (new ToolStripItemClickedEventArgs (mouse_currently_over));
955                                         
956                                 // Fire the item's MouseUp event
957                                 if (mouse_currently_over != null)
958                                         mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseUp);
959
960                                 // The event handler may have blocked until the mouse moved off of the ToolStripItem
961                                 if (mouse_currently_over == null)
962                                         return;
963                         }
964
965                         base.OnMouseUp (mea);
966                 }
967
968                 protected override void OnPaint (PaintEventArgs e)
969                 {
970                         base.OnPaint (e);
971
972                         // Draw the grip
973                         this.OnPaintGrip (e);
974
975                         // Make each item draw itself
976                         foreach (ToolStripItem tsi in this.displayed_items) {
977                                 if (tsi.Visible) {
978                                         e.Graphics.TranslateTransform (tsi.Bounds.Left, tsi.Bounds.Top);
979                                         tsi.FireEvent (e, ToolStripItemEventType.Paint);
980                                         e.Graphics.ResetTransform ();
981                                 }
982                         }
983
984                         // Paint the Overflow button if it's visible
985                         if (this.overflow_button != null && this.overflow_button.Visible) {
986                                 e.Graphics.TranslateTransform (this.overflow_button.Bounds.Left, this.overflow_button.Bounds.Top);
987                                 this.overflow_button.FireEvent (e, ToolStripItemEventType.Paint);
988                                 e.Graphics.ResetTransform ();
989                         }
990
991                         Rectangle affected_bounds = new Rectangle (Point.Empty, this.Size);
992
993                         ToolStripRenderEventArgs pevent = new ToolStripRenderEventArgs (e.Graphics, this, affected_bounds, Color.Empty);
994                         pevent.InternalConnectedArea = CalculateConnectedArea ();
995
996                         this.Renderer.DrawToolStripBorder (pevent);
997                 }
998
999                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1000                 protected override void OnPaintBackground (PaintEventArgs e)
1001                 {
1002                         base.OnPaintBackground (e);
1003
1004                         Rectangle affected_bounds = new Rectangle (Point.Empty, this.Size);
1005                         ToolStripRenderEventArgs tsrea = new ToolStripRenderEventArgs (e.Graphics, this, affected_bounds, SystemColors.Control);
1006                         
1007                         this.Renderer.DrawToolStripBackground (tsrea);
1008                 }
1009
1010                 protected internal virtual void OnPaintGrip (PaintEventArgs e)
1011                 {
1012                         // Never draw a grip with these two layouts
1013                         if (this.layout_style == ToolStripLayoutStyle.Flow || this.layout_style == ToolStripLayoutStyle.Table)
1014                                 return;
1015                         
1016                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintGripEvent]);
1017                         if (eh != null)
1018                                 eh (this, e);
1019
1020                         if (!(this is MenuStrip)) {
1021                                 if (this.orientation == Orientation.Horizontal)
1022                                         e.Graphics.TranslateTransform (2, 0);
1023                                 else
1024                                         e.Graphics.TranslateTransform (0, 2);
1025                         }
1026
1027                         this.Renderer.DrawGrip (new ToolStripGripRenderEventArgs (e.Graphics, this, this.GripRectangle, this.GripDisplayStyle, this.grip_style));
1028                         e.Graphics.ResetTransform ();
1029                 }
1030
1031                 protected virtual void OnRendererChanged (EventArgs e)
1032                 {
1033                         EventHandler eh = (EventHandler)(Events [RendererChangedEvent]);
1034                         if (eh != null)
1035                                 eh (this, e);
1036                 }
1037
1038                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1039                 protected override void OnRightToLeftChanged (EventArgs e)
1040                 {
1041                         base.OnRightToLeftChanged (e);
1042
1043                         foreach (ToolStripItem tsi in this.Items)
1044                                 tsi.OnParentRightToLeftChanged (e);
1045                 }
1046
1047                 protected override void OnScroll (ScrollEventArgs se)
1048                 {
1049                         base.OnScroll (se);
1050                 }
1051                 
1052                 protected override void OnTabStopChanged (EventArgs e)
1053                 {
1054                         base.OnTabStopChanged (e);
1055                 }
1056
1057                 protected override void OnVisibleChanged (EventArgs e)
1058                 {
1059                         base.OnVisibleChanged (e);
1060                 }
1061
1062                 protected override bool ProcessCmdKey (ref Message m, Keys keyData)
1063                 {
1064                         return base.ProcessCmdKey (ref m, keyData);
1065                 }
1066
1067                 protected override bool ProcessDialogKey (Keys keyData)
1068                 {
1069                         if (!this.KeyboardActive)
1070                                 return false;
1071                                 
1072                         // Give each item a chance to handle the key
1073                         foreach (ToolStripItem tsi in this.Items)
1074                                 if (tsi.ProcessDialogKey (keyData))
1075                                         return true;
1076                         
1077                         // See if I want to handle it
1078                         if (this.ProcessArrowKey (keyData))
1079                                 return true;
1080                         
1081                         ToolStrip ts = null;
1082                         
1083                         switch (keyData) {
1084                                 case Keys.Escape:
1085                                         this.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1086                                         return true;
1087                         
1088                                 case Keys.Control | Keys.Tab:
1089                                         ts = ToolStripManager.GetNextToolStrip (this, true);
1090                                         
1091                                         if (ts != null) {
1092                                                 foreach (ToolStripItem tsi in this.Items)
1093                                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1094
1095                                                 ToolStripManager.SetActiveToolStrip (ts, true);
1096                                                 ts.SelectNextToolStripItem (null, true);
1097                                         }
1098                                         
1099                                         return true;
1100                                 case Keys.Control | Keys.Shift | Keys.Tab:
1101                                         ts = ToolStripManager.GetNextToolStrip (this, false);
1102
1103                                         if (ts != null) {
1104                                                 foreach (ToolStripItem tsi in this.Items)
1105                                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1106
1107                                                 ToolStripManager.SetActiveToolStrip (ts, true);
1108                                                 ts.SelectNextToolStripItem (null, true);
1109                                         }
1110                                         
1111                                         return true;
1112                                 case Keys.Down:
1113                                 case Keys.Up:
1114                                 case Keys.Left:
1115                                 case Keys.Right:
1116                                         if (GetCurrentlySelectedItem () is ToolStripControlHost)
1117                                                 return false;
1118                                         break;
1119                         }
1120
1121                         return base.ProcessDialogKey (keyData);
1122                 }
1123
1124                 protected override bool ProcessMnemonic (char charCode)
1125                 {
1126                         // If any item has an explicit mnemonic, it gets the message
1127                         foreach (ToolStripItem tsi in this.Items)
1128                                 if (tsi.Enabled && tsi.Visible && !string.IsNullOrEmpty (tsi.Text) && Control.IsMnemonic (charCode, tsi.Text))
1129                                         return tsi.ProcessMnemonic (charCode);
1130
1131                         string code = Char.ToUpper (charCode).ToString ();
1132                         
1133                         // If any item's text starts with our letter, it gets the message
1134                         if (this is ToolStripDropDownMenu)
1135                                 foreach (ToolStripItem tsi in this.Items)
1136                                         if (tsi.Enabled && tsi.Visible && !string.IsNullOrEmpty (tsi.Text) && tsi.Text.ToUpper ().StartsWith (code) && !(tsi is ToolStripControlHost))
1137                                                 return tsi.ProcessMnemonic (charCode);
1138
1139                         return base.ProcessMnemonic (charCode);
1140                 }
1141                 
1142                 [MonoTODO ()]
1143                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1144                 protected virtual void RestoreFocus ()
1145                 {
1146                 }
1147
1148                 [MonoTODO ()]
1149                 protected override void Select (bool directed, bool forward)
1150                 {
1151                         foreach (ToolStripItem tsi in this.DisplayedItems)
1152                                 if (tsi.CanSelect) {
1153                                         tsi.Select ();
1154                                         break;
1155                                 }
1156                 }
1157                 
1158                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1159                 {
1160                         base.SetBoundsCore (x, y, width, height, specified);
1161                 }
1162
1163                 protected virtual void SetDisplayedItems ()
1164                 {
1165                         this.displayed_items.Clear ();
1166                         
1167                         foreach (ToolStripItem tsi in this.items)
1168                                 if (tsi.Placement == ToolStripItemPlacement.Main && tsi.Available) {
1169                                         this.displayed_items.AddNoOwnerOrLayout (tsi);
1170                                         tsi.Parent = this; 
1171                                 }
1172                                 else if (tsi.Placement == ToolStripItemPlacement.Overflow)
1173                                         tsi.Parent = this.OverflowButton.DropDown; 
1174                         
1175                         if (this.OverflowButton != null)
1176                                 this.OverflowButton.DropDown.SetDisplayedItems ();
1177                 }
1178
1179                 protected internal void SetItemLocation (ToolStripItem item, Point location)
1180                 {
1181                         if (item == null)
1182                                 throw new ArgumentNullException ("item");
1183                                 
1184                         if (item.Owner != this)
1185                                 throw new NotSupportedException ("The item is not owned by this ToolStrip");
1186                                 
1187                         item.SetBounds (new Rectangle (location, item.Size));
1188                 }
1189                 
1190                 protected internal static void SetItemParent (ToolStripItem item, ToolStrip parent)
1191                 {
1192                         if (item.Owner != null) {
1193                                 item.Owner.Items.RemoveNoOwnerOrLayout (item);
1194
1195                                 if (item.Owner is ToolStripOverflow)
1196                                         (item.Owner as ToolStripOverflow).ParentToolStrip.Items.RemoveNoOwnerOrLayout (item);
1197                         }
1198                         
1199                         parent.Items.AddNoOwnerOrLayout (item);
1200                         item.Parent = parent;
1201                 }
1202
1203                 protected override void SetVisibleCore (bool visible)
1204                 {
1205                         base.SetVisibleCore (visible);
1206                 }
1207
1208                 protected override void WndProc (ref Message m)
1209                 {
1210                         base.WndProc (ref m);
1211                 }
1212                 #endregion
1213
1214                 #region Public Events
1215                 static object BeginDragEvent = new object ();
1216                 static object EndDragEvent = new object ();
1217                 static object ItemAddedEvent = new object ();
1218                 static object ItemClickedEvent = new object ();
1219                 static object ItemRemovedEvent = new object ();
1220                 static object LayoutCompletedEvent = new object ();
1221                 static object LayoutStyleChangedEvent = new object ();
1222                 static object PaintGripEvent = new object ();
1223                 static object RendererChangedEvent = new object ();
1224
1225                 [Browsable (true)]
1226                 [EditorBrowsable (EditorBrowsableState.Always)]
1227                 public new event EventHandler AutoSizeChanged {
1228                         add { base.AutoSizeChanged += value; }
1229                         remove { base.AutoSizeChanged -= value; }
1230                 }
1231
1232                 [MonoTODO ()]
1233                 public event EventHandler BeginDrag {
1234                         add { Events.AddHandler (BeginDragEvent, value); }
1235                         remove { Events.RemoveHandler (BeginDragEvent, value); }
1236                 }
1237
1238                 [Browsable (false)]
1239                 public new event EventHandler CausesValidationChanged {
1240                         add { base.CausesValidationChanged += value; }
1241                         remove { base.CausesValidationChanged -= value; }
1242                 }
1243
1244                 [Browsable (false)]
1245                 [EditorBrowsable (EditorBrowsableState.Never)]
1246                 public new event ControlEventHandler ControlAdded {
1247                         add { base.ControlAdded += value; }
1248                         remove { base.ControlAdded -= value; }
1249                 }
1250
1251                 [Browsable (false)]
1252                 [EditorBrowsable (EditorBrowsableState.Never)]
1253                 public new event ControlEventHandler ControlRemoved {
1254                         add { base.ControlRemoved += value; }
1255                         remove { base.ControlRemoved -= value; }
1256                 }
1257                 
1258                 [Browsable (false)]
1259                 public new event EventHandler CursorChanged {
1260                         add { base.CursorChanged += value; }
1261                         remove { base.CursorChanged -= value; }
1262                 }
1263
1264                 [MonoTODO ()]
1265                 public event EventHandler EndDrag {
1266                         add { Events.AddHandler (EndDragEvent, value); }
1267                         remove { Events.RemoveHandler (EndDragEvent, value); }
1268                 }
1269
1270                 [Browsable (false)]
1271                 public new event EventHandler ForeColorChanged {
1272                         add { base.ForeColorChanged += value; }
1273                         remove { base.ForeColorChanged -= value; }
1274                 }
1275
1276                 public event ToolStripItemEventHandler ItemAdded {
1277                         add { Events.AddHandler (ItemAddedEvent, value); }
1278                         remove { Events.RemoveHandler (ItemAddedEvent, value); }
1279                 }
1280
1281                 public event ToolStripItemClickedEventHandler ItemClicked {
1282                         add { Events.AddHandler (ItemClickedEvent, value); }
1283                         remove { Events.RemoveHandler (ItemClickedEvent, value); }
1284                 }
1285
1286                 public event ToolStripItemEventHandler ItemRemoved {
1287                         add { Events.AddHandler (ItemRemovedEvent, value); }
1288                         remove { Events.RemoveHandler (ItemRemovedEvent, value); }
1289                 }
1290
1291                 public event EventHandler LayoutCompleted {
1292                         add { Events.AddHandler (LayoutCompletedEvent, value); }
1293                         remove { Events.RemoveHandler (LayoutCompletedEvent, value); }
1294                 }
1295
1296                 public event EventHandler LayoutStyleChanged {
1297                         add { Events.AddHandler (LayoutStyleChangedEvent, value); }
1298                         remove { Events.RemoveHandler (LayoutStyleChangedEvent, value); }
1299                 }
1300
1301                 public event PaintEventHandler PaintGrip {
1302                         add { Events.AddHandler (PaintGripEvent, value); }
1303                         remove { Events.RemoveHandler (PaintGripEvent, value); }
1304                 }
1305
1306                 public event EventHandler RendererChanged {
1307                         add { Events.AddHandler (RendererChangedEvent, value); }
1308                         remove { Events.RemoveHandler (RendererChangedEvent, value); }
1309                 }
1310                 #endregion
1311
1312                 #region Internal Properties
1313                 internal virtual bool KeyboardActive
1314                 {
1315                         get { return this.keyboard_active; }
1316                         set {
1317                                 if (this.keyboard_active != value) {
1318                                         this.keyboard_active = value;
1319                                         
1320                                         if (value)
1321                                                 Application.KeyboardCapture = this;
1322                                         else if (Application.KeyboardCapture == this) {
1323                                                 Application.KeyboardCapture = null;
1324                                                 ToolStripManager.ActivatedByKeyboard = false;
1325                                         }
1326                                         
1327                                         // Redraw for mnemonic underlines
1328                                         this.Invalidate ();
1329                                 }
1330                         }
1331                 }
1332                 #endregion
1333                 
1334                 #region Private Methods
1335                 internal virtual Rectangle CalculateConnectedArea ()
1336                 {
1337                         return Rectangle.Empty;
1338                 }
1339                 
1340                 internal void ChangeSelection (ToolStripItem nextItem)
1341                 {
1342                         if (Application.KeyboardCapture != this)
1343                                 ToolStripManager.SetActiveToolStrip (this, ToolStripManager.ActivatedByKeyboard);
1344                                 
1345                         foreach (ToolStripItem tsi in this.Items)
1346                                 if (tsi != nextItem)
1347                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1348
1349                         ToolStripItem current = GetCurrentlySelectedItem ();
1350
1351                         if (current != null && !(current is ToolStripControlHost))
1352                                 this.FocusInternal (true);
1353
1354                         if (nextItem is ToolStripControlHost)
1355                                 (nextItem as ToolStripControlHost).Focus ();
1356
1357                         nextItem.Select ();
1358                         
1359                         if (nextItem.Parent is MenuStrip && (nextItem.Parent as MenuStrip).MenuDroppedDown)
1360                                 (nextItem as ToolStripMenuItem).HandleAutoExpansion ();
1361                 }
1362                 
1363                 internal virtual void Dismiss ()
1364                 {
1365                         this.Dismiss (ToolStripDropDownCloseReason.AppClicked);
1366                 }
1367                 
1368                 internal virtual void Dismiss (ToolStripDropDownCloseReason reason)
1369                 {
1370                         // Release our stranglehold on the keyboard
1371                         this.KeyboardActive = false;
1372                         
1373                         // Set our drop down flag to false;
1374                         this.menu_selected = false;
1375                         
1376                         // Make sure all of our items are deselected and repainted
1377                         foreach (ToolStripItem tsi in this.Items)
1378                                 tsi.Dismiss (reason);
1379                                 
1380                         // We probably need to redraw for mnemonic underlines
1381                         this.Invalidate ();
1382                 }
1383
1384                 internal ToolStripItem GetCurrentlySelectedItem ()
1385                 {
1386                         foreach (ToolStripItem tsi in this.DisplayedItems)
1387                                 if (tsi.Selected)
1388                                         return tsi;
1389                                         
1390                         return null;
1391                 }
1392                 
1393                 internal ToolStripItem GetCurrentlyFocusedItem ()
1394                 {
1395                         foreach (ToolStripItem tsi in this.DisplayedItems)
1396                                 if ((tsi is ToolStripControlHost) && (tsi as ToolStripControlHost).Control.Focused)
1397                                         return tsi;
1398
1399                         return null;
1400                 }
1401
1402                 internal override Size GetPreferredSizeCore (Size proposedSize)
1403                 {
1404                         return GetToolStripPreferredSize (proposedSize);
1405                 }
1406                 
1407                 internal virtual Size GetToolStripPreferredSize (Size proposedSize)
1408                 {
1409                         Size new_size = Size.Empty;
1410
1411                         // TODO: This is total duct tape.  We really have to call into the correct
1412                         // layout engine, do a dry run of the layout, and find out our true
1413                         // preferred dimensions.
1414                         if (this.LayoutStyle == ToolStripLayoutStyle.Flow) {
1415                                 Point currentLocation = Point.Empty;
1416                                 int tallest = 0;
1417                                 
1418                                 foreach (ToolStripItem tsi in items) {
1419                                         if ((DisplayRectangle.Width - currentLocation.X) < (tsi.Width + tsi.Margin.Horizontal)) {
1420
1421                                                 currentLocation.Y += tallest;
1422                                                 tallest = 0;
1423                                                 
1424                                                 currentLocation.X = DisplayRectangle.Left;
1425                                         }
1426
1427                                         // Offset the left margin and set the control to our point
1428                                         currentLocation.Offset (tsi.Margin.Left, 0);
1429                                         tallest = Math.Max (tallest, tsi.Height + tsi.Margin.Vertical);
1430                                         
1431                                         // Update our location pointer
1432                                         currentLocation.X += tsi.Width + tsi.Margin.Right;
1433                                 }
1434
1435                                 currentLocation.Y += tallest;
1436                                 return new Size (currentLocation.X, currentLocation.Y);
1437                         }
1438                                 
1439                         if (this.orientation == Orientation.Vertical) {
1440                                 foreach (ToolStripItem tsi in this.items)
1441                                         if (tsi.Available)  {
1442                                                 Size tsi_preferred = tsi.GetPreferredSize (Size.Empty);
1443                                                 new_size.Height += tsi_preferred.Height + tsi.Margin.Top + tsi.Margin.Bottom;
1444
1445                                                 if (new_size.Width < (this.Padding.Horizontal + tsi_preferred.Width + tsi.Margin.Horizontal))
1446                                                         new_size.Width = (this.Padding.Horizontal + tsi_preferred.Width + tsi.Margin.Horizontal);
1447                                         }
1448
1449                                 new_size.Height += (this.GripRectangle.Height + this.GripMargin.Vertical + this.Padding.Vertical + 4);
1450                                 
1451                                 if (new_size.Width == 0)
1452                                         new_size.Width = ExplicitBounds.Width;
1453                                         
1454                                 return new_size;
1455                         } else {
1456                                 foreach (ToolStripItem tsi in this.items) 
1457                                         if (tsi.Available) {
1458                                                 Size tsi_preferred = tsi.GetPreferredSize (Size.Empty);
1459                                                 new_size.Width += tsi_preferred.Width + tsi.Margin.Left + tsi.Margin.Right;
1460                                                 
1461                                                 if (new_size.Height < (this.Padding.Vertical + tsi_preferred.Height + tsi.Margin.Vertical))
1462                                                         new_size.Height = (this.Padding.Vertical + tsi_preferred.Height + tsi.Margin.Vertical);
1463                                         }
1464                                         
1465                                 new_size.Width += (this.GripRectangle.Width + this.GripMargin.Horizontal + this.Padding.Horizontal + 4);
1466
1467                                 if (new_size.Height == 0)
1468                                         new_size.Height = ExplicitBounds.Height;
1469
1470                                 if (this is StatusStrip)
1471                                         new_size.Height = Math.Max (new_size.Height, 22);
1472                                         
1473                                 return new_size;
1474                         }
1475                 }
1476                 
1477                 internal virtual ToolStrip GetTopLevelToolStrip ()
1478                 {
1479                         return this;
1480                 }
1481                 
1482                 internal virtual void HandleItemClick (ToolStripItem dismissingItem)
1483                 {
1484                         this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.ItemClicked);
1485                 }
1486                 
1487                 internal void HideMenus (bool release, ToolStripDropDownCloseReason reason)
1488                 {
1489                         if (this is MenuStrip && release && menu_selected)
1490                                 (this as MenuStrip).FireMenuDeactivate ();
1491                                 
1492                         if (release)
1493                                 menu_selected = false;
1494                                 
1495                         NotifySelectedChanged (null);
1496                 }
1497
1498                 internal void NotifySelectedChanged (ToolStripItem tsi)
1499                 {
1500                         foreach (ToolStripItem tsi2 in this.DisplayedItems)
1501                                 if (tsi != tsi2)
1502                                         if (tsi2 is ToolStripDropDownItem)
1503                                                 (tsi2 as ToolStripDropDownItem).HideDropDown (ToolStripDropDownCloseReason.Keyboard);
1504
1505                         if (this.OverflowButton != null) {
1506                                 ToolStripItemCollection tsic = this.OverflowButton.DropDown.DisplayedItems;
1507                                 
1508                                 foreach (ToolStripItem tsi2 in tsic)
1509                                         if (tsi != tsi2)
1510                                                 if (tsi2 is ToolStripDropDownItem)
1511                                                         (tsi2 as ToolStripDropDownItem).HideDropDown (ToolStripDropDownCloseReason.Keyboard);
1512                         
1513                                 this.OverflowButton.HideDropDown ();
1514                         }
1515                         
1516                         foreach (ToolStripItem tsi2 in this.Items)
1517                                 if (tsi != tsi2)
1518                                         tsi2.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1519                 }
1520                 
1521                 internal virtual bool OnMenuKey ()
1522                 {
1523                         return false;
1524                 }
1525
1526                 internal virtual bool ProcessArrowKey (Keys keyData)
1527                 {
1528                         ToolStripItem tsi;
1529                         
1530                         switch (keyData) {
1531                                 case Keys.Right:
1532                                         tsi = this.GetCurrentlySelectedItem ();
1533                                         
1534                                         if (tsi is ToolStripControlHost)
1535                                                 return false;
1536                                         
1537                                         tsi = this.SelectNextToolStripItem (tsi, true);
1538                                         
1539                                         if (tsi is ToolStripControlHost)
1540                                                 (tsi as ToolStripControlHost).Focus ();
1541                                                 
1542                                         return true;
1543                                 case Keys.Tab:
1544                                         tsi = this.GetCurrentlySelectedItem ();
1545
1546                                         tsi = this.SelectNextToolStripItem (tsi, true);
1547
1548                                         if (tsi is ToolStripControlHost)
1549                                                 (tsi as ToolStripControlHost).Focus ();
1550                                                 
1551                                         return true;
1552                                 case Keys.Left:
1553                                         tsi = this.GetCurrentlySelectedItem ();
1554
1555                                         if (tsi is ToolStripControlHost)
1556                                                 return false;
1557
1558                                         tsi = this.SelectNextToolStripItem (tsi, false);
1559
1560                                         if (tsi is ToolStripControlHost)
1561                                                 (tsi as ToolStripControlHost).Focus ();
1562
1563                                         return true;
1564                                 case Keys.Shift | Keys.Tab:
1565                                         tsi = this.GetCurrentlySelectedItem ();
1566                                         
1567                                         tsi = this.SelectNextToolStripItem (tsi, false);
1568
1569                                         if (tsi is ToolStripControlHost)
1570                                                 (tsi as ToolStripControlHost).Focus ();
1571
1572                                         return true;
1573                         }
1574
1575                         return false;
1576                 }
1577
1578                 internal virtual ToolStripItem SelectNextToolStripItem (ToolStripItem start, bool forward)
1579                 {
1580                         ToolStripItem next_item = this.GetNextItem (start, forward ? ArrowDirection.Right : ArrowDirection.Left);
1581                         
1582                         if (next_item == null)
1583                                 return next_item;
1584                                 
1585                         this.ChangeSelection (next_item);
1586
1587                         if (next_item is ToolStripControlHost)
1588                                 (next_item as ToolStripControlHost).Focus ();
1589                 
1590                         return next_item;
1591                 }
1592
1593                 #region Stuff for ToolTips
1594                 private void MouseEnteredItem (ToolStripItem item)
1595                 {
1596                         if (this.show_item_tool_tips && !(item is ToolStripTextBox)) {
1597                                 tooltip_currently_showing = item;
1598                                 ToolTipTimer.Start ();
1599                         }
1600                 }
1601                 
1602                 private void MouseLeftItem (ToolStripItem item)
1603                 {
1604                         ToolTipTimer.Stop ();
1605                         ToolTipWindow.Hide (this);
1606                         tooltip_currently_showing = null;
1607                 }
1608                 
1609                 private Timer ToolTipTimer {
1610                         get {
1611                                 if (tooltip_timer == null) {
1612                                         tooltip_timer = new Timer ();
1613                                         tooltip_timer.Enabled = false;
1614                                         tooltip_timer.Interval = 500;
1615                                         tooltip_timer.Tick += new EventHandler (ToolTipTimer_Tick);
1616                                 }
1617                                 
1618                                 return tooltip_timer;
1619                         }
1620                 }
1621                 
1622                 private ToolTip ToolTipWindow {
1623                         get {
1624                                 if (tooltip_window == null)
1625                                         tooltip_window = new ToolTip ();
1626                                         
1627                                 return tooltip_window;
1628                         }
1629                 }
1630                 
1631                 private void ToolTipTimer_Tick (object o, EventArgs args)
1632                 {
1633                         string tooltip = tooltip_currently_showing.GetToolTip ();
1634                         
1635                         if (!string.IsNullOrEmpty (tooltip))
1636                                 ToolTipWindow.Present (this, tooltip);
1637
1638                         tooltip_currently_showing.FireEvent (EventArgs.Empty, ToolStripItemEventType.MouseHover);
1639
1640                         ToolTipTimer.Stop ();
1641                 }
1642                 #endregion
1643
1644                 #region Stuff for Merging
1645                 internal ToolStrip CurrentlyMergedWith {
1646                         get { return this.currently_merged_with; }
1647                         set { this.currently_merged_with = value; }
1648                 }
1649                 
1650                 internal List<ToolStripItem> HiddenMergedItems {
1651                         get {
1652                                 if (this.hidden_merged_items == null)
1653                                         this.hidden_merged_items = new List<ToolStripItem> ();
1654                                         
1655                                 return this.hidden_merged_items;
1656                         }
1657                 }
1658                 
1659                 internal bool IsCurrentlyMerged {
1660                         get { return this.is_currently_merged; }
1661                         set { 
1662                                 this.is_currently_merged = value; 
1663                                 
1664                                 if (!value && this is MenuStrip) 
1665                                         foreach (ToolStripMenuItem tsmi in this.Items)
1666                                                 tsmi.DropDown.IsCurrentlyMerged = value;
1667                          }
1668                 }
1669                 
1670                 internal void BeginMerge ()
1671                 {
1672                         if (!IsCurrentlyMerged) {
1673                                 IsCurrentlyMerged = true;
1674                                 
1675                                 if (this.pre_merge_items == null) {
1676                                         this.pre_merge_items = new List<ToolStripItem> ();
1677                         
1678                                 foreach (ToolStripItem tsi in this.Items)
1679                                         this.pre_merge_items.Add (tsi);
1680                                 }
1681                         }
1682                 }
1683                 
1684                 internal void RevertMergeItem (ToolStripItem item)
1685                 {
1686                         int index = 0;
1687
1688                         // Remove it from it's current Parent
1689                         if (item.Parent != null && item.Parent != this) {
1690                                 if (item.Parent is ToolStripOverflow)
1691                                         (item.Parent as ToolStripOverflow).ParentToolStrip.Items.RemoveNoOwnerOrLayout (item);
1692                                 else
1693                                         item.Parent.Items.RemoveNoOwnerOrLayout (item);
1694
1695                                 item.Parent = item.Owner;       
1696                         }
1697                         
1698                         // Find where the item was before the merge
1699                         index = item.Owner.pre_merge_items.IndexOf (item);
1700
1701                         // Find the first pre-merge item that was after this item, that
1702                         // is currently in the Items collection.  Insert our item before
1703                         // that one.
1704                         for (int i = index; i < this.pre_merge_items.Count; i++) {
1705                                 if (this.Items.Contains (this.pre_merge_items[i])) {
1706                                         item.Owner.Items.InsertNoOwnerOrLayout (this.Items.IndexOf (this.pre_merge_items[i]), item);
1707                                         return;
1708                                 }
1709                         }
1710                         
1711                         // There aren't any items that are supposed to be after this item,
1712                         // so just append it to the end.
1713                         item.Owner.Items.AddNoOwnerOrLayout (item);
1714                 }
1715                 #endregion
1716                 #endregion
1717
1718                 #region ToolStripAccessibleObject
1719                 [ComVisible (true)]
1720                 public class ToolStripAccessibleObject : ControlAccessibleObject
1721                 {
1722                         #region Public Constructor
1723                         public ToolStripAccessibleObject (ToolStrip owner) : base (owner)
1724                         {
1725                         }
1726                         #endregion
1727                         
1728                         #region Public Properties
1729                         public override AccessibleRole Role {
1730                                 get { return AccessibleRole.ToolBar; }
1731                         }
1732                         #endregion
1733
1734                         #region Public Methods
1735                         public override AccessibleObject GetChild (int index)
1736                         {
1737                                 return base.GetChild (index);
1738                         }
1739
1740                         public override int GetChildCount ()
1741                         {
1742                                 return (owner as ToolStrip).Items.Count;
1743                         }
1744
1745                         public override AccessibleObject HitTest (int x, int y)
1746                         {
1747                                 return base.HitTest (x, y);
1748                         }
1749                         #endregion
1750                 }
1751                 #endregion
1752         }
1753 }
1754 #endif