2 // RadioButtonRenderer.cs
\r
4 // Permission is hereby granted, free of charge, to any person obtaining
\r
5 // a copy of this software and associated documentation files (the
\r
6 // "Software"), to deal in the Software without restriction, including
\r
7 // without limitation the rights to use, copy, modify, merge, publish,
\r
8 // distribute, sublicense, and/or sell copies of the Software, and to
\r
9 // permit persons to whom the Software is furnished to do so, subject to
\r
10 // the following conditions:
\r
12 // The above copyright notice and this permission notice shall be
\r
13 // included in all copies or substantial portions of the Software.
\r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 // Copyright (c) 2006 Novell, Inc.
\r
26 // Jonathan Pobst (monkey@jpobst.com)
\r
30 using System.Drawing;
\r
31 using System.Windows.Forms.VisualStyles;
\r
33 namespace System.Windows.Forms
\r
35 public sealed class RadioButtonRenderer
\r
37 private static bool always_use_visual_styles = false;
\r
39 #region Private Constructor
\r
40 private RadioButtonRenderer () { }
\r
43 #region Public Static Methods
\r
44 public static void DrawRadioButton (Graphics g, Point glyphLocation, RadioButtonState state)
\r
46 DrawRadioButton (g, glyphLocation, Rectangle.Empty, String.Empty, null, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
\r
49 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, bool focused, RadioButtonState state)
\r
51 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
\r
54 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, TextFormatFlags flags, bool focused, RadioButtonState state)
\r
56 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, flags, null, Rectangle.Empty, focused, state);
\r
59 public static void DrawRadioButton (Graphics g, Point glyphLocation, Rectangle textBounds, string radioButtonText, Font font, Image image, Rectangle imageBounds, bool focused, RadioButtonState state)
\r
61 DrawRadioButton (g, glyphLocation, textBounds, radioButtonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
\r
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)
\r
66 Rectangle bounds = new Rectangle (glyphLocation, GetGlyphSize (g, state));
\r
68 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
\r
69 VisualStyleRenderer vsr = GetRadioButtonRenderer (state);
\r
71 vsr.DrawBackground (g, bounds);
\r
74 vsr.DrawImage (g, imageBounds, image);
\r
77 ControlPaint.DrawFocusRectangle (g, textBounds);
\r
79 if (radioButtonText != String.Empty)
\r
80 if (state == RadioButtonState.CheckedDisabled || state == RadioButtonState.UncheckedDisabled)
\r
81 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.GrayText, flags);
\r
83 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.ControlText, flags);
\r
87 case RadioButtonState.CheckedDisabled:
\r
88 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Inactive | ButtonState.Checked);
\r
90 case RadioButtonState.CheckedHot:
\r
91 case RadioButtonState.CheckedNormal:
\r
92 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Checked);
\r
94 case RadioButtonState.CheckedPressed:
\r
95 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Pushed | ButtonState.Checked);
\r
97 case RadioButtonState.UncheckedDisabled:
\r
98 case RadioButtonState.UncheckedPressed:
\r
99 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Inactive);
\r
101 case RadioButtonState.UncheckedHot:
\r
102 case RadioButtonState.UncheckedNormal:
\r
103 ControlPaint.DrawRadioButton (g, bounds, ButtonState.Normal);
\r
108 g.DrawImage (image, imageBounds);
\r
111 ControlPaint.DrawFocusRectangle (g, textBounds);
\r
113 if (radioButtonText != String.Empty)
\r
114 TextRenderer.DrawText (g, radioButtonText, font, textBounds, SystemColors.ControlText, flags);
\r
119 public static bool IsBackgroundPartiallyTransparent (RadioButtonState state)
\r
121 if (!VisualStyleRenderer.IsSupported)
\r
124 VisualStyleRenderer vsr = GetRadioButtonRenderer (state);
\r
126 return vsr.IsBackgroundPartiallyTransparent ();
\r
129 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
\r
131 if (!VisualStyleRenderer.IsSupported)
\r
134 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedNormal);
\r
136 vsr.DrawParentBackground (g, bounds, childControl);
\r
139 public static Size GetGlyphSize (Graphics g, RadioButtonState state)
\r
141 if (!VisualStyleRenderer.IsSupported)
\r
142 return new Size (13, 13);
\r
144 VisualStyleRenderer vsr = GetRadioButtonRenderer(state);
\r
146 return vsr.GetPartSize (g, ThemeSizeType.Draw);
\r
150 #region Private Static Methods
\r
151 private static VisualStyleRenderer GetRadioButtonRenderer (RadioButtonState state)
\r
154 case RadioButtonState.CheckedDisabled:
\r
155 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedDisabled);
\r
156 case RadioButtonState.CheckedHot:
\r
157 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedHot);
\r
158 case RadioButtonState.CheckedNormal:
\r
159 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedNormal);
\r
160 case RadioButtonState.CheckedPressed:
\r
161 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.CheckedPressed);
\r
162 case RadioButtonState.UncheckedDisabled:
\r
163 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedDisabled);
\r
164 case RadioButtonState.UncheckedHot:
\r
165 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedHot);
\r
166 case RadioButtonState.UncheckedNormal:
\r
168 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedNormal);
\r
169 case RadioButtonState.UncheckedPressed:
\r
170 return new VisualStyleRenderer (VisualStyleElement.Button.RadioButton.UncheckedPressed);
\r
175 #region Public Static Properties
\r
176 public static bool RenderMatchingApplicationState {
\r
177 get { return !always_use_visual_styles; }
\r
178 set { always_use_visual_styles = !value; }
\r