2007-07-18 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDownMenu.cs
1 //
2 // ToolStripDropDownMenu.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 using System.Drawing;
30 using System.ComponentModel;
31 using System.Runtime.InteropServices;
32 using System.Windows.Forms.Layout;
33
34 namespace System.Windows.Forms
35 {
36         [ComVisible (true)]
37         [ClassInterface (ClassInterfaceType.AutoDispatch)]
38         [Designer ("System.Windows.Forms.Design.ToolStripDropDownDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39         public class ToolStripDropDownMenu : ToolStripDropDown
40         {
41                 private ToolStripLayoutStyle layout_style;
42                 private bool show_check_margin;
43                 private bool show_image_margin;
44
45                 #region Public Constructors
46                 public ToolStripDropDownMenu () : base ()
47                 {
48                         this.layout_style = ToolStripLayoutStyle.Flow;
49                         this.show_image_margin = true;
50                 }
51                 #endregion
52
53                 #region Public Properties
54                 public override Rectangle DisplayRectangle {
55                         get { return base.DisplayRectangle; }
56                 }
57
58                 public override LayoutEngine LayoutEngine {
59                         get { return base.LayoutEngine; }
60                 }
61                 
62                 [DefaultValue (ToolStripLayoutStyle.Flow)]
63                 public new ToolStripLayoutStyle LayoutStyle {
64                         get { return this.layout_style; }
65                         set { this.layout_style = value; }
66                 }
67                 
68                 [DefaultValue (false)]
69                 public bool ShowCheckMargin {
70                         get { return this.show_check_margin; }
71                         set { this.show_check_margin = value; }
72                 }
73
74                 [DefaultValue (true)]
75                 public bool ShowImageMargin {
76                         get { return this.show_image_margin; }
77                         set { this.show_image_margin = value; }
78                 }
79                 #endregion
80
81                 #region Protected Properties
82                 protected override Padding DefaultPadding {
83                         get { return base.DefaultPadding; }
84                 }
85
86                 protected internal override Size MaxItemSize {
87                         get { return Size; }
88                 }
89                 #endregion
90
91                 #region Protected Methods
92                 protected internal override ToolStripItem CreateDefaultItem (string text, Image image, EventHandler onClick)
93                 {
94                         return base.CreateDefaultItem (text, image, onClick);
95                 }
96
97                 protected override void OnFontChanged (EventArgs e)
98                 {
99                         base.OnFontChanged (e);
100                 }
101
102                 protected override void OnLayout (LayoutEventArgs e)
103                 {
104                         // Find the widest menu item
105                         int widest = 0;
106
107                         foreach (ToolStripItem tsi in this.Items) {
108                                 if (!tsi.Available)
109                                         continue;
110
111                                 tsi.SetPlacement (ToolStripItemPlacement.Main);
112
113                                 if (tsi.GetPreferredSize (Size.Empty).Width > widest)
114                                         widest = tsi.GetPreferredSize (Size.Empty).Width;
115                         }
116
117                         int x = this.Padding.Left;
118                         
119                         if (show_check_margin || show_image_margin)
120                                 widest += 68 - this.Padding.Horizontal;
121                         else
122                                 widest += 47 - this.Padding.Horizontal;
123                         
124                         int y = this.Padding.Top;
125
126                         foreach (ToolStripItem tsi in this.Items) {
127                                 if (!tsi.Available)
128                                         continue;
129
130                                 y += tsi.Margin.Top;
131
132                                 int height = 0;
133
134                                 if (tsi is ToolStripSeparator)
135                                         height = 7;
136                                 else
137                                         height = 22;
138
139                                 tsi.SetBounds (new Rectangle (x, y, widest, height));
140                                 y += tsi.Height + tsi.Margin.Bottom;
141                         }
142
143                         this.Size = new Size (widest + this.Padding.Horizontal, y + this.Padding.Bottom);// + 2);
144                         this.SetDisplayedItems ();
145                         this.OnLayoutCompleted (EventArgs.Empty);
146                         this.Invalidate ();
147                 }
148                 
149                 protected override void OnPaintBackground (PaintEventArgs pevent)
150                 {
151                         Rectangle affected_bounds = new Rectangle (Point.Empty, this.Size);
152
153                         ToolStripRenderEventArgs e = new ToolStripRenderEventArgs (pevent.Graphics, this, affected_bounds, SystemColors.Control);
154                         e.InternalConnectedArea = CalculateConnectedArea ();
155
156                         this.Renderer.DrawToolStripBackground (e);
157                         
158                         if (this.ShowCheckMargin || this.ShowImageMargin)
159                                 this.Renderer.DrawImageMargin (e);
160                 }
161
162                 protected override void SetDisplayedItems ()
163                 {
164                         base.SetDisplayedItems ();
165                 }
166                 #endregion
167
168                 #region Internal Methods
169                 internal override Rectangle CalculateConnectedArea ()
170                 {
171                         if (this.OwnerItem != null && !this.OwnerItem.IsOnDropDown)
172                                 return new Rectangle (1, 0, this.OwnerItem.Width - 2, 2);
173
174                         return base.CalculateConnectedArea ();
175                 }
176                 #endregion
177         }
178 }
179 #endif