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