New tests.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThemeVisualStyles.cs
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) 2008 George Giolfan
21 // Copyright (c) 2004-2006 Novell, Inc.
22 //
23 // Authors:
24 //      George Giolfan, georgegiolfan@yahoo.com
25 //      Ernesto Carrea, equistango@gmail.com
26
27 using System.Drawing;
28 using System.Drawing.Drawing2D;
29 using System.Windows.Forms.VisualStyles;
30
31 namespace System.Windows.Forms
32 {
33         /// <summary>
34         /// VisualStyles theme.
35         /// </summary>
36         /// <remarks>
37         /// This theme uses only the managed VisualStyles API.
38         /// To select it, set MONO_THEME to VisualStyles and call <see cref="Application.EnableVisualStyles"/>.
39         /// </remarks>
40         class ThemeVisualStyles : ThemeWin32Classic
41         {
42                 static bool render_client_areas;
43                 static bool render_non_client_areas;
44
45                 public ThemeVisualStyles ()
46                 {
47                         Update ();
48                 }
49
50                 public override void ResetDefaults ()
51                 {
52                         base.ResetDefaults ();
53                         Update ();
54                 }
55
56                 static void Update ()
57                 {
58                         bool visual_styles_is_enabled_by_user = VisualStyleInformation.IsEnabledByUser;
59                         render_client_areas =
60                                 visual_styles_is_enabled_by_user &&
61                                 (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled ||
62                                 Application.VisualStyleState == VisualStyleState.ClientAreaEnabled);
63                         render_non_client_areas =
64                                 visual_styles_is_enabled_by_user &&
65                                 (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled/* ||
66                                 Application.VisualStyleState == VisualStyleState.NonClientAreaEnabled*/);
67                 }
68
69                 public static bool RenderClientAreas {
70                         get { return render_client_areas; }
71                 }
72
73                 #region Controls
74                 #region ButtonBase
75                 public override void DrawButtonBase (Graphics dc, Rectangle clip_area, ButtonBase button)
76                 {
77                         if (button.FlatStyle == FlatStyle.System) {
78                                 ButtonRenderer.DrawButton (
79                                         dc,
80                                         new Rectangle (Point.Empty, button.Size),
81                                         button.Text,
82                                         button.Font,
83                                         button.TextFormatFlags,
84                                         null,
85                                         Rectangle.Empty,
86                                         ShouldPaintFocusRectagle (button),
87                                         GetPushButtonState (button)
88                                 );
89                                 return;
90                         }
91                         base.DrawButtonBase (dc, clip_area, button);
92                 }
93                 static PushButtonState GetPushButtonState (ButtonBase button)
94                 {
95                         if (!button.Enabled)
96                                 return PushButtonState.Disabled;
97                         if (button.Pressed)
98                                 return PushButtonState.Pressed;
99                         if (button.Entered)
100                                 return PushButtonState.Hot;
101                         if (button.IsDefault || button.Focused || button.paint_as_acceptbutton)
102                                 return PushButtonState.Default;
103                         return PushButtonState.Normal;
104                 }
105                 #endregion
106 #if NET_2_0
107                 #region Button 2.0
108                 public override void DrawButtonBackground (Graphics g, Button button, Rectangle clipArea)
109                 {
110                         if (!RenderClientAreas ||
111                                 !button.UseVisualStyleBackColor) {
112                                 base.DrawButtonBackground (g, button, clipArea);
113                                 return;
114                         }
115                         ButtonRenderer.GetPushButtonRenderer (GetPushButtonState (button)).DrawBackground (g, new Rectangle (Point.Empty, button.Size));
116                 }
117                 #endregion
118 #endif
119                 #region CheckBox
120                 protected override void CheckBox_DrawCheckBox (Graphics dc, CheckBox checkbox, ButtonState state, Rectangle checkbox_rectangle)
121                 {
122                         if (checkbox.Appearance == Appearance.Normal && checkbox.FlatStyle == FlatStyle.System) {
123                                 CheckBoxRenderer.DrawCheckBox (
124                                         dc,
125                                         new Point (checkbox_rectangle.Left, checkbox_rectangle.Top),
126                                         GetCheckBoxState (checkbox)
127                                 );
128                                 return;
129                         }
130                         base.CheckBox_DrawCheckBox(dc, checkbox, state, checkbox_rectangle);
131                 }
132                 static CheckBoxState GetCheckBoxState (CheckBox checkBox)
133                 {
134                         switch (checkBox.CheckState) {
135                         case CheckState.Checked:
136                                 if (!checkBox.Enabled)
137                                         return CheckBoxState.CheckedDisabled;
138                                 else if (checkBox.Pressed)
139                                         return CheckBoxState.CheckedPressed;
140                                 else if (checkBox.Entered)
141                                         return CheckBoxState.CheckedHot;
142                                 return CheckBoxState.CheckedNormal;
143                         case CheckState.Indeterminate:
144                                 if (!checkBox.Enabled)
145                                         return CheckBoxState.MixedDisabled;
146                                 else if (checkBox.Pressed)
147                                         return CheckBoxState.MixedPressed;
148                                 else if (checkBox.Entered)
149                                         return CheckBoxState.MixedHot;
150                                 return CheckBoxState.MixedNormal;
151                         default:
152                                 if (!checkBox.Enabled)
153                                         return CheckBoxState.UncheckedDisabled;
154                                 else if (checkBox.Pressed)
155                                         return CheckBoxState.UncheckedPressed;
156                                 else if (checkBox.Entered)
157                                         return CheckBoxState.UncheckedHot;
158                                 return CheckBoxState.UncheckedNormal;
159                         }
160                 }
161                 #endregion
162                 #region ComboBox
163                 static VisualStyleElement ComboBoxGetVisualStyleElement (ComboBox comboBox, ButtonState state)
164                 {
165                         if (state == ButtonState.Inactive)
166                                 return VisualStyleElement.ComboBox.DropDownButton.Disabled;
167                         if (state == ButtonState.Pushed)
168                                 return VisualStyleElement.ComboBox.DropDownButton.Pressed;
169                         if (comboBox.DropDownButtonEntered)
170                                 return VisualStyleElement.ComboBox.DropDownButton.Hot;
171                         return VisualStyleElement.ComboBox.DropDownButton.Normal;
172                 }
173                 public override void ComboBoxDrawNormalDropDownButton (ComboBox comboBox, Graphics g, Rectangle clippingArea, Rectangle area, ButtonState state)
174                 {
175                         if (!RenderClientAreas) {
176                                 base.ComboBoxDrawNormalDropDownButton (comboBox, g, clippingArea, area, state);
177                                 return;
178                         }
179                         VisualStyleElement element = ComboBoxGetVisualStyleElement (comboBox, state);
180                         if (!VisualStyleRenderer.IsElementDefined (element)) {
181                                 base.ComboBoxDrawNormalDropDownButton (comboBox, g, clippingArea, area, state);
182                                 return;
183                         }
184                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
185                 }
186                 public override bool ComboBoxNormalDropDownButtonHasTransparentBackground (ComboBox comboBox, ButtonState state)
187                 {
188                         if (!RenderClientAreas)
189                                 return base.ComboBoxNormalDropDownButtonHasTransparentBackground (comboBox, state);
190                         VisualStyleElement element = ComboBoxGetVisualStyleElement (comboBox, state);
191                         if (!VisualStyleRenderer.IsElementDefined (element))
192                                 return base.ComboBoxNormalDropDownButtonHasTransparentBackground (comboBox, state);
193                         return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
194                 }
195                 public override bool ComboBoxDropDownButtonHasHotElementStyle (ComboBox comboBox)
196                 {
197                         if (!RenderClientAreas)
198                                 return base.ComboBoxDropDownButtonHasHotElementStyle (comboBox);
199 #if NET_2_0
200                         switch (comboBox.FlatStyle) {
201                         case FlatStyle.Flat:
202                         case FlatStyle.Popup:
203                                 return base.ComboBoxDropDownButtonHasHotElementStyle (comboBox);
204                         }
205 #endif
206                         return true;
207                 }
208                 static bool ComboBoxShouldPaintBackground (ComboBox comboBox)
209                 {
210                         if (comboBox.DropDownStyle == ComboBoxStyle.Simple)
211                                 return false;
212 #if NET_2_0
213                         switch (comboBox.FlatStyle) {
214                         case FlatStyle.Flat:
215                         case FlatStyle.Popup:
216                                 return false;
217                         }
218 #endif 
219                         return true;
220                 }
221                 public override void ComboBoxDrawBackground (ComboBox comboBox, Graphics g, Rectangle clippingArea, FlatStyle style)
222                 {
223                         if (!RenderClientAreas || !ComboBoxShouldPaintBackground (comboBox)) {
224                                 base.ComboBoxDrawBackground (comboBox, g, clippingArea, style);
225                                 return;
226                         }
227                         VisualStyleElement element;
228                         if (!comboBox.Enabled)
229                                 element = VisualStyleElement.ComboBox.Border.Disabled;
230                         else if (comboBox.Entered)
231                                 element = VisualStyleElement.ComboBox.Border.Hot;
232                         else if (comboBox.Focused)
233                                 element = VisualStyleElement.ComboBox.Border.Focused;
234                         else
235                                 element = VisualStyleElement.ComboBox.Border.Normal;
236                         if (!VisualStyleRenderer.IsElementDefined (element)) {
237                                 base.ComboBoxDrawBackground (comboBox, g, clippingArea, style);
238                                 return;
239                         }
240                         new VisualStyleRenderer (element).DrawBackground (g, new Rectangle (Point.Empty, comboBox.Size), clippingArea);
241                 }
242                 public override bool CombBoxBackgroundHasHotElementStyle (ComboBox comboBox)
243                 {
244                         if (RenderClientAreas &&
245                                 ComboBoxShouldPaintBackground (comboBox) &&
246                                 comboBox.Enabled &&
247                                 VisualStyleRenderer.IsElementDefined (VisualStyleElement.ComboBox.Border.Hot))
248                                 return true;
249                         return base.CombBoxBackgroundHasHotElementStyle (comboBox);
250                 }
251                 #endregion
252                 #region ControlPaint
253                 #region DrawButton
254                 public override void CPDrawButton (Graphics dc, Rectangle rectangle, ButtonState state)
255                 {
256                         if (!RenderClientAreas ||
257                                 (state & ButtonState.Flat) == ButtonState.Flat ||
258                                 (state & ButtonState.Checked) == ButtonState.Checked) {
259                                 base.CPDrawButton (dc, rectangle, state);
260                                 return;
261                         }
262                         VisualStyleElement element;
263                         if ((state & ButtonState.Inactive) == ButtonState.Inactive)
264                                 element = VisualStyleElement.Button.PushButton.Disabled;
265                         else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
266                                 element = VisualStyleElement.Button.PushButton.Pressed;
267                         else
268                                 element = VisualStyleElement.Button.PushButton.Normal;
269                         if (!VisualStyleRenderer.IsElementDefined (element)) {
270                                 base.CPDrawButton (dc, rectangle, state);
271                                 return;
272                         }
273                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
274                 }
275                 #endregion
276                 #region DrawCaptionButton
277                 public override void CPDrawCaptionButton (Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state)
278                 {
279                         if (!RenderClientAreas ||
280                                 (state & ButtonState.Flat) == ButtonState.Flat ||
281                                 (state & ButtonState.Checked) == ButtonState.Checked) {
282                                 base.CPDrawCaptionButton (graphics, rectangle, button, state);
283                                 return;
284                         }
285                         VisualStyleElement element = GetCaptionButtonVisualStyleElement (button, state);
286                         if (!VisualStyleRenderer.IsElementDefined (element)) {
287                                 base.CPDrawCaptionButton (graphics, rectangle, button, state);
288                                 return;
289                         }
290                         new VisualStyleRenderer (element).DrawBackground (graphics, rectangle);
291                 }
292                 static VisualStyleElement GetCaptionButtonVisualStyleElement (CaptionButton button, ButtonState state)
293                 {
294                         switch (button) {
295                         case CaptionButton.Minimize:
296                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
297                                         return VisualStyleElement.Window.MinButton.Disabled;
298                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
299                                         return VisualStyleElement.Window.MinButton.Pressed;
300                                 else
301                                         return VisualStyleElement.Window.MinButton.Normal;
302                         case CaptionButton.Maximize:
303                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
304                                         return VisualStyleElement.Window.MaxButton.Disabled;
305                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
306                                         return VisualStyleElement.Window.MaxButton.Pressed;
307                                 else
308                                         return VisualStyleElement.Window.MaxButton.Normal;
309                         case CaptionButton.Close:
310                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
311                                         return VisualStyleElement.Window.CloseButton.Disabled;
312                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
313                                         return VisualStyleElement.Window.CloseButton.Pressed;
314                                 else
315                                         return VisualStyleElement.Window.CloseButton.Normal;
316                         case CaptionButton.Restore:
317                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
318                                         return VisualStyleElement.Window.RestoreButton.Disabled;
319                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
320                                         return VisualStyleElement.Window.RestoreButton.Pressed;
321                                 else
322                                         return VisualStyleElement.Window.RestoreButton.Normal;
323                         default:
324                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
325                                         return VisualStyleElement.Window.HelpButton.Disabled;
326                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
327                                         return VisualStyleElement.Window.HelpButton.Pressed;
328                                 else
329                                         return VisualStyleElement.Window.HelpButton.Normal;
330                         }
331                 }
332                 #endregion
333                 #region DrawCheckBox
334                 public override void CPDrawCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
335                 {
336                         if (!RenderClientAreas ||
337                                 (state & ButtonState.Flat) == ButtonState.Flat) {
338                                 base.CPDrawCheckBox (dc, rectangle, state);
339                                 return;
340                         }
341                         VisualStyleElement element;
342                         if ((state & ButtonState.Checked) == ButtonState.Checked)
343                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
344                                         element = VisualStyleElement.Button.CheckBox.CheckedDisabled;
345                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
346                                         element = VisualStyleElement.Button.CheckBox.CheckedPressed;
347                                 else
348                                         element = VisualStyleElement.Button.CheckBox.CheckedNormal;
349                         else
350                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
351                                         element = VisualStyleElement.Button.CheckBox.UncheckedDisabled;
352                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
353                                         element = VisualStyleElement.Button.CheckBox.UncheckedPressed;
354                                 else
355                                         element = VisualStyleElement.Button.CheckBox.UncheckedNormal;
356                         if (!VisualStyleRenderer.IsElementDefined (element)) {
357                                 base.CPDrawCheckBox (dc, rectangle, state);
358                                 return;
359                         }
360                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
361                 }
362                 #endregion
363                 #region DrawComboButton
364                 public override void CPDrawComboButton (Graphics graphics, Rectangle rectangle, ButtonState state)
365                 {
366                         if (!RenderClientAreas ||
367                                 (state & ButtonState.Flat) == ButtonState.Flat ||
368                                 (state & ButtonState.Checked) == ButtonState.Checked) {
369                                 base.CPDrawComboButton (graphics, rectangle, state);
370                                 return;
371                         }
372                         VisualStyleElement element;
373                         if ((state & ButtonState.Inactive) == ButtonState.Inactive)
374                                 element = VisualStyleElement.ComboBox.DropDownButton.Disabled;
375                         else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
376                                 element = VisualStyleElement.ComboBox.DropDownButton.Pressed;
377                         else
378                                 element = VisualStyleElement.ComboBox.DropDownButton.Normal;
379                         if (!VisualStyleRenderer.IsElementDefined (element)) {
380                                 base.CPDrawComboButton (graphics, rectangle, state);
381                                 return;
382                         }
383                         new VisualStyleRenderer (element).DrawBackground (graphics, rectangle);
384                 }
385                 #endregion
386                 #region DrawMixedCheckBox
387                 public override void CPDrawMixedCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
388                 {
389                         if (!RenderClientAreas ||
390                                 (state & ButtonState.Flat) == ButtonState.Flat) {
391                                 base.CPDrawMixedCheckBox (dc, rectangle, state);
392                                 return;
393                         }
394                         VisualStyleElement element;
395                         if ((state & ButtonState.Checked) == ButtonState.Checked)
396                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
397                                         element = VisualStyleElement.Button.CheckBox.MixedDisabled;
398                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
399                                         element = VisualStyleElement.Button.CheckBox.MixedPressed;
400                                 else
401                                         element = VisualStyleElement.Button.CheckBox.MixedNormal;
402                         else
403                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
404                                         element = VisualStyleElement.Button.CheckBox.UncheckedDisabled;
405                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
406                                         element = VisualStyleElement.Button.CheckBox.UncheckedPressed;
407                                 else
408                                         element = VisualStyleElement.Button.CheckBox.UncheckedNormal;
409                         if (!VisualStyleRenderer.IsElementDefined (element)) {
410                                 base.CPDrawMixedCheckBox (dc, rectangle, state);
411                                 return;
412                         }
413                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
414                 }
415                 #endregion
416                 #region DrawRadioButton
417                 public override void CPDrawRadioButton (Graphics dc, Rectangle rectangle, ButtonState state)
418                 {
419                         if (!RenderClientAreas ||
420                                 (state & ButtonState.Flat) == ButtonState.Flat) {
421                                 base.CPDrawRadioButton (dc, rectangle, state);
422                                 return;
423                         }
424                         VisualStyleElement element;
425                         if ((state & ButtonState.Checked) == ButtonState.Checked)
426                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
427                                         element = VisualStyleElement.Button.RadioButton.CheckedDisabled;
428                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
429                                         element = VisualStyleElement.Button.RadioButton.CheckedPressed;
430                                 else
431                                         element = VisualStyleElement.Button.RadioButton.CheckedNormal;
432                         else
433                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
434                                         element = VisualStyleElement.Button.RadioButton.UncheckedDisabled;
435                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
436                                         element = VisualStyleElement.Button.RadioButton.UncheckedPressed;
437                                 else
438                                         element = VisualStyleElement.Button.RadioButton.UncheckedNormal;
439                         if (!VisualStyleRenderer.IsElementDefined (element)) {
440                                 base.CPDrawRadioButton (dc, rectangle, state);
441                                 return;
442                         }
443                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
444                 }
445                 #endregion
446                 #region DrawScrollButton
447                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state)
448                 {
449                         if (!RenderClientAreas ||
450                                 (state & ButtonState.Flat) == ButtonState.Flat ||
451                                 (state & ButtonState.Checked) == ButtonState.Checked) {
452                                 base.CPDrawScrollButton (dc, area, type, state);
453                                 return;
454                         }
455                         VisualStyleElement element = GetScrollButtonVisualStyleElement (type, state);
456                         if (!VisualStyleRenderer.IsElementDefined (element)) {
457                                 base.CPDrawScrollButton (dc, area, type, state);
458                                 return;
459                         }
460                         new VisualStyleRenderer (element).DrawBackground (dc, area);
461                 }
462                 static VisualStyleElement GetScrollButtonVisualStyleElement (ScrollButton type, ButtonState state)
463                 {
464                         switch (type) {
465                         case ScrollButton.Left:
466                                 if (IsDisabled (state))
467                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftDisabled;
468                                 else if (IsPressed (state))
469                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
470                                 else
471                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
472                         case ScrollButton.Right:
473                                 if (IsDisabled (state))
474                                         return VisualStyleElement.ScrollBar.ArrowButton.RightDisabled;
475                                 else if (IsPressed (state))
476                                         return VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
477                                 else
478                                         return VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
479                         case ScrollButton.Up:
480                                 if (IsDisabled (state))
481                                         return VisualStyleElement.ScrollBar.ArrowButton.UpDisabled;
482                                 else if (IsPressed (state))
483                                         return VisualStyleElement.ScrollBar.ArrowButton.UpPressed;
484                                 else
485                                         return VisualStyleElement.ScrollBar.ArrowButton.UpNormal;
486                         default:
487                                 if (IsDisabled (state))
488                                         return VisualStyleElement.ScrollBar.ArrowButton.DownDisabled;
489                                 else if (IsPressed (state))
490                                         return VisualStyleElement.ScrollBar.ArrowButton.DownPressed;
491                                 else
492                                         return VisualStyleElement.ScrollBar.ArrowButton.DownNormal;
493                         }
494                 }
495                 static bool IsDisabled (ButtonState state)
496                 {
497                         return (state & ButtonState.Inactive) == ButtonState.Inactive;
498                 }
499                 static bool IsPressed (ButtonState state)
500                 {
501                         return (state & ButtonState.Pushed) == ButtonState.Pushed;
502                 }
503                 #endregion
504                 #endregion
505 #if NET_2_0
506                 #region DataGridView
507                 #region DataGridViewHeaderCell
508                 #region DataGridViewRowHeaderCell
509                 public override bool DataGridViewRowHeaderCellDrawBackground (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
510                 {
511                         if (!RenderClientAreas ||
512                                 !cell.DataGridView.EnableHeadersVisualStyles)
513                                 return base.DataGridViewRowHeaderCellDrawBackground (cell, g, bounds);
514                         VisualStyleElement element = DataGridViewRowHeaderCellGetVisualStyleElement (cell);
515                         if (!VisualStyleRenderer.IsElementDefined (element))
516                                 return base.DataGridViewRowHeaderCellDrawBackground (cell, g, bounds);
517                         bounds.Width--;
518                         Bitmap bitmap = new Bitmap (bounds.Height, bounds.Width);
519                         Graphics bitmap_g = Graphics.FromImage (bitmap);
520                         Rectangle bitmap_rectangle = new Rectangle (Point.Empty, bitmap.Size);
521                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
522                         if (!AreEqual (element, VisualStyleElement.Header.Item.Normal) && renderer.IsBackgroundPartiallyTransparent ())
523                                 new VisualStyleRenderer (VisualStyleElement.Header.Item.Normal).DrawBackground (bitmap_g, bitmap_rectangle);
524                         renderer.DrawBackground (bitmap_g, bitmap_rectangle);
525                         bitmap_g.Dispose ();
526                         g.Transform = new Matrix(0, 1, 1, 0, 0, 0);
527                         g.DrawImage (bitmap, bounds.Y, bounds.X);
528                         bitmap.Dispose ();
529                         g.ResetTransform ();
530                         return true;
531                 }
532                 public override bool DataGridViewRowHeaderCellDrawSelectionBackground (DataGridViewRowHeaderCell cell)
533                 {
534                         if (!RenderClientAreas ||
535                                 !cell.DataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (DataGridViewRowHeaderCellGetVisualStyleElement (cell)))
536                                 return base.DataGridViewRowHeaderCellDrawSelectionBackground (cell);
537                         return true;
538                 }
539                 public override bool DataGridViewRowHeaderCellDrawBorder (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
540                 {
541                         if (!RenderClientAreas ||
542                                 !cell.DataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (DataGridViewRowHeaderCellGetVisualStyleElement (cell)))
543                                 return base.DataGridViewRowHeaderCellDrawBorder (cell, g, bounds);
544                         g.DrawLine (cell.GetBorderPen (), bounds.Right - 1, bounds.Top, bounds.Right - 1, bounds.Bottom - 1);
545                         return true;
546                 }
547                 static VisualStyleElement DataGridViewRowHeaderCellGetVisualStyleElement (DataGridViewRowHeaderCell cell)
548                 {
549                         if (cell.DataGridView.PressedHeaderCell == cell)
550                                 return VisualStyleElement.Header.Item.Pressed;
551                         if (cell.DataGridView.EnteredHeaderCell == cell)
552                                 return VisualStyleElement.Header.Item.Hot;
553                         if (cell.OwningRow.SelectedInternal)
554                                 return VisualStyleElement.Header.Item.Pressed;
555                         return VisualStyleElement.Header.Item.Normal;
556                 }
557                 #endregion
558                 #region DataGridViewColumnHeaderCell
559                 public override bool DataGridViewColumnHeaderCellDrawBackground (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
560                 {
561                         if (!RenderClientAreas ||
562                                 !cell.DataGridView.EnableHeadersVisualStyles || cell is DataGridViewTopLeftHeaderCell)
563                                 return base.DataGridViewColumnHeaderCellDrawBackground (cell, g, bounds);
564                         VisualStyleElement element = DataGridViewColumnHeaderCellGetVisualStyleElement (cell);
565                         if (!VisualStyleRenderer.IsElementDefined (element))
566                                 return base.DataGridViewColumnHeaderCellDrawBackground (cell, g, bounds);
567                         bounds.Height--;
568                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
569                         if (!AreEqual (element, VisualStyleElement.Header.Item.Normal) && renderer.IsBackgroundPartiallyTransparent ())
570                             new VisualStyleRenderer (VisualStyleElement.Header.Item.Normal).DrawBackground (g, bounds);
571                         renderer.DrawBackground (g, bounds);
572                         return true;
573                 }
574                 public override bool DataGridViewColumnHeaderCellDrawBorder (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
575                 {
576                         if (!RenderClientAreas ||
577                                 !cell.DataGridView.EnableHeadersVisualStyles ||
578                                 cell is DataGridViewTopLeftHeaderCell ||
579                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Normal))
580                                 return base.DataGridViewColumnHeaderCellDrawBorder (cell, g, bounds);
581                         g.DrawLine (cell.GetBorderPen (), bounds.Left, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 1);
582                         return true;
583                 }
584                 static VisualStyleElement DataGridViewColumnHeaderCellGetVisualStyleElement (DataGridViewColumnHeaderCell cell)
585                 {
586                         if (cell.DataGridView.PressedHeaderCell == cell)
587                                 return VisualStyleElement.Header.Item.Pressed;
588                         if (cell.DataGridView.EnteredHeaderCell == cell)
589                                 return VisualStyleElement.Header.Item.Hot;
590                         return VisualStyleElement.Header.Item.Normal;
591                 }
592                 #endregion
593                 public override bool DataGridViewHeaderCellHasPressedStyle (DataGridView dataGridView)
594                 {
595                         if (!RenderClientAreas ||
596                                 !dataGridView.EnableHeadersVisualStyles ||
597                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Pressed))
598                                 return base.DataGridViewHeaderCellHasPressedStyle (dataGridView);
599                         return true;
600                 }
601                 public override bool DataGridViewHeaderCellHasHotStyle (DataGridView dataGridView)
602                 {
603                         if (!RenderClientAreas ||
604                                 !dataGridView.EnableHeadersVisualStyles ||
605                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Hot))
606                                 return base.DataGridViewHeaderCellHasHotStyle (dataGridView);
607                         return true;
608                 }
609                 #endregion
610                 #endregion
611 #endif
612                 #region DateTimePicker
613                 #region Border
614                 protected override void DateTimePickerDrawBorder (DateTimePicker dateTimePicker, Graphics g, Rectangle clippingArea)
615                 {
616                         if (!RenderClientAreas) {
617                                 base.DateTimePickerDrawBorder (dateTimePicker, g, clippingArea);
618                                 return;
619                         }
620                         VisualStyleElement element;
621                         if (!dateTimePicker.Enabled)
622                                 element = VisualStyleElement.DatePicker.DateBorder.Disabled;
623                         else if (dateTimePicker.Entered)
624                                 element = VisualStyleElement.DatePicker.DateBorder.Hot;
625                         else if (dateTimePicker.Focused)
626                                 element = VisualStyleElement.DatePicker.DateBorder.Focused;
627                         else
628                                 element = VisualStyleElement.DatePicker.DateBorder.Normal;
629                         if (!VisualStyleRenderer.IsElementDefined (element)) {
630                                 base.DateTimePickerDrawBorder (dateTimePicker, g, clippingArea);
631                                 return;
632                         }
633                         new VisualStyleRenderer (element).DrawBackground (g, new Rectangle (Point.Empty, dateTimePicker.Size), clippingArea);
634                 }
635                 public override bool DateTimePickerBorderHasHotElementStyle {
636                         get {
637                                 if (RenderClientAreas &&
638                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.DatePicker.DateBorder.Hot))
639                                         return true;
640                                 return base.DateTimePickerBorderHasHotElementStyle;
641                         }
642                 }
643                 #endregion
644                 #region Drop down button
645                 protected override void DateTimePickerDrawDropDownButton (DateTimePicker dateTimePicker, Graphics g, Rectangle clippingArea)
646                 {
647                         if (!RenderClientAreas) {
648                                 base.DateTimePickerDrawDropDownButton (dateTimePicker, g, clippingArea);
649                                 return;
650                         }
651                         VisualStyleElement element;
652                         if (!dateTimePicker.Enabled)
653                                 element = VisualStyleElement.DatePicker.ShowCalendarButtonRight.Disabled;
654                         else if (dateTimePicker.is_drop_down_visible)
655                                 element = VisualStyleElement.DatePicker.ShowCalendarButtonRight.Pressed;
656                         else if (dateTimePicker.DropDownButtonEntered)
657                                 element = VisualStyleElement.DatePicker.ShowCalendarButtonRight.Hot;
658                         else
659                                 element = VisualStyleElement.DatePicker.ShowCalendarButtonRight.Normal;
660                         if (!VisualStyleRenderer.IsElementDefined (element)) {
661                                 base.DateTimePickerDrawDropDownButton (dateTimePicker, g, clippingArea);
662                                 return;
663                         }
664                         new VisualStyleRenderer (element).DrawBackground (g, dateTimePicker.drop_down_arrow_rect, clippingArea);
665                 }
666                 //TODO: Until somebody figures out how to obtain the proper width this will need to be updated when new Windows versions/themes are released.
667                 const int DateTimePickerDropDownWidthOnWindowsVista = 34;
668                 const int DateTimePickerDropDownHeightOnWindowsVista = 20;
669                 public override Rectangle DateTimePickerGetDropDownButtonArea (DateTimePicker dateTimePicker)
670                 {
671                         if (!RenderClientAreas)
672                                 return base.DateTimePickerGetDropDownButtonArea (dateTimePicker);
673                         VisualStyleElement element = VisualStyleElement.DatePicker.ShowCalendarButtonRight.Pressed;
674                         if (!VisualStyleRenderer.IsElementDefined (element))
675                                 return base.DateTimePickerGetDropDownButtonArea (dateTimePicker);
676                         Size size = new Size (DateTimePickerDropDownWidthOnWindowsVista, DateTimePickerDropDownHeightOnWindowsVista);
677                         return new Rectangle (dateTimePicker.Width - size.Width, 0, size.Width, size.Height);
678                 }
679                 public override Rectangle DateTimePickerGetDateArea (DateTimePicker dateTimePicker)
680                 {
681                         if (!RenderClientAreas ||
682                                 dateTimePicker.ShowUpDown)
683                                 return base.DateTimePickerGetDateArea (dateTimePicker);
684                         VisualStyleElement element = VisualStyleElement.DatePicker.DateBorder.Normal;
685                         if (!VisualStyleRenderer.IsElementDefined (element))
686                                 return base.DateTimePickerGetDateArea (dateTimePicker);
687                         Graphics g = dateTimePicker.CreateGraphics ();
688                         Rectangle result = new VisualStyleRenderer (element).GetBackgroundContentRectangle (g, dateTimePicker.ClientRectangle);
689                         g.Dispose ();
690                         result.Width -= DateTimePickerDropDownWidthOnWindowsVista;
691                         return result;
692                 }
693                 public override bool DateTimePickerDropDownButtonHasHotElementStyle {
694                         get {
695                                 if (RenderClientAreas &&
696                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.DatePicker.ShowCalendarButtonRight.Hot))
697                                         return true;
698                                 return base.DateTimePickerDropDownButtonHasHotElementStyle;
699                         }
700                 }
701                 #endregion
702                 #endregion
703                 #region ListView
704                 protected override void ListViewDrawColumnHeaderBackground (ListView listView, ColumnHeader columnHeader, Graphics g, Rectangle area, Rectangle clippingArea)
705                 {
706                         if (!RenderClientAreas) {
707                                 base.ListViewDrawColumnHeaderBackground (listView, columnHeader, g, area, clippingArea);
708                                 return;
709                         }
710                         VisualStyleElement element;
711                         if (listView.HeaderStyle == ColumnHeaderStyle.Clickable)
712                                 if (columnHeader.Pressed)
713                                         element = VisualStyleElement.Header.Item.Pressed;
714                                 else if (columnHeader == listView.EnteredColumnHeader)
715                                         element = VisualStyleElement.Header.Item.Hot;
716                                 else
717                                         element = VisualStyleElement.Header.Item.Normal;
718                         else
719                                 element = VisualStyleElement.Header.Item.Normal;
720                         if (!VisualStyleRenderer.IsElementDefined (element)) {
721                                 base.ListViewDrawColumnHeaderBackground (listView, columnHeader, g, area, clippingArea);
722                                 return;
723                         }
724                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
725                 }
726                 protected override void ListViewDrawUnusedHeaderBackground (ListView listView, Graphics g, Rectangle area, Rectangle clippingArea)
727                 {
728                         if (!RenderClientAreas) {
729                                 base.ListViewDrawUnusedHeaderBackground (listView, g, area, clippingArea);
730                                 return;
731                         }
732                         VisualStyleElement element = VisualStyleElement.Header.Item.Normal;
733                         if (!VisualStyleRenderer.IsElementDefined (element)) {
734                                 base.ListViewDrawUnusedHeaderBackground (listView, g, area, clippingArea);
735                                 return;
736                         }
737                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
738                 }
739                 public override bool ListViewHasHotHeaderStyle {
740                         get {
741                                 if (!RenderClientAreas ||
742                                         !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Hot))
743                                         return base.ListViewHasHotHeaderStyle;
744                                 return true;
745                         }
746                 }
747                 public override int ListViewGetHeaderHeight (ListView listView, Font font)
748                 {
749                         if (!RenderClientAreas)
750                                 return base.ListViewGetHeaderHeight (listView, font);
751                         VisualStyleElement element = VisualStyleElement.Header.Item.Normal;
752                         if (!VisualStyleRenderer.IsElementDefined (element))
753                                 return base.ListViewGetHeaderHeight (listView, font);
754                         Control control = null;
755                         Graphics g;
756                         if (listView == null) {
757                                 control = new Control ();
758                                 g = control.CreateGraphics ();
759                         } else
760                                 g = listView.CreateGraphics ();
761                         int result = new VisualStyleRenderer (element).GetPartSize (g, ThemeSizeType.True).Height;
762                         g.Dispose ();
763                         if (listView == null)
764                                 control.Dispose ();
765                         return result;
766                 }
767                 #endregion
768                 #region GroupBox
769                 public override void DrawGroupBox (Graphics dc, Rectangle area, GroupBox box)
770                 {
771                         GroupBoxRenderer.DrawGroupBox (
772                                 dc,
773                                 new Rectangle (Point.Empty, box.Size),
774                                 box.Text,
775                                 box.Font,
776                                 box.ForeColor == GroupBox.DefaultForeColor ? Color.Empty : box.ForeColor,
777                                 box.Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled);
778                 }
779                 #endregion
780                 #region Managed window
781                 Rectangle ManagedWindowGetTitleBarRectangle (InternalWindowManager wm)
782                 {
783                         return new Rectangle (0, 0, wm.Form.Width, ManagedWindowTitleBarHeight (wm) + ManagedWindowBorderWidth (wm) * (wm.IsMinimized ? 2 : 1));
784                 }
785                 Region ManagedWindowGetWindowRegion (Form form)
786                 {
787                         if (form.WindowManager is MdiWindowManager && form.WindowManager.IsMaximized)
788                                 return null;
789                         VisualStyleElement title_bar_element = ManagedWindowGetTitleBarVisualStyleElement (form.WindowManager);
790                         if (!VisualStyleRenderer.IsElementDefined (title_bar_element))
791                                 return null;
792                         VisualStyleRenderer renderer = new VisualStyleRenderer (title_bar_element);
793                         if (!renderer.IsBackgroundPartiallyTransparent ())
794                                 return null;
795                         IDeviceContext dc = GetMeasurementDeviceContext ();
796                         Rectangle title_bar_rectangle = ManagedWindowGetTitleBarRectangle (form.WindowManager);
797                         Region region = renderer.GetBackgroundRegion (dc, title_bar_rectangle);
798                         ReleaseMeasurementDeviceContext (dc);
799                         region.Union (new Rectangle (0, title_bar_rectangle.Bottom, form.Width, form.Height));
800                         return region;
801                 }
802                 public override void ManagedWindowOnSizeInitializedOrChanged (Form form)
803                 {
804                         base.ManagedWindowOnSizeInitializedOrChanged (form);
805                         if (!render_non_client_areas)
806                                 return;
807                         form.Region = ManagedWindowGetWindowRegion (form);
808                 }
809                 protected override Rectangle ManagedWindowDrawTitleBarAndBorders (Graphics dc, Rectangle clip, InternalWindowManager wm)
810                 {
811                         if (!render_non_client_areas)
812                                 return base.ManagedWindowDrawTitleBarAndBorders (dc, clip, wm);
813                         VisualStyleElement title_bar_element = ManagedWindowGetTitleBarVisualStyleElement (wm);
814                         VisualStyleElement left_border_element;
815                         VisualStyleElement right_border_element;
816                         VisualStyleElement bottom_border_element;
817                         ManagedWindowGetBorderVisualStyleElements (wm, out left_border_element, out right_border_element, out bottom_border_element);
818                         if (!VisualStyleRenderer.IsElementDefined (title_bar_element) ||
819                                 (!wm.IsMinimized && (
820                                 !VisualStyleRenderer.IsElementDefined (left_border_element) ||
821                                 !VisualStyleRenderer.IsElementDefined (right_border_element) ||
822                                 !VisualStyleRenderer.IsElementDefined (bottom_border_element))))
823                                 return base.ManagedWindowDrawTitleBarAndBorders (dc, clip, wm);
824                         VisualStyleRenderer renderer = new VisualStyleRenderer (title_bar_element);
825                         Rectangle title_bar_rectangle = ManagedWindowGetTitleBarRectangle (wm);
826                         renderer.DrawBackground (dc, title_bar_rectangle, clip);
827                         if (!wm.IsMinimized) {
828                                 int border_width = ManagedWindowBorderWidth (wm);
829                                 renderer.SetParameters (left_border_element);
830                                 renderer.DrawBackground (dc, new Rectangle (
831                                         0,
832                                         title_bar_rectangle.Bottom,
833                                         border_width,
834                                         wm.Form.Height - title_bar_rectangle.Bottom
835                                         ), clip);
836                                 renderer.SetParameters (right_border_element);
837                                 renderer.DrawBackground (dc, new Rectangle (
838                                         wm.Form.Width - border_width,
839                                         title_bar_rectangle.Bottom,
840                                         border_width,
841                                         wm.Form.Height - title_bar_rectangle.Bottom
842                                         ), clip);
843                                 renderer.SetParameters (bottom_border_element);
844                                 renderer.DrawBackground (dc, new Rectangle (
845                                         0,
846                                         wm.Form.Height - border_width,
847                                         wm.Form.Width,
848                                         border_width
849                                         ), clip);
850                         }
851                         return title_bar_rectangle;
852                 }
853                 static FormWindowState ManagedWindowGetWindowState (InternalWindowManager wm)
854                 {
855                         return wm.GetWindowState ();
856                 }
857                 static bool ManagedWindowIsDisabled (InternalWindowManager wm)
858                 {
859                         return !wm.Form.Enabled;
860                 }
861                 static bool ManagedWindowIsActive (InternalWindowManager wm)
862                 {
863                         return wm.IsActive;
864                 }
865                 static VisualStyleElement ManagedWindowGetTitleBarVisualStyleElement (InternalWindowManager wm)
866                 {
867                         if (wm.IsToolWindow)
868                                 #region Small window
869                                 switch (ManagedWindowGetWindowState (wm)) {
870                                 case FormWindowState.Minimized:
871                                         if (ManagedWindowIsDisabled (wm))
872                                                 return VisualStyleElement.Window.SmallMinCaption.Disabled;
873                                         else if (ManagedWindowIsActive (wm))
874                                                 return VisualStyleElement.Window.SmallMinCaption.Active;
875                                         return VisualStyleElement.Window.SmallMinCaption.Inactive;
876                                 case FormWindowState.Maximized:
877                                         if (ManagedWindowIsDisabled (wm))
878                                                 return VisualStyleElement.Window.SmallMaxCaption.Disabled;
879                                         else if (ManagedWindowIsActive (wm))
880                                                 return VisualStyleElement.Window.SmallMaxCaption.Active;
881                                         return VisualStyleElement.Window.SmallMaxCaption.Inactive;
882                                 default:
883                                         if (ManagedWindowIsDisabled (wm))
884                                                 return VisualStyleElement.Window.SmallCaption.Disabled;
885                                         else if (ManagedWindowIsActive (wm))
886                                                 return VisualStyleElement.Window.SmallCaption.Active;
887                                         return VisualStyleElement.Window.SmallCaption.Inactive;
888                                 }
889                                 #endregion
890                         else
891                                 #region Normal window
892                                 switch (ManagedWindowGetWindowState (wm)) {
893                                 case FormWindowState.Minimized:
894                                         if (ManagedWindowIsDisabled (wm))
895                                                 return VisualStyleElement.Window.MinCaption.Disabled;
896                                         else if (ManagedWindowIsActive (wm))
897                                                 return VisualStyleElement.Window.MinCaption.Active;
898                                         return VisualStyleElement.Window.MinCaption.Inactive;
899                                 case FormWindowState.Maximized:
900                                         if (ManagedWindowIsDisabled (wm))
901                                                 return VisualStyleElement.Window.MaxCaption.Disabled;
902                                         else if (ManagedWindowIsActive (wm))
903                                                 return VisualStyleElement.Window.MaxCaption.Active;
904                                         return VisualStyleElement.Window.MaxCaption.Inactive;
905                                 default:
906                                         if (ManagedWindowIsDisabled (wm))
907                                                 return VisualStyleElement.Window.Caption.Disabled;
908                                         else if (ManagedWindowIsActive (wm))
909                                                 return VisualStyleElement.Window.Caption.Active;
910                                         return VisualStyleElement.Window.Caption.Inactive;
911                                 }
912                                 #endregion
913                 }
914                 static void ManagedWindowGetBorderVisualStyleElements (InternalWindowManager wm, out VisualStyleElement left, out VisualStyleElement right, out VisualStyleElement bottom)
915                 {
916                         bool active = !ManagedWindowIsDisabled (wm) && ManagedWindowIsActive (wm);
917                         if (wm.IsToolWindow) {
918                                 if (active) {
919                                         left = VisualStyleElement.Window.SmallFrameLeft.Active;
920                                         right = VisualStyleElement.Window.SmallFrameRight.Active;
921                                         bottom = VisualStyleElement.Window.SmallFrameBottom.Active;
922                                 } else {
923                                         left = VisualStyleElement.Window.SmallFrameLeft.Inactive;
924                                         right = VisualStyleElement.Window.SmallFrameRight.Inactive;
925                                         bottom = VisualStyleElement.Window.SmallFrameBottom.Inactive;
926                                 }
927                         } else {
928                                 if (active) {
929                                         left = VisualStyleElement.Window.FrameLeft.Active;
930                                         right = VisualStyleElement.Window.FrameRight.Active;
931                                         bottom = VisualStyleElement.Window.FrameBottom.Active;
932                                 } else {
933                                         left = VisualStyleElement.Window.FrameLeft.Inactive;
934                                         right = VisualStyleElement.Window.FrameRight.Inactive;
935                                         bottom = VisualStyleElement.Window.FrameBottom.Inactive;
936                                 }
937                         }
938                 }
939                 public override bool ManagedWindowTitleButtonHasHotElementStyle (TitleButton button, Form form)
940                 {
941                         if (render_non_client_areas && (button.State & ButtonState.Inactive) != ButtonState.Inactive) {
942                                 VisualStyleElement element;
943                                 if (ManagedWindowIsMaximizedMdiChild (form))
944                                         switch (button.Caption) {
945                                         case CaptionButton.Close:
946                                                 element = VisualStyleElement.Window.MdiCloseButton.Hot;
947                                                 break;
948                                         case CaptionButton.Help:
949                                                 element = VisualStyleElement.Window.MdiHelpButton.Hot;
950                                                 break;
951                                         case CaptionButton.Minimize:
952                                                 element = VisualStyleElement.Window.MdiMinButton.Hot;
953                                                 break;
954                                         default:
955                                                 element = VisualStyleElement.Window.MdiRestoreButton.Hot;
956                                                 break;
957                                         }
958                                 else if (form.WindowManager.IsToolWindow)
959                                         element = VisualStyleElement.Window.SmallCloseButton.Hot;
960                                 else
961                                         switch (button.Caption) {
962                                         case CaptionButton.Close:
963                                                 element = VisualStyleElement.Window.CloseButton.Hot;
964                                                 break;
965                                         case CaptionButton.Help:
966                                                 element = VisualStyleElement.Window.HelpButton.Hot;
967                                                 break;
968                                         case CaptionButton.Maximize:
969                                                 element = VisualStyleElement.Window.MaxButton.Hot;
970                                                 break;
971                                         case CaptionButton.Minimize:
972                                                 element = VisualStyleElement.Window.MinButton.Hot;
973                                                 break;
974                                         default:
975                                                 element = VisualStyleElement.Window.RestoreButton.Hot;
976                                                 break;
977                                         }
978                                 if (VisualStyleRenderer.IsElementDefined (element))
979                                         return true;
980                         }
981                         return base.ManagedWindowTitleButtonHasHotElementStyle (button, form);
982                 }
983                 static bool ManagedWindowIsMaximizedMdiChild (Form form)
984                 {
985                         return form.WindowManager is MdiWindowManager &&
986                                 ManagedWindowGetWindowState (form.WindowManager) == FormWindowState.Maximized;
987                 }
988                 static bool ManagedWindowTitleButtonIsDisabled (TitleButton button, InternalWindowManager wm)
989                 {
990                         return (button.State & ButtonState.Inactive) == ButtonState.Inactive;
991                 }
992                 static bool ManagedWindowTitleButtonIsPressed (TitleButton button)
993                 {
994                         return (button.State & ButtonState.Pushed) == ButtonState.Pushed;
995                 }
996                 static VisualStyleElement ManagedWindowGetTitleButtonVisualStyleElement (TitleButton button, Form form)
997                 {
998                         if (form.WindowManager.IsToolWindow) {
999                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1000                                         return VisualStyleElement.Window.SmallCloseButton.Disabled;
1001                                 if (ManagedWindowTitleButtonIsPressed (button))
1002                                         return VisualStyleElement.Window.SmallCloseButton.Pressed;
1003                                 if (button.Entered)
1004                                         return VisualStyleElement.Window.SmallCloseButton.Hot;
1005                                 return VisualStyleElement.Window.SmallCloseButton.Normal;
1006                         }
1007                         switch (button.Caption) {
1008                         case CaptionButton.Close:
1009                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1010                                         return VisualStyleElement.Window.CloseButton.Disabled;
1011                                 if (ManagedWindowTitleButtonIsPressed (button))
1012                                         return VisualStyleElement.Window.CloseButton.Pressed;
1013                                 if (button.Entered)
1014                                         return VisualStyleElement.Window.CloseButton.Hot;
1015                                 return VisualStyleElement.Window.CloseButton.Normal;
1016                         case CaptionButton.Help:
1017                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1018                                         return VisualStyleElement.Window.HelpButton.Disabled;
1019                                 if (ManagedWindowTitleButtonIsPressed (button))
1020                                         return VisualStyleElement.Window.HelpButton.Pressed;
1021                                 if (button.Entered)
1022                                         return VisualStyleElement.Window.HelpButton.Hot;
1023                                 return VisualStyleElement.Window.HelpButton.Normal;
1024                         case CaptionButton.Maximize:
1025                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1026                                         return VisualStyleElement.Window.MaxButton.Disabled;
1027                                 if (ManagedWindowTitleButtonIsPressed (button))
1028                                         return VisualStyleElement.Window.MaxButton.Pressed;
1029                                 if (button.Entered)
1030                                         return VisualStyleElement.Window.MaxButton.Hot;
1031                                 return VisualStyleElement.Window.MaxButton.Normal;
1032                         case CaptionButton.Minimize:
1033                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1034                                         return VisualStyleElement.Window.MinButton.Disabled;
1035                                 if (ManagedWindowTitleButtonIsPressed (button))
1036                                         return VisualStyleElement.Window.MinButton.Pressed;
1037                                 if (button.Entered)
1038                                         return VisualStyleElement.Window.MinButton.Hot;
1039                                 return VisualStyleElement.Window.MinButton.Normal;
1040                         default:
1041                                 if (ManagedWindowTitleButtonIsDisabled (button, form.WindowManager))
1042                                         return VisualStyleElement.Window.RestoreButton.Disabled;
1043                                 if (ManagedWindowTitleButtonIsPressed (button))
1044                                         return VisualStyleElement.Window.RestoreButton.Pressed;
1045                                 if (button.Entered)
1046                                         return VisualStyleElement.Window.RestoreButton.Hot;
1047                                 return VisualStyleElement.Window.RestoreButton.Normal;
1048                         }
1049                 }
1050                 protected override void ManagedWindowDrawTitleButton (Graphics dc, TitleButton button, Rectangle clip, Form form)
1051                 {
1052                         if (!render_non_client_areas) {
1053                                 base.ManagedWindowDrawTitleButton (dc, button, clip, form);
1054                                 return;
1055                         }
1056                         VisualStyleElement element = ManagedWindowGetTitleButtonVisualStyleElement (button, form);
1057                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1058                                 base.ManagedWindowDrawTitleButton (dc, button, clip, form);
1059                                 return;
1060                         }
1061                         new VisualStyleRenderer (element).DrawBackground (dc, button.Rectangle, clip);
1062                 }
1063                 public override Size ManagedWindowButtonSize (InternalWindowManager wm)
1064                 {
1065                         if (!render_non_client_areas)
1066                                 return base.ManagedWindowButtonSize (wm);
1067                         VisualStyleElement element = wm.IsToolWindow && !wm.IsMinimized ?
1068                                 VisualStyleElement.Window.SmallCloseButton.Normal :
1069                                 VisualStyleElement.Window.CloseButton.Normal;
1070                         if (!VisualStyleRenderer.IsElementDefined (element))
1071                                 return base.ManagedWindowButtonSize (wm);
1072                         IDeviceContext dc = GetMeasurementDeviceContext ();
1073                         Size result = new VisualStyleRenderer (element).GetPartSize (dc, ThemeSizeType.True);
1074                         ReleaseMeasurementDeviceContext (dc);
1075                         return result;
1076                 }
1077                 public override void ManagedWindowDrawMenuButton (Graphics dc, TitleButton button, Rectangle clip, InternalWindowManager wm)
1078                 {
1079                         if (!render_non_client_areas) {
1080                                 base.ManagedWindowDrawMenuButton (dc, button, clip, wm);
1081                                 return;
1082                         }
1083                         VisualStyleElement element = ManagedWindowGetMenuButtonVisualStyleElement (button, wm);
1084                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1085                                 base.ManagedWindowDrawMenuButton (dc, button, clip, wm);
1086                                 return;
1087                         }
1088                         new VisualStyleRenderer (element).DrawBackground (dc, button.Rectangle, clip);
1089                 }
1090                 static VisualStyleElement ManagedWindowGetMenuButtonVisualStyleElement (TitleButton button, InternalWindowManager wm)
1091                 {
1092                         switch (button.Caption) {
1093                         case CaptionButton.Close:
1094                                 if (ManagedWindowTitleButtonIsDisabled (button, wm))
1095                                         return VisualStyleElement.Window.MdiCloseButton.Disabled;
1096                                 if (ManagedWindowTitleButtonIsPressed (button))
1097                                         return VisualStyleElement.Window.MdiCloseButton.Pressed;
1098                                 if (button.Entered)
1099                                         return VisualStyleElement.Window.MdiCloseButton.Hot;
1100                                 return VisualStyleElement.Window.MdiCloseButton.Normal;
1101                         case CaptionButton.Help:
1102                                 if (ManagedWindowTitleButtonIsDisabled (button, wm))
1103                                         return VisualStyleElement.Window.MdiHelpButton.Disabled;
1104                                 if (ManagedWindowTitleButtonIsPressed (button))
1105                                         return VisualStyleElement.Window.MdiHelpButton.Pressed;
1106                                 if (button.Entered)
1107                                         return VisualStyleElement.Window.MdiHelpButton.Hot;
1108                                 return VisualStyleElement.Window.MdiHelpButton.Normal;
1109                         case CaptionButton.Minimize:
1110                                 if (ManagedWindowTitleButtonIsDisabled (button, wm))
1111                                         return VisualStyleElement.Window.MdiMinButton.Disabled;
1112                                 if (ManagedWindowTitleButtonIsPressed (button))
1113                                         return VisualStyleElement.Window.MdiMinButton.Pressed;
1114                                 if (button.Entered)
1115                                         return VisualStyleElement.Window.MdiMinButton.Hot;
1116                                 return VisualStyleElement.Window.MdiMinButton.Normal;
1117                         default:
1118                                 if (ManagedWindowTitleButtonIsDisabled (button, wm))
1119                                         return VisualStyleElement.Window.MdiRestoreButton.Disabled;
1120                                 if (ManagedWindowTitleButtonIsPressed (button))
1121                                         return VisualStyleElement.Window.MdiRestoreButton.Pressed;
1122                                 if (button.Entered)
1123                                         return VisualStyleElement.Window.MdiRestoreButton.Hot;
1124                                 return VisualStyleElement.Window.MdiRestoreButton.Normal;
1125                         }
1126                 }
1127                 #endregion
1128                 #region ProgressBar
1129                 public override void DrawProgressBar (Graphics dc, Rectangle clip_rect, ProgressBar ctrl)
1130                 {
1131                         if (!RenderClientAreas ||
1132                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.ProgressBar.Bar.Normal) ||
1133                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.ProgressBar.Chunk.Normal)) {
1134                                 base.DrawProgressBar (dc, clip_rect, ctrl);
1135                                 return;
1136                         }
1137                         VisualStyleRenderer renderer = new VisualStyleRenderer (VisualStyleElement.ProgressBar.Bar.Normal);
1138                         renderer.DrawBackground (dc, ctrl.ClientRectangle, clip_rect);
1139                         Rectangle client_area = renderer.GetBackgroundContentRectangle (dc, new Rectangle (Point.Empty, ctrl.Size));
1140                         renderer = new VisualStyleRenderer (VisualStyleElement.ProgressBar.Chunk.Normal);
1141                         /* Draw Blocks */
1142                         int draw_mode = 0;
1143                         int max_blocks = int.MaxValue;
1144                         int start_pixel = client_area.X;
1145 #if NET_2_0
1146                         draw_mode = (int)ctrl.Style;
1147 #endif
1148                         switch (draw_mode) {
1149 #if NET_2_0
1150                         case 1: // Continuous
1151                                 client_area.Width = (int)(client_area.Width * ((double)(ctrl.Value - ctrl.Minimum) / (double)(Math.Max (ctrl.Maximum - ctrl.Minimum, 1))));
1152                                 renderer.DrawBackground (dc, client_area, clip_rect);
1153                                 break;
1154                         case 2: // Marquee
1155                                 int ms_diff = (int)(DateTime.Now - ctrl.start).TotalMilliseconds;
1156                                 double percent_done = (double)ms_diff % (double)ctrl.MarqueeAnimationSpeed / (double)ctrl.MarqueeAnimationSpeed;
1157                                 max_blocks = 5;
1158                                 start_pixel = client_area.X + (int)(client_area.Width * percent_done);
1159                                 goto default;
1160 #endif
1161                         default: // Blocks
1162                                 int block_width = renderer.GetInteger (IntegerProperty.ProgressChunkSize);
1163                                 int first_pixel_outside_filled_area = (int)(((double)(ctrl.Value - ctrl.Minimum) * client_area.Width) / (Math.Max (ctrl.Maximum - ctrl.Minimum, 1))) + client_area.X;
1164                                 int block_count = 0;
1165                                 int increment = block_width + renderer.GetInteger (IntegerProperty.ProgressSpaceSize);
1166                                 Rectangle block_rect = new Rectangle (start_pixel, client_area.Y, block_width, client_area.Height);
1167                                 while (true) {
1168                                         if (max_blocks != int.MaxValue) {
1169                                                 if (block_count == max_blocks)
1170                                                         break;
1171                                                 if (block_rect.Right >= client_area.Width)
1172                                                         block_rect.X -= client_area.Width;
1173                                         } else {
1174                                                 if (block_rect.X >= first_pixel_outside_filled_area)
1175                                                         break;
1176                                                 if (block_rect.Right >= first_pixel_outside_filled_area)
1177                                                         if (first_pixel_outside_filled_area == client_area.Right)
1178                                                                 block_rect.Width = first_pixel_outside_filled_area - block_rect.X;
1179                                                         else
1180                                                                 break;
1181                                         }
1182                                         if (clip_rect.IntersectsWith (block_rect))
1183                                                 renderer.DrawBackground (dc, block_rect, clip_rect);
1184                                         block_rect.X += increment;
1185                                         block_count++;
1186                                 }
1187                                 break;
1188                         }
1189                 }
1190                 #endregion
1191                 #region RadioButton
1192                 protected override void RadioButton_DrawButton (RadioButton radio_button, Graphics dc, ButtonState state, Rectangle radiobutton_rectangle) {
1193                         if (radio_button.Appearance == Appearance.Normal && radio_button.FlatStyle == FlatStyle.System) {
1194                                 RadioButtonRenderer.DrawRadioButton (
1195                                         dc,
1196                                         new Point (radiobutton_rectangle.Left, radiobutton_rectangle.Top),
1197                                         GetRadioButtonState (radio_button)
1198                                 );
1199                                 return;
1200                         }
1201                         base.RadioButton_DrawButton(radio_button, dc, state, radiobutton_rectangle);
1202                 }
1203                 static RadioButtonState GetRadioButtonState (RadioButton checkBox)
1204                 {
1205                         if (checkBox.Checked) {
1206                                 if (!checkBox.Enabled)
1207                                         return RadioButtonState.CheckedDisabled;
1208                                 else if (checkBox.Pressed)
1209                                         return RadioButtonState.CheckedPressed;
1210                                 else if (checkBox.Entered)
1211                                         return RadioButtonState.CheckedHot;
1212                                 return RadioButtonState.CheckedNormal;
1213                         } else {
1214                                 if (!checkBox.Enabled)
1215                                         return RadioButtonState.UncheckedDisabled;
1216                                 else if (checkBox.Pressed)
1217                                         return RadioButtonState.UncheckedPressed;
1218                                 else if (checkBox.Entered)
1219                                         return RadioButtonState.UncheckedHot;
1220                                 return RadioButtonState.UncheckedNormal;
1221                         }
1222                 }
1223                 #endregion
1224                 #region ScrollBar
1225                 public override void DrawScrollBar (Graphics dc, Rectangle clip, ScrollBar bar)
1226                 {
1227                         if (!RenderClientAreas ||
1228                                 !ScrollBarAreElementsDefined) {
1229                                 base.DrawScrollBar (dc, clip, bar);
1230                                 return;
1231                         }
1232                         VisualStyleElement element;
1233                         VisualStyleRenderer renderer;
1234                         int scroll_button_width = bar.scrollbutton_width;
1235                         int scroll_button_height = bar.scrollbutton_height;
1236                         if (bar.vert) {
1237                                 bar.FirstArrowArea = new Rectangle (0, 0, bar.Width, scroll_button_height);
1238                                 bar.SecondArrowArea = new Rectangle (
1239                                         0,
1240                                         bar.ClientRectangle.Height - scroll_button_height,
1241                                         bar.Width,
1242                                         scroll_button_height);
1243                                 Rectangle thumb_pos = bar.ThumbPos;
1244                                 thumb_pos.Width = bar.Width;
1245                                 bar.ThumbPos = thumb_pos;
1246                                 #region Background, upper track
1247                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
1248                                         element = VisualStyleElement.ScrollBar.LowerTrackVertical.Pressed;
1249                                 else
1250                                         element = bar.Enabled ?
1251                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Normal :
1252                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
1253                                 renderer = new VisualStyleRenderer (element);
1254                                 Rectangle upper_track_rect = new Rectangle (
1255                                         0,
1256                                         0,
1257                                         bar.ClientRectangle.Width,
1258                                         bar.ThumbPos.Top);
1259                                 if (clip.IntersectsWith (upper_track_rect))
1260                                         renderer.DrawBackground (dc, upper_track_rect, clip);
1261                                 #endregion
1262                                 #region Background, lower track
1263                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
1264                                         element = VisualStyles.VisualStyleElement.ScrollBar.LowerTrackVertical.Pressed;
1265                                 else
1266                                         element = bar.Enabled ?
1267                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Normal :
1268                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
1269                                 renderer = new VisualStyleRenderer (element);
1270                                 Rectangle lower_track_rect = new Rectangle (
1271                                         0,
1272                                         bar.ThumbPos.Bottom,
1273                                         bar.ClientRectangle.Width,
1274                                         bar.ClientRectangle.Height - bar.ThumbPos.Bottom);
1275                                 if (clip.IntersectsWith (lower_track_rect))
1276                                         renderer.DrawBackground (dc, lower_track_rect, clip);
1277                                 #endregion
1278                                 #region Buttons
1279                                 if (clip.IntersectsWith (bar.FirstArrowArea)) {
1280                                         if (!bar.Enabled)
1281                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpDisabled;
1282                                         else if (bar.firstbutton_state == ButtonState.Pushed)
1283                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpPressed;
1284                                         else if (bar.FirstButtonEntered)
1285                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpHot;
1286                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1287                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpHover;
1288                                         else
1289                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpNormal;
1290                                         renderer = new VisualStyleRenderer (element);
1291                                         renderer.DrawBackground (dc, bar.FirstArrowArea);
1292                                 }
1293                                 if (clip.IntersectsWith (bar.SecondArrowArea)) {
1294                                         if (!bar.Enabled)
1295                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownDisabled;
1296                                         else if (bar.secondbutton_state == ButtonState.Pushed)
1297                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownPressed;
1298                                         else if (bar.SecondButtonEntered)
1299                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownHot;
1300                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1301                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownHover;
1302                                         else
1303                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownNormal;
1304                                         renderer = new VisualStyleRenderer (element);
1305                                         renderer.DrawBackground (dc, bar.SecondArrowArea);
1306                                 }
1307                                 #endregion
1308                                 #region Thumb and grip
1309                                 if (!bar.Enabled)
1310                                         element = VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
1311                                 else if (bar.ThumbPressed)
1312                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Pressed;
1313                                 else if (bar.ThumbEntered)
1314                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Hot;
1315                                 else
1316                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Normal;
1317                                 renderer = new VisualStyleRenderer (element);
1318                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
1319
1320                                 if (bar.Enabled && bar.ThumbPos.Height >= 20) {
1321                                         element = VisualStyleElement.ScrollBar.GripperVertical.Normal;
1322                                         if (VisualStyleRenderer.IsElementDefined (element)) {
1323                                                 renderer = new VisualStyleRenderer (element);
1324                                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
1325                                         }
1326                                 }
1327                                 #endregion
1328                         } else {
1329                                 bar.FirstArrowArea = new Rectangle (0, 0, scroll_button_width, bar.Height);
1330                                 bar.SecondArrowArea = new Rectangle (
1331                                         bar.ClientRectangle.Width - scroll_button_width,
1332                                         0,
1333                                         scroll_button_width,
1334                                         bar.Height);
1335                                 Rectangle thumb_pos = bar.ThumbPos;
1336                                 thumb_pos.Height = bar.Height;
1337                                 bar.ThumbPos = thumb_pos;
1338                                 #region Background, left track
1339                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
1340                                         element = VisualStyleElement.ScrollBar.LeftTrackHorizontal.Pressed;
1341                                 else
1342                                         element = bar.Enabled ?
1343                                                 VisualStyleElement.ScrollBar.LeftTrackHorizontal.Normal :
1344                                                 VisualStyleElement.ScrollBar.LeftTrackHorizontal.Disabled;
1345                                 renderer = new VisualStyleRenderer (element);
1346                                 Rectangle left_track_rect = new Rectangle (
1347                                         0,
1348                                         0,
1349                                         bar.ThumbPos.Left,
1350                                         bar.ClientRectangle.Height);
1351                                 if (clip.IntersectsWith (left_track_rect))
1352                                         renderer.DrawBackground (dc, left_track_rect, clip);
1353                                 #endregion
1354                                 #region Background, right track
1355                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
1356                                         element = VisualStyleElement.ScrollBar.RightTrackHorizontal.Pressed;
1357                                 else
1358                                         element = bar.Enabled ?
1359                                                 VisualStyleElement.ScrollBar.RightTrackHorizontal.Normal :
1360                                                 VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled;
1361                                 renderer = new VisualStyleRenderer (element);
1362                                 Rectangle right_track_rect = new Rectangle (
1363                                         bar.ThumbPos.Right,
1364                                         0,
1365                                         bar.ClientRectangle.Width - bar.ThumbPos.Right,
1366                                         bar.ClientRectangle.Height);
1367                                 if (clip.IntersectsWith (right_track_rect))
1368                                         renderer.DrawBackground (dc, right_track_rect, clip);
1369                                 #endregion
1370                                 #region Buttons
1371                                 if (clip.IntersectsWith (bar.FirstArrowArea)) {
1372                                         if (!bar.Enabled)
1373                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftDisabled;
1374                                         else if (bar.firstbutton_state == ButtonState.Pushed)
1375                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
1376                                         else if (bar.FirstButtonEntered)
1377                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftHot;
1378                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1379                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftHover;
1380                                         else
1381                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
1382                                         renderer = new VisualStyleRenderer (element);
1383                                         renderer.DrawBackground (dc, bar.FirstArrowArea);
1384                                 }
1385                                 if (clip.IntersectsWith (bar.SecondArrowArea)) {
1386                                         if (!bar.Enabled)
1387                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightDisabled;
1388                                         else if (bar.secondbutton_state == ButtonState.Pushed)
1389                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
1390                                         else if (bar.SecondButtonEntered)
1391                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightHot;
1392                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1393                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightHover;
1394                                         else
1395                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
1396                                         renderer = new VisualStyleRenderer (element);
1397                                         renderer.DrawBackground (dc, bar.SecondArrowArea);
1398                                 }
1399                                 #endregion
1400                                 #region Thumb and grip
1401                                 if (!bar.Enabled)
1402                                         element = VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled;
1403                                 else if (bar.ThumbPressed)
1404                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Pressed;
1405                                 else if (bar.ThumbEntered)
1406                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Hot;
1407                                 else
1408                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Normal;
1409                                 renderer = new VisualStyleRenderer (element);
1410                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
1411
1412                                 if (bar.Enabled && bar.ThumbPos.Height >= 20) {
1413                                         element = VisualStyleElement.ScrollBar.GripperHorizontal.Normal;
1414                                         if (VisualStyleRenderer.IsElementDefined (element)) {
1415                                                 renderer = new VisualStyleRenderer (element);
1416                                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
1417                                         }
1418                                 }
1419                                 #endregion
1420                         }
1421                 }
1422                 public override bool ScrollBarHasHotElementStyles {
1423                         get {
1424                                 if (!RenderClientAreas)
1425                                         return base.ScrollBarHasHotElementStyles;
1426                                 return ScrollBarAreElementsDefined;
1427                         }
1428                 }
1429                 public override bool ScrollBarHasPressedThumbStyle {
1430                         get {
1431                                 if (!RenderClientAreas)
1432                                         return base.ScrollBarHasPressedThumbStyle;
1433                                 return ScrollBarAreElementsDefined;
1434                         }
1435                 }
1436                 const int WindowsVistaMajorVersion = 6;
1437                 static bool ScrollBarHasHoverArrowButtonStyleVisualStyles =
1438                         Environment.OSVersion.Version.Major >= WindowsVistaMajorVersion;
1439                 public override bool ScrollBarHasHoverArrowButtonStyle {
1440                         get {
1441                                 if (RenderClientAreas &&
1442                                         ScrollBarHasHoverArrowButtonStyleVisualStyles)
1443                                         return ScrollBarAreElementsDefined;
1444                                 return base.ScrollBarHasHoverArrowButtonStyle;
1445                         }
1446                 }
1447                 static bool ScrollBarAreElementsDefined {
1448                         get {
1449                                 return
1450                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ArrowButton.DownDisabled) &&
1451                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.LeftTrackHorizontal.Disabled) &&
1452                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled) &&
1453                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled) &&
1454                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Disabled) &&
1455                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ThumbButtonVertical.Disabled) &&
1456                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.UpperTrackVertical.Disabled);
1457                         }
1458                 }
1459                 #endregion
1460                 #region StatusBar
1461                 protected override void DrawStatusBarBackground(Graphics dc, Rectangle clip, StatusBar sb) {
1462                         if (!RenderClientAreas) {
1463                                 base.DrawStatusBarBackground (dc, clip, sb);
1464                                 return;
1465                         }
1466                         VisualStyleElement element = VisualStyleElement.Status.Bar.Normal;
1467                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1468                                 base.DrawStatusBarBackground (dc, clip, sb);
1469                                 return;
1470                         }
1471                         new VisualStyleRenderer (element).DrawBackground (dc, sb.ClientRectangle, clip);
1472                 }
1473                 protected override void DrawStatusBarSizingGrip (Graphics dc, Rectangle clip, StatusBar sb, Rectangle area)
1474                 {
1475                         if (!RenderClientAreas) {
1476                                 base.DrawStatusBarSizingGrip (dc, clip, sb, area);
1477                                 return;
1478                         }
1479                         VisualStyleElement element = VisualStyleElement.Status.Gripper.Normal;
1480                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1481                                 base.DrawStatusBarSizingGrip (dc, clip, sb, area);
1482                                 return;
1483                         }
1484                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1485                         Rectangle sizing_grip_rectangle = new Rectangle (Point.Empty, renderer.GetPartSize (dc, ThemeSizeType.True));
1486                         sizing_grip_rectangle.X = sb.Width - sizing_grip_rectangle.Width;
1487                         sizing_grip_rectangle.Y = sb.Height - sizing_grip_rectangle.Height;
1488                         renderer.DrawBackground (dc, sizing_grip_rectangle, clip);
1489                 }
1490                 protected override void DrawStatusBarPanelBackground (Graphics dc, Rectangle area, StatusBarPanel panel)
1491                 {
1492                         if (!RenderClientAreas) {
1493                                 base.DrawStatusBarPanelBackground (dc, area, panel);
1494                                 return;
1495                         }
1496                         VisualStyleElement element = VisualStyleElement.Status.Pane.Normal;
1497                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1498                                 base.DrawStatusBarPanelBackground (dc, area, panel);
1499                                 return;
1500                         }
1501                         new VisualStyleRenderer (element).DrawBackground (dc, area);
1502                 }
1503                 #endregion
1504                 #region TextBoxBase
1505                 static bool TextBoxBaseShouldPaint (TextBoxBase textBoxBase)
1506                 {
1507                         return textBoxBase.BorderStyle == BorderStyle.Fixed3D;
1508                 }
1509                 static VisualStyleElement TextBoxBaseGetVisualStyleElement (TextBoxBase textBoxBase)
1510                 {
1511                         if (!textBoxBase.Enabled)
1512                                 return VisualStyleElement.TextBox.TextEdit.Disabled;
1513                         if (textBoxBase.ReadOnly)
1514                                 return VisualStyleElement.TextBox.TextEdit.ReadOnly;
1515                         if (textBoxBase.Entered)
1516                                 return VisualStyleElement.TextBox.TextEdit.Hot;
1517                         if (textBoxBase.Focused)
1518                                 return VisualStyleElement.TextBox.TextEdit.Focused;
1519                         return VisualStyleElement.TextBox.TextEdit.Normal;
1520                 }
1521                 public override void TextBoxBaseFillBackground (TextBoxBase textBoxBase, Graphics g, Rectangle clippingArea)
1522                 {
1523                         if (!RenderClientAreas ||
1524                                 !TextBoxBaseShouldPaint (textBoxBase)) {
1525                                 base.TextBoxBaseFillBackground (textBoxBase, g, clippingArea);
1526                                 return;
1527                         }
1528                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1529                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1530                                 base.TextBoxBaseFillBackground (textBoxBase, g, clippingArea);
1531                                 return;
1532                         }
1533                         Rectangle bounds = new Rectangle(Point.Empty, textBoxBase.Size);
1534                         bounds.X -= (bounds.Width - textBoxBase.ClientSize.Width) / 2;
1535                         bounds.Y -= (bounds.Height - textBoxBase.ClientSize.Height) / 2;
1536                         new VisualStyleRenderer (element).DrawBackground (g, bounds, clippingArea);
1537                 }
1538                 public override bool TextBoxBaseHandleWmNcPaint (TextBoxBase textBoxBase, ref Message m)
1539                 {
1540                         if (!RenderClientAreas ||
1541                                 !TextBoxBaseShouldPaint (textBoxBase))
1542                                 return base.TextBoxBaseHandleWmNcPaint (textBoxBase, ref m);
1543                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1544                         if (!VisualStyleRenderer.IsElementDefined (element))
1545                                 return base.TextBoxBaseHandleWmNcPaint (textBoxBase, ref m);
1546                         PaintEventArgs e = XplatUI.PaintEventStart (ref m, textBoxBase.Handle, false);
1547                         new VisualStyleRenderer (element).DrawBackgroundExcludingArea (
1548                                 e.Graphics,
1549                                 new Rectangle (Point.Empty, textBoxBase.Size),
1550                                 new Rectangle (new Point ((textBoxBase.Width - textBoxBase.ClientSize.Width) / 2,
1551                                         (textBoxBase.Height - textBoxBase.ClientSize.Height) / 2),
1552                                         textBoxBase.ClientSize));
1553                         XplatUI.PaintEventEnd (ref m, textBoxBase.Handle, false);
1554                         return true;
1555                 }
1556                 public override bool TextBoxBaseShouldPaintBackground (TextBoxBase textBoxBase)
1557                 {
1558                         if (!RenderClientAreas ||
1559                                 !TextBoxBaseShouldPaint (textBoxBase))
1560                                 return base.TextBoxBaseShouldPaintBackground (textBoxBase);
1561                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1562                         if (!VisualStyleRenderer.IsElementDefined (element))
1563                                 return base.TextBoxBaseShouldPaintBackground (textBoxBase);
1564                         return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
1565                 }
1566                 #endregion
1567                 #region ToolBar
1568                 static bool ToolBarIsDisabled (ToolBarItem item)
1569                 {
1570                         return !item.Button.Enabled;
1571                 }
1572                 static bool ToolBarIsPressed (ToolBarItem item)
1573                 {
1574                         return item.Pressed;
1575                 }
1576                 static bool ToolBarIsChecked (ToolBarItem item)
1577                 {
1578                         return item.Button.Pushed;
1579                 }
1580                 static bool ToolBarIsHot (ToolBarItem item)
1581                 {
1582                         return item.Hilight;
1583                 }
1584                 #region Border
1585                 protected override void DrawToolBarButtonBorder (Graphics dc, ToolBarItem item, bool is_flat)
1586                 {
1587                         if (!RenderClientAreas) {
1588                                 base.DrawToolBarButtonBorder (dc, item, is_flat);
1589                                 return;
1590                         }
1591                         if (item.Button.Style == ToolBarButtonStyle.Separator)
1592                                 return;
1593                         VisualStyleElement element;
1594                         if (item.Button.Style == ToolBarButtonStyle.DropDownButton)
1595                                 element = ToolBarGetDropDownButtonVisualStyleElement (item);
1596                         else
1597                                 element = ToolBarGetButtonVisualStyleElement (item);
1598                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1599                                 base.DrawToolBarButtonBorder (dc, item, is_flat);
1600                                 return;
1601                         }
1602                         Rectangle rectangle = item.Rectangle;
1603                         if (item.Button.Style == ToolBarButtonStyle.DropDownButton && item.Button.Parent.DropDownArrows)
1604                                 rectangle.Width -= ToolBarDropDownWidth;
1605                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
1606                 }
1607                 private static VisualStyleElement ToolBarGetDropDownButtonVisualStyleElement (ToolBarItem item)
1608                 {
1609                         if (item.Button.Parent.DropDownArrows) {
1610                                 if (ToolBarIsDisabled (item))
1611                                         return VisualStyleElement.ToolBar.SplitButton.Disabled;
1612                                 if (ToolBarIsPressed (item))
1613                                         return VisualStyleElement.ToolBar.SplitButton.Pressed;
1614                                 if (ToolBarIsChecked (item))
1615                                         if (ToolBarIsHot (item))
1616                                                 return VisualStyleElement.ToolBar.SplitButton.HotChecked;
1617                                         else
1618                                                 return VisualStyleElement.ToolBar.SplitButton.Checked;
1619                                 if (ToolBarIsHot (item))
1620                                         return VisualStyleElement.ToolBar.SplitButton.Hot;
1621                                 return VisualStyleElement.ToolBar.SplitButton.Normal;
1622                         } else {
1623                                 if (ToolBarIsDisabled (item))
1624                                         return VisualStyleElement.ToolBar.DropDownButton.Disabled;
1625                                 if (ToolBarIsPressed (item))
1626                                         return VisualStyleElement.ToolBar.DropDownButton.Pressed;
1627                                 if (ToolBarIsChecked (item))
1628                                         if (ToolBarIsHot (item))
1629                                                 return VisualStyleElement.ToolBar.DropDownButton.HotChecked;
1630                                         else
1631                                                 return VisualStyleElement.ToolBar.DropDownButton.Checked;
1632                                 if (ToolBarIsHot (item))
1633                                         return VisualStyleElement.ToolBar.DropDownButton.Hot;
1634                                 return VisualStyleElement.ToolBar.DropDownButton.Normal;
1635                         }
1636                 }
1637                 private static VisualStyleElement ToolBarGetButtonVisualStyleElement (ToolBarItem item)
1638                 {
1639                         if (ToolBarIsDisabled (item))
1640                                 return VisualStyleElement.ToolBar.Button.Disabled;
1641                         if (ToolBarIsPressed (item))
1642                                 return VisualStyleElement.ToolBar.Button.Pressed;
1643                         if (ToolBarIsChecked (item))
1644                                 if (ToolBarIsHot (item))
1645                                         return VisualStyleElement.ToolBar.Button.HotChecked;
1646                                 else
1647                                         return VisualStyleElement.ToolBar.Button.Checked;
1648                         if (ToolBarIsHot (item))
1649                                 return VisualStyleElement.ToolBar.Button.Hot;
1650                         return VisualStyleElement.ToolBar.Button.Normal;
1651                 }
1652                 #endregion
1653                 #region Separator
1654                 protected override void DrawToolBarSeparator (Graphics dc, ToolBarItem item)
1655                 {
1656                         if (!RenderClientAreas) {
1657                                 base.DrawToolBarSeparator (dc, item);
1658                                 return;
1659                         }
1660                         VisualStyleElement element = ToolBarGetSeparatorVisualStyleElement (item);
1661                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1662                                 base.DrawToolBarSeparator (dc, item);
1663                                 return;
1664                         }
1665                         new VisualStyleRenderer (element).DrawBackground (dc, item.Rectangle);
1666                 }
1667                 static VisualStyleElement ToolBarGetSeparatorVisualStyleElement (ToolBarItem toolBarItem)
1668                 {
1669                         return toolBarItem.Button.Parent.Vertical ?
1670                                 VisualStyleElement.ToolBar.SeparatorVertical.Normal :
1671                                 VisualStyleElement.ToolBar.SeparatorHorizontal.Normal;
1672                 }
1673                 #endregion
1674                 #region Toggle button background
1675                 protected override void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarItem item)
1676                 {
1677                         if (!RenderClientAreas ||
1678                                 !VisualStyleRenderer.IsElementDefined (ToolBarGetButtonVisualStyleElement (item)))
1679                                 base.DrawToolBarToggleButtonBackground (dc, item);
1680                 }
1681                 #endregion
1682                 #region Drop down arrow
1683                 protected override void DrawToolBarDropDownArrow (Graphics dc, ToolBarItem item, bool is_flat)
1684                 {
1685                         if (!RenderClientAreas) {
1686                                 base.DrawToolBarDropDownArrow (dc, item, is_flat);
1687                                 return;
1688                         }
1689                         VisualStyleElement element = ToolBarGetDropDownArrowVisualStyleElement (item);
1690                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1691                                 base.DrawToolBarDropDownArrow (dc, item, is_flat);
1692                                 return;
1693                         }
1694                         Rectangle rect = item.Rectangle;
1695                         rect.X = item.Rectangle.Right - ToolBarDropDownWidth;
1696                         rect.Width = ToolBarDropDownWidth;
1697                         new VisualStyleRenderer (element).DrawBackground (dc, rect);
1698                 }
1699                 private static VisualStyleElement ToolBarGetDropDownArrowVisualStyleElement (ToolBarItem item)
1700                 {
1701                         if (ToolBarIsDisabled (item))
1702                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Disabled;
1703                         if (ToolBarIsPressed (item))
1704                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Pressed;
1705                         if (ToolBarIsChecked (item))
1706                                 if (ToolBarIsHot (item))
1707                                         return VisualStyleElement.ToolBar.SplitButtonDropDown.HotChecked;
1708                                 else
1709                                         return VisualStyleElement.ToolBar.SplitButtonDropDown.Checked;
1710                         if (ToolBarIsHot (item))
1711                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Hot;
1712                         return VisualStyleElement.ToolBar.SplitButtonDropDown.Normal;
1713                 }
1714                 #endregion
1715                 public override bool ToolBarHasHotElementStyles (ToolBar toolBar)
1716                 {
1717                         if (!RenderClientAreas)
1718                                 return base.ToolBarHasHotElementStyles (toolBar);
1719                         return true;
1720                 }
1721                 public override bool ToolBarHasHotCheckedElementStyles {
1722                         get {
1723                                 if (!RenderClientAreas)
1724                                         return base.ToolBarHasHotCheckedElementStyles;
1725                                 return true;
1726                         }
1727                 }
1728                 #endregion
1729                 #region ToolTip
1730                 protected override void ToolTipDrawBackground (Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control)
1731                 {
1732                         if (!RenderClientAreas) {
1733                                 base.ToolTipDrawBackground (dc, clip_rectangle, control);
1734                                 return;
1735                         }
1736                         VisualStyleElement element = VisualStyleElement.ToolTip.Standard.Normal;
1737                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1738                                 base.ToolTipDrawBackground (dc, clip_rectangle, control);
1739                                 return;
1740                         }
1741                         new VisualStyleRenderer (element).DrawBackground (dc, control.Bounds);
1742                 }
1743                 public override bool ToolTipTransparentBackground {
1744                         get {
1745                                 if (!RenderClientAreas)
1746                                         return base.ToolTipTransparentBackground;
1747                                 VisualStyleElement element = VisualStyleElement.ToolTip.Standard.Normal;
1748                                 if (!VisualStyleRenderer.IsElementDefined (element))
1749                                         return base.ToolTipTransparentBackground;
1750                                 return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
1751                         }
1752                 }
1753                 #endregion
1754                 #region TrackBar
1755                 protected override Size TrackBarGetThumbSize (TrackBar trackBar)
1756                 {
1757                         if (!RenderClientAreas)
1758                                 return base.TrackBarGetThumbSize (trackBar);
1759                         VisualStyleElement element = TrackBarGetThumbVisualStyleElement (trackBar);
1760                         if (!VisualStyleRenderer.IsElementDefined (element))
1761                                 return base.TrackBarGetThumbSize (trackBar);
1762                         Graphics g = trackBar.CreateGraphics ();
1763                         Size result = new VisualStyleRenderer (element).GetPartSize (g, ThemeSizeType.True);
1764                         g.Dispose ();
1765                         return trackBar.Orientation == Orientation.Horizontal ? result : TrackBarRotateVerticalThumbSize (result);
1766                 }
1767                 static VisualStyleElement TrackBarGetThumbVisualStyleElement (TrackBar trackBar)
1768                 {
1769                         if (trackBar.Orientation == Orientation.Horizontal)
1770                                 switch (trackBar.TickStyle) {
1771                                 case TickStyle.BottomRight:
1772                                 case TickStyle.None:
1773                                         return TrackBarGetHorizontalThumbBottomVisualStyleElement (trackBar);
1774                                 case TickStyle.TopLeft:
1775                                         return TrackBarGetHorizontalThumbTopVisualStyleElement (trackBar);
1776                                 default:
1777                                         return TrackBarGetHorizontalThumbVisualStyleElement (trackBar);
1778                                 }
1779                         else
1780                                 switch (trackBar.TickStyle) {
1781                                 case TickStyle.BottomRight:
1782                                 case TickStyle.None:
1783                                         return TrackBarGetVerticalThumbRightVisualStyleElement (trackBar);
1784                                 case TickStyle.TopLeft:
1785                                         return TrackBarGetVerticalThumbLeftVisualStyleElement (trackBar);
1786                                 default:
1787                                         return TrackBarGetVerticalThumbVisualStyleElement (trackBar);
1788                                 }
1789                 }
1790                 static Size TrackBarRotateVerticalThumbSize (Size value)
1791                 {
1792                         int temporary = value.Width;
1793                         value.Width = value.Height;
1794                         value.Height = temporary;
1795                         return value;
1796                 }
1797                 #region Track
1798                 protected override void TrackBarDrawHorizontalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
1799                 {
1800                         if (!RenderClientAreas) {
1801                                 base.TrackBarDrawHorizontalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1802                                 return;
1803                         }
1804                         VisualStyleElement element = VisualStyleElement.TrackBar.Track.Normal;
1805                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1806                                 base.TrackBarDrawHorizontalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1807                                 return;
1808                         }
1809                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1810                         renderer.DrawBackground (dc, new Rectangle (channel_startpoint, new Size (thumb_area.Width, renderer.GetPartSize (dc, ThemeSizeType.True).Height)), clippingArea);
1811                 }
1812                 protected override void TrackBarDrawVerticalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
1813                 {
1814                         if (!RenderClientAreas) {
1815                                 base.TrackBarDrawVerticalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1816                                 return;
1817                         }
1818                         VisualStyleElement element = VisualStyleElement.TrackBar.TrackVertical.Normal;
1819                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1820                                 base.TrackBarDrawVerticalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1821                                 return;
1822                         }
1823                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1824                         renderer.DrawBackground (dc, new Rectangle (channel_startpoint, new Size (renderer.GetPartSize (dc, ThemeSizeType.True).Width, thumb_area.Height)), clippingArea);
1825                 }
1826                 #endregion
1827                 #region Thumb
1828                 static bool TrackBarIsDisabled (TrackBar trackBar)
1829                 {
1830                         return !trackBar.Enabled;
1831                 }
1832                 static bool TrackBarIsHot (TrackBar trackBar)
1833                 {
1834                         return trackBar.ThumbEntered;
1835                 }
1836                 static bool TrackBarIsPressed (TrackBar trackBar)
1837                 {
1838                         return trackBar.thumb_pressed;
1839                 }
1840                 static bool TrackBarIsFocused (TrackBar trackBar)
1841                 {
1842                         return trackBar.Focused;
1843                 }
1844                 #region Horizontal
1845                 protected override void TrackBarDrawHorizontalThumbBottom (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1846                 {
1847                         if (!RenderClientAreas) {
1848                                 base.TrackBarDrawHorizontalThumbBottom (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1849                                 return;
1850                         }
1851                         VisualStyleElement element = TrackBarGetHorizontalThumbBottomVisualStyleElement (trackBar);
1852                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1853                                 base.TrackBarDrawHorizontalThumbBottom (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1854                                 return;
1855                         }
1856                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1857                 }
1858                 static VisualStyleElement TrackBarGetHorizontalThumbBottomVisualStyleElement (TrackBar trackBar)
1859                 {
1860                         if (TrackBarIsDisabled (trackBar))
1861                                 return VisualStyleElement.TrackBar.ThumbBottom.Disabled;
1862                         else if (TrackBarIsPressed (trackBar))
1863                                 return VisualStyleElement.TrackBar.ThumbBottom.Pressed;
1864                         else if (TrackBarIsHot (trackBar))
1865                                 return VisualStyleElement.TrackBar.ThumbBottom.Hot;
1866                         else if (TrackBarIsFocused (trackBar))
1867                                 return VisualStyleElement.TrackBar.ThumbBottom.Focused;
1868                         return VisualStyleElement.TrackBar.ThumbBottom.Normal;
1869                 }
1870                 protected override void TrackBarDrawHorizontalThumbTop (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1871                 {
1872                         if (!RenderClientAreas) {
1873                                 base.TrackBarDrawHorizontalThumbTop (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1874                                 return;
1875                         }
1876                         VisualStyleElement element = TrackBarGetHorizontalThumbTopVisualStyleElement (trackBar);
1877                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1878                                 base.TrackBarDrawHorizontalThumbTop (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1879                                 return;
1880                         }
1881                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1882                 }
1883                 static VisualStyleElement TrackBarGetHorizontalThumbTopVisualStyleElement (TrackBar trackBar)
1884                 {
1885                         if (TrackBarIsDisabled (trackBar))
1886                                 return VisualStyleElement.TrackBar.ThumbTop.Disabled;
1887                         else if (TrackBarIsPressed (trackBar))
1888                                 return VisualStyleElement.TrackBar.ThumbTop.Pressed;
1889                         else if (TrackBarIsHot (trackBar))
1890                                 return VisualStyleElement.TrackBar.ThumbTop.Hot;
1891                         else if (TrackBarIsFocused (trackBar))
1892                                 return VisualStyleElement.TrackBar.ThumbTop.Focused;
1893                         return VisualStyleElement.TrackBar.ThumbTop.Normal;
1894                 }
1895                 protected override void TrackBarDrawHorizontalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1896                 {
1897                         if (!RenderClientAreas) {
1898                                 base.TrackBarDrawHorizontalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1899                                 return;
1900                         }
1901                         VisualStyleElement element = TrackBarGetHorizontalThumbVisualStyleElement (trackBar);
1902                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1903                                 base.TrackBarDrawHorizontalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1904                                 return;
1905                         }
1906                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1907                 }
1908                 static VisualStyleElement TrackBarGetHorizontalThumbVisualStyleElement (TrackBar trackBar)
1909                 {
1910                         if (TrackBarIsDisabled (trackBar))
1911                                 return VisualStyleElement.TrackBar.Thumb.Disabled;
1912                         else if (TrackBarIsPressed (trackBar))
1913                                 return VisualStyleElement.TrackBar.Thumb.Pressed;
1914                         else if (TrackBarIsHot (trackBar))
1915                                 return VisualStyleElement.TrackBar.Thumb.Hot;
1916                         else if (TrackBarIsFocused (trackBar))
1917                                 return VisualStyleElement.TrackBar.Thumb.Focused;
1918                         return VisualStyleElement.TrackBar.Thumb.Normal;
1919                 }
1920                 #endregion
1921                 #region Vertical
1922                 static Rectangle TrackBarRotateVerticalThumbSize (Rectangle value)
1923                 {
1924                         int temporary = value.Width;
1925                         value.Width = value.Height;
1926                         value.Height = temporary;
1927                         return value;
1928                 }
1929                 protected override void TrackBarDrawVerticalThumbRight (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1930                 {
1931                         if (!RenderClientAreas) {
1932                                 base.TrackBarDrawVerticalThumbRight (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1933                                 return;
1934                         }
1935                         VisualStyleElement element = TrackBarGetVerticalThumbRightVisualStyleElement (trackBar);
1936                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1937                                 base.TrackBarDrawVerticalThumbRight (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1938                                 return;
1939                         }
1940                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1941                 }
1942                 static VisualStyleElement TrackBarGetVerticalThumbRightVisualStyleElement (TrackBar trackBar)
1943                 {
1944                         if (TrackBarIsDisabled (trackBar))
1945                                 return VisualStyleElement.TrackBar.ThumbRight.Disabled;
1946                         else if (TrackBarIsPressed (trackBar))
1947                                 return VisualStyleElement.TrackBar.ThumbRight.Pressed;
1948                         else if (TrackBarIsHot (trackBar))
1949                                 return VisualStyleElement.TrackBar.ThumbRight.Hot;
1950                         else if (TrackBarIsFocused (trackBar))
1951                                 return VisualStyleElement.TrackBar.ThumbRight.Focused;
1952                         return VisualStyleElement.TrackBar.ThumbRight.Normal;
1953                 }
1954                 protected override void TrackBarDrawVerticalThumbLeft (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1955                 {
1956                         if (!RenderClientAreas) {
1957                                 base.TrackBarDrawVerticalThumbLeft (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1958                                 return;
1959                         }
1960                         VisualStyleElement element = TrackBarGetVerticalThumbLeftVisualStyleElement (trackBar);
1961                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1962                                 base.TrackBarDrawVerticalThumbLeft (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1963                                 return;
1964                         }
1965                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1966                 }
1967                 static VisualStyleElement TrackBarGetVerticalThumbLeftVisualStyleElement (TrackBar trackBar)
1968                 {
1969                         if (TrackBarIsDisabled (trackBar))
1970                                 return VisualStyleElement.TrackBar.ThumbLeft.Disabled;
1971                         else if (TrackBarIsPressed (trackBar))
1972                                 return VisualStyleElement.TrackBar.ThumbLeft.Pressed;
1973                         else if (TrackBarIsHot (trackBar))
1974                                 return VisualStyleElement.TrackBar.ThumbLeft.Hot;
1975                         else if (TrackBarIsFocused (trackBar))
1976                                 return VisualStyleElement.TrackBar.ThumbLeft.Focused;
1977                         return VisualStyleElement.TrackBar.ThumbLeft.Normal;
1978                 }
1979                 protected override void TrackBarDrawVerticalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1980                 {
1981                         if (!RenderClientAreas) {
1982                                 base.TrackBarDrawVerticalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1983                                 return;
1984                         }
1985                         VisualStyleElement element = TrackBarGetVerticalThumbVisualStyleElement (trackBar);
1986                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1987                                 base.TrackBarDrawVerticalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1988                                 return;
1989                         }
1990                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1991                 }
1992                 static VisualStyleElement TrackBarGetVerticalThumbVisualStyleElement (TrackBar trackBar)
1993                 {
1994                         if (TrackBarIsDisabled (trackBar))
1995                                 return VisualStyleElement.TrackBar.ThumbVertical.Disabled;
1996                         else if (TrackBarIsPressed (trackBar))
1997                                 return VisualStyleElement.TrackBar.ThumbVertical.Pressed;
1998                         else if (TrackBarIsHot (trackBar))
1999                                 return VisualStyleElement.TrackBar.ThumbVertical.Hot;
2000                         else if (TrackBarIsFocused (trackBar))
2001                                 return VisualStyleElement.TrackBar.ThumbVertical.Focused;
2002                         return VisualStyleElement.TrackBar.ThumbVertical.Normal;
2003                 }
2004                 #endregion
2005                 #endregion
2006                 #region Ticks
2007                 const EdgeStyle TrackBarTickEdgeStyle = EdgeStyle.Bump;
2008                 const EdgeEffects TrackBarTickEdgeEffects = EdgeEffects.None;
2009                 #region Horizontal
2010                 protected override ITrackBarTickPainter TrackBarGetHorizontalTickPainter (Graphics g)
2011                 {
2012                         if (!RenderClientAreas ||
2013                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.TrackBar.Ticks.Normal))
2014                                 return base.TrackBarGetHorizontalTickPainter (g);
2015                         return new TrackBarHorizontalTickPainter (g);
2016                 }
2017                 class TrackBarHorizontalTickPainter : ITrackBarTickPainter
2018                 {
2019                         readonly Graphics g;
2020                         readonly VisualStyleRenderer renderer;
2021                         public TrackBarHorizontalTickPainter (Graphics g)
2022                         {
2023                                 this.g = g;
2024                                 renderer = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);
2025                         }
2026                         public void Paint (float x1, float y1, float x2, float y2)
2027                         {
2028                                 renderer.DrawEdge (g, new Rectangle (
2029                                         (int)Math.Round (x1),
2030                                         (int)Math.Round (y1),
2031                                         1,
2032                                         (int)Math.Round (y2 - y1) + 1), Edges.Left, TrackBarTickEdgeStyle, TrackBarTickEdgeEffects);
2033                         }
2034                 }
2035                 #endregion
2036                 #region Vertical
2037                 protected override ITrackBarTickPainter TrackBarGetVerticalTickPainter (Graphics g)
2038                 {
2039                         if (!RenderClientAreas ||
2040                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.TrackBar.TicksVertical.Normal))
2041                                 return base.TrackBarGetVerticalTickPainter (g);
2042                         return new TrackBarVerticalTickPainter (g);
2043                 }
2044                 class TrackBarVerticalTickPainter : ITrackBarTickPainter
2045                 {
2046                         readonly Graphics g;
2047                         readonly VisualStyleRenderer renderer;
2048                         public TrackBarVerticalTickPainter (Graphics g)
2049                         {
2050                                 this.g = g;
2051                                 renderer = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);
2052                         }
2053                         public void Paint (float x1, float y1, float x2, float y2)
2054                         {
2055                                 renderer.DrawEdge (g, new Rectangle (
2056                                         (int)Math.Round (x1),
2057                                         (int)Math.Round (y1),
2058                                         (int)Math.Round (x2 - x1) + 1,
2059                                         1), Edges.Top, TrackBarTickEdgeStyle, TrackBarTickEdgeEffects);
2060                         }
2061                 }
2062                 #endregion
2063                 #endregion
2064                 public override bool TrackBarHasHotThumbStyle {
2065                         get {
2066                                 if (!RenderClientAreas)
2067                                         return base.TrackBarHasHotThumbStyle;
2068                                 return true;
2069                         }
2070                 }
2071                 #endregion
2072                 #region TreeView
2073                 [MonoTODO("Use the sizing information provided by the VisualStyles API.")]
2074                 public override void TreeViewDrawNodePlusMinus (TreeView treeView, TreeNode node, Graphics dc, int x, int middle)
2075                 {
2076                         if (!RenderClientAreas) {
2077                                 base.TreeViewDrawNodePlusMinus (treeView, node, dc, x, middle);
2078                                 return;
2079                         }
2080                         VisualStyleElement element = node.IsExpanded ?
2081                                 VisualStyleElement.TreeView.Glyph.Opened : 
2082                                 VisualStyleElement.TreeView.Glyph.Closed;
2083                         if (!VisualStyleRenderer.IsElementDefined (element)) {
2084                                 base.TreeViewDrawNodePlusMinus (treeView, node, dc, x, middle);
2085                                 return;
2086                         }
2087                         new VisualStyleRenderer (element).DrawBackground (dc, new Rectangle (x, middle - 4, 9, 9));
2088                 }
2089                 #endregion
2090                 #region UpDownBase
2091                 public override void UpDownBaseDrawButton (Graphics g, Rectangle bounds, bool top, PushButtonState state)
2092                 {
2093                         if (!RenderClientAreas) {
2094                                 base.UpDownBaseDrawButton (g, bounds, top, state);
2095                                 return;
2096                         }
2097                         VisualStyleElement element;
2098                         if (top)
2099                                 switch (state) {
2100                                 case PushButtonState.Disabled:
2101                                         element = VisualStyleElement.Spin.Up.Disabled;
2102                                         break;
2103                                 case PushButtonState.Pressed:
2104                                         element = VisualStyleElement.Spin.Up.Pressed;
2105                                         break;
2106                                 case PushButtonState.Hot:
2107                                         element = VisualStyleElement.Spin.Up.Hot;
2108                                         break;
2109                                 default:
2110                                         element = VisualStyleElement.Spin.Up.Normal;
2111                                         break;
2112                                 }
2113                         else
2114                                 switch (state) {
2115                                 case PushButtonState.Disabled:
2116                                         element = VisualStyleElement.Spin.Down.Disabled;
2117                                         break;
2118                                 case PushButtonState.Pressed:
2119                                         element = VisualStyleElement.Spin.Down.Pressed;
2120                                         break;
2121                                 case PushButtonState.Hot:
2122                                         element = VisualStyleElement.Spin.Down.Hot;
2123                                         break;
2124                                 default:
2125                                         element = VisualStyleElement.Spin.Down.Normal;
2126                                         break;
2127                                 }
2128                         if (!VisualStyleRenderer.IsElementDefined (element)) {
2129                                 base.UpDownBaseDrawButton (g, bounds, top, state);
2130                                 return;
2131                         }
2132                         new VisualStyleRenderer (element).DrawBackground (g, bounds);
2133                 }
2134                 public override bool UpDownBaseHasHotButtonStyle {
2135                         get {
2136                                 if (!RenderClientAreas)
2137                                         return base.UpDownBaseHasHotButtonStyle;
2138                                 return true;
2139                         }
2140                 }
2141                 #endregion
2142                 #endregion
2143
2144 #if NET_2_0
2145                 static bool AreEqual (VisualStyleElement value1, VisualStyleElement value2)
2146                 {
2147                         return
2148                                 value1.ClassName == value1.ClassName &&
2149                                 value1.Part == value2.Part &&
2150                                 value1.State == value2.State;
2151                 }
2152 #endif
2153                 #region Measurement device context
2154                 static Control control;
2155                 static IDeviceContext GetMeasurementDeviceContext ()
2156                 {
2157                         if (control == null)
2158                                 control = new Control ();
2159                         return control.CreateGraphics ();
2160                 }
2161                 static void ReleaseMeasurementDeviceContext (IDeviceContext dc)
2162                 {
2163                         dc.Dispose ();
2164                 }
2165                 #endregion
2166         }
2167 }