New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TrackBarRenderer.cs
1 //\r
2 // TrackBarRenderer.cs\r
3 //\r
4 // Permission is hereby granted, free of charge, to any person obtaining\r
5 // a copy of this software and associated documentation files (the\r
6 // "Software"), to deal in the Software without restriction, including\r
7 // without limitation the rights to use, copy, modify, merge, publish,\r
8 // distribute, sublicense, and/or sell copies of the Software, and to\r
9 // permit persons to whom the Software is furnished to do so, subject to\r
10 // the following conditions:\r
11 // \r
12 // The above copyright notice and this permission notice shall be\r
13 // included in all copies or substantial portions of the Software.\r
14 // \r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22 //\r
23 // Copyright (c) 2006 Novell, Inc.\r
24 //\r
25 // Authors:\r
26 //      Jonathan Pobst (monkey@jpobst.com)\r
27 //\r
28 \r
29 #if NET_2_0\r
30 using System.Drawing;\r
31 using System.Windows.Forms.VisualStyles;\r
32 \r
33 namespace System.Windows.Forms\r
34 {\r
35         public sealed class TrackBarRenderer\r
36         {\r
37                 #region Private Constructor\r
38                 private TrackBarRenderer () { }\r
39                 #endregion\r
40 \r
41                 #region Public Static Methods\r
42                 public static void DrawBottomPointingThumb(Graphics g, Rectangle bounds, TrackBarThumbState state)\r
43                 {\r
44                         if (!IsSupported)\r
45                                 throw new InvalidOperationException ();\r
46 \r
47                         VisualStyleRenderer vsr;\r
48 \r
49                         switch (state) {\r
50                                 case TrackBarThumbState.Disabled:\r
51                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);\r
52                                         break;\r
53                                 case TrackBarThumbState.Hot:\r
54                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);\r
55                                         break;\r
56                                 case TrackBarThumbState.Normal:\r
57                                 default:\r
58                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);\r
59                                         break;\r
60                                 case TrackBarThumbState.Pressed:\r
61                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);\r
62                                         break;\r
63                         }\r
64 \r
65                         vsr.DrawBackground (g, bounds);\r
66                 }\r
67 \r
68                 public static void DrawHorizontalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)\r
69                 {\r
70                         if (!IsSupported)\r
71                                 throw new InvalidOperationException ();\r
72 \r
73                         VisualStyleRenderer vsr;\r
74 \r
75                         switch (state) {\r
76                                 case TrackBarThumbState.Disabled:\r
77                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Disabled);\r
78                                         break;\r
79                                 case TrackBarThumbState.Hot:\r
80                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Hot);\r
81                                         break;\r
82                                 case TrackBarThumbState.Normal:\r
83                                 default:\r
84                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Normal);\r
85                                         break;\r
86                                 case TrackBarThumbState.Pressed:\r
87                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Thumb.Pressed);\r
88                                         break;\r
89                         }\r
90 \r
91                         vsr.DrawBackground (g, bounds);\r
92                 }\r
93 \r
94                 public static void DrawHorizontalTicks(Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)\r
95                 {\r
96                         if (!IsSupported)\r
97                                 throw new InvalidOperationException ();\r
98 \r
99                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)\r
100                                 return;\r
101                                 \r
102                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);\r
103                         \r
104                         double x = bounds.Left;\r
105                         double delta = (double)(bounds.Width - 2) / (double)(numTicks-1);\r
106                         \r
107                         for(int i = 0; i < numTicks; i++)\r
108                         {\r
109                                 vsr.DrawEdge(g, new Rectangle((int)Math.Round(x), bounds.Top, 5, bounds.Height), Edges.Left, edgeStyle, EdgeEffects.None);\r
110                                 x += delta;\r
111                         }\r
112                 }\r
113                 \r
114                 public static void DrawHorizontalTrack(Graphics g, Rectangle bounds)\r
115                 {\r
116                         if (!IsSupported)\r
117                                 throw new InvalidOperationException ();\r
118                                 \r
119                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);\r
120                         \r
121                         vsr.DrawBackground (g, bounds);\r
122                 }\r
123 \r
124                 public static void DrawLeftPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)\r
125                 {\r
126                         if (!IsSupported)\r
127                                 throw new InvalidOperationException ();\r
128 \r
129                         VisualStyleRenderer vsr;\r
130 \r
131                         switch (state) {\r
132                                 case TrackBarThumbState.Disabled:\r
133                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);\r
134                                         break;\r
135                                 case TrackBarThumbState.Hot:\r
136                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);\r
137                                         break;\r
138                                 case TrackBarThumbState.Normal:\r
139                                 default:\r
140                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);\r
141                                         break;\r
142                                 case TrackBarThumbState.Pressed:\r
143                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);\r
144                                         break;\r
145                         }\r
146 \r
147                         vsr.DrawBackground (g, bounds);\r
148                 }\r
149 \r
150                 public static void DrawRightPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)\r
151                 {\r
152                         if (!IsSupported)\r
153                                 throw new InvalidOperationException ();\r
154 \r
155                         VisualStyleRenderer vsr;\r
156 \r
157                         switch (state) {\r
158                                 case TrackBarThumbState.Disabled:\r
159                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);\r
160                                         break;\r
161                                 case TrackBarThumbState.Hot:\r
162                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);\r
163                                         break;\r
164                                 case TrackBarThumbState.Normal:\r
165                                 default:\r
166                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);\r
167                                         break;\r
168                                 case TrackBarThumbState.Pressed:\r
169                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);\r
170                                         break;\r
171                         }\r
172 \r
173                         vsr.DrawBackground (g, bounds);\r
174                 }\r
175 \r
176                 public static void DrawTopPointingThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)\r
177                 {\r
178                         if (!IsSupported)\r
179                                 throw new InvalidOperationException ();\r
180 \r
181                         VisualStyleRenderer vsr;\r
182 \r
183                         switch (state) {\r
184                                 case TrackBarThumbState.Disabled:\r
185                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);\r
186                                         break;\r
187                                 case TrackBarThumbState.Hot:\r
188                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);\r
189                                         break;\r
190                                 case TrackBarThumbState.Normal:\r
191                                 default:\r
192                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);\r
193                                         break;\r
194                                 case TrackBarThumbState.Pressed:\r
195                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);\r
196                                         break;\r
197                         }\r
198 \r
199                         vsr.DrawBackground (g, bounds);\r
200                 }\r
201 \r
202                 public static void DrawVerticalThumb (Graphics g, Rectangle bounds, TrackBarThumbState state)\r
203                 {\r
204                         if (!IsSupported)\r
205                                 throw new InvalidOperationException ();\r
206 \r
207                         VisualStyleRenderer vsr;\r
208 \r
209                         switch (state) {\r
210                                 case TrackBarThumbState.Disabled:\r
211                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Disabled);\r
212                                         break;\r
213                                 case TrackBarThumbState.Hot:\r
214                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Hot);\r
215                                         break;\r
216                                 case TrackBarThumbState.Normal:\r
217                                 default:\r
218                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Normal);\r
219                                         break;\r
220                                 case TrackBarThumbState.Pressed:\r
221                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbVertical.Pressed);\r
222                                         break;\r
223                         }\r
224 \r
225                         vsr.DrawBackground (g, bounds);\r
226                 }\r
227 \r
228                 public static void DrawVerticalTicks (Graphics g, Rectangle bounds, int numTicks, EdgeStyle edgeStyle)\r
229                 {\r
230                         if (!IsSupported)\r
231                                 throw new InvalidOperationException ();\r
232 \r
233                         if (bounds.Height <= 0 || bounds.Width <= 0 || numTicks <= 0)\r
234                                 return;\r
235 \r
236                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);\r
237 \r
238                         double y = bounds.Top;\r
239                         double delta = (double)(bounds.Height - 2) / (double)(numTicks - 1);\r
240 \r
241                         for (int i = 0; i < numTicks; i++) {\r
242                                 vsr.DrawEdge (g, new Rectangle (bounds.Left, (int)Math.Round (y), bounds.Width, 5), Edges.Top, edgeStyle, EdgeEffects.None);\r
243                                 y += delta;\r
244                         }\r
245                 }\r
246 \r
247                 public static void DrawVerticalTrack (Graphics g, Rectangle bounds)\r
248                 {\r
249                         if (!IsSupported)\r
250                                 throw new InvalidOperationException ();\r
251 \r
252                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.Track.Normal);\r
253 \r
254                         vsr.DrawBackground (g, bounds);\r
255                 }\r
256 \r
257                 public static Size GetBottomPointingThumbSize(Graphics g, TrackBarThumbState state)\r
258                 {\r
259                         if (!IsSupported)\r
260                                 throw new InvalidOperationException ();\r
261 \r
262                         VisualStyleRenderer vsr;\r
263 \r
264                         switch (state) {\r
265                                 case TrackBarThumbState.Disabled:\r
266                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Disabled);\r
267                                         break;\r
268                                 case TrackBarThumbState.Hot:\r
269                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Hot);\r
270                                         break;\r
271                                 case TrackBarThumbState.Normal:\r
272                                 default:\r
273                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Normal);\r
274                                         break;\r
275                                 case TrackBarThumbState.Pressed:\r
276                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbBottom.Pressed);\r
277                                         break;\r
278                         }\r
279 \r
280                         return vsr.GetPartSize (g, ThemeSizeType.Draw);\r
281                 }\r
282 \r
283                 public static Size GetLeftPointingThumbSize (Graphics g, TrackBarThumbState state)\r
284                 {\r
285                         if (!IsSupported)\r
286                                 throw new InvalidOperationException ();\r
287 \r
288                         VisualStyleRenderer vsr;\r
289 \r
290                         switch (state) {\r
291                                 case TrackBarThumbState.Disabled:\r
292                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Disabled);\r
293                                         break;\r
294                                 case TrackBarThumbState.Hot:\r
295                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Hot);\r
296                                         break;\r
297                                 case TrackBarThumbState.Normal:\r
298                                 default:\r
299                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Normal);\r
300                                         break;\r
301                                 case TrackBarThumbState.Pressed:\r
302                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbLeft.Pressed);\r
303                                         break;\r
304                         }\r
305 \r
306                         return vsr.GetPartSize (g, ThemeSizeType.Draw);\r
307                 }\r
308 \r
309                 public static Size GetRightPointingThumbSize (Graphics g, TrackBarThumbState state)\r
310                 {\r
311                         if (!IsSupported)\r
312                                 throw new InvalidOperationException ();\r
313 \r
314                         VisualStyleRenderer vsr;\r
315 \r
316                         switch (state) {\r
317                                 case TrackBarThumbState.Disabled:\r
318                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Disabled);\r
319                                         break;\r
320                                 case TrackBarThumbState.Hot:\r
321                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Hot);\r
322                                         break;\r
323                                 case TrackBarThumbState.Normal:\r
324                                 default:\r
325                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Normal);\r
326                                         break;\r
327                                 case TrackBarThumbState.Pressed:\r
328                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbRight.Pressed);\r
329                                         break;\r
330                         }\r
331 \r
332                         return vsr.GetPartSize (g, ThemeSizeType.Draw);\r
333                 }\r
334 \r
335                 public static Size GetTopPointingThumbSize (Graphics g, TrackBarThumbState state)\r
336                 {\r
337                         if (!IsSupported)\r
338                                 throw new InvalidOperationException ();\r
339 \r
340                         VisualStyleRenderer vsr;\r
341 \r
342                         switch (state) {\r
343                                 case TrackBarThumbState.Disabled:\r
344                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Disabled);\r
345                                         break;\r
346                                 case TrackBarThumbState.Hot:\r
347                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Hot);\r
348                                         break;\r
349                                 case TrackBarThumbState.Normal:\r
350                                 default:\r
351                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Normal);\r
352                                         break;\r
353                                 case TrackBarThumbState.Pressed:\r
354                                         vsr = new VisualStyleRenderer (VisualStyleElement.TrackBar.ThumbTop.Pressed);\r
355                                         break;\r
356                         }\r
357 \r
358                         return vsr.GetPartSize (g, ThemeSizeType.Draw);\r
359                 }\r
360                 #endregion\r
361 \r
362                 #region Public Static Properties\r
363                 public static bool IsSupported {\r
364                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }\r
365                 }\r
366                 #endregion\r
367         }\r
368 }\r
369 #endif