New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TrackBarRenderer.cs
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.
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 TrackBarRenderer
36         {
37                 #region Private Constructor
38                 private TrackBarRenderer () { }
39                 #endregion
40
41                 #region Public Static Methods
42                 public static void DrawBottomPointingThumb(Graphics g, Rectangle bounds, TrackBarThumbState state)
43                 {
44                         if (!IsSupported)
45                                 throw new InvalidOperationException ();
46
47                         VisualStyleRenderer vsr;
48
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;
63                         }
64
65                         vsr.DrawBackground (g, bounds);
66                 }
67
68                 public static void DrawHorizontalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
69                 {
70                         if (!IsSupported)
71                                 throw new InvalidOperationException ();
72
73                         VisualStyleRenderer vsr;
74
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;
89                         }
90
91                         vsr.DrawBackground (g, bounds);
92                 }
93
94                 public static void DrawHorizontalTicks(Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
95                 {
96                         if (!IsSupported)
97                                 throw new InvalidOperationException ();
98
99                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
100                                 return;
101                                 
102                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);
103                         
104                         double x = bounds.Left;
105                         double delta = (double)(bounds.Width - 2) / (double)(numTicks-1);
106                         
107                         for(int i = 0; i < numTicks; i++)
108                         {
109                                 vsr.DrawEdge(g, new Rectangle((int)Math.Round(x), bounds.Top, 5, bounds.Height), Edges.Left, edgeStyle, EdgeEffects.None);
110                                 x += delta;
111                         }
112                 }
113                 
114                 public static void DrawHorizontalTrack(Graphics g, Rectangle bounds)
115                 {
116                         if (!IsSupported)
117                                 throw new InvalidOperationException ();
118                                 
119                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
120                         
121                         vsr.DrawBackground (g, bounds);
122                 }
123
124                 public static void DrawLeftPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
125                 {
126                         if (!IsSupported)
127                                 throw new InvalidOperationException ();
128
129                         VisualStyleRenderer vsr;
130
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;
145                         }
146
147                         vsr.DrawBackground (g, bounds);
148                 }
149
150                 public static void DrawRightPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
151                 {
152                         if (!IsSupported)
153                                 throw new InvalidOperationException ();
154
155                         VisualStyleRenderer vsr;
156
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;
171                         }
172
173                         vsr.DrawBackground (g, bounds);
174                 }
175
176                 public static void DrawTopPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
177                 {
178                         if (!IsSupported)
179                                 throw new InvalidOperationException ();
180
181                         VisualStyleRenderer vsr;
182
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;
197                         }
198
199                         vsr.DrawBackground (g, bounds);
200                 }
201
202                 public static void DrawVerticalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
203                 {
204                         if (!IsSupported)
205                                 throw new InvalidOperationException ();
206
207                         VisualStyleRenderer vsr;
208
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;
223                         }
224
225                         vsr.DrawBackground (g, bounds);
226                 }
227
228                 public static void DrawVerticalTicks (Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
229                 {
230                         if (!IsSupported)
231                                 throw new InvalidOperationException ();
232
233                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
234                                 return;
235
236                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);
237
238                         double y = bounds.Top;
239                         double delta = (double)(bounds.Height - 2) / (double)(numTicks - 1);
240
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;
244                         }
245                 }
246
247                 public static void DrawVerticalTrack (Graphics g, Rectangle bounds)
248                 {
249                         if (!IsSupported)
250                                 throw new InvalidOperationException ();
251
252                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
253
254                         vsr.DrawBackground (g, bounds);
255                 }
256
257                 public static Size GetBottomPointingThumbSize(Graphics g, TrackBarThumbState state)
258                 {
259                         if (!IsSupported)
260                                 throw new InvalidOperationException ();
261
262                         VisualStyleRenderer vsr;
263
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;
278                         }
279
280                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
281                 }
282
283                 public static Size GetLeftPointingThumbSize (Graphics g, TrackBarThumbState state)
284                 {
285                         if (!IsSupported)
286                                 throw new InvalidOperationException ();
287
288                         VisualStyleRenderer vsr;
289
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;
304                         }
305
306                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
307                 }
308
309                 public static Size GetRightPointingThumbSize (Graphics g, TrackBarThumbState state)
310                 {
311                         if (!IsSupported)
312                                 throw new InvalidOperationException ();
313
314                         VisualStyleRenderer vsr;
315
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;
330                         }
331
332                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
333                 }
334
335                 public static Size GetTopPointingThumbSize (Graphics g, TrackBarThumbState state)
336                 {
337                         if (!IsSupported)
338                                 throw new InvalidOperationException ();
339
340                         VisualStyleRenderer vsr;
341
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;
356                         }
357
358                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
359                 }
360                 #endregion
361
362                 #region Public Static Properties
363                 public static bool IsSupported {
364                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
365                 }
366                 #endregion
367         }
368 }
369 #endif