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