2009-02-21 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Managed.Windows.Forms / System.Windows.Forms / TrackBarRenderer.cs
blobf6f5f39130ca766c5b4f55ca6455140fccb3fff6
1 //
2 // TrackBarRenderer.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.
23 // Copyright (c) 2006 Novell, Inc.
25 // Authors:
26 // Jonathan Pobst (monkey@jpobst.com)
29 #if NET_2_0
30 using System.Drawing;
31 using System.Windows.Forms.VisualStyles;
33 namespace System.Windows.Forms
35 public sealed class TrackBarRenderer
37 #region Private Constructor
38 private TrackBarRenderer () { }
39 #endregion
41 #region Public Static Methods
42 public static void DrawBottomPointingThumb(Graphics g, Rectangle bounds, TrackBarThumbState state)
44 if (!IsSupported)
45 throw new InvalidOperationException ();
47 VisualStyleRenderer vsr;
49 switch (state) {
50 case TrackBarThumbState.Disabled:
51 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);
52 break;
53 case TrackBarThumbState.Hot:
54 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);
55 break;
56 case TrackBarThumbState.Normal:
57 default:
58 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);
59 break;
60 case TrackBarThumbState.Pressed:
61 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);
62 break;
65 vsr.DrawBackground (g, bounds);
68 public static void DrawHorizontalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
70 if (!IsSupported)
71 throw new InvalidOperationException ();
73 VisualStyleRenderer vsr;
75 switch (state) {
76 case TrackBarThumbState.Disabled:
77 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Disabled);
78 break;
79 case TrackBarThumbState.Hot:
80 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Hot);
81 break;
82 case TrackBarThumbState.Normal:
83 default:
84 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Normal);
85 break;
86 case TrackBarThumbState.Pressed:
87 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Pressed);
88 break;
91 vsr.DrawBackground (g, bounds);
94 public static void DrawHorizontalTicks(Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
96 if (!IsSupported)
97 throw new InvalidOperationException ();
99 if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
100 return;
102 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);
104 double x = bounds.Left;
105 double delta = (double)(bounds.Width - 2) / (double)(numTicks-1);
107 for(int i = 0; i < numTicks; i++)
109 vsr.DrawEdge(g, new Rectangle((int)Math.Round(x), bounds.Top, 5, bounds.Height), Edges.Left, edgeStyle, EdgeEffects.None);
110 x += delta;
114 public static void DrawHorizontalTrack(Graphics g, Rectangle bounds)
116 if (!IsSupported)
117 throw new InvalidOperationException ();
119 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
121 vsr.DrawBackground (g, bounds);
124 public static void DrawLeftPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
126 if (!IsSupported)
127 throw new InvalidOperationException ();
129 VisualStyleRenderer vsr;
131 switch (state) {
132 case TrackBarThumbState.Disabled:
133 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);
134 break;
135 case TrackBarThumbState.Hot:
136 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);
137 break;
138 case TrackBarThumbState.Normal:
139 default:
140 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);
141 break;
142 case TrackBarThumbState.Pressed:
143 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);
144 break;
147 vsr.DrawBackground (g, bounds);
150 public static void DrawRightPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
152 if (!IsSupported)
153 throw new InvalidOperationException ();
155 VisualStyleRenderer vsr;
157 switch (state) {
158 case TrackBarThumbState.Disabled:
159 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);
160 break;
161 case TrackBarThumbState.Hot:
162 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);
163 break;
164 case TrackBarThumbState.Normal:
165 default:
166 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);
167 break;
168 case TrackBarThumbState.Pressed:
169 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);
170 break;
173 vsr.DrawBackground (g, bounds);
176 public static void DrawTopPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
178 if (!IsSupported)
179 throw new InvalidOperationException ();
181 VisualStyleRenderer vsr;
183 switch (state) {
184 case TrackBarThumbState.Disabled:
185 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);
186 break;
187 case TrackBarThumbState.Hot:
188 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);
189 break;
190 case TrackBarThumbState.Normal:
191 default:
192 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);
193 break;
194 case TrackBarThumbState.Pressed:
195 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);
196 break;
199 vsr.DrawBackground (g, bounds);
202 public static void DrawVerticalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
204 if (!IsSupported)
205 throw new InvalidOperationException ();
207 VisualStyleRenderer vsr;
209 switch (state) {
210 case TrackBarThumbState.Disabled:
211 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Disabled);
212 break;
213 case TrackBarThumbState.Hot:
214 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Hot);
215 break;
216 case TrackBarThumbState.Normal:
217 default:
218 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Normal);
219 break;
220 case TrackBarThumbState.Pressed:
221 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Pressed);
222 break;
225 vsr.DrawBackground (g, bounds);
228 public static void DrawVerticalTicks (Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
230 if (!IsSupported)
231 throw new InvalidOperationException ();
233 if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
234 return;
236 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);
238 double y = bounds.Top;
239 double delta = (double)(bounds.Height - 2) / (double)(numTicks - 1);
241 for (int i = 0; i < numTicks; i++) {
242 vsr.DrawEdge (g, new Rectangle (bounds.Left, (int)Math.Round (y), bounds.Width, 5), Edges.Top, edgeStyle, EdgeEffects.None);
243 y += delta;
247 public static void DrawVerticalTrack (Graphics g, Rectangle bounds)
249 if (!IsSupported)
250 throw new InvalidOperationException ();
252 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
254 vsr.DrawBackground (g, bounds);
257 public static Size GetBottomPointingThumbSize(Graphics g, TrackBarThumbState state)
259 if (!IsSupported)
260 throw new InvalidOperationException ();
262 VisualStyleRenderer vsr;
264 switch (state) {
265 case TrackBarThumbState.Disabled:
266 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);
267 break;
268 case TrackBarThumbState.Hot:
269 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);
270 break;
271 case TrackBarThumbState.Normal:
272 default:
273 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);
274 break;
275 case TrackBarThumbState.Pressed:
276 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);
277 break;
280 return vsr.GetPartSize (g, ThemeSizeType.Draw);
283 public static Size GetLeftPointingThumbSize (Graphics g, TrackBarThumbState state)
285 if (!IsSupported)
286 throw new InvalidOperationException ();
288 VisualStyleRenderer vsr;
290 switch (state) {
291 case TrackBarThumbState.Disabled:
292 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);
293 break;
294 case TrackBarThumbState.Hot:
295 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);
296 break;
297 case TrackBarThumbState.Normal:
298 default:
299 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);
300 break;
301 case TrackBarThumbState.Pressed:
302 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);
303 break;
306 return vsr.GetPartSize (g, ThemeSizeType.Draw);
309 public static Size GetRightPointingThumbSize (Graphics g, TrackBarThumbState state)
311 if (!IsSupported)
312 throw new InvalidOperationException ();
314 VisualStyleRenderer vsr;
316 switch (state) {
317 case TrackBarThumbState.Disabled:
318 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);
319 break;
320 case TrackBarThumbState.Hot:
321 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);
322 break;
323 case TrackBarThumbState.Normal:
324 default:
325 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);
326 break;
327 case TrackBarThumbState.Pressed:
328 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);
329 break;
332 return vsr.GetPartSize (g, ThemeSizeType.Draw);
335 public static Size GetTopPointingThumbSize (Graphics g, TrackBarThumbState state)
337 if (!IsSupported)
338 throw new InvalidOperationException ();
340 VisualStyleRenderer vsr;
342 switch (state) {
343 case TrackBarThumbState.Disabled:
344 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);
345 break;
346 case TrackBarThumbState.Hot:
347 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);
348 break;
349 case TrackBarThumbState.Normal:
350 default:
351 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);
352 break;
353 case TrackBarThumbState.Pressed:
354 vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);
355 break;
358 return vsr.GetPartSize (g, ThemeSizeType.Draw);
360 #endregion
362 #region Public Static Properties
363 public static bool IsSupported {
364 get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
366 #endregion
369 #endif