* TreeNode.cs: Add the 2.0 collapse method.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CheckBoxRenderer.cs
1 //
2 // CheckBoxRenderer.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 CheckBoxRenderer
36         {
37                 private static bool always_use_visual_styles = false;
38
39                 #region Private Constructor
40                 private CheckBoxRenderer () {}
41                 #endregion
42                 
43                 #region Public Static Methods
44                 public static void DrawCheckBox (Graphics g, Point glyphLocation, CheckBoxState state)
45                 {
46                         DrawCheckBox (g, glyphLocation, Rectangle.Empty, String.Empty, null, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
47                 }
48
49                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, bool focused, CheckBoxState state)
50                 {
51                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
52                 }
53
54                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, bool focused, CheckBoxState state)
55                 {
56                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, flags, null, Rectangle.Empty, focused, state);
57                 }
58
59                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
60                 {
61                         DrawCheckBox (g, glyphLocation, textBounds, checkBoxText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
62                 }
63
64                 public static void DrawCheckBox (Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, CheckBoxState state)
65                 {
66                         Rectangle bounds = new Rectangle (glyphLocation, GetGlyphSize (g, state));
67
68                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
69                                 VisualStyleRenderer vsr = GetCheckBoxRenderer (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 (checkBoxText != String.Empty)
80                                         if (state == CheckBoxState.CheckedDisabled || state == CheckBoxState.MixedDisabled || state == CheckBoxState.UncheckedDisabled)
81                                                 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.GrayText, flags);
82                                         else
83                                                 TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
84                         } else {
85                                 switch (state) {
86                                         case CheckBoxState.CheckedDisabled:
87                                         case CheckBoxState.MixedDisabled:
88                                         case CheckBoxState.MixedPressed:
89                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive | ButtonState.Checked);
90                                                 break;
91                                         case CheckBoxState.CheckedHot:
92                                         case CheckBoxState.CheckedNormal:
93                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Checked);
94                                                 break;
95                                         case CheckBoxState.CheckedPressed:
96                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Pushed | ButtonState.Checked);
97                                                 break;
98                                         case CheckBoxState.MixedHot:
99                                         case CheckBoxState.MixedNormal:
100                                                 ControlPaint.DrawMixedCheckBox (g, bounds, ButtonState.Checked);
101                                                 break;
102                                         case CheckBoxState.UncheckedDisabled:
103                                         case CheckBoxState.UncheckedPressed:
104                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Inactive);
105                                                 break;
106                                         case CheckBoxState.UncheckedHot:
107                                         case CheckBoxState.UncheckedNormal:
108                                                 ControlPaint.DrawCheckBox (g, bounds, ButtonState.Normal);
109                                                 break;
110                                 }
111
112                                 if (image != null)
113                                         g.DrawImage (image, imageBounds);
114
115                                 if (focused)
116                                         ControlPaint.DrawFocusRectangle (g, textBounds);
117
118                                 if (checkBoxText != String.Empty)
119                                         TextRenderer.DrawText (g, checkBoxText, font, textBounds, SystemColors.ControlText, flags);
120                         }
121                 }
122
123                 public static bool IsBackgroundPartiallyTransparent (CheckBoxState state)
124                 {
125                         if (!VisualStyleRenderer.IsSupported)
126                                 return false;
127
128                         VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
129
130                         return vsr.IsBackgroundPartiallyTransparent ();
131                 }
132
133                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
134                 {
135                         if (!VisualStyleRenderer.IsSupported)
136                                 return;
137                                 
138                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
139
140                         vsr.DrawParentBackground (g, bounds, childControl);
141                 }
142
143                 public static Size GetGlyphSize (Graphics g, CheckBoxState state)
144                 {
145                         if (!VisualStyleRenderer.IsSupported)
146                                 return new Size (13, 13);
147
148                         VisualStyleRenderer vsr = GetCheckBoxRenderer (state);
149
150                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
151                 }
152                 #endregion
153
154                 #region Private Static Methods
155                 private static VisualStyleRenderer GetCheckBoxRenderer (CheckBoxState state)
156                 {
157                         switch (state) {
158                                 case CheckBoxState.CheckedDisabled:
159                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedDisabled);
160                                 case CheckBoxState.CheckedHot:
161                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedHot);
162                                 case CheckBoxState.CheckedNormal:
163                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedNormal);
164                                 case CheckBoxState.CheckedPressed:
165                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.CheckedPressed);
166                                 case CheckBoxState.MixedDisabled:
167                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedDisabled);
168                                 case CheckBoxState.MixedHot:
169                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedHot);
170                                 case CheckBoxState.MixedNormal:
171                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedNormal);
172                                 case CheckBoxState.MixedPressed:
173                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.MixedPressed);
174                                 case CheckBoxState.UncheckedDisabled:
175                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedDisabled);
176                                 case CheckBoxState.UncheckedHot:
177                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedHot);
178                                 case CheckBoxState.UncheckedNormal:
179                                 default:
180                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedNormal);
181                                 case CheckBoxState.UncheckedPressed:
182                                         return new VisualStyleRenderer (VisualStyleElement.Button.CheckBox.UncheckedPressed);
183                         }
184                 }
185                 #endregion
186
187                 #region Public Static Properties
188                 public static bool RenderMatchingApplicationState {
189                         get { return !always_use_visual_styles; }
190                         set { always_use_visual_styles = !value; }
191                 }
192                 #endregion
193         }
194 }
195 #endif