48cb784c007197ab2e5ed86bc972bf4f96272825
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripItem.cs
1 //
2 // ToolStripItem.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 #if NET_2_0
29
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33
34 namespace System.Windows.Forms
35 {
36         [DefaultEvent ("Click")]
37         [DefaultProperty ("Text")]
38         [DesignTimeVisible (false)]
39         [ToolboxItem (false)]
40         public abstract class ToolStripItem : Component, IDropTarget, IComponent, IDisposable
41         {
42                 #region Private Variables
43                 private AccessibleObject accessibility_object;
44                 private string accessible_default_action_description;
45                 private bool allow_drop;
46                 private ToolStripItemAlignment alignment;
47                 private AnchorStyles anchor;
48                 private bool available;
49                 private bool auto_size;
50                 private bool auto_tool_tip;
51                 private Color back_color;
52                 private Image background_image;
53                 private ImageLayout background_image_layout;
54                 private Rectangle bounds;
55                 private bool can_select;
56                 private ToolStripItemDisplayStyle display_style;
57                 private DockStyle dock;
58                 private bool double_click_enabled;
59                 private bool enabled;
60                 private Size explicit_size;
61                 private Font font;
62                 private Color fore_color;
63                 private Image image;
64                 private ContentAlignment image_align;
65                 private int image_index;
66                 private string image_key;
67                 private ToolStripItemImageScaling image_scaling;
68                 private Color image_transparent_color;
69                 private bool is_disposed;
70                 internal bool is_pressed;
71                 private bool is_selected;
72                 private Padding margin;
73                 private string name;
74                 private ToolStripItemOverflow overflow;
75                 private ToolStrip owner;
76                 internal ToolStripItem owner_item;
77                 private Padding padding;
78                 private ToolStripItemPlacement placement;
79                 private RightToLeft right_to_left;
80                 private bool right_to_left_auto_mirror_image;
81                 private Object tag;
82                 private string text;
83                 private ContentAlignment text_align;
84                 private TextImageRelation text_image_relation;
85                 private string tool_tip_text;
86                 private bool visible;
87
88                 private ToolStrip parent;
89                 private Size text_size;
90                 #endregion
91
92                 #region Public Constructors
93                 protected ToolStripItem ()
94                         : this (String.Empty, null, null, String.Empty)
95                 {
96                 }
97
98                 protected ToolStripItem (string text, Image image, EventHandler onClick)
99                         : this (text, image, onClick, String.Empty)
100                 {
101                 }
102
103                 protected ToolStripItem (string text, Image image, EventHandler onClick, string name)
104                 {
105                         this.alignment = ToolStripItemAlignment.Left;
106                         this.anchor = AnchorStyles.Left | AnchorStyles.Top;
107                         this.auto_size = true;
108                         this.auto_tool_tip = this.DefaultAutoToolTip;
109                         this.available = true;
110                         this.back_color = Control.DefaultBackColor;
111                         this.background_image_layout = ImageLayout.Tile;
112                         this.can_select = true;
113                         this.display_style = this.DefaultDisplayStyle;
114                         this.dock = DockStyle.None;
115                         this.enabled = true;
116                         this.font = new Font ("Tahoma", 8.25f);
117                         this.fore_color = Control.DefaultForeColor;
118                         this.image = image;
119                         this.image_align = ContentAlignment.MiddleCenter;
120                         this.image_index = -1;
121                         this.image_key = string.Empty;
122                         this.image_scaling = ToolStripItemImageScaling.SizeToFit;
123                         this.image_transparent_color = Color.Empty;
124                         this.margin = this.DefaultMargin;
125                         this.name = name;
126                         this.overflow = ToolStripItemOverflow.AsNeeded;
127                         this.padding = this.DefaultPadding;
128                         this.placement = ToolStripItemPlacement.None;
129                         this.right_to_left = RightToLeft.Inherit;
130                         this.bounds.Size = this.DefaultSize;
131                         this.text = text;
132                         this.text_align = ContentAlignment.MiddleCenter;
133                         this.text_image_relation = TextImageRelation.ImageBeforeText;
134                         this.visible = true;
135
136                         this.Click += onClick;
137                         OnLayout (new LayoutEventArgs (null, string.Empty));
138                 }
139                 #endregion
140
141                 #region Public Properties
142                 [Browsable (false)]
143                 [EditorBrowsable (EditorBrowsableState.Advanced)]
144                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
145                 public AccessibleObject AccessibilityObject {
146                         get { 
147                                 if (this.accessibility_object == null)
148                                         this.accessibility_object = CreateAccessibilityInstance ();
149                                         
150                                 return this.accessibility_object;
151                         }
152                 }
153
154                 [Browsable (false)]
155                 [EditorBrowsable (EditorBrowsableState.Advanced)]
156                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157                 public string AccessibleDefaultActionDescription {
158                         get {
159                                 if (this.accessibility_object == null)
160                                         return null;
161                                 
162                                 return this.accessible_default_action_description;
163                         }
164                         set { this.accessible_default_action_description = value; }
165                 }
166
167                 [Localizable (true)]
168                 [DefaultValue (null)]
169                 public string AccessibleDescription {
170                         get {
171                                 if (this.accessibility_object == null)
172                                         return null;
173                                 
174                                 return this.AccessibilityObject.Description;
175                         }
176                         set { this.AccessibilityObject.description = value; }
177                 }
178
179                 [Localizable (true)]
180                 [DefaultValue (null)]
181                 public string AccessibleName {
182                         get { 
183                                 if (this.accessibility_object == null)
184                                         return null;
185                                         
186                                 return this.AccessibilityObject.Name; 
187                         }
188                         set { this.AccessibilityObject.Name = value; }
189                 }
190                 
191                 [DefaultValue (AccessibleRole.Default)]
192                 public AccessibleRole AccessibleRole {
193                         get
194                         {
195                                 if (this.accessibility_object == null)
196                                         return AccessibleRole.Default;
197                                 
198                                 return this.AccessibilityObject.Role;
199                         }
200                         set { this.AccessibilityObject.role = value; }
201                 }
202                 
203                 [DefaultValue (ToolStripItemAlignment.Left)]
204                 public ToolStripItemAlignment Alignment {
205                         get { return this.alignment; }
206                         set {
207                                 if (!Enum.IsDefined (typeof (ToolStripItemAlignment), value))
208                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemAlignment", value));
209
210                                 this.alignment = value;
211                         }
212                 }
213
214                 [MonoTODO]
215                 public virtual bool AllowDrop {
216                         get {
217                                 return this.allow_drop;
218                         }
219                         
220                         set {
221                                 this.allow_drop = value;
222                         }
223                 }
224                 
225                 [Browsable (false)]
226                 [DefaultValue (AnchorStyles.Top | AnchorStyles.Left)]
227                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
228                 public AnchorStyles Anchor {
229                         get { return this.anchor; }
230                         set { this.anchor = value; }
231                 }
232                         
233                 [Localizable (true)]
234                 [DefaultValue (true)]
235                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
236                 [RefreshProperties (RefreshProperties.All)]
237                 public bool AutoSize {
238                         get { return this.auto_size; }
239                         set { 
240                                 this.auto_size = value; 
241                                 this.CalculateAutoSize (); 
242                         }
243                 }
244
245                 [DefaultValue (false)]
246                 public bool AutoToolTip {
247                         get { return this.auto_tool_tip; }
248                         set { this.auto_tool_tip = value; }
249                 }
250
251                 [Browsable (false)]
252                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
253                 public bool Available {
254                         get { return this.available; }
255                         set {
256                                 if (this.available != value) {
257                                         available = value;
258                                         visible = value;
259
260                                         if (this.parent != null)
261                                                 parent.PerformLayout (); 
262                                                 
263                                         OnAvailableChanged (EventArgs.Empty); 
264                                         OnVisibleChanged (EventArgs.Empty);
265                                 }
266                         }
267                 }
268
269                 public virtual Color BackColor {
270                         get { return this.back_color; }
271                         set {
272                                 if (this.back_color != value) {
273                                         back_color = value;
274                                         OnBackColorChanged (EventArgs.Empty);
275                                         this.Invalidate ();
276                                 }
277                         }
278                 }
279
280                 [Localizable (true)]
281                 [DefaultValue (null)]
282                 public virtual Image BackgroundImage {
283                         get { return this.background_image; }
284                         set { 
285                                 if (this.background_image != value) {
286                                         this.background_image = value;
287                                         this.Invalidate ();
288                                 }
289                         }
290                 }
291
292                 [Localizable (true)]
293                 [DefaultValue (ImageLayout.Tile)]
294                 public virtual ImageLayout BackgroundImageLayout {
295                         get { return this.background_image_layout; }
296                         set { 
297                                 if (this.background_image_layout != value) {
298                                         this.background_image_layout = value;
299                                         this.Invalidate (); 
300                                 }
301                         }
302                 }
303
304                 [Browsable (false)]
305                 public virtual Rectangle Bounds {
306                         get { return this.bounds; }
307                 }
308
309                 [Browsable (false)]
310                 public virtual bool CanSelect {
311                         get { return this.can_select; }
312                 }
313
314                 [Browsable (false)]
315                 public Rectangle ContentRectangle {
316                         get {
317                                 // ToolStripLabels don't have a border
318                                 if (this is ToolStripLabel || this is ToolStripStatusLabel)
319                                         return new Rectangle (0, 0, this.bounds.Width, this.bounds.Height);
320
321                                 if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
322                                         return new Rectangle (2, 2, this.bounds.Width - 13, this.bounds.Height - 4);
323
324                                 return new Rectangle (2, 2, this.bounds.Width - 4, this.bounds.Height - 4);
325                         }
326                 }
327
328                 public virtual ToolStripItemDisplayStyle DisplayStyle {
329                         get { return this.display_style; }
330                         set {
331                                 if (this.display_style != value) {
332                                         this.display_style = value; 
333                                         this.CalculateAutoSize (); 
334                                         OnDisplayStyleChanged (EventArgs.Empty);
335                                         if (this.Parent != null)
336                                                 this.Parent.PerformLayout ();
337                                 }
338                         }
339                 }
340
341                 public bool IsDisposed {
342                         get { return this.is_disposed; }
343                 }
344                 
345                 [Browsable (false)]
346                 [DefaultValue (DockStyle.None)]
347                 public DockStyle Dock {
348                         get { return this.dock; }
349                         set {
350                                 if (this.dock != value) {
351                                         if (!Enum.IsDefined (typeof (DockStyle), value))
352                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for DockStyle", value));
353
354                                         this.dock = value;
355                                         this.CalculateAutoSize ();
356                                 }
357                         }
358                 }
359
360                 [DefaultValue (false)]
361                 public bool DoubleClickEnabled {
362                         get { return this.double_click_enabled; }
363                         set { this.double_click_enabled = value; }
364                 }
365
366                 [Localizable (true)]
367                 [DefaultValue (true)]
368                 public virtual bool Enabled {
369                         get { return enabled; }
370                         set { 
371                                 if (this.enabled != value) {
372                                         this.enabled = value; 
373                                         OnEnabledChanged (EventArgs.Empty); 
374                                         this.Invalidate ();
375                                 }
376                         }
377                 }
378
379                 [Localizable (true)]
380                 public virtual Font Font
381                 {
382                         get { return this.font; }
383                         set { 
384                                 if (this.font != value) {
385                                         this.font = value; 
386                                         this.CalculateAutoSize (); 
387                                         this.OnFontChanged (EventArgs.Empty); 
388                                         this.Invalidate ();
389                                 }
390                         }
391                 }
392
393                 public virtual Color ForeColor {
394                         get { return this.fore_color; }
395                         set { 
396                                 if (this.fore_color != value) {
397                                         this.fore_color = value; 
398                                         this.OnForeColorChanged (EventArgs.Empty); 
399                                         this.Invalidate ();
400                                 }
401                         }
402                 }
403
404                 [Browsable (false)]
405                 [EditorBrowsable (EditorBrowsableState.Always)]
406                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
407                 public int Height {
408                         get { return this.Size.Height; }
409                         set { 
410                                 this.bounds.Height = value; 
411                                 this.explicit_size.Height = value;
412                                 
413                                 if (this.Visible) {
414                                         this.CalculateAutoSize ();
415                                         this.OnBoundsChanged ();
416                                         this.Invalidate (); 
417                                 } 
418                         }
419                 }
420
421                 [Localizable (true)]
422                 public virtual Image Image {
423                         get { 
424                                 if (this.image != null)
425                                         return this.image;
426                                         
427                                 if (this.image_index >= 0)
428                                         if (this.owner != null && this.owner.ImageList != null)
429                                                 return this.owner.ImageList.Images[this.image_index];
430
431
432                                 if (!string.IsNullOrEmpty (this.image_key))
433                                         if (this.owner != null && this.owner.ImageList != null)
434                                                 return this.owner.ImageList.Images[this.image_key];
435                                                 
436                                 return null;
437                         }
438                         set {
439                                 if (this.image != value) {
440                                         this.image = value; 
441                                         this.image_index = -1;
442                                         this.image_key = string.Empty;
443                                         this.CalculateAutoSize (); 
444                                         this.Invalidate ();
445                                 }
446                         }
447                 }
448
449                 [Localizable (true)]
450                 [DefaultValue (ContentAlignment.MiddleCenter)]
451                 public ContentAlignment ImageAlign {
452                         get { return this.image_align; }
453                         set {
454                                 if (!Enum.IsDefined (typeof (ContentAlignment), value))
455                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
456
457                                 this.image_align = value;
458                                 this.Invalidate ();
459                         }
460                 }
461
462                 [Localizable (true)]
463                 [Browsable (false)]
464                 [RelatedImageList ("Owner.ImageList")]
465                 [RefreshProperties (RefreshProperties.Repaint)]
466                 public int ImageIndex {
467                         get { return this.image_index; }
468                         set {
469                                 if (this.image_index != value) {
470                                         // Lamespec: MSDN says ArgumentException, tests say otherwise
471                                         if (value < -1)
472                                                 throw new ArgumentOutOfRangeException ("ImageIndex cannot be less than -1");
473
474                                         this.image_index = value;
475                                         this.image = null;
476                                         this.image_key = string.Empty;
477                                         this.CalculateAutoSize ();
478                                         this.Invalidate ();
479                                 }
480                         }
481                 }
482
483                 [Localizable (true)]
484                 [Browsable (false)]
485                 [RelatedImageList ("Owner.ImageList")]
486                 [RefreshProperties (RefreshProperties.Repaint)]
487                 public string ImageKey
488                 {
489                         get { return this.image_key; }
490                         set { 
491                                 if (this.image_key != value) {
492                                         this.image = null;
493                                         this.image_index = -1;
494                                         this.image_key = value;
495                                         this.CalculateAutoSize ();
496                                         this.Invalidate ();
497                                 }
498                         }
499                 }
500                 
501                 [Localizable (true)]
502                 [DefaultValue (ToolStripItemImageScaling.SizeToFit)]
503                 public ToolStripItemImageScaling ImageScaling {
504                         get { return this.image_scaling; }
505                         set { 
506                                 this.image_scaling = value; 
507                                 this.CalculateAutoSize (); 
508                                 this.Invalidate (); 
509                         }
510                 }
511
512                 [Localizable (true)]
513                 public Color ImageTransparentColor {
514                         get { return this.image_transparent_color; }
515                         set { this.image_transparent_color = value; }
516                 }
517                 
518                 [Browsable (false)]
519                 public bool IsOnDropDown {
520                         get {
521                                 if (this.parent != null && this.parent is ToolStripDropDown)
522                                         return true;
523
524                                 return false;
525                         }
526                 }
527
528                 [Browsable (false)]
529                 public bool IsOnOverflow {
530                         get { return this.placement == ToolStripItemPlacement.Overflow; }
531                 }
532                 
533                 public Padding Margin {
534                         get { return this.margin; }
535                         set {
536                                 this.margin = value; 
537                                 this.CalculateAutoSize ();
538                         }
539                 }
540
541                 [DefaultValue (null)]
542                 [Browsable (false)]
543                 public string Name {
544                         get { return this.name; }
545                         set { this.name = value; }
546                 }
547
548                 [DefaultValue (ToolStripItemOverflow.AsNeeded)]
549                 public ToolStripItemOverflow Overflow {
550                         get { return this.overflow; }
551                         set { 
552                                 if (this.overflow != value) {
553                                         if (!Enum.IsDefined (typeof (ToolStripItemOverflow), value))
554                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemOverflow", value));
555                                 
556                                         this.overflow = value;
557                                         
558                                         if (owner != null)
559                                                 owner.PerformLayout ();
560                                 }
561                         }
562                 }
563                         
564                 [Browsable (false)]
565                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
566                 public ToolStrip Owner {
567                         get { return this.owner; }
568                         set { 
569                                 if (this.owner != value) {
570                                         this.owner = value; 
571                                         this.CalculateAutoSize (); 
572                                         OnOwnerChanged (EventArgs.Empty);
573                                 }
574                         }
575                 }
576
577                 [Browsable (false)]
578                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
579                 public ToolStripItem OwnerItem {
580                         get { return this.owner_item; }
581                 }
582
583                 public virtual Padding Padding {
584                         get { return this.padding; }
585                         set { 
586                                 this.padding = value; 
587                                 this.CalculateAutoSize (); 
588                                 this.Invalidate (); 
589                         }
590                 }
591
592                 [Browsable (false)]
593                 public ToolStripItemPlacement Placement {
594                         get { return this.placement; }
595                 }
596                 
597                 [Browsable (false)]
598                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
599                 public virtual bool Pressed { get { return this.is_pressed; } }
600
601                 [MonoTODO ("Stub, not implemented")]
602                 [Localizable (true)]
603                 public virtual RightToLeft RightToLeft {
604                         get { return this.right_to_left; }
605                         set { this.right_to_left = value; }
606                 }
607                 
608                 [Localizable (true)]
609                 public bool RightToLeftAutoMirrorImage {
610                         get { return this.right_to_left_auto_mirror_image; }
611                         set { 
612                                 if (this.right_to_left_auto_mirror_image != value) {
613                                         this.right_to_left_auto_mirror_image = value;
614                                         this.Invalidate ();
615                                 }
616                         }
617                 }
618                 
619                 [Browsable (false)]
620                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
621                 public virtual bool Selected { get { return this.is_selected; } }
622
623                 [Localizable (true)]
624                 public virtual Size Size {
625                         get { 
626                                 if (!this.AutoSize && this.explicit_size != Size.Empty) 
627                                         return this.explicit_size; 
628                                         
629                                 return this.bounds.Size; 
630                         }
631                         set { 
632                                 this.bounds.Size = value; 
633                                 this.explicit_size = value;
634                                 
635                                 if (this.Visible) {
636                                         this.CalculateAutoSize ();
637                                         this.OnBoundsChanged (); 
638                                 }
639                         }
640                 }
641
642                 [Localizable (false)]
643                 [Bindable (true)]
644                 [DefaultValue (null)]
645                 [TypeConverter (typeof (StringConverter))]
646                 public Object Tag {
647                         get { return this.tag; }
648                         set { this.tag = value; }
649                 }
650
651                 [Localizable (true)]
652                 [DefaultValue ("")]
653                 public virtual string Text
654                 {
655                         get { return this.text; }
656                         set { 
657                                 if (this.text != value) { 
658                                         this.text = value; 
659                                         this.CalculateAutoSize (); 
660                                         this.OnTextChanged (EventArgs.Empty); 
661                                         this.Invalidate (); 
662                                 } 
663                         }
664                 }
665
666                 [Localizable (true)]
667                 [DefaultValue (ContentAlignment.MiddleCenter)]
668                 public virtual ContentAlignment TextAlign {
669                         get { return this.text_align; }
670                         set {
671                                 if (!Enum.IsDefined (typeof (ContentAlignment), value))
672                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
673                                 this.text_align = value;
674                                 this.Invalidate ();
675                         }
676                 }
677
678                 [Localizable (true)]
679                 [DefaultValue (TextImageRelation.ImageBeforeText)]
680                 public TextImageRelation TextImageRelation {
681                         get { return this.text_image_relation; }
682                         set { 
683                                 this.text_image_relation = value; 
684                                 this.CalculateAutoSize (); 
685                                 this.Invalidate (); 
686                         }
687                 }
688
689                 [Localizable (true)]
690                 public string ToolTipText {
691                         get { return this.tool_tip_text; }
692                         set { this.tool_tip_text = value; }
693                 }
694
695                 [Localizable (true)]
696                 public bool Visible {
697                         get { 
698                                 if (this.parent == null)
699                                         return false;
700                         
701                                 return this.visible && this.parent.Visible; 
702                         }
703                         set { 
704                                 if (this.visible != value) {
705                                         this.available = value;
706                                         this.SetVisibleCore (value);
707                                 }
708                         }
709                 }
710
711                 [Browsable (false)]
712                 [EditorBrowsable (EditorBrowsableState.Always)]
713                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
714                 public int Width {
715                         get { return this.Size.Width; }
716                         set { 
717                                 this.bounds.Width = value; 
718                                 this.explicit_size.Width = value;
719                                 
720                                 if (this.Visible) {
721                                         this.CalculateAutoSize ();
722                                         this.OnBoundsChanged ();
723                                         this.Invalidate ();
724                                 }
725                         }
726                 }
727                 #endregion
728
729                 #region Protected Properties
730                 protected virtual bool DefaultAutoToolTip { get { return false; } }
731                 protected virtual ToolStripItemDisplayStyle DefaultDisplayStyle { get { return ToolStripItemDisplayStyle.ImageAndText; } }
732                 protected internal virtual Padding DefaultMargin { get { return new Padding (0, 1, 0, 2); } }
733                 protected virtual Padding DefaultPadding { get { return new Padding (); } }
734                 protected virtual Size DefaultSize { get { return new Size (23, 23); } }
735                 protected internal virtual bool DismissWhenClicked { get { return true; } }
736                 [Browsable (false)]
737                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
738                 protected internal ToolStrip Parent {
739                         get { return this.parent; }
740                         set { 
741                                 if (this.parent != value) {
742                                         ToolStrip old_parent = this.parent;
743                                         this.parent = value; 
744                                         OnParentChanged(old_parent, this.parent);
745                                 }
746                         }
747                 }
748                 protected internal virtual bool ShowKeyboardCues { get { return false; } }
749                 #endregion
750
751                 #region Public Methods
752                 public ToolStrip GetCurrentParent ()
753                 { 
754                         return this.parent; 
755                 }
756
757                 public virtual Size GetPreferredSize (Size constrainingSize)
758                 {
759                         return this.CalculatePreferredSize (constrainingSize);
760                 }
761
762                 public void Invalidate ()
763                 {
764                         if (parent != null)
765                                 parent.Invalidate (this.bounds);
766                 }
767
768                 public void Invalidate (Rectangle r)
769                 {
770                         if (parent != null)
771                                 parent.Invalidate (r);
772                 }
773
774                 public void PerformClick ()
775                 { 
776                         this.OnClick (EventArgs.Empty); 
777                 }
778
779                 [EditorBrowsable (EditorBrowsableState.Never)]
780                 public virtual void ResetBackColor () { this.BackColor = Control.DefaultBackColor; }
781
782                 [EditorBrowsable (EditorBrowsableState.Never)]
783                 public virtual void ResetDisplayStyle () { this.display_style = this.DefaultDisplayStyle; }
784
785                 [EditorBrowsable (EditorBrowsableState.Never)]
786                 public virtual void ResetFont () { this.font = new Font ("Tahoma", 8.25f); }
787
788                 [EditorBrowsable (EditorBrowsableState.Never)]
789                 public virtual void ResetForeColor () { this.ForeColor = Control.DefaultForeColor; }
790
791                 [EditorBrowsable (EditorBrowsableState.Never)]
792                 public virtual void ResetImage () { this.image = null; }
793
794                 [EditorBrowsable (EditorBrowsableState.Never)]
795                 public void ResetMargin () { this.margin = this.DefaultMargin; }
796
797                 [EditorBrowsable (EditorBrowsableState.Never)]
798                 public void ResetPadding () { this.padding = this.DefaultPadding; }
799
800                 public void Select ()
801                 {
802                         if (!this.is_selected && this.CanSelect) {
803                                 this.is_selected = true;
804                                 this.Invalidate ();
805                                 this.Parent.NotifySelectedChanged (this);
806                         }
807                 }
808
809                 public override string ToString ()
810                 {
811                         return this.text;
812                 }
813                 #endregion
814
815                 #region Protected Methods
816                 [EditorBrowsable (EditorBrowsableState.Advanced)]
817                 protected virtual AccessibleObject CreateAccessibilityInstance ()
818                 {
819                         return new ToolStripItemAccessibleObject (this);
820                 }
821
822                 protected override void Dispose (bool disposing)
823                 {
824                         if (!is_disposed && disposing)
825                                 is_disposed = true;
826                                 
827                         base.Dispose (disposing);
828                 }
829                 
830                 protected virtual void OnAvailableChanged (EventArgs e)
831                 {
832                         EventHandler eh = (EventHandler)(Events [AvailableChangedEvent]);
833                         if (eh != null)
834                                 eh (this, e);
835                 }
836
837                 [EditorBrowsable (EditorBrowsableState.Advanced)]
838                 protected virtual void OnBackColorChanged (EventArgs e)
839                 {
840                         EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
841                         if (eh != null)
842                                 eh (this, e);
843                 }
844
845                 protected virtual void OnBoundsChanged ()
846                 {
847                         OnLayout (new LayoutEventArgs(null, ""));
848                 }
849
850                 protected virtual void OnClick (EventArgs e)
851                 {
852                         EventHandler eh = (EventHandler)(Events [ClickEvent]);
853                         if (eh != null)
854                                 eh (this, e);
855                 }
856
857                 [EditorBrowsable (EditorBrowsableState.Advanced)]
858                 protected virtual void OnDisplayStyleChanged (EventArgs e)
859                 {
860                         EventHandler eh = (EventHandler)(Events [DisplayStyleChangedEvent]);
861                         if (eh != null)
862                                 eh (this, e);
863                 }
864
865                 protected virtual void OnDoubleClick (EventArgs e)
866                 {
867                         EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
868                         if (eh != null)
869                                 eh (this, e);
870
871                         if (!double_click_enabled)
872                                 OnClick (e);
873                 }
874
875                 void IDropTarget.OnDragDrop (DragEventArgs e)
876                 {
877                         // XXX
878                 }
879
880                 void IDropTarget.OnDragEnter (DragEventArgs e)
881                 {
882                         // XXX
883                 }
884
885                 void IDropTarget.OnDragLeave (EventArgs e)
886                 {
887                         // XXX
888                 }
889
890                 void IDropTarget.OnDragOver (DragEventArgs e)
891                 {
892                         // XXX
893                 }
894
895                 protected virtual void OnEnabledChanged (EventArgs e)
896                 {
897                         EventHandler eh = (EventHandler)(Events [EnabledChangedEvent]);
898                         if (eh != null)
899                                 eh (this, e);
900                 }
901
902                 [EditorBrowsable (EditorBrowsableState.Advanced)]
903                 protected virtual void OnFontChanged (EventArgs e)
904                 {
905                 }
906
907                 [EditorBrowsable (EditorBrowsableState.Advanced)]
908                 protected virtual void OnForeColorChanged (EventArgs e)
909                 {
910                         EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
911                         if (eh != null)
912                                 eh (this, e);
913                 }
914
915                 protected virtual void OnLayout (LayoutEventArgs e)
916                 {
917                 }
918
919                 protected virtual void OnLocationChanged (EventArgs e)
920                 {
921                         EventHandler eh = (EventHandler)(Events [LocationChangedEvent]);
922                         if (eh != null)
923                                 eh (this, e);
924                 }
925
926                 protected virtual void OnMouseDown (MouseEventArgs e)
927                 {
928                         if (this.Enabled) {
929                                 this.is_pressed = true;
930                                 this.Invalidate ();
931
932                                 MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
933                                 if (eh != null)
934                                         eh (this, e);
935                         }
936                 }
937
938                 protected virtual void OnMouseEnter (EventArgs e)
939                 {
940                         this.Select ();
941
942                         EventHandler eh = (EventHandler)(Events [MouseEnterEvent]);
943                         if (eh != null)
944                                 eh (this, e);
945                 }
946
947                 protected virtual void OnMouseHover (EventArgs e)
948                 {
949                         if (this.Enabled) {
950                                 EventHandler eh = (EventHandler)(Events [MouseHoverEvent]);
951                                 if (eh != null)
952                                         eh (this, e);
953                         }
954                 }
955
956                 protected virtual void OnMouseLeave (EventArgs e)
957                 {
958                         if (this.CanSelect) {
959                                 this.is_selected = false;
960                                 this.is_pressed = false;
961                                 this.Invalidate ();
962                         }
963
964                         EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
965                         if (eh != null)
966                                 eh (this, e);
967                 }
968
969                 protected virtual void OnMouseMove (MouseEventArgs e)
970                 {
971                         if (this.Enabled) {
972                                 MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
973                                 if (eh != null)
974                                         eh (this, e);
975                         }
976                 }
977
978                 protected virtual void OnMouseUp (MouseEventArgs e)
979                 {
980                         if (this.Enabled) {
981                                 this.is_pressed = false;
982                                 this.Invalidate ();
983
984                                 if (this.IsOnDropDown)
985                                         if (!(this is ToolStripDropDownItem) || (this as ToolStripDropDownItem).DropDown.Visible == false) {
986                                                 if ((this.Parent as ToolStripDropDown).OwnerItem != null)
987                                                         ((this.Parent as ToolStripDropDown).OwnerItem as ToolStripDropDownItem).HideDropDown ();
988                                                 else
989                                                         (this.Parent as ToolStripDropDown).Hide ();
990                                         }
991                                                 
992                                 
993                                 MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
994                                 if (eh != null)
995                                         eh (this, e);
996                         }
997                 }
998
999                 protected virtual void OnOwnerChanged (EventArgs e)
1000                 {
1001                         EventHandler eh = (EventHandler)(Events [OwnerChangedEvent]);
1002                         if (eh != null)
1003                                 eh (this, e);
1004                 }
1005
1006                 protected internal virtual void OnOwnerFontChanged (EventArgs e)
1007                 {
1008                 }
1009                 
1010                 protected virtual void OnPaint (PaintEventArgs e)
1011                 {
1012                         if (this.parent != null)
1013                                 this.parent.Renderer.DrawItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
1014                                 
1015                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
1016                         if (eh != null)
1017                                 eh (this, e);
1018                 }
1019
1020                 // This is never called.
1021                 protected virtual void OnParentBackColorChanged (EventArgs e)
1022                 {
1023                 }
1024                 
1025                 protected virtual void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
1026                 {
1027                         if (oldParent != null)
1028                                 oldParent.PerformLayout ();
1029                                 
1030                         if (newParent != null)
1031                                 newParent.PerformLayout ();
1032                 }
1033
1034                 protected internal virtual void OnParentEnabledChanged (EventArgs e)
1035                 {
1036                         this.OnEnabledChanged (e);
1037                 }
1038
1039                 // This is never called.
1040                 protected virtual void OnParentForeColorChanged (EventArgs e)
1041                 {
1042                 }
1043         
1044                 protected internal virtual void OnParentRightToLeftChanged (EventArgs e)
1045                 {
1046                         this.OnRightToLeftChanged (e);
1047                 }
1048                 
1049                 protected virtual void OnRightToLeftChanged (EventArgs e)
1050                 {
1051                         EventHandler eh = (EventHandler)(Events[RightToLeftChangedEvent]);
1052                         if (eh != null)
1053                                 eh (this, e);
1054                 }
1055                 
1056                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1057                 protected virtual void OnTextChanged (EventArgs e)
1058                 {
1059                         EventHandler eh = (EventHandler)(Events [TextChangedEvent]);
1060                         if (eh != null)
1061                                 eh (this, e);
1062                 }
1063
1064                 protected virtual void OnVisibleChanged (EventArgs e)
1065                 {
1066                         EventHandler eh = (EventHandler)(Events [VisibleChangedEvent]);
1067                         if (eh != null)
1068                                 eh (this, e);
1069                 }
1070
1071                 protected internal virtual bool ProcessCmdKey (ref Message m, Keys keyData)
1072                 {
1073                         return false;
1074                 }
1075                 
1076                 protected internal virtual void SetBounds (Rectangle bounds)
1077                 {
1078                         if (this.bounds != bounds) {
1079                                 this.bounds = bounds;
1080                                 OnBoundsChanged ();
1081                         }
1082                 }
1083                 
1084                 protected virtual void SetVisibleCore (bool visible)
1085                 {
1086                         this.visible = visible;
1087                         this.OnVisibleChanged (EventArgs.Empty);
1088                         this.Invalidate ();
1089                 }
1090                 #endregion
1091
1092                 #region Public Events
1093                 static object AvailableChangedEvent = new object ();
1094                 static object BackColorChangedEvent = new object ();
1095                 static object ClickEvent = new object ();
1096                 static object DisplayStyleChangedEvent = new object ();
1097                 static object DoubleClickEvent = new object ();
1098                 static object EnabledChangedEvent = new object ();
1099                 static object ForeColorChangedEvent = new object ();
1100                 static object LocationChangedEvent = new object ();
1101                 static object MouseDownEvent = new object ();
1102                 static object MouseEnterEvent = new object ();
1103                 static object MouseHoverEvent = new object ();
1104                 static object MouseLeaveEvent = new object ();
1105                 static object MouseMoveEvent = new object ();
1106                 static object MouseUpEvent = new object ();
1107                 static object OwnerChangedEvent = new object ();
1108                 static object PaintEvent = new object ();
1109                 static object RightToLeftChangedEvent = new object ();
1110                 static object TextChangedEvent = new object ();
1111                 static object VisibleChangedEvent = new object ();
1112
1113                 [Browsable (false)]
1114                 public event EventHandler AvailableChanged {
1115                         add { Events.AddHandler (AvailableChangedEvent, value); }
1116                         remove {Events.RemoveHandler (AvailableChangedEvent, value); }
1117                 }
1118
1119                 public event EventHandler BackColorChanged {
1120                         add { Events.AddHandler (BackColorChangedEvent, value); }
1121                         remove {Events.RemoveHandler (BackColorChangedEvent, value); }
1122                 }
1123
1124                 public event EventHandler Click {
1125                         add { Events.AddHandler (ClickEvent, value); }
1126                         remove {Events.RemoveHandler (ClickEvent, value); }
1127                 }
1128
1129                 public event EventHandler DisplayStyleChanged {
1130                         add { Events.AddHandler (DisplayStyleChangedEvent, value); }
1131                         remove {Events.RemoveHandler (DisplayStyleChangedEvent, value); }
1132                 }
1133
1134                 public event EventHandler DoubleClick {
1135                         add { Events.AddHandler (DoubleClickEvent, value); }
1136                         remove {Events.RemoveHandler (DoubleClickEvent, value); }
1137                 }
1138
1139                 public event EventHandler EnabledChanged {
1140                         add { Events.AddHandler (EnabledChangedEvent, value); }
1141                         remove {Events.RemoveHandler (EnabledChangedEvent, value); }
1142                 }
1143
1144                 public event EventHandler ForeColorChanged {
1145                         add { Events.AddHandler (ForeColorChangedEvent, value); }
1146                         remove {Events.RemoveHandler (ForeColorChangedEvent, value); }
1147                 }
1148
1149                 public event EventHandler LocationChanged {
1150                         add { Events.AddHandler (LocationChangedEvent, value); }
1151                         remove {Events.RemoveHandler (LocationChangedEvent, value); }
1152                 }
1153
1154                 public event MouseEventHandler MouseDown {
1155                         add { Events.AddHandler (MouseDownEvent, value); }
1156                         remove {Events.RemoveHandler (MouseDownEvent, value); }
1157                 }
1158
1159                 public event EventHandler MouseEnter {
1160                         add { Events.AddHandler (MouseEnterEvent, value); }
1161                         remove {Events.RemoveHandler (MouseEnterEvent, value); }
1162                 }
1163
1164                 public event EventHandler MouseHover {
1165                         add { Events.AddHandler (MouseHoverEvent, value); }
1166                         remove {Events.RemoveHandler (MouseHoverEvent, value); }
1167                 }
1168
1169                 public event EventHandler MouseLeave {
1170                         add { Events.AddHandler (MouseLeaveEvent, value); }
1171                         remove {Events.RemoveHandler (MouseLeaveEvent, value); }
1172                 }
1173
1174                 public event MouseEventHandler MouseMove {
1175                         add { Events.AddHandler (MouseMoveEvent, value); }
1176                         remove {Events.RemoveHandler (MouseMoveEvent, value); }
1177                 }
1178
1179                 public event MouseEventHandler MouseUp {
1180                         add { Events.AddHandler (MouseUpEvent, value); }
1181                         remove {Events.RemoveHandler (MouseUpEvent, value); }
1182                 }
1183
1184                 public event EventHandler OwnerChanged {
1185                         add { Events.AddHandler (OwnerChangedEvent, value); }
1186                         remove {Events.RemoveHandler (OwnerChangedEvent, value); }
1187                 }
1188
1189                 public event PaintEventHandler Paint {
1190                         add { Events.AddHandler (PaintEvent, value); }
1191                         remove {Events.RemoveHandler (PaintEvent, value); }
1192                 }
1193
1194                 public event EventHandler RightToLeftChanged {
1195                         add { Events.AddHandler (RightToLeftChangedEvent, value); }
1196                         remove { Events.RemoveHandler (RightToLeftChangedEvent, value); }
1197                 }
1198                 
1199                 public event EventHandler TextChanged {
1200                         add { Events.AddHandler (TextChangedEvent, value); }
1201                         remove {Events.RemoveHandler (TextChangedEvent, value); }
1202                 }
1203
1204                 public event EventHandler VisibleChanged {
1205                         add { Events.AddHandler (VisibleChangedEvent, value); }
1206                         remove {Events.RemoveHandler (VisibleChangedEvent, value); }
1207                 }
1208                 #endregion
1209
1210                 #region Internal Methods
1211                 internal Rectangle AlignInRectangle (Rectangle outer, Size inner, ContentAlignment align)
1212                 {
1213                         int x = 0;
1214                         int y = 0;
1215
1216                         if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
1217                                 x = outer.X;
1218                         else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
1219                                 x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
1220                         else if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
1221                                 x = outer.Right - inner.Width;
1222                         if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight)
1223                                 y = outer.Y;
1224                         else if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight)
1225                                 y = outer.Y + (outer.Height - inner.Height) / 2;
1226                         else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomRight || align == ContentAlignment.BottomLeft)
1227                                 y = outer.Bottom - inner.Height;
1228
1229                         return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
1230                 }
1231
1232                 internal void CalculateAutoSize ()
1233                 {
1234                         this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty: this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
1235
1236                         if (!this.auto_size || this is ToolStripControlHost)
1237                                 return;
1238                         //this.text_size.Width += 6;
1239
1240                         Size final_size = this.CalculatePreferredSize (Size.Empty);
1241
1242                         if (final_size != this.Size) {
1243                                 this.bounds.Width = final_size.Width;
1244                                 if (this.parent != null)
1245                                         this.parent.PerformLayout ();
1246                         }
1247                 }
1248
1249                 internal virtual Size CalculatePreferredSize (Size constrainingSize)
1250                 {
1251                         if (!this.auto_size)
1252                                 return this.explicit_size;
1253                                 
1254                         Size preferred_size = this.DefaultSize;
1255
1256                         switch (this.display_style) {
1257                                 case ToolStripItemDisplayStyle.Text:
1258                                         int width = text_size.Width + this.padding.Horizontal;
1259                                         int height = text_size.Height + this.padding.Vertical;
1260                                         preferred_size = new Size (width, height);
1261                                         break;
1262                                 case ToolStripItemDisplayStyle.Image:
1263                                         if (this.Image == null)
1264                                                 preferred_size = this.DefaultSize;
1265                                         else {
1266                                                 switch (this.image_scaling) {
1267                                                         case ToolStripItemImageScaling.None:
1268                                                                 preferred_size = this.Image.Size;
1269                                                                 break;
1270                                                         case ToolStripItemImageScaling.SizeToFit:
1271                                                                 if (this.parent == null)
1272                                                                         preferred_size = this.Image.Size;
1273                                                                 else
1274                                                                         preferred_size = this.parent.ImageScalingSize;
1275                                                                 break;
1276                                                 }
1277                                         }
1278                                         break;
1279                                 case ToolStripItemDisplayStyle.ImageAndText:
1280                                         int width2 = text_size.Width + this.padding.Horizontal;
1281                                         int height2 = text_size.Height + this.padding.Vertical;
1282
1283                                         if (this.Image != null) {
1284                                                 switch (this.text_image_relation) {
1285                                                         case TextImageRelation.Overlay:
1286                                                                 width2 = Math.Max (width2, this.Image.Width);
1287                                                                 height2 = Math.Max (height2, this.Image.Height);
1288                                                                 break;
1289                                                         case TextImageRelation.ImageAboveText:
1290                                                         case TextImageRelation.TextAboveImage:
1291                                                                 width2 = Math.Max (width2, this.Image.Width);
1292                                                                 height2 += this.Image.Height;
1293                                                                 break;
1294                                                         case TextImageRelation.ImageBeforeText:
1295                                                         case TextImageRelation.TextBeforeImage:
1296                                                                 height2 = Math.Max (height2, this.Image.Height);
1297                                                                 width2 += this.Image.Width;
1298                                                                 break;
1299                                                 }
1300                                         }
1301
1302                                         preferred_size = new Size (width2, height2);
1303                                         break;
1304                         }
1305
1306                         if (!(this is ToolStripLabel)) {                // Everything but labels have a border
1307                                 preferred_size.Height += 4;
1308                                 preferred_size.Width += 4;
1309                         }
1310                         
1311                         // Account for ToolStripDropDownButton's drop down arrow
1312                         if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
1313                                 preferred_size.Width += 9;
1314
1315                         return preferred_size;
1316                 }
1317
1318                 internal void CalculateTextAndImageRectangles (out Rectangle text_rect, out Rectangle image_rect)
1319                 {
1320                         this.CalculateTextAndImageRectangles (this.ContentRectangle, out text_rect, out image_rect);
1321                 }
1322                 
1323                 internal void CalculateTextAndImageRectangles (Rectangle contentRectangle, out Rectangle text_rect, out Rectangle image_rect)
1324                 {
1325                         text_rect = Rectangle.Empty;
1326                         image_rect = Rectangle.Empty;
1327                                 
1328                         switch (this.display_style) {
1329                                 case ToolStripItemDisplayStyle.None:
1330                                         break;
1331                                 case ToolStripItemDisplayStyle.Text:
1332                                         if (this.text != string.Empty)
1333                                                 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1334                                         break;
1335                                 case ToolStripItemDisplayStyle.Image:
1336                                         if (this.Image != null)
1337                                                 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1338                                         break;
1339                                 case ToolStripItemDisplayStyle.ImageAndText:
1340                                         if (this.text != string.Empty && this.Image == null)
1341                                                 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1342                                         else if (this.text == string.Empty && this.Image != null)
1343                                                 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1344                                         else if (this.text == string.Empty && this.Image == null)
1345                                                 break;
1346                                         else {
1347                                                 Rectangle text_area;
1348                                                 Rectangle image_area;
1349
1350                                                 switch (this.text_image_relation) {
1351                                                         case TextImageRelation.Overlay:
1352                                                                 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1353                                                                 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1354                                                                 break;
1355                                                         case TextImageRelation.ImageAboveText:
1356                                                                 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Bottom - (text_size.Height - 4), contentRectangle.Width, text_size.Height - 4);
1357                                                                 image_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1358
1359                                                                 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1360                                                                 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1361                                                                 break;
1362                                                         case TextImageRelation.TextAboveImage:
1363                                                                 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, text_size.Height - 4);
1364                                                                 image_area = new Rectangle (contentRectangle.Left, text_area.Bottom, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1365
1366                                                                 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1367                                                                 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1368                                                                 break;
1369                                                         case TextImageRelation.ImageBeforeText:
1370                                                                 LayoutTextBeforeOrAfterImage (contentRectangle, false, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1371                                                                 break;
1372                                                         case TextImageRelation.TextBeforeImage:
1373                                                                 LayoutTextBeforeOrAfterImage (contentRectangle, true, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1374                                                                 break;
1375                                                 }
1376                                         }
1377                                         break;
1378                         }
1379                 }
1380
1381                 private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, ContentAlignment textAlign, ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
1382                 {
1383                         int element_spacing = 0;        // Spacing between the Text and the Image
1384                         int total_width = textSize.Width + element_spacing + imageSize.Width;
1385                         int excess_width = totalArea.Width - total_width;
1386                         int offset = 0;
1387                         
1388                         Rectangle final_text_rect;
1389                         Rectangle final_image_rect;
1390
1391                         HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
1392                         HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
1393                         
1394                         if (h_image == HorizontalAlignment.Left)
1395                                 offset = 0;
1396                         else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
1397                                 offset = excess_width;
1398                         else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
1399                                 offset += (int)(excess_width / 3);
1400                         else
1401                                 offset += (int)(2 * (excess_width / 3));
1402                                 
1403                         if (textFirst) {
1404                                 final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1405                                 final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1406                         } else {
1407                                 final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1408                                 final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1409                         }
1410                         
1411                         textRect = final_text_rect;
1412                         imageRect = final_image_rect;
1413                 }
1414                 
1415                 private HorizontalAlignment GetHorizontalAlignment (ContentAlignment align)
1416                 {
1417                         switch (align) {
1418                                 case ContentAlignment.BottomLeft:
1419                                 case ContentAlignment.MiddleLeft:
1420                                 case ContentAlignment.TopLeft:
1421                                         return HorizontalAlignment.Left;
1422                                 case ContentAlignment.BottomCenter:
1423                                 case ContentAlignment.MiddleCenter:
1424                                 case ContentAlignment.TopCenter:
1425                                         return HorizontalAlignment.Center;
1426                                 case ContentAlignment.BottomRight:
1427                                 case ContentAlignment.MiddleRight:
1428                                 case ContentAlignment.TopRight:
1429                                         return HorizontalAlignment.Right;
1430                         }
1431                         
1432                         return HorizontalAlignment.Left;
1433                 }
1434                 
1435                 private Size GetImageSize ()
1436                 {
1437                         if (this.Image == null)
1438                                 return Size.Empty;
1439                         
1440                         if (this.image_scaling == ToolStripItemImageScaling.None)
1441                                 return this.Image.Size;
1442                                 
1443                         if (this.Parent == null)
1444                                 return Size.Empty;
1445                                 
1446                         return this.Parent.ImageScalingSize;
1447                 }
1448                 
1449                 internal string GetToolTip ()
1450                 {
1451                         if (this.auto_tool_tip && string.IsNullOrEmpty (this.tool_tip_text))
1452                                 return this.text;
1453                                 
1454                         return this.tool_tip_text;
1455                 }
1456                 
1457                 internal void FireEvent (EventArgs e, ToolStripItemEventType met)
1458                 {
1459                         // If we're disabled, don't fire any of these events, except Paint
1460                         if (!this.Enabled && met != ToolStripItemEventType.Paint)
1461                                 return;
1462                                 
1463                         switch (met) {
1464                                 case ToolStripItemEventType.MouseUp:
1465                                         this.OnMouseUp ((MouseEventArgs)e);
1466                                         this.OnClick ((MouseEventArgs)e);
1467                                         break;
1468                                 case ToolStripItemEventType.MouseDown:
1469                                         this.OnMouseDown ((MouseEventArgs)e);
1470                                         break;
1471                                 case ToolStripItemEventType.MouseEnter:
1472                                         this.OnMouseEnter (e);
1473                                         break;
1474                                 case ToolStripItemEventType.MouseHover:
1475                                         this.OnMouseHover (e);
1476                                         break;
1477                                 case ToolStripItemEventType.MouseLeave:
1478                                         this.OnMouseLeave (e);
1479                                         break;
1480                                 case ToolStripItemEventType.MouseMove:
1481                                         this.OnMouseMove ((MouseEventArgs)e);
1482                                         break;
1483                                 case ToolStripItemEventType.Paint:
1484                                         this.OnPaint ((PaintEventArgs)e);
1485                                         break;
1486                                 case ToolStripItemEventType.Click:
1487                                         this.OnClick (e);
1488                                         break;
1489                         }
1490                 }
1491                 
1492                 internal virtual void SetPlacement (ToolStripItemPlacement placement)
1493                 {
1494                         this.placement = placement;
1495                 }
1496
1497                 internal bool InternalVisible {
1498                         get { return this.visible; }
1499                         set { this.visible = value; }
1500                 }
1501                 
1502                 internal Point Location {
1503                         get { return this.bounds.Location; }
1504                         set {
1505                                 if (this.bounds.Location != value) {
1506                                         this.bounds.Location = value;
1507                                         this.OnLocationChanged (EventArgs.Empty);
1508                                 }
1509                         }
1510                 }
1511
1512                 internal int Top {
1513                         get { return this.bounds.Y; }
1514                         set {
1515                                 if (this.bounds.Y != value) {
1516                                         this.bounds.Y = value;
1517                                         this.OnLocationChanged (EventArgs.Empty);
1518                                 }
1519                         }
1520                 }
1521
1522                 internal int Left {
1523                         get { return this.bounds.X; }
1524                         set {
1525                                 if (this.bounds.X != value) {
1526                                         this.bounds.X = value;
1527                                         this.OnLocationChanged (EventArgs.Empty);
1528                                 }
1529                         }
1530                 }
1531                 
1532                 internal int Right { get { return this.bounds.Right; } }
1533                 internal int Bottom { get { return this.bounds.Bottom; } }
1534                 #endregion
1535                 
1536                 public class ToolStripItemAccessibleObject : AccessibleObject
1537                 {
1538                         private ToolStripItem owner_item;
1539                         
1540                         public ToolStripItemAccessibleObject (ToolStripItem ownerItem)
1541                         {
1542                                 if (ownerItem == null)
1543                                         throw new ArgumentNullException ("ownerItem");
1544                                         
1545                                 this.owner_item = ownerItem;
1546                                 base.default_action = string.Empty;
1547                                 base.keyboard_shortcut = string.Empty;
1548                                 base.name = string.Empty;
1549                                 base.value = string.Empty;
1550                         }
1551
1552                         #region Public Properties
1553                         public override Rectangle Bounds {
1554                                 get {
1555                                         return owner_item.Visible ? owner_item.Bounds : Rectangle.Empty;
1556                                 }
1557                         }
1558
1559                         public override string DefaultAction {
1560                                 get { return base.DefaultAction; }
1561                         }
1562
1563                         public override string Description {
1564                                 get { return base.Description; }
1565                         }
1566
1567                         public override string Help {
1568                                 get { return base.Help; }
1569                         }
1570
1571                         public override string KeyboardShortcut {
1572                                 get { return base.KeyboardShortcut; }
1573                         }
1574
1575                         public override string Name {
1576                                 get {
1577                                         if (base.name == string.Empty)
1578                                                 return owner_item.Text;
1579                                                 
1580                                         return base.Name;
1581                                 }
1582                                 set { base.Name = value; }
1583                         }
1584
1585                         public override AccessibleObject Parent {
1586                                 get { return base.Parent; }
1587                         }
1588
1589                         public override AccessibleRole Role {
1590                                 get { return base.Role; }
1591                         }
1592
1593                         public override AccessibleStates State {
1594                                 get { return base.State; }
1595                         }
1596                         #endregion
1597
1598                         #region Public Methods
1599                         public void AddState (AccessibleStates state)
1600                         {
1601                                 base.state = state;
1602                         }
1603
1604                         public override void DoDefaultAction ()
1605                         {
1606                                 base.DoDefaultAction ();
1607                         }
1608
1609                         public override int GetHelpTopic (out string FileName)
1610                         {
1611                                 return base.GetHelpTopic (out FileName);
1612                         }
1613
1614                         public override AccessibleObject Navigate (AccessibleNavigation navdir)
1615                         {
1616                                 return base.Navigate (navdir);
1617                         }
1618
1619                         public override string ToString ()
1620                         {
1621                                 return string.Format ("ToolStripItemAccessibleObject: Owner = {0}", owner_item.ToString());
1622                         }
1623                         #endregion
1624                 }
1625         }
1626 }
1627 #endif