Fix bug #395
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextBoxRenderer.cs
1 //
2 // TextBoxRenderer.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 TextBoxRenderer
35         {
36                 #region Private Constructor
37                 private TextBoxRenderer () { }
38                 #endregion
39
40                 #region Public Static Methods
41                 public static void DrawTextBox (Graphics g, Rectangle bounds, TextBoxState state)
42                 {
43                         DrawTextBox (g, bounds, String.Empty, null, Rectangle.Empty, TextFormatFlags.Default, state);
44                 }
45
46                 public static void DrawTextBox (Graphics g, Rectangle bounds, string textBoxText, Font font, TextBoxState state)
47                 {
48                         DrawTextBox (g, bounds, textBoxText, font, Rectangle.Empty, TextFormatFlags.Default, state);
49                 }
50
51                 public static void DrawTextBox (Graphics g, Rectangle bounds, string textBoxText, Font font, Rectangle textBounds, TextBoxState state)
52                 {
53                         DrawTextBox (g, bounds, textBoxText, font, textBounds, TextFormatFlags.Default, state);
54                 }
55
56                 public static void DrawTextBox (Graphics g, Rectangle bounds, string textBoxText, Font font, TextFormatFlags flags, TextBoxState state)
57                 {
58                         DrawTextBox (g, bounds, textBoxText, font, Rectangle.Empty, flags, state);
59                 }
60
61                 public static void DrawTextBox (Graphics g, Rectangle bounds, string textBoxText, Font font, Rectangle textBounds, TextFormatFlags flags, TextBoxState state)
62                 {
63                         if (!IsSupported)
64                                 throw new InvalidOperationException ();
65
66                         VisualStyleRenderer vsr;
67
68                         switch (state) {
69                                 case TextBoxState.Assist:
70                                         vsr = new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Assist);
71                                         break;
72                                 case TextBoxState.Disabled:
73                                         vsr = new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Disabled);
74                                         break;
75                                 case TextBoxState.Hot:
76                                         vsr = new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Hot);
77                                         break;
78                                 case TextBoxState.Normal:
79                                 case TextBoxState.Readonly:
80                                 default:
81                                         vsr = new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Normal);
82                                         break;
83                                 case TextBoxState.Selected:
84                                         vsr = new VisualStyleRenderer (VisualStyleElement.TextBox.TextEdit.Selected);
85                                         break;
86                         }
87
88                         vsr.DrawBackground (g, bounds);
89
90                         if (textBounds == Rectangle.Empty)
91                                 textBounds = new Rectangle (bounds.Left + 3, bounds.Top + 3, bounds.Width - 6, bounds.Height - 6);
92
93                         if (textBoxText != String.Empty)
94                                 if (state == TextBoxState.Disabled)
95                                         TextRenderer.DrawText (g, textBoxText, font, textBounds, SystemColors.GrayText, flags);
96                                 else
97                                         TextRenderer.DrawText (g, textBoxText, font, textBounds, SystemColors.ControlText, flags);
98                 }
99                 #endregion
100
101                 #region Public Static Properties
102                 public static bool IsSupported {
103                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
104                 }
105                 #endregion
106         }
107 }