1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc.
21 //
22 // Authors:
23 //	Jonathan Pobst (monkey@jpobst.com)
24 
25 using System;
26 using System.Drawing;
27 using System.Drawing.Drawing2D;
28 
29 namespace System.Windows.Forms.Theming.Default
30 {
31 	/// <summary>
32 	/// Summary description for Button.
33 	/// </summary>
34 	internal class RadioButtonPainter
35 	{
RadioButtonPainter()36 		public RadioButtonPainter ()
37 		{
38 		}
39 
40 		protected SystemResPool ResPool { get { return ThemeEngine.Current.ResPool; } }
41 
PaintRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, ElementState state, FlatStyle style, bool isChecked)42 		public void PaintRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, ElementState state, FlatStyle style, bool isChecked)
43 		{
44 			switch (style) {
45 				case FlatStyle.Standard:
46 				case FlatStyle.System:
47 					switch (state) {
48 						case ElementState.Normal:
49 							DrawNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
50 							break;
51 						case ElementState.Hot:
52 							DrawHotRadioButton (g, bounds, backColor, foreColor, isChecked);
53 							break;
54 						case ElementState.Pressed:
55 							DrawPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
56 							break;
57 						case ElementState.Disabled:
58 							DrawDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
59 							break;
60 					}
61 					break;
62 				case FlatStyle.Flat:
63 					switch (state) {
64 						case ElementState.Normal:
65 							DrawFlatNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
66 							break;
67 						case ElementState.Hot:
68 							DrawFlatHotRadioButton (g, bounds, backColor, foreColor, isChecked);
69 							break;
70 						case ElementState.Pressed:
71 							DrawFlatPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
72 							break;
73 						case ElementState.Disabled:
74 							DrawFlatDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
75 							break;
76 					}
77 					break;
78 				case FlatStyle.Popup:
79 					switch (state) {
80 						case ElementState.Normal:
81 							DrawPopupNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
82 							break;
83 						case ElementState.Hot:
84 							DrawPopupHotRadioButton (g, bounds, backColor, foreColor, isChecked);
85 							break;
86 						case ElementState.Pressed:
87 							DrawPopupPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
88 							break;
89 						case ElementState.Disabled:
90 							DrawPopupDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
91 							break;
92 					}
93 					break;
94 			}
95 		}
96 
97 		#region Standard
DrawNormalRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)98 		public virtual void DrawNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
99 		{
100 			ButtonState bs = ButtonState.Normal;
101 
102 			if (isChecked)
103 				bs |= ButtonState.Checked;
104 
105 			ControlPaint.DrawRadioButton (g, bounds, bs);
106 		}
107 
DrawHotRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)108 		public virtual void DrawHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
109 		{
110 			DrawNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
111 		}
112 
DrawPressedRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)113 		public virtual void DrawPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
114 		{
115 			ButtonState bs = ButtonState.Pushed;
116 
117 			if (isChecked)
118 				bs |= ButtonState.Checked;
119 
120 			ControlPaint.DrawRadioButton (g, bounds, bs);
121 		}
122 
DrawDisabledRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)123 		public virtual void DrawDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
124 		{
125 			ButtonState bs = ButtonState.Inactive;
126 
127 			if (isChecked)
128 				bs |= ButtonState.Checked;
129 
130 			ControlPaint.DrawRadioButton (g, bounds, bs);
131 		}
132 		#endregion
133 
134 		#region FlatStyle
DrawFlatNormalRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)135 		public virtual void DrawFlatNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
136 		{
137 			g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
138 			g.FillPie (SystemBrushes.ControlLightLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
139 
140 			if (isChecked)
141 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
142 		}
143 
DrawFlatHotRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)144 		public virtual void DrawFlatHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
145 		{
146 			g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
147 			g.FillPie (SystemBrushes.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
148 
149 			if (isChecked)
150 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
151 		}
152 
DrawFlatPressedRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)153 		public virtual void DrawFlatPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
154 		{
155 			g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
156 			g.FillPie (SystemBrushes.ControlLightLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
157 
158 			if (isChecked)
159 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
160 		}
161 
DrawFlatDisabledRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)162 		public virtual void DrawFlatDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
163 		{
164 			g.FillPie (SystemBrushes.Control, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
165 			g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
166 
167 			if (isChecked)
168 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDark);
169 		}
170 		#endregion
171 
172 		#region Popup
DrawPopupNormalRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)173 		public virtual void DrawPopupNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
174 		{
175 			g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
176 			g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
177 
178 			if (isChecked)
179 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
180 		}
181 
DrawPopupHotRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)182 		public virtual void DrawPopupHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
183 		{
184 			g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
185 			g.DrawArc (SystemPens.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
186 
187 			g.DrawArc (SystemPens.ControlDark, bounds, 135, 180);
188 			g.DrawArc (SystemPens.ControlLightLight, bounds, 315, 180);
189 
190 			if (isChecked)
191 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
192 		}
193 
DrawPopupPressedRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)194 		public virtual void DrawPopupPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
195 		{
196 			g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
197 			g.DrawArc (SystemPens.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
198 
199 			g.DrawArc (SystemPens.ControlDark, bounds, 135, 180);
200 			g.DrawArc (SystemPens.ControlLightLight, bounds, 315, 180);
201 
202 			if (isChecked)
203 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
204 		}
205 
DrawPopupDisabledRadioButton(Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)206 		public virtual void DrawPopupDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
207 		{
208 			g.FillPie (SystemBrushes.Control, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
209 			g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
210 
211 			if (isChecked)
212 				DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
213 		}
214 		#endregion
215 
216 		#region Glyph
DrawFlatRadioGlyphDot(Graphics g, Rectangle bounds, Color dotColor)217 		protected void DrawFlatRadioGlyphDot (Graphics g, Rectangle bounds, Color dotColor)
218 		{
219 			int lineWidth = Math.Max (1, Math.Min (bounds.Width, bounds.Height) / 3);
220 
221 			Pen dot_pen = ResPool.GetPen (dotColor);
222 			Brush dot_brush = ResPool.GetSolidBrush (dotColor);
223 
224 			if (bounds.Height > 13) {
225 				g.FillPie (dot_brush, bounds.X + lineWidth, bounds.Y + lineWidth, bounds.Width - lineWidth * 2, bounds.Height - lineWidth * 2, 0, 359);
226 			} else {
227 				int x_half_pos = (bounds.Width / 2) + bounds.X;
228 				int y_half_pos = (bounds.Height / 2) + bounds.Y;
229 
230 				g.DrawLine (dot_pen, x_half_pos - 1, y_half_pos, x_half_pos + 2, y_half_pos);
231 				g.DrawLine (dot_pen, x_half_pos - 1, y_half_pos + 1, x_half_pos + 2, y_half_pos + 1);
232 
233 				g.DrawLine (dot_pen, x_half_pos, y_half_pos - 1, x_half_pos, y_half_pos + 2);
234 				g.DrawLine (dot_pen, x_half_pos + 1, y_half_pos - 1, x_half_pos + 1, y_half_pos + 2);
235 			}
236 		}
237 		#endregion
238 	}
239 }
240