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