2008-06-05 George Giolfan <georgegiolfan@yahoo.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThemeVisualStyles.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2008 George Giolfan
21 // Copyright (c) 2004-2006 Novell, Inc.
22 //
23 // Authors:
24 //      George Giolfan, georgegiolfan@yahoo.com
25 //      Ernesto Carrea, equistango@gmail.com
26
27 using System.Drawing;
28 using System.Drawing.Drawing2D;
29 using System.Windows.Forms.VisualStyles;
30
31 namespace System.Windows.Forms
32 {
33         /// <summary>
34         /// VisualStyles theme.
35         /// </summary>
36         /// <remarks>
37         /// This theme uses only the managed VisualStyles API.
38         /// To select it, set MONO_THEME to VisualStyles and call <see cref="Application.EnableVisualStyles"/>.
39         /// </remarks>
40         class ThemeVisualStyles : ThemeWin32Classic
41         {
42                 #region ButtonBase
43                 public override void DrawButtonBase (Graphics dc, Rectangle clip_area, ButtonBase button)
44                 {
45                         if (button.FlatStyle == FlatStyle.System) {
46                                 ButtonRenderer.DrawButton (
47                                         dc,
48                                         new Rectangle (Point.Empty, button.Size),
49                                         button.Text,
50                                         button.Font,
51                                         button.TextFormatFlags,
52                                         null,
53                                         Rectangle.Empty,
54                                         ShouldPaintFocusRectagle (button),
55                                         GetPushButtonState (button)
56                                 );
57                                 return;
58                         }
59                         base.DrawButtonBase (dc, clip_area, button);
60                 }
61                 static PushButtonState GetPushButtonState (ButtonBase button)
62                 {
63                         if (!button.Enabled)
64                                 return PushButtonState.Disabled;
65                         if (button.Pressed)
66                                 return PushButtonState.Pressed;
67                         if (button.Entered)
68                                 return PushButtonState.Hot;
69                         if (button.IsDefault || button.Focused || button.paint_as_acceptbutton)
70                                 return PushButtonState.Default;
71                         return PushButtonState.Normal;
72                 }
73                 #endregion
74 #if NET_2_0
75                 #region Button 2.0
76                 public override void DrawButtonBackground (Graphics g, Button button, Rectangle clipArea)
77                 {
78                         if (!button.UseVisualStyleBackColor) {
79                                 base.DrawButtonBackground (g, button, clipArea);
80                                 return;
81                         }
82                         ButtonRenderer.GetPushButtonRenderer (GetPushButtonState (button)).DrawBackground (g, new Rectangle (Point.Empty, button.Size));
83                 }
84                 #endregion
85 #endif
86                 #region CheckBox
87                 protected override void CheckBox_DrawCheckBox (Graphics dc, CheckBox checkbox, ButtonState state, Rectangle checkbox_rectangle)
88                 {
89                         if (checkbox.Appearance == Appearance.Normal && checkbox.FlatStyle == FlatStyle.System) {
90                                 CheckBoxRenderer.DrawCheckBox (
91                                         dc,
92                                         new Point (checkbox_rectangle.Left, checkbox_rectangle.Top),
93                                         GetCheckBoxState (checkbox)
94                                 );
95                                 return;
96                         }
97                         base.CheckBox_DrawCheckBox(dc, checkbox, state, checkbox_rectangle);
98                 }
99                 static CheckBoxState GetCheckBoxState (CheckBox checkBox)
100                 {
101                         switch (checkBox.CheckState) {
102                         case CheckState.Checked:
103                                 if (!checkBox.Enabled)
104                                         return CheckBoxState.CheckedDisabled;
105                                 else if (checkBox.Pressed)
106                                         return CheckBoxState.CheckedPressed;
107                                 else if (checkBox.Entered)
108                                         return CheckBoxState.CheckedHot;
109                                 return CheckBoxState.CheckedNormal;
110                         case CheckState.Indeterminate:
111                                 if (!checkBox.Enabled)
112                                         return CheckBoxState.MixedDisabled;
113                                 else if (checkBox.Pressed)
114                                         return CheckBoxState.MixedPressed;
115                                 else if (checkBox.Entered)
116                                         return CheckBoxState.MixedHot;
117                                 return CheckBoxState.MixedNormal;
118                         default:
119                                 if (!checkBox.Enabled)
120                                         return CheckBoxState.UncheckedDisabled;
121                                 else if (checkBox.Pressed)
122                                         return CheckBoxState.UncheckedPressed;
123                                 else if (checkBox.Entered)
124                                         return CheckBoxState.UncheckedHot;
125                                 return CheckBoxState.UncheckedNormal;
126                         }
127                 }
128                 #endregion
129                 #region ComboBox
130                 static VisualStyleElement ComboBoxGetVisualStyleElement (ComboBox comboBox, ButtonState state)
131                 {
132                         if (state == ButtonState.Inactive)
133                                 return VisualStyleElement.ComboBox.DropDownButton.Disabled;
134                         if (state == ButtonState.Pushed)
135                                 return VisualStyleElement.ComboBox.DropDownButton.Pressed;
136                         if (comboBox.DropDownButtonEntered)
137                                 return VisualStyleElement.ComboBox.DropDownButton.Hot;
138                         return VisualStyleElement.ComboBox.DropDownButton.Normal;
139                 }
140                 public override void ComboBoxDrawNormalDropDownButton (ComboBox comboBox, Graphics g, Rectangle clippingArea, Rectangle area, ButtonState state)
141                 {
142                         VisualStyleElement element = ComboBoxGetVisualStyleElement (comboBox, state);
143                         if (!VisualStyleRenderer.IsElementDefined (element)) {
144                                 base.ComboBoxDrawNormalDropDownButton (comboBox, g, clippingArea, area, state);
145                                 return;
146                         }
147                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
148                 }
149                 public override bool ComboBoxNormalDropDownButtonHasTransparentBackground (ComboBox comboBox, ButtonState state)
150                 {
151                         VisualStyleElement element = ComboBoxGetVisualStyleElement (comboBox, state);
152                         if (!VisualStyleRenderer.IsElementDefined (element))
153                                 return base.ComboBoxNormalDropDownButtonHasTransparentBackground (comboBox, state);
154                         return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
155                 }
156                 public override bool ComboBoxDropDownButtonHasHotElementStyle (ComboBox comboBox)
157                 {
158 #if NET_2_0
159                         switch (comboBox.FlatStyle) {
160                         case FlatStyle.Flat:
161                         case FlatStyle.Popup:
162                                 return base.ComboBoxDropDownButtonHasHotElementStyle (comboBox);
163                         }
164 #endif
165                         return true;
166                 }
167                 #endregion
168                 #region ControlPaint
169                 #region DrawButton
170                 public override void CPDrawButton (Graphics dc, Rectangle rectangle, ButtonState state)
171                 {
172                         if ((state & ButtonState.Flat) == ButtonState.Flat ||
173                                 (state & ButtonState.Checked) == ButtonState.Checked) {
174                                 base.CPDrawButton (dc, rectangle, state);
175                                 return;
176                         }
177                         VisualStyleElement element;
178                         if ((state & ButtonState.Inactive) == ButtonState.Inactive)
179                                 element = VisualStyleElement.Button.PushButton.Disabled;
180                         else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
181                                 element = VisualStyleElement.Button.PushButton.Pressed;
182                         else
183                                 element = VisualStyleElement.Button.PushButton.Normal;
184                         if (!VisualStyleRenderer.IsElementDefined (element)) {
185                                 base.CPDrawButton (dc, rectangle, state);
186                                 return;
187                         }
188                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
189                 }
190                 #endregion
191                 #region DrawCaptionButton
192                 public override void CPDrawCaptionButton (Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state)
193                 {
194                         if ((state & ButtonState.Flat) == ButtonState.Flat ||
195                                 (state & ButtonState.Checked) == ButtonState.Checked) {
196                                 base.CPDrawCaptionButton (graphics, rectangle, button, state);
197                                 return;
198                         }
199                         VisualStyleElement element = GetCaptionButtonVisualStyleElement (button, state);
200                         if (!VisualStyleRenderer.IsElementDefined (element)) {
201                                 base.CPDrawCaptionButton (graphics, rectangle, button, state);
202                                 return;
203                         }
204                         new VisualStyleRenderer (element).DrawBackground (graphics, rectangle);
205                 }
206                 static VisualStyleElement GetCaptionButtonVisualStyleElement (CaptionButton button, ButtonState state)
207                 {
208                         switch (button) {
209                         case CaptionButton.Minimize:
210                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
211                                         return VisualStyleElement.Window.MinButton.Disabled;
212                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
213                                         return VisualStyleElement.Window.MinButton.Pressed;
214                                 else
215                                         return VisualStyleElement.Window.MinButton.Normal;
216                         case CaptionButton.Maximize:
217                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
218                                         return VisualStyleElement.Window.MaxButton.Disabled;
219                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
220                                         return VisualStyleElement.Window.MaxButton.Pressed;
221                                 else
222                                         return VisualStyleElement.Window.MaxButton.Normal;
223                         case CaptionButton.Close:
224                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
225                                         return VisualStyleElement.Window.CloseButton.Disabled;
226                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
227                                         return VisualStyleElement.Window.CloseButton.Pressed;
228                                 else
229                                         return VisualStyleElement.Window.CloseButton.Normal;
230                         case CaptionButton.Restore:
231                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
232                                         return VisualStyleElement.Window.RestoreButton.Disabled;
233                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
234                                         return VisualStyleElement.Window.RestoreButton.Pressed;
235                                 else
236                                         return VisualStyleElement.Window.RestoreButton.Normal;
237                         default:
238                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
239                                         return VisualStyleElement.Window.HelpButton.Disabled;
240                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
241                                         return VisualStyleElement.Window.HelpButton.Pressed;
242                                 else
243                                         return VisualStyleElement.Window.HelpButton.Normal;
244                         }
245                 }
246                 #endregion
247                 #region DrawCheckBox
248                 public override void CPDrawCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
249                 {
250                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
251                                 base.CPDrawCheckBox (dc, rectangle, state);
252                                 return;
253                         }
254                         VisualStyleElement element;
255                         if ((state & ButtonState.Checked) == ButtonState.Checked)
256                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
257                                         element = VisualStyleElement.Button.CheckBox.CheckedDisabled;
258                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
259                                         element = VisualStyleElement.Button.CheckBox.CheckedPressed;
260                                 else
261                                         element = VisualStyleElement.Button.CheckBox.CheckedNormal;
262                         else
263                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
264                                         element = VisualStyleElement.Button.CheckBox.UncheckedDisabled;
265                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
266                                         element = VisualStyleElement.Button.CheckBox.UncheckedPressed;
267                                 else
268                                         element = VisualStyleElement.Button.CheckBox.UncheckedNormal;
269                         if (!VisualStyleRenderer.IsElementDefined (element)) {
270                                 base.CPDrawCheckBox (dc, rectangle, state);
271                                 return;
272                         }
273                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
274                 }
275                 #endregion
276                 #region DrawComboButton
277                 public override void CPDrawComboButton (Graphics graphics, Rectangle rectangle, ButtonState state)
278                 {
279                         if ((state & ButtonState.Flat) == ButtonState.Flat ||
280                                 (state & ButtonState.Checked) == ButtonState.Checked) {
281                                 base.CPDrawComboButton (graphics, rectangle, state);
282                                 return;
283                         }
284                         VisualStyleElement element;
285                         if ((state & ButtonState.Inactive) == ButtonState.Inactive)
286                                 element = VisualStyleElement.ComboBox.DropDownButton.Disabled;
287                         else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
288                                 element = VisualStyleElement.ComboBox.DropDownButton.Pressed;
289                         else
290                                 element = VisualStyleElement.ComboBox.DropDownButton.Normal;
291                         if (!VisualStyleRenderer.IsElementDefined (element)) {
292                                 base.CPDrawComboButton (graphics, rectangle, state);
293                                 return;
294                         }
295                         new VisualStyleRenderer (element).DrawBackground (graphics, rectangle);
296                 }
297                 #endregion
298                 #region DrawMixedCheckBox
299                 public override void CPDrawMixedCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
300                 {
301                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
302                                 base.CPDrawMixedCheckBox (dc, rectangle, state);
303                                 return;
304                         }
305                         VisualStyleElement element;
306                         if ((state & ButtonState.Checked) == ButtonState.Checked)
307                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
308                                         element = VisualStyleElement.Button.CheckBox.MixedDisabled;
309                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
310                                         element = VisualStyleElement.Button.CheckBox.MixedPressed;
311                                 else
312                                         element = VisualStyleElement.Button.CheckBox.MixedNormal;
313                         else
314                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
315                                         element = VisualStyleElement.Button.CheckBox.UncheckedDisabled;
316                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
317                                         element = VisualStyleElement.Button.CheckBox.UncheckedPressed;
318                                 else
319                                         element = VisualStyleElement.Button.CheckBox.UncheckedNormal;
320                         if (!VisualStyleRenderer.IsElementDefined (element)) {
321                                 base.CPDrawMixedCheckBox (dc, rectangle, state);
322                                 return;
323                         }
324                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
325                 }
326                 #endregion
327                 #region DrawRadioButton
328                 public override void CPDrawRadioButton (Graphics dc, Rectangle rectangle, ButtonState state)
329                 {
330                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
331                                 base.CPDrawRadioButton (dc, rectangle, state);
332                                 return;
333                         }
334                         VisualStyleElement element;
335                         if ((state & ButtonState.Checked) == ButtonState.Checked)
336                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
337                                         element = VisualStyleElement.Button.RadioButton.CheckedDisabled;
338                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
339                                         element = VisualStyleElement.Button.RadioButton.CheckedPressed;
340                                 else
341                                         element = VisualStyleElement.Button.RadioButton.CheckedNormal;
342                         else
343                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
344                                         element = VisualStyleElement.Button.RadioButton.UncheckedDisabled;
345                                 else if ((state & ButtonState.Pushed) == ButtonState.Pushed)
346                                         element = VisualStyleElement.Button.RadioButton.UncheckedPressed;
347                                 else
348                                         element = VisualStyleElement.Button.RadioButton.UncheckedNormal;
349                         if (!VisualStyleRenderer.IsElementDefined (element)) {
350                                 base.CPDrawRadioButton (dc, rectangle, state);
351                                 return;
352                         }
353                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
354                 }
355                 #endregion
356                 #region DrawScrollButton
357                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state)
358                 {
359                         if ((state & ButtonState.Flat) == ButtonState.Flat ||
360                                 (state & ButtonState.Checked) == ButtonState.Checked) {
361                                 base.CPDrawScrollButton (dc, area, type, state);
362                                 return;
363                         }
364                         VisualStyleElement element = GetScrollButtonVisualStyleElement (type, state);
365                         if (!VisualStyleRenderer.IsElementDefined (element)) {
366                                 base.CPDrawScrollButton (dc, area, type, state);
367                                 return;
368                         }
369                         new VisualStyleRenderer (element).DrawBackground (dc, area);
370                 }
371                 static VisualStyleElement GetScrollButtonVisualStyleElement (ScrollButton type, ButtonState state)
372                 {
373                         switch (type) {
374                         case ScrollButton.Left:
375                                 if (IsDisabled (state))
376                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftDisabled;
377                                 else if (IsPressed (state))
378                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
379                                 else
380                                         return VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
381                         case ScrollButton.Right:
382                                 if (IsDisabled (state))
383                                         return VisualStyleElement.ScrollBar.ArrowButton.RightDisabled;
384                                 else if (IsPressed (state))
385                                         return VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
386                                 else
387                                         return VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
388                         case ScrollButton.Up:
389                                 if (IsDisabled (state))
390                                         return VisualStyleElement.ScrollBar.ArrowButton.UpDisabled;
391                                 else if (IsPressed (state))
392                                         return VisualStyleElement.ScrollBar.ArrowButton.UpPressed;
393                                 else
394                                         return VisualStyleElement.ScrollBar.ArrowButton.UpNormal;
395                         default:
396                                 if (IsDisabled (state))
397                                         return VisualStyleElement.ScrollBar.ArrowButton.DownDisabled;
398                                 else if (IsPressed (state))
399                                         return VisualStyleElement.ScrollBar.ArrowButton.DownPressed;
400                                 else
401                                         return VisualStyleElement.ScrollBar.ArrowButton.DownNormal;
402                         }
403                 }
404                 static bool IsDisabled (ButtonState state)
405                 {
406                         return (state & ButtonState.Inactive) == ButtonState.Inactive;
407                 }
408                 static bool IsPressed (ButtonState state)
409                 {
410                         return (state & ButtonState.Pushed) == ButtonState.Pushed;
411                 }
412                 #endregion
413                 #endregion
414 #if NET_2_0
415                 #region DataGridView
416                 #region DataGridViewHeaderCell
417                 #region DataGridViewRowHeaderCell
418                 public override bool DataGridViewRowHeaderCellDrawBackground (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
419                 {
420                         if (!cell.DataGridView.EnableHeadersVisualStyles)
421                                 return base.DataGridViewRowHeaderCellDrawBackground (cell, g, bounds);
422                         VisualStyleElement element = DataGridViewRowHeaderCellGetVisualStyleElement (cell);
423                         if (!VisualStyleRenderer.IsElementDefined (element))
424                                 return base.DataGridViewRowHeaderCellDrawBackground (cell, g, bounds);
425                         bounds.Width--;
426                         Bitmap bitmap = new Bitmap (bounds.Height, bounds.Width);
427                         Graphics bitmap_g = Graphics.FromImage (bitmap);
428                         Rectangle bitmap_rectangle = new Rectangle (Point.Empty, bitmap.Size);
429                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
430                         if (!AreEqual (element, VisualStyleElement.Header.Item.Normal) && renderer.IsBackgroundPartiallyTransparent ())
431                                 new VisualStyleRenderer (VisualStyleElement.Header.Item.Normal).DrawBackground (bitmap_g, bitmap_rectangle);
432                         renderer.DrawBackground (bitmap_g, bitmap_rectangle);
433                         bitmap_g.Dispose ();
434                         g.Transform = new Matrix(0, 1, 1, 0, 0, 0);
435                         g.DrawImage (bitmap, bounds.Y, bounds.X);
436                         bitmap.Dispose ();
437                         g.ResetTransform ();
438                         return true;
439                 }
440                 public override bool DataGridViewRowHeaderCellDrawSelectionBackground (DataGridViewRowHeaderCell cell)
441                 {
442                         if (!cell.DataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (DataGridViewRowHeaderCellGetVisualStyleElement (cell)))
443                                 return base.DataGridViewRowHeaderCellDrawSelectionBackground (cell);
444                         return true;
445                 }
446                 public override bool DataGridViewRowHeaderCellDrawBorder (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
447                 {
448                         if (!cell.DataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (DataGridViewRowHeaderCellGetVisualStyleElement (cell)))
449                                 return base.DataGridViewRowHeaderCellDrawBorder (cell, g, bounds);
450                         g.DrawLine (cell.GetBorderPen (), bounds.Right - 1, bounds.Top, bounds.Right - 1, bounds.Bottom - 1);
451                         return true;
452                 }
453                 static VisualStyleElement DataGridViewRowHeaderCellGetVisualStyleElement (DataGridViewRowHeaderCell cell)
454                 {
455                         if (cell.DataGridView.PressedHeaderCell == cell)
456                                 return VisualStyleElement.Header.Item.Pressed;
457                         if (cell.DataGridView.EnteredHeaderCell == cell)
458                                 return VisualStyleElement.Header.Item.Hot;
459                         if (cell.OwningRow.SelectedInternal)
460                                 return VisualStyleElement.Header.Item.Pressed;
461                         return VisualStyleElement.Header.Item.Normal;
462                 }
463                 #endregion
464                 #region DataGridViewColumnHeaderCell
465                 public override bool DataGridViewColumnHeaderCellDrawBackground (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
466                 {
467                         if (!cell.DataGridView.EnableHeadersVisualStyles || cell is DataGridViewTopLeftHeaderCell)
468                                 return base.DataGridViewColumnHeaderCellDrawBackground (cell, g, bounds);
469                         VisualStyleElement element = DataGridViewColumnHeaderCellGetVisualStyleElement (cell);
470                         if (!VisualStyleRenderer.IsElementDefined (element))
471                                 return base.DataGridViewColumnHeaderCellDrawBackground (cell, g, bounds);
472                         bounds.Height--;
473                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
474                         if (!AreEqual (element, VisualStyleElement.Header.Item.Normal) && renderer.IsBackgroundPartiallyTransparent ())
475                             new VisualStyleRenderer (VisualStyleElement.Header.Item.Normal).DrawBackground (g, bounds);
476                         renderer.DrawBackground (g, bounds);
477                         return true;
478                 }
479                 public override bool DataGridViewColumnHeaderCellDrawBorder (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
480                 {
481                         if (!cell.DataGridView.EnableHeadersVisualStyles || cell is DataGridViewTopLeftHeaderCell || !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Normal))
482                                 return base.DataGridViewColumnHeaderCellDrawBorder (cell, g, bounds);
483                         g.DrawLine (cell.GetBorderPen (), bounds.Left, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 1);
484                         return true;
485                 }
486                 static VisualStyleElement DataGridViewColumnHeaderCellGetVisualStyleElement (DataGridViewColumnHeaderCell cell)
487                 {
488                         if (cell.DataGridView.PressedHeaderCell == cell)
489                                 return VisualStyleElement.Header.Item.Pressed;
490                         if (cell.DataGridView.EnteredHeaderCell == cell)
491                                 return VisualStyleElement.Header.Item.Hot;
492                         return VisualStyleElement.Header.Item.Normal;
493                 }
494                 #endregion
495                 public override bool DataGridViewHeaderCellHasPressedStyle (DataGridView dataGridView)
496                 {
497                         if (!dataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Pressed))
498                                 return base.DataGridViewHeaderCellHasPressedStyle (dataGridView);
499                         return true;
500                 }
501                 public override bool DataGridViewHeaderCellHasHotStyle (DataGridView dataGridView)
502                 {
503                         if (!dataGridView.EnableHeadersVisualStyles || !VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Hot))
504                                 return base.DataGridViewHeaderCellHasHotStyle (dataGridView);
505                         return true;
506                 }
507                 #endregion
508                 #endregion
509 #endif
510                 #region ListView
511                 protected override void ListViewDrawColumnHeaderBackground (ListView listView, ColumnHeader columnHeader, Graphics g, Rectangle area, Rectangle clippingArea)
512                 {
513                         VisualStyleElement element;
514                         if (listView.HeaderStyle == ColumnHeaderStyle.Clickable)
515                                 if (columnHeader.Pressed)
516                                         element = VisualStyleElement.Header.Item.Pressed;
517                                 else if (columnHeader == listView.EnteredColumnHeader)
518                                         element = VisualStyleElement.Header.Item.Hot;
519                                 else
520                                         element = VisualStyleElement.Header.Item.Normal;
521                         else
522                                 element = VisualStyleElement.Header.Item.Normal;
523                         if (!VisualStyleRenderer.IsElementDefined (element)) {
524                                 base.ListViewDrawColumnHeaderBackground (listView, columnHeader, g, area, clippingArea);
525                                 return;
526                         }
527                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
528                 }
529                 protected override void ListViewDrawUnusedHeaderBackground (ListView listView, Graphics g, Rectangle area, Rectangle clippingArea)
530                 {
531                         VisualStyleElement element = VisualStyleElement.Header.Item.Normal;
532                         if (!VisualStyleRenderer.IsElementDefined (element)) {
533                                 base.ListViewDrawUnusedHeaderBackground (listView, g, area, clippingArea);
534                                 return;
535                         }
536                         new VisualStyleRenderer (element).DrawBackground (g, area, clippingArea);
537                 }
538                 public override bool ListViewHasHotHeaderStyle {
539                         get {
540                                 if (!VisualStyleRenderer.IsElementDefined (VisualStyleElement.Header.Item.Hot))
541                                         return base.ListViewHasHotHeaderStyle;
542                                 return true;
543                         }
544                 }
545                 public override int ListViewGetHeaderHeight (ListView listView, Font font)
546                 {
547                         VisualStyleElement element = VisualStyleElement.Header.Item.Normal;
548                         if (!VisualStyleRenderer.IsElementDefined (element))
549                                 return base.ListViewGetHeaderHeight (listView, font);
550                         Control control = null;
551                         Graphics g;
552                         if (listView == null) {
553                                 control = new Control ();
554                                 g = control.CreateGraphics ();
555                         } else
556                                 g = listView.CreateGraphics ();
557                         int result = new VisualStyleRenderer (element).GetPartSize (g, ThemeSizeType.True).Height;
558                         g.Dispose ();
559                         if (listView == null)
560                                 control.Dispose ();
561                         return result;
562                 }
563                 #endregion
564                 #region GroupBox
565                 public override void DrawGroupBox (Graphics dc, Rectangle area, GroupBox box)
566                 {
567                         GroupBoxRenderer.DrawGroupBox (
568                                 dc,
569                                 new Rectangle (Point.Empty, box.Size),
570                                 box.Text,
571                                 box.Font,
572                                 box.ForeColor == GroupBox.DefaultForeColor ? Color.Empty : box.ForeColor,
573                                 box.Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled);
574                 }
575                 #endregion
576                 #region Managed windows
577                 [MonoTODO("When other VisualStyles implementations are supported, check if the visual style elements are defined.")]
578                 [MonoTODO("When the rest of the MDI code is fixed, make sure minimized windows are painted correctly,restrict the caption text drawing area to exclude the title buttons and handle the title bar height correctly.")]
579                 public override void DrawManagedWindowDecorations (Graphics dc, Rectangle clip, InternalWindowManager wm)
580                 {
581                         if (!wm.HasBorders || (wm.Form.IsMdiChild && wm.IsMaximized))
582                                 return;
583                         VisualStyleElement element;
584                         #region Title bar background
585                         #region Select caption visual style element
586                         if (wm.IsToolWindow)
587                                 #region Small window
588                                 switch (wm.form.window_state) {
589                                 case FormWindowState.Minimized:
590                                         if (!wm.Form.Enabled)
591                                                 element = VisualStyleElement.Window.SmallMinCaption.Disabled;
592                                         else if (wm.IsActive)
593                                                 element = VisualStyleElement.Window.SmallMinCaption.Active;
594                                         else
595                                                 element = VisualStyleElement.Window.SmallMinCaption.Inactive;
596                                         break;
597                                 case FormWindowState.Maximized:
598                                         if (!wm.Form.Enabled)
599                                                 element = VisualStyleElement.Window.SmallMaxCaption.Disabled;
600                                         else if (wm.IsActive)
601                                                 element = VisualStyleElement.Window.SmallMaxCaption.Active;
602                                         else
603                                                 element = VisualStyleElement.Window.SmallMaxCaption.Inactive;
604                                         break;
605                                 default:
606                                         if (!wm.Form.Enabled)
607                                                 element = VisualStyleElement.Window.SmallCaption.Disabled;
608                                         else if (wm.IsActive)
609                                                 element = VisualStyleElement.Window.SmallCaption.Active;
610                                         else
611                                                 element = VisualStyleElement.Window.SmallCaption.Inactive;
612                                         break;
613                                 }
614                                 #endregion
615                         else
616                                 #region Normal window
617                                 switch (wm.form.window_state) {
618                                 case FormWindowState.Minimized:
619                                         if (!wm.Form.Enabled)
620                                                 element = VisualStyleElement.Window.MinCaption.Disabled;
621                                         else if (wm.IsActive)
622                                                 element = VisualStyleElement.Window.MinCaption.Active;
623                                         else
624                                                 element = VisualStyleElement.Window.MinCaption.Inactive;
625                                         break;
626                                 case FormWindowState.Maximized:
627                                         if (!wm.Form.Enabled)
628                                                 element = VisualStyleElement.Window.MaxCaption.Disabled;
629                                         else if (wm.IsActive)
630                                                 element = VisualStyleElement.Window.MaxCaption.Active;
631                                         else
632                                                 element = VisualStyleElement.Window.MaxCaption.Inactive;
633                                         break;
634                                 default:
635                                         if (!wm.Form.Enabled)
636                                                 element = VisualStyleElement.Window.Caption.Disabled;
637                                         else if (wm.IsActive)
638                                                 element = VisualStyleElement.Window.Caption.Active;
639                                         else
640                                                 element = VisualStyleElement.Window.Caption.Inactive;
641                                         break;
642                                 }
643                                 #endregion
644                         #endregion
645                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
646                         Rectangle title_bar_rectangle = new Rectangle (
647                                 0,
648                                 0,
649                                 wm.Form.Width,
650                                 renderer.GetPartSize (dc, ThemeSizeType.True).Height
651                         );
652                         Rectangle caption_text_area = title_bar_rectangle;
653                         renderer.DrawBackground (dc, title_bar_rectangle, clip);
654                         #endregion
655                         int border_width = ManagedWindowBorderWidth (wm);
656                         #region Icon
657                         if (!wm.IsToolWindow && wm.Form.FormBorderStyle != FormBorderStyle.FixedDialog
658 #if NET_2_0
659                         && wm.Form.ShowIcon
660 #endif
661                         ) {
662                                 Rectangle icon_rectangle = new Rectangle (
663                                         border_width + 3,
664                                         border_width + 2,
665                                         wm.IconWidth,
666                                         wm.IconWidth);
667                                 caption_text_area.X += icon_rectangle.Width;
668                                 if (icon_rectangle.IntersectsWith (clip))
669                                         dc.DrawIcon (wm.Form.Icon, icon_rectangle);
670                         }
671                         #endregion
672                         #region Title bar buttons
673                         foreach (TitleButton button in wm.TitleButtons.AllButtons) {
674                                 if (!button.Visible || !button.Rectangle.IntersectsWith (clip))
675                                         continue;
676                                 new VisualStyleRenderer (GetCaptionButtonVisualStyleElement (button.Caption, button.State)).DrawBackground (dc, button.Rectangle, clip);
677                         }
678                         #endregion
679                         #region Borders
680                         if (wm.GetWindowState () == FormWindowState.Normal) {
681                                 #region Left
682                                 if (wm.IsToolWindow)
683                                         if (wm.IsActive)
684                                                 element = VisualStyleElement.Window.SmallFrameLeft.Active;
685                                         else
686                                                 element = VisualStyleElement.Window.SmallFrameLeft.Inactive;
687                                 else
688                                         if (wm.IsActive)
689                                                 element = VisualStyleElement.Window.FrameLeft.Active;
690                                         else
691                                                 element = VisualStyleElement.Window.FrameLeft.Inactive;
692                                 renderer = new VisualStyleRenderer (element);
693                                 renderer.DrawBackground (dc, new Rectangle (
694                                         0,
695                                         title_bar_rectangle.Bottom,
696                                         border_width,
697                                         wm.form.Height - title_bar_rectangle.Bottom
698                                         ), clip);
699                                 #endregion
700                                 #region Right
701                                 if (wm.IsToolWindow)
702                                         if (wm.IsActive)
703                                                 element = VisualStyleElement.Window.SmallFrameRight.Active;
704                                         else
705                                                 element = VisualStyleElement.Window.SmallFrameRight.Inactive;
706                                 else
707                                         if (wm.IsActive)
708                                                 element = VisualStyleElement.Window.FrameRight.Active;
709                                         else
710                                                 element = VisualStyleElement.Window.FrameRight.Inactive;
711
712                                 renderer = new VisualStyleRenderer (element);
713                                 renderer.DrawBackground (dc, new Rectangle (
714                                         wm.form.Width - border_width,
715                                         title_bar_rectangle.Bottom,
716                                         border_width,
717                                         wm.form.Height - title_bar_rectangle.Bottom
718                                         ), clip);
719                                 #endregion
720                                 #region Bottom
721                                 if (wm.IsToolWindow)
722                                         if (wm.IsActive)
723                                                 element = VisualStyleElement.Window.SmallFrameBottom.Active;
724                                         else
725                                                 element = VisualStyleElement.Window.SmallFrameBottom.Inactive;
726                                 else
727                                         if (wm.IsActive)
728                                                 element = VisualStyleElement.Window.FrameBottom.Active;
729                                         else
730                                                 element = VisualStyleElement.Window.FrameBottom.Inactive;
731
732                                 renderer = new VisualStyleRenderer (element);
733                                 renderer.DrawBackground (dc, new Rectangle (
734                                         0,
735                                         wm.form.Height - border_width,
736                                         wm.form.Width,
737                                         border_width
738                                         ), clip);
739                                 #endregion
740                                 caption_text_area.X += border_width;
741                         }
742                         #endregion
743                         #region Caption text
744                         string window_caption = wm.Form.Text;
745                         if (window_caption != null && window_caption.Length != 0 && caption_text_area.IntersectsWith (clip)) {
746                                 StringFormat format = new StringFormat ();
747                                 format.FormatFlags = StringFormatFlags.NoWrap;
748                                 format.Trimming = StringTrimming.EllipsisCharacter;
749                                 format.LineAlignment = StringAlignment.Center;
750                                 dc.DrawString (
751                                         window_caption,
752                                         WindowBorderFont,
753                                         ThemeEngine.Current.ResPool.GetSolidBrush (Color.White),
754                                         caption_text_area,
755                                         format
756                                 );
757                         }
758                         #endregion
759                 }
760                 #endregion
761                 #region ProgressBar
762                 public override void DrawProgressBar (Graphics dc, Rectangle clip_rect, ProgressBar ctrl)
763                 {
764                         if (!VisualStyleRenderer.IsElementDefined (VisualStyleElement.ProgressBar.Bar.Normal) ||
765                                 !VisualStyleRenderer.IsElementDefined (VisualStyleElement.ProgressBar.Chunk.Normal)) {
766                                 base.DrawProgressBar (dc, clip_rect, ctrl);
767                                 return;
768                         }
769                         VisualStyleRenderer renderer = new VisualStyleRenderer (VisualStyleElement.ProgressBar.Bar.Normal);
770                         renderer.DrawBackground (dc, ctrl.ClientRectangle, clip_rect);
771                         Rectangle client_area = renderer.GetBackgroundContentRectangle (dc, new Rectangle (Point.Empty, ctrl.Size));
772                         renderer = new VisualStyleRenderer (VisualStyleElement.ProgressBar.Chunk.Normal);
773                         /* Draw Blocks */
774                         int draw_mode = 0;
775                         int max_blocks = int.MaxValue;
776                         int start_pixel = client_area.X;
777 #if NET_2_0
778                         draw_mode = (int)ctrl.Style;
779 #endif
780                         switch (draw_mode) {
781 #if NET_2_0
782                         case 1: // Continuous
783                                 client_area.Width = (int)(client_area.Width * ((double)(ctrl.Value - ctrl.Minimum) / (double)(Math.Max (ctrl.Maximum - ctrl.Minimum, 1))));
784                                 renderer.DrawBackground (dc, client_area, clip_rect);
785                                 break;
786                         case 2: // Marquee
787                                 int ms_diff = (int)(DateTime.Now - ctrl.start).TotalMilliseconds;
788                                 double percent_done = (double)ms_diff % (double)ctrl.MarqueeAnimationSpeed / (double)ctrl.MarqueeAnimationSpeed;
789                                 max_blocks = 5;
790                                 start_pixel = client_area.X + (int)(client_area.Width * percent_done);
791                                 goto default;
792 #endif
793                         default: // Blocks
794                                 int block_width = renderer.GetInteger (IntegerProperty.ProgressChunkSize);
795                                 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;
796                                 int block_count = 0;
797                                 int increment = block_width + renderer.GetInteger (IntegerProperty.ProgressSpaceSize);
798                                 Rectangle block_rect = new Rectangle (start_pixel, client_area.Y, block_width, client_area.Height);
799                                 while (true) {
800                                         if (max_blocks != int.MaxValue) {
801                                                 if (block_count == max_blocks)
802                                                         break;
803                                                 if (block_rect.Right >= client_area.Width)
804                                                         block_rect.X -= client_area.Width;
805                                         } else {
806                                                 if (block_rect.X >= first_pixel_outside_filled_area)
807                                                         break;
808                                                 if (block_rect.Right >= first_pixel_outside_filled_area)
809                                                         if (first_pixel_outside_filled_area == client_area.Right)
810                                                                 block_rect.Width = first_pixel_outside_filled_area - block_rect.X;
811                                                         else
812                                                                 break;
813                                         }
814                                         if (clip_rect.IntersectsWith (block_rect))
815                                                 renderer.DrawBackground (dc, block_rect, clip_rect);
816                                         block_rect.X += increment;
817                                         block_count++;
818                                 }
819                                 break;
820                         }
821                 }
822                 #endregion
823                 #region RadioButton
824                 protected override void RadioButton_DrawButton (RadioButton radio_button, Graphics dc, ButtonState state, Rectangle radiobutton_rectangle) {
825                         if (radio_button.Appearance == Appearance.Normal && radio_button.FlatStyle == FlatStyle.System) {
826                                 RadioButtonRenderer.DrawRadioButton (
827                                         dc,
828                                         new Point (radiobutton_rectangle.Left, radiobutton_rectangle.Top),
829                                         GetRadioButtonState (radio_button)
830                                 );
831                                 return;
832                         }
833                         base.RadioButton_DrawButton(radio_button, dc, state, radiobutton_rectangle);
834                 }
835                 static RadioButtonState GetRadioButtonState (RadioButton checkBox)
836                 {
837                         if (checkBox.Checked) {
838                                 if (!checkBox.Enabled)
839                                         return RadioButtonState.CheckedDisabled;
840                                 else if (checkBox.Pressed)
841                                         return RadioButtonState.CheckedPressed;
842                                 else if (checkBox.Entered)
843                                         return RadioButtonState.CheckedHot;
844                                 return RadioButtonState.CheckedNormal;
845                         } else {
846                                 if (!checkBox.Enabled)
847                                         return RadioButtonState.UncheckedDisabled;
848                                 else if (checkBox.Pressed)
849                                         return RadioButtonState.UncheckedPressed;
850                                 else if (checkBox.Entered)
851                                         return RadioButtonState.UncheckedHot;
852                                 return RadioButtonState.UncheckedNormal;
853                         }
854                 }
855                 #endregion
856                 #region ScrollBar
857                 public override void DrawScrollBar (Graphics dc, Rectangle clip, ScrollBar bar)
858                 {
859                         if (!ScrollBarAreElementsDefined) {
860                                 base.DrawScrollBar (dc, clip, bar);
861                                 return;
862                         }
863                         VisualStyleElement element;
864                         VisualStyleRenderer renderer;
865                         int scroll_button_width = bar.scrollbutton_width;
866                         int scroll_button_height = bar.scrollbutton_height;
867                         if (bar.vert) {
868                                 bar.FirstArrowArea = new Rectangle (0, 0, bar.Width, scroll_button_height);
869                                 bar.SecondArrowArea = new Rectangle (
870                                         0,
871                                         bar.ClientRectangle.Height - scroll_button_height,
872                                         bar.Width,
873                                         scroll_button_height);
874                                 Rectangle thumb_pos = bar.ThumbPos;
875                                 thumb_pos.Width = bar.Width;
876                                 bar.ThumbPos = thumb_pos;
877                                 #region Background, upper track
878                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
879                                         element = VisualStyleElement.ScrollBar.LowerTrackVertical.Pressed;
880                                 else
881                                         element = bar.Enabled ?
882                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Normal :
883                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
884                                 renderer = new VisualStyleRenderer (element);
885                                 Rectangle upper_track_rect = new Rectangle (
886                                         0,
887                                         0,
888                                         bar.ClientRectangle.Width,
889                                         bar.ThumbPos.Top);
890                                 if (clip.IntersectsWith (upper_track_rect))
891                                         renderer.DrawBackground (dc, upper_track_rect, clip);
892                                 #endregion
893                                 #region Background, lower track
894                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
895                                         element = VisualStyles.VisualStyleElement.ScrollBar.LowerTrackVertical.Pressed;
896                                 else
897                                         element = bar.Enabled ?
898                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Normal :
899                                                 VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
900                                 renderer = new VisualStyleRenderer (element);
901                                 Rectangle lower_track_rect = new Rectangle (
902                                         0,
903                                         bar.ThumbPos.Bottom,
904                                         bar.ClientRectangle.Width,
905                                         bar.ClientRectangle.Height - bar.ThumbPos.Bottom);
906                                 if (clip.IntersectsWith (lower_track_rect))
907                                         renderer.DrawBackground (dc, lower_track_rect, clip);
908                                 #endregion
909                                 #region Buttons
910                                 if (clip.IntersectsWith (bar.FirstArrowArea)) {
911                                         if (!bar.Enabled)
912                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpDisabled;
913                                         else if (bar.firstbutton_state == ButtonState.Pushed)
914                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpPressed;
915                                         else if (bar.FirstButtonEntered)
916                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpHot;
917                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
918                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpHover;
919                                         else
920                                                 element = VisualStyleElement.ScrollBar.ArrowButton.UpNormal;
921                                         renderer = new VisualStyleRenderer (element);
922                                         renderer.DrawBackground (dc, bar.FirstArrowArea);
923                                 }
924                                 if (clip.IntersectsWith (bar.SecondArrowArea)) {
925                                         if (!bar.Enabled)
926                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownDisabled;
927                                         else if (bar.secondbutton_state == ButtonState.Pushed)
928                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownPressed;
929                                         else if (bar.SecondButtonEntered)
930                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownHot;
931                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
932                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownHover;
933                                         else
934                                                 element = VisualStyleElement.ScrollBar.ArrowButton.DownNormal;
935                                         renderer = new VisualStyleRenderer (element);
936                                         renderer.DrawBackground (dc, bar.SecondArrowArea);
937                                 }
938                                 #endregion
939                                 #region Thumb and grip
940                                 if (!bar.Enabled)
941                                         element = VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled;
942                                 else if (bar.ThumbPressed)
943                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Pressed;
944                                 else if (bar.ThumbEntered)
945                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Hot;
946                                 else
947                                         element = VisualStyleElement.ScrollBar.ThumbButtonVertical.Normal;
948                                 renderer = new VisualStyleRenderer (element);
949                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
950
951                                 if (bar.Enabled && bar.ThumbPos.Height >= 20) {
952                                         element = VisualStyleElement.ScrollBar.GripperVertical.Normal;
953                                         renderer = new VisualStyleRenderer (element);
954                                         renderer.DrawBackground (dc, bar.ThumbPos, clip);
955                                 }
956                                 #endregion
957                         } else {
958                                 bar.FirstArrowArea = new Rectangle (0, 0, scroll_button_width, bar.Height);
959                                 bar.SecondArrowArea = new Rectangle (
960                                         bar.ClientRectangle.Width - scroll_button_width,
961                                         0,
962                                         scroll_button_width,
963                                         bar.Height);
964                                 Rectangle thumb_pos = bar.ThumbPos;
965                                 thumb_pos.Height = bar.Height;
966                                 bar.ThumbPos = thumb_pos;
967                                 #region Background, left track
968                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
969                                         element = VisualStyleElement.ScrollBar.LeftTrackHorizontal.Pressed;
970                                 else
971                                         element = bar.Enabled ?
972                                                 VisualStyleElement.ScrollBar.LeftTrackHorizontal.Normal :
973                                                 VisualStyleElement.ScrollBar.LeftTrackHorizontal.Disabled;
974                                 renderer = new VisualStyleRenderer (element);
975                                 Rectangle left_track_rect = new Rectangle (
976                                         0,
977                                         0,
978                                         bar.ThumbPos.Left,
979                                         bar.ClientRectangle.Height);
980                                 if (clip.IntersectsWith (left_track_rect))
981                                         renderer.DrawBackground (dc, left_track_rect, clip);
982                                 #endregion
983                                 #region Background, right track
984                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
985                                         element = VisualStyleElement.ScrollBar.RightTrackHorizontal.Pressed;
986                                 else
987                                         element = bar.Enabled ?
988                                                 VisualStyleElement.ScrollBar.RightTrackHorizontal.Normal :
989                                                 VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled;
990                                 renderer = new VisualStyleRenderer (element);
991                                 Rectangle right_track_rect = new Rectangle (
992                                         bar.ThumbPos.Right,
993                                         0,
994                                         bar.ClientRectangle.Width - bar.ThumbPos.Right,
995                                         bar.ClientRectangle.Height);
996                                 if (clip.IntersectsWith (right_track_rect))
997                                         renderer.DrawBackground (dc, right_track_rect, clip);
998                                 #endregion
999                                 #region Buttons
1000                                 if (clip.IntersectsWith (bar.FirstArrowArea)) {
1001                                         if (!bar.Enabled)
1002                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftDisabled;
1003                                         else if (bar.firstbutton_state == ButtonState.Pushed)
1004                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
1005                                         else if (bar.FirstButtonEntered)
1006                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftHot;
1007                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1008                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftHover;
1009                                         else
1010                                                 element = VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
1011                                         renderer = new VisualStyleRenderer (element);
1012                                         renderer.DrawBackground (dc, bar.FirstArrowArea);
1013                                 }
1014                                 if (clip.IntersectsWith (bar.SecondArrowArea)) {
1015                                         if (!bar.Enabled)
1016                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightDisabled;
1017                                         else if (bar.secondbutton_state == ButtonState.Pushed)
1018                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
1019                                         else if (bar.SecondButtonEntered)
1020                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightHot;
1021                                         else if (ScrollBarHasHoverArrowButtonStyleVisualStyles && bar.Entered)
1022                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightHover;
1023                                         else
1024                                                 element = VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
1025                                         renderer = new VisualStyleRenderer (element);
1026                                         renderer.DrawBackground (dc, bar.SecondArrowArea);
1027                                 }
1028                                 #endregion
1029                                 #region Thumb and grip
1030                                 if (!bar.Enabled)
1031                                         element = VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled;
1032                                 else if (bar.ThumbPressed)
1033                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Pressed;
1034                                 else if (bar.ThumbEntered)
1035                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Hot;
1036                                 else
1037                                         element = VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Normal;
1038                                 renderer = new VisualStyleRenderer (element);
1039                                 renderer.DrawBackground (dc, bar.ThumbPos, clip);
1040
1041                                 if (bar.Enabled && bar.ThumbPos.Height >= 20) {
1042                                         element = VisualStyleElement.ScrollBar.GripperHorizontal.Normal;
1043                                         renderer = new VisualStyleRenderer (element);
1044                                         renderer.DrawBackground (dc, bar.ThumbPos, clip);
1045                                 }
1046                                 #endregion
1047                         }
1048                 }
1049                 public override bool ScrollBarHasHotElementStyles {
1050                         get {
1051                                 return ScrollBarAreElementsDefined;
1052                         }
1053                 }
1054                 public override bool ScrollBarHasPressedThumbStyle {
1055                         get {
1056                                 return ScrollBarAreElementsDefined;
1057                         }
1058                 }
1059                 const int WindowsVistaMajorVersion = 6;
1060                 static bool ScrollBarHasHoverArrowButtonStyleVisualStyles =
1061                         Environment.OSVersion.Version.Major >= WindowsVistaMajorVersion;
1062                 public override bool ScrollBarHasHoverArrowButtonStyle {
1063                         get {
1064                                 if (ScrollBarHasHoverArrowButtonStyleVisualStyles)
1065                                         return ScrollBarAreElementsDefined;
1066                                 return base.ScrollBarHasHoverArrowButtonStyle;
1067                         }
1068                 }
1069                 static bool ScrollBarAreElementsDefined {
1070                         get {
1071                                 return
1072                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ArrowButton.DownDisabled) &&
1073                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.GripperHorizontal.Normal) &&
1074                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.GripperVertical.Normal) &&
1075                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.LeftTrackHorizontal.Disabled) &&
1076                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.LowerTrackVertical.Disabled) &&
1077                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.RightTrackHorizontal.Disabled) &&
1078                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.SizeBox.LeftAlign) &&
1079                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ThumbButtonHorizontal.Disabled) &&
1080                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.ThumbButtonVertical.Disabled) &&
1081                                         VisualStyleRenderer.IsElementDefined (VisualStyleElement.ScrollBar.UpperTrackVertical.Disabled);
1082                         }
1083                 }
1084                 #endregion
1085                 #region StatusBar
1086                 protected override void DrawStatusBarBackground(Graphics dc, Rectangle clip, StatusBar sb) {
1087                         VisualStyleElement element = VisualStyleElement.Status.Bar.Normal;
1088                         if (!VisualStyleRenderer.IsElementDefined (element))
1089                                 base.DrawStatusBarBackground (dc, clip, sb);
1090                         new VisualStyleRenderer (element).DrawBackground (dc, sb.ClientRectangle, clip);
1091                 }
1092                 protected override void DrawStatusBarSizingGrip (Graphics dc, Rectangle clip, StatusBar sb, Rectangle area)
1093                 {
1094                         VisualStyleElement element = VisualStyleElement.Status.Gripper.Normal;
1095                         if (!VisualStyleRenderer.IsElementDefined (element))
1096                                 base.DrawStatusBarSizingGrip (dc, clip, sb, area);
1097                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1098                         Rectangle sizing_grip_rectangle = new Rectangle (Point.Empty, renderer.GetPartSize (dc, ThemeSizeType.True));
1099                         sizing_grip_rectangle.X = sb.Width - sizing_grip_rectangle.Width;
1100                         sizing_grip_rectangle.Y = sb.Height - sizing_grip_rectangle.Height;
1101                         renderer.DrawBackground (dc, sizing_grip_rectangle, clip);
1102                 }
1103                 protected override void DrawStatusBarPanelBackground (Graphics dc, Rectangle area, StatusBarPanel panel)
1104                 {
1105                         VisualStyleElement element = VisualStyleElement.Status.Pane.Normal;
1106                         if (!VisualStyleRenderer.IsElementDefined (element))
1107                                 base.DrawStatusBarPanelBackground (dc, area, panel);
1108                         new VisualStyleRenderer (element).DrawBackground (dc, area);
1109                 }
1110                 #endregion
1111                 #region TextBoxBase
1112                 static bool TextBoxBaseShouldPaint (TextBoxBase textBoxBase)
1113                 {
1114                         return textBoxBase.BorderStyle == BorderStyle.Fixed3D;
1115                 }
1116                 static VisualStyleElement TextBoxBaseGetVisualStyleElement (TextBoxBase textBoxBase)
1117                 {
1118                         if (!textBoxBase.Enabled)
1119                                 return VisualStyleElement.TextBox.TextEdit.Disabled;
1120                         if (textBoxBase.ReadOnly)
1121                                 return VisualStyleElement.TextBox.TextEdit.ReadOnly;
1122                         if (textBoxBase.Entered)
1123                                 return VisualStyleElement.TextBox.TextEdit.Hot;
1124                         if (textBoxBase.Focused)
1125                                 return VisualStyleElement.TextBox.TextEdit.Focused;
1126                         return VisualStyleElement.TextBox.TextEdit.Normal;
1127                 }
1128                 public override void TextBoxBaseFillBackground (TextBoxBase textBoxBase, Graphics g, Rectangle clippingArea)
1129                 {
1130                         if (!TextBoxBaseShouldPaint (textBoxBase)) {
1131                                 base.TextBoxBaseFillBackground (textBoxBase, g, clippingArea);
1132                                 return;
1133                         }
1134                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1135                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1136                                 base.TextBoxBaseFillBackground (textBoxBase, g, clippingArea);
1137                                 return;
1138                         }
1139                         Rectangle bounds = new Rectangle(Point.Empty, textBoxBase.Size);
1140                         bounds.X -= (bounds.Width - textBoxBase.ClientSize.Width) / 2;
1141                         bounds.Y -= (bounds.Height - textBoxBase.ClientSize.Height) / 2;
1142                         new VisualStyleRenderer (element).DrawBackground (g, bounds, clippingArea);
1143                 }
1144                 public override bool TextBoxBaseHandleWmNcPaint (TextBoxBase textBoxBase, ref Message m)
1145                 {
1146                         if (!TextBoxBaseShouldPaint (textBoxBase))
1147                                 return base.TextBoxBaseHandleWmNcPaint (textBoxBase, ref m);
1148                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1149                         if (!VisualStyleRenderer.IsElementDefined (element))
1150                                 return base.TextBoxBaseHandleWmNcPaint (textBoxBase, ref m);
1151                         PaintEventArgs e = XplatUI.PaintEventStart (ref m, textBoxBase.Handle, false);
1152                         new VisualStyleRenderer (element).DrawBackgroundExcludingArea (
1153                                 e.Graphics,
1154                                 new Rectangle (Point.Empty, textBoxBase.Size),
1155                                 new Rectangle (new Point ((textBoxBase.Width - textBoxBase.ClientSize.Width) / 2,
1156                                         (textBoxBase.Height - textBoxBase.ClientSize.Height) / 2),
1157                                         textBoxBase.ClientSize));
1158                         XplatUI.PaintEventEnd (ref m, textBoxBase.Handle, false);
1159                         return true;
1160                 }
1161                 public override bool TextBoxBaseShouldPaintBackground (TextBoxBase textBoxBase)
1162                 {
1163                         if (!TextBoxBaseShouldPaint (textBoxBase))
1164                                 return base.TextBoxBaseShouldPaintBackground (textBoxBase);
1165                         VisualStyleElement element = TextBoxBaseGetVisualStyleElement (textBoxBase);
1166                         if (!VisualStyleRenderer.IsElementDefined (element))
1167                                 return base.TextBoxBaseShouldPaintBackground (textBoxBase);
1168                         return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
1169                 }
1170                 #endregion
1171                 #region TrackBar
1172                 protected override Size TrackBarGetThumbSize (TrackBar trackBar)
1173                 {
1174                         VisualStyleElement element = TrackBarGetThumbVisualStyleElement (trackBar);
1175                         if (!VisualStyleRenderer.IsElementDefined (element))
1176                                 return base.TrackBarGetThumbSize (trackBar);
1177                         Graphics g = trackBar.CreateGraphics ();
1178                         Size result = new VisualStyleRenderer (element).GetPartSize (g, ThemeSizeType.True);
1179                         g.Dispose ();
1180                         return trackBar.Orientation == Orientation.Horizontal ? result : TrackBarRotateVerticalThumbSize (result);
1181                 }
1182                 static VisualStyleElement TrackBarGetThumbVisualStyleElement (TrackBar trackBar)
1183                 {
1184                         if (trackBar.Orientation == Orientation.Horizontal)
1185                                 switch (trackBar.TickStyle) {
1186                                 case TickStyle.BottomRight:
1187                                 case TickStyle.None:
1188                                         return TrackBarGetHorizontalThumbBottomVisualStyleElement (trackBar);
1189                                 case TickStyle.TopLeft:
1190                                         return TrackBarGetHorizontalThumbTopVisualStyleElement (trackBar);
1191                                 default:
1192                                         return TrackBarGetHorizontalThumbVisualStyleElement (trackBar);
1193                                 }
1194                         else
1195                                 switch (trackBar.TickStyle) {
1196                                 case TickStyle.BottomRight:
1197                                 case TickStyle.None:
1198                                         return TrackBarGetVerticalThumbRightVisualStyleElement (trackBar);
1199                                 case TickStyle.TopLeft:
1200                                         return TrackBarGetVerticalThumbLeftVisualStyleElement (trackBar);
1201                                 default:
1202                                         return TrackBarGetVerticalThumbVisualStyleElement (trackBar);
1203                                 }
1204                 }
1205                 static Size TrackBarRotateVerticalThumbSize (Size value)
1206                 {
1207                         int temporary = value.Width;
1208                         value.Width = value.Height;
1209                         value.Height = temporary;
1210                         return value;
1211                 }
1212                 #region Track
1213                 protected override void TrackBarDrawHorizontalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
1214                 {
1215                         VisualStyleElement element = VisualStyleElement.TrackBar.Track.Normal;
1216                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1217                                 base.TrackBarDrawHorizontalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1218                                 return;
1219                         }
1220                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1221                         renderer.DrawBackground (dc, new Rectangle (channel_startpoint, new Size (thumb_area.Width, renderer.GetPartSize (dc, ThemeSizeType.True).Height)), clippingArea);
1222                 }
1223                 protected override void TrackBarDrawVerticalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
1224                 {
1225                         VisualStyleElement element = VisualStyleElement.TrackBar.TrackVertical.Normal;
1226                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1227                                 base.TrackBarDrawVerticalTrack (dc, thumb_area, channel_startpoint, clippingArea);
1228                                 return;
1229                         }
1230                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
1231                         renderer.DrawBackground (dc, new Rectangle (channel_startpoint, new Size (renderer.GetPartSize (dc, ThemeSizeType.True).Width, thumb_area.Height)), clippingArea);
1232                 }
1233                 #endregion
1234                 #region Thumb
1235                 static bool TrackBarIsDisabled (TrackBar trackBar)
1236                 {
1237                         return !trackBar.Enabled;
1238                 }
1239                 static bool TrackBarIsHot (TrackBar trackBar)
1240                 {
1241                         return trackBar.ThumbEntered;
1242                 }
1243                 static bool TrackBarIsPressed (TrackBar trackBar)
1244                 {
1245                         return trackBar.thumb_pressed;
1246                 }
1247                 static bool TrackBarIsFocused (TrackBar trackBar)
1248                 {
1249                         return trackBar.Focused;
1250                 }
1251                 #region Horizontal
1252                 protected override void TrackBarDrawHorizontalThumbBottom (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1253                 {
1254                         VisualStyleElement element = TrackBarGetHorizontalThumbBottomVisualStyleElement (trackBar);
1255                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1256                                 base.TrackBarDrawHorizontalThumbBottom (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1257                                 return;
1258                         }
1259                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1260                 }
1261                 static VisualStyleElement TrackBarGetHorizontalThumbBottomVisualStyleElement (TrackBar trackBar)
1262                 {
1263                         if (TrackBarIsDisabled (trackBar))
1264                                 return VisualStyleElement.TrackBar.ThumbBottom.Disabled;
1265                         else if (TrackBarIsPressed (trackBar))
1266                                 return VisualStyleElement.TrackBar.ThumbBottom.Pressed;
1267                         else if (TrackBarIsHot (trackBar))
1268                                 return VisualStyleElement.TrackBar.ThumbBottom.Hot;
1269                         else if (TrackBarIsFocused (trackBar))
1270                                 return VisualStyleElement.TrackBar.ThumbBottom.Focused;
1271                         return VisualStyleElement.TrackBar.ThumbBottom.Normal;
1272                 }
1273                 protected override void TrackBarDrawHorizontalThumbTop (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1274                 {
1275                         VisualStyleElement element = TrackBarGetHorizontalThumbTopVisualStyleElement (trackBar);
1276                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1277                                 base.TrackBarDrawHorizontalThumbTop (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1278                                 return;
1279                         }
1280                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1281                 }
1282                 static VisualStyleElement TrackBarGetHorizontalThumbTopVisualStyleElement (TrackBar trackBar)
1283                 {
1284                         if (TrackBarIsDisabled (trackBar))
1285                                 return VisualStyleElement.TrackBar.ThumbTop.Disabled;
1286                         else if (TrackBarIsPressed (trackBar))
1287                                 return VisualStyleElement.TrackBar.ThumbTop.Pressed;
1288                         else if (TrackBarIsHot (trackBar))
1289                                 return VisualStyleElement.TrackBar.ThumbTop.Hot;
1290                         else if (TrackBarIsFocused (trackBar))
1291                                 return VisualStyleElement.TrackBar.ThumbTop.Focused;
1292                         return VisualStyleElement.TrackBar.ThumbTop.Normal;
1293                 }
1294                 protected override void TrackBarDrawHorizontalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1295                 {
1296                         VisualStyleElement element = TrackBarGetHorizontalThumbVisualStyleElement (trackBar);
1297                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1298                                 base.TrackBarDrawHorizontalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1299                                 return;
1300                         }
1301                         new VisualStyleRenderer (element).DrawBackground (dc, thumb_pos, clippingArea);
1302                 }
1303                 static VisualStyleElement TrackBarGetHorizontalThumbVisualStyleElement (TrackBar trackBar)
1304                 {
1305                         if (TrackBarIsDisabled (trackBar))
1306                                 return VisualStyleElement.TrackBar.Thumb.Disabled;
1307                         else if (TrackBarIsPressed (trackBar))
1308                                 return VisualStyleElement.TrackBar.Thumb.Pressed;
1309                         else if (TrackBarIsHot (trackBar))
1310                                 return VisualStyleElement.TrackBar.Thumb.Hot;
1311                         else if (TrackBarIsFocused (trackBar))
1312                                 return VisualStyleElement.TrackBar.Thumb.Focused;
1313                         return VisualStyleElement.TrackBar.Thumb.Normal;
1314                 }
1315                 #endregion
1316                 #region Vertical
1317                 static Rectangle TrackBarRotateVerticalThumbSize (Rectangle value)
1318                 {
1319                         int temporary = value.Width;
1320                         value.Width = value.Height;
1321                         value.Height = temporary;
1322                         return value;
1323                 }
1324                 protected override void TrackBarDrawVerticalThumbRight (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1325                 {
1326                         VisualStyleElement element = TrackBarGetVerticalThumbRightVisualStyleElement (trackBar);
1327                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1328                                 base.TrackBarDrawVerticalThumbRight (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1329                                 return;
1330                         }
1331                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1332                 }
1333                 static VisualStyleElement TrackBarGetVerticalThumbRightVisualStyleElement (TrackBar trackBar)
1334                 {
1335                         if (TrackBarIsDisabled (trackBar))
1336                                 return VisualStyleElement.TrackBar.ThumbRight.Disabled;
1337                         else if (TrackBarIsPressed (trackBar))
1338                                 return VisualStyleElement.TrackBar.ThumbRight.Pressed;
1339                         else if (TrackBarIsHot (trackBar))
1340                                 return VisualStyleElement.TrackBar.ThumbRight.Hot;
1341                         else if (TrackBarIsFocused (trackBar))
1342                                 return VisualStyleElement.TrackBar.ThumbRight.Focused;
1343                         return VisualStyleElement.TrackBar.ThumbRight.Normal;
1344                 }
1345                 protected override void TrackBarDrawVerticalThumbLeft (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1346                 {
1347                         VisualStyleElement element = TrackBarGetVerticalThumbLeftVisualStyleElement (trackBar);
1348                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1349                                 base.TrackBarDrawVerticalThumbLeft (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1350                                 return;
1351                         }
1352                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1353                 }
1354                 static VisualStyleElement TrackBarGetVerticalThumbLeftVisualStyleElement (TrackBar trackBar)
1355                 {
1356                         if (TrackBarIsDisabled (trackBar))
1357                                 return VisualStyleElement.TrackBar.ThumbLeft.Disabled;
1358                         else if (TrackBarIsPressed (trackBar))
1359                                 return VisualStyleElement.TrackBar.ThumbLeft.Pressed;
1360                         else if (TrackBarIsHot (trackBar))
1361                                 return VisualStyleElement.TrackBar.ThumbLeft.Hot;
1362                         else if (TrackBarIsFocused (trackBar))
1363                                 return VisualStyleElement.TrackBar.ThumbLeft.Focused;
1364                         return VisualStyleElement.TrackBar.ThumbLeft.Normal;
1365                 }
1366                 protected override void TrackBarDrawVerticalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
1367                 {
1368                         VisualStyleElement element = TrackBarGetVerticalThumbVisualStyleElement (trackBar);
1369                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1370                                 base.TrackBarDrawVerticalThumb (dc, thumb_pos, br_thumb, clippingArea, trackBar);
1371                                 return;
1372                         }
1373                         new VisualStyleRenderer (element).DrawBackground (dc, TrackBarRotateVerticalThumbSize (thumb_pos), clippingArea);
1374                 }
1375                 static VisualStyleElement TrackBarGetVerticalThumbVisualStyleElement (TrackBar trackBar)
1376                 {
1377                         if (TrackBarIsDisabled (trackBar))
1378                                 return VisualStyleElement.TrackBar.ThumbVertical.Disabled;
1379                         else if (TrackBarIsPressed (trackBar))
1380                                 return VisualStyleElement.TrackBar.ThumbVertical.Pressed;
1381                         else if (TrackBarIsHot (trackBar))
1382                                 return VisualStyleElement.TrackBar.ThumbVertical.Hot;
1383                         else if (TrackBarIsFocused (trackBar))
1384                                 return VisualStyleElement.TrackBar.ThumbVertical.Focused;
1385                         return VisualStyleElement.TrackBar.ThumbVertical.Normal;
1386                 }
1387                 #endregion
1388                 #endregion
1389                 #region Ticks
1390                 const EdgeStyle TrackBarTickEdgeStyle = EdgeStyle.Bump;
1391                 const EdgeEffects TrackBarTickEdgeEffects = EdgeEffects.None;
1392                 #region Horizontal
1393                 protected override ITrackBarTickPainter TrackBarGetHorizontalTickPainter (Graphics g)
1394                 {
1395                         if (!VisualStyleRenderer.IsElementDefined (VisualStyleElement.TrackBar.Ticks.Normal))
1396                                 return base.TrackBarGetHorizontalTickPainter (g);
1397                         return new TrackBarHorizontalTickPainter (g);
1398                 }
1399                 class TrackBarHorizontalTickPainter : ITrackBarTickPainter
1400                 {
1401                         readonly Graphics g;
1402                         readonly VisualStyleRenderer renderer;
1403                         public TrackBarHorizontalTickPainter (Graphics g)
1404                         {
1405                                 this.g = g;
1406                                 renderer = new VisualStyleRenderer (VisualStyleElement.TrackBar.Ticks.Normal);
1407                         }
1408                         public void Paint (float x1, float y1, float x2, float y2)
1409                         {
1410                                 renderer.DrawEdge (g, new Rectangle (
1411                                         (int)Math.Round (x1),
1412                                         (int)Math.Round (y1),
1413                                         1,
1414                                         (int)Math.Round (y2 - y1) + 1), Edges.Left, TrackBarTickEdgeStyle, TrackBarTickEdgeEffects);
1415                         }
1416                 }
1417                 #endregion
1418                 #region Vertical
1419                 protected override ITrackBarTickPainter TrackBarGetVerticalTickPainter (Graphics g)
1420                 {
1421                         if (!VisualStyleRenderer.IsElementDefined (VisualStyleElement.TrackBar.TicksVertical.Normal))
1422                                 return base.TrackBarGetVerticalTickPainter (g);
1423                         return new TrackBarVerticalTickPainter (g);
1424                 }
1425                 class TrackBarVerticalTickPainter : ITrackBarTickPainter
1426                 {
1427                         readonly Graphics g;
1428                         readonly VisualStyleRenderer renderer;
1429                         public TrackBarVerticalTickPainter (Graphics g)
1430                         {
1431                                 this.g = g;
1432                                 renderer = new VisualStyleRenderer (VisualStyleElement.TrackBar.TicksVertical.Normal);
1433                         }
1434                         public void Paint (float x1, float y1, float x2, float y2)
1435                         {
1436                                 renderer.DrawEdge (g, new Rectangle (
1437                                         (int)Math.Round (x1),
1438                                         (int)Math.Round (y1),
1439                                         (int)Math.Round (x2 - x1) + 1,
1440                                         1), Edges.Top, TrackBarTickEdgeStyle, TrackBarTickEdgeEffects);
1441                         }
1442                 }
1443                 #endregion
1444                 #endregion
1445                 public override bool TrackBarHasHotThumbStyle {
1446                         get {
1447                                 return true;
1448                         }
1449                 }
1450                 #endregion
1451                 #region ToolBar
1452                 static bool ToolBarIsDisabled (ToolBarItem item)
1453                 {
1454                         return !item.Button.Enabled;
1455                 }
1456                 static bool ToolBarIsPressed (ToolBarItem item)
1457                 {
1458                         return item.Pressed;
1459                 }
1460                 static bool ToolBarIsChecked (ToolBarItem item)
1461                 {
1462                         return item.Button.Pushed;
1463                 }
1464                 static bool ToolBarIsHot (ToolBarItem item)
1465                 {
1466                         return item.Hilight;
1467                 }
1468                 #region Border
1469                 protected override void DrawToolBarButtonBorder (Graphics dc, ToolBarItem item, bool is_flat)
1470                 {
1471                         if (item.Button.Style == ToolBarButtonStyle.Separator)
1472                                 return;
1473                         VisualStyleElement element;
1474                         if (item.Button.Style == ToolBarButtonStyle.DropDownButton)
1475                                 element = ToolBarGetDropDownButtonVisualStyleElement (item);
1476                         else
1477                                 element = ToolBarGetButtonVisualStyleElement (item);
1478                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1479                                 base.DrawToolBarButtonBorder (dc, item, is_flat);
1480                                 return;
1481                         }
1482                         Rectangle rectangle = item.Rectangle;
1483                         if (item.Button.Style == ToolBarButtonStyle.DropDownButton && item.Button.Parent.DropDownArrows)
1484                                 rectangle.Width -= ToolBarDropDownWidth;
1485                         new VisualStyleRenderer (element).DrawBackground (dc, rectangle);
1486                 }
1487                 private static VisualStyleElement ToolBarGetDropDownButtonVisualStyleElement (ToolBarItem item)
1488                 {
1489                         if (item.Button.Parent.DropDownArrows) {
1490                                 if (ToolBarIsDisabled (item))
1491                                         return VisualStyleElement.ToolBar.SplitButton.Disabled;
1492                                 if (ToolBarIsPressed (item))
1493                                         return VisualStyleElement.ToolBar.SplitButton.Pressed;
1494                                 if (ToolBarIsChecked (item))
1495                                         if (ToolBarIsHot (item))
1496                                                 return VisualStyleElement.ToolBar.SplitButton.HotChecked;
1497                                         else
1498                                                 return VisualStyleElement.ToolBar.SplitButton.Checked;
1499                                 if (ToolBarIsHot (item))
1500                                         return VisualStyleElement.ToolBar.SplitButton.Hot;
1501                                 return VisualStyleElement.ToolBar.SplitButton.Normal;
1502                         } else {
1503                                 if (ToolBarIsDisabled (item))
1504                                         return VisualStyleElement.ToolBar.DropDownButton.Disabled;
1505                                 if (ToolBarIsPressed (item))
1506                                         return VisualStyleElement.ToolBar.DropDownButton.Pressed;
1507                                 if (ToolBarIsChecked (item))
1508                                         if (ToolBarIsHot (item))
1509                                                 return VisualStyleElement.ToolBar.DropDownButton.HotChecked;
1510                                         else
1511                                                 return VisualStyleElement.ToolBar.DropDownButton.Checked;
1512                                 if (ToolBarIsHot (item))
1513                                         return VisualStyleElement.ToolBar.DropDownButton.Hot;
1514                                 return VisualStyleElement.ToolBar.DropDownButton.Normal;
1515                         }
1516                 }
1517                 private static VisualStyleElement ToolBarGetButtonVisualStyleElement (ToolBarItem item)
1518                 {
1519                         if (ToolBarIsDisabled (item))
1520                                 return VisualStyleElement.ToolBar.Button.Disabled;
1521                         if (ToolBarIsPressed (item))
1522                                 return VisualStyleElement.ToolBar.Button.Pressed;
1523                         if (ToolBarIsChecked (item))
1524                                 if (ToolBarIsHot (item))
1525                                         return VisualStyleElement.ToolBar.Button.HotChecked;
1526                                 else
1527                                         return VisualStyleElement.ToolBar.Button.Checked;
1528                         if (ToolBarIsHot (item))
1529                                 return VisualStyleElement.ToolBar.Button.Hot;
1530                         return VisualStyleElement.ToolBar.Button.Normal;
1531                 }
1532                 #endregion
1533                 #region Separator
1534                 protected override void DrawToolBarSeparator (Graphics dc, ToolBarItem item)
1535                 {
1536                         VisualStyleElement element = ToolBarGetSeparatorVisualStyleElement (item);
1537                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1538                                 base.DrawToolBarSeparator (dc, item);
1539                                 return;
1540                         }
1541                         Rectangle area = item.Rectangle;
1542                         new VisualStyleRenderer (element).DrawBackground (dc, item.Rectangle);
1543                 }
1544                 static VisualStyleElement ToolBarGetSeparatorVisualStyleElement (ToolBarItem toolBarItem)
1545                 {
1546                         return toolBarItem.Button.Parent.Vertical ?
1547                                 VisualStyleElement.ToolBar.SeparatorVertical.Normal :
1548                                 VisualStyleElement.ToolBar.SeparatorHorizontal.Normal;
1549                 }
1550                 #endregion
1551                 #region Toggle button background
1552                 protected override void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarItem item)
1553                 {
1554                         if (!VisualStyleRenderer.IsElementDefined (ToolBarGetButtonVisualStyleElement (item)))
1555                                 base.DrawToolBarToggleButtonBackground (dc, item);
1556                 }
1557                 #endregion
1558                 #region Drop down arrow
1559                 protected override void DrawToolBarDropDownArrow (Graphics dc, ToolBarItem item, bool is_flat)
1560                 {
1561                         VisualStyleElement element = ToolBarGetDropDownArrowVisualStyleElement (item);
1562                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1563                                 base.DrawToolBarDropDownArrow (dc, item, is_flat);
1564                                 return;
1565                         }
1566                         Rectangle rect = item.Rectangle;
1567                         rect.X = item.Rectangle.Right - ToolBarDropDownWidth;
1568                         rect.Width = ToolBarDropDownWidth;
1569                         new VisualStyleRenderer (element).DrawBackground (dc, rect);
1570                 }
1571                 private static VisualStyleElement ToolBarGetDropDownArrowVisualStyleElement (ToolBarItem item)
1572                 {
1573                         if (ToolBarIsDisabled (item))
1574                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Disabled;
1575                         if (ToolBarIsPressed (item))
1576                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Pressed;
1577                         if (ToolBarIsChecked (item))
1578                                 if (ToolBarIsHot (item))
1579                                         return VisualStyleElement.ToolBar.SplitButtonDropDown.HotChecked;
1580                                 else
1581                                         return VisualStyleElement.ToolBar.SplitButtonDropDown.Checked;
1582                         if (ToolBarIsHot (item))
1583                                 return VisualStyleElement.ToolBar.SplitButtonDropDown.Hot;
1584                         return VisualStyleElement.ToolBar.SplitButtonDropDown.Normal;
1585                 }
1586                 #endregion
1587                 public override bool ToolBarHasHotElementStyles (ToolBar toolBar)
1588                 {
1589                         return true;
1590                 }
1591                 public override bool ToolBarHasHotCheckedElementStyles {
1592                         get {
1593                                 return true;
1594                         }
1595                 }
1596                 #endregion
1597                 #region ToolTip
1598                 protected override void ToolTipDrawBackground (Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control)
1599                 {
1600                         VisualStyleElement element = VisualStyleElement.ToolTip.Standard.Normal;
1601                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1602                                 base.ToolTipDrawBackground (dc, clip_rectangle, control);
1603                                 return;
1604                         }
1605                         new VisualStyleRenderer (element).DrawBackground (dc, control.Bounds);
1606                 }
1607                 public override bool ToolTipTransparentBackground {
1608                         get {
1609                                 VisualStyleElement element = VisualStyleElement.ToolTip.Standard.Normal;
1610                                 if (!VisualStyleRenderer.IsElementDefined (element))
1611                                         return base.ToolTipTransparentBackground;
1612                                 return new VisualStyleRenderer (element).IsBackgroundPartiallyTransparent ();
1613                         }
1614                 }
1615                 #endregion
1616                 #region TreeView
1617                 [MonoTODO("Use the sizing information provided by the VisualStyles API.")]
1618                 public override void TreeViewDrawNodePlusMinus (TreeView treeView, TreeNode node, Graphics dc, int x, int middle)
1619                 {
1620                         VisualStyleElement element = node.IsExpanded ?
1621                                 VisualStyleElement.TreeView.Glyph.Opened : 
1622                                 VisualStyleElement.TreeView.Glyph.Closed;
1623                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1624                                 base.TreeViewDrawNodePlusMinus (treeView, node, dc, x, middle);
1625                                 return;
1626                         }
1627                         new VisualStyleRenderer (element).DrawBackground (dc, new Rectangle (x, middle - 4, 9, 9));
1628                 }
1629                 #endregion
1630                 #region UpDownBase
1631                 public override void UpDownBaseDrawButton (Graphics g, Rectangle bounds, bool top, PushButtonState state)
1632                 {
1633                         VisualStyleElement element;
1634                         if (top)
1635                                 switch (state) {
1636                                 case PushButtonState.Disabled:
1637                                         element = VisualStyleElement.Spin.Up.Disabled;
1638                                         break;
1639                                 case PushButtonState.Pressed:
1640                                         element = VisualStyleElement.Spin.Up.Pressed;
1641                                         break;
1642                                 case PushButtonState.Hot:
1643                                         element = VisualStyleElement.Spin.Up.Hot;
1644                                         break;
1645                                 default:
1646                                         element = VisualStyleElement.Spin.Up.Normal;
1647                                         break;
1648                                 }
1649                         else
1650                                 switch (state) {
1651                                 case PushButtonState.Disabled:
1652                                         element = VisualStyleElement.Spin.Down.Disabled;
1653                                         break;
1654                                 case PushButtonState.Pressed:
1655                                         element = VisualStyleElement.Spin.Down.Pressed;
1656                                         break;
1657                                 case PushButtonState.Hot:
1658                                         element = VisualStyleElement.Spin.Down.Hot;
1659                                         break;
1660                                 default:
1661                                         element = VisualStyleElement.Spin.Down.Normal;
1662                                         break;
1663                                 }
1664                         if (!VisualStyleRenderer.IsElementDefined (element)) {
1665                                 base.UpDownBaseDrawButton (g, bounds, top, state);
1666                                 return;
1667                         }
1668                         new VisualStyleRenderer (element).DrawBackground (g, bounds);
1669                 }
1670                 public override bool UpDownBaseHasHotButtonStyle {
1671                         get {
1672                                 return true;
1673                         }
1674                 }
1675                 #endregion
1676
1677                 static bool AreEqual (VisualStyleElement value1, VisualStyleElement value2)
1678                 {
1679                         return
1680                                 value1.ClassName == value1.ClassName &&
1681                                 value1.Part == value2.Part &&
1682                                 value1.State == value2.State;
1683                 }
1684         }
1685 }