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