2007-06-04 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ComboBoxRenderer.cs
1 //
2 // ComboBoxRenderer.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 Novell, Inc.
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System.Drawing;
31 using System.Windows.Forms.VisualStyles;
32
33 namespace System.Windows.Forms
34 {
35         public sealed class ComboBoxRenderer
36         {
37                 #region Private Constructor
38                 private ComboBoxRenderer () { }
39                 #endregion
40
41                 #region Public Static Methods
42                 public static void DrawDropDownButton (Graphics g, Rectangle bounds, ComboBoxState state)
43                 {
44                         if (!IsSupported)
45                                 throw new InvalidOperationException ();
46
47                         GetComboRenderer (state).DrawBackground (g, bounds);
48                 }
49
50                 public static void DrawTextBox (Graphics g, Rectangle bounds, string comboBoxText, Font font, Rectangle textBounds, TextFormatFlags flags, ComboBoxState state)
51                 {
52                         if (!IsSupported)
53                                 throw new InvalidOperationException ();
54
55                         GetTextBoxRenderer (state).DrawBackground (g, bounds);
56
57                         if (textBounds == Rectangle.Empty)
58                                 textBounds = new Rectangle (bounds.Left + 3, bounds.Top, bounds.Width - 4, bounds.Height);
59
60                         if (comboBoxText != String.Empty)
61                                 if (state == ComboBoxState.Disabled)
62                                         TextRenderer.DrawText (g, comboBoxText, font, textBounds, SystemColors.GrayText, flags);
63                                 else
64                                         TextRenderer.DrawText (g, comboBoxText, font, textBounds, SystemColors.ControlText, flags);
65                 }
66
67                 public static void DrawTextBox (Graphics g, Rectangle bounds, ComboBoxState state)
68                 {
69                         DrawTextBox (g, bounds, String.Empty, null, Rectangle.Empty, TextFormatFlags.VerticalCenter, state);
70                 }
71
72                 public static void DrawTextBox (Graphics g, Rectangle bounds, string comboBoxText, Font font, ComboBoxState state)
73                 {
74                         DrawTextBox (g, bounds, comboBoxText, font, Rectangle.Empty, TextFormatFlags.VerticalCenter, state);
75                 }
76
77                 public static void DrawTextBox (Graphics g, Rectangle bounds, string comboBoxText, Font font, Rectangle textBounds, ComboBoxState state)
78                 {
79                         DrawTextBox (g, bounds, comboBoxText, font, textBounds, TextFormatFlags.Default, state);
80                 }
81
82                 public static void DrawTextBox (Graphics g, Rectangle bounds, string comboBoxText, Font font, TextFormatFlags flags, ComboBoxState state)
83                 {
84                         DrawTextBox (g, bounds, comboBoxText, font, Rectangle.Empty, flags |= TextFormatFlags.VerticalCenter, state);
85                 }
86                 #endregion
87
88                 #region Private Static Methods
89                 private static VisualStyleRenderer GetComboRenderer (ComboBoxState state)
90                 {
91                         switch (state) {
92                                 case ComboBoxState.Disabled:
93                                         return new VisualStyleRenderer (VisualStyleElement.ComboBox.DropDownButton.Disabled);
94                                 case ComboBoxState.Hot:
95                                         return new VisualStyleRenderer (VisualStyleElement.ComboBox.DropDownButton.Hot);
96                                 case ComboBoxState.Normal:
97                                 default:
98                                         return new VisualStyleRenderer (VisualStyleElement.ComboBox.DropDownButton.Normal);
99                                 case ComboBoxState.Pressed:
100                                         return new VisualStyleRenderer (VisualStyleElement.ComboBox.DropDownButton.Pressed);
101                         }
102                 }
103
104                 private static VisualStyleRenderer GetTextBoxRenderer (ComboBoxState state)
105                 {
106                         switch (state) {
107                                 case ComboBoxState.Disabled:
108                                         return new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Disabled);
109                                 case ComboBoxState.Hot:
110                                         return new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Hot);
111                                 case ComboBoxState.Normal:
112                                 case ComboBoxState.Pressed:
113                                 default:
114                                         return new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Normal);
115                         }
116                 }
117                 #endregion
118
119                 #region Public Static Properties
120                 public static bool IsSupported {
121                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
122                 }
123                 #endregion
124         }
125 }
126 #endif