* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CheckBoxRenderer.cs
1 //
2 // CheckBoxRenderer.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 #if NET_2_0
35         public
36 #endif
37         sealed class CheckBoxRenderer
38         {
39                 private static bool always_use_visual_styles = false;
40
41                 #region Private Constructor
42                 private CheckBoxRenderer () {}
43                 #endregion
44                 
45                 #region Public Static Methods
46                 public static void DrawCheckBox (Graphics g, Point glyphLocation, CheckBoxState state)
47                 {
48                         DrawCheckBox (g, glyphLocation, Rectangle.Empty, String.Empty, null, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
49                 }
50
51                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, bool focused, CheckBoxState state)
52                 {
53                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
54                 }
55
56                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, bool focused, CheckBoxState state)
57                 {
58                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, flags, null, Rectangle.Empty, focused, state);
59                 }
60
61                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
62                 {
63                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
64                 }
65
66                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
67                 {
68                         Rectangle bounds = new Rectangle (glyphLocation, GetGlyphSize (g, state));
69
70                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
71                                 VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
72
73                                 vsr.DrawBackground (g, bounds);
74
75                                 if (image != null)
76                                         vsr.DrawImage (g, imageBounds, image);
77
78                                 if (focused)
79                                         ControlPaint.DrawFocusRectangle (g, textBounds);
80
81                                 if (checkBoxText != String.Empty)
82                                         if (state == CheckBoxState.CheckedDisabled || state == CheckBoxState.MixedDisabled || state == CheckBoxState.UncheckedDisabled)
83                                                 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.GrayText, flags);
84                                         else
85                                                 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
86                         } else {
87                                 switch (state) {
88                                         case CheckBoxState.CheckedDisabled:
89                                         case CheckBoxState.MixedDisabled:
90                                         case CheckBoxState.MixedPressed:
91                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive | ButtonState.Checked);
92                                                 break;
93                                         case CheckBoxState.CheckedHot:
94                                         case CheckBoxState.CheckedNormal:
95                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Checked);
96                                                 break;
97                                         case CheckBoxState.CheckedPressed:
98                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Pushed | ButtonState.Checked);
99                                                 break;
100                                         case CheckBoxState.MixedHot:
101                                         case CheckBoxState.MixedNormal:
102                                                 ControlPaint.DrawMixedCheckBox (g, bounds, ButtonState.Checked);
103                                                 break;
104                                         case CheckBoxState.UncheckedDisabled:
105                                         case CheckBoxState.UncheckedPressed:
106                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive);
107                                                 break;
108                                         case CheckBoxState.UncheckedHot:
109                                         case CheckBoxState.UncheckedNormal:
110                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Normal);
111                                                 break;
112                                 }
113
114                                 if (image != null)
115                                         g.DrawImage (image, imageBounds);
116
117                                 if (focused)
118                                         ControlPaint.DrawFocusRectangle (g, textBounds);
119
120                                 if (checkBoxText != String.Empty)
121                                         TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
122                         }
123                 }
124
125                 public static bool IsBackgroundPartiallyTransparent (CheckBoxState state)
126                 {
127                         if (!VisualStyleRenderer.IsSupported)
128                                 return false;
129
130                         VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
131
132                         return vsr.IsBackgroundPartiallyTransparent ();
133                 }
134
135                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
136                 {
137                         if (!VisualStyleRenderer.IsSupported)
138                                 return;
139                                 
140                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
141
142                         vsr.DrawParentBackground (g, bounds, childControl);
143                 }
144
145                 public static Size GetGlyphSize (Graphics g, CheckBoxState state)
146                 {
147                         if (!VisualStyleRenderer.IsSupported)
148                                 return new Size (13, 13);
149
150                         VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
151
152                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
153                 }
154                 #endregion
155
156                 #region Private Static Methods
157                 private static VisualStyleRenderer GetCheckBoxRenderer (CheckBoxState state)
158                 {
159                         switch (state) {
160                                 case CheckBoxState.CheckedDisabled:
161                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedDisabled);
162                                 case CheckBoxState.CheckedHot:
163                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedHot);
164                                 case CheckBoxState.CheckedNormal:
165                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedNormal);
166                                 case CheckBoxState.CheckedPressed:
167                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedPressed);
168                                 case CheckBoxState.MixedDisabled:
169                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedDisabled);
170                                 case CheckBoxState.MixedHot:
171                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedHot);
172                                 case CheckBoxState.MixedNormal:
173                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedNormal);
174                                 case CheckBoxState.MixedPressed:
175                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedPressed);
176                                 case CheckBoxState.UncheckedDisabled:
177                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedDisabled);
178                                 case CheckBoxState.UncheckedHot:
179                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedHot);
180                                 case CheckBoxState.UncheckedNormal:
181                                 default:
182                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
183                                 case CheckBoxState.UncheckedPressed:
184                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedPressed);
185                         }
186                 }
187                 #endregion
188
189                 #region Public Static Properties
190                 public static bool RenderMatchingApplicationState {
191                         get { return !always_use_visual_styles; }
192                         set { always_use_visual_styles = !value; }
193                 }
194                 #endregion
195         }
196 }