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