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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Novell, Inc.
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.Windows.Forms.VisualStyles;
32 namespace System.Windows.Forms
34 public sealed class CheckBoxRenderer
36 private static bool always_use_visual_styles = false;
38 #region Private Constructor
39 private CheckBoxRenderer () {}
42 #region Public Static Methods
43 public static void DrawCheckBox (Graphics g, Point glyphLocation, CheckBoxState state)
45 DrawCheckBox (g, glyphLocation, Rectangle.Empty, String.Empty, null, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
48 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, bool focused, CheckBoxState state)
50 DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
53 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, bool focused, CheckBoxState state)
55 DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, flags, null, Rectangle.Empty, focused, state);
58 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
60 DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
63 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
65 Rectangle bounds = new Rectangle (glyphLocation, GetGlyphSize (g, state));
67 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
68 VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
70 vsr.DrawBackground (g, bounds);
73 vsr.DrawImage (g, imageBounds, image);
76 ControlPaint.DrawFocusRectangle (g, textBounds);
78 if (checkBoxText != String.Empty)
79 if (state == CheckBoxState.CheckedDisabled || state == CheckBoxState.MixedDisabled || state == CheckBoxState.UncheckedDisabled)
80 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.GrayText, flags);
82 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
85 case CheckBoxState.CheckedDisabled:
86 case CheckBoxState.MixedDisabled:
87 case CheckBoxState.MixedPressed:
88 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive | ButtonState.Checked);
90 case CheckBoxState.CheckedHot:
91 case CheckBoxState.CheckedNormal:
92 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Checked);
94 case CheckBoxState.CheckedPressed:
95 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Pushed | ButtonState.Checked);
97 case CheckBoxState.MixedHot:
98 case CheckBoxState.MixedNormal:
99 ControlPaint.DrawMixedCheckBox (g, bounds, ButtonState.Checked);
101 case CheckBoxState.UncheckedDisabled:
102 case CheckBoxState.UncheckedPressed:
103 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive);
105 case CheckBoxState.UncheckedHot:
106 case CheckBoxState.UncheckedNormal:
107 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Normal);
112 g.DrawImage (image, imageBounds);
115 ControlPaint.DrawFocusRectangle (g, textBounds);
117 if (checkBoxText != String.Empty)
118 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
122 public static bool IsBackgroundPartiallyTransparent (CheckBoxState state)
124 if (!VisualStyleRenderer.IsSupported)
127 VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
129 return vsr.IsBackgroundPartiallyTransparent ();
132 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
134 if (!VisualStyleRenderer.IsSupported)
137 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
139 vsr.DrawParentBackground (g, bounds, childControl);
142 public static Size GetGlyphSize (Graphics g, CheckBoxState state)
144 if (!VisualStyleRenderer.IsSupported)
145 return new Size (13, 13);
147 VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
149 return vsr.GetPartSize (g, ThemeSizeType.Draw);
153 #region Private Static Methods
154 private static VisualStyleRenderer GetCheckBoxRenderer (CheckBoxState state)
157 case CheckBoxState.CheckedDisabled:
158 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedDisabled);
159 case CheckBoxState.CheckedHot:
160 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedHot);
161 case CheckBoxState.CheckedNormal:
162 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedNormal);
163 case CheckBoxState.CheckedPressed:
164 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedPressed);
165 case CheckBoxState.MixedDisabled:
166 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedDisabled);
167 case CheckBoxState.MixedHot:
168 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedHot);
169 case CheckBoxState.MixedNormal:
170 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedNormal);
171 case CheckBoxState.MixedPressed:
172 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedPressed);
173 case CheckBoxState.UncheckedDisabled:
174 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedDisabled);
175 case CheckBoxState.UncheckedHot:
176 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedHot);
177 case CheckBoxState.UncheckedNormal:
179 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
180 case CheckBoxState.UncheckedPressed:
181 return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedPressed);
186 #region Public Static Properties
187 public static bool RenderMatchingApplicationState {
188 get { return !always_use_visual_styles; }
189 set { always_use_visual_styles = !value; }