2007-08-17 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
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.back_color = Control.DefaultBackColor;
102                         this.can_overflow = true;
103                         base.CausesValidation = false;
104                         this.default_drop_down_direction = ToolStripDropDownDirection.BelowRight;
105                         this.displayed_items = new ToolStripItemCollection (this, null, true);
106                         this.Dock = this.DefaultDock;
107                         base.Font = new Font ("Tahoma", 8.25f);
108                         this.fore_color = Control.DefaultForeColor;
109                         this.grip_margin = this.DefaultGripMargin;
110                         this.grip_style = ToolStripGripStyle.Visible;
111                         this.image_scaling_size = new Size (16, 16);
112                         this.layout_style = ToolStripLayoutStyle.HorizontalStackWithOverflow;
113                         this.orientation = Orientation.Horizontal;
114                         if (!(this is ToolStripDropDown))
115                                 this.overflow_button = new ToolStripOverflowButton (this);
116                         this.renderer = null;
117                         this.render_mode = ToolStripRenderMode.ManagerRenderMode;
118                         this.show_item_tool_tips = this.DefaultShowItemToolTips;
119                         base.TabStop = false;
120                         this.text_direction = ToolStripTextDirection.Horizontal;
121                         this.ResumeLayout ();
122                         DoAutoSize ();
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 = false; }
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 { base.TabStop = value; }
508                 }
509
510                 [DefaultValue (ToolStripTextDirection.Horizontal)]
511                 public virtual ToolStripTextDirection TextDirection {
512                         get { return this.text_direction; }
513                         set {
514                                 if (!Enum.IsDefined (typeof (ToolStripTextDirection), value))
515                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripTextDirection", value));
516
517                                 if (this.text_direction != value) {
518                                         this.text_direction = value;
519                                         
520                                         this.PerformLayout (this, "TextDirection");
521                                                 
522                                         this.Invalidate ();
523                                 }
524                         }
525                 }
526
527                 [Browsable (false)]
528                 [EditorBrowsable (EditorBrowsableState.Never)]
529                 public new VScrollProperties VerticalScroll {
530                         get { return base.VerticalScroll; }
531                 }
532                 #endregion
533
534                 #region Protected Properties
535                 protected virtual DockStyle DefaultDock { get { return DockStyle.Top; } }
536                 protected virtual Padding DefaultGripMargin { get { return new Padding (2); } }
537                 protected override Padding DefaultMargin { get { return Padding.Empty; } }
538                 protected override Padding DefaultPadding { get { return new Padding (0, 0, 1, 0); } }
539                 protected virtual bool DefaultShowItemToolTips { get { return true; } }
540                 protected override Size DefaultSize { get { return new Size (100, 25); } }
541                 protected internal virtual ToolStripItemCollection DisplayedItems { get { return this.displayed_items; } }
542                 protected internal virtual Size MaxItemSize {
543                         get { return new Size (Width - (GripStyle == ToolStripGripStyle.Hidden ? 1 : 8), Height); }
544                 }
545                 #endregion
546
547                 #region Public Methods
548                 [EditorBrowsable (EditorBrowsableState.Never)]
549                 public new Control GetChildAtPoint (Point point)
550                 {
551                         return base.GetChildAtPoint (point);
552                 }
553
554                 [EditorBrowsable (EditorBrowsableState.Never)]
555                 public new Control GetChildAtPoint (Point pt, GetChildAtPointSkip skipValue)
556                 {
557                         return base.GetChildAtPoint (pt, skipValue);
558                 }
559                 
560                 public ToolStripItem GetItemAt (Point point)
561                 {
562                         foreach (ToolStripItem tsi in this.displayed_items)
563                                 if (tsi.Visible && tsi.Bounds.Contains (point))
564                                         return tsi;
565
566                         return null;
567                 }
568
569                 public ToolStripItem GetItemAt (int x, int y)
570                 {
571                         return GetItemAt (new Point (x, y));
572                 }
573
574                 public virtual ToolStripItem GetNextItem (ToolStripItem start, ArrowDirection direction)
575                 {
576                         if (!Enum.IsDefined (typeof (ArrowDirection), direction))
577                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ArrowDirection", direction));
578
579                         ToolStripItem current_best = null;
580                         int current_best_point;
581                         
582                         switch (direction) {
583                                 case ArrowDirection.Right:
584                                         current_best_point = int.MaxValue;
585
586                                         if (start != null)
587                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
588                                                         if (loop_tsi.Left >= start.Right && loop_tsi.Left < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
589                                                                 current_best = loop_tsi;
590                                                                 current_best_point = loop_tsi.Left;
591                                                         }
592                                                         
593                                         if (current_best == null)
594                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
595                                                         if (loop_tsi.Left < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
596                                                                 current_best = loop_tsi;
597                                                                 current_best_point = loop_tsi.Left;
598                                                         }
599                                                         
600                                         break;
601                                 case ArrowDirection.Up:
602                                         current_best_point = int.MinValue;
603
604                                         if (start != null)
605                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
606                                                         if (loop_tsi.Bottom <= start.Top && loop_tsi.Top > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
607                                                                 current_best = loop_tsi;
608                                                                 current_best_point = loop_tsi.Top;
609                                                         }
610
611                                         if (current_best == null)
612                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
613                                                         if (loop_tsi.Top > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
614                                                                 current_best = loop_tsi;
615                                                                 current_best_point = loop_tsi.Top;
616                                                         }
617
618                                         break;
619                                 case ArrowDirection.Left:
620                                         current_best_point = int.MinValue;
621
622                                         if (start != null)
623                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
624                                                         if (loop_tsi.Right <= start.Left && loop_tsi.Left > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
625                                                                 current_best = loop_tsi;
626                                                                 current_best_point = loop_tsi.Left;
627                                                         }
628
629                                         if (current_best == null)
630                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
631                                                         if (loop_tsi.Left > current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
632                                                                 current_best = loop_tsi;
633                                                                 current_best_point = loop_tsi.Left;
634                                                         }
635
636                                         break;
637                                 case ArrowDirection.Down:
638                                         current_best_point = int.MaxValue;
639
640                                         if (start != null) 
641                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
642                                                         if (loop_tsi.Top >= start.Bottom && loop_tsi.Bottom < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
643                                                                 current_best = loop_tsi;
644                                                                 current_best_point = loop_tsi.Top;
645                                                         }
646
647                                         if (current_best == null)
648                                                 foreach (ToolStripItem loop_tsi in this.DisplayedItems)
649                                                         if (loop_tsi.Top < current_best_point && loop_tsi.Visible && loop_tsi.CanSelect) {
650                                                                 current_best = loop_tsi;
651                                                                 current_best_point = loop_tsi.Top;
652                                                         }
653
654                                         break;
655                         }
656
657                         return current_best;
658                 }
659
660                 [EditorBrowsable (EditorBrowsableState.Never)]
661                 public void ResetMinimumSize ()
662                 {
663                         this.MinimumSize = new Size (-1, -1);
664                 }
665
666                 [EditorBrowsable (EditorBrowsableState.Never)]
667                 public new void SetAutoScrollMargin (int x, int y)
668                 {
669                         base.SetAutoScrollMargin (x, y);
670                 }
671                 
672                 public override string ToString ()
673                 {
674                         return String.Format ("{0}, Name: {1}, Items: {2}", base.ToString(), this.Name, this.items.Count.ToString ());
675                 }
676                 #endregion
677
678                 #region Protected Methods
679                 protected override AccessibleObject CreateAccessibilityInstance ()
680                 {
681                         return new ToolStripAccessibleObject (this);
682                 }
683                 
684                 protected override ControlCollection CreateControlsInstance ()
685                 {
686                         return base.CreateControlsInstance ();
687                 }
688
689                 protected internal virtual ToolStripItem CreateDefaultItem (string text, Image image, EventHandler onClick)
690                 {
691                         if (text == "-")
692                                 return new ToolStripSeparator ();
693
694                         if (this is ToolStripDropDown)
695                                 return new ToolStripMenuItem (text, image, onClick);
696                                 
697                         return new ToolStripButton (text, image, onClick);
698                 }
699
700                 protected virtual LayoutSettings CreateLayoutSettings (ToolStripLayoutStyle layoutStyle)
701                 {
702                         switch (layoutStyle) {
703                                 case ToolStripLayoutStyle.Flow:
704                                         return new FlowLayoutSettings ();
705                                 case ToolStripLayoutStyle.Table:
706                                         //return new TableLayoutSettings ();
707                                 case ToolStripLayoutStyle.StackWithOverflow:
708                                 case ToolStripLayoutStyle.HorizontalStackWithOverflow:
709                                 case ToolStripLayoutStyle.VerticalStackWithOverflow:
710                                 default:
711                                         return null;
712                         }
713                 }
714                 
715                 protected override void Dispose (bool disposing)
716                 {
717                         if (!IsDisposed) {
718                                 ToolStripManager.RemoveToolStrip (this);
719                                 base.Dispose (disposing);
720                         }
721                 }
722
723                 [MonoTODO ("Not called")]
724                 protected virtual void OnBeginDrag (EventArgs e)
725                 {
726                         EventHandler eh = (EventHandler)(Events[BeginDragEvent]);
727                         if (eh != null)
728                                 eh (this, e);
729                 }
730                 
731                 protected override void OnDockChanged (EventArgs e)
732                 {
733                         base.OnDockChanged (e);
734                 }
735
736                 [MonoTODO ("Not called")]
737                 protected virtual void OnEndDrag (EventArgs e)
738                 {
739                         EventHandler eh = (EventHandler)(Events[EndDragEvent]);
740                         if (eh != null)
741                                 eh (this, e);
742                 }
743
744                 protected override bool IsInputChar (char charCode)
745                 {
746                         return base.IsInputChar (charCode);
747                 }
748
749                 protected override bool IsInputKey (Keys keyData)
750                 {
751                         return base.IsInputKey (keyData);
752                 }
753                 
754                 protected override void OnEnabledChanged (EventArgs e)
755                 {
756                         base.OnEnabledChanged (e);
757                         
758                         foreach (ToolStripItem tsi in this.Items)
759                                 tsi.OnParentEnabledChanged (EventArgs.Empty);
760                 }
761
762                 protected override void OnFontChanged (EventArgs e)
763                 {
764                         base.OnFontChanged (e);
765                 }
766
767                 protected override void OnHandleCreated (EventArgs e)
768                 {
769                         base.OnHandleCreated (e);
770                 }
771
772                 protected override void OnHandleDestroyed (EventArgs e)
773                 {
774                         base.OnHandleDestroyed (e);
775                 }
776
777                 protected override void OnInvalidated (InvalidateEventArgs e)
778                 {
779                         base.OnInvalidated (e);
780                 }
781
782                 protected internal virtual void OnItemAdded (ToolStripItemEventArgs e)
783                 {
784                         e.Item.Available = true;
785                         e.Item.SetPlacement (ToolStripItemPlacement.Main);
786                         this.DoAutoSize ();
787                         
788                         if (this.Created)
789                                 this.PerformLayout ();
790                         
791                         ToolStripItemEventHandler eh = (ToolStripItemEventHandler)(Events [ItemAddedEvent]);
792                         if (eh != null)
793                                 eh (this, e);
794                 }
795
796                 protected virtual void OnItemClicked (ToolStripItemClickedEventArgs e)
797                 {
798                         if (this.KeyboardActive)
799                                 ToolStripManager.SetActiveToolStrip (null, false);
800                         
801                         ToolStripItemClickedEventHandler eh = (ToolStripItemClickedEventHandler)(Events [ItemClickedEvent]);
802                         if (eh != null)
803                                 eh (this, e);
804                 }
805
806                 protected internal virtual void OnItemRemoved (ToolStripItemEventArgs e)
807                 {
808                         ToolStripItemEventHandler eh = (ToolStripItemEventHandler)(Events [ItemRemovedEvent]);
809                         if (eh != null)
810                                 eh (this, e);
811                 }
812
813                 protected override void OnLayout (LayoutEventArgs e)
814                 {
815                         DoAutoSize ();
816                         base.OnLayout (e);
817
818                         this.SetDisplayedItems ();
819                         this.OnLayoutCompleted (EventArgs.Empty);
820                         this.Invalidate ();
821                 }
822
823                 protected virtual void OnLayoutCompleted (EventArgs e)
824                 {
825                         EventHandler eh = (EventHandler)(Events [LayoutCompletedEvent]);
826                         if (eh != null)
827                                 eh (this, e);
828                 }
829
830                 protected virtual void OnLayoutStyleChanged (EventArgs e)
831                 {
832                         EventHandler eh = (EventHandler)(Events[LayoutStyleChangedEvent]);
833                         if (eh != null)
834                                 eh (this, e);
835                 }
836
837                 protected override void OnLeave (EventArgs e)
838                 {
839                         base.OnLeave (e);
840                 }
841
842                 protected override void OnLostFocus (EventArgs e)
843                 {
844                         base.OnLostFocus (e);
845                 }
846
847                 protected override void OnMouseCaptureChanged (EventArgs e)
848                 {
849                         base.OnMouseCaptureChanged (e);
850                 }
851                 
852                 protected override void OnMouseDown (MouseEventArgs mea)
853                 {
854                         if (mouse_currently_over != null)
855                         {
856                                 if (this is MenuStrip && !(mouse_currently_over as ToolStripMenuItem).HasDropDownItems) {
857                                         if (!menu_selected)
858                                                 (this as MenuStrip).FireMenuActivate ();
859                                         
860                                         return;
861                                 }
862                                         
863                                 mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseDown);
864                                 
865                                 if (this is MenuStrip && !menu_selected) {
866                                         (this as MenuStrip).FireMenuActivate ();
867                                         menu_selected = true;                           
868                                 }
869                         } else {
870                                 if (this is MenuStrip)
871                                         this.HideMenus (true, ToolStripDropDownCloseReason.AppClicked);
872                         }
873                         
874                         if (this is MenuStrip)
875                                 this.Capture = false;
876
877                         base.OnMouseDown (mea);
878                 }
879
880                 protected override void OnMouseLeave (EventArgs e)
881                 {
882                         if (mouse_currently_over != null) {
883                                 MouseLeftItem (mouse_currently_over);
884                                 mouse_currently_over.FireEvent (e, ToolStripItemEventType.MouseLeave);
885                                 mouse_currently_over = null;
886                         }
887
888                         base.OnMouseLeave (e);
889                 }
890
891                 protected override void OnMouseMove (MouseEventArgs mea)
892                 {
893                         ToolStripItem tsi;
894                         // Find the item we are now 
895                         if (this.overflow_button != null && this.overflow_button.Visible && this.overflow_button.Bounds.Contains (mea.Location))
896                                 tsi = this.overflow_button;
897                         else
898                                 tsi = this.GetItemAt (mea.X, mea.Y);
899
900                         if (tsi != null) {
901                                 // If we were already hovering on this item, just send a mouse move
902                                 if (tsi == mouse_currently_over) 
903                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseMove);
904                                 else {
905                                         // If we were over a different item, fire a mouse leave on it
906                                         if (mouse_currently_over != null) {
907                                                 MouseLeftItem (tsi);
908                                                 mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseLeave);
909                                         }
910                                         
911                                         // Set the new item we are currently over
912                                         mouse_currently_over = tsi;
913                                         
914                                         // Fire mouse enter and mouse move
915                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseEnter);
916                                         MouseEnteredItem (tsi);
917                                         tsi.FireEvent (mea, ToolStripItemEventType.MouseMove);
918
919                                         // If we're over something with a drop down, show it
920                                         if (menu_selected && mouse_currently_over.Enabled && mouse_currently_over is ToolStripDropDownItem && (mouse_currently_over as ToolStripDropDownItem).HasDropDownItems)
921                                                 (mouse_currently_over as ToolStripDropDownItem).ShowDropDown ();
922                                 }
923                         } else {
924                                 // We're not over anything now, just fire the mouse leave on what we used to be over
925                                 if (mouse_currently_over != null) {
926                                         MouseLeftItem (tsi);
927                                         mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseLeave);
928                                         mouse_currently_over = null;
929                                 }
930                         }
931                         
932                         base.OnMouseMove (mea);
933                 }
934
935                 protected override void OnMouseUp (MouseEventArgs mea)
936                 {
937                         // If we're currently over an item (set in MouseMove)
938                         if (mouse_currently_over != null) {
939                                 // Fire the item's MouseUp event
940                                 mouse_currently_over.FireEvent (mea, ToolStripItemEventType.MouseUp);
941
942                                 // The event handler may have blocked until the mouse moved off of the ToolStripItem
943                                 if (mouse_currently_over == null)
944                                         return;
945                                         
946                                 // Fire our ItemClicked event
947                                 OnItemClicked (new ToolStripItemClickedEventArgs (mouse_currently_over));
948                         }
949
950                         base.OnMouseUp (mea);
951                 }
952
953                 protected override void OnPaint (PaintEventArgs e)
954                 {
955                         base.OnPaint (e);
956
957                         // Draw the grip
958                         this.OnPaintGrip (e);
959
960                         // Make each item draw itself
961                         foreach (ToolStripItem tsi in this.displayed_items) {
962                                 e.Graphics.TranslateTransform (tsi.Bounds.Left, tsi.Bounds.Top);
963                                 tsi.FireEvent (e, ToolStripItemEventType.Paint);
964                                 e.Graphics.ResetTransform ();
965                         }
966
967                         // Paint the Overflow button if it's visible
968                         if (this.overflow_button != null && this.overflow_button.Visible) {
969                                 e.Graphics.TranslateTransform (this.overflow_button.Bounds.Left, this.overflow_button.Bounds.Top);
970                                 this.overflow_button.FireEvent (e, ToolStripItemEventType.Paint);
971                                 e.Graphics.ResetTransform ();
972                         }
973
974                         Rectangle affected_bounds = new Rectangle (Point.Empty, this.Size);
975
976                         ToolStripRenderEventArgs pevent = new ToolStripRenderEventArgs (e.Graphics, this, affected_bounds, Color.Empty);
977                         pevent.InternalConnectedArea = CalculateConnectedArea ();
978
979                         this.Renderer.DrawToolStripBorder (pevent);
980                 }
981
982                 [EditorBrowsable (EditorBrowsableState.Advanced)]
983                 protected override void OnPaintBackground (PaintEventArgs pevent)
984                 {
985                         base.OnPaintBackground (pevent);
986
987                         Rectangle affected_bounds = new Rectangle (Point.Empty, this.Size);
988                         ToolStripRenderEventArgs e = new ToolStripRenderEventArgs (pevent.Graphics, this, affected_bounds, SystemColors.Control);
989                         
990                         this.Renderer.DrawToolStripBackground (e);
991                 }
992
993                 protected internal virtual void OnPaintGrip (PaintEventArgs e)
994                 {
995                         // Never draw a grip with these two layouts
996                         if (this.layout_style == ToolStripLayoutStyle.Flow || this.layout_style == ToolStripLayoutStyle.Table)
997                                 return;
998                         
999                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintGripEvent]);
1000                         if (eh != null)
1001                                 eh (this, e);
1002
1003                         if (!(this is MenuStrip)) {
1004                                 if (this.orientation == Orientation.Horizontal)
1005                                         e.Graphics.TranslateTransform (2, 0);
1006                                 else
1007                                         e.Graphics.TranslateTransform (0, 2);
1008                         }
1009
1010                         this.Renderer.DrawGrip (new ToolStripGripRenderEventArgs (e.Graphics, this, this.GripRectangle, this.GripDisplayStyle, this.grip_style));
1011                         e.Graphics.ResetTransform ();
1012                 }
1013
1014                 protected virtual void OnRendererChanged (EventArgs e)
1015                 {
1016                         EventHandler eh = (EventHandler)(Events [RendererChangedEvent]);
1017                         if (eh != null)
1018                                 eh (this, e);
1019                 }
1020
1021                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1022                 protected override void OnRightToLeftChanged (EventArgs e)
1023                 {
1024                         base.OnRightToLeftChanged (e);
1025
1026                         foreach (ToolStripItem tsi in this.Items)
1027                                 tsi.OnParentRightToLeftChanged (e);
1028                 }
1029
1030                 protected override void OnScroll (ScrollEventArgs se)
1031                 {
1032                         base.OnScroll (se);
1033                 }
1034                 
1035                 protected override void OnTabStopChanged (EventArgs e)
1036                 {
1037                         base.OnTabStopChanged (e);
1038                 }
1039
1040                 protected override void OnVisibleChanged (EventArgs e)
1041                 {
1042                         base.OnVisibleChanged (e);
1043                 }
1044
1045                 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
1046                 {
1047                         return base.ProcessCmdKey (ref msg, keyData);
1048                 }
1049
1050                 protected override bool ProcessDialogKey (Keys keyData)
1051                 {
1052                         if (!this.KeyboardActive)
1053                                 return false;
1054                                 
1055                         // Give each item a chance to handle the key
1056                         foreach (ToolStripItem tsi in this.Items)
1057                                 if (tsi.ProcessDialogKey (keyData))
1058                                         return true;
1059                         
1060                         // See if I want to handle it
1061                         if (this.ProcessArrowKey (keyData))
1062                                 return true;
1063                         
1064                         ToolStrip ts = null;
1065                         
1066                         switch (keyData) {
1067                                 case Keys.Escape:
1068                                         this.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1069                                         return true;
1070                         
1071                                 case Keys.Control | Keys.Tab:
1072                                         ts = ToolStripManager.GetNextToolStrip (this, true);
1073                                         
1074                                         if (ts != null) {
1075                                                 foreach (ToolStripItem tsi in this.Items)
1076                                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1077
1078                                                 ToolStripManager.SetActiveToolStrip (ts, true);
1079                                                 ts.SelectNextToolStripItem (null, true);
1080                                         }
1081                                         
1082                                         return true;
1083                                 case Keys.Control | Keys.Shift | Keys.Tab:
1084                                         ts = ToolStripManager.GetNextToolStrip (this, false);
1085
1086                                         if (ts != null) {
1087                                                 foreach (ToolStripItem tsi in this.Items)
1088                                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1089
1090                                                 ToolStripManager.SetActiveToolStrip (ts, true);
1091                                                 ts.SelectNextToolStripItem (null, true);
1092                                         }
1093                                         
1094                                         return true;
1095                                 case Keys.Down:
1096                                 case Keys.Up:
1097                                 case Keys.Left:
1098                                 case Keys.Right:
1099                                         if (GetCurrentlySelectedItem () is ToolStripControlHost)
1100                                                 return false;
1101                                         break;
1102                         }
1103
1104                         return base.ProcessDialogKey (keyData);
1105                 }
1106
1107                 protected override bool ProcessMnemonic (char charCode)
1108                 {
1109                         // If any item has an explicit mnemonic, it gets the message
1110                         foreach (ToolStripItem tsi in this.Items)
1111                                 if (tsi.Enabled && tsi.Visible && !string.IsNullOrEmpty (tsi.Text) && Control.IsMnemonic (charCode, tsi.Text))
1112                                         return tsi.ProcessMnemonic (charCode);
1113
1114                         string code = Char.ToUpper (charCode).ToString ();
1115                         
1116                         // If any item's text starts with our letter, it gets the message
1117                         if (this is MenuStrip)
1118                                 foreach (ToolStripItem tsi in this.Items)
1119                                         if (tsi.Enabled && tsi.Visible && !string.IsNullOrEmpty (tsi.Text) && tsi.Text.ToUpper ().StartsWith (code))
1120                                                 return tsi.ProcessMnemonic (charCode);
1121
1122                         return base.ProcessMnemonic (charCode);
1123                 }
1124                 
1125                 [MonoTODO ()]
1126                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1127                 protected virtual void RestoreFocus ()
1128                 {
1129                 }
1130
1131                 [MonoTODO ()]
1132                 protected override void Select (bool directed, bool forward)
1133                 {
1134                         base.Select (directed, forward);
1135                 }
1136                 
1137                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1138                 {
1139                         base.SetBoundsCore (x, y, width, height, specified);
1140                 }
1141
1142                 protected virtual void SetDisplayedItems ()
1143                 {
1144                         this.displayed_items.Clear ();
1145                         
1146                         foreach (ToolStripItem tsi in this.items)
1147                                 if (tsi.Placement == ToolStripItemPlacement.Main && tsi.Available) {
1148                                         this.displayed_items.AddNoOwnerOrLayout (tsi);
1149                                         tsi.Parent = this; 
1150                                 }
1151                                 else if (tsi.Placement == ToolStripItemPlacement.Overflow)
1152                                         tsi.Parent = this.OverflowButton.DropDown; 
1153                         
1154                         if (this.OverflowButton != null)
1155                                 this.OverflowButton.DropDown.SetDisplayedItems ();
1156                 }
1157
1158                 protected internal void SetItemLocation (ToolStripItem item, Point location)
1159                 {
1160                         if (item == null)
1161                                 throw new ArgumentNullException ("item");
1162                                 
1163                         if (item.Owner != this)
1164                                 throw new NotSupportedException ("The item is not owned by this ToolStrip");
1165                                 
1166                         item.SetBounds (new Rectangle (location, item.Size));
1167                 }
1168                 
1169                 protected internal static void SetItemParent (ToolStripItem item, ToolStrip parent)
1170                 {
1171                         if (item.Owner != null) {
1172                                 item.Owner.Items.RemoveNoOwnerOrLayout (item);
1173
1174                                 if (item.Owner is ToolStripOverflow)
1175                                         (item.Owner as ToolStripOverflow).ParentToolStrip.Items.RemoveNoOwnerOrLayout (item);
1176                         }
1177                         
1178                         parent.Items.AddNoOwnerOrLayout (item);
1179                         item.Parent = parent;
1180                 }
1181
1182                 protected override void SetVisibleCore (bool value)
1183                 {
1184                         base.SetVisibleCore (value);
1185                 }
1186
1187                 protected override void WndProc (ref Message m)
1188                 {
1189                         base.WndProc (ref m);
1190                 }
1191                 #endregion
1192
1193                 #region Public Events
1194                 static object BeginDragEvent = new object ();
1195                 static object EndDragEvent = new object ();
1196                 static object ItemAddedEvent = new object ();
1197                 static object ItemClickedEvent = new object ();
1198                 static object ItemRemovedEvent = new object ();
1199                 static object LayoutCompletedEvent = new object ();
1200                 static object LayoutStyleChangedEvent = new object ();
1201                 static object PaintGripEvent = new object ();
1202                 static object RendererChangedEvent = new object ();
1203
1204                 [Browsable (true)]
1205                 [EditorBrowsable (EditorBrowsableState.Always)]
1206                 public new event EventHandler AutoSizeChanged {
1207                         add { base.AutoSizeChanged += value; }
1208                         remove { base.AutoSizeChanged -= value; }
1209                 }
1210
1211                 [MonoTODO ()]
1212                 public event EventHandler BeginDrag {
1213                         add { Events.AddHandler (BeginDragEvent, value); }
1214                         remove { Events.RemoveHandler (BeginDragEvent, value); }
1215                 }
1216
1217                 [Browsable (false)]
1218                 public new event EventHandler CausesValidationChanged {
1219                         add { base.CausesValidationChanged += value; }
1220                         remove { base.CausesValidationChanged -= value; }
1221                 }
1222
1223                 [Browsable (false)]
1224                 [EditorBrowsable (EditorBrowsableState.Never)]
1225                 public new event ControlEventHandler ControlAdded {
1226                         add { base.ControlAdded += value; }
1227                         remove { base.ControlAdded -= value; }
1228                 }
1229
1230                 [Browsable (false)]
1231                 [EditorBrowsable (EditorBrowsableState.Never)]
1232                 public new event ControlEventHandler ControlRemoved {
1233                         add { base.ControlRemoved += value; }
1234                         remove { base.ControlRemoved -= value; }
1235                 }
1236                 
1237                 [Browsable (false)]
1238                 public new event EventHandler CursorChanged {
1239                         add { base.CursorChanged += value; }
1240                         remove { base.CursorChanged -= value; }
1241                 }
1242
1243                 [MonoTODO ()]
1244                 public event EventHandler EndDrag {
1245                         add { Events.AddHandler (EndDragEvent, value); }
1246                         remove { Events.RemoveHandler (EndDragEvent, value); }
1247                 }
1248
1249                 [Browsable (false)]
1250                 public new event EventHandler ForeColorChanged {
1251                         add { base.ForeColorChanged += value; }
1252                         remove { base.ForeColorChanged -= value; }
1253                 }
1254
1255                 public event ToolStripItemEventHandler ItemAdded {
1256                         add { Events.AddHandler (ItemAddedEvent, value); }
1257                         remove { Events.RemoveHandler (ItemAddedEvent, value); }
1258                 }
1259
1260                 public event ToolStripItemClickedEventHandler ItemClicked {
1261                         add { Events.AddHandler (ItemClickedEvent, value); }
1262                         remove { Events.RemoveHandler (ItemClickedEvent, value); }
1263                 }
1264
1265                 public event ToolStripItemEventHandler ItemRemoved {
1266                         add { Events.AddHandler (ItemRemovedEvent, value); }
1267                         remove { Events.RemoveHandler (ItemRemovedEvent, value); }
1268                 }
1269
1270                 public event EventHandler LayoutCompleted {
1271                         add { Events.AddHandler (LayoutCompletedEvent, value); }
1272                         remove { Events.RemoveHandler (LayoutCompletedEvent, value); }
1273                 }
1274
1275                 public event EventHandler LayoutStyleChanged {
1276                         add { Events.AddHandler (LayoutStyleChangedEvent, value); }
1277                         remove { Events.RemoveHandler (LayoutStyleChangedEvent, value); }
1278                 }
1279
1280                 public event PaintEventHandler PaintGrip {
1281                         add { Events.AddHandler (PaintGripEvent, value); }
1282                         remove { Events.RemoveHandler (PaintGripEvent, value); }
1283                 }
1284
1285                 public event EventHandler RendererChanged {
1286                         add { Events.AddHandler (RendererChangedEvent, value); }
1287                         remove { Events.RemoveHandler (RendererChangedEvent, value); }
1288                 }
1289                 #endregion
1290
1291                 #region Internal Properties
1292                 internal virtual bool KeyboardActive
1293                 {
1294                         get { return this.keyboard_active; }
1295                         set {
1296                                 if (this.keyboard_active != value) {
1297                                         this.keyboard_active = value;
1298                                         
1299                                         if (value)
1300                                                 Application.KeyboardCapture = this;
1301                                         else if (Application.KeyboardCapture == this) {
1302                                                 Application.KeyboardCapture = null;
1303                                                 ToolStripManager.ActivatedByKeyboard = false;
1304                                         }
1305                                         
1306                                         // Redraw for mnemonic underlines
1307                                         this.Invalidate ();
1308                                 }
1309                         }
1310                 }
1311                 #endregion
1312                 
1313                 #region Private Methods
1314                 internal virtual Rectangle CalculateConnectedArea ()
1315                 {
1316                         return Rectangle.Empty;
1317                 }
1318                 
1319                 internal void ChangeSelection (ToolStripItem nextItem)
1320                 {
1321                         if (Application.KeyboardCapture != this)
1322                                 ToolStripManager.SetActiveToolStrip (this, ToolStripManager.ActivatedByKeyboard);
1323                                 
1324                         foreach (ToolStripItem tsi in this.Items)
1325                                 if (tsi != nextItem)
1326                                         tsi.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1327                                         
1328                         nextItem.Select ();
1329                         
1330                         if (nextItem.Parent is MenuStrip && (nextItem.Parent as MenuStrip).MenuDroppedDown)
1331                                 (nextItem as ToolStripMenuItem).HandleAutoExpansion ();
1332                 }
1333                 
1334                 internal virtual void Dismiss ()
1335                 {
1336                         this.Dismiss (ToolStripDropDownCloseReason.AppClicked);
1337                 }
1338                 
1339                 internal virtual void Dismiss (ToolStripDropDownCloseReason reason)
1340                 {
1341                         // Release our stranglehold on the keyboard
1342                         this.KeyboardActive = false;
1343                         
1344                         // Set our drop down flag to false;
1345                         this.menu_selected = false;
1346                         
1347                         // Make sure all of our items are deselected and repainted
1348                         foreach (ToolStripItem tsi in this.Items)
1349                                 tsi.Dismiss (reason);
1350                                 
1351                         // We probably need to redraw for mnemonic underlines
1352                         this.Invalidate ();
1353                 }
1354                 
1355                 private void DoAutoSize ()
1356                 {
1357                         if (this.AutoSize == true && this.Dock == DockStyle.None) {
1358                                 Size new_size = GetToolStripPreferredSize (Size.Empty);
1359                                 SetBounds (Left, Top, new_size.Width, new_size.Height, BoundsSpecified.None);
1360                         }
1361                                 
1362                         if (this.AutoSize == true && this.Orientation == Orientation.Horizontal && (this.Dock == DockStyle.Top || this.Dock == DockStyle.Bottom))
1363                                 SetBounds (Left, Top, Width, GetToolStripPreferredSize (Size.Empty).Height, BoundsSpecified.None);
1364                 }
1365
1366                 internal ToolStripItem GetCurrentlySelectedItem ()
1367                 {
1368                         foreach (ToolStripItem tsi in this.DisplayedItems)
1369                                 if (tsi.Selected)
1370                                         return tsi;
1371                                         
1372                         return null;
1373                 }
1374                 
1375                 internal virtual Size GetToolStripPreferredSize (Size proposedSize)
1376                 {
1377                         Size new_size = new Size (0, this.Height);
1378
1379                         if (this.orientation == Orientation.Vertical) {
1380                                 foreach (ToolStripItem tsi in this.items)
1381                                         if (tsi.GetPreferredSize (Size.Empty).Height + tsi.Margin.Top + tsi.Margin.Bottom > new_size.Height)
1382                                                 new_size.Height = tsi.GetPreferredSize (Size.Empty).Height + tsi.Margin.Top + tsi.Margin.Bottom;
1383
1384                                 new_size.Height += this.Padding.Top + this.Padding.Bottom;
1385                                 new_size.Width = this.Width;
1386                         } else {
1387                                 foreach (ToolStripItem tsi in this.items) 
1388                                         if (tsi.Available) {
1389                                                 Size tsi_preferred = tsi.GetPreferredSize (Size.Empty);
1390                                                 new_size.Width += tsi_preferred.Width + tsi.Margin.Left + tsi.Margin.Right;
1391                                                 
1392                                                 if (new_size.Height < (this.Padding.Vertical + tsi_preferred.Height))
1393                                                         new_size.Height = (this.Padding.Vertical + tsi_preferred.Height);
1394                                         }
1395                         }
1396
1397                         new_size.Width += (this.GripRectangle.Width + this.GripMargin.Horizontal + this.Padding.Horizontal + 4);
1398                         return new_size;
1399                 }
1400                 
1401                 internal virtual ToolStrip GetTopLevelToolStrip ()
1402                 {
1403                         return this;
1404                 }
1405                 
1406                 internal virtual void HandleItemClick (ToolStripItem dismissingItem)
1407                 {
1408                         this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.ItemClicked);
1409                         this.OnItemClicked (new ToolStripItemClickedEventArgs (dismissingItem));
1410                 }
1411                 
1412                 internal void HideMenus (bool release, ToolStripDropDownCloseReason reason)
1413                 {
1414                         if (this is MenuStrip && release && menu_selected)
1415                                 (this as MenuStrip).FireMenuDeactivate ();
1416                                 
1417                         if (release)
1418                                 menu_selected = false;
1419                                 
1420                         NotifySelectedChanged (null);
1421                 }
1422
1423                 internal void NotifySelectedChanged (ToolStripItem tsi)
1424                 {
1425                         foreach (ToolStripItem tsi2 in this.DisplayedItems)
1426                                 if (tsi != tsi2)
1427                                         if (tsi2 is ToolStripDropDownItem)
1428                                                 (tsi2 as ToolStripDropDownItem).HideDropDown (ToolStripDropDownCloseReason.Keyboard);
1429
1430                         if (this.OverflowButton != null) {
1431                                 ToolStripItemCollection tsic = this.OverflowButton.DropDown.DisplayedItems;
1432                                 
1433                                 foreach (ToolStripItem tsi2 in tsic)
1434                                         if (tsi != tsi2)
1435                                                 if (tsi2 is ToolStripDropDownItem)
1436                                                         (tsi2 as ToolStripDropDownItem).HideDropDown (ToolStripDropDownCloseReason.Keyboard);
1437                         
1438                                 this.OverflowButton.HideDropDown ();
1439                         }
1440                         
1441                         foreach (ToolStripItem tsi2 in this.Items)
1442                                 if (tsi != tsi2)
1443                                         tsi2.Dismiss (ToolStripDropDownCloseReason.Keyboard);
1444                 }
1445                 
1446                 internal virtual bool OnMenuKey ()
1447                 {
1448                         return false;
1449                 }
1450
1451                 internal virtual bool ProcessArrowKey (Keys keyData)
1452                 {
1453                         ToolStripItem tsi;
1454                         
1455                         switch (keyData) {
1456                                 case Keys.Right:
1457                                         tsi = this.GetCurrentlySelectedItem ();
1458                                         
1459                                         if (tsi is ToolStripControlHost)
1460                                                 return false;
1461                                         
1462                                         tsi = this.SelectNextToolStripItem (tsi, true);
1463                                         
1464                                         if (tsi is ToolStripControlHost)
1465                                                 (tsi as ToolStripControlHost).Focus ();
1466                                                 
1467                                         return true;
1468                                 case Keys.Tab:
1469                                         tsi = this.GetCurrentlySelectedItem ();
1470
1471                                         tsi = this.SelectNextToolStripItem (tsi, true);
1472
1473                                         if (tsi is ToolStripControlHost)
1474                                                 (tsi as ToolStripControlHost).Focus ();
1475                                                 
1476                                         return true;
1477                                 case Keys.Left:
1478                                         tsi = this.GetCurrentlySelectedItem ();
1479
1480                                         if (tsi is ToolStripControlHost)
1481                                                 return false;
1482
1483                                         tsi = this.SelectNextToolStripItem (tsi, false);
1484
1485                                         if (tsi is ToolStripControlHost)
1486                                                 (tsi as ToolStripControlHost).Focus ();
1487
1488                                         return true;
1489                                 case Keys.Shift | Keys.Tab:
1490                                         tsi = this.GetCurrentlySelectedItem ();
1491                                         
1492                                         tsi = this.SelectNextToolStripItem (tsi, false);
1493
1494                                         if (tsi is ToolStripControlHost)
1495                                                 (tsi as ToolStripControlHost).Focus ();
1496
1497                                         return true;
1498                         }
1499
1500                         return false;
1501                 }
1502
1503                 internal virtual ToolStripItem SelectNextToolStripItem (ToolStripItem start, bool forward)
1504                 {
1505                         ToolStripItem next_item = this.GetNextItem (start, forward ? ArrowDirection.Right : ArrowDirection.Left);
1506                         
1507                         this.ChangeSelection (next_item);
1508
1509                         if (next_item is ToolStripControlHost)
1510                                 (next_item as ToolStripControlHost).Focus ();
1511                 
1512                         return next_item;
1513                 }
1514
1515                 #region Stuff for ToolTips
1516                 private void MouseEnteredItem (ToolStripItem item)
1517                 {
1518                         if (this.show_item_tool_tips) {
1519                                 tooltip_currently_showing = item;
1520                                 ToolTipTimer.Start ();
1521                         }
1522                 }
1523                 
1524                 private void MouseLeftItem (ToolStripItem item)
1525                 {
1526                         ToolTipTimer.Stop ();
1527                         ToolTipWindow.Hide (this);
1528                         tooltip_currently_showing = null;
1529                 }
1530                 
1531                 private Timer ToolTipTimer {
1532                         get {
1533                                 if (tooltip_timer == null) {
1534                                         tooltip_timer = new Timer ();
1535                                         tooltip_timer.Enabled = false;
1536                                         tooltip_timer.Interval = 500;
1537                                         tooltip_timer.Tick += new EventHandler (ToolTipTimer_Tick);
1538                                 }
1539                                 
1540                                 return tooltip_timer;
1541                         }
1542                 }
1543                 
1544                 private ToolTip ToolTipWindow {
1545                         get {
1546                                 if (tooltip_window == null)
1547                                         tooltip_window = new ToolTip ();
1548                                         
1549                                 return tooltip_window;
1550                         }
1551                 }
1552                 
1553                 private void ToolTipTimer_Tick (object o, EventArgs args)
1554                 {
1555                         string tooltip = tooltip_currently_showing.GetToolTip ();
1556                         
1557                         if (!string.IsNullOrEmpty (tooltip))
1558                                 ToolTipWindow.Present (this, tooltip);
1559
1560                         tooltip_currently_showing.FireEvent (EventArgs.Empty, ToolStripItemEventType.MouseHover);
1561
1562                         ToolTipTimer.Stop ();
1563                 }
1564                 #endregion
1565
1566                 #region Stuff for Merging
1567                 internal ToolStrip CurrentlyMergedWith {
1568                         get { return this.currently_merged_with; }
1569                         set { this.currently_merged_with = value; }
1570                 }
1571                 
1572                 internal List<ToolStripItem> HiddenMergedItems {
1573                         get {
1574                                 if (this.hidden_merged_items == null)
1575                                         this.hidden_merged_items = new List<ToolStripItem> ();
1576                                         
1577                                 return this.hidden_merged_items;
1578                         }
1579                 }
1580                 
1581                 internal bool IsCurrentlyMerged {
1582                         get { return this.is_currently_merged; }
1583                         set { 
1584                                 this.is_currently_merged = value; 
1585                                 
1586                                 if (!value && this is MenuStrip) 
1587                                         foreach (ToolStripMenuItem tsmi in this.Items)
1588                                                 tsmi.DropDown.IsCurrentlyMerged = value;
1589                          }
1590                 }
1591                 
1592                 internal void BeginMerge ()
1593                 {
1594                         if (!IsCurrentlyMerged) {
1595                                 IsCurrentlyMerged = true;
1596                                 
1597                                 if (this.pre_merge_items == null) {
1598                                         this.pre_merge_items = new List<ToolStripItem> ();
1599                         
1600                                 foreach (ToolStripItem tsi in this.Items)
1601                                         this.pre_merge_items.Add (tsi);
1602                                 }
1603                         }
1604                 }
1605                 
1606                 internal void RevertMergeItem (ToolStripItem item)
1607                 {
1608                         int index = 0;
1609
1610                         // Remove it from it's current Parent
1611                         if (item.Parent != null && item.Parent != this) {
1612                                 if (item.Parent is ToolStripOverflow)
1613                                         (item.Parent as ToolStripOverflow).ParentToolStrip.Items.RemoveNoOwnerOrLayout (item);
1614                                 else
1615                                         item.Parent.Items.RemoveNoOwnerOrLayout (item);
1616
1617                                 item.Parent = item.Owner;       
1618                         }
1619                         
1620                         // Find where the item was before the merge
1621                         index = item.Owner.pre_merge_items.IndexOf (item);
1622
1623                         // Find the first pre-merge item that was after this item, that
1624                         // is currently in the Items collection.  Insert our item before
1625                         // that one.
1626                         for (int i = index; i < this.pre_merge_items.Count; i++) {
1627                                 if (this.Items.Contains (this.pre_merge_items[i])) {
1628                                         item.Owner.Items.InsertNoOwnerOrLayout (this.Items.IndexOf (this.pre_merge_items[i]), item);
1629                                         return;
1630                                 }
1631                         }
1632                         
1633                         // There aren't any items that are supposed to be after this item,
1634                         // so just append it to the end.
1635                         item.Owner.Items.AddNoOwnerOrLayout (item);
1636                 }
1637                 #endregion
1638                 #endregion
1639
1640                 #region ToolStripAccessibleObject
1641                 [ComVisible (true)]
1642                 public class ToolStripAccessibleObject : ControlAccessibleObject
1643                 {
1644                         #region Public Constructor
1645                         public ToolStripAccessibleObject (ToolStrip owner) : base (owner)
1646                         {
1647                         }
1648                         #endregion
1649                         
1650                         #region Public Properties
1651                         public override AccessibleRole Role {
1652                                 get { return AccessibleRole.ToolBar; }
1653                         }
1654                         #endregion
1655
1656                         #region Public Methods
1657                         public override AccessibleObject GetChild (int index)
1658                         {
1659                                 return base.GetChild (index);
1660                         }
1661
1662                         public override int GetChildCount ()
1663                         {
1664                                 return (owner as ToolStrip).Items.Count;
1665                         }
1666
1667                         public override AccessibleObject HitTest (int x, int y)
1668                         {
1669                                 return base.HitTest (x, y);
1670                         }
1671                         #endregion
1672                 }
1673                 #endregion
1674         }
1675 }
1676 #endif