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