2006-10-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripLabel.cs
1 //\r
2 // ToolStripLabel.cs\r
3 //\r
4 // Permission is hereby granted, free of charge, to any person obtaining\r
5 // a copy of this software and associated documentation files (the\r
6 // "Software"), to deal in the Software without restriction, including\r
7 // without limitation the rights to use, copy, modify, merge, publish,\r
8 // distribute, sublicense, and/or sell copies of the Software, and to\r
9 // permit persons to whom the Software is furnished to do so, subject to\r
10 // the following conditions:\r
11 // \r
12 // The above copyright notice and this permission notice shall be\r
13 // included in all copies or substantial portions of the Software.\r
14 // \r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22 //\r
23 // Copyright (c) 2006 Jonathan Pobst\r
24 //\r
25 // Authors:\r
26 //      Jonathan Pobst (monkey@jpobst.com)\r
27 //\r
28 #if NET_2_0\r
29 \r
30 using System.Drawing;\r
31 \r
32 namespace System.Windows.Forms\r
33 {\r
34         public class ToolStripLabel : ToolStripItem\r
35         {\r
36                 private Color active_link_color;\r
37                 private bool is_link;\r
38                 private LinkBehavior link_behavior;\r
39                 private Color link_color;\r
40                 private bool link_visited;\r
41                 private Color visited_link_color;\r
42 \r
43                 #region Public Constructors\r
44                 public ToolStripLabel ()\r
45                         : this (String.Empty, null, false, null, String.Empty)\r
46                 {\r
47                 }\r
48 \r
49                 public ToolStripLabel (Image image)\r
50                         : this (String.Empty, image, false, null, String.Empty)\r
51                 {\r
52                 }\r
53 \r
54                 public ToolStripLabel (string text)\r
55                         : this (text, null, false, null, String.Empty)\r
56                 {\r
57                 }\r
58 \r
59                 public ToolStripLabel (string text, Image image)\r
60                         : this (text, image, false, null, String.Empty)\r
61                 {\r
62                 }\r
63 \r
64                 public ToolStripLabel (string text, Image image, bool isLink)\r
65                         : this (text, image, isLink, null, String.Empty)\r
66                 {\r
67                 }\r
68 \r
69                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)\r
70                         : this (text, image, isLink, onClick, String.Empty)\r
71                 {\r
72                 }\r
73 \r
74                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)\r
75                         : base (text, image, onClick, name)\r
76                 {\r
77                         this.active_link_color = Color.Red;\r
78                         this.is_link = isLink;\r
79                         this.link_behavior = LinkBehavior.SystemDefault;\r
80                         this.link_color = Color.Blue;\r
81                         this.link_visited = false;\r
82                         this.visited_link_color = Color.Purple;\r
83                 }\r
84                 #endregion\r
85 \r
86                 #region Public Properties\r
87                 public Color ActiveLinkColor {\r
88                         get { return this.active_link_color; }\r
89                         set {\r
90                                 this.active_link_color = value; \r
91                                 this.Invalidate ();\r
92                         }\r
93                 }\r
94 \r
95                 public override bool CanSelect { get { return false; } }\r
96                 \r
97                 public bool IsLink {\r
98                         get { return this.is_link; }\r
99                         set {\r
100                                 this.is_link = value; \r
101                                 this.Invalidate ();\r
102                         }\r
103                 }\r
104 \r
105                 public LinkBehavior LinkBehavior {\r
106                         get { return this.link_behavior; }\r
107                         set {\r
108                                 this.link_behavior = value; \r
109                                 this.Invalidate ();\r
110                         }\r
111                 }\r
112 \r
113                 public Color LinkColor {\r
114                         get { return this.link_color; }\r
115                         set {\r
116                                 this.link_color = value; \r
117                                 this.Invalidate ();\r
118                         }\r
119                 }\r
120 \r
121                 public bool LinkVisited {\r
122                         get { return this.link_visited; }\r
123                         set {\r
124                                 this.link_visited = value; \r
125                                 this.Invalidate ();\r
126                         }\r
127                 }\r
128 \r
129                 public Color VisitedLinkColor\r
130                 {\r
131                         get { return this.visited_link_color; }\r
132                         set {\r
133                                 this.visited_link_color = value; \r
134                                 this.Invalidate ();\r
135                         }\r
136                 }\r
137                 #endregion\r
138 \r
139                 #region Protected Methods\r
140                 protected override void OnFontChanged (EventArgs e)\r
141                 {\r
142                         base.OnFontChanged (e);\r
143                 }\r
144 \r
145                 protected override void OnMouseEnter (EventArgs e)\r
146                 {\r
147                         base.OnMouseEnter (e);\r
148                 }\r
149 \r
150                 protected override void OnMouseLeave (EventArgs e)\r
151                 {\r
152                         base.OnMouseLeave (e);\r
153                 }\r
154                 \r
155                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)\r
156                 {\r
157                         base.OnPaint (e);\r
158 \r
159                         if (this.Owner != null) {\r
160                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;\r
161                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);\r
162 \r
163                                 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));\r
164 \r
165                                 Rectangle text_layout_rect;\r
166                                 Rectangle image_layout_rect;\r
167 \r
168                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);\r
169 \r
170                                 if (image_layout_rect != Rectangle.Empty)\r
171                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));\r
172                                 if (text_layout_rect != Rectangle.Empty)\r
173                                         if (this.is_link) {\r
174                                                 if (this.Pressed)               // Mouse Down\r
175                                                 {\r
176                                                         switch (this.link_behavior) {\r
177                                                                 case LinkBehavior.SystemDefault:\r
178                                                                 case LinkBehavior.AlwaysUnderline:\r
179                                                                 case LinkBehavior.HoverUnderline:\r
180                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.active_link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));\r
181                                                                         break;\r
182                                                                 case LinkBehavior.NeverUnderline:\r
183                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.active_link_color, this.Font, this.TextAlign));\r
184                                                                         break;\r
185                                                         }\r
186                                                 }\r
187                                                 else if (this.Selected)         // Hover\r
188                                                 {\r
189                                                         switch (this.link_behavior) {\r
190                                                                 case LinkBehavior.SystemDefault:\r
191                                                                 case LinkBehavior.AlwaysUnderline:\r
192                                                                 case LinkBehavior.HoverUnderline:\r
193                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));\r
194                                                                         break;\r
195                                                                 case LinkBehavior.NeverUnderline:\r
196                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));\r
197                                                                         break;\r
198                                                         }\r
199                                                 }\r
200                                                 else {\r
201 \r
202                                                         if (this.link_visited)          // Normal, Visited\r
203                                                         {\r
204                                                                 switch (this.link_behavior) {\r
205                                                                         case LinkBehavior.SystemDefault:\r
206                                                                         case LinkBehavior.AlwaysUnderline:\r
207                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.visited_link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));\r
208                                                                                 break;\r
209                                                                         case LinkBehavior.NeverUnderline:\r
210                                                                         case LinkBehavior.HoverUnderline:\r
211                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.visited_link_color, this.Font, this.TextAlign));\r
212                                                                                 break;\r
213                                                                 }\r
214                                                         }\r
215                                                         else                            // Normal\r
216                                                         {\r
217                                                                 switch (this.link_behavior) {\r
218                                                                         case LinkBehavior.SystemDefault:\r
219                                                                         case LinkBehavior.AlwaysUnderline:\r
220                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));\r
221                                                                                 break;\r
222                                                                         case LinkBehavior.NeverUnderline:\r
223                                                                         case LinkBehavior.HoverUnderline:\r
224                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));\r
225                                                                                 break;\r
226                                                                 }\r
227 \r
228                                                         }\r
229                                                 }\r
230                                         } else\r
231                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));\r
232                         }\r
233                 }\r
234                 #endregion\r
235         }\r
236 }\r
237 #endif