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