2 // RadioButtonRenderer.cs
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)
31 using System.Windows.Forms.VisualStyles;
33 namespace System.Windows.Forms
35 public sealed class RadioButtonRenderer
37 private static bool always_use_visual_styles = false;
39 #region Private Constructor
40 private RadioButtonRenderer () { }
43 #region Public Static Methods
44 public static void DrawRadioButton (Graphics g, Point glyphLocation, RadioButtonState state)
46 DrawRadioButton (g, glyphLocation, Rectangle.Empty, String.Empty, null, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
49 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, bool focused, RadioButtonState state)
51 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
54 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, bool focused, RadioButtonState state)
56 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, flags, null, Rectangle.Empty, focused, state);
59 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, Image image, Rectangle imageBounds, bool focused, RadioButtonState state)
61 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
64 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, RadioButtonState state)
66 Rectangle bounds = new Rectangle (glyphLocation, GetGlyphSize (g, state));
68 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
69 VisualStyleRenderer vsr = GetRadioButtonRenderer (state);
71 vsr.DrawBackground (g, bounds);
74 vsr.DrawImage (g, imageBounds, image);
77 ControlPaint.DrawFocusRectangle (g, textBounds);
79 if (radioButtonText != String.Empty)
80 if (state == RadioButtonState.CheckedDisabled || state == RadioButtonState.UncheckedDisabled)
81 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.GrayText, flags);
83 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.ControlText, flags);
87 case RadioButtonState.CheckedDisabled:
88 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Inactive | ButtonState.Checked);
90 case RadioButtonState.CheckedHot:
91 case RadioButtonState.CheckedNormal:
92 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Checked);
94 case RadioButtonState.CheckedPressed:
95 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Pushed | ButtonState.Checked);
97 case RadioButtonState.UncheckedDisabled:
98 case RadioButtonState.UncheckedPressed:
99 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Inactive);
101 case RadioButtonState.UncheckedHot:
102 case RadioButtonState.UncheckedNormal:
103 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Normal);
108 g.DrawImage (image, imageBounds);
111 ControlPaint.DrawFocusRectangle (g, textBounds);
113 if (radioButtonText != String.Empty)
114 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.ControlText, flags);
119 public static bool IsBackgroundPartiallyTransparent (RadioButtonState state)
121 if (!VisualStyleRenderer.IsSupported)
124 VisualStyleRenderer vsr = GetRadioButtonRenderer (state);
126 return vsr.IsBackgroundPartiallyTransparent ();
129 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
131 if (!VisualStyleRenderer.IsSupported)
134 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedNormal);
136 vsr.DrawParentBackground (g, bounds, childControl);
139 public static Size GetGlyphSize (Graphics g, RadioButtonState state)
141 if (!VisualStyleRenderer.IsSupported)
142 return new Size (13, 13);
144 VisualStyleRenderer vsr = GetRadioButtonRenderer(state);
146 return vsr.GetPartSize (g, ThemeSizeType.Draw);
150 #region Private Static Methods
151 private static VisualStyleRenderer GetRadioButtonRenderer (RadioButtonState state)
154 case RadioButtonState.CheckedDisabled:
155 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedDisabled);
156 case RadioButtonState.CheckedHot:
157 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedHot);
158 case RadioButtonState.CheckedNormal:
159 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedNormal);
160 case RadioButtonState.CheckedPressed:
161 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedPressed);
162 case RadioButtonState.UncheckedDisabled:
163 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedDisabled);
164 case RadioButtonState.UncheckedHot:
165 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedHot);
166 case RadioButtonState.UncheckedNormal:
168 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedNormal);
169 case RadioButtonState.UncheckedPressed:
170 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedPressed);
175 #region Public Static Properties
176 public static bool RenderMatchingApplicationState {
177 get { return !always_use_visual_styles; }
178 set { always_use_visual_styles = !value; }