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