Merge pull request #1275 from ranma42/fix-lib64
[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 using System.Drawing;
30 using System.Windows.Forms.VisualStyles;
31
32 namespace System.Windows.Forms
33 {
34         public sealed class TrackBarRenderer
35         {
36                 #region Private Constructor
37                 private TrackBarRenderer () { }
38                 #endregion
39
40                 #region Public Static Methods
41                 public static void DrawBottomPointingThumb(Graphics g, Rectangle bounds, TrackBarThumbState state)
42                 {
43                         if (!IsSupported)
44                                 throw new InvalidOperationException ();
45
46                         VisualStyleRenderer vsr;
47
48                         switch (state) {
49                                 case TrackBarThumbState.Disabled:
50                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);
51                                         break;
52                                 case TrackBarThumbState.Hot:
53                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);
54                                         break;
55                                 case TrackBarThumbState.Normal:
56                                 default:
57                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);
58                                         break;
59                                 case TrackBarThumbState.Pressed:
60                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);
61                                         break;
62                         }
63
64                         vsr.DrawBackground (g, bounds);
65                 }
66
67                 public static void DrawHorizontalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
68                 {
69                         if (!IsSupported)
70                                 throw new InvalidOperationException ();
71
72                         VisualStyleRenderer vsr;
73
74                         switch (state) {
75                                 case TrackBarThumbState.Disabled:
76                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Disabled);
77                                         break;
78                                 case TrackBarThumbState.Hot:
79                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Hot);
80                                         break;
81                                 case TrackBarThumbState.Normal:
82                                 default:
83                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Normal);
84                                         break;
85                                 case TrackBarThumbState.Pressed:
86                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Pressed);
87                                         break;
88                         }
89
90                         vsr.DrawBackground (g, bounds);
91                 }
92
93                 public static void DrawHorizontalTicks(Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
94                 {
95                         if (!IsSupported)
96                                 throw new InvalidOperationException ();
97
98                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
99                                 return;
100                                 
101                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);
102                         
103                         double x = bounds.Left;
104                         double delta = (double)(bounds.Width - 2) / (double)(numTicks-1);
105                         
106                         for(int i = 0; i < numTicks; i++)
107                         {
108                                 vsr.DrawEdge(g, new Rectangle((int)Math.Round(x), bounds.Top, 5, bounds.Height), Edges.Left, edgeStyle, EdgeEffects.None);
109                                 x += delta;
110                         }
111                 }
112                 
113                 public static void DrawHorizontalTrack(Graphics g, Rectangle bounds)
114                 {
115                         if (!IsSupported)
116                                 throw new InvalidOperationException ();
117                                 
118                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
119                         
120                         vsr.DrawBackground (g, bounds);
121                 }
122
123                 public static void DrawLeftPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
124                 {
125                         if (!IsSupported)
126                                 throw new InvalidOperationException ();
127
128                         VisualStyleRenderer vsr;
129
130                         switch (state) {
131                                 case TrackBarThumbState.Disabled:
132                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);
133                                         break;
134                                 case TrackBarThumbState.Hot:
135                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);
136                                         break;
137                                 case TrackBarThumbState.Normal:
138                                 default:
139                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);
140                                         break;
141                                 case TrackBarThumbState.Pressed:
142                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);
143                                         break;
144                         }
145
146                         vsr.DrawBackground (g, bounds);
147                 }
148
149                 public static void DrawRightPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
150                 {
151                         if (!IsSupported)
152                                 throw new InvalidOperationException ();
153
154                         VisualStyleRenderer vsr;
155
156                         switch (state) {
157                                 case TrackBarThumbState.Disabled:
158                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);
159                                         break;
160                                 case TrackBarThumbState.Hot:
161                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);
162                                         break;
163                                 case TrackBarThumbState.Normal:
164                                 default:
165                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);
166                                         break;
167                                 case TrackBarThumbState.Pressed:
168                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);
169                                         break;
170                         }
171
172                         vsr.DrawBackground (g, bounds);
173                 }
174
175                 public static void DrawTopPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
176                 {
177                         if (!IsSupported)
178                                 throw new InvalidOperationException ();
179
180                         VisualStyleRenderer vsr;
181
182                         switch (state) {
183                                 case TrackBarThumbState.Disabled:
184                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);
185                                         break;
186                                 case TrackBarThumbState.Hot:
187                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);
188                                         break;
189                                 case TrackBarThumbState.Normal:
190                                 default:
191                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);
192                                         break;
193                                 case TrackBarThumbState.Pressed:
194                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);
195                                         break;
196                         }
197
198                         vsr.DrawBackground (g, bounds);
199                 }
200
201                 public static void DrawVerticalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)
202                 {
203                         if (!IsSupported)
204                                 throw new InvalidOperationException ();
205
206                         VisualStyleRenderer vsr;
207
208                         switch (state) {
209                                 case TrackBarThumbState.Disabled:
210                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Disabled);
211                                         break;
212                                 case TrackBarThumbState.Hot:
213                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Hot);
214                                         break;
215                                 case TrackBarThumbState.Normal:
216                                 default:
217                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Normal);
218                                         break;
219                                 case TrackBarThumbState.Pressed:
220                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Pressed);
221                                         break;
222                         }
223
224                         vsr.DrawBackground (g, bounds);
225                 }
226
227                 public static void DrawVerticalTicks (Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)
228                 {
229                         if (!IsSupported)
230                                 throw new InvalidOperationException ();
231
232                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)
233                                 return;
234
235                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);
236
237                         double y = bounds.Top;
238                         double delta = (double)(bounds.Height - 2) / (double)(numTicks - 1);
239
240                         for (int i = 0; i < numTicks; i++) {
241                                 vsr.DrawEdge (g, new Rectangle (bounds.Left, (int)Math.Round (y), bounds.Width, 5), Edges.Top, edgeStyle, EdgeEffects.None);
242                                 y += delta;
243                         }
244                 }
245
246                 public static void DrawVerticalTrack (Graphics g, Rectangle bounds)
247                 {
248                         if (!IsSupported)
249                                 throw new InvalidOperationException ();
250
251                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);
252
253                         vsr.DrawBackground (g, bounds);
254                 }
255
256                 public static Size GetBottomPointingThumbSize(Graphics g, TrackBarThumbState state)
257                 {
258                         if (!IsSupported)
259                                 throw new InvalidOperationException ();
260
261                         VisualStyleRenderer vsr;
262
263                         switch (state) {
264                                 case TrackBarThumbState.Disabled:
265                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);
266                                         break;
267                                 case TrackBarThumbState.Hot:
268                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);
269                                         break;
270                                 case TrackBarThumbState.Normal:
271                                 default:
272                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);
273                                         break;
274                                 case TrackBarThumbState.Pressed:
275                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);
276                                         break;
277                         }
278
279                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
280                 }
281
282                 public static Size GetLeftPointingThumbSize (Graphics g, TrackBarThumbState state)
283                 {
284                         if (!IsSupported)
285                                 throw new InvalidOperationException ();
286
287                         VisualStyleRenderer vsr;
288
289                         switch (state) {
290                                 case TrackBarThumbState.Disabled:
291                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);
292                                         break;
293                                 case TrackBarThumbState.Hot:
294                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);
295                                         break;
296                                 case TrackBarThumbState.Normal:
297                                 default:
298                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);
299                                         break;
300                                 case TrackBarThumbState.Pressed:
301                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);
302                                         break;
303                         }
304
305                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
306                 }
307
308                 public static Size GetRightPointingThumbSize (Graphics g, TrackBarThumbState state)
309                 {
310                         if (!IsSupported)
311                                 throw new InvalidOperationException ();
312
313                         VisualStyleRenderer vsr;
314
315                         switch (state) {
316                                 case TrackBarThumbState.Disabled:
317                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);
318                                         break;
319                                 case TrackBarThumbState.Hot:
320                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);
321                                         break;
322                                 case TrackBarThumbState.Normal:
323                                 default:
324                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);
325                                         break;
326                                 case TrackBarThumbState.Pressed:
327                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);
328                                         break;
329                         }
330
331                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
332                 }
333
334                 public static Size GetTopPointingThumbSize (Graphics g, TrackBarThumbState state)
335                 {
336                         if (!IsSupported)
337                                 throw new InvalidOperationException ();
338
339                         VisualStyleRenderer vsr;
340
341                         switch (state) {
342                                 case TrackBarThumbState.Disabled:
343                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);
344                                         break;
345                                 case TrackBarThumbState.Hot:
346                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);
347                                         break;
348                                 case TrackBarThumbState.Normal:
349                                 default:
350                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);
351                                         break;
352                                 case TrackBarThumbState.Pressed:
353                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);
354                                         break;
355                         }
356
357                         return vsr.GetPartSize (g, ThemeSizeType.Draw);
358                 }
359                 #endregion
360
361                 #region Public Static Properties
362                 public static bool IsSupported {
363                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
364                 }
365                 #endregion
366         }
367 }