2007-01-08 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDownButton.cs
1 //
2 // ToolStripDropDownButton.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32
33 namespace System.Windows.Forms
34 {
35         public class ToolStripDropDownButton : ToolStripDropDownItem
36         {
37                 private bool show_drop_down_arrow = true;
38
39                 #region Public Constructors
40                 public ToolStripDropDownButton()
41                         : this (string.Empty, null, null, string.Empty)
42                 {
43                 }
44                 
45                 public ToolStripDropDownButton (Image image)
46                         : this (string.Empty, image, null, string.Empty)
47                 {
48                 }
49                 
50                 public ToolStripDropDownButton (string text)
51                         : this (text, null, null, string.Empty)
52                 {
53                 }
54                 
55                 public ToolStripDropDownButton (string text, Image image)
56                         : this (text, image, null, string.Empty)
57                 {
58                 }
59                 
60                 public ToolStripDropDownButton (string text, Image image, EventHandler onClick)
61                         : this (text, image, onClick, string.Empty)
62                 {
63                 }
64                 
65                 public ToolStripDropDownButton (string text, Image image, params ToolStripItem[] dropDownItems)
66                         : base (text, image, dropDownItems)
67                 {
68                 }
69                 
70                 public ToolStripDropDownButton (string text, Image image, EventHandler onClick, string name)
71                         : base (text, image, onClick, name)
72                 {
73                 }
74                 #endregion
75
76                 #region Public Properties
77                 public new bool AutoToolTip {
78                         get { return base.AutoToolTip; }
79                         set { base.AutoToolTip = value; }
80                 }
81                 
82                 public bool ShowDropDownArrow {
83                         get { return this.show_drop_down_arrow; }
84                         set { this.show_drop_down_arrow = value; }
85                 }
86                 #endregion
87
88                 #region Protected Properties
89                 protected override bool DefaultAutoToolTip {
90                         get { return true; }
91                 }
92                 #endregion
93
94                 #region Protected Methods
95                 protected override ToolStripDropDown CreateDefaultDropDown ()
96                 {
97                         return base.CreateDefaultDropDown ();
98                 }
99
100                 protected override void OnMouseDown (MouseEventArgs e)
101                 {
102                         if (this.DropDown.Visible)
103                                 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
104                         else
105                                 this.ShowDropDown ();
106                         
107                         base.OnMouseDown (e);
108                 }
109                 protected override void OnPaint (PaintEventArgs e)
110                 {
111                         base.OnPaint (e);
112
113                         if (this.Owner != null) {
114                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
115                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
116
117                                 this.Owner.Renderer.DrawDropDownButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
118
119                                 Rectangle text_layout_rect;
120                                 Rectangle image_layout_rect;
121
122                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
123
124                                 if (text_layout_rect != Rectangle.Empty)
125                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
126                                 if (image_layout_rect != Rectangle.Empty)
127                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
128                                 if (this.ShowDropDownArrow)
129                                         this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 10, 0, 6, this.Height), Color.Black, ArrowDirection.Down));
130                                 return;
131                         }
132                 }
133                 #endregion
134         }
135 }
136 #endif