Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThemeWin32Classic.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) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //      Peter Bartok, pbartok@novell.com
25 //      John BouAntoun, jba-mono@optusnet.com.au
26 //      Marek Safar, marek.safar@seznam.cz
27 //      Alexander Olk, alex.olk@googlemail.com
28 //
29
30 using System.ComponentModel;
31 using System.Data;
32 using System.Drawing;
33 using System.Drawing.Drawing2D;
34 using System.Drawing.Imaging;
35 using System.Drawing.Printing;
36 using System.Drawing.Text;
37 using System.Text;
38 using System.Windows.Forms.Theming;
39
40 namespace System.Windows.Forms
41 {
42
43         internal class ThemeWin32Classic : Theme
44         {               
45                 public override Version Version {
46                         get {
47                                 return new Version(0, 1, 0, 0);
48                         }
49                 }
50
51                 /* Hardcoded colour values not exposed in the API constants in all configurations */
52                 protected static readonly Color arrow_color = Color.Black;
53                 protected static readonly Color pen_ticks_color = Color.Black;
54                 protected static StringFormat string_format_menu_text;
55                 protected static StringFormat string_format_menu_shortcut;
56                 protected static StringFormat string_format_menu_menubar_text;
57                 static ImageAttributes imagedisabled_attributes;
58                 Font window_border_font;
59                 const int SEPARATOR_HEIGHT = 6;
60                 const int SEPARATOR_MIN_WIDTH = 20;
61                 const int SM_CXBORDER = 1;
62                 const int SM_CYBORDER = 1;              
63                 const int MENU_TAB_SPACE = 8;           // Pixels added to the width of an item because of a tabd
64                 const int MENU_BAR_ITEMS_SPACE = 8;     // Space between menu bar items
65                 const int CheckSize = 13;
66
67                 #region Principal Theme Methods
68                 public ThemeWin32Classic ()
69                 {                       
70                         ResetDefaults ();
71                 }
72
73                 public override void ResetDefaults() {
74                         defaultWindowBackColor = this.ColorWindow;
75                         defaultWindowForeColor = this.ColorControlText;
76                         window_border_font = null;
77                         
78                         /* Menu string formats */
79                         string_format_menu_text = new StringFormat ();
80                         string_format_menu_text.LineAlignment = StringAlignment.Center;
81                         string_format_menu_text.Alignment = StringAlignment.Near;
82                         string_format_menu_text.HotkeyPrefix = HotkeyPrefix.Show;
83                         string_format_menu_text.SetTabStops (0f, new float [] { 50f });
84                         string_format_menu_text.FormatFlags |= StringFormatFlags.NoWrap;
85
86                         string_format_menu_shortcut = new StringFormat ();      
87                         string_format_menu_shortcut.LineAlignment = StringAlignment.Center;
88                         string_format_menu_shortcut.Alignment = StringAlignment.Far;
89
90                         string_format_menu_menubar_text = new StringFormat ();
91                         string_format_menu_menubar_text.LineAlignment = StringAlignment.Center;
92                         string_format_menu_menubar_text.Alignment = StringAlignment.Center;
93                         string_format_menu_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
94                 }
95
96                 public override bool DoubleBufferingSupported {
97                         get {return true; }
98                 }
99
100                 public override int HorizontalScrollBarHeight {
101                         get {
102                                 return XplatUI.HorizontalScrollBarHeight;
103                         }
104                 }
105
106                 public override int VerticalScrollBarWidth {
107                         get {
108                                 return XplatUI.VerticalScrollBarWidth;
109                         }
110                 }
111                 
112                 public override Font WindowBorderFont {
113                         get {
114                                 return window_border_font ?? (window_border_font = new Font(FontFamily.GenericSansSerif, 8.25f, FontStyle.Bold));
115                         }
116                 }
117
118                 #endregion      // Principal Theme Methods
119
120                 #region Internal Methods
121                 protected Brush GetControlBackBrush (Color c) {
122                         if (c.ToArgb () == DefaultControlBackColor.ToArgb ())
123                                 return SystemBrushes.Control;
124                         return ResPool.GetSolidBrush (c);
125                 }
126
127                 protected Brush GetControlForeBrush (Color c) {
128                         if (c.ToArgb () == DefaultControlForeColor.ToArgb ())
129                                 return SystemBrushes.ControlText;
130                         return ResPool.GetSolidBrush (c);
131                 }
132                 #endregion      // Internal Methods
133
134                 #region Control
135                 public override Font GetLinkFont (Control control) 
136                 {
137                         return new Font (control.Font.FontFamily, control.Font.Size, control.Font.Style | FontStyle.Underline, control.Font.Unit); 
138                 }
139                 #endregion      // Control
140
141                 #region OwnerDraw Support
142                 public  override void DrawOwnerDrawBackground (DrawItemEventArgs e)
143                 {
144                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
145                                 e.Graphics.FillRectangle (SystemBrushes.Highlight, e.Bounds);
146                                 return;
147                         }
148
149                         e.Graphics.FillRectangle (ResPool.GetSolidBrush(e.BackColor), e.Bounds);
150                 }
151
152                 public  override void DrawOwnerDrawFocusRectangle (DrawItemEventArgs e)
153                 {
154                         if (e.State == DrawItemState.Focus)
155                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, e.ForeColor, e.BackColor);
156                 }
157                 #endregion      // OwnerDraw Support
158
159                 #region Button
160                 #region Standard Button Style
161                 public override void DrawButton (Graphics g, Button b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
162                 {
163                         // Draw Button Background
164                         DrawButtonBackground (g, b, clipRectangle);
165
166                         // If we have an image, draw it
167                         if (imageBounds.Size != Size.Empty)
168                                 DrawButtonImage (g, b, imageBounds);
169
170                         // If we're focused, draw a focus rectangle
171                         if (b.Focused && b.Enabled && b.ShowFocusCues)
172                                 DrawButtonFocus (g, b);
173
174                         // If we have text, draw it
175                         if (textBounds != Rectangle.Empty)
176                                 DrawButtonText (g, b, textBounds);
177                 }
178
179                 public virtual void DrawButtonBackground (Graphics g, Button button, Rectangle clipArea) 
180                 {
181                         if (button.Pressed)
182                                 ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor);
183                         else if (button.InternalSelected)
184                                 ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor);
185                         else if (button.Entered)
186                                 ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor);
187                         else if (!button.Enabled)
188                                 ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor);
189                         else
190                                 ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor);
191                 }
192
193                 public virtual void DrawButtonFocus (Graphics g, Button button)
194                 {
195                         ControlPaint.DrawFocusRectangle (g, Rectangle.Inflate (button.ClientRectangle, -4, -4));
196                 }
197
198                 public virtual void DrawButtonImage (Graphics g, ButtonBase button, Rectangle imageBounds)
199                 {
200                         if (button.Enabled)
201                                 g.DrawImage (button.Image, imageBounds);
202                         else
203                                 CPDrawImageDisabled (g, button.Image, imageBounds.Left, imageBounds.Top, ColorControl);
204                 }
205
206                 public virtual void DrawButtonText (Graphics g, ButtonBase button, Rectangle textBounds)
207                 {
208                         // Ensure that at least one line is going to get displayed.
209                         // Line limit does not ensure that despite its description.
210                         textBounds.Height = Math.Max (textBounds.Height, button.Font.Height);
211                         
212                         if (button.Enabled)
213                                 TextRenderer.DrawTextInternal (g, button.Text, button.Font, textBounds, button.ForeColor, button.TextFormatFlags, button.UseCompatibleTextRendering);
214                         else
215                                 DrawStringDisabled20 (g, button.Text, button.Font, textBounds, button.BackColor, button.TextFormatFlags, button.UseCompatibleTextRendering);
216                 }
217                 #endregion
218
219                 #region FlatStyle Button Style
220                 public override void DrawFlatButton (Graphics g, ButtonBase b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
221                 {
222                         // Draw Button Background
223                         if (b.BackgroundImage == null)
224                                 DrawFlatButtonBackground (g, b, clipRectangle);
225
226                         // If we have an image, draw it
227                         if (imageBounds.Size != Size.Empty)
228                                 DrawFlatButtonImage (g, b, imageBounds);
229
230                         // If we're focused, draw a focus rectangle
231                         if (b.Focused && b.Enabled && b.ShowFocusCues)
232                                 DrawFlatButtonFocus (g, b);
233
234                         // If we have text, draw it
235                         if (textBounds != Rectangle.Empty)
236                                 DrawFlatButtonText (g, b, textBounds);
237                 }
238
239                 public virtual void DrawFlatButtonBackground (Graphics g, ButtonBase button, Rectangle clipArea)
240                 {
241                         if (button.Pressed)
242                                 ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor, button.FlatAppearance);
243                         else if (button.InternalSelected) {
244                                 if (button.Entered) 
245                                         ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Default | ButtonThemeState.Entered, button.BackColor, button.ForeColor, button.FlatAppearance);
246                                 else
247                                         ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor, button.FlatAppearance);
248                         }
249                         else if (button.Entered)
250                                 ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor, button.FlatAppearance);
251                         else if (!button.Enabled)
252                                 ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor, button.FlatAppearance);
253                         else
254                                 ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor, button.FlatAppearance);
255                 }
256
257                 public virtual void DrawFlatButtonFocus (Graphics g, ButtonBase button)
258                 {
259                         if (!button.Pressed) {
260                                 Color focus_color = ControlPaint.Dark (button.BackColor);
261                                 g.DrawRectangle (ResPool.GetPen (focus_color), new Rectangle (button.ClientRectangle.Left + 4, button.ClientRectangle.Top + 4, button.ClientRectangle.Width - 9, button.ClientRectangle.Height - 9));
262                         }
263                 }
264
265                 public virtual void DrawFlatButtonImage (Graphics g, ButtonBase button, Rectangle imageBounds)
266                 {
267                         // No changes from Standard for image for this theme
268                         DrawButtonImage (g, button, imageBounds);
269                 }
270
271                 public virtual void DrawFlatButtonText (Graphics g, ButtonBase button, Rectangle textBounds)
272                 {
273                         // No changes from Standard for text for this theme
274                         DrawButtonText (g, button, textBounds);
275                 }
276                 #endregion
277
278                 #region Popup Button Style
279                 public override void DrawPopupButton (Graphics g, Button b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
280                 {
281                         // Draw Button Background
282                         DrawPopupButtonBackground (g, b, clipRectangle);
283
284                         // If we have an image, draw it
285                         if (imageBounds.Size != Size.Empty)
286                                 DrawPopupButtonImage (g, b, imageBounds);
287
288                         // If we're focused, draw a focus rectangle
289                         if (b.Focused && b.Enabled && b.ShowFocusCues)
290                                 DrawPopupButtonFocus (g, b);
291
292                         // If we have text, draw it
293                         if (textBounds != Rectangle.Empty)
294                                 DrawPopupButtonText (g, b, textBounds);
295                 }
296
297                 public virtual void DrawPopupButtonBackground (Graphics g, Button button, Rectangle clipArea)
298                 {
299                         if (button.Pressed)
300                                 ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor);
301                         else if (button.Entered)
302                                 ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor);
303                         else if (button.InternalSelected)
304                                 ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor);
305                         else if (!button.Enabled)
306                                 ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor);
307                         else
308                                 ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor);
309                 }
310
311                 public virtual void DrawPopupButtonFocus (Graphics g, Button button)
312                 {
313                         // No changes from Standard for image for this theme
314                         DrawButtonFocus (g, button);
315                 }
316
317                 public virtual void DrawPopupButtonImage (Graphics g, Button button, Rectangle imageBounds)
318                 {
319                         // No changes from Standard for image for this theme
320                         DrawButtonImage (g, button, imageBounds);
321                 }
322
323                 public virtual void DrawPopupButtonText (Graphics g, Button button, Rectangle textBounds)
324                 {
325                         // No changes from Standard for image for this theme
326                         DrawButtonText (g, button, textBounds);
327                 }
328                 #endregion
329
330                 #region Button Layout Calculations
331                 public override Size CalculateButtonAutoSize (Button button)
332                 {
333                         Size ret_size = Size.Empty;
334                         Size text_size = TextRenderer.MeasureTextInternal (button.Text, button.Font, button.UseCompatibleTextRendering);
335                         Size image_size = button.Image == null ? Size.Empty : button.Image.Size;
336                         
337                         // Pad the text size
338                         if (button.Text.Length != 0) {
339                                 text_size.Height += 4;
340                                 text_size.Width += 4;
341                         }
342                         
343                         switch (button.TextImageRelation) {
344                                 case TextImageRelation.Overlay:
345                                         ret_size.Height = Math.Max (button.Text.Length == 0 ? 0 : text_size.Height, image_size.Height);
346                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
347                                         break;
348                                 case TextImageRelation.ImageAboveText:
349                                 case TextImageRelation.TextAboveImage:
350                                         ret_size.Height = text_size.Height + image_size.Height;
351                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
352                                         break;
353                                 case TextImageRelation.ImageBeforeText:
354                                 case TextImageRelation.TextBeforeImage:
355                                         ret_size.Height = Math.Max (text_size.Height, image_size.Height);
356                                         ret_size.Width = text_size.Width + image_size.Width;
357                                         break;
358                         }
359
360                         // Pad the result
361                         ret_size.Height += (button.Padding.Vertical + 6);
362                         ret_size.Width += (button.Padding.Horizontal + 6);
363                         
364                         return ret_size;
365                 }
366
367                 public override void CalculateButtonTextAndImageLayout (ButtonBase button, out Rectangle textRectangle, out Rectangle imageRectangle)
368                 {
369                         Image image = button.Image;
370                         string text = button.Text;
371                         Rectangle content_rect = button.PaddingClientRectangle;
372                         Size text_size = TextRenderer.MeasureTextInternal (text, button.Font, content_rect.Size, button.TextFormatFlags, button.UseCompatibleTextRendering);
373                         Size image_size = image == null ? Size.Empty : image.Size;
374
375                         textRectangle = Rectangle.Empty;
376                         imageRectangle = Rectangle.Empty;
377                         
378                         switch (button.TextImageRelation) {
379                                 case TextImageRelation.Overlay:
380                                         // Overlay is easy, text always goes here
381                                         textRectangle = Rectangle.Inflate (content_rect, -4, -4);
382
383                                         if (button.Pressed)
384                                                 textRectangle.Offset (1, 1);
385                                                 
386                                         // Image is dependent on ImageAlign
387                                         if (image == null)
388                                                 return;
389                                                 
390                                         int image_x = 0;
391                                         int image_y = 0;
392                                         int image_height = image.Height;
393                                         int image_width = image.Width;
394                                         
395                                         switch (button.ImageAlign) {
396                                                 case System.Drawing.ContentAlignment.TopLeft:
397                                                         image_x = 5;
398                                                         image_y = 5;
399                                                         break;
400                                                 case System.Drawing.ContentAlignment.TopCenter:
401                                                         image_x = (content_rect.Width - image_width) / 2;
402                                                         image_y = 5;
403                                                         break;
404                                                 case System.Drawing.ContentAlignment.TopRight:
405                                                         image_x = content_rect.Width - image_width - 5;
406                                                         image_y = 5;
407                                                         break;
408                                                 case System.Drawing.ContentAlignment.MiddleLeft:
409                                                         image_x = 5;
410                                                         image_y = (content_rect.Height - image_height) / 2;
411                                                         break;
412                                                 case System.Drawing.ContentAlignment.MiddleCenter:
413                                                         image_x = (content_rect.Width - image_width) / 2;
414                                                         image_y = (content_rect.Height - image_height) / 2;
415                                                         break;
416                                                 case System.Drawing.ContentAlignment.MiddleRight:
417                                                         image_x = content_rect.Width - image_width - 4;
418                                                         image_y = (content_rect.Height - image_height) / 2;
419                                                         break;
420                                                 case System.Drawing.ContentAlignment.BottomLeft:
421                                                         image_x = 5;
422                                                         image_y = content_rect.Height - image_height - 4;
423                                                         break;
424                                                 case System.Drawing.ContentAlignment.BottomCenter:
425                                                         image_x = (content_rect.Width - image_width) / 2;
426                                                         image_y = content_rect.Height - image_height - 4;
427                                                         break;
428                                                 case System.Drawing.ContentAlignment.BottomRight:
429                                                         image_x = content_rect.Width - image_width - 4;
430                                                         image_y = content_rect.Height - image_height - 4;
431                                                         break;
432                                                 default:
433                                                         image_x = 5;
434                                                         image_y = 5;
435                                                         break;
436                                         }
437                                         
438                                         imageRectangle = new Rectangle (image_x, image_y, image_width, image_height);
439                                         break;
440                                 case TextImageRelation.ImageAboveText:
441                                         content_rect.Inflate (-4, -4);
442                                         LayoutTextAboveOrBelowImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
443                                         break;
444                                 case TextImageRelation.TextAboveImage:
445                                         content_rect.Inflate (-4, -4);
446                                         LayoutTextAboveOrBelowImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
447                                         break;
448                                 case TextImageRelation.ImageBeforeText:
449                                         content_rect.Inflate (-4, -4);
450                                         LayoutTextBeforeOrAfterImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
451                                         break;
452                                 case TextImageRelation.TextBeforeImage:
453                                         content_rect.Inflate (-4, -4);
454                                         LayoutTextBeforeOrAfterImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
455                                         break;
456                         }
457                 }
458
459                 private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, System.Drawing.ContentAlignment textAlign, System.Drawing.ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
460                 {
461                         int element_spacing = 0;        // Spacing between the Text and the Image
462                         int total_width = textSize.Width + element_spacing + imageSize.Width;
463                         
464                         if (!textFirst)
465                                 element_spacing += 2;
466                                 
467                         // If the text is too big, chop it down to the size we have available to it
468                         if (total_width > totalArea.Width) {
469                                 textSize.Width = totalArea.Width - element_spacing - imageSize.Width;
470                                 total_width = totalArea.Width;
471                         }
472                         
473                         int excess_width = totalArea.Width - total_width;
474                         int offset = 0;
475
476                         Rectangle final_text_rect;
477                         Rectangle final_image_rect;
478
479                         HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
480                         HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
481
482                         if (h_image == HorizontalAlignment.Left)
483                                 offset = 0;
484                         else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
485                                 offset = excess_width;
486                         else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
487                                 offset += (int)(excess_width / 3);
488                         else
489                                 offset += (int)(2 * (excess_width / 3));
490
491                         if (textFirst) {
492                                 final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
493                                 final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
494                         }
495                         else {
496                                 final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
497                                 final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
498                         }
499
500                         textRect = final_text_rect;
501                         imageRect = final_image_rect;
502                 }
503
504                 private void LayoutTextAboveOrBelowImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, System.Drawing.ContentAlignment textAlign, System.Drawing.ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
505                 {
506                         int element_spacing = 0;        // Spacing between the Text and the Image
507                         int total_height = textSize.Height + element_spacing + imageSize.Height;
508
509                         if (textFirst)
510                                 element_spacing += 2;
511
512                         if (textSize.Width > totalArea.Width)
513                                 textSize.Width = totalArea.Width;
514                                 
515                         // If the there isn't enough room and we're text first, cut out the image
516                         if (total_height > totalArea.Height && textFirst) {
517                                 imageSize = Size.Empty;
518                                 total_height = totalArea.Height;
519                         }
520
521                         int excess_height = totalArea.Height - total_height;
522                         int offset = 0;
523
524                         Rectangle final_text_rect;
525                         Rectangle final_image_rect;
526
527                         VerticalAlignment v_text = GetVerticalAlignment (textAlign);
528                         VerticalAlignment v_image = GetVerticalAlignment (imageAlign);
529
530                         if (v_image == VerticalAlignment.Top)
531                                 offset = 0;
532                         else if (v_image == VerticalAlignment.Bottom && v_text == VerticalAlignment.Bottom)
533                                 offset = excess_height;
534                         else if (v_image == VerticalAlignment.Center && (v_text == VerticalAlignment.Top || v_text == VerticalAlignment.Center))
535                                 offset += (int)(excess_height / 3);
536                         else
537                                 offset += (int)(2 * (excess_height / 3));
538
539                         if (textFirst) {
540                                 final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, totalArea.Top + offset, textSize.Width, textSize.Height);
541                                 final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, final_text_rect.Bottom + element_spacing, imageSize.Width, imageSize.Height);
542                         }
543                         else {
544                                 final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, totalArea.Top + offset, imageSize.Width, imageSize.Height);
545                                 final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, final_image_rect.Bottom + element_spacing, textSize.Width, textSize.Height);
546                                 
547                                 if (final_text_rect.Bottom > totalArea.Bottom)
548                                         final_text_rect.Y = totalArea.Top;
549                         }
550
551                         textRect = final_text_rect;
552                         imageRect = final_image_rect;
553                 }
554                 
555                 private HorizontalAlignment GetHorizontalAlignment (System.Drawing.ContentAlignment align)
556                 {
557                         switch (align) {
558                                 case System.Drawing.ContentAlignment.BottomLeft:
559                                 case System.Drawing.ContentAlignment.MiddleLeft:
560                                 case System.Drawing.ContentAlignment.TopLeft:
561                                         return HorizontalAlignment.Left;
562                                 case System.Drawing.ContentAlignment.BottomCenter:
563                                 case System.Drawing.ContentAlignment.MiddleCenter:
564                                 case System.Drawing.ContentAlignment.TopCenter:
565                                         return HorizontalAlignment.Center;
566                                 case System.Drawing.ContentAlignment.BottomRight:
567                                 case System.Drawing.ContentAlignment.MiddleRight:
568                                 case System.Drawing.ContentAlignment.TopRight:
569                                         return HorizontalAlignment.Right;
570                         }
571
572                         return HorizontalAlignment.Left;
573                 }
574
575                 private enum VerticalAlignment
576                 {
577                         Top = 0,
578                         Center = 1,
579                         Bottom = 2
580                 }
581                 
582                 private VerticalAlignment GetVerticalAlignment (System.Drawing.ContentAlignment align)
583                 {
584                         switch (align) {
585                                 case System.Drawing.ContentAlignment.TopLeft:
586                                 case System.Drawing.ContentAlignment.TopCenter:
587                                 case System.Drawing.ContentAlignment.TopRight:
588                                         return VerticalAlignment.Top;
589                                 case System.Drawing.ContentAlignment.MiddleLeft:
590                                 case System.Drawing.ContentAlignment.MiddleCenter:
591                                 case System.Drawing.ContentAlignment.MiddleRight:
592                                         return VerticalAlignment.Center;
593                                 case System.Drawing.ContentAlignment.BottomLeft:
594                                 case System.Drawing.ContentAlignment.BottomCenter:
595                                 case System.Drawing.ContentAlignment.BottomRight:
596                                         return VerticalAlignment.Bottom;
597                         }
598
599                         return VerticalAlignment.Top;
600                 }
601
602                 internal Rectangle AlignInRectangle (Rectangle outer, Size inner, System.Drawing.ContentAlignment align)
603                 {
604                         int x = 0;
605                         int y = 0;
606
607                         if (align == System.Drawing.ContentAlignment.BottomLeft || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.TopLeft)
608                                 x = outer.X;
609                         else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.TopCenter)
610                                 x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
611                         else if (align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.MiddleRight || align == System.Drawing.ContentAlignment.TopRight)
612                                 x = outer.Right - inner.Width;
613                         if (align == System.Drawing.ContentAlignment.TopCenter || align == System.Drawing.ContentAlignment.TopLeft || align == System.Drawing.ContentAlignment.TopRight)
614                                 y = outer.Y;
615                         else if (align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.MiddleRight)
616                                 y = outer.Y + (outer.Height - inner.Height) / 2;
617                         else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.BottomLeft)
618                                 y = outer.Bottom - inner.Height;
619
620                         return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
621                 }
622                 #endregion
623                 #endregion
624
625                 #region ButtonBase
626                 public override void DrawButtonBase(Graphics dc, Rectangle clip_area, ButtonBase button)
627                 {
628                         // Draw the button: Draw border, etc.
629                         ButtonBase_DrawButton(button, dc);
630
631                         // Draw the image
632                         if (button.FlatStyle != FlatStyle.System && ((button.image != null) || (button.image_list != null)))
633                                 ButtonBase_DrawImage(button, dc);
634                         
635                         // Draw the focus rectangle
636                         if (ShouldPaintFocusRectagle (button))
637                                 ButtonBase_DrawFocus(button, dc);
638                         
639                         // Now the text
640                         if (button.Text != null && button.Text != String.Empty)
641                                 ButtonBase_DrawText(button, dc);
642                 }
643
644                 protected static bool ShouldPaintFocusRectagle (ButtonBase button)
645                 {
646                         return (button.Focused || button.paint_as_acceptbutton) && button.Enabled && button.ShowFocusCues;
647                 }
648
649                 protected virtual void ButtonBase_DrawButton (ButtonBase button, Graphics dc)
650                 {
651                         Rectangle borderRectangle;
652                         bool check_or_radio = false;
653                         bool check_or_radio_checked = false;
654                         
655                         bool is_ColorControl = button.BackColor.ToArgb () == ColorControl.ToArgb () ? true : false;
656                         
657                         CPColor cpcolor = is_ColorControl ? CPColor.Empty : ResPool.GetCPColor (button.BackColor);
658                         
659                         if (button is CheckBox) {
660                                 check_or_radio = true;
661                                 check_or_radio_checked = ((CheckBox)button).Checked;
662                         } else if (button is RadioButton) {
663                                 check_or_radio = true;
664                                 check_or_radio_checked = ((RadioButton)button).Checked;
665                         }
666                         
667                         if (button.Focused && button.Enabled && !check_or_radio) {
668                                 // shrink the rectangle for the normal button drawing inside the focus rectangle
669                                 borderRectangle = Rectangle.Inflate (button.ClientRectangle, -1, -1);
670                         } else {
671                                 borderRectangle = button.ClientRectangle;
672                         }
673                         
674                         if (button.FlatStyle == FlatStyle.Popup) {
675                                 if (!button.is_pressed && !button.is_entered && !check_or_radio_checked)
676                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor, is_ColorControl, button.BackColor);
677                                 else if (!button.is_pressed && button.is_entered &&!check_or_radio_checked)
678                                         Internal_DrawButton (dc, borderRectangle, 2, cpcolor, is_ColorControl, button.BackColor);
679                                 else if (button.is_pressed || check_or_radio_checked)
680                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor, is_ColorControl, button.BackColor);
681                         } else if (button.FlatStyle == FlatStyle.Flat) {
682                                 if (button.is_entered && !button.is_pressed && !check_or_radio_checked) {
683                                         if ((button.image == null) && (button.image_list == null)) {
684                                                 Brush brush = is_ColorControl ? SystemBrushes.ControlDark : ResPool.GetSolidBrush (cpcolor.Dark);
685                                                 dc.FillRectangle (brush, borderRectangle);
686                                         }
687                                 } else if (button.is_pressed || check_or_radio_checked) {
688                                         if ((button.image == null) && (button.image_list == null)) {
689                                                 Brush brush = is_ColorControl ? SystemBrushes.ControlLightLight : ResPool.GetSolidBrush (cpcolor.LightLight);
690                                                 dc.FillRectangle (brush, borderRectangle);
691                                         }
692                                         
693                                         Pen pen = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
694                                         dc.DrawRectangle (pen, borderRectangle.X + 4, borderRectangle.Y + 4,
695                                                           borderRectangle.Width - 9, borderRectangle.Height - 9);
696                                 }
697                                 
698                                 Internal_DrawButton (dc, borderRectangle, 3, cpcolor, is_ColorControl, button.BackColor);
699                         } else {
700                                 if ((!button.is_pressed || !button.Enabled) && !check_or_radio_checked)
701                                         Internal_DrawButton (dc, borderRectangle, 0, cpcolor, is_ColorControl, button.BackColor);
702                                 else
703                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor, is_ColorControl, button.BackColor);
704                         }
705                 }
706                 
707                 private void Internal_DrawButton (Graphics dc, Rectangle rect, int state, CPColor cpcolor, bool is_ColorControl, Color backcolor)
708                 {
709                         switch (state) {
710                         case 0: // normal or normal disabled button
711                                 Pen pen = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
712                                 dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
713                                 dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
714                                 
715                                 pen = is_ColorControl ? SystemPens.Control : ResPool.GetPen (backcolor);
716                                 dc.DrawLine (pen, rect.X + 1, rect.Y + 1, rect.X + 1, rect.Bottom - 3);
717                                 dc.DrawLine (pen, rect.X + 2, rect.Y + 1, rect.Right - 3, rect.Y + 1);
718                                 
719                                 pen = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
720                                 dc.DrawLine (pen, rect.X + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
721                                 dc.DrawLine (pen, rect.Right - 2, rect.Y + 1, rect.Right - 2, rect.Bottom - 3);
722                                 
723                                 pen = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
724                                 dc.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
725                                 dc.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 2);
726                                 break;
727                         case 1: // popup button normal (or pressed normal or popup button)
728                                 pen = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
729                                 dc.DrawRectangle (pen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
730                                 break;
731                         case 2: // popup button poped up
732                                 pen = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
733                                 dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
734                                 dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
735                                 
736                                 pen = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
737                                 dc.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
738                                 dc.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 2);
739                                 break;
740                         case 3: // flat button not entered
741                                 pen = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
742                                 dc.DrawRectangle (pen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
743                                 break;
744                         default:
745                                 break;
746                         }
747                 }
748                 
749                 protected virtual void ButtonBase_DrawImage(ButtonBase button, Graphics dc)
750                 {
751                         // Need to draw a picture
752                         Image   i;
753                         int     image_x;
754                         int     image_y;
755                         int     image_width;
756                         int     image_height;
757                         
758                         int width = button.ClientSize.Width;
759                         int height = button.ClientSize.Height;
760
761                         if (button.ImageIndex != -1) {   // We use ImageIndex instead of image_index since it will return -1 if image_list is null
762                                 i = button.image_list.Images[button.ImageIndex];
763                         } else {
764                                 i = button.image;
765                         }
766
767                         image_width = i.Width;
768                         image_height = i.Height;
769                         
770                         switch (button.ImageAlign) {
771                                 case ContentAlignment.TopLeft: {
772                                         image_x = 5;
773                                         image_y = 5;
774                                         break;
775                                 }
776                                         
777                                 case ContentAlignment.TopCenter: {
778                                         image_x = (width - image_width) / 2;
779                                         image_y = 5;
780                                         break;
781                                 }
782                                         
783                                 case ContentAlignment.TopRight: {
784                                         image_x = width - image_width - 5;
785                                         image_y = 5;
786                                         break;
787                                 }
788                                         
789                                 case ContentAlignment.MiddleLeft: {
790                                         image_x = 5;
791                                         image_y = (height - image_height) / 2;
792                                         break;
793                                 }
794                                         
795                                 case ContentAlignment.MiddleCenter: {
796                                         image_x = (width - image_width) / 2;
797                                         image_y = (height - image_height) / 2;
798                                         break;
799                                 }
800                                         
801                                 case ContentAlignment.MiddleRight: {
802                                         image_x = width - image_width - 4;
803                                         image_y = (height - image_height) / 2;
804                                         break;
805                                 }
806                                         
807                                 case ContentAlignment.BottomLeft: {
808                                         image_x = 5;
809                                         image_y = height - image_height - 4;
810                                         break;
811                                 }
812                                         
813                                 case ContentAlignment.BottomCenter: {
814                                         image_x = (width - image_width) / 2;
815                                         image_y = height - image_height - 4;
816                                         break;
817                                 }
818                                         
819                                 case ContentAlignment.BottomRight: {
820                                         image_x = width - image_width - 4;
821                                         image_y = height - image_height - 4;
822                                         break;
823                                 }
824                                         
825                                 default: {
826                                         image_x = 5;
827                                         image_y = 5;
828                                         break;
829                                 }
830                         }
831                         
832                         dc.SetClip (new Rectangle(3, 3, width - 5, height - 5));
833
834                         if (button.Enabled)
835                                 dc.DrawImage (i, image_x, image_y, image_width, image_height);
836                         else
837                                 CPDrawImageDisabled (dc, i, image_x, image_y, ColorControl);
838
839                         dc.ResetClip ();
840                 }
841                 
842                 protected virtual void ButtonBase_DrawFocus(ButtonBase button, Graphics dc)
843                 {
844                         Color focus_color = button.ForeColor;
845                         
846                         int inflate_value = -3;
847                         
848                         if (!(button is CheckBox) && !(button is RadioButton)) {
849                                 inflate_value = -4;
850                                 
851                                 if (button.FlatStyle == FlatStyle.Popup && !button.is_pressed)
852                                         focus_color = ControlPaint.Dark(button.BackColor);
853                                 
854                                 dc.DrawRectangle (ResPool.GetPen (focus_color), button.ClientRectangle.X, button.ClientRectangle.Y, 
855                                                   button.ClientRectangle.Width - 1, button.ClientRectangle.Height - 1);
856                         }
857                         
858                         if (button.Focused) {
859                                 Rectangle rect = Rectangle.Inflate (button.ClientRectangle, inflate_value, inflate_value);
860                                 ControlPaint.DrawFocusRectangle (dc, rect);
861                         }
862                 }
863                 
864                 protected virtual void ButtonBase_DrawText(ButtonBase button, Graphics dc)
865                 {
866                         Rectangle buttonRectangle = button.ClientRectangle;
867                         Rectangle text_rect = Rectangle.Inflate(buttonRectangle, -4, -4);
868                         
869                         if (button.is_pressed) {
870                                 text_rect.X++;
871                                 text_rect.Y++;
872                         }
873                         
874                         // Ensure that at least one line is going to get displayed.
875                         // Line limit does not ensure that despite its description.
876                         text_rect.Height = Math.Max (button.Font.Height, text_rect.Height);
877                         
878                         if (button.Enabled) {                                   
879                                 dc.DrawString(button.Text, button.Font, ResPool.GetSolidBrush (button.ForeColor), text_rect, button.text_format);
880                         } else {
881                                 if (button.FlatStyle == FlatStyle.Flat || button.FlatStyle == FlatStyle.Popup) {
882                                         dc.DrawString(button.Text, button.Font, ResPool.GetSolidBrush (ColorGrayText), text_rect, button.text_format);
883                                 } else {
884                                         CPDrawStringDisabled (dc, button.Text, button.Font, button.BackColor, text_rect, button.text_format);
885                                 }
886                         }
887                 }
888                 
889                 public override Size ButtonBaseDefaultSize {
890                         get {
891                                 return new Size (75, 23);
892                         }
893                 }
894                 #endregion      // ButtonBase
895
896                 #region CheckBox
897                 public override void DrawCheckBox (Graphics g, CheckBox cb, Rectangle glyphArea, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
898                 {
899                         // Draw Button Background
900                         if (cb.Appearance == Appearance.Button && cb.FlatStyle != FlatStyle.Flat)
901                                 ButtonBase_DrawButton (cb, g);
902                         else if (cb.Appearance != Appearance.Button)
903                                 DrawCheckBoxGlyph (g, cb, glyphArea);
904
905                         // Draw the borders and such for a Flat CheckBox Button
906                         if (cb.Appearance == Appearance.Button && cb.FlatStyle == FlatStyle.Flat)
907                         DrawFlatButton (g, cb, textBounds, imageBounds, clipRectangle);
908                         
909                         // If we have an image, draw it
910                         if (imageBounds.Size != Size.Empty)
911                                 DrawCheckBoxImage (g, cb, imageBounds);
912
913                         if (cb.Focused && cb.Enabled && cb.ShowFocusCues && textBounds != Rectangle.Empty)
914                                 DrawCheckBoxFocus (g, cb, textBounds);
915
916                         // If we have text, draw it
917                         if (textBounds != Rectangle.Empty)
918                                 DrawCheckBoxText (g, cb, textBounds);
919                 }
920
921                 public virtual void DrawCheckBoxGlyph (Graphics g, CheckBox cb, Rectangle glyphArea)
922                 {
923                         if (cb.Pressed)
924                                 ThemeElements.CurrentTheme.CheckBoxPainter.PaintCheckBox (g, glyphArea, cb.BackColor, cb.ForeColor, ElementState.Pressed, cb.FlatStyle, cb.CheckState);
925                         else if (cb.InternalSelected)
926                                 ThemeElements.CurrentTheme.CheckBoxPainter.PaintCheckBox (g, glyphArea, cb.BackColor, cb.ForeColor, ElementState.Normal, cb.FlatStyle, cb.CheckState);
927                         else if (cb.Entered)
928                                 ThemeElements.CurrentTheme.CheckBoxPainter.PaintCheckBox (g, glyphArea, cb.BackColor, cb.ForeColor, ElementState.Hot, cb.FlatStyle, cb.CheckState);
929                         else if (!cb.Enabled)
930                                 ThemeElements.CurrentTheme.CheckBoxPainter.PaintCheckBox (g, glyphArea, cb.BackColor, cb.ForeColor, ElementState.Disabled, cb.FlatStyle, cb.CheckState);
931                         else
932                                 ThemeElements.CurrentTheme.CheckBoxPainter.PaintCheckBox (g, glyphArea, cb.BackColor, cb.ForeColor, ElementState.Normal, cb.FlatStyle, cb.CheckState);
933                 }
934
935                 public virtual void DrawCheckBoxFocus (Graphics g, CheckBox cb, Rectangle focusArea)
936                 {
937                         ControlPaint.DrawFocusRectangle (g, focusArea);
938                 }
939
940                 public virtual void DrawCheckBoxImage (Graphics g, CheckBox cb, Rectangle imageBounds)
941                 {
942                         if (cb.Enabled)
943                                 g.DrawImage (cb.Image, imageBounds);
944                         else
945                                 CPDrawImageDisabled (g, cb.Image, imageBounds.Left, imageBounds.Top, ColorControl);
946                 }
947
948                 public virtual void DrawCheckBoxText (Graphics g, CheckBox cb, Rectangle textBounds)
949                 {
950                         if (cb.Enabled)
951                                 TextRenderer.DrawTextInternal (g, cb.Text, cb.Font, textBounds, cb.ForeColor, cb.TextFormatFlags, cb.UseCompatibleTextRendering);
952                         else
953                                 DrawStringDisabled20 (g, cb.Text, cb.Font, textBounds, cb.BackColor, cb.TextFormatFlags, cb.UseCompatibleTextRendering);
954                 }
955
956                 public override void CalculateCheckBoxTextAndImageLayout (ButtonBase button, Point p, out Rectangle glyphArea, out Rectangle textRectangle, out Rectangle imageRectangle)
957                 {
958                         int check_size = CheckSize;
959
960                         if (button is CheckBox)
961                                 check_size = (button as CheckBox).Appearance == Appearance.Normal ? check_size : 0;
962                                 
963                         glyphArea = new Rectangle (button.Padding.Left, button.Padding.Top, check_size, check_size);
964                         
965                         Rectangle content_rect = button.PaddingClientRectangle;
966                         ContentAlignment align = ContentAlignment.TopLeft;
967                         
968                         if (button is CheckBox)
969                                 align = (button as CheckBox).CheckAlign;
970                         else if (button is RadioButton)
971                                 align = (button as RadioButton).CheckAlign;
972
973                         switch (align) {
974                                 case ContentAlignment.BottomCenter:
975                                         glyphArea.Y += content_rect.Height - check_size - 2;
976                                         glyphArea.X += (content_rect.Width - check_size) / 2;
977                                         break;
978                                 case ContentAlignment.BottomLeft:
979                                         glyphArea.Y += content_rect.Height - check_size - 2;
980                                         content_rect.Width -= check_size;
981                                         content_rect.Offset (check_size, 0);
982                                         break;
983                                 case ContentAlignment.BottomRight:
984                                         glyphArea.Y += content_rect.Height - check_size - 2;
985                                         glyphArea.X += content_rect.Width - check_size;
986                                         content_rect.Width -= check_size;
987                                         break;
988                                 case ContentAlignment.MiddleCenter:
989                                         glyphArea.Y += (content_rect.Height - check_size) / 2;
990                                         glyphArea.X += (content_rect.Width - check_size) / 2;
991                                         break;
992                                 case ContentAlignment.MiddleLeft:
993                                         glyphArea.Y += (content_rect.Height - check_size) / 2;
994                                         content_rect.Width -= check_size;
995                                         content_rect.Offset (check_size, 0);
996                                         break;
997                                 case ContentAlignment.MiddleRight:
998                                         glyphArea.Y += (content_rect.Height - check_size) / 2;
999                                         glyphArea.X += content_rect.Width - check_size;
1000                                         content_rect.Width -= check_size;
1001                                         break;
1002                                 case ContentAlignment.TopCenter:
1003                                         glyphArea.X += (content_rect.Width - check_size) / 2;
1004                                         break;
1005                                 case ContentAlignment.TopLeft:
1006                                         content_rect.Width -= check_size;
1007                                         content_rect.Offset (check_size, 0);
1008                                         break;
1009                                 case ContentAlignment.TopRight:
1010                                         glyphArea.X += content_rect.Width - check_size;
1011                                         content_rect.Width -= check_size;
1012                                         break;
1013                         }
1014                         
1015                         Image image = button.Image;
1016                         string text = button.Text;
1017                         
1018                         Size proposed = Size.Empty;
1019                         
1020                         // Force wrapping if we aren't AutoSize and our text is too long
1021                         if (!button.AutoSize)
1022                                 proposed.Width = button.PaddingClientRectangle.Width - glyphArea.Width - 2;
1023
1024                         Size text_size = TextRenderer.MeasureTextInternal (text, button.Font, proposed, button.TextFormatFlags, button.UseCompatibleTextRendering);
1025                         
1026                         // Text can't be bigger than the content rectangle
1027                         text_size.Height = Math.Min (text_size.Height, content_rect.Height);
1028                         text_size.Width = Math.Min (text_size.Width, content_rect.Width);
1029                         
1030                         Size image_size = image == null ? Size.Empty : image.Size;
1031
1032                         textRectangle = Rectangle.Empty;
1033                         imageRectangle = Rectangle.Empty;
1034
1035                         switch (button.TextImageRelation) {
1036                                 case TextImageRelation.Overlay:
1037                                         // Text is centered vertically, and 2 pixels to the right
1038                                         textRectangle.X = content_rect.Left + 2;
1039                                         textRectangle.Y = button.PaddingClientRectangle.Top + ((content_rect.Height - text_size.Height) / 2) - 1;
1040                                         textRectangle.Size = text_size;
1041
1042                                         // Image is dependent on ImageAlign
1043                                         if (image == null)
1044                                                 return;
1045
1046                                         int image_x = button.PaddingClientRectangle.Left;
1047                                         int image_y = button.PaddingClientRectangle.Top;
1048                                         int image_height = image.Height;
1049                                         int image_width = image.Width;
1050
1051                                         switch (button.ImageAlign) {
1052                                                 case System.Drawing.ContentAlignment.TopLeft:
1053                                                         image_x += 5;
1054                                                         image_y += 5;
1055                                                         break;
1056                                                 case System.Drawing.ContentAlignment.TopCenter:
1057                                                         image_x += (content_rect.Width - image_width) / 2;
1058                                                         image_y += 5;
1059                                                         break;
1060                                                 case System.Drawing.ContentAlignment.TopRight:
1061                                                         image_x += content_rect.Width - image_width - 5;
1062                                                         image_y += 5;
1063                                                         break;
1064                                                 case System.Drawing.ContentAlignment.MiddleLeft:
1065                                                         image_x += 5;
1066                                                         image_y += (content_rect.Height - image_height) / 2;
1067                                                         break;
1068                                                 case System.Drawing.ContentAlignment.MiddleCenter:
1069                                                         image_x += (content_rect.Width - image_width) / 2;
1070                                                         image_y += (content_rect.Height - image_height) / 2;
1071                                                         break;
1072                                                 case System.Drawing.ContentAlignment.MiddleRight:
1073                                                         image_x += content_rect.Width - image_width - 4;
1074                                                         image_y += (content_rect.Height - image_height) / 2;
1075                                                         break;
1076                                                 case System.Drawing.ContentAlignment.BottomLeft:
1077                                                         image_x += 5;
1078                                                         image_y += content_rect.Height - image_height - 4;
1079                                                         break;
1080                                                 case System.Drawing.ContentAlignment.BottomCenter:
1081                                                         image_x += (content_rect.Width - image_width) / 2;
1082                                                         image_y += content_rect.Height - image_height - 4;
1083                                                         break;
1084                                                 case System.Drawing.ContentAlignment.BottomRight:
1085                                                         image_x += content_rect.Width - image_width - 4;
1086                                                         image_y += content_rect.Height - image_height - 4;
1087                                                         break;
1088                                                 default:
1089                                                         image_x += 5;
1090                                                         image_y += 5;
1091                                                         break;
1092                                         }
1093
1094                                         imageRectangle = new Rectangle (image_x + check_size, image_y, image_width, image_height);
1095                                         break;
1096                                 case TextImageRelation.ImageAboveText:
1097                                         content_rect.Inflate (-4, -4);
1098                                         LayoutTextAboveOrBelowImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
1099                                         break;
1100                                 case TextImageRelation.TextAboveImage:
1101                                         content_rect.Inflate (-4, -4);
1102                                         LayoutTextAboveOrBelowImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
1103                                         break;
1104                                 case TextImageRelation.ImageBeforeText:
1105                                         content_rect.Inflate (-4, -4);
1106                                         LayoutTextBeforeOrAfterImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
1107                                         break;
1108                                 case TextImageRelation.TextBeforeImage:
1109                                         content_rect.Inflate (-4, -4);
1110                                         LayoutTextBeforeOrAfterImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
1111                                         break;
1112                         }
1113                 }
1114
1115                 public override Size CalculateCheckBoxAutoSize (CheckBox checkBox)
1116                 {
1117                         Size ret_size = Size.Empty;
1118                         Size text_size = TextRenderer.MeasureTextInternal (checkBox.Text, checkBox.Font, checkBox.UseCompatibleTextRendering);
1119                         Size image_size = checkBox.Image == null ? Size.Empty : checkBox.Image.Size;
1120
1121                         // Pad the text size
1122                         if (checkBox.Text.Length != 0) {
1123                                 text_size.Height += 4;
1124                                 text_size.Width += 4;
1125                         }
1126
1127                         switch (checkBox.TextImageRelation) {
1128                                 case TextImageRelation.Overlay:
1129                                         ret_size.Height = Math.Max (checkBox.Text.Length == 0 ? 0 : text_size.Height, image_size.Height);
1130                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
1131                                         break;
1132                                 case TextImageRelation.ImageAboveText:
1133                                 case TextImageRelation.TextAboveImage:
1134                                         ret_size.Height = text_size.Height + image_size.Height;
1135                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
1136                                         break;
1137                                 case TextImageRelation.ImageBeforeText:
1138                                 case TextImageRelation.TextBeforeImage:
1139                                         ret_size.Height = Math.Max (text_size.Height, image_size.Height);
1140                                         ret_size.Width = text_size.Width + image_size.Width;
1141                                         break;
1142                         }
1143
1144                         // Pad the result
1145                         ret_size.Height += (checkBox.Padding.Vertical);
1146                         ret_size.Width += (checkBox.Padding.Horizontal) + 15;
1147
1148                         // There seems to be a minimum height
1149                         if (ret_size.Height == checkBox.Padding.Vertical)
1150                                 ret_size.Height += 14;
1151                                 
1152                         return ret_size;
1153                 }
1154
1155                 public override void DrawCheckBox(Graphics dc, Rectangle clip_area, CheckBox checkbox) {
1156                         StringFormat            text_format;
1157                         Rectangle               client_rectangle;
1158                         Rectangle               text_rectangle;
1159                         Rectangle               checkbox_rectangle;
1160                         int                     checkmark_size = CheckSize;
1161                         int                     checkmark_space = 4;
1162
1163                         client_rectangle = checkbox.ClientRectangle;
1164                         text_rectangle = client_rectangle;
1165                         checkbox_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, checkmark_size, checkmark_size);
1166
1167                         text_format = new StringFormat();
1168                         text_format.Alignment = StringAlignment.Near;
1169                         text_format.LineAlignment = StringAlignment.Center;
1170                         if (checkbox.ShowKeyboardCuesInternal)
1171                                 text_format.HotkeyPrefix = HotkeyPrefix.Show;
1172                         else
1173                                 text_format.HotkeyPrefix = HotkeyPrefix.Hide;
1174
1175                         /* Calculate the position of text and checkbox rectangle */
1176                         if (checkbox.appearance!=Appearance.Button) {
1177                                 switch(checkbox.check_alignment) {
1178                                         case ContentAlignment.BottomCenter: {
1179                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
1180                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
1181                                                 text_rectangle.X=client_rectangle.X;
1182                                                 text_rectangle.Width=client_rectangle.Width;
1183                                                 text_rectangle.Height=client_rectangle.Height-checkbox_rectangle.Y-checkmark_space;
1184                                                 break;
1185                                         }
1186
1187                                         case ContentAlignment.BottomLeft: {
1188                                                 checkbox_rectangle.X=client_rectangle.Left;
1189                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
1190                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
1191                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
1192                                                 break;
1193                                         }
1194
1195                                         case ContentAlignment.BottomRight: {
1196                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
1197                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
1198                                                 text_rectangle.X=client_rectangle.X;
1199                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
1200                                                 break;
1201                                         }
1202
1203                                         case ContentAlignment.MiddleCenter: {
1204                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
1205                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
1206                                                 text_rectangle.X=client_rectangle.X;
1207                                                 text_rectangle.Width=client_rectangle.Width;
1208                                                 break;
1209                                         }
1210
1211                                         default:
1212                                         case ContentAlignment.MiddleLeft: {
1213                                                 checkbox_rectangle.X=client_rectangle.Left;
1214                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
1215                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
1216                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                                                                             
1217                                                 break;
1218                                         }
1219
1220                                         case ContentAlignment.MiddleRight: {
1221                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
1222                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
1223                                                 text_rectangle.X=client_rectangle.X;
1224                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
1225                                                 break;
1226                                         }
1227
1228                                         case ContentAlignment.TopCenter: {
1229                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
1230                                                 checkbox_rectangle.Y=client_rectangle.Top;
1231                                                 text_rectangle.X=client_rectangle.X;
1232                                                 text_rectangle.Width=client_rectangle.Width;
1233                                                 text_rectangle.Y=checkmark_size+checkmark_space;
1234                                                 text_rectangle.Height=client_rectangle.Height-checkmark_size-checkmark_space;
1235                                                 break;
1236                                         }
1237
1238                                         case ContentAlignment.TopLeft: {
1239                                                 checkbox_rectangle.X=client_rectangle.Left;
1240                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
1241                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
1242                                                 break;
1243                                         }
1244
1245                                         case ContentAlignment.TopRight: {
1246                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
1247                                                 text_rectangle.X=client_rectangle.X;
1248                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
1249                                                 break;
1250                                         }
1251                                 }
1252                         } else {
1253                                 text_rectangle.X=client_rectangle.X;
1254                                 text_rectangle.Width=client_rectangle.Width;
1255                         }
1256                         
1257                         /* Set the horizontal alignment of our text */
1258                         switch(checkbox.text_alignment) {
1259                                 case ContentAlignment.BottomLeft:
1260                                 case ContentAlignment.MiddleLeft:
1261                                 case ContentAlignment.TopLeft: {
1262                                         text_format.Alignment=StringAlignment.Near;
1263                                         break;
1264                                 }
1265
1266                                 case ContentAlignment.BottomCenter:
1267                                 case ContentAlignment.MiddleCenter:
1268                                 case ContentAlignment.TopCenter: {
1269                                         text_format.Alignment=StringAlignment.Center;
1270                                         break;
1271                                 }
1272
1273                                 case ContentAlignment.BottomRight:
1274                                 case ContentAlignment.MiddleRight:
1275                                 case ContentAlignment.TopRight: {
1276                                         text_format.Alignment=StringAlignment.Far;
1277                                         break;
1278                                 }
1279                         }
1280
1281                         /* Set the vertical alignment of our text */
1282                         switch(checkbox.text_alignment) {
1283                                 case ContentAlignment.TopLeft: 
1284                                 case ContentAlignment.TopCenter: 
1285                                 case ContentAlignment.TopRight: {
1286                                         text_format.LineAlignment=StringAlignment.Near;
1287                                         break;
1288                                 }
1289
1290                                 case ContentAlignment.BottomLeft:
1291                                 case ContentAlignment.BottomCenter:
1292                                 case ContentAlignment.BottomRight: {
1293                                         text_format.LineAlignment=StringAlignment.Far;
1294                                         break;
1295                                 }
1296
1297                                 case ContentAlignment.MiddleLeft:
1298                                 case ContentAlignment.MiddleCenter:
1299                                 case ContentAlignment.MiddleRight: {
1300                                         text_format.LineAlignment=StringAlignment.Center;
1301                                         break;
1302                                 }
1303                         }
1304
1305                         ButtonState state = ButtonState.Normal;
1306                         if (checkbox.FlatStyle == FlatStyle.Flat) {
1307                                 state |= ButtonState.Flat;
1308                         }
1309                         
1310                         if (checkbox.Checked) {
1311                                 state |= ButtonState.Checked;
1312                         }
1313                         
1314                         if (checkbox.ThreeState && (checkbox.CheckState == CheckState.Indeterminate)) {
1315                                 state |= ButtonState.Checked;
1316                                 state |= ButtonState.Pushed;                            
1317                         }
1318                         
1319                         // finally make sure the pushed and inavtive states are rendered
1320                         if (!checkbox.Enabled) {
1321                                 state |= ButtonState.Inactive;
1322                         }
1323                         else if (checkbox.is_pressed) {
1324                                 state |= ButtonState.Pushed;
1325                         }
1326                         
1327                         // Start drawing
1328                         
1329                         CheckBox_DrawCheckBox(dc, checkbox, state, checkbox_rectangle);
1330                         
1331                         if ((checkbox.image != null) || (checkbox.image_list != null))
1332                                 ButtonBase_DrawImage(checkbox, dc);
1333                         
1334                         CheckBox_DrawText(checkbox, text_rectangle, dc, text_format);
1335
1336                         if (checkbox.Focused && checkbox.Enabled && checkbox.appearance != Appearance.Button && checkbox.Text != String.Empty && checkbox.ShowFocusCues) {
1337                                 SizeF text_size = dc.MeasureString (checkbox.Text, checkbox.Font);
1338                                 
1339                                 Rectangle focus_rect = Rectangle.Empty;
1340                                 focus_rect.X = text_rectangle.X;
1341                                 focus_rect.Y = (int)((text_rectangle.Height - text_size.Height) / 2);
1342                                 focus_rect.Size = text_size.ToSize ();
1343                                 CheckBox_DrawFocus (checkbox, dc, focus_rect);
1344                         }
1345
1346                         text_format.Dispose ();
1347                 }
1348
1349                 protected virtual void CheckBox_DrawCheckBox( Graphics dc, CheckBox checkbox, ButtonState state, Rectangle checkbox_rectangle )
1350                 {
1351                         Brush brush = checkbox.BackColor.ToArgb () == ColorControl.ToArgb () ? SystemBrushes.Control : ResPool.GetSolidBrush (checkbox.BackColor);
1352                         dc.FillRectangle (brush, checkbox.ClientRectangle);                     
1353                         // render as per normal button
1354                         if (checkbox.appearance==Appearance.Button) {
1355                                 ButtonBase_DrawButton (checkbox, dc);
1356                                 
1357                                 if ((checkbox.Focused) && checkbox.Enabled)
1358                                         ButtonBase_DrawFocus(checkbox, dc);
1359                         } else {
1360                                 // establish if we are rendering a flat style of some sort
1361                                 if (checkbox.FlatStyle == FlatStyle.Flat || checkbox.FlatStyle == FlatStyle.Popup) {
1362                                         DrawFlatStyleCheckBox (dc, checkbox_rectangle, checkbox);
1363                                 } else {
1364                                         CPDrawCheckBox (dc, checkbox_rectangle, state);
1365                                 }
1366                         }
1367                 }
1368                 
1369                 protected virtual void CheckBox_DrawText( CheckBox checkbox, Rectangle text_rectangle, Graphics dc, StringFormat text_format )
1370                 {
1371                         DrawCheckBox_and_RadioButtonText (checkbox, text_rectangle, dc, 
1372                                                           text_format, checkbox.Appearance, checkbox.Checked);
1373                 }
1374                 
1375                 protected virtual void CheckBox_DrawFocus( CheckBox checkbox, Graphics dc, Rectangle text_rectangle )
1376                 {
1377                         DrawInnerFocusRectangle (dc, text_rectangle, checkbox.BackColor);
1378                 }
1379
1380                 // renders a checkBox with the Flat and Popup FlatStyle
1381                 protected virtual void DrawFlatStyleCheckBox (Graphics graphics, Rectangle rectangle, CheckBox checkbox)
1382                 {
1383                         Pen                     pen;                    
1384                         Rectangle       rect;
1385                         Rectangle       checkbox_rectangle;
1386                         Rectangle       fill_rectangle;
1387                         int                     lineWidth;
1388                         int                     Scale;
1389                         
1390                         // set up our rectangles first
1391                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered) {
1392                                 // clip one pixel from bottom right for non popup rendered checkboxes
1393                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-1, 0), Math.Max(rectangle.Height-1,0));
1394                                 fill_rectangle = new Rectangle(checkbox_rectangle.X+1, checkbox_rectangle.Y+1, Math.Max(checkbox_rectangle.Width-3, 0), Math.Max(checkbox_rectangle.Height-3,0));
1395                         } else {
1396                                 // clip two pixels from bottom right for non popup rendered checkboxes
1397                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-2, 0), Math.Max(rectangle.Height-2,0));
1398                                 fill_rectangle = new Rectangle(checkbox_rectangle.X+1, checkbox_rectangle.Y+1, Math.Max(checkbox_rectangle.Width-2, 0), Math.Max(checkbox_rectangle.Height-2,0));
1399                         }       
1400                         
1401                         
1402                         // if disabled render in disabled state
1403                         if (checkbox.Enabled) {
1404                                 // process the state of the checkbox
1405                                 if (checkbox.is_entered || checkbox.Capture) {
1406                                         // decide on which background color to use
1407                                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered && checkbox.Capture) {
1408                                                 graphics.FillRectangle(ResPool.GetSolidBrush (checkbox.BackColor), fill_rectangle);
1409                                         } else if (checkbox.FlatStyle == FlatStyle.Flat) { 
1410                                                 if (!checkbox.is_pressed) {
1411                                                         graphics.FillRectangle(ResPool.GetSolidBrush (checkbox.BackColor), fill_rectangle);
1412                                                 } else
1413                                                         graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);
1414                                         } else {
1415                                                 // use regular window background color
1416                                                 graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);
1417                                         }
1418                                         
1419                                         // render the outer border
1420                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
1421                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
1422                                         } else {
1423                                                 // draw sunken effect
1424                                                 CPDrawBorder3D (graphics, checkbox_rectangle, Border3DStyle.SunkenInner, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, checkbox.BackColor);
1425                                         }
1426                                 } else {
1427                                         graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);                           
1428                                         
1429                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
1430                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
1431                                         } else {
1432                                                 // draw the outer border
1433                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ControlPaint.DarkDark (checkbox.BackColor), ButtonBorderStyle.Solid);
1434                                         }                       
1435                                 }
1436                         } else {
1437                                 if (checkbox.FlatStyle == FlatStyle.Popup) {
1438                                         graphics.FillRectangle(SystemBrushes.Control, fill_rectangle);
1439                                 }       
1440                         
1441                                 // draw disabled state,
1442                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ColorControlDark, ButtonBorderStyle.Solid);
1443                         }               
1444                         
1445                         if (checkbox.Checked) {
1446                                 /* Need to draw a check-mark */
1447                                 
1448                                 /* Make sure we've got at least a line width of 1 */
1449                                 lineWidth = Math.Max(3, fill_rectangle.Width/3);
1450                                 Scale=Math.Max(1, fill_rectangle.Width/9);
1451                                 
1452                                 // flat style check box is rendered inside a rectangle shifted down by one
1453                                 rect=new Rectangle(fill_rectangle.X, fill_rectangle.Y+1, fill_rectangle.Width, fill_rectangle.Height);
1454                                 if (checkbox.Enabled) {
1455                                         pen=ResPool.GetPen(checkbox.ForeColor);
1456                                 } else {
1457                                         pen=SystemPens.ControlDark;
1458                                 }
1459                                 
1460                                 for (int i=0; i<lineWidth; i++) {
1461                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
1462                                         graphics.DrawLine(pen, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i, rect.Left+lineWidth/2+6*Scale, rect.Top+lineWidth-2*Scale+i);
1463                                 }
1464                         }                                       
1465                 }
1466
1467                 private void DrawCheckBox_and_RadioButtonText (ButtonBase button_base, Rectangle text_rectangle, Graphics dc, 
1468                                                                StringFormat text_format, Appearance appearance, bool ischecked)
1469                 {
1470                         // offset the text if it's pressed and a button
1471                         if (appearance == Appearance.Button) {
1472                                 if (ischecked || (button_base.Capture && button_base.FlatStyle != FlatStyle.Flat)) {
1473                                         text_rectangle.X ++;
1474                                         text_rectangle.Y ++;
1475                                 }
1476                                 
1477                                 text_rectangle.Inflate (-4, -4);
1478                         }
1479                         
1480                         /* Place the text; to be compatible with Windows place it after the checkbox has been drawn */
1481                         
1482                         // Windows seems to not wrap text in certain situations, this matches as close as I could get it
1483                         if ((float)(button_base.Font.Height * 1.5f) > text_rectangle.Height) {
1484                                 text_format.FormatFlags |= StringFormatFlags.NoWrap;
1485                         }
1486                         if (button_base.Enabled) {
1487                                 dc.DrawString (button_base.Text, button_base.Font, ResPool.GetSolidBrush (button_base.ForeColor), text_rectangle, text_format);                 
1488                         } else if (button_base.FlatStyle == FlatStyle.Flat || button_base.FlatStyle == FlatStyle.Popup) {
1489                                 dc.DrawString (button_base.Text, button_base.Font, SystemBrushes.ControlDarkDark, text_rectangle, text_format);
1490                         } else {
1491                                 CPDrawStringDisabled (dc, button_base.Text, button_base.Font, button_base.BackColor, text_rectangle, text_format);
1492                         }
1493                 }
1494                 #endregion      // CheckBox
1495                 
1496                 #region CheckedListBox
1497                 
1498                 public override void DrawCheckedListBoxItem (CheckedListBox ctrl, DrawItemEventArgs e)
1499                 {                       
1500                         Color back_color, fore_color;
1501                         Rectangle item_rect = e.Bounds;
1502                         ButtonState state;
1503
1504                         /* Draw checkbox */             
1505
1506                         if ((e.State & DrawItemState.Checked) == DrawItemState.Checked) {
1507                                 state = ButtonState.Checked;
1508                                 if ((e.State & DrawItemState.Inactive) == DrawItemState.Inactive)
1509                                         state |= ButtonState.Inactive;
1510                         } else
1511                                 state = ButtonState.Normal;
1512
1513                         if (ctrl.ThreeDCheckBoxes == false)
1514                                 state |= ButtonState.Flat;
1515
1516                         Rectangle checkbox_rect = new Rectangle (2, (item_rect.Height - 11) / 2, CheckSize, CheckSize);
1517                         ControlPaint.DrawCheckBox (e.Graphics,
1518                                 item_rect.X + checkbox_rect.X, item_rect.Y + checkbox_rect.Y,
1519                                 checkbox_rect.Width, checkbox_rect.Height,
1520                                 state);
1521
1522                         item_rect.X += checkbox_rect.Right;
1523                         item_rect.Width -= checkbox_rect.Right;
1524                         
1525                         /* Draw text*/
1526                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
1527                                 back_color = ColorHighlight;
1528                                 fore_color = ColorHighlightText;
1529                         }
1530                         else {
1531                                 back_color = e.BackColor;
1532                                 fore_color = e.ForeColor;
1533                         }
1534                         
1535                         e.Graphics.FillRectangle (ResPool.GetSolidBrush
1536                                 (back_color), item_rect);
1537
1538                         e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
1539                                 ResPool.GetSolidBrush (fore_color),
1540                                 item_rect, ctrl.StringFormat);
1541                                         
1542                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1543                                 CPDrawFocusRectangle (e.Graphics, item_rect,
1544                                         fore_color, back_color);
1545                         }
1546                 }
1547                 
1548                 #endregion // CheckedListBox
1549                 
1550                 #region ComboBox                
1551                 public override void DrawComboBoxItem (ComboBox ctrl, DrawItemEventArgs e)
1552                 {
1553                         Color back_color, fore_color;
1554                         Rectangle text_draw = e.Bounds;
1555                         StringFormat string_format = new StringFormat ();
1556                         string_format.FormatFlags = StringFormatFlags.LineLimit;
1557                         
1558                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
1559                                 back_color = ColorHighlight;
1560                                 fore_color = ColorHighlightText;
1561                         }
1562                         else {
1563                                 back_color = e.BackColor;
1564                                 fore_color = e.ForeColor;
1565                         }
1566                         
1567                         if (!ctrl.Enabled)
1568                                 fore_color = ColorInactiveCaptionText;
1569                                                         
1570                         e.Graphics.FillRectangle (ResPool.GetSolidBrush (back_color), e.Bounds);
1571
1572                         if (e.Index != -1) {
1573                                 e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
1574                                         ResPool.GetSolidBrush (fore_color),
1575                                         text_draw, string_format);
1576                         }
1577                         
1578                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1579                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, fore_color, back_color);
1580                         }
1581
1582                         string_format.Dispose ();
1583                 }
1584                 
1585                 public override void DrawFlatStyleComboButton (Graphics graphics, Rectangle rectangle, ButtonState state)
1586                 {
1587                         Point[]                 arrow = new Point[3];
1588                         Point                           P1;
1589                         Point                           P2;
1590                         Point                           P3;
1591                         int                             centerX;
1592                         int                             centerY;
1593                         int                             shiftX;
1594                         int                             shiftY;
1595                         Rectangle               rect;
1596
1597                         rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
1598                         centerX=rect.Left+rect.Width/2;
1599                         centerY=rect.Top+rect.Height/2;
1600                         shiftX=Math.Max(1, rect.Width/8);
1601                         shiftY=Math.Max(1, rect.Height/8);
1602
1603                         if ((state & ButtonState.Pushed)!=0) {
1604                                 shiftX++;
1605                                 shiftY++;
1606                         }
1607
1608                         rect.Y-=shiftY;
1609                         centerY-=shiftY;
1610                         P1=new Point(rect.Left + 1, centerY);
1611                         P2=new Point(rect.Right - 1, centerY);
1612                         P3=new Point(centerX, rect.Bottom - 1);
1613
1614                         arrow[0]=P1;
1615                         arrow[1]=P2;
1616                         arrow[2]=P3;
1617                         
1618                         /* Draw the arrow */
1619                         if ((state & ButtonState.Inactive)!=0) {
1620                                 /* Move away from the shadow */
1621                                 arrow[0].X += 1;        arrow[0].Y += 1;
1622                                 arrow[1].X += 1;        arrow[1].Y += 1;
1623                                 arrow[2].X += 1;        arrow[2].Y += 1;
1624                                 
1625                                 graphics.FillPolygon(SystemBrushes.ControlLightLight, arrow, FillMode.Winding);
1626
1627                                 arrow[0]=P1;
1628                                 arrow[1]=P2;
1629                                 arrow[2]=P3;
1630
1631                                 graphics.FillPolygon(SystemBrushes.ControlDark, arrow, FillMode.Winding);
1632                         } else {
1633                                 graphics.FillPolygon(SystemBrushes.ControlText, arrow, FillMode.Winding);
1634                         }               
1635                 }
1636                 public override void ComboBoxDrawNormalDropDownButton (ComboBox comboBox, Graphics g, Rectangle clippingArea, Rectangle area, ButtonState state)
1637                 {
1638                         CPDrawComboButton (g, area, state);
1639                 }
1640                 public override bool ComboBoxNormalDropDownButtonHasTransparentBackground (ComboBox comboBox, ButtonState state)
1641                 {
1642                         return true;
1643                 }
1644                 public override bool ComboBoxDropDownButtonHasHotElementStyle (ComboBox comboBox)
1645                 {
1646                         return false;
1647                 }
1648                 public override void ComboBoxDrawBackground (ComboBox comboBox, Graphics g, Rectangle clippingArea, FlatStyle style)
1649                 {
1650                         if (!comboBox.Enabled)
1651                                 g.FillRectangle (ResPool.GetSolidBrush (ColorControl), comboBox.ClientRectangle);
1652
1653                         if (comboBox.DropDownStyle == ComboBoxStyle.Simple)
1654                                 g.FillRectangle (ResPool.GetSolidBrush (comboBox.Parent.BackColor), comboBox.ClientRectangle);
1655
1656                         if (style == FlatStyle.Popup && (comboBox.Entered || comboBox.Focused)) {
1657                                 Rectangle area = comboBox.TextArea;
1658                                 area.Height -= 1;
1659                                 area.Width -= 1;
1660                                 g.DrawRectangle (ResPool.GetPen (SystemColors.ControlDark), area);
1661                                 g.DrawLine (ResPool.GetPen (SystemColors.ControlDark), comboBox.ButtonArea.X - 1, comboBox.ButtonArea.Top, comboBox.ButtonArea.X - 1, comboBox.ButtonArea.Bottom);
1662                         }
1663                         bool is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1664                         if (!is_flat && clippingArea.IntersectsWith (comboBox.TextArea))
1665                                 ControlPaint.DrawBorder3D (g, comboBox.TextArea, Border3DStyle.Sunken);
1666                 }
1667                 public override bool CombBoxBackgroundHasHotElementStyle (ComboBox comboBox)
1668                 {
1669                         return false;
1670                 }
1671                 #endregion ComboBox
1672                 
1673                 #region Datagrid
1674                 public override int DataGridPreferredColumnWidth { get { return 75;} }
1675                 public override int DataGridMinimumColumnCheckBoxHeight { get { return 16;} }
1676                 public override int DataGridMinimumColumnCheckBoxWidth { get { return 16;} }
1677                 public override Color DataGridAlternatingBackColor { get { return ColorWindow;} }
1678                 public override Color DataGridBackColor { get  { return  ColorWindow;} }                
1679                 public override Color DataGridBackgroundColor { get  { return  ColorAppWorkspace;} }
1680                 public override Color DataGridCaptionBackColor { get  { return ColorActiveCaption;} }
1681                 public override Color DataGridCaptionForeColor { get  { return ColorActiveCaptionText;} }
1682                 public override Color DataGridGridLineColor { get { return ColorControl;} }
1683                 public override Color DataGridHeaderBackColor { get  { return ColorControl;} }
1684                 public override Color DataGridHeaderForeColor { get  { return ColorControlText;} }
1685                 public override Color DataGridLinkColor { get  { return ColorHotTrack;} }
1686                 public override Color DataGridLinkHoverColor { get  { return ColorHotTrack;} }
1687                 public override Color DataGridParentRowsBackColor { get  { return ColorControl;} }
1688                 public override Color DataGridParentRowsForeColor { get  { return ColorWindowText;} }
1689                 public override Color DataGridSelectionBackColor { get  { return ColorActiveCaption;} }
1690                 public override Color DataGridSelectionForeColor { get  { return ColorActiveCaptionText;} }
1691                 
1692                 public override void DataGridPaint (PaintEventArgs pe, DataGrid grid)
1693                 {
1694                         DataGridPaintCaption (pe.Graphics, pe.ClipRectangle, grid);
1695                         DataGridPaintParentRows (pe.Graphics, pe.ClipRectangle, grid);
1696                         DataGridPaintColumnHeaders (pe.Graphics, pe.ClipRectangle, grid);
1697                         DataGridPaintRows (pe.Graphics, grid.cells_area, pe.ClipRectangle, grid);
1698
1699                         // Paint scrollBar corner
1700                         if (grid.VScrollBar.Visible && grid.HScrollBar.Visible) {
1701
1702                                 Rectangle corner = new Rectangle (grid.ClientRectangle.X + grid.ClientRectangle.Width - grid.VScrollBar.Width,
1703                                                                   grid.ClientRectangle.Y + grid.ClientRectangle.Height - grid.HScrollBar.Height,
1704                                                                   grid.VScrollBar.Width, grid.HScrollBar.Height);
1705
1706                                 if (pe.ClipRectangle.IntersectsWith (corner)) {
1707                                         pe.Graphics.FillRectangle (ResPool.GetSolidBrush (grid.ParentRowsBackColor),
1708                                                                    corner);
1709                                 }
1710                         }
1711                 }
1712
1713                 public override void DataGridPaintCaption (Graphics g, Rectangle clip, DataGrid grid)
1714                 {
1715                         Rectangle bounds = clip;
1716                         bounds.Intersect (grid.caption_area);
1717
1718                         // Background
1719                         g.FillRectangle (ResPool.GetSolidBrush (grid.CaptionBackColor), bounds);
1720
1721                         // Bottom line
1722                 g.DrawLine (ResPool.GetPen (grid.CurrentTableStyle.CurrentHeaderForeColor),
1723                                         bounds.X, bounds.Y + bounds.Height -1, 
1724                     bounds.X + bounds.Width, bounds.Y + bounds.Height -1);
1725
1726                         // Caption text
1727                         if (grid.CaptionText != String.Empty) {
1728                                 Rectangle text_rect = grid.caption_area;
1729                                 text_rect.Y += text_rect.Height / 2 - grid.CaptionFont.Height / 2;
1730                                 text_rect.Height = grid.CaptionFont.Height;
1731
1732                                 g.DrawString (grid.CaptionText, grid.CaptionFont,
1733                                               ResPool.GetSolidBrush (grid.CaptionForeColor),
1734                                               text_rect);
1735                         }
1736
1737                         // Back button
1738                         if (bounds.IntersectsWith (grid.back_button_rect)) {
1739                                 g.DrawImage (grid.back_button_image, grid.back_button_rect);
1740                                 if (grid.back_button_mouseover) {
1741                                         CPDrawBorder3D (g, grid.back_button_rect, grid.back_button_active ? Border3DStyle.Sunken : Border3DStyle.Raised, all_sides);
1742                                 }
1743                         }
1744
1745                         // Rows button
1746                         if (bounds.IntersectsWith (grid.parent_rows_button_rect)) {
1747                                 g.DrawImage (grid.parent_rows_button_image, grid.parent_rows_button_rect);
1748                                 if (grid.parent_rows_button_mouseover) {
1749                                         CPDrawBorder3D (g, grid.parent_rows_button_rect, grid.parent_rows_button_active ? Border3DStyle.Sunken : Border3DStyle.Raised, all_sides);
1750                                 }
1751                         }
1752                 }
1753
1754                 public override void DataGridPaintColumnHeaders (Graphics g, Rectangle clip, DataGrid grid)
1755                 {
1756                         if (!grid.CurrentTableStyle.ColumnHeadersVisible)
1757                                 return;
1758
1759                         Rectangle columns_area = grid.column_headers_area;
1760
1761                         // Paint corner shared between row and column header
1762                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
1763                                 Rectangle rect_bloc = grid.column_headers_area;
1764                                 rect_bloc.Width = grid.RowHeaderWidth;
1765                                 if (clip.IntersectsWith (rect_bloc)) {
1766                                         if (grid.FlatMode)
1767                                                 g.FillRectangle (ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderBackColor), rect_bloc);
1768                                         else
1769                                                 CPDrawBorder3D (g, rect_bloc, Border3DStyle.RaisedInner, 
1770                                                         Border3DSide.Left | Border3DSide.Right | 
1771                                                         Border3DSide.Top | Border3DSide.Bottom | Border3DSide.Middle, 
1772                                                         grid.CurrentTableStyle.CurrentHeaderBackColor);
1773                                 }
1774
1775                                 columns_area.X += grid.RowHeaderWidth;
1776                                 columns_area.Width -= grid.RowHeaderWidth;
1777                         }
1778
1779                         // Set column painting
1780                         Rectangle rect_columnhdr = new Rectangle ();
1781                         int col_pixel;
1782                         Region current_clip;
1783                         Region prev_clip = g.Clip;
1784                         rect_columnhdr.Y = columns_area.Y;
1785                         rect_columnhdr.Height = columns_area.Height;
1786
1787                         int column_cnt = grid.FirstVisibleColumn + grid.VisibleColumnCount;
1788                         for (int column = grid.FirstVisibleColumn; column < column_cnt; column++) {
1789                                 if (grid.CurrentTableStyle.GridColumnStyles[column].bound == false)
1790                                         continue;
1791                                 
1792                                 col_pixel = grid.GetColumnStartingPixel (column);
1793                                 rect_columnhdr.X = columns_area.X + col_pixel - grid.HorizPixelOffset;
1794                                 rect_columnhdr.Width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
1795
1796                                 if (clip.IntersectsWith (rect_columnhdr) == false)
1797                                         continue;
1798
1799                                 current_clip = new Region (rect_columnhdr);
1800                                 current_clip.Intersect (columns_area);
1801                                 current_clip.Intersect (prev_clip);
1802                                 g.Clip = current_clip;
1803
1804                                 DataGridPaintColumnHeader (g, rect_columnhdr, grid, column);
1805
1806                                 current_clip.Dispose ();
1807                         }
1808
1809                         g.Clip = prev_clip;
1810                                 
1811                         Rectangle not_usedarea = grid.column_headers_area;
1812                         not_usedarea.X = (column_cnt == 0) ? grid.RowHeaderWidth : rect_columnhdr.X + rect_columnhdr.Width;
1813                         not_usedarea.Width = grid.ClientRectangle.X + grid.ClientRectangle.Width - not_usedarea.X;
1814                         g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
1815                 }
1816
1817                 public override void DataGridPaintColumnHeader (Graphics g, Rectangle bounds, DataGrid grid, int col)
1818                 {
1819                         // Background
1820                         g.FillRectangle (ResPool.GetSolidBrush (grid.CurrentTableStyle.HeaderBackColor), bounds);
1821
1822                         // Paint Borders
1823                         if (!grid.FlatMode) {
1824                                 g.DrawLine (ResPool.GetPen (ColorControlLightLight),
1825                                         bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
1826                                 
1827                                 if (col == 0) {
1828                                         g.DrawLine (ResPool.GetPen (ColorControlLightLight),
1829                                                 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
1830                                 } else {
1831                                         g.DrawLine (ResPool.GetPen (ColorControlLightLight),
1832                                                 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 3);
1833                                 }
1834                                 
1835                                 if (col == (grid.VisibleColumnCount -1)) {
1836                                         g.DrawLine (ResPool.GetPen (ColorControlDark),
1837                                                 bounds.X + bounds.Width - 1, bounds.Y, 
1838                                                 bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
1839                                 } else {
1840                                         g.DrawLine (ResPool.GetPen (ColorControlDark),
1841                                                 bounds.X + bounds.Width - 1, bounds.Y + 2, 
1842                                                 bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 3);
1843                                 }
1844
1845                                 g.DrawLine (ResPool.GetPen (ColorControlDark),
1846                                         bounds.X, bounds.Y + bounds.Height - 1, 
1847                                         bounds.X + bounds.Width, bounds.Y + bounds.Height - 1);
1848                         }
1849
1850                         bounds.X += 2;
1851                         bounds.Width -= 2;
1852
1853                         DataGridColumnStyle style = grid.CurrentTableStyle.GridColumnStyles[col];
1854
1855                         if (style.ArrowDrawingMode != DataGridColumnStyle.ArrowDrawing.No)
1856                                 bounds.Width -= 16;
1857
1858                         // Caption
1859                         StringFormat format = new StringFormat ();
1860                         format.FormatFlags |= StringFormatFlags.NoWrap;
1861                         format.LineAlignment = StringAlignment.Center;
1862                         format.Trimming = StringTrimming.Character;
1863
1864                         g.DrawString (style.HeaderText, grid.CurrentTableStyle.HeaderFont, 
1865                                 ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderForeColor), 
1866                                 bounds, format);
1867
1868                         // Arrow (6 x 6)
1869                         if (style.ArrowDrawingMode != DataGridColumnStyle.ArrowDrawing.No) {
1870                                 Point pnt = new Point (bounds.X + bounds.Width + 4, bounds.Y + ((bounds.Height - 6)/2));
1871                                 
1872                                 if (style.ArrowDrawingMode == DataGridColumnStyle.ArrowDrawing.Ascending) {
1873                                         g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
1874                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
1875                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
1876                                 } else {
1877                                         g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
1878                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
1879                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
1880                                 }
1881                         }
1882                 }
1883
1884                 public override void DataGridPaintParentRows (Graphics g, Rectangle clip, DataGrid grid)
1885                 {
1886                         Rectangle rect_row = new Rectangle ();
1887
1888                         rect_row.X = grid.ParentRowsArea.X;
1889                         rect_row.Width = grid.ParentRowsArea.Width;
1890                         rect_row.Height = (grid.CaptionFont.Height + 3);
1891
1892                         object[] parentRows = grid.data_source_stack.ToArray();
1893                         
1894                         Region current_clip;
1895                         Region prev_clip = g.Clip;
1896                         for (int row = 0; row < parentRows.Length; row++) {
1897                                 rect_row.Y = grid.ParentRowsArea.Y + row * rect_row.Height;
1898
1899                                 if (clip.IntersectsWith (rect_row) == false)
1900                                         continue;
1901
1902                                 current_clip = new Region (rect_row);
1903                                 current_clip.Intersect (prev_clip);
1904                                 g.Clip = current_clip;
1905
1906                                 DataGridPaintParentRow (g, rect_row, (DataGridDataSource)parentRows[parentRows.Length - row - 1], grid);
1907
1908                                 current_clip.Dispose ();
1909                         }
1910                         
1911                         g.Clip = prev_clip;
1912                 }
1913
1914                 public override void DataGridPaintParentRow (Graphics g, Rectangle bounds, DataGridDataSource row, DataGrid grid)
1915                 {
1916                         // Background
1917                         g.FillRectangle (ResPool.GetSolidBrush (grid.ParentRowsBackColor),
1918                                          bounds);
1919
1920                         Font bold_font = new Font (grid.Font.FontFamily, grid.Font.Size, grid.Font.Style | FontStyle.Bold);
1921                         // set up some standard string formating variables
1922                         StringFormat text_format = new StringFormat();
1923                         text_format.LineAlignment = StringAlignment.Center;
1924                         text_format.Alignment = StringAlignment.Near;
1925
1926                         string table_name = "";
1927                         if (row.view is DataRowView)
1928                                 table_name = ((ITypedList)((DataRowView)row.view).DataView).GetListName (null) + ": ";
1929                         // XXX else?
1930
1931                         Rectangle       text_rect;
1932                         Size            text_size;
1933
1934                         text_size = g.MeasureString (table_name, bold_font).ToSize();
1935                         text_rect = new Rectangle(new Point(bounds.X + 3, bounds.Y + bounds.Height - text_size.Height), text_size);
1936
1937                         g.DrawString (table_name,
1938                                       bold_font, ResPool.GetSolidBrush (grid.ParentRowsForeColor), text_rect, text_format);
1939
1940                         foreach (PropertyDescriptor pd in ((ICustomTypeDescriptor)row.view).GetProperties()) {
1941                                 if (typeof(IBindingList).IsAssignableFrom (pd.PropertyType))
1942                                         continue;
1943
1944                                 text_rect.X += text_rect.Size.Width + 5;
1945
1946                                 string text = String.Format ("{0}: {1}",
1947                                                              pd.Name,
1948                                                              pd.GetValue (row.view));
1949
1950                                 text_rect.Size = g.MeasureString (text, grid.Font).ToSize();
1951                                 text_rect.Y = bounds.Y + bounds.Height - text_rect.Height; // XXX
1952
1953                                 g.DrawString (text,
1954                                               grid.Font, ResPool.GetSolidBrush (grid.ParentRowsForeColor), text_rect, text_format);
1955                         }
1956
1957             // Paint Borders
1958                         if (!grid.FlatMode) {
1959                 CPDrawBorder3D (g, bounds, Border3DStyle.RaisedInner, 
1960                     Border3DSide.Left | Border3DSide.Right | 
1961                     Border3DSide.Top | Border3DSide.Bottom);
1962                         }
1963                 }
1964
1965                 public override void DataGridPaintRowHeaderArrow (Graphics g, Rectangle bounds, DataGrid grid) 
1966                 {               
1967                         Point[] arrow = new Point[3];
1968                         Point P1, P2, P3;
1969                         int centerX, centerY, shiftX;                   
1970                         Rectangle rect;
1971                         
1972                         rect = new Rectangle (bounds.X + bounds.Width /4, 
1973                                 bounds.Y + bounds.Height/4, bounds.Width / 2, bounds.Height / 2);
1974                         
1975                         centerX = rect.Left + rect.Width / 2;
1976                         centerY = rect.Top + rect.Height / 2;
1977                         shiftX = Math.Max (1, rect.Width / 8);                  
1978                         rect.X -= shiftX;
1979                         centerX -= shiftX;                      
1980                         P1 = new Point (centerX, rect.Top - 1);
1981                         P2 = new Point (centerX, rect.Bottom);
1982                         P3 = new Point (rect.Right, centerY);                   
1983                         arrow[0] = P1;
1984                         arrow[1] = P2;
1985                         arrow[2] = P3;
1986                         
1987                         g.FillPolygon (ResPool.GetSolidBrush 
1988                                 (grid.CurrentTableStyle.CurrentHeaderForeColor), arrow, FillMode.Winding);
1989                 }
1990
1991                 public override void DataGridPaintRowHeaderStar (Graphics g, Rectangle bounds, DataGrid grid) 
1992                 {
1993                         int x = bounds.X + 4;
1994                         int y = bounds.Y + 3;
1995                         Pen pen = ResPool.GetPen (grid.CurrentTableStyle.CurrentHeaderForeColor);
1996
1997                         g.DrawLine (pen, x + 4, y, x + 4, y + 8);
1998                         g.DrawLine (pen, x, y + 4, x + 8, y + 4);
1999                         g.DrawLine (pen, x + 1, y + 1, x + 7, y + 7);
2000                         g.DrawLine (pen, x + 7, y + 1, x + 1, y + 7);
2001                 }               
2002
2003                 public override void DataGridPaintRowHeader (Graphics g, Rectangle bounds, int row, DataGrid grid)
2004                 {
2005                         bool is_add_row = grid.ShowEditRow && row == grid.DataGridRows.Length - 1;
2006                         bool is_current_row = row == grid.CurrentCell.RowNumber;
2007
2008                         // Background
2009                         g.FillRectangle (ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderBackColor), bounds);
2010
2011                         // Draw arrow
2012                         if (is_current_row) {
2013                                 if (grid.IsChanging) {
2014                                         g.DrawString ("...", grid.Font,
2015                                                       ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderForeColor),
2016                                                       bounds);
2017                                 } else {
2018                                         Rectangle rect = new Rectangle (bounds.X - 2, bounds.Y, 18, 18);
2019                                         DataGridPaintRowHeaderArrow (g, rect, grid);
2020                                 }
2021                         }
2022                         else if (is_add_row) {
2023                                 DataGridPaintRowHeaderStar (g, bounds, grid);
2024                         }
2025
2026                         if (!grid.FlatMode && !is_add_row) {
2027                                 CPDrawBorder3D (g, bounds, Border3DStyle.RaisedInner, 
2028                                         Border3DSide.Left | Border3DSide.Right | 
2029                                         Border3DSide.Top | Border3DSide.Bottom);
2030                         }
2031                 }
2032                 
2033                 public override void DataGridPaintRows (Graphics g, Rectangle cells, Rectangle clip, DataGrid grid)
2034                 {
2035                         Rectangle rect_row = new Rectangle ();
2036                         Rectangle not_usedarea = new Rectangle ();
2037
2038                         int rowcnt = grid.VisibleRowCount;
2039                         
2040                         bool showing_add_row = false;
2041
2042                         if (grid.RowsCount < grid.DataGridRows.Length) {
2043                                 /* the table has an add row */
2044
2045                                 if (grid.FirstVisibleRow + grid.VisibleRowCount >= grid.DataGridRows.Length) {
2046                                         showing_add_row = true;
2047                                 }
2048                         }
2049
2050                         rect_row.Width = cells.Width + grid.RowHeadersArea.Width;
2051                         for (int r = 0; r < rowcnt; r++) {
2052                                 int row = grid.FirstVisibleRow + r;
2053                                 if (row == grid.DataGridRows.Length - 1)
2054                                         rect_row.Height = grid.DataGridRows[row].Height;
2055                                 else
2056                                         rect_row.Height = grid.DataGridRows[row + 1].VerticalOffset - grid.DataGridRows[row].VerticalOffset;
2057                                 rect_row.Y = cells.Y + grid.DataGridRows[row].VerticalOffset - grid.DataGridRows[grid.FirstVisibleRow].VerticalOffset;
2058                                 if (clip.IntersectsWith (rect_row)) {
2059                                         if (grid.CurrentTableStyle.HasRelations
2060                                             && !(showing_add_row && row == grid.DataGridRows.Length - 1))
2061                                                 DataGridPaintRelationRow (g, row, rect_row, false, clip, grid);
2062                                         else
2063                                                 DataGridPaintRow (g, row, rect_row, showing_add_row && row == grid.DataGridRows.Length - 1, clip, grid);
2064                                 }
2065                         }
2066
2067                         not_usedarea.X = 0;
2068                         // the rowcnt == 0 check is needed because
2069                         // otherwise we'd draw over the caption on
2070                         // empty datasources (since rect_row would be
2071                         // Empty)
2072                         if (rowcnt == 0)
2073                                 not_usedarea.Y = cells.Y;
2074                         else
2075                                 not_usedarea.Y = rect_row.Y + rect_row.Height;
2076                         not_usedarea.Height = cells.Y + cells.Height - rect_row.Y - rect_row.Height;
2077                         not_usedarea.Width = cells.Width + grid.RowHeadersArea.Width;
2078
2079                         g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
2080                 }
2081
2082                 public override void DataGridPaintRelationRow (Graphics g, int row, Rectangle row_rect, bool is_newrow,
2083                                                                Rectangle clip, DataGrid grid)
2084                 {
2085                         Rectangle rect_header;
2086                         Rectangle icon_bounds = new Rectangle ();
2087                         Pen pen = ThemeEngine.Current.ResPool.GetPen (grid.CurrentTableStyle.ForeColor);
2088
2089                         /* paint the header if it's visible and intersects the clip */
2090                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
2091                                 rect_header = row_rect;
2092                                 rect_header.Width = grid.RowHeaderWidth;
2093                                 row_rect.X += grid.RowHeaderWidth;
2094                                 if (clip.IntersectsWith (rect_header)) {
2095                                         DataGridPaintRowHeader (g, rect_header, row, grid);
2096                                 }
2097
2098                                 icon_bounds = rect_header;
2099                                 icon_bounds.X += icon_bounds.Width / 2;
2100                                 icon_bounds.Y += 3;
2101                                 icon_bounds.Width = 8;
2102                                 icon_bounds.Height = 8;
2103
2104                                 g.DrawRectangle (pen, icon_bounds);
2105
2106                                 /* the - part of the icon */
2107                                 g.DrawLine (pen,
2108                                             icon_bounds.X + 2, icon_bounds.Y + icon_bounds.Height / 2,
2109                                             icon_bounds.X + icon_bounds.Width - 2, icon_bounds.Y + icon_bounds.Height / 2);
2110                                             
2111                                 if (!grid.IsExpanded (row)) {
2112                                         /* the | part of the icon */
2113                                         g.DrawLine (pen,
2114                                                     icon_bounds.X + icon_bounds.Width / 2, icon_bounds.Y + 2,
2115                                                     icon_bounds.X + icon_bounds.Width / 2, icon_bounds.Y + icon_bounds.Height - 2);
2116                                 }
2117                         }
2118
2119                         Rectangle nested_rect = row_rect;
2120
2121                         if (grid.DataGridRows[row].IsExpanded)
2122                                 nested_rect.Height -= grid.DataGridRows[row].RelationHeight;
2123
2124                         DataGridPaintRowContents (g, row, nested_rect, is_newrow, clip, grid);
2125
2126                         if (grid.DataGridRows[row].IsExpanded) {
2127                                 // XXX we should create this in the
2128                                 // datagrid and cache it for use by
2129                                 // the theme instead of doing it each
2130                                 // time through here
2131                                 string[] relations = grid.CurrentTableStyle.Relations;
2132                                 StringBuilder relation_builder = new StringBuilder ("");
2133
2134                                 for (int i = 0; i < relations.Length; i ++) {
2135                                         if (i > 0)
2136                                                 relation_builder.Append ("\n");
2137
2138                                         relation_builder.Append (relations[i]);
2139                                 }
2140                                 string relation_text = relation_builder.ToString ();
2141
2142                                 StringFormat string_format = new StringFormat ();
2143                                 string_format.FormatFlags |= StringFormatFlags.NoWrap;
2144
2145
2146                                 //Region prev_clip = g.Clip;
2147                                 //Region current_clip;
2148                                 Rectangle rect_cell = row_rect;
2149
2150                                 rect_cell.X = nested_rect.X + grid.GetColumnStartingPixel (grid.FirstVisibleColumn) - grid.HorizPixelOffset;
2151                                 rect_cell.Y += nested_rect.Height;
2152                                 rect_cell.Height = grid.DataGridRows[row].RelationHeight;
2153
2154                                 rect_cell.Width = 0;
2155                                 int column_cnt = grid.FirstVisibleColumn + grid.VisibleColumnCount;
2156                                 for (int column = grid.FirstVisibleColumn; column < column_cnt; column++) {
2157                                         if (grid.CurrentTableStyle.GridColumnStyles[column].bound == false)
2158                                                 continue;
2159                                         rect_cell.Width += grid.CurrentTableStyle.GridColumnStyles[column].Width;
2160                                 }
2161                                 rect_cell.Width = Math.Max (rect_cell.Width, grid.DataGridRows[row].relation_area.Width);
2162
2163                                 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (grid.CurrentTableStyle.BackColor),
2164                                                  rect_cell);
2165
2166
2167                                 /* draw the line leading from the +/- to the relation area */
2168                                 Rectangle outline = grid.DataGridRows[row].relation_area;
2169                                 outline.Y = rect_cell.Y;
2170                                 outline.Height --;
2171
2172                                 g.DrawLine (pen,
2173                                             icon_bounds.X + icon_bounds.Width / 2, icon_bounds.Y + icon_bounds.Height,
2174                                             icon_bounds.X + icon_bounds.Width / 2, outline.Y + outline.Height / 2);
2175
2176                                 g.DrawLine (pen,
2177                                             icon_bounds.X + icon_bounds.Width / 2, outline.Y + outline.Height / 2,
2178                                             outline.X, outline.Y + outline.Height / 2);
2179
2180                                 g.DrawRectangle (pen, outline);
2181
2182                                 g.DrawString (relation_text, grid.LinkFont, ResPool.GetSolidBrush (grid.LinkColor),
2183                                               outline, string_format);
2184
2185                                 if (row_rect.X + row_rect.Width > rect_cell.X + rect_cell.Width) {
2186                                         Rectangle not_usedarea = new Rectangle ();
2187                                         not_usedarea.X = rect_cell.X + rect_cell.Width;
2188                                         not_usedarea.Width = row_rect.X + row_rect.Width - rect_cell.X - rect_cell.Width;
2189                                         not_usedarea.Y = row_rect.Y;
2190                                         not_usedarea.Height = row_rect.Height;
2191                                         if (clip.IntersectsWith (not_usedarea))
2192                                                 g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor),
2193                                                                  not_usedarea);
2194                                 }
2195                         }
2196                 }
2197
2198                 public override void DataGridPaintRowContents (Graphics g, int row, Rectangle row_rect, bool is_newrow,
2199                                                                Rectangle clip, DataGrid grid)
2200                 {
2201                         Rectangle rect_cell = new Rectangle ();
2202                         int col_pixel;
2203                         Color backcolor, forecolor;
2204                         Brush backBrush, foreBrush;
2205                         Rectangle not_usedarea = Rectangle.Empty;
2206
2207                         rect_cell.Y = row_rect.Y;
2208                         rect_cell.Height = row_rect.Height;
2209
2210                         if (grid.IsSelected (row)) {
2211                                 backcolor =  grid.SelectionBackColor;
2212                                 forecolor =  grid.SelectionForeColor;
2213                         } else {
2214                                 if (row % 2 == 0) {
2215                                         backcolor =  grid.BackColor;
2216                                 } else {
2217                                         backcolor =  grid.AlternatingBackColor;
2218                                 }
2219
2220                                 forecolor =  grid.ForeColor;
2221                         }                       
2222
2223
2224                         backBrush = ResPool.GetSolidBrush (backcolor);
2225                         foreBrush = ResPool.GetSolidBrush (forecolor);
2226
2227                         // PaintCells at row, column
2228                         int column_cnt = grid.FirstVisibleColumn + grid.VisibleColumnCount;
2229                         DataGridCell current_cell = grid.CurrentCell;
2230
2231                         if (column_cnt > 0) {
2232                                 Region prev_clip = g.Clip;
2233                                 Region current_clip;
2234
2235                                 for (int column = grid.FirstVisibleColumn; column < column_cnt; column++) {
2236                                         if (grid.CurrentTableStyle.GridColumnStyles[column].bound == false)
2237                                                 continue;
2238
2239                                         col_pixel = grid.GetColumnStartingPixel (column);
2240
2241                                         rect_cell.X = row_rect.X + col_pixel - grid.HorizPixelOffset;
2242                                         rect_cell.Width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
2243
2244                                         if (clip.IntersectsWith (rect_cell)) {
2245                                                 current_clip = new Region (rect_cell);
2246                                                 current_clip.Intersect (row_rect);
2247                                                 current_clip.Intersect (prev_clip);
2248                                                 g.Clip = current_clip;
2249
2250                                                 Brush colBackBrush = backBrush;
2251                                                 Brush colForeBrush = foreBrush;
2252
2253                                                 // If we are in the precise cell we are editing, then use the normal colors
2254                                                 // even if we are selected.
2255                                                 if (grid.is_editing && column == current_cell.ColumnNumber && row == current_cell.RowNumber) {
2256                                                         colBackBrush = ResPool.GetSolidBrush (grid.BackColor);
2257                                                         colForeBrush = ResPool.GetSolidBrush (grid.ForeColor);
2258                                                 }
2259
2260                                                 if (is_newrow) {
2261                                                         grid.CurrentTableStyle.GridColumnStyles[column].PaintNewRow (g, rect_cell, 
2262                                                                                                                      colBackBrush,
2263                                                                                                                      colForeBrush);
2264                                                 } else {
2265                                                         grid.CurrentTableStyle.GridColumnStyles[column].Paint (g, rect_cell, grid.ListManager, row,
2266                                                                                                                colBackBrush,
2267                                                                                                                colForeBrush,
2268                                                                                                                grid.RightToLeft == RightToLeft.Yes);
2269                                                 }
2270
2271                                                 current_clip.Dispose ();
2272                                         }
2273                                 }
2274
2275                                 g.Clip = prev_clip;
2276                         
2277                                 if (row_rect.X + row_rect.Width > rect_cell.X + rect_cell.Width) {
2278                                         not_usedarea.X = rect_cell.X + rect_cell.Width;
2279                                         not_usedarea.Width = row_rect.X + row_rect.Width - rect_cell.X - rect_cell.Width;
2280                                         not_usedarea.Y = row_rect.Y;
2281                                         not_usedarea.Height = row_rect.Height;
2282                                 }
2283                         }
2284                         else {
2285                                 not_usedarea = row_rect;
2286                         }
2287
2288                         if (!not_usedarea.IsEmpty && clip.IntersectsWith (not_usedarea))
2289                                 g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor),
2290                                                  not_usedarea);
2291                 }
2292
2293                 public override void DataGridPaintRow (Graphics g, int row, Rectangle row_rect, bool is_newrow,
2294                                                        Rectangle clip, DataGrid grid)
2295                 {                       
2296                         /* paint the header if it's visible and intersects the clip */
2297                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
2298                                 Rectangle rect_header = row_rect;
2299                                 rect_header.Width = grid.RowHeaderWidth;
2300                                 row_rect.X += grid.RowHeaderWidth;
2301                                 if (clip.IntersectsWith (rect_header)) {
2302                                         DataGridPaintRowHeader (g, rect_header, row, grid);
2303                                 }
2304                         }
2305
2306                         DataGridPaintRowContents (g, row, row_rect, is_newrow, clip, grid);
2307                 }
2308                 
2309                 #endregion // Datagrid
2310
2311                 #region DataGridView
2312                 #region DataGridViewHeaderCell
2313                 #region DataGridViewRowHeaderCell
2314                 public override bool DataGridViewRowHeaderCellDrawBackground (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
2315                 {
2316                         return false;
2317                 }
2318
2319                 public override bool DataGridViewRowHeaderCellDrawSelectionBackground (DataGridViewRowHeaderCell cell)
2320                 {
2321                         return false;
2322                 }
2323
2324                 public override bool DataGridViewRowHeaderCellDrawBorder (DataGridViewRowHeaderCell cell, Graphics g, Rectangle bounds)
2325                 {
2326                         return false;
2327                 }
2328                 #endregion
2329
2330                 #region DataGridViewColumnHeaderCell
2331                 public override bool DataGridViewColumnHeaderCellDrawBackground (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
2332                 {
2333                         return false;
2334                 }
2335
2336                 public override bool DataGridViewColumnHeaderCellDrawBorder (DataGridViewColumnHeaderCell cell, Graphics g, Rectangle bounds)
2337                 {
2338                         return false;
2339                 }
2340                 #endregion
2341
2342                 public override bool DataGridViewHeaderCellHasPressedStyle  (DataGridView dataGridView)
2343                 {
2344                         return false;
2345                 }
2346
2347                 public override bool DataGridViewHeaderCellHasHotStyle (DataGridView dataGridView)
2348                 {
2349                         return false;
2350                 }
2351                 #endregion
2352                 #endregion
2353
2354                 #region DateTimePicker
2355                 protected virtual void DateTimePickerDrawBorder (DateTimePicker dateTimePicker, Graphics g, Rectangle clippingArea)
2356                 {
2357                         this.CPDrawBorder3D (g, dateTimePicker.ClientRectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, dateTimePicker.BackColor);
2358                 }
2359
2360                 protected virtual void DateTimePickerDrawDropDownButton (DateTimePicker dateTimePicker, Graphics g, Rectangle clippingArea)
2361                 {
2362                         ButtonState state = dateTimePicker.is_drop_down_visible ? ButtonState.Pushed : ButtonState.Normal;
2363                         g.FillRectangle (ResPool.GetSolidBrush (ColorControl), dateTimePicker.drop_down_arrow_rect);
2364                         this.CPDrawComboButton ( 
2365                           g, 
2366                           dateTimePicker.drop_down_arrow_rect, 
2367                           state);
2368                 }
2369
2370                 public override void DrawDateTimePicker(Graphics dc, Rectangle clip_rectangle, DateTimePicker dtp)
2371                 {
2372
2373                         if (!clip_rectangle.IntersectsWith (dtp.ClientRectangle))
2374                                 return;
2375
2376                         // draw the outer border
2377                         Rectangle button_bounds = dtp.ClientRectangle;
2378                         DateTimePickerDrawBorder (dtp, dc, clip_rectangle);
2379
2380                         // deflate by the border width
2381                         if (clip_rectangle.IntersectsWith (dtp.drop_down_arrow_rect)) {
2382                                 button_bounds.Inflate (-2,-2);
2383                                 if (!dtp.ShowUpDown) {
2384                                         DateTimePickerDrawDropDownButton (dtp, dc, clip_rectangle);
2385                                 } else {
2386                                         ButtonState up_state = dtp.is_up_pressed ? ButtonState.Pushed : ButtonState.Normal;
2387                                         ButtonState down_state = dtp.is_down_pressed ? ButtonState.Pushed : ButtonState.Normal;
2388                                         Rectangle up_bounds = dtp.drop_down_arrow_rect;
2389                                         Rectangle down_bounds = dtp.drop_down_arrow_rect;
2390
2391                                         up_bounds.Height = up_bounds.Height / 2;
2392                                         down_bounds.Y = up_bounds.Height;
2393                                         down_bounds.Height = dtp.Height - up_bounds.Height;
2394                                         if (down_bounds.Height > up_bounds.Height)
2395                                         {
2396                                                 down_bounds.Y += 1;
2397                                                 down_bounds.Height -= 1;
2398                                         }
2399
2400                                         up_bounds.Inflate (-1, -1);
2401                                         down_bounds.Inflate (-1, -1);
2402
2403                                         ControlPaint.DrawScrollButton (dc, up_bounds, ScrollButton.Up, up_state);
2404                                         ControlPaint.DrawScrollButton (dc, down_bounds, ScrollButton.Down, down_state);
2405                                 }
2406                         }
2407
2408                         // render the date part
2409                         if (!clip_rectangle.IntersectsWith (dtp.date_area_rect))
2410                                 return;
2411
2412                         // fill the background
2413                         dc.FillRectangle (SystemBrushes.Window, dtp.date_area_rect);
2414
2415                         // Update date_area_rect if we are drawing the checkbox
2416                         Rectangle date_area_rect = dtp.date_area_rect;
2417                         if (dtp.ShowCheckBox) {
2418                                 Rectangle check_box_rect = dtp.CheckBoxRect;
2419                                 date_area_rect.X = date_area_rect.X + check_box_rect.Width + DateTimePicker.check_box_space * 2;
2420                                 date_area_rect.Width = date_area_rect.Width - check_box_rect.Width - DateTimePicker.check_box_space * 2;
2421
2422                                 ButtonState bs = dtp.Checked ? ButtonState.Checked : ButtonState.Normal;
2423                                 CPDrawCheckBox(dc, check_box_rect, bs);
2424
2425                                 if (dtp.is_checkbox_selected)
2426                                         CPDrawFocusRectangle (dc, check_box_rect, dtp.foreground_color, dtp.background_color);
2427                         }
2428
2429                         // render each text part
2430                         using (StringFormat text_format = StringFormat.GenericTypographic)
2431                         {
2432                                 text_format.LineAlignment = StringAlignment.Near;
2433                                 text_format.Alignment = StringAlignment.Near;
2434                                 text_format.FormatFlags = text_format.FormatFlags | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox;
2435                                 text_format.FormatFlags &= ~StringFormatFlags.NoClip;
2436
2437                                 // Calculate the rectangles for each part 
2438                                 if (dtp.part_data.Length > 0 && dtp.part_data[0].drawing_rectangle.IsEmpty)
2439                                 {
2440                                         Graphics gr = dc;
2441                                         for (int i = 0; i < dtp.part_data.Length; i++)
2442                                         {
2443                                                 DateTimePicker.PartData fd = dtp.part_data[i];
2444                                                 RectangleF text_rect = new RectangleF();
2445                                                 string text = fd.GetText(dtp.Value);
2446                                                 text_rect.Size = gr.MeasureString (text, dtp.Font, 250, text_format);
2447                                                 if (!fd.is_literal)
2448                                                         text_rect.Width = Math.Max (dtp.CalculateMaxWidth(fd.value, gr, text_format), text_rect.Width);
2449
2450                                                 if (i > 0) {
2451                                                         text_rect.X = dtp.part_data[i - 1].drawing_rectangle.Right;
2452                                                 } else {
2453                                                         text_rect.X = date_area_rect.X;
2454                                                 }
2455                                                 text_rect.Y = 2;
2456                                                 text_rect.Inflate (1, 0);
2457                                                 fd.drawing_rectangle = text_rect;
2458                                         }
2459                                 }
2460                                 
2461                                 // draw the text part
2462                                 Brush text_brush = ResPool.GetSolidBrush (dtp.ShowCheckBox && dtp.Checked == false ?
2463                                                 SystemColors.GrayText : dtp.ForeColor); // Use GrayText if Checked is false
2464                                 RectangleF clip_rectangleF = clip_rectangle;
2465
2466                                 for (int i = 0; i < dtp.part_data.Length; i++)
2467                                 {
2468                                         DateTimePicker.PartData fd = dtp.part_data [i];
2469                                         string text;
2470
2471                                         if (!clip_rectangleF.IntersectsWith (fd.drawing_rectangle))
2472                                                 continue;
2473
2474                                         text = dtp.editing_part_index == i ? dtp.editing_text : fd.GetText (dtp.Value);
2475
2476                                         PointF text_position = new PointF ();
2477                                         SizeF text_size;
2478                                         RectangleF text_rect;
2479
2480                                         text_size = dc.MeasureString (text, dtp.Font, 250, text_format);
2481                                         text_position.X = (fd.drawing_rectangle.Left + fd.drawing_rectangle.Width / 2) - text_size.Width / 2;
2482                                         text_position.Y = (fd.drawing_rectangle.Top + fd.drawing_rectangle.Height / 2) - text_size.Height / 2;
2483                                         text_rect = new RectangleF (text_position, text_size);
2484                                         text_rect = RectangleF.Intersect (text_rect, date_area_rect);
2485                                         
2486                                         if (text_rect.IsEmpty)
2487                                                 break;
2488
2489                                         if (text_rect.Right >= date_area_rect.Right)
2490                                                 text_format.FormatFlags &= ~StringFormatFlags.NoClip;
2491                                         else
2492                                                 text_format.FormatFlags |= StringFormatFlags.NoClip;
2493                                         
2494                                         if (fd.Selected) {
2495                                                 dc.FillRectangle (SystemBrushes.Highlight, text_rect);
2496                                                 dc.DrawString (text, dtp.Font, SystemBrushes.HighlightText, text_rect, text_format);
2497                                         
2498                                         } else {
2499                                                 dc.DrawString (text, dtp.Font, text_brush, text_rect, text_format);
2500                                         }
2501
2502                                         if (fd.drawing_rectangle.Right > date_area_rect.Right)
2503                                                 break; // the next part would be not be visible, so don't draw anything more.
2504                                 }
2505                         }
2506                 }
2507
2508                 public override bool DateTimePickerBorderHasHotElementStyle {
2509                         get {
2510                                 return false;
2511                         }
2512                 }
2513
2514                 public override Rectangle DateTimePickerGetDropDownButtonArea (DateTimePicker dateTimePicker)
2515                 {
2516                         Rectangle rect = dateTimePicker.ClientRectangle;
2517                         rect.X = rect.Right - SystemInformation.VerticalScrollBarWidth - 2;
2518                         if (rect.Width > (SystemInformation.VerticalScrollBarWidth + 2)) {
2519                                 rect.Width = SystemInformation.VerticalScrollBarWidth;
2520                         } else {
2521                                 rect.Width = Math.Max (rect.Width - 2, 0);
2522                         }
2523                         
2524                         rect.Inflate (0, -2);
2525                         return rect;
2526                 }
2527
2528                 public override Rectangle DateTimePickerGetDateArea (DateTimePicker dateTimePicker)
2529                 {
2530                         Rectangle rect = dateTimePicker.ClientRectangle;
2531                         if (dateTimePicker.ShowUpDown) {
2532                                 // set the space to the left of the up/down button
2533                                 if (rect.Width > (DateTimePicker.up_down_width + 4)) {
2534                                         rect.Width -= (DateTimePicker.up_down_width + 4);
2535                                 } else {
2536                                         rect.Width = 0;
2537                                 }
2538                         } else {
2539                                 // set the space to the left of the up/down button
2540                                 // TODO make this use up down button
2541                                 if (rect.Width > (SystemInformation.VerticalScrollBarWidth + 4)) {
2542                                         rect.Width -= SystemInformation.VerticalScrollBarWidth;
2543                                 } else {
2544                                         rect.Width = 0;
2545                                 }
2546                         }
2547                         
2548                         rect.Inflate (-2, -2);
2549                         return rect;
2550                 }
2551                 public override bool DateTimePickerDropDownButtonHasHotElementStyle {
2552                         get {
2553                                 return false;
2554                         }
2555                 }
2556                 #endregion // DateTimePicker
2557
2558                 #region GroupBox
2559                 public override void DrawGroupBox (Graphics dc,  Rectangle area, GroupBox box) {
2560                         StringFormat    text_format;
2561                         SizeF           size;
2562                         int             width;
2563                         int             y;
2564
2565                         dc.FillRectangle (GetControlBackBrush (box.BackColor), box.ClientRectangle);
2566                         
2567                         text_format = new StringFormat();
2568                         text_format.HotkeyPrefix = HotkeyPrefix.Show;
2569
2570                         size = dc.MeasureString (box.Text, box.Font);
2571                         width = 0;
2572
2573                         if (size.Width > 0) {
2574                                 width = ((int) size.Width) + 7;
2575                         
2576                                 if (width > box.Width - 16)
2577                                         width = box.Width - 16;
2578                         }
2579                         
2580                         y = box.Font.Height / 2;
2581
2582                         // Clip the are that the text will be in
2583                         Region prev_clip = dc.Clip;
2584                         dc.SetClip (new Rectangle (10, 0, width, box.Font.Height), CombineMode.Exclude);
2585                         /* Draw group box*/
2586                         CPDrawBorder3D (dc, new Rectangle (0, y, box.Width, box.Height - y), Border3DStyle.Etched, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, box.BackColor);
2587                         dc.Clip = prev_clip;
2588
2589                         /* Text */
2590                         if (box.Text.Length != 0) {
2591                                 if (box.Enabled) {
2592                                         dc.DrawString (box.Text, box.Font, ResPool.GetSolidBrush (box.ForeColor), 10, 0, text_format);
2593                                 } else {
2594                                         CPDrawStringDisabled (dc, box.Text, box.Font, box.BackColor, 
2595                                                               new RectangleF (10, 0, width,  box.Font.Height), text_format);
2596                                 }
2597                         }
2598                         
2599                         text_format.Dispose (); 
2600                 }
2601
2602                 public override Size GroupBoxDefaultSize {
2603                         get {
2604                                 return new Size (200,100);
2605                         }
2606                 }
2607                 #endregion
2608
2609                 #region HScrollBar
2610                 public override Size HScrollBarDefaultSize {
2611                         get {
2612                                 return new Size (80, this.ScrollBarButtonSize);
2613                         }
2614                 }
2615
2616                 #endregion      // HScrollBar
2617
2618                 #region ListBox
2619
2620                 public override void DrawListBoxItem (ListBox ctrl, DrawItemEventArgs e)
2621                 {
2622                         Color back_color, fore_color;
2623                         
2624                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
2625                                 back_color = ColorHighlight;
2626                                 fore_color = ColorHighlightText;
2627                         } else {
2628                                 back_color = e.BackColor;
2629                                 fore_color = e.ForeColor;
2630                         }
2631
2632                         e.Graphics.FillRectangle (ResPool.GetSolidBrush (back_color), e.Bounds);
2633
2634                         e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
2635                                                ResPool.GetSolidBrush (fore_color),
2636                                                e.Bounds, ctrl.StringFormat);
2637                                         
2638                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
2639                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, fore_color, back_color);
2640                 }
2641                 
2642                 #endregion ListBox
2643
2644                 #region ListView
2645                 // Drawing
2646                 public override void DrawListViewItems (Graphics dc, Rectangle clip, ListView control)
2647                 {
2648                         bool details = control.View == View.Details;
2649                         int first = control.FirstVisibleIndex;  
2650                         int lastvisibleindex = control.LastVisibleIndex;
2651
2652                         if (control.VirtualMode)
2653                                 control.OnCacheVirtualItems (new CacheVirtualItemsEventArgs (first, lastvisibleindex));
2654
2655                         for (int i = first; i <= lastvisibleindex; i++) {                                       
2656                                 ListViewItem item = control.GetItemAtDisplayIndex (i);
2657                                 if (clip.IntersectsWith (item.Bounds)) {
2658                                         bool owner_draw = false;
2659                                         if (control.OwnerDraw)
2660                                                 owner_draw = DrawListViewItemOwnerDraw (dc, item, i);
2661                                         if (!owner_draw)
2662                                         {
2663                                                 DrawListViewItem (dc, control, item);
2664                                                 if (control.View == View.Details)
2665                                                         DrawListViewSubItems (dc, control, item);
2666                                         }
2667                                 }
2668                         }       
2669
2670                         if (control.UsingGroups) {
2671                                 // Use InternalCount instead of Count to take into account Default Group as needed
2672                                 for (int i = 0; i < control.Groups.InternalCount; i++) {
2673                                         ListViewGroup group = control.Groups.GetInternalGroup (i);
2674                                         if (group.ItemCount > 0 && clip.IntersectsWith (group.HeaderBounds))
2675                                                 DrawListViewGroupHeader (dc, control, group);
2676                                 }
2677                         }
2678
2679                         ListViewInsertionMark insertion_mark = control.InsertionMark;
2680                         int insertion_mark_index = insertion_mark.Index;
2681                         if (Application.VisualStylesEnabled && insertion_mark.Bounds != Rectangle.Empty &&
2682                                         (control.View != View.Details && control.View != View.List) &&
2683                                         insertion_mark_index > -1 && insertion_mark_index < control.Items.Count) {
2684
2685                                 Brush brush = ResPool.GetSolidBrush (insertion_mark.Color);
2686                                 dc.FillRectangle (brush, insertion_mark.Line);
2687                                 dc.FillPolygon (brush, insertion_mark.TopTriangle);
2688                                 dc.FillPolygon (brush, insertion_mark.BottomTriangle);
2689                         }
2690                         
2691                         // draw the gridlines
2692                         if (details && control.GridLines && !control.UsingGroups) {
2693                                 Size control_size = control.ClientSize;
2694                                 int top = (control.HeaderStyle == ColumnHeaderStyle.None) ?
2695                                         0 : control.header_control.Height;
2696
2697                                 // draw vertical gridlines
2698                                 foreach (ColumnHeader col in control.Columns) {
2699                                         int column_right = col.Rect.Right - control.h_marker;
2700                                         dc.DrawLine (SystemPens.Control,
2701                                                      column_right, top,
2702                                                      column_right, control_size.Height);
2703                                 }
2704
2705                                 // draw horizontal gridlines
2706                                 int item_height = control.ItemSize.Height;
2707                                 if (item_height == 0)
2708                                         item_height =  control.Font.Height + 2;
2709
2710                                 int y = top + item_height - (control.v_marker % item_height); // scroll bar offset
2711                                 while (y < control_size.Height) {
2712                                         dc.DrawLine (SystemPens.Control, 0, y, control_size.Width, y);
2713                                         y += item_height;
2714                                 }
2715                         }                       
2716                         
2717                         // Draw corner between the two scrollbars
2718                         if (control.h_scroll.Visible == true && control.v_scroll.Visible == true) {
2719                                 Rectangle rect = new Rectangle ();
2720                                 rect.X = control.h_scroll.Location.X + control.h_scroll.Width;
2721                                 rect.Width = control.v_scroll.Width;
2722                                 rect.Y = control.v_scroll.Location.Y + control.v_scroll.Height;
2723                                 rect.Height = control.h_scroll.Height;
2724                                 dc.FillRectangle (SystemBrushes.Control, rect);
2725                         }
2726
2727                         Rectangle box_select_rect = control.item_control.BoxSelectRectangle;
2728                         if (!box_select_rect.Size.IsEmpty)
2729                                 dc.DrawRectangle (ResPool.GetDashPen (ColorControlText, DashStyle.Dot), box_select_rect);
2730
2731                 }
2732
2733                 public override void DrawListViewHeader (Graphics dc, Rectangle clip, ListView control)
2734                 {       
2735                         bool details = (control.View == View.Details);
2736                                 
2737                         // border is drawn directly in the Paint method
2738                         if (details && control.HeaderStyle != ColumnHeaderStyle.None) {                         
2739                                 dc.FillRectangle (SystemBrushes.Control,
2740                                                   0, 0, control.TotalWidth, control.Font.Height + 5);
2741                                 if (control.Columns.Count > 0) {
2742                                         foreach (ColumnHeader col in control.Columns) {
2743                                                 Rectangle rect = col.Rect;
2744                                                 rect.X -= control.h_marker;
2745
2746                                                 bool owner_draw = false;
2747                                                 if (control.OwnerDraw)
2748                                                         owner_draw = DrawListViewColumnHeaderOwnerDraw (dc, control, col, rect);
2749                                                 if (owner_draw)
2750                                                         continue;
2751
2752                                                 ListViewDrawColumnHeaderBackground (control, col, dc, rect, clip);
2753                                                 rect.X += 5;
2754                                                 rect.Width -= 10;
2755                                                 if (rect.Width <= 0)
2756                                                         continue;
2757
2758                                                 int image_index;
2759                                                 if (control.SmallImageList == null)
2760                                                         image_index = -1;
2761                                                 else 
2762                                                         image_index = col.ImageKey == String.Empty ? col.ImageIndex : control.SmallImageList.Images.IndexOfKey (col.ImageKey);
2763
2764                                                 if (image_index > -1 && image_index < control.SmallImageList.Images.Count) {
2765                                                         int image_width = control.SmallImageList.ImageSize.Width + 5;
2766                                                         int text_width = (int)dc.MeasureString (col.Text, control.Font).Width;
2767                                                         int x_origin = rect.X;
2768                                                         int y_origin = rect.Y + ((rect.Height - control.SmallImageList.ImageSize.Height) / 2);
2769
2770                                                         switch (col.TextAlign) {
2771                                                                 case HorizontalAlignment.Left:
2772                                                                         break;
2773                                                                 case HorizontalAlignment.Right:
2774                                                                         x_origin = rect.Right - (text_width + image_width);
2775                                                                         break;
2776                                                                 case HorizontalAlignment.Center:
2777                                                                         x_origin = (rect.Width - (text_width + image_width)) / 2 + rect.X;
2778                                                                         break;
2779                                                         }
2780
2781                                                         if (x_origin < rect.X)
2782                                                                 x_origin = rect.X;
2783
2784                                                         control.SmallImageList.Draw (dc, new Point (x_origin, y_origin), image_index);
2785                                                         rect.X += image_width;
2786                                                         rect.Width -= image_width;
2787                                                 }
2788
2789                                                 dc.DrawString (col.Text, control.Font, SystemBrushes.ControlText, rect, col.Format);
2790                                         }
2791                                         int right = control.GetReorderedColumn (control.Columns.Count - 1).Rect.Right - control.h_marker;
2792                                         if (right < control.Right) {
2793                                                 Rectangle rect = control.Columns [0].Rect;
2794                                                 rect.X = right;
2795                                                 rect.Width = control.Right - right;
2796                                                 ListViewDrawUnusedHeaderBackground (control, dc, rect, clip);
2797                                         }
2798                                 }
2799                         }
2800                 }
2801
2802                 protected virtual void ListViewDrawColumnHeaderBackground (ListView listView, ColumnHeader columnHeader, Graphics g, Rectangle area, Rectangle clippingArea)
2803                 {
2804                         ButtonState state;
2805                         if (listView.HeaderStyle == ColumnHeaderStyle.Clickable)
2806                                 state = columnHeader.Pressed ? ButtonState.Pushed : ButtonState.Normal;
2807                         else
2808                                 state = ButtonState.Flat;
2809                         CPDrawButton (g, area, state);
2810                 }
2811                 
2812                 protected virtual void ListViewDrawUnusedHeaderBackground (ListView listView, Graphics g, Rectangle area, Rectangle clippingArea)
2813                 {
2814                         ButtonState state;
2815                         if (listView.HeaderStyle == ColumnHeaderStyle.Clickable)
2816                                 state = ButtonState.Normal;
2817                         else
2818                                 state = ButtonState.Flat;
2819                         CPDrawButton (g, area, state);
2820                 }
2821
2822                 public override void DrawListViewHeaderDragDetails (Graphics dc, ListView view, ColumnHeader col, int target_x)
2823                 {
2824                         Rectangle rect = col.Rect;
2825                         rect.X -= view.h_marker;
2826                         Color color = Color.FromArgb (0x7f, ColorControlDark.R, ColorControlDark.G, ColorControlDark.B);
2827                         dc.FillRectangle (ResPool.GetSolidBrush (color), rect);
2828                         rect.X += 3;
2829                         rect.Width -= 8;
2830                         if (rect.Width <= 0)
2831                                 return;
2832                         color = Color.FromArgb (0x7f, ColorControlText.R, ColorControlText.G, ColorControlText.B);
2833                         dc.DrawString (col.Text, view.Font, ResPool.GetSolidBrush (color), rect, col.Format);
2834                         dc.DrawLine (ResPool.GetSizedPen (ColorHighlight, 2), target_x, 0, target_x, col.Rect.Height);
2835                 }
2836
2837                 protected virtual bool DrawListViewColumnHeaderOwnerDraw (Graphics dc, ListView control, ColumnHeader column, Rectangle bounds)
2838                 {
2839                         ListViewItemStates state = ListViewItemStates.ShowKeyboardCues;
2840                         if (column.Pressed)
2841                                 state |= ListViewItemStates.Selected;
2842
2843                         DrawListViewColumnHeaderEventArgs args = new DrawListViewColumnHeaderEventArgs (dc,
2844                                         bounds, column.Index, column, state, SystemColors.ControlText, ThemeEngine.Current.ColorControl, DefaultFont);
2845                         control.OnDrawColumnHeader (args);
2846
2847                         return !args.DrawDefault;
2848                 }
2849
2850                 protected virtual bool DrawListViewItemOwnerDraw (Graphics dc, ListViewItem item, int index)
2851                 {
2852                         ListViewItemStates item_state = ListViewItemStates.ShowKeyboardCues;
2853                         if (item.Selected)
2854                                 item_state |= ListViewItemStates.Selected;
2855                         if (item.Focused)
2856                                 item_state |= ListViewItemStates.Focused;
2857                                                 
2858                         DrawListViewItemEventArgs args = new DrawListViewItemEventArgs (dc,
2859                                         item, item.Bounds, index, item_state);
2860                         item.ListView.OnDrawItem (args);
2861
2862                         if (args.DrawDefault)
2863                                 return false;
2864
2865                         if (item.ListView.View == View.Details) {
2866                                 int count = Math.Min (item.ListView.Columns.Count, item.SubItems.Count);
2867                                 
2868                                 // Do system drawing for subitems if no owner draw is done
2869                                 for (int j = 0; j < count; j++) {
2870                                         if (!DrawListViewSubItemOwnerDraw (dc, item, item_state, j)) {
2871                                                 if (j == 0) // The first sub item contains the main item semantics
2872                                                         DrawListViewItem (dc, item.ListView, item);
2873                                                 else
2874                                                         DrawListViewSubItem (dc, item.ListView, item, j);
2875                                         }
2876                                 }
2877                         }
2878                         
2879                         return true;
2880                 }
2881
2882                 protected virtual void DrawListViewItem (Graphics dc, ListView control, ListViewItem item)
2883                 {                               
2884                         Rectangle rect_checkrect = item.CheckRectReal;
2885                         Rectangle icon_rect = item.GetBounds (ItemBoundsPortion.Icon);
2886                         Rectangle full_rect = item.GetBounds (ItemBoundsPortion.Entire);
2887                         Rectangle text_rect = item.GetBounds (ItemBoundsPortion.Label);                 
2888
2889                         // Tile view doesn't support CheckBoxes
2890                         if (control.CheckBoxes && control.View != View.Tile) {
2891                                 if (control.StateImageList == null) {
2892                                         // Make sure we've got at least a line width of 1
2893                                         int check_wd = Math.Max (3, rect_checkrect.Width / 6);
2894                                         int scale = Math.Max (1, rect_checkrect.Width / 12);
2895
2896                                         // set the checkbox background
2897                                         dc.FillRectangle (SystemBrushes.Window,
2898                                                           rect_checkrect);
2899                                         // define a rectangle inside the border area
2900                                         Rectangle rect = new Rectangle (rect_checkrect.X + 2,
2901                                                                         rect_checkrect.Y + 2,
2902                                                                         rect_checkrect.Width - 4,
2903                                                                         rect_checkrect.Height - 4);
2904                                         Pen pen = ResPool.GetSizedPen (this.ColorWindowText, 2);
2905                                         dc.DrawRectangle (pen, rect);
2906
2907                                         // Need to draw a check-mark
2908                                         if (item.Checked) {
2909                                                 Pen check_pen = ResPool.GetSizedPen (this.ColorWindowText, 1);
2910                                                 // adjustments to get the check-mark at the right place
2911                                                 rect.X ++; rect.Y ++;
2912                                                 // following logic is taken from DrawFrameControl method
2913                                                 int x_offset = rect.Width / 5;
2914                                                 int y_offset = rect.Height / 3;
2915                                                 for (int i = 0; i < check_wd; i++) {
2916                                                         dc.DrawLine (check_pen, rect.Left + x_offset,
2917                                                                      rect.Top + y_offset + i,
2918                                                                      rect.Left + x_offset + 2 * scale,
2919                                                                      rect.Top + y_offset + 2 * scale + i);
2920                                                         dc.DrawLine (check_pen,
2921                                                                      rect.Left + x_offset + 2 * scale,
2922                                                                      rect.Top + y_offset + 2 * scale + i,
2923                                                                      rect.Left + x_offset + 6 * scale,
2924                                                                      rect.Top + y_offset - 2 * scale + i);
2925                                                 }
2926                                         }
2927                                 }
2928                                 else {
2929                                         int simage_idx;
2930                                         if (item.Checked)
2931                                                 simage_idx = control.StateImageList.Images.Count > 1 ? 1 : -1;
2932                                         else
2933                                                 simage_idx = control.StateImageList.Images.Count > 0 ? 0 : -1;
2934
2935                                         if (simage_idx > -1)
2936                                                 control.StateImageList.Draw (dc, rect_checkrect.Location, simage_idx);
2937                                 }
2938                         }
2939
2940                         ImageList image_list = control.View == View.LargeIcon || control.View == View.Tile ? control.LargeImageList : control.SmallImageList;
2941                         if (image_list != null) {
2942                                 int idx;
2943
2944                                 if (item.ImageKey != String.Empty)
2945                                         idx = image_list.Images.IndexOfKey (item.ImageKey);
2946                                 else
2947                                         idx = item.ImageIndex;
2948
2949                                 if (idx > -1 && idx < image_list.Images.Count)
2950                                         image_list.Draw (dc, icon_rect.Location, idx);
2951                         }
2952
2953                         // draw the item text                   
2954                         // format for the item text
2955                         StringFormat format = new StringFormat ();
2956                         if (control.View == View.SmallIcon || control.View == View.LargeIcon)
2957                                 format.LineAlignment = StringAlignment.Near;
2958                         else
2959                                 format.LineAlignment = StringAlignment.Center;
2960                         if (control.View == View.LargeIcon)
2961                                 format.Alignment = StringAlignment.Center;
2962                         else
2963                                 format.Alignment = StringAlignment.Near;
2964                         
2965                         if (control.LabelWrap && control.View != View.Details && control.View != View.Tile)
2966                                 format.FormatFlags = StringFormatFlags.LineLimit;
2967                         else
2968                                 format.FormatFlags = StringFormatFlags.NoWrap;
2969
2970                         if ((control.View == View.LargeIcon && !item.Focused) || control.View == View.Details || control.View == View.Tile)
2971                                 format.Trimming = StringTrimming.EllipsisCharacter;
2972
2973                         Rectangle highlight_rect = text_rect;
2974                         if (control.View == View.Details) { // Adjustments for Details view
2975                                 Size text_size = Size.Ceiling (dc.MeasureString (item.Text, item.Font));
2976
2977                                 if (!control.FullRowSelect) // Selection shouldn't be outside the item bounds
2978                                         highlight_rect.Width = Math.Min (text_size.Width + 4, text_rect.Width);
2979                         }
2980
2981                         if (item.Selected && control.Focused)
2982                                 dc.FillRectangle (SystemBrushes.Highlight, highlight_rect);
2983                         else if (item.Selected && !control.HideSelection)
2984                                 dc.FillRectangle (SystemBrushes.Control, highlight_rect);
2985                         else
2986                                 dc.FillRectangle (ResPool.GetSolidBrush (item.BackColor), text_rect);
2987                         
2988                         Brush textBrush =
2989                                 !control.Enabled ? SystemBrushes.ControlLight :
2990                                 (item.Selected && control.Focused) ? SystemBrushes.HighlightText :
2991                                 this.ResPool.GetSolidBrush (item.ForeColor);
2992
2993                         // Tile view renders its Text in a different fashion
2994                         if (control.View == View.Tile && Application.VisualStylesEnabled) {
2995                                 // Item.Text is drawn using its first subitem's bounds
2996                                 dc.DrawString (item.Text, item.Font, textBrush, item.SubItems [0].Bounds, format);
2997
2998                                 int count = Math.Min (control.Columns.Count, item.SubItems.Count);
2999                                 for (int i = 1; i < count; i++) {
3000                                         ListViewItem.ListViewSubItem sub_item = item.SubItems [i];
3001                                         if (sub_item.Text == null || sub_item.Text.Length == 0)
3002                                                 continue;
3003
3004                                         Brush itemBrush = item.Selected && control.Focused ? 
3005                                                 SystemBrushes.HighlightText : GetControlForeBrush (sub_item.ForeColor);
3006                                         dc.DrawString (sub_item.Text, sub_item.Font, itemBrush, sub_item.Bounds, format);
3007                                 }
3008                         } else
3009                         
3010                         if (item.Text != null && item.Text.Length > 0) {
3011                                 Font font = item.Font;
3012
3013                                 if (control.HotTracking && item.Hot)
3014                                         font = item.HotFont;
3015
3016                                 if (item.Selected && control.Focused)
3017                                         dc.DrawString (item.Text, font, textBrush, highlight_rect, format);
3018                                 else
3019                                         dc.DrawString (item.Text, font, textBrush, text_rect, format);
3020                         }
3021
3022                         if (item.Focused && control.Focused) {                          
3023                                 Rectangle focus_rect = highlight_rect;
3024                                 if (control.FullRowSelect && control.View == View.Details) {
3025                                         int width = 0;
3026                                         foreach (ColumnHeader col in control.Columns)
3027                                                 width += col.Width;
3028                                         focus_rect = new Rectangle (0, full_rect.Y, width, full_rect.Height);
3029                                 }
3030                                 if (control.ShowFocusCues) {
3031                                         if (item.Selected)
3032                                                 CPDrawFocusRectangle (dc, focus_rect, ColorHighlightText, ColorHighlight);
3033                                         else
3034                                                 CPDrawFocusRectangle (dc, focus_rect, control.ForeColor, control.BackColor);
3035                                 }
3036                         }
3037
3038                         format.Dispose ();
3039                 }
3040
3041                 protected virtual void DrawListViewSubItems (Graphics dc, ListView control, ListViewItem item)
3042                 {
3043                         int columns_count = control.Columns.Count;
3044                         int count = Math.Min (item.SubItems.Count, columns_count);
3045                         // 0th item already done (in this case)
3046                         for (int i = 1; i < count; i++)
3047                                 DrawListViewSubItem (dc, control, item, i);
3048
3049                         // Fill in selection for remaining columns if Column.Count > SubItems.Count
3050                         Rectangle sub_item_rect = item.GetBounds (ItemBoundsPortion.Label);
3051                         if (item.Selected && (control.Focused || !control.HideSelection) && control.FullRowSelect) {
3052                                 for (int index = count; index < columns_count; index++) {
3053                                         ColumnHeader col = control.Columns [index];
3054                                         sub_item_rect.X = col.Rect.X - control.h_marker;
3055                                         sub_item_rect.Width = col.Wd;
3056                                         dc.FillRectangle (control.Focused ? SystemBrushes.Highlight : SystemBrushes.Control, 
3057                                                         sub_item_rect);
3058                                 }
3059                         }
3060                 }
3061
3062                 protected virtual void DrawListViewSubItem (Graphics dc, ListView control, ListViewItem item, int index)
3063                 {
3064                         ListViewItem.ListViewSubItem subItem = item.SubItems [index];
3065                         ColumnHeader col = control.Columns [index];
3066                         StringFormat format = new StringFormat ();
3067                         format.Alignment = col.Format.Alignment;
3068                         format.LineAlignment = StringAlignment.Center;
3069                         format.FormatFlags = StringFormatFlags.NoWrap;
3070                         format.Trimming = StringTrimming.EllipsisCharacter;
3071
3072                         Rectangle sub_item_rect = subItem.Bounds;
3073                         Rectangle sub_item_text_rect = sub_item_rect;
3074                         sub_item_text_rect.X += 3;
3075                         sub_item_text_rect.Width -= ListViewItemPaddingWidth;
3076                                                 
3077                         SolidBrush sub_item_back_br = null;
3078                         SolidBrush sub_item_fore_br = null;
3079                         Font sub_item_font = null;
3080                                                 
3081                         if (item.UseItemStyleForSubItems) {
3082                                 sub_item_back_br = ResPool.GetSolidBrush (item.BackColor);
3083                                 sub_item_fore_br = ResPool.GetSolidBrush (item.ForeColor);
3084
3085                                 // Hot tracking for subitems only applies when UseStyle is true
3086                                 if (control.HotTracking && item.Hot)
3087                                         sub_item_font = item.HotFont;
3088                                 else
3089                                         sub_item_font = item.Font;
3090                         } else {
3091                                 sub_item_back_br = ResPool.GetSolidBrush (subItem.BackColor);
3092                                 sub_item_fore_br = ResPool.GetSolidBrush (subItem.ForeColor);
3093                                 sub_item_font = subItem.Font;
3094                         }
3095                                                 
3096                         if (item.Selected && (control.Focused || !control.HideSelection) && control.FullRowSelect) {
3097                                 Brush bg, text;
3098                                 if (control.Focused) {
3099                                         bg = SystemBrushes.Highlight;
3100                                         text = SystemBrushes.HighlightText;
3101                                 } else {
3102                                         bg = SystemBrushes.Control;
3103                                         text = sub_item_fore_br;
3104                                                         
3105                                 }
3106                                                         
3107                                 dc.FillRectangle (bg, sub_item_rect);
3108                                 if (subItem.Text != null && subItem.Text.Length > 0)
3109                                         dc.DrawString (subItem.Text, sub_item_font,
3110                                                         text, sub_item_text_rect, format);
3111                         } else {
3112                                 dc.FillRectangle (sub_item_back_br, sub_item_rect);
3113                                 if (subItem.Text != null && subItem.Text.Length > 0)
3114                                         dc.DrawString (subItem.Text, sub_item_font,
3115                                                         sub_item_fore_br,
3116                                                         sub_item_text_rect, format);
3117                         }
3118
3119                         format.Dispose ();
3120                 }
3121
3122                 protected virtual bool DrawListViewSubItemOwnerDraw (Graphics dc, ListViewItem item, ListViewItemStates state, int index)
3123                 {
3124                         ListView control = item.ListView;
3125                         ListViewItem.ListViewSubItem subitem = item.SubItems [index];
3126
3127                         DrawListViewSubItemEventArgs args = new DrawListViewSubItemEventArgs (dc, subitem.Bounds, item, 
3128                                         subitem, item.Index, index, control.Columns [index], state);
3129                         control.OnDrawSubItem (args);
3130                         
3131                         return !args.DrawDefault;
3132                 }
3133
3134                 protected virtual void DrawListViewGroupHeader (Graphics dc, ListView control, ListViewGroup group)
3135                 {
3136                         Rectangle text_bounds = group.HeaderBounds;
3137                         Rectangle header_bounds = group.HeaderBounds;
3138                         text_bounds.Offset (8, 0);
3139                         text_bounds.Inflate (-8, 0);
3140                         int text_height = control.Font.Height + 2; // add a tiny padding between the text and the group line
3141
3142                         Font font = new Font (control.Font, control.Font.Style | FontStyle.Bold);
3143                         Brush brush = new LinearGradientBrush (new Point (header_bounds.Left, 0), new Point (header_bounds.Left + ListViewGroupLineWidth, 0), 
3144                                         SystemColors.Desktop, Color.White);
3145                         Pen pen = new Pen (brush);
3146
3147                         StringFormat sformat = new StringFormat ();
3148                         switch (group.HeaderAlignment) {
3149                                 case HorizontalAlignment.Left:
3150                                         sformat.Alignment = StringAlignment.Near;
3151                                         break;
3152                                 case HorizontalAlignment.Center:
3153                                         sformat.Alignment = StringAlignment.Center;
3154                                         break;
3155                                 case HorizontalAlignment.Right:
3156                                         sformat.Alignment = StringAlignment.Far;
3157                                         break;
3158                         }
3159
3160                         sformat.LineAlignment = StringAlignment.Near;
3161                         dc.DrawString (group.Header, font, SystemBrushes.ControlText, text_bounds, sformat);
3162                         dc.DrawLine (pen, header_bounds.Left, header_bounds.Top + text_height, header_bounds.Left + ListViewGroupLineWidth, 
3163                                         header_bounds.Top + text_height);
3164
3165                         sformat.Dispose ();
3166                         font.Dispose ();
3167                         pen.Dispose ();
3168                         brush.Dispose ();
3169                 }
3170
3171                 public override bool ListViewHasHotHeaderStyle {
3172                         get {
3173                                 return false;
3174                         }
3175                 }
3176
3177                 // Sizing
3178                 public override int ListViewGetHeaderHeight (ListView listView, Font font)
3179                 {
3180                         return ListViewGetHeaderHeight (font);
3181                 }
3182
3183                 static int ListViewGetHeaderHeight (Font font)
3184                 {
3185                         return font.Height + 5;
3186                 }
3187
3188                 public static int ListViewGetHeaderHeight ()
3189                 {
3190                         return ListViewGetHeaderHeight (ThemeEngine.Current.DefaultFont);
3191                 }
3192
3193                 public override Size ListViewCheckBoxSize {
3194                         get { return new Size (16, 16); }
3195                 }
3196
3197                 public override int ListViewColumnHeaderHeight {
3198                         get { return 16; }
3199                 }
3200
3201                 public override int ListViewDefaultColumnWidth {
3202                         get { return 60; }
3203                 }
3204
3205                 public override int ListViewVerticalSpacing {
3206                         get { return 22; }
3207                 }
3208
3209                 public override int ListViewEmptyColumnWidth {
3210                         get { return 10; }
3211                 }
3212
3213                 public override int ListViewHorizontalSpacing {
3214                         get { return 4; }
3215                 }
3216
3217                 public override int ListViewItemPaddingWidth {
3218                         get { return 6; }
3219                 }
3220
3221                 public override Size ListViewDefaultSize {
3222                         get { return new Size (121, 97); }
3223                 }
3224
3225                 public override int ListViewGroupHeight { 
3226                         get { return 20; }
3227                 }
3228
3229                 public int ListViewGroupLineWidth {
3230                         get { return 200; }
3231                 }
3232
3233                 public override int ListViewTileWidthFactor {
3234                         get { return 22; }
3235                 }
3236
3237                 public override int ListViewTileHeightFactor {
3238                         get { return 3; }
3239                 }
3240                 #endregion      // ListView
3241                 
3242                 #region Menus
3243                 
3244                 public override void CalcItemSize (Graphics dc, MenuItem item, int y, int x, bool menuBar)
3245                 {
3246                         item.X = x;
3247                         item.Y = y;
3248
3249                         if (item.Visible == false) {
3250                                 item.Width = 0;
3251                                 item.Height = 0;
3252                                 return;
3253                         }
3254
3255                         if (item.Separator == true) {
3256                                 item.Height = SEPARATOR_HEIGHT;
3257                                 item.Width = SEPARATOR_MIN_WIDTH;
3258                                 return;
3259                         }
3260                         
3261                         if (item.MeasureEventDefined) {
3262                                 MeasureItemEventArgs mi = new MeasureItemEventArgs (dc, item.Index);
3263                                 item.PerformMeasureItem (mi);
3264                                 item.Height = mi.ItemHeight;
3265                                 item.Width = mi.ItemWidth;
3266                                 return;
3267                         } else {                
3268                                 SizeF size;
3269                                 size =  dc.MeasureString (item.Text, MenuFont, int.MaxValue, string_format_menu_text);
3270                                 item.Width = (int) size.Width;
3271                                 item.Height = (int) size.Height;
3272         
3273                                 if (!menuBar) {
3274                                         if (item.Shortcut != Shortcut.None && item.ShowShortcut) {
3275                                                 item.XTab = MenuCheckSize.Width + MENU_TAB_SPACE + (int) size.Width;
3276                                                 size =  dc.MeasureString (" " + item.GetShortCutText (), MenuFont);
3277                                                 item.Width += MENU_TAB_SPACE + (int) size.Width;
3278                                         }
3279         
3280                                         item.Width += 4 + (MenuCheckSize.Width * 2);
3281                                 } else {
3282                                         item.Width += MENU_BAR_ITEMS_SPACE;
3283                                         x += item.Width;
3284                                 }
3285         
3286                                 if (item.Height < MenuHeight)
3287                                         item.Height = MenuHeight;
3288                         }
3289                 }
3290                 
3291                 // Updates the menu rect and returns the height
3292                 public override int CalcMenuBarSize (Graphics dc, Menu menu, int width)
3293                 {
3294                         int x = 0;
3295                         int y = 0;
3296                         menu.Height = 0;
3297
3298                         foreach (MenuItem item in menu.MenuItems) {
3299
3300                                 CalcItemSize (dc, item, y, x, true);
3301
3302                                 if (x + item.Width > width) {
3303                                         item.X = 0;
3304                                         y += item.Height;
3305                                         item.Y = y;
3306                                         x = 0;
3307                                 }
3308
3309                                 x += item.Width;
3310                                 item.MenuBar = true;                            
3311
3312                                 if (y + item.Height > menu.Height)
3313                                         menu.Height = item.Height + y;
3314                         }
3315
3316                         menu.Width = width;                                             
3317                         return menu.Height;
3318                 }
3319
3320                 public override void CalcPopupMenuSize (Graphics dc, Menu menu)
3321                 {
3322                         int x = 3;
3323                         int start = 0;
3324                         int i, n, y, max;
3325
3326                         menu.Height = 0;
3327
3328                         while (start < menu.MenuItems.Count) {
3329                                 y = 3;
3330                                 max = 0;
3331                                 for (i = start; i < menu.MenuItems.Count; i++) {
3332                                         MenuItem item = menu.MenuItems [i];
3333
3334                                         if ((i != start) && (item.Break || item.BarBreak))
3335                                                 break;
3336
3337                                         CalcItemSize (dc, item, y, x, false);
3338                                         y += item.Height;
3339
3340                                         if (item.Width > max)
3341                                                 max = item.Width;
3342                                 }
3343
3344                                 // Replace the -1 by the menu width (separators)
3345                                 for (n = start; n < i; n++, start++)
3346                                         menu.MenuItems [n].Width = max;
3347
3348                                 if (y > menu.Height)
3349                                         menu.Height = y;
3350
3351                                 x+= max;
3352                         }
3353
3354                         menu.Width = x;
3355                         
3356                         //space for border
3357                         menu.Width += 2;
3358                         menu.Height += 2;
3359
3360                         menu.Width += SM_CXBORDER;
3361                 menu.Height += SM_CYBORDER;
3362                 }
3363                 
3364                 // Draws a menu bar in a window
3365                 public override void DrawMenuBar (Graphics dc, Menu menu, Rectangle rect)
3366                 {
3367                         if (menu.Height == 0)
3368                                 CalcMenuBarSize (dc, menu, rect.Width);
3369
3370                         bool keynav = (menu as MainMenu).tracker.hotkey_active;
3371                         HotkeyPrefix hp = MenuAccessKeysUnderlined || keynav ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
3372                         string_format_menu_menubar_text.HotkeyPrefix = hp;
3373                         string_format_menu_text.HotkeyPrefix = hp;
3374
3375                         rect.Height = menu.Height;
3376                         dc.FillRectangle (SystemBrushes.Menu, rect);
3377                         
3378                         for (int i = 0; i < menu.MenuItems.Count; i++) {
3379                                 MenuItem item = menu.MenuItems [i];
3380                                 Rectangle item_rect = item.bounds;
3381                                 item_rect.X += rect.X;
3382                                 item_rect.Y += rect.Y;
3383                                 item.MenuHeight = menu.Height;
3384                                 item.PerformDrawItem (new DrawItemEventArgs (dc, MenuFont, item_rect, i, item.Status)); 
3385                         }       
3386                 }               
3387                 
3388                 protected Bitmap CreateGlyphBitmap (Size size, MenuGlyph glyph, Color color)
3389                 {
3390                         Color bg_color;
3391                         if (color.R == 0 && color.G == 0 && color.B == 0)
3392                                 bg_color = Color.White;
3393                         else
3394                                 bg_color = Color.Black;
3395                         
3396                         Bitmap  bmp = new Bitmap (size.Width, size.Height);
3397                         Graphics gr = Graphics.FromImage (bmp);
3398                         Rectangle rect = new Rectangle (Point.Empty, size);
3399                         gr.FillRectangle (ResPool.GetSolidBrush (bg_color), rect);
3400                         CPDrawMenuGlyph (gr, rect, glyph, color, Color.Empty);
3401                         bmp.MakeTransparent (bg_color);
3402                         gr.Dispose ();
3403                         
3404                         return bmp;
3405                 }
3406
3407                 public override void DrawMenuItem (MenuItem item, DrawItemEventArgs e)
3408                 {
3409                         StringFormat string_format;
3410                         Rectangle rect_text = e.Bounds;
3411                         
3412                         if (item.Visible == false)
3413                                 return;
3414
3415                         if (item.MenuBar)
3416                                 string_format = string_format_menu_menubar_text;
3417                         else
3418                                 string_format = string_format_menu_text;
3419
3420                         if (item.Separator == true) {
3421                                 int liney = e.Bounds.Y + (e.Bounds.Height / 2);
3422                                 
3423                                 e.Graphics.DrawLine (SystemPens.ControlDark,
3424                                         e.Bounds.X, liney, e.Bounds.X + e.Bounds.Width, liney);
3425
3426                                 e.Graphics.DrawLine (SystemPens.ControlLight,
3427                                         e.Bounds.X, liney + 1, e.Bounds.X + e.Bounds.Width, liney + 1);
3428
3429                                 return;
3430                         }
3431
3432                         if (!item.MenuBar)
3433                                 rect_text.X += MenuCheckSize.Width;
3434
3435                         if (item.BarBreak) { /* Draw vertical break bar*/
3436                                 Rectangle rect = e.Bounds;
3437                                 rect.Y++;
3438                                 rect.Width = 3;
3439                                 rect.Height = item.MenuHeight - 6;
3440
3441                                 e.Graphics.DrawLine (SystemPens.ControlDark,
3442                                         rect.X, rect.Y , rect.X, rect.Y + rect.Height);
3443
3444                                 e.Graphics.DrawLine (SystemPens.ControlLight,
3445                                         rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
3446                         }                       
3447                         
3448                         Color color_text;
3449                         Color color_back;
3450                         Brush brush_text = null;
3451                         Brush brush_back = null;
3452                         
3453                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected && !item.MenuBar) {
3454                                 color_text = ColorHighlightText;
3455                                 color_back = ColorHighlight;
3456                                 brush_text = SystemBrushes.HighlightText;
3457                                 brush_back = SystemBrushes.Highlight;
3458                         } else {
3459                                 color_text = ColorMenuText;
3460                                 color_back = ColorMenu;
3461                                 brush_text = ResPool.GetSolidBrush (ColorMenuText);
3462                                 brush_back = SystemBrushes.Menu;
3463                         }
3464
3465                         /* Draw background */
3466                         if (!item.MenuBar)
3467                                 e.Graphics.FillRectangle (brush_back, e.Bounds);
3468                         
3469                         if (item.Enabled) {
3470                                 e.Graphics.DrawString (item.Text, e.Font,
3471                                         brush_text,
3472                                         rect_text, string_format);
3473                                 
3474                                 if (item.MenuBar) {
3475                                         Border3DStyle border_style = Border3DStyle.Adjust;
3476                                         if ((item.Status & DrawItemState.HotLight) != 0)
3477                                                 border_style = Border3DStyle.RaisedInner;
3478                                         else if ((item.Status & DrawItemState.Selected) != 0)
3479                                                 border_style = Border3DStyle.SunkenOuter;
3480                                         
3481                                         if (border_style != Border3DStyle.Adjust)
3482                                                 CPDrawBorder3D(e.Graphics, e.Bounds, border_style,  Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, ColorMenu);
3483                                 }
3484                         } else {
3485                                 if ((item.Status & DrawItemState.Selected) != DrawItemState.Selected) {
3486                                         e.Graphics.DrawString (item.Text, e.Font, Brushes.White, 
3487                                                                new RectangleF(rect_text.X + 1, rect_text.Y + 1, rect_text.Width, rect_text.Height),
3488                                                                string_format);
3489
3490                                 }
3491                                 
3492                                 e.Graphics.DrawString (item.Text, e.Font, ResPool.GetSolidBrush(ColorGrayText), rect_text, string_format);
3493                         }
3494
3495                         if (!item.MenuBar && item.Shortcut != Shortcut.None && item.ShowShortcut) {
3496                                 string str = item.GetShortCutText ();
3497                                 Rectangle rect = rect_text;
3498                                 rect.X = item.XTab;
3499                                 rect.Width -= item.XTab;
3500
3501                                 if (item.Enabled) {
3502                                         e.Graphics.DrawString (str, e.Font, brush_text, rect, string_format_menu_shortcut);
3503                                 } else {
3504                                         if ((item.Status & DrawItemState.Selected) != DrawItemState.Selected) {
3505                                                 e.Graphics.DrawString (str, e.Font, Brushes.White, 
3506                                                                        new RectangleF(rect.X + 1, rect.Y + 1, rect.Width, rect_text.Height),
3507                                                                        string_format_menu_shortcut);
3508
3509                                         }
3510                                         e.Graphics.DrawString (str, e.Font, ResPool.GetSolidBrush(ColorGrayText), rect, string_format_menu_shortcut);
3511                                 }
3512                         }
3513
3514                         /* Draw arrow */
3515                         if (item.MenuBar == false && (item.IsPopup || item.MdiList)) {
3516
3517                                 int cx = MenuCheckSize.Width;
3518                                 int cy = MenuCheckSize.Height;
3519                                 Bitmap  bmp = CreateGlyphBitmap (new Size (cx, cy), MenuGlyph.Arrow, color_text);
3520                                 
3521                                 if (item.Enabled) {
3522                                         e.Graphics.DrawImage (bmp, e.Bounds.X + e.Bounds.Width - cx,
3523                                                 e.Bounds.Y + ((e.Bounds.Height - cy) /2));
3524                                 } else {
3525                                         ControlPaint.DrawImageDisabled (e.Graphics, bmp, e.Bounds.X + e.Bounds.Width - cx,
3526                                                 e.Bounds.Y + ((e.Bounds.Height - cy) /2),  color_back);
3527                                 }
3528  
3529                                 bmp.Dispose ();
3530                         }
3531
3532                         /* Draw checked or radio */
3533                         if (item.MenuBar == false && item.Checked) {
3534
3535                                 Rectangle area = e.Bounds;
3536                                 int cx = MenuCheckSize.Width;
3537                                 int cy = MenuCheckSize.Height;
3538                                 Bitmap  bmp = CreateGlyphBitmap (new Size (cx, cy), item.RadioCheck ? MenuGlyph.Bullet : MenuGlyph.Checkmark, color_text);
3539
3540                                 e.Graphics.DrawImage (bmp, area.X, e.Bounds.Y + ((e.Bounds.Height - cy) / 2));
3541
3542                                 bmp.Dispose ();
3543                         }                       
3544                 }               
3545                         
3546                 public override void DrawPopupMenu (Graphics dc, Menu menu, Rectangle cliparea, Rectangle rect)
3547                 {
3548                         // Fill rectangle area
3549                         dc.FillRectangle (SystemBrushes.Menu, cliparea);
3550                         
3551                         // Draw menu borders
3552                         CPDrawBorder3D (dc, rect, Border3DStyle.Raised, all_sides);
3553                         
3554                         // Draw menu items
3555                         for (int i = 0; i < menu.MenuItems.Count; i++) {
3556                                 if (cliparea.IntersectsWith (menu.MenuItems [i].bounds)) {
3557                                         MenuItem item = menu.MenuItems [i];
3558                                         item.MenuHeight = menu.Height;
3559                                         item.PerformDrawItem (new DrawItemEventArgs (dc, MenuFont, item.bounds, i, item.Status));
3560                                 }
3561                         }
3562                 }
3563                 
3564                 #endregion // Menus
3565
3566                 #region MonthCalendar
3567
3568                 // draw the month calendar
3569                 public override void DrawMonthCalendar(Graphics dc, Rectangle clip_rectangle, MonthCalendar mc) 
3570                 {
3571                         Rectangle client_rectangle = mc.ClientRectangle;
3572                         Size month_size = mc.SingleMonthSize;
3573                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
3574                         Size calendar_spacing = (Size)((object)mc.calendar_spacing);
3575                         Size date_cell_size = (Size)((object)mc.date_cell_size);
3576                         
3577                         // draw the singlecalendars
3578                         int x_offset = 1;
3579                         int y_offset = 1;
3580                         // adjust for the position of the specific month
3581                         for (int i=0; i < mc.CalendarDimensions.Height; i++) 
3582                         {
3583                                 if (i > 0) 
3584                                 {
3585                                         y_offset += month_size.Height + calendar_spacing.Height;
3586                                 }
3587                                 // now adjust for x position    
3588                                 for (int j=0; j < mc.CalendarDimensions.Width; j++) 
3589                                 {
3590                                         if (j > 0) 
3591                                         {
3592                                                 x_offset += month_size.Width + calendar_spacing.Width;
3593                                         } 
3594                                         else 
3595                                         {
3596                                                 x_offset = 1;
3597                                         }
3598
3599                                         Rectangle month_rect = new Rectangle (x_offset, y_offset, month_size.Width, month_size.Height);
3600                                         if (month_rect.IntersectsWith (clip_rectangle)) {
3601                                                 DrawSingleMonth (
3602                                                         dc,
3603                                                         clip_rectangle,
3604                                                         month_rect,
3605                                                         mc,
3606                                                         i,
3607                                                         j);
3608                                         }
3609                                 }
3610                         }
3611                         
3612                         Rectangle bottom_rect = new Rectangle (
3613                                                 client_rectangle.X,
3614                                                 Math.Max(client_rectangle.Bottom - date_cell_size.Height - 3, 0),
3615                                                 client_rectangle.Width,
3616                                                 date_cell_size.Height + 2);
3617                         // draw the today date if it's set
3618                         if (mc.ShowToday && bottom_rect.IntersectsWith (clip_rectangle)) 
3619                         {
3620                                 dc.FillRectangle (GetControlBackBrush (mc.BackColor), bottom_rect);
3621                                 if (mc.ShowToday) {
3622                                         int today_offset = 5;
3623                                         if (mc.ShowTodayCircle) 
3624                                         {
3625                                                 Rectangle today_circle_rect = new Rectangle (
3626                                                         client_rectangle.X + 5,
3627                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height - 2, 0),
3628                                                         date_cell_size.Width,
3629                                                         date_cell_size.Height);
3630                                                         DrawTodayCircle (dc, today_circle_rect);
3631                                                 today_offset += date_cell_size.Width + 5;
3632                                         }
3633                                         // draw today's date
3634                                         StringFormat text_format = new StringFormat();
3635                                         text_format.LineAlignment = StringAlignment.Center;
3636                                         text_format.Alignment = StringAlignment.Near;
3637                                         Rectangle today_rect = new Rectangle (
3638                                                         today_offset + client_rectangle.X,
3639                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height, 0),
3640                                                         Math.Max(client_rectangle.Width - today_offset, 0),
3641                                                         date_cell_size.Height);
3642                                         dc.DrawString ("Today: " + DateTime.Now.ToShortDateString(), mc.bold_font, GetControlForeBrush (mc.ForeColor), today_rect, text_format);
3643                                         text_format.Dispose ();
3644                                 }                               
3645                         }
3646                         
3647                         Brush border_brush;
3648                         
3649                         if (mc.owner == null)
3650                                 border_brush = GetControlBackBrush (mc.BackColor);
3651                         else
3652                                 border_brush = SystemBrushes.ControlDarkDark;
3653                                 
3654                         // finally paint the borders of the calendars as required
3655                         for (int i = 0; i <= mc.CalendarDimensions.Width; i++) {
3656                                 if (i == 0 && clip_rectangle.X == client_rectangle.X) {
3657                                         dc.FillRectangle (border_brush, client_rectangle.X, client_rectangle.Y, 1, client_rectangle.Height);
3658                                 } else if (i == mc.CalendarDimensions.Width && clip_rectangle.Right == client_rectangle.Right) {
3659                                         dc.FillRectangle (border_brush, client_rectangle.Right - 1, client_rectangle.Y, 1, client_rectangle.Height);
3660                                 } else { 
3661                                         Rectangle rect = new Rectangle (
3662                                                 client_rectangle.X + (month_size.Width*i) + (calendar_spacing.Width * (i-1)) + 1,
3663                                                 client_rectangle.Y,
3664                                                 calendar_spacing.Width,
3665                                                 client_rectangle.Height);
3666                                         if (i < mc.CalendarDimensions.Width && i > 0 && clip_rectangle.IntersectsWith (rect)) {
3667                                                 dc.FillRectangle (border_brush, rect);
3668                                         }
3669                                 }
3670                         }
3671                         for (int i = 0; i <= mc.CalendarDimensions.Height; i++) {
3672                                 if (i == 0 && clip_rectangle.Y == client_rectangle.Y) {
3673                                         dc.FillRectangle (border_brush, client_rectangle.X, client_rectangle.Y, client_rectangle.Width, 1);
3674                                 } else if (i == mc.CalendarDimensions.Height && clip_rectangle.Bottom == client_rectangle.Bottom) {
3675                                         dc.FillRectangle (border_brush, client_rectangle.X, client_rectangle.Bottom - 1, client_rectangle.Width, 1);
3676                                 } else { 
3677                                         Rectangle rect = new Rectangle (
3678                                                 client_rectangle.X,
3679                                                 client_rectangle.Y + (month_size.Height*i) + (calendar_spacing.Height*(i-1)) + 1,
3680                                                 client_rectangle.Width,
3681                                                 calendar_spacing.Height);
3682                                         if (i < mc.CalendarDimensions.Height && i > 0 && clip_rectangle.IntersectsWith (rect)) {
3683                                                 dc.FillRectangle (border_brush, rect);
3684                                         }
3685                                 }
3686                         }
3687                         
3688                         // draw the drop down border if need
3689                         if (mc.owner != null) {
3690                                 Rectangle bounds = mc.ClientRectangle;
3691                                 if (clip_rectangle.Contains (mc.Location)) {
3692                                         // find out if top or left line to draw
3693                                         if(clip_rectangle.Contains (new Point (bounds.Left, bounds.Bottom))) {
3694                                         
3695                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Y, bounds.X, bounds.Bottom-1);
3696                                         }
3697                                         if(clip_rectangle.Contains (new Point (bounds.Right, bounds.Y))) {
3698                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Y, bounds.Right-1, bounds.Y);
3699                                         }
3700                                 }
3701                                 if (clip_rectangle.Contains (new Point(bounds.Right, bounds.Bottom))) {
3702                                         // find out if bottom or right line to draw
3703                                         if(clip_rectangle.Contains (new Point (bounds.Left, bounds.Bottom))) {
3704                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1);
3705                                         }
3706                                         if(clip_rectangle.Contains (new Point (bounds.Right, bounds.Y))) {
3707                                                 dc.DrawLine (SystemPens.ControlText, bounds.Right-1, bounds.Y, bounds.Right-1, bounds.Bottom-1);
3708                                         }
3709                                 }
3710                         }
3711                 }
3712
3713                 // darws a single part of the month calendar (with one month)
3714                 private void DrawSingleMonth(Graphics dc, Rectangle clip_rectangle, Rectangle rectangle, MonthCalendar mc, int row, int col) 
3715                 {
3716                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
3717                         Size title_size = (Size)((object)mc.title_size);
3718                         Size date_cell_size = (Size)((object)mc.date_cell_size);
3719                         DateTime current_month = (DateTime)((object)mc.current_month);
3720                         DateTime sunday = new DateTime(2006, 10, 1);
3721                         
3722                         // draw the title back ground
3723                         DateTime this_month = current_month.AddMonths (row*mc.CalendarDimensions.Width+col);
3724                         Rectangle title_rect = new Rectangle(rectangle.X, rectangle.Y, title_size.Width, title_size.Height);
3725                         if (title_rect.IntersectsWith (clip_rectangle)) {
3726                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), title_rect);
3727                                 // draw the title                               
3728                                 string title_text = this_month.ToString ("MMMM yyyy");
3729                                 dc.DrawString (title_text, mc.bold_font, ResPool.GetSolidBrush (mc.TitleForeColor), title_rect, mc.centered_format);
3730
3731                                 if (mc.ShowYearUpDown) {
3732                                         Rectangle year_rect;
3733                                         Rectangle upRect, downRect;
3734                                         ButtonState upState, downState;
3735                                         
3736                                         mc.GetYearNameRectangles (title_rect, row * mc.CalendarDimensions.Width + col, out year_rect, out upRect, out downRect);
3737                                         dc.FillRectangle (ResPool.GetSolidBrush (SystemColors.Control), year_rect);
3738                                         dc.DrawString (this_month.ToString ("yyyy"), mc.bold_font, ResPool.GetSolidBrush (Color.Black), year_rect, mc.centered_format);
3739                                         
3740                                         upState = mc.IsYearGoingUp ? ButtonState.Pushed : ButtonState.Normal;
3741                                         downState = mc.IsYearGoingDown ? ButtonState.Pushed : ButtonState.Normal;
3742
3743                                         ControlPaint.DrawScrollButton (dc, upRect, ScrollButton.Up, upState);
3744                                         ControlPaint.DrawScrollButton (dc, downRect, ScrollButton.Down, downState);
3745                                 }
3746
3747                                 // draw previous and next buttons if it's time
3748                                 if (row == 0 && col == 0) 
3749                                 {
3750                                         // draw previous button
3751                                         DrawMonthCalendarButton (
3752                                                 dc,
3753                                                 rectangle,
3754                                                 mc,
3755                                                 title_size,
3756                                                 mc.button_x_offset,
3757                                                 (System.Drawing.Size)((object)mc.button_size),
3758                                                 true);
3759                                 }
3760                                 if (row == 0 && col == mc.CalendarDimensions.Width-1) 
3761                                 {
3762                                         // draw next button
3763                                         DrawMonthCalendarButton (
3764                                                 dc,
3765                                                 rectangle,
3766                                                 mc,
3767                                                 title_size,
3768                                                 mc.button_x_offset,
3769                                                 (System.Drawing.Size)((object)mc.button_size),
3770                                                 false);
3771                                 }
3772                         }
3773                         
3774                         // set the week offset and draw week nums if needed
3775                         int col_offset = (mc.ShowWeekNumbers) ? 1 : 0;
3776                         Rectangle day_name_rect = new Rectangle(
3777                                 rectangle.X,
3778                                 rectangle.Y + title_size.Height,
3779                                 (7 + col_offset) * date_cell_size.Width,
3780                                 date_cell_size.Height);
3781                         if (day_name_rect.IntersectsWith (clip_rectangle)) {
3782                                 dc.FillRectangle (GetControlBackBrush (mc.BackColor), day_name_rect);
3783                                 // draw the day names 
3784                                 DayOfWeek first_day_of_week = mc.GetDayOfWeek(mc.FirstDayOfWeek);
3785                                 for (int i=0; i < 7; i++) 
3786                                 {
3787                                         int position = i - (int) first_day_of_week;
3788                                         if (position < 0) 
3789                                         {
3790                                                 position = 7 + position;
3791                                         }
3792                                         // draw it
3793                                         Rectangle day_rect = new Rectangle(
3794                                                 day_name_rect.X + ((i + col_offset)* date_cell_size.Width),
3795                                                 day_name_rect.Y,
3796                                                 date_cell_size.Width,
3797                                                 date_cell_size.Height);
3798                                         dc.DrawString (sunday.AddDays (i + (int) first_day_of_week).ToString ("ddd"), mc.Font, ResPool.GetSolidBrush (mc.TitleBackColor), day_rect, mc.centered_format);
3799                                 }
3800                                 
3801                                 // draw the vertical divider
3802                                 int vert_divider_y = Math.Max(title_size.Height+ date_cell_size.Height-1, 0);
3803                                 dc.DrawLine (
3804                                         ResPool.GetPen (mc.ForeColor),
3805                                         rectangle.X + (col_offset * date_cell_size.Width) + mc.divider_line_offset,
3806                                         rectangle.Y + vert_divider_y,
3807                                         rectangle.Right - mc.divider_line_offset,
3808                                         rectangle.Y + vert_divider_y);
3809                         }
3810
3811
3812                         // draw the actual date items in the grid (including the week numbers)
3813                         Rectangle date_rect = new Rectangle (
3814                                 rectangle.X,
3815                                 rectangle.Y + title_size.Height + date_cell_size.Height,
3816                                 date_cell_size.Width,
3817                                 date_cell_size.Height);
3818                         int month_row_count = 0;
3819                         bool draw_week_num_divider = false;
3820                         DateTime current_date = mc.GetFirstDateInMonthGrid ( new DateTime (this_month.Year, this_month.Month, 1));
3821                         for (int i=0; i < 6; i++) 
3822                         {
3823                                 // establish if this row is in our clip_area
3824                                 Rectangle row_rect = new Rectangle (
3825                                         rectangle.X,
3826                                         rectangle.Y + title_size.Height + (date_cell_size.Height * (i+1)),
3827                                         date_cell_size.Width * 7,
3828                                         date_cell_size.Height);
3829                                 if (mc.ShowWeekNumbers) {
3830                                         row_rect.Width += date_cell_size.Width;
3831                                 }
3832                 
3833                                 bool draw_row = row_rect.IntersectsWith (clip_rectangle);
3834                                 if (draw_row) {
3835                                         dc.FillRectangle (GetControlBackBrush (mc.BackColor), row_rect);
3836                                 }
3837                                 // establish if this is a valid week to draw
3838                                 if (mc.IsValidWeekToDraw (this_month, current_date, row, col)) {
3839                                         month_row_count = i;
3840                                 }
3841                                 
3842                                 // draw the week number if required
3843                                 if (mc.ShowWeekNumbers && month_row_count == i) {
3844                                         if (!draw_week_num_divider) {
3845                                                 draw_week_num_divider = draw_row;
3846                                         }
3847                                         // get the week for this row
3848                                         int week = mc.GetWeekOfYear (current_date);     
3849
3850                                         if (draw_row) {
3851                                                 dc.DrawString (
3852                                                         week.ToString(),
3853                                                         mc.Font,
3854                                                         ResPool.GetSolidBrush (mc.TitleBackColor),
3855                                                         date_rect,
3856                                                         mc.centered_format);
3857                                         }
3858                                         date_rect.Offset(date_cell_size.Width, 0);
3859                                 }
3860                                                                 
3861                                 // only draw the days if we have to
3862                                 if(month_row_count == i) {
3863                                         for (int j=0; j < 7; j++) 
3864                                         {
3865                                                 if (draw_row) {
3866                                                         DrawMonthCalendarDate (
3867                                                                 dc,
3868                                                                 date_rect,
3869                                                                 mc,
3870                                                                 current_date,
3871                                                                 this_month,
3872                                                                 row,
3873                                                                 col);
3874                                                 }
3875
3876                                                 // move the day on
3877                                                 current_date = current_date.AddDays(1);
3878                                                 date_rect.Offset(date_cell_size.Width, 0);
3879                                         }
3880
3881                                         // shift the rectangle down one row
3882                                         int offset = (mc.ShowWeekNumbers) ? -8 : -7;
3883                                         date_rect.Offset(offset*date_cell_size.Width, date_cell_size.Height);
3884                                 }
3885                         }
3886
3887                         // month_row_count is zero based, so add one
3888                         month_row_count++;
3889
3890                         // draw week numbers if required
3891                         if (draw_week_num_divider) {
3892                                 col_offset = 1;
3893                                 dc.DrawLine (
3894                                         ResPool.GetPen (mc.ForeColor),
3895                                         rectangle.X + date_cell_size.Width - 1,
3896                                         rectangle.Y + title_size.Height + date_cell_size.Height + mc.divider_line_offset,
3897                                         rectangle.X + date_cell_size.Width - 1,
3898                                         rectangle.Y + title_size.Height + date_cell_size.Height + (month_row_count * date_cell_size.Height) - mc.divider_line_offset);
3899                         }
3900                 }
3901
3902                 // draws the pervious or next button
3903                 private void DrawMonthCalendarButton (Graphics dc, Rectangle rectangle, MonthCalendar mc, Size title_size, int x_offset, Size button_size, bool is_previous) 
3904                 {
3905                         const int arrow_width = 4;
3906                         const int arrow_height = 7;
3907
3908                         bool is_clicked = false;
3909                         Rectangle button_rect;
3910                         PointF arrow_center;
3911                         PointF [] arrow_path = new PointF [3];
3912                         
3913                         // prepare the button
3914                         if (is_previous) 
3915                         {
3916                                 is_clicked = mc.is_previous_clicked;
3917
3918                                 button_rect = new Rectangle (
3919                                         rectangle.X + 1 + x_offset,
3920                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
3921                                         Math.Max(button_size.Width - 1, 0),
3922                                         Math.Max(button_size.Height - 1, 0));
3923
3924                                 arrow_center = new PointF (button_rect.X + ((button_rect.Width + arrow_width) / 2.0f), 
3925                                                                                         rectangle.Y + ((button_rect.Height + arrow_height) / 2) + 1);
3926                                 if (is_clicked) {
3927                                         arrow_center.X += 1;
3928                                         arrow_center.Y += 1;
3929                                 }
3930
3931                                 arrow_path [0].X = arrow_center.X;
3932                                 arrow_path [0].Y = arrow_center.Y - arrow_height / 2.0f + 0.5f;
3933                                 arrow_path [1].X = arrow_center.X;
3934                                 arrow_path [1].Y = arrow_center.Y + arrow_height / 2.0f + 0.5f;
3935                                 arrow_path [2].X = arrow_center.X - arrow_width;
3936                                 arrow_path [2].Y = arrow_center.Y + 0.5f;
3937                         }
3938                         else
3939                         {
3940                                 is_clicked = mc.is_next_clicked;
3941
3942                                 button_rect = new Rectangle (
3943                                         rectangle.Right - 1 - x_offset - button_size.Width,
3944                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
3945                                         Math.Max(button_size.Width - 1, 0),
3946                                         Math.Max(button_size.Height - 1, 0));
3947
3948                                 arrow_center = new PointF (button_rect.X + ((button_rect.Width + arrow_width) / 2.0f), 
3949                                                                                         rectangle.Y + ((button_rect.Height + arrow_height) / 2) + 1);
3950                                 if (is_clicked) {
3951                                         arrow_center.X += 1;
3952                                         arrow_center.Y += 1;
3953                                 }
3954
3955                                 arrow_path [0].X = arrow_center.X - arrow_width;
3956                                 arrow_path [0].Y = arrow_center.Y - arrow_height / 2.0f + 0.5f;
3957                                 arrow_path [1].X = arrow_center.X - arrow_width;
3958                                 arrow_path [1].Y = arrow_center.Y + arrow_height / 2.0f + 0.5f;
3959                                 arrow_path [2].X = arrow_center.X;
3960                                 arrow_path [2].Y = arrow_center.Y + 0.5f;
3961                         }
3962
3963                         // fill the background
3964                         dc.FillRectangle (SystemBrushes.Control, button_rect);
3965                         // draw the border
3966                         if (is_clicked) {
3967                                 dc.DrawRectangle (SystemPens.ControlDark, button_rect);
3968                         }
3969                         else {
3970                                 CPDrawBorder3D (dc, button_rect, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
3971                         }
3972                         // draw the arrow
3973                         dc.FillPolygon (SystemBrushes.ControlText, arrow_path);                 
3974                         //dc.FillPolygon (SystemBrushes.ControlText, arrow_path, FillMode.Winding);
3975                 }
3976                 
3977
3978                 // draws one day in the calendar grid
3979                 private void DrawMonthCalendarDate (Graphics dc, Rectangle rectangle, MonthCalendar mc, DateTime date, DateTime month, int row, int col) {
3980                         Color date_color = mc.ForeColor;
3981                         Rectangle interior = new Rectangle (rectangle.X, rectangle.Y, Math.Max(rectangle.Width - 1, 0), Math.Max(rectangle.Height - 1, 0));
3982
3983                         // find out if we are the lead of the first calendar or the trail of the last calendar                                          
3984                         if (date.Year != month.Year || date.Month != month.Month) {
3985                                 DateTime check_date = month.AddMonths (-1);
3986                                 // check if it's the month before 
3987                                 if (check_date.Year == date.Year && check_date.Month == date.Month && row == 0 && col == 0) {
3988                                         date_color = mc.TrailingForeColor;
3989                                 } else {
3990                                         // check if it's the month after
3991                                         check_date = month.AddMonths (1);
3992                                         if (check_date.Year == date.Year && check_date.Month == date.Month && row == mc.CalendarDimensions.Height-1 && col == mc.CalendarDimensions.Width-1) {
3993                                                 date_color = mc.TrailingForeColor;
3994                                         } else {
3995                                                 return;
3996                                         }
3997                                 }
3998                         } else {
3999                                 date_color = mc.ForeColor;
4000                         }
4001
4002                         const int inflate = -1;
4003
4004                         if (date == mc.SelectionStart.Date && date == mc.SelectionEnd.Date) {
4005                                 // see if the date is in the start of selection
4006                                 date_color = mc.BackColor;
4007                                 // draw the left hand of the back ground
4008                                 Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);                             
4009                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 0, 360);
4010                         } else if (date == mc.SelectionStart.Date) {
4011                                 // see if the date is in the start of selection
4012                                 date_color = mc.BackColor;
4013                                 // draw the left hand of the back ground
4014                                 Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);                             
4015                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 90, 180);
4016                                 // fill the other side as a straight rect
4017                                 if (date < mc.SelectionEnd.Date) 
4018                                 {
4019                                         // use rectangle instead of rectangle to go all the way to edge of rect
4020                                         selection_rect.X = (int) Math.Floor((double)(rectangle.X + rectangle.Width / 2));
4021                                         selection_rect.Width = Math.Max(rectangle.Right - selection_rect.X, 0);
4022                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
4023                                 }
4024                         } else if (date == mc.SelectionEnd.Date) {
4025                                 // see if it is the end of selection
4026                                 date_color = mc.BackColor;
4027                                 // draw the left hand of the back ground
4028                                 Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);
4029                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 270, 180);
4030                                 // fill the other side as a straight rect
4031                                 if (date > mc.SelectionStart.Date) {
4032                                         selection_rect.X = rectangle.X;
4033                                         selection_rect.Width = rectangle.Width - (rectangle.Width / 2);
4034                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
4035                                 }
4036                         } else if (date > mc.SelectionStart.Date && date < mc.SelectionEnd.Date) {
4037                                 // now see if it's in the middle
4038                                 date_color = mc.BackColor;
4039                                 // draw the left hand of the back ground
4040                                 Rectangle selection_rect = Rectangle.Inflate (rectangle, 0, inflate);
4041                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
4042                         }
4043
4044                         // establish if it's a bolded font
4045                         Font font = mc.IsBoldedDate (date) ? mc.bold_font : mc.Font;
4046
4047                         // just draw the date now
4048                         dc.DrawString (date.Day.ToString(), font, ResPool.GetSolidBrush (date_color), rectangle, mc.centered_format);
4049
4050                         // today circle if needed
4051                         if (mc.ShowTodayCircle && date == DateTime.Now.Date) {
4052                                 DrawTodayCircle (dc, interior);
4053                         }
4054
4055                         // draw the selection grid
4056                         if (mc.is_date_clicked && mc.clicked_date == date) {
4057                                 Pen pen = ResPool.GetDashPen (Color.Black, DashStyle.Dot);
4058                                 dc.DrawRectangle (pen, interior);
4059                         }
4060                 }
4061
4062                 private void DrawTodayCircle (Graphics dc, Rectangle rectangle) {
4063                         Color circle_color = Color.FromArgb (248, 0, 0);
4064                         // draw the left hand of the circle 
4065                         Rectangle lhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 4, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 5, 0));
4066                         Rectangle rhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 1, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 2, 0));
4067                         Point [] curve_points = new Point [3];
4068                         curve_points [0] = new Point (lhs_circle_rect.X, rhs_circle_rect.Y + rhs_circle_rect.Height/12);
4069                         curve_points [1] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/9, rhs_circle_rect.Y);
4070                         curve_points [2] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/2 + 1, rhs_circle_rect.Y);
4071
4072                         Pen pen = ResPool.GetSizedPen(circle_color, 2);
4073                         dc.DrawArc (pen, lhs_circle_rect, 90, 180);
4074                         dc.DrawArc (pen, rhs_circle_rect, 270, 180);                                    
4075                         dc.DrawCurve (pen, curve_points);
4076                         dc.DrawLine (ResPool.GetPen (circle_color), curve_points [2], new Point (curve_points [2].X, lhs_circle_rect.Y));
4077                 }
4078
4079                 #endregion      // MonthCalendar
4080
4081                 #region Panel
4082                 public override Size PanelDefaultSize {
4083                         get {
4084                                 return new Size (200, 100);
4085                         }
4086                 }
4087                 #endregion      // Panel
4088
4089                 #region PictureBox
4090                 public override void DrawPictureBox (Graphics dc, Rectangle clip, PictureBox pb) {
4091                         Rectangle client = pb.ClientRectangle;
4092
4093                         client = new Rectangle (client.Left + pb.Padding.Left, client.Top + pb.Padding.Top, client.Width - pb.Padding.Horizontal, client.Height - pb.Padding.Vertical);
4094
4095                         // FIXME - instead of drawing the whole picturebox every time
4096                         // intersect the clip rectangle with the drawn picture and only draw what's needed,
4097                         // Also, we only need a background fill where no image goes
4098                         if (pb.Image != null) {
4099                                 switch (pb.SizeMode) {
4100                                 case PictureBoxSizeMode.StretchImage:
4101                                         dc.DrawImage (pb.Image, client.Left, client.Top, client.Width, client.Height);
4102                                         break;
4103
4104                                 case PictureBoxSizeMode.CenterImage:
4105                                         dc.DrawImage (pb.Image, (client.Width / 2) - (pb.Image.Width / 2), (client.Height / 2) - (pb.Image.Height / 2));
4106                                         break;
4107
4108                                 case PictureBoxSizeMode.Zoom:
4109                                         Size image_size;
4110                                         
4111                                         if (((float)pb.Image.Width / (float)pb.Image.Height) >= ((float)client.Width / (float)client.Height))
4112                                                 image_size = new Size (client.Width, (pb.Image.Height * client.Width) / pb.Image.Width);
4113                                         else
4114                                                 image_size = new Size ((pb.Image.Width * client.Height) / pb.Image.Height, client.Height);
4115
4116                                         dc.DrawImage (pb.Image, (client.Width / 2) - (image_size.Width / 2), (client.Height / 2) - (image_size.Height / 2), image_size.Width, image_size.Height);
4117                                         break;
4118
4119                                 default:
4120                                         // Normal, AutoSize
4121                                         dc.DrawImage (pb.Image, client.Left, client.Top, pb.Image.Width, pb.Image.Height);
4122                                         break;
4123                                 }
4124
4125                                 return;
4126                         }
4127                 }
4128
4129                 public override Size PictureBoxDefaultSize {
4130                         get {
4131                                 return new Size (100, 50);
4132                         }
4133                 }
4134                 #endregion      // PictureBox
4135
4136                 #region PrintPreviewControl
4137                 public override int PrintPreviewControlPadding {
4138                         get { return 8; }
4139                 }
4140
4141                 public override Size PrintPreviewControlGetPageSize (PrintPreviewControl preview)
4142                 {
4143                         int page_width, page_height;
4144                         int padding = PrintPreviewControlPadding;
4145                         PreviewPageInfo[] pis = preview.page_infos;
4146
4147                         if (preview.AutoZoom) {
4148                                 int height_available = preview.ClientRectangle.Height - (preview.Rows) * padding - 2 * padding;
4149                                 int width_available = preview.ClientRectangle.Width - (preview.Columns - 1) * padding - 2 * padding;
4150
4151                                 float image_ratio = (float)pis[0].Image.Width / pis[0].Image.Height;
4152
4153                                 /* try to lay things out using the width to determine the size */
4154                                 page_width = width_available / preview.Columns;
4155                                 page_height = (int)(page_width / image_ratio);
4156
4157                                 /* does the height fit? */
4158                                 if (page_height * (preview.Rows + 1) > height_available) {
4159                                         /* no, lay things out via the height */
4160                                         page_height = height_available / (preview.Rows + 1);
4161                                         page_width = (int)(page_height * image_ratio);
4162                                 }
4163                         }
4164                         else {
4165                                 page_width = (int)(pis[0].Image.Width * preview.Zoom);
4166                                 page_height = (int)(pis[0].Image.Height * preview.Zoom);
4167                         }
4168
4169                         return new Size (page_width, page_height);
4170                 }
4171
4172                 public override void PrintPreviewControlPaint (PaintEventArgs pe, PrintPreviewControl preview, Size page_size)
4173                 {
4174                         int padding = 8;
4175                         PreviewPageInfo[] pis = preview.page_infos;
4176                         if (pis == null)
4177                                 return;
4178
4179                         int page_x, page_y;
4180
4181                         int width = page_size.Width * preview.Columns + padding * (preview.Columns - 1) + 2 * padding;
4182                         int height = page_size.Height * (preview.Rows + 1) + padding * preview.Rows + 2 * padding;
4183
4184                         Rectangle viewport = preview.ViewPort;
4185
4186                         pe.Graphics.Clip = new Region (viewport);
4187
4188                         /* center things if we can */
4189                         int off_x = viewport.Width / 2 - width / 2;
4190                         if (off_x < 0) off_x = 0;
4191                         int off_y = viewport.Height / 2 - height / 2;
4192                         if (off_y < 0) off_y = 0;
4193
4194                         page_y = off_y + padding - preview.vbar_value;
4195
4196                         if (preview.StartPage > 0) {
4197                                 int p = preview.StartPage - 1;
4198                                 for (int py = 0; py < preview.Rows + 1; py ++) {
4199                                         page_x = off_x + padding - preview.hbar_value;
4200                                         for (int px = 0; px < preview.Columns; px ++) {
4201                                                 if (p >= pis.Length)
4202                                                         continue;
4203                                                 Image image = preview.image_cache[p];
4204                                                 if (image == null)
4205                                                         image = pis[p].Image;
4206                                                 Rectangle dest = new Rectangle (new Point (page_x, page_y), page_size);
4207
4208                                                 pe.Graphics.DrawImage (image, dest, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
4209
4210                                                 page_x += padding + page_size.Width;
4211                                                 p++;
4212                                         }
4213                                         page_y += padding + page_size.Height;
4214                                 }
4215                         }
4216                 }
4217                 #endregion      // PrintPreviewControl
4218
4219                 #region ProgressBar
4220                 public override void DrawProgressBar (Graphics dc, Rectangle clip_rect, ProgressBar ctrl) 
4221                 {
4222                         Rectangle client_area = ctrl.client_area;
4223                         
4224                         /* Draw border */
4225                         CPDrawBorder3D (dc, ctrl.ClientRectangle, Border3DStyle.SunkenOuter, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom & ~Border3DSide.Middle, ColorControl);
4226                         
4227                         /* Draw Blocks */
4228                         int draw_mode = 0;
4229                         int max_blocks = int.MaxValue;
4230                         int start_pixel = client_area.X;
4231                         draw_mode = (int) ctrl.Style;
4232
4233                         switch (draw_mode) {
4234                         case 1: { // Continuous
4235                                 int pixels_to_draw;
4236                                 pixels_to_draw = (int)(client_area.Width * ((double)(ctrl.Value - ctrl.Minimum) / (double)(Math.Max(ctrl.Maximum - ctrl.Minimum, 1))));
4237                                 dc.FillRectangle (ResPool.GetSolidBrush (ctrl.ForeColor), new Rectangle (client_area.X, client_area.Y, pixels_to_draw, client_area.Height));
4238                                 break;
4239                         }
4240                         case 2: // Marquee
4241                                 if (XplatUI.ThemesEnabled) {
4242                                         int ms_diff = (int) (DateTime.Now - ctrl.start).TotalMilliseconds;
4243                                         double percent_done = (double) ms_diff / ProgressBarMarqueeSpeedScaling 
4244                                                 % (double)ctrl.MarqueeAnimationSpeed / (double)ctrl.MarqueeAnimationSpeed;
4245                                         max_blocks = 5;
4246                                         start_pixel = client_area.X + (int) (client_area.Width * percent_done);
4247                                 }
4248                                 
4249                                 goto case 0;
4250                         case 0:
4251                         default:  // Blocks
4252                                 Rectangle block_rect;
4253                                 int space_betweenblocks = ProgressBarChunkSpacing;
4254                                 int block_width;
4255                                 int increment;
4256                                 int barpos_pixels;
4257                                 int block_count = 0;
4258                                 
4259                                 block_width = ProgressBarGetChunkSize (client_area.Height);
4260                                 block_width = Math.Max (block_width, 0); // block_width is used to break out the loop below, it must be >= 0!
4261                                 barpos_pixels = (int)(((double)(ctrl.Value - ctrl.Minimum) * client_area.Width) / (Math.Max (ctrl.Maximum - ctrl.Minimum, 1)));
4262                                 increment = block_width + space_betweenblocks;
4263                                 
4264                                 block_rect = new Rectangle (start_pixel, client_area.Y, block_width, client_area.Height);
4265                                 while (true) {
4266                                         if (max_blocks != int.MaxValue) {
4267                                                 if (block_count >= max_blocks)
4268                                                         break;
4269                                                 if (block_rect.X > client_area.Width)
4270                                                         block_rect.X -= client_area.Width;
4271                                         } else {
4272                                                 if ((block_rect.X - client_area.X) >= barpos_pixels)
4273                                                         break;
4274                                         }
4275                                         
4276                                         if (clip_rect.IntersectsWith (block_rect) == true) {                            
4277                                                 dc.FillRectangle (ResPool.GetSolidBrush (ctrl.ForeColor), block_rect);
4278                                         }                               
4279                                         
4280                                         block_rect.X  += increment;
4281                                         block_count++;
4282                                 }
4283                                 break;
4284                         
4285                         }
4286                 }
4287                 
4288                 public const int ProgressBarChunkSpacing = 2;
4289
4290                 public static int ProgressBarGetChunkSize ()
4291                 {
4292                         return ProgressBarGetChunkSize (ProgressBarDefaultHeight);
4293                 }
4294                 
4295                 static int ProgressBarGetChunkSize (int progressBarClientAreaHeight)
4296                 {
4297                         int size = (progressBarClientAreaHeight * 2) / 3;
4298                         return size;
4299                 }
4300
4301                 const int ProgressBarDefaultHeight = 23;
4302
4303                 public override Size ProgressBarDefaultSize {
4304                         get {
4305                                 return new Size (100, ProgressBarDefaultHeight);
4306                         }
4307                 }
4308
4309                 public const double ProgressBarMarqueeSpeedScaling = 15;
4310
4311                 #endregion      // ProgressBar
4312
4313                 #region RadioButton
4314                 public override void DrawRadioButton (Graphics dc, Rectangle clip_rectangle, RadioButton radio_button) {
4315                         StringFormat    text_format;
4316                         Rectangle       client_rectangle;
4317                         Rectangle       text_rectangle;
4318                         Rectangle       radiobutton_rectangle;
4319                         int             radiobutton_size = 13;
4320                         int     radiobutton_space = 4;
4321
4322                         client_rectangle = radio_button.ClientRectangle;
4323                         text_rectangle = client_rectangle;
4324                         radiobutton_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, radiobutton_size, radiobutton_size);
4325
4326                         text_format = new StringFormat();
4327                         text_format.Alignment = StringAlignment.Near;
4328                         text_format.LineAlignment = StringAlignment.Center;
4329                         text_format.HotkeyPrefix = HotkeyPrefix.Show;
4330
4331                         /* Calculate the position of text and checkbox rectangle */
4332                         if (radio_button.appearance!=Appearance.Button) {
4333                                 switch(radio_button.radiobutton_alignment) {
4334                                 case ContentAlignment.BottomCenter: {
4335                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
4336                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
4337                                         text_rectangle.X=client_rectangle.X;
4338                                         text_rectangle.Width=client_rectangle.Width;
4339                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
4340                                         break;
4341                                 }
4342
4343                                 case ContentAlignment.BottomLeft: {
4344                                         radiobutton_rectangle.X=client_rectangle.Left;
4345                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
4346                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
4347                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;                                 
4348                                         break;
4349                                 }
4350
4351                                 case ContentAlignment.BottomRight: {
4352                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
4353                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
4354                                         text_rectangle.X=client_rectangle.X;
4355                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
4356                                         break;
4357                                 }
4358
4359                                 case ContentAlignment.MiddleCenter: {
4360                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
4361                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
4362                                         text_rectangle.X=client_rectangle.X;
4363                                         text_rectangle.Width=client_rectangle.Width;
4364                                         break;
4365                                 }
4366
4367                                 default:
4368                                 case ContentAlignment.MiddleLeft: {
4369                                         radiobutton_rectangle.X=client_rectangle.Left;
4370                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
4371                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
4372                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
4373                                         break;
4374                                 }
4375
4376                                 case ContentAlignment.MiddleRight: {
4377                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
4378                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
4379                                         text_rectangle.X=client_rectangle.X;
4380                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
4381                                         break;
4382                                 }
4383
4384                                 case ContentAlignment.TopCenter: {
4385                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
4386                                         radiobutton_rectangle.Y=client_rectangle.Top;
4387                                         text_rectangle.X=client_rectangle.X;
4388                                         text_rectangle.Y=radiobutton_size+radiobutton_space;
4389                                         text_rectangle.Width=client_rectangle.Width;
4390                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
4391                                         break;
4392                                 }
4393
4394                                 case ContentAlignment.TopLeft: {
4395                                         radiobutton_rectangle.X=client_rectangle.Left;
4396                                         radiobutton_rectangle.Y=client_rectangle.Top;
4397                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
4398                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
4399                                         break;
4400                                 }
4401
4402                                 case ContentAlignment.TopRight: {
4403                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
4404                                         radiobutton_rectangle.Y=client_rectangle.Top;
4405                                         text_rectangle.X=client_rectangle.X;
4406                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
4407                                         break;
4408                                 }
4409                                 }
4410                         } else {
4411                                 text_rectangle.X=client_rectangle.X;
4412                                 text_rectangle.Width=client_rectangle.Width;
4413                         }
4414                         
4415                         /* Set the horizontal alignment of our text */
4416                         switch(radio_button.text_alignment) {
4417                                 case ContentAlignment.BottomLeft:
4418                                 case ContentAlignment.MiddleLeft:
4419                                 case ContentAlignment.TopLeft: {
4420                                         text_format.Alignment=StringAlignment.Near;
4421                                         break;
4422                                 }
4423
4424                                 case ContentAlignment.BottomCenter:
4425                                 case ContentAlignment.MiddleCenter:
4426                                 case ContentAlignment.TopCenter: {
4427                                         text_format.Alignment=StringAlignment.Center;
4428                                         break;
4429                                 }
4430
4431                                 case ContentAlignment.BottomRight:
4432                                 case ContentAlignment.MiddleRight:
4433                                 case ContentAlignment.TopRight: {
4434                                         text_format.Alignment=StringAlignment.Far;
4435                                         break;
4436                                 }
4437                         }
4438
4439                         /* Set the vertical alignment of our text */
4440                         switch(radio_button.text_alignment) {
4441                                 case ContentAlignment.TopLeft: 
4442                                 case ContentAlignment.TopCenter: 
4443                                 case ContentAlignment.TopRight: {
4444                                         text_format.LineAlignment=StringAlignment.Near;
4445                                         break;
4446                                 }
4447
4448                                 case ContentAlignment.BottomLeft:
4449                                 case ContentAlignment.BottomCenter:
4450                                 case ContentAlignment.BottomRight: {
4451                                         text_format.LineAlignment=StringAlignment.Far;
4452                                         break;
4453                                 }
4454
4455                                 case ContentAlignment.MiddleLeft:
4456                                 case ContentAlignment.MiddleCenter:
4457                                 case ContentAlignment.MiddleRight: {
4458                                         text_format.LineAlignment=StringAlignment.Center;
4459                                         break;
4460                                 }
4461                         }
4462
4463                         ButtonState state = ButtonState.Normal;
4464                         if (radio_button.FlatStyle == FlatStyle.Flat) {
4465                                 state |= ButtonState.Flat;
4466                         }
4467                         
4468                         if (radio_button.Checked) {
4469                                 state |= ButtonState.Checked;
4470                         }
4471                         
4472                         if (!radio_button.Enabled) {
4473                                 state |= ButtonState.Inactive;
4474                         }
4475
4476                         // Start drawing
4477                         RadioButton_DrawButton(radio_button, dc, state, radiobutton_rectangle);
4478                         
4479                         if ((radio_button.image != null) || (radio_button.image_list != null))
4480                                 ButtonBase_DrawImage(radio_button, dc);
4481                         
4482                         RadioButton_DrawText(radio_button, text_rectangle, dc, text_format);
4483
4484                         if (radio_button.Focused && radio_button.Enabled && radio_button.appearance != Appearance.Button && radio_button.Text != String.Empty && radio_button.ShowFocusCues) {
4485                                 SizeF text_size = dc.MeasureString (radio_button.Text, radio_button.Font);
4486                                 
4487                                 Rectangle focus_rect = Rectangle.Empty;
4488                                 focus_rect.X = text_rectangle.X;
4489                                 focus_rect.Y = (int)((text_rectangle.Height - text_size.Height) / 2);
4490                                 focus_rect.Size = text_size.ToSize ();
4491
4492                                 RadioButton_DrawFocus (radio_button, dc, focus_rect);
4493                         }
4494                         
4495                         text_format.Dispose ();
4496                 }
4497
4498                 protected virtual void RadioButton_DrawButton(RadioButton radio_button, Graphics dc, ButtonState state, Rectangle radiobutton_rectangle)
4499                 {
4500                         dc.FillRectangle(GetControlBackBrush (radio_button.BackColor), radio_button.ClientRectangle);
4501                         
4502                         if (radio_button.appearance==Appearance.Button) {
4503                                 ButtonBase_DrawButton (radio_button, dc);
4504                                 
4505                                 if ((radio_button.Focused) && radio_button.Enabled)
4506                                         ButtonBase_DrawFocus(radio_button, dc);
4507                         } else {
4508                                 // establish if we are rendering a flat style of some sort
4509                                 if (radio_button.FlatStyle == FlatStyle.Flat || radio_button.FlatStyle == FlatStyle.Popup) {
4510                                         DrawFlatStyleRadioButton (dc, radiobutton_rectangle, radio_button);
4511                                 } else {
4512                                         CPDrawRadioButton(dc, radiobutton_rectangle, state);
4513                                 }
4514                         }
4515                 }
4516                 
4517                 protected virtual void RadioButton_DrawText(RadioButton radio_button, Rectangle text_rectangle, Graphics dc, StringFormat text_format)
4518                 {
4519                         DrawCheckBox_and_RadioButtonText (radio_button, text_rectangle, dc, 
4520                                                           text_format, radio_button.Appearance, radio_button.Checked);
4521                 }
4522                 
4523                 protected virtual void RadioButton_DrawFocus(RadioButton radio_button, Graphics dc, Rectangle text_rectangle)
4524                 {
4525                         DrawInnerFocusRectangle (dc, text_rectangle, radio_button.BackColor);
4526                 }
4527                 
4528                 
4529                 // renders a radio button with the Flat and Popup FlatStyle
4530                 protected virtual void DrawFlatStyleRadioButton (Graphics graphics, Rectangle rectangle, RadioButton radio_button)
4531                 {
4532                         int     lineWidth;
4533                         
4534                         if (radio_button.Enabled) {
4535                                 
4536                                 // draw the outer flatstyle arcs
4537                                 if (radio_button.FlatStyle == FlatStyle.Flat) {
4538                                         graphics.DrawArc (SystemPens.ControlDarkDark, rectangle, 0, 359);
4539                                         
4540                                         // fill in the area depending on whether or not the mouse is hovering
4541                                         if ((radio_button.is_entered || radio_button.Capture) && !radio_button.is_pressed) {
4542                                                 graphics.FillPie (SystemBrushes.ControlLight, rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
4543                                         } else {
4544                                                 graphics.FillPie (SystemBrushes.ControlLightLight, rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
4545                                         }
4546                                 } else {
4547                                         // must be a popup radio button
4548                                         // fill the control
4549                                         graphics.FillPie (SystemBrushes.ControlLightLight, rectangle, 0, 359);
4550
4551                                         if (radio_button.is_entered || radio_button.Capture) {
4552                                                 // draw the popup 3d button knob
4553                                                 graphics.DrawArc (SystemPens.ControlLight, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 0, 359);
4554
4555                                                 graphics.DrawArc (SystemPens.ControlDark, rectangle, 135, 180);
4556                                                 graphics.DrawArc (SystemPens.ControlLightLight, rectangle, 315, 180);
4557                                                 
4558                                         } else {
4559                                                 // just draw lighter flatstyle outer circle
4560                                                 graphics.DrawArc (SystemPens.ControlDark, rectangle, 0, 359);                                           
4561                                         }                                                                               
4562                                 }
4563                         } else {
4564                                 // disabled
4565                                 // fill control background color regardless of actual backcolor
4566                                 graphics.FillPie (SystemBrushes.Control, rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
4567                                 // draw the ark as control dark
4568                                 graphics.DrawArc (SystemPens.ControlDark, rectangle, 0, 359);
4569                         }
4570
4571                         // draw the check
4572                         if (radio_button.Checked) {
4573                                 lineWidth = Math.Max (1, Math.Min(rectangle.Width, rectangle.Height)/3);
4574                                 
4575                                 Pen dot_pen = SystemPens.ControlDarkDark;
4576                                 Brush dot_brush = SystemBrushes.ControlDarkDark;
4577
4578                                 if (!radio_button.Enabled || ((radio_button.FlatStyle == FlatStyle.Popup) && radio_button.is_pressed)) {
4579                                         dot_pen = SystemPens.ControlDark;
4580                                         dot_brush = SystemBrushes.ControlDark;
4581                                 } 
4582                                 
4583                                 if (rectangle.Height >  13) {
4584                                         graphics.FillPie (dot_brush, rectangle.X + lineWidth, rectangle.Y + lineWidth, rectangle.Width - lineWidth * 2, rectangle.Height - lineWidth * 2, 0, 359);
4585                                 } else {
4586                                         int x_half_pos = (rectangle.Width / 2) + rectangle.X;
4587                                         int y_half_pos = (rectangle.Height / 2) + rectangle.Y;
4588                                         
4589                                         graphics.DrawLine (dot_pen, x_half_pos - 1, y_half_pos, x_half_pos + 2, y_half_pos);
4590                                         graphics.DrawLine (dot_pen, x_half_pos - 1, y_half_pos + 1, x_half_pos + 2, y_half_pos + 1);
4591                                         
4592                                         graphics.DrawLine (dot_pen, x_half_pos, y_half_pos - 1, x_half_pos, y_half_pos + 2);
4593                                         graphics.DrawLine (dot_pen, x_half_pos + 1, y_half_pos - 1, x_half_pos + 1, y_half_pos + 2);
4594                                 }
4595                         }
4596                 }
4597
4598                 public override Size RadioButtonDefaultSize {
4599                         get {
4600                                 return new Size (104,24);
4601                         }
4602                 }
4603
4604                 public override void DrawRadioButton (Graphics g, RadioButton rb, Rectangle glyphArea, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
4605                 {
4606                         // Draw Button Background
4607                         if (rb.FlatStyle == FlatStyle.Flat || rb.FlatStyle == FlatStyle.Popup) {
4608                                 glyphArea.Height -= 2;
4609                                 glyphArea.Width -= 2;
4610                         }
4611                         
4612                         DrawRadioButtonGlyph (g, rb, glyphArea);
4613
4614                         // If we have an image, draw it
4615                         if (imageBounds.Size != Size.Empty)
4616                                 DrawRadioButtonImage (g, rb, imageBounds);
4617
4618                         if (rb.Focused && rb.Enabled && rb.ShowFocusCues && textBounds.Size != Size.Empty)
4619                                 DrawRadioButtonFocus (g, rb, textBounds);
4620
4621                         // If we have text, draw it
4622                         if (textBounds != Rectangle.Empty)
4623                                 DrawRadioButtonText (g, rb, textBounds);
4624                 }
4625
4626                 public virtual void DrawRadioButtonGlyph (Graphics g, RadioButton rb, Rectangle glyphArea)
4627                 {
4628                         if (rb.Pressed)
4629                                 ThemeElements.CurrentTheme.RadioButtonPainter.PaintRadioButton (g, glyphArea, rb.BackColor, rb.ForeColor, ElementState.Pressed, rb.FlatStyle, rb.Checked);
4630                         else if (rb.InternalSelected)
4631                                 ThemeElements.CurrentTheme.RadioButtonPainter.PaintRadioButton (g, glyphArea, rb.BackColor, rb.ForeColor, ElementState.Normal, rb.FlatStyle, rb.Checked);
4632                         else if (rb.Entered)
4633                                 ThemeElements.CurrentTheme.RadioButtonPainter.PaintRadioButton (g, glyphArea, rb.BackColor, rb.ForeColor, ElementState.Hot, rb.FlatStyle, rb.Checked);
4634                         else if (!rb.Enabled)
4635                                 ThemeElements.CurrentTheme.RadioButtonPainter.PaintRadioButton (g, glyphArea, rb.BackColor, rb.ForeColor, ElementState.Disabled, rb.FlatStyle, rb.Checked);
4636                         else
4637                                 ThemeElements.CurrentTheme.RadioButtonPainter.PaintRadioButton (g, glyphArea, rb.BackColor, rb.ForeColor, ElementState.Normal, rb.FlatStyle, rb.Checked);
4638                 }
4639
4640                 public virtual void DrawRadioButtonFocus (Graphics g, RadioButton rb, Rectangle focusArea)
4641                 {
4642                         ControlPaint.DrawFocusRectangle (g, focusArea);
4643                 }
4644
4645                 public virtual void DrawRadioButtonImage (Graphics g, RadioButton rb, Rectangle imageBounds)
4646                 {
4647                         if (rb.Enabled)
4648                                 g.DrawImage (rb.Image, imageBounds);
4649                         else
4650                                 CPDrawImageDisabled (g, rb.Image, imageBounds.Left, imageBounds.Top, ColorControl);
4651                 }
4652
4653                 public virtual void DrawRadioButtonText (Graphics g, RadioButton rb, Rectangle textBounds)
4654                 {
4655                         if (rb.Enabled)
4656                                 TextRenderer.DrawTextInternal (g, rb.Text, rb.Font, textBounds, rb.ForeColor, rb.TextFormatFlags, rb.UseCompatibleTextRendering);
4657                         else
4658                                 DrawStringDisabled20 (g, rb.Text, rb.Font, textBounds, rb.BackColor, rb.TextFormatFlags, rb.UseCompatibleTextRendering);
4659                 }
4660
4661                 public override Size CalculateRadioButtonAutoSize (RadioButton rb)
4662                 {
4663                         Size ret_size = Size.Empty;
4664                         Size text_size = TextRenderer.MeasureTextInternal (rb.Text, rb.Font, rb.UseCompatibleTextRendering);
4665                         Size image_size = rb.Image == null ? Size.Empty : rb.Image.Size;
4666
4667                         // Pad the text size
4668                         if (rb.Text.Length != 0) {
4669                                 text_size.Height += 4;
4670                                 text_size.Width += 4;
4671                         }
4672
4673                         switch (rb.TextImageRelation) {
4674                                 case TextImageRelation.Overlay:
4675                                         ret_size.Height = Math.Max (rb.Text.Length == 0 ? 0 : text_size.Height, image_size.Height);
4676                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
4677                                         break;
4678                                 case TextImageRelation.ImageAboveText:
4679                                 case TextImageRelation.TextAboveImage:
4680                                         ret_size.Height = text_size.Height + image_size.Height;
4681                                         ret_size.Width = Math.Max (text_size.Width, image_size.Width);
4682                                         break;
4683                                 case TextImageRelation.ImageBeforeText:
4684                                 case TextImageRelation.TextBeforeImage:
4685                                         ret_size.Height = Math.Max (text_size.Height, image_size.Height);
4686                                         ret_size.Width = text_size.Width + image_size.Width;
4687                                         break;
4688                         }
4689
4690                         // Pad the result
4691                         ret_size.Height += (rb.Padding.Vertical);
4692                         ret_size.Width += (rb.Padding.Horizontal) + 15;
4693
4694                         // There seems to be a minimum height
4695                         if (ret_size.Height == rb.Padding.Vertical)
4696                                 ret_size.Height += 14;
4697
4698                         return ret_size;
4699                 }
4700
4701                 public override void CalculateRadioButtonTextAndImageLayout (ButtonBase b, Point offset, out Rectangle glyphArea, out Rectangle textRectangle, out Rectangle imageRectangle)
4702                 {
4703                         CalculateCheckBoxTextAndImageLayout (b, offset, out glyphArea, out textRectangle, out imageRectangle);
4704                 }
4705                 #endregion      // RadioButton
4706
4707                 #region ScrollBar
4708                 public override void DrawScrollBar (Graphics dc, Rectangle clip, ScrollBar bar)
4709                 {
4710                         int             scrollbutton_width = bar.scrollbutton_width;
4711                         int             scrollbutton_height = bar.scrollbutton_height;
4712                         Rectangle       first_arrow_area;
4713                         Rectangle       second_arrow_area;                      
4714                         Rectangle       thumb_pos;
4715                         
4716                         thumb_pos = bar.ThumbPos;
4717
4718                         if (bar.vert) {
4719                                 first_arrow_area = new Rectangle(0, 0, bar.Width, scrollbutton_height);
4720                                 bar.FirstArrowArea = first_arrow_area;
4721
4722                                 second_arrow_area = new Rectangle(0, bar.ClientRectangle.Height - scrollbutton_height, bar.Width, scrollbutton_height);
4723                                 bar.SecondArrowArea = second_arrow_area;
4724
4725                                 thumb_pos.Width = bar.Width;
4726                                 bar.ThumbPos = thumb_pos;
4727
4728                                 Brush VerticalBrush;
4729                                 /* Background, upper track */
4730                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
4731                                         VerticalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63, 63, 63), Color.Black);
4732                                 else
4733                                         VerticalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, Color.White);
4734                                 Rectangle UpperTrack = new Rectangle (0, 0, bar.ClientRectangle.Width, bar.ThumbPos.Bottom);
4735                                 if (clip.IntersectsWith (UpperTrack))
4736                                         dc.FillRectangle (VerticalBrush, UpperTrack);
4737
4738                                 /* Background, lower track */
4739                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
4740                                         VerticalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63, 63, 63), Color.Black);
4741                                 else
4742                                         VerticalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, Color.White);
4743                                 Rectangle LowerTrack = new Rectangle (0, bar.ThumbPos.Bottom, bar.ClientRectangle.Width, bar.ClientRectangle.Height - bar.ThumbPos.Bottom);
4744                                 if (clip.IntersectsWith (LowerTrack))
4745                                         dc.FillRectangle (VerticalBrush, LowerTrack);
4746
4747                                 /* Buttons */
4748                                 if (clip.IntersectsWith (first_arrow_area))
4749                                         CPDrawScrollButton (dc, first_arrow_area, ScrollButton.Up, bar.firstbutton_state);
4750                                 if (clip.IntersectsWith (second_arrow_area))
4751                                         CPDrawScrollButton (dc, second_arrow_area, ScrollButton.Down, bar.secondbutton_state);
4752                         } else {
4753                                 first_arrow_area = new Rectangle(0, 0, scrollbutton_width, bar.Height);
4754                                 bar.FirstArrowArea = first_arrow_area;
4755
4756                                 second_arrow_area = new Rectangle (bar.ClientRectangle.Width - scrollbutton_width, 0, scrollbutton_width, bar.Height);
4757                                 bar.SecondArrowArea = second_arrow_area;
4758
4759                                 thumb_pos.Height = bar.Height;
4760                                 bar.ThumbPos = thumb_pos;
4761
4762                                 Brush HorizontalBrush;
4763                                 //Background, left track
4764                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Backwards)
4765                                         HorizontalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63, 63, 63), Color.Black);
4766                                 else
4767                                         HorizontalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, Color.White);
4768                                 Rectangle LeftTrack = new Rectangle (0, 0, bar.ThumbPos.Right, bar.ClientRectangle.Height);
4769                                 if (clip.IntersectsWith (LeftTrack))
4770                                         dc.FillRectangle (HorizontalBrush, LeftTrack);
4771
4772                                 //Background, right track
4773                                 if (bar.thumb_moving == ScrollBar.ThumbMoving.Forward)
4774                                         HorizontalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63, 63, 63), Color.Black);
4775                                 else
4776                                         HorizontalBrush = ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, Color.White);
4777                                 Rectangle RightTrack = new Rectangle (bar.ThumbPos.Right, 0, bar.ClientRectangle.Width - bar.ThumbPos.Right, bar.ClientRectangle.Height);
4778                                 if (clip.IntersectsWith (RightTrack))
4779                                         dc.FillRectangle (HorizontalBrush, RightTrack);
4780
4781                                 /* Buttons */
4782                                 if (clip.IntersectsWith (first_arrow_area))
4783                                         CPDrawScrollButton (dc, first_arrow_area, ScrollButton.Left, bar.firstbutton_state);
4784                                 if (clip.IntersectsWith (second_arrow_area))
4785                                         CPDrawScrollButton (dc, second_arrow_area, ScrollButton.Right, bar.secondbutton_state);
4786                         }
4787
4788                         /* Thumb */
4789                         ScrollBar_DrawThumb(bar, thumb_pos, clip, dc);                          
4790                 }
4791
4792                 protected virtual void ScrollBar_DrawThumb(ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc)
4793                 {
4794                         if (bar.Enabled && thumb_pos.Width > 0 && thumb_pos.Height > 0 && clip.IntersectsWith(thumb_pos))
4795                                 DrawScrollButtonPrimitive(dc, thumb_pos, ButtonState.Normal);
4796                 }
4797
4798                 public override int ScrollBarButtonSize {
4799                         get { return 16; }
4800                 }
4801
4802                 public override bool ScrollBarHasHotElementStyles {
4803                         get {
4804                                 return false;
4805                         }
4806                 }
4807
4808                 public override bool ScrollBarHasPressedThumbStyle {
4809                         get { 
4810                                 return false;
4811                         }
4812                 }
4813
4814                 public override bool ScrollBarHasHoverArrowButtonStyle {
4815                         get {
4816                                 return false;
4817                         }
4818                 }
4819                 #endregion      // ScrollBar
4820
4821                 #region StatusBar
4822                 public  override void DrawStatusBar (Graphics real_dc, Rectangle clip, StatusBar sb) {
4823                         Rectangle area = sb.ClientRectangle;
4824                         int horz_border = 2;
4825                         int vert_border = 2;
4826
4827                         Image backbuffer = new Bitmap (sb.ClientSize.Width, sb.ClientSize.Height, real_dc);
4828                         Graphics dc = Graphics.FromImage (backbuffer);
4829                         
4830                         DrawStatusBarBackground (dc, clip, sb);
4831                         
4832                         if (!sb.ShowPanels && sb.Text != String.Empty) {
4833                                 string text = sb.Text;
4834                                 StringFormat string_format = new StringFormat ();
4835                                 string_format.Trimming = StringTrimming.Character;
4836                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
4837                                 
4838                                 if (text.Length > 127)
4839                                         text = text.Substring (0, 127);
4840
4841                                 if (text [0] == '\t') {
4842                                         string_format.Alignment = StringAlignment.Center;
4843                                         text = text.Substring (1);
4844                                         if (text [0] == '\t') {
4845                                                 string_format.Alignment = StringAlignment.Far;
4846                                                 text = text.Substring (1);
4847                                         }
4848                                 }
4849                 
4850                                 dc.DrawString (text, sb.Font, ResPool.GetSolidBrush (sb.ForeColor),
4851                                                 new Rectangle(area.X + 2, area.Y + 2, area.Width - 4, area.Height - 4), string_format);
4852                                 string_format.Dispose ();
4853                         } else if (sb.ShowPanels) {
4854                                 Brush br_forecolor = GetControlForeBrush (sb.ForeColor);
4855                                 int prev_x = area.X + horz_border;
4856                                 int y = area.Y + vert_border;
4857                                 for (int i = 0; i < sb.Panels.Count; i++) {
4858                                         Rectangle pr = new Rectangle (prev_x, y,
4859                                                 sb.Panels [i].Width, area.Height);
4860                                         prev_x += pr.Width + StatusBarHorzGapWidth;
4861                                         if (pr.IntersectsWith (clip))
4862                                                 DrawStatusBarPanel (dc, pr, i, br_forecolor, sb.Panels [i]);
4863                                 }
4864                         }
4865
4866                         if (sb.SizingGrip)
4867                                 DrawStatusBarSizingGrip (dc, clip, sb, area);
4868                         
4869                         real_dc.DrawImage (backbuffer, 0, 0);
4870                         dc.Dispose ();
4871                         backbuffer.Dispose ();
4872
4873                 }
4874
4875                 protected virtual void DrawStatusBarBackground (Graphics dc, Rectangle clip, StatusBar sb)
4876                 {
4877                         bool is_color_control = sb.BackColor.ToArgb () == ColorControl.ToArgb ();
4878
4879                         Brush brush = is_color_control ? SystemBrushes.Control : ResPool.GetSolidBrush (sb.BackColor);
4880                         dc.FillRectangle (brush, clip);
4881                 }
4882
4883                 protected virtual void DrawStatusBarSizingGrip (Graphics dc, Rectangle clip, StatusBar sb, Rectangle area)
4884                 {
4885                         area = new Rectangle (area.Right - 16 - 2, area.Bottom - 12 - 1, 16, 16);
4886                         CPDrawSizeGrip (dc, ColorControl, area);
4887                 }
4888
4889                 protected virtual void DrawStatusBarPanel (Graphics dc, Rectangle area, int index,
4890                         Brush br_forecolor, StatusBarPanel panel) {
4891                         int border_size = 3; // this is actually const, even if the border style is none
4892                         int icon_width = 16;
4893                         
4894                         area.Height -= border_size;
4895
4896                         DrawStatusBarPanelBackground (dc, area, panel);
4897
4898                         if (panel.Style == StatusBarPanelStyle.OwnerDraw) {
4899                                 StatusBarDrawItemEventArgs e = new StatusBarDrawItemEventArgs (
4900                                         dc, panel.Parent.Font, area, index, DrawItemState.Default,
4901                                         panel, panel.Parent.ForeColor, panel.Parent.BackColor);
4902                                 panel.Parent.OnDrawItemInternal (e);
4903                                 return;
4904                         }
4905
4906                         string text = panel.Text;
4907                         StringFormat string_format = new StringFormat ();
4908                         string_format.Trimming = StringTrimming.Character;
4909                         string_format.FormatFlags = StringFormatFlags.NoWrap;
4910
4911                         
4912                         if (text != null && text.Length > 0 && text [0] == '\t') {
4913                                 string_format.Alignment = StringAlignment.Center;
4914                                 text = text.Substring (1);
4915                                 if (text [0] == '\t') {
4916                                         string_format.Alignment = StringAlignment.Far;
4917                                         text = text.Substring (1);
4918                                 }
4919                         }
4920
4921                         Rectangle string_rect = Rectangle.Empty;
4922                         int x;
4923                         int len;
4924                         int icon_x = 0;
4925                         int y = (area.Height / 2 - (int) panel.Parent.Font.Size / 2) - 1;
4926
4927                         switch (panel.Alignment) {
4928                         case HorizontalAlignment.Right:
4929                                 len = (int) dc.MeasureString (text, panel.Parent.Font).Width;
4930                                 x = area.Right - len - 4;
4931                                 string_rect = new Rectangle (x, y, 
4932                                                 area.Right - x - border_size,
4933                                                 area.Bottom - y - border_size);
4934                                 if (panel.Icon != null) {
4935                                         icon_x = x - icon_width - 2;
4936                                 }
4937                                 break;
4938                         case HorizontalAlignment.Center:
4939                                 len = (int) dc.MeasureString (text, panel.Parent.Font).Width;
4940                                 x = area.Left + ((panel.Width - len) / 2);
4941                                 
4942                                 string_rect = new Rectangle (x, y, 
4943                                                 area.Right - x - border_size,
4944                                                 area.Bottom - y - border_size);
4945
4946                                 if (panel.Icon != null) {
4947                                         icon_x = x - icon_width - 2;
4948                                 }
4949                                 break;
4950
4951                                 
4952                         default:
4953                                 int left = area.Left + border_size;;
4954                                 if (panel.Icon != null) {
4955                                         icon_x = area.Left + 2;
4956                                         left = icon_x + icon_width + 2;
4957                                 }
4958
4959                                 x = left;
4960                                 string_rect = new Rectangle (x, y, 
4961                                                 area.Right - x - border_size,
4962                                                 area.Bottom - y - border_size);
4963                                 break;
4964                         }
4965
4966                         RectangleF clip_bounds = dc.ClipBounds;
4967                         dc.SetClip (area);
4968                         dc.DrawString (text, panel.Parent.Font, br_forecolor, string_rect, string_format);                      
4969                         dc.SetClip (clip_bounds);
4970
4971                         if (panel.Icon != null) {
4972                                 dc.DrawIcon (panel.Icon, new Rectangle (icon_x, y, icon_width, icon_width));
4973                         }
4974                 }
4975
4976                 protected virtual void DrawStatusBarPanelBackground (Graphics dc, Rectangle area, StatusBarPanel panel)
4977                 {
4978                         if (panel.BorderStyle != StatusBarPanelBorderStyle.None) {
4979                                 Border3DStyle border_style = Border3DStyle.SunkenOuter;
4980                                 if (panel.BorderStyle == StatusBarPanelBorderStyle.Raised)
4981                                         border_style = Border3DStyle.RaisedInner;
4982                                         
4983                                 CPDrawBorder3D(dc, area, border_style, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, panel.Parent.BackColor);
4984                         }
4985                 }
4986
4987                 public override int StatusBarSizeGripWidth {
4988                         get { return 15; }
4989                 }
4990
4991                 public override int StatusBarHorzGapWidth {
4992                         get { return 3; }
4993                 }
4994
4995                 public override Size StatusBarDefaultSize {
4996                         get {
4997                                 return new Size (100, 22);
4998                         }
4999                 }
5000                 #endregion      // StatusBar
5001
5002                 #region TabControl
5003
5004                 #region TabControl settings
5005
5006                 public override Size TabControlDefaultItemSize {
5007                         get { return ThemeElements.CurrentTheme.TabControlPainter.DefaultItemSize; }
5008                 }
5009
5010                 public override Point TabControlDefaultPadding {
5011                         get { return ThemeElements.CurrentTheme.TabControlPainter.DefaultPadding; }
5012                 }
5013
5014                 public override int TabControlMinimumTabWidth {
5015                         get { return ThemeElements.CurrentTheme.TabControlPainter.MinimumTabWidth; }
5016                 }
5017
5018                 public override Rectangle TabControlSelectedDelta {
5019                         get { return ThemeElements.CurrentTheme.TabControlPainter.SelectedTabDelta; }
5020                 }
5021
5022                 public override int TabControlSelectedSpacing {
5023                         get { return ThemeElements.CurrentTheme.TabControlPainter.SelectedSpacing; }
5024                 }
5025                 
5026                 public override int TabPanelOffsetX {
5027                         get { return ThemeElements.CurrentTheme.TabControlPainter.TabPanelOffset.X; }
5028                 }
5029                 
5030                 public override int TabPanelOffsetY {
5031                         get { return ThemeElements.CurrentTheme.TabControlPainter.TabPanelOffset.Y; }
5032                 }
5033
5034                 public override int TabControlColSpacing {
5035                         get { return ThemeElements.CurrentTheme.TabControlPainter.ColSpacing; }
5036                 }
5037
5038                 public override Point TabControlImagePadding {
5039                         get { return ThemeElements.CurrentTheme.TabControlPainter.ImagePadding; }
5040                 }
5041
5042                 public override int TabControlScrollerWidth {
5043                         get {return ThemeElements.CurrentTheme.TabControlPainter.ScrollerWidth; }
5044                 }
5045
5046
5047                 public override Size TabControlGetSpacing (TabControl tab) 
5048                 {
5049                         try {
5050                                 return ThemeElements.CurrentTheme.TabControlPainter.RowSpacing (tab);
5051                         } catch {
5052                                 throw new Exception ("Invalid Appearance value: " + tab.Appearance);
5053                         }
5054                 }
5055                 #endregion
5056
5057                 public override void DrawTabControl (Graphics dc, Rectangle area, TabControl tab)
5058                 {
5059                         ThemeElements.CurrentTheme.TabControlPainter.Draw (dc, area, tab);
5060                 }
5061
5062                 public override Rectangle TabControlGetDisplayRectangle (TabControl tab)
5063                 {
5064                         return ThemeElements.CurrentTheme.TabControlPainter.GetDisplayRectangle (tab);
5065                 }
5066
5067                 public override Rectangle TabControlGetPanelRect (TabControl tab)
5068                 {
5069                         return ThemeElements.CurrentTheme.TabControlPainter.GetTabPanelRect (tab);
5070                 }
5071
5072                 #endregion
5073
5074                 #region TextBox
5075                 public override void TextBoxBaseFillBackground (TextBoxBase textBoxBase, Graphics g, Rectangle clippingArea)
5076                 {
5077                         if (textBoxBase.backcolor_set || (textBoxBase.Enabled && !textBoxBase.read_only)) {
5078                                 g.FillRectangle(ResPool.GetSolidBrush(textBoxBase.BackColor), clippingArea);
5079                         } else {
5080                                 g.FillRectangle(ResPool.GetSolidBrush(ColorControl), clippingArea);
5081                         }
5082                 }
5083
5084                 public override bool TextBoxBaseHandleWmNcPaint (TextBoxBase textBoxBase, ref Message m)
5085                 {
5086                         return false;
5087                 }
5088
5089                 public override bool TextBoxBaseShouldPaintBackground (TextBoxBase textBoxBase)
5090                 {
5091                         return true;
5092                 }
5093                 #endregion
5094
5095                 #region ToolBar
5096                 public  override void DrawToolBar (Graphics dc, Rectangle clip_rectangle, ToolBar control) 
5097                 {
5098                         StringFormat format = new StringFormat ();
5099                         format.Trimming = StringTrimming.EllipsisCharacter;
5100                         format.LineAlignment = StringAlignment.Center;
5101                         if (control.ShowKeyboardCuesInternal)
5102                                 format.HotkeyPrefix = HotkeyPrefix.Show;
5103                         else
5104                                 format.HotkeyPrefix = HotkeyPrefix.Hide;
5105
5106                         if (control.TextAlign == ToolBarTextAlign.Underneath)
5107                                 format.Alignment = StringAlignment.Center;
5108                         else
5109                                 format.Alignment = StringAlignment.Near;
5110
5111                         if (control.Appearance != ToolBarAppearance.Flat || control.Parent == null) {
5112                                 dc.FillRectangle (SystemBrushes.Control, clip_rectangle);
5113                         }
5114
5115                         if (control.Divider && clip_rectangle.Y < 2) {
5116                                 if (clip_rectangle.Y < 1) {
5117                                         dc.DrawLine (SystemPens.ControlDark, clip_rectangle.X, 0, clip_rectangle.Right, 0);
5118                                 }
5119                                 dc.DrawLine (SystemPens.ControlLightLight, clip_rectangle.X, 1, clip_rectangle.Right, 1);
5120                         }
5121
5122                         foreach (ToolBarItem item in control.items)
5123                                 if (item.Button.Visible && clip_rectangle.IntersectsWith (item.Rectangle))
5124                                         DrawToolBarButton (dc, control, item, format);
5125
5126                         format.Dispose ();
5127                 }
5128
5129                 protected virtual void DrawToolBarButton (Graphics dc, ToolBar control, ToolBarItem item, StringFormat format)
5130                 {
5131                         bool is_flat = (control.Appearance == ToolBarAppearance.Flat);
5132                         
5133                         DrawToolBarButtonBorder (dc, item, is_flat);
5134
5135                         switch (item.Button.Style) {
5136                         case ToolBarButtonStyle.DropDownButton:
5137                                 if (control.DropDownArrows)
5138                                         DrawToolBarDropDownArrow (dc, item, is_flat);
5139                                 DrawToolBarButtonContents (dc, control, item, format);
5140                                 break;
5141
5142                         case ToolBarButtonStyle.Separator:
5143                                 if (is_flat)
5144                                         DrawToolBarSeparator (dc, item);
5145                                 break;
5146
5147                         case ToolBarButtonStyle.ToggleButton:
5148                                 DrawToolBarToggleButtonBackground (dc, item);
5149                                 DrawToolBarButtonContents (dc, control, item, format);
5150                                 break;
5151
5152                         default:
5153                                 DrawToolBarButtonContents (dc, control, item, format);
5154                                 break;
5155                         }
5156                 }
5157
5158                 const Border3DSide all_sides = Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom;
5159
5160                 protected virtual void DrawToolBarButtonBorder (Graphics dc, ToolBarItem item, bool is_flat)
5161                 {
5162                         if (item.Button.Style == ToolBarButtonStyle.Separator)
5163                                 return;
5164
5165                         Border3DStyle style;
5166
5167                         if (is_flat) {
5168                                 if (item.Button.Pushed || item.Pressed)
5169                                         style = Border3DStyle.SunkenOuter;
5170                                 else if (item.Hilight)
5171                                         style = Border3DStyle.RaisedInner;
5172                                 else
5173                                         return;
5174
5175                         } else {
5176                                 if (item.Button.Pushed || item.Pressed)
5177                                         style = Border3DStyle.Sunken;
5178                                 else 
5179                                         style = Border3DStyle.Raised;
5180                         }
5181                         
5182                         Rectangle rect = item.Rectangle;
5183                         if ((item.Button.Style == ToolBarButtonStyle.DropDownButton) && (item.Button.Parent.DropDownArrows) && is_flat)
5184                                 rect.Width -= ToolBarDropDownWidth;
5185
5186                         CPDrawBorder3D (dc, rect, style, all_sides);
5187                 }
5188
5189                 protected virtual void DrawToolBarSeparator (Graphics dc, ToolBarItem item)
5190                 {
5191                         Rectangle area = item.Rectangle;
5192                         int offset = (int) SystemPens.Control.Width + 1;
5193                         dc.DrawLine (SystemPens.ControlDark, area.X + 1, area.Y, area.X + 1, area.Bottom);
5194                         dc.DrawLine (SystemPens.ControlLight, area.X + offset, area.Y, area.X + offset, area.Bottom);
5195                 }
5196
5197                 protected virtual void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarItem item)
5198                 {
5199                         Brush brush;
5200                         Rectangle area = item.Rectangle;
5201                         area.X += ToolBarImageGripWidth;
5202                         area.Y += ToolBarImageGripWidth;
5203                         area.Width -= 2 * ToolBarImageGripWidth;
5204                         area.Height -= 2 * ToolBarImageGripWidth;
5205                         
5206                         if (item.Button.Pushed)
5207                                 brush = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, ColorControlLightLight);
5208                         else if (item.Button.PartialPush)
5209                                 brush = SystemBrushes.ControlLight;
5210                         else
5211                                 brush = SystemBrushes.Control;
5212                          
5213                         dc.FillRectangle (brush, area);
5214                 }
5215
5216                 protected virtual void DrawToolBarDropDownArrow (Graphics dc, ToolBarItem item, bool is_flat)
5217                 {
5218                         Rectangle rect = item.Rectangle;
5219                         rect.X = item.Rectangle.Right - ToolBarDropDownWidth;
5220                         rect.Width = ToolBarDropDownWidth;
5221                         
5222                         if (is_flat) {
5223                                 if (item.DDPressed)
5224                                         CPDrawBorder3D (dc, rect, Border3DStyle.SunkenOuter, all_sides);
5225                                 else if (item.Button.Pushed || item.Pressed)
5226                                         CPDrawBorder3D (dc, rect, Border3DStyle.SunkenOuter, all_sides);
5227                                 else if (item.Hilight)
5228                                         CPDrawBorder3D (dc, rect, Border3DStyle.RaisedInner, all_sides);
5229                         } else {
5230                                 if (item.DDPressed)
5231                                         CPDrawBorder3D (dc, rect, Border3DStyle.Flat, all_sides);
5232                                 else if (item.Button.Pushed || item.Pressed)
5233                                         CPDrawBorder3D (dc, Rectangle.Inflate(rect, -1, -1), Border3DStyle.SunkenOuter, all_sides);
5234                                 else
5235                                         CPDrawBorder3D (dc, rect, Border3DStyle.Raised, all_sides);
5236                         }
5237                         
5238                         PointF [] vertices = new PointF [3];
5239                         PointF ddCenter = new PointF (rect.X + (rect.Width/2.0f), rect.Y + (rect.Height / 2));
5240                         
5241                         // Increase vertical and horizontal position by 1 when button is pressed
5242                         if (item.Pressed || item.Button.Pushed || item.DDPressed) {
5243                             ddCenter.X += 1;
5244                             ddCenter.Y += 1;
5245                         }
5246                         
5247                         vertices [0].X = ddCenter.X - ToolBarDropDownArrowWidth / 2.0f + 0.5f;
5248                         vertices [0].Y = ddCenter.Y;
5249                         vertices [1].X = ddCenter.X + ToolBarDropDownArrowWidth / 2.0f + 0.5f;
5250                         vertices [1].Y = ddCenter.Y;
5251                         vertices [2].X = ddCenter.X + 0.5f; // 0.5 is added for adjustment
5252                         vertices [2].Y = ddCenter.Y + ToolBarDropDownArrowHeight;
5253                         dc.FillPolygon (SystemBrushes.ControlText, vertices);
5254                 }
5255
5256                 protected virtual void DrawToolBarButtonContents (Graphics dc, ToolBar control, ToolBarItem item, StringFormat format)
5257                 {
5258                         if (item.Button.Image != null) {
5259                                 int x = item.ImageRectangle.X + ToolBarImageGripWidth;
5260                                 int y = item.ImageRectangle.Y + ToolBarImageGripWidth;
5261                                 
5262                                 // Increase vertical and horizontal position by 1 when button is pressed
5263                                 if (item.Pressed || item.Button.Pushed) {
5264                                     x += 1;
5265                                     y += 1;
5266                                 }
5267                                 
5268                                 if (item.Button.Enabled)
5269                                         dc.DrawImage (item.Button.Image, x, y);
5270                                 else 
5271                                         CPDrawImageDisabled (dc, item.Button.Image, x, y, ColorControl);
5272                         }
5273
5274                         Rectangle text_rect = item.TextRectangle;
5275                         if (text_rect.Width <= 0 || text_rect.Height <= 0)
5276                                 return;
5277
5278                         if (item.Pressed || item.Button.Pushed) {
5279                                 text_rect.X += 1;
5280                                 text_rect.Y += 1;
5281                         }
5282                         
5283                         if (item.Button.Enabled)
5284                                 dc.DrawString (item.Button.Text, control.Font, SystemBrushes.ControlText, text_rect, format);
5285                         else
5286                                 CPDrawStringDisabled (dc, item.Button.Text, control.Font, control.BackColor, text_rect, format);
5287                 }
5288
5289                 // Grip width for the ToolBar
5290                 public override int ToolBarGripWidth {
5291                         get { return 2;}
5292                 }
5293
5294                 // Grip width for the Image on the ToolBarButton
5295                 public override int ToolBarImageGripWidth {
5296                         get { return 2;}
5297                 }
5298
5299                 // width of the separator
5300                 public override int ToolBarSeparatorWidth {
5301                         get { return 4; }
5302                 }
5303
5304                 // width of the dropdown arrow rect
5305                 public override int ToolBarDropDownWidth {
5306                         get { return 13; }
5307                 }
5308
5309                 // width for the dropdown arrow on the ToolBarButton
5310                 public override int ToolBarDropDownArrowWidth {
5311                         get { return 5;}
5312                 }
5313
5314                 // height for the dropdown arrow on the ToolBarButton
5315                 public override int ToolBarDropDownArrowHeight {
5316                         get { return 3;}
5317                 }
5318
5319                 public override Size ToolBarDefaultSize {
5320                         get {
5321                                 return new Size (100, 42);
5322                         }
5323                 }
5324
5325                 public override bool ToolBarHasHotElementStyles (ToolBar toolBar)
5326                 {
5327                         return toolBar.Appearance == ToolBarAppearance.Flat;
5328                 }
5329
5330                 public override bool ToolBarHasHotCheckedElementStyles {
5331                         get {
5332                                 return false;
5333                         }
5334                 }
5335                 #endregion      // ToolBar
5336
5337                 #region ToolTip
5338                 public override void DrawToolTip(Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control)
5339                 {
5340                         ToolTipDrawBackground (dc, clip_rectangle, control);
5341
5342                         TextFormatFlags flags = TextFormatFlags.HidePrefix;
5343
5344                         Color foreground = control.ForeColor;
5345                         if (control.title.Length > 0) {
5346                                 Font bold_font = new Font (control.Font, control.Font.Style | FontStyle.Bold);
5347                                 TextRenderer.DrawTextInternal (dc, control.title, bold_font, control.title_rect,
5348                                                 foreground, flags, false);
5349                                 bold_font.Dispose ();
5350                         }
5351
5352                         if (control.icon != null)
5353                                 dc.DrawIcon (control.icon, control.icon_rect);
5354
5355                         TextRenderer.DrawTextInternal (dc, control.Text, control.Font, control.text_rect, foreground, flags, false);
5356                 }
5357
5358                 protected virtual void ToolTipDrawBackground (Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control)
5359                 {
5360                         Brush back_brush = ResPool.GetSolidBrush (control.BackColor);
5361                         dc.FillRectangle (back_brush, control.ClientRectangle);
5362                         dc.DrawRectangle (SystemPens.WindowFrame, 0, 0, control.Width - 1, control.Height - 1);
5363                 }
5364
5365                 public override Size ToolTipSize(ToolTip.ToolTipWindow tt, string text)
5366                 {
5367                         Size size = TextRenderer.MeasureTextInternal (text, tt.Font, false);
5368                         size.Width += 4;
5369                         size.Height += 3;
5370                         Rectangle text_rect = new Rectangle (Point.Empty, size);
5371                         text_rect.Inflate (-2, -1);
5372                         tt.text_rect = text_rect;
5373                         tt.icon_rect = tt.title_rect = Rectangle.Empty;
5374
5375                         Size title_size = Size.Empty;
5376                         if (tt.title.Length > 0) {
5377                                 Font bold_font = new Font (tt.Font, tt.Font.Style | FontStyle.Bold);
5378                                 title_size = TextRenderer.MeasureTextInternal (tt.title, bold_font, false);
5379                                 bold_font.Dispose ();
5380                         }
5381
5382                         Size icon_size = Size.Empty;
5383                         if (tt.icon != null)
5384                                 icon_size = new Size (size.Height, size.Height);
5385
5386                         if (icon_size != Size.Empty || title_size != Size.Empty) {
5387                                 int padding = 8;
5388                                 int top_area_width = 0;
5389                                 int top_area_height = icon_size.Height > title_size.Height ? icon_size.Height : title_size.Height;
5390                                 Size text_size = size;
5391                                 Point location = new Point (padding, padding);
5392
5393                                 if (icon_size != Size.Empty) {
5394                                         tt.icon_rect = new Rectangle (location, icon_size);
5395                                         top_area_width = icon_size.Width + padding;
5396                                 }
5397
5398                                 if (title_size != Size.Empty) {
5399                                         Rectangle title_rect = new Rectangle (location, new Size (title_size.Width, top_area_height));
5400                                         if (icon_size != Size.Empty)
5401                                                 title_rect.X += icon_size.Width + padding;
5402
5403                                         tt.title_rect = title_rect;
5404                                         top_area_width += title_size.Width;
5405                                 }
5406
5407                                 tt.text_rect = new Rectangle (new Point (location.X, location.Y + top_area_height + padding),
5408                                                 text_size);
5409
5410                                 size.Height += padding + top_area_height;
5411                                 if (top_area_width > size.Width)
5412                                         size.Width = top_area_width;
5413
5414                                 // margins
5415                                 size.Width += padding * 2;
5416                                 size.Height += padding * 2;
5417                         }
5418
5419                         return size;
5420                 }
5421                 
5422                 public override bool ToolTipTransparentBackground {
5423                         get {
5424                                 return false;
5425                         }
5426                 }
5427                 #endregion      // ToolTip
5428
5429                 #region BalloonWindow
5430                 NotifyIcon.BalloonWindow balloon_window;
5431                 
5432                 public override void ShowBalloonWindow (IntPtr handle, int timeout, string title, string text, ToolTipIcon icon)
5433                 {
5434                         Control control = Control.FromHandle(handle);
5435                         
5436                         if (control == null)
5437                                 return;
5438
5439                         if (balloon_window != null) {
5440                                 balloon_window.Close ();
5441                                 balloon_window.Dispose ();
5442                         }
5443
5444                         balloon_window = new NotifyIcon.BalloonWindow (handle);
5445                         balloon_window.Title = title;
5446                         balloon_window.Text = text;
5447                         balloon_window.Icon = icon;
5448                         balloon_window.Timeout = timeout;
5449                         balloon_window.Show ();
5450                 }
5451
5452                 public override void HideBalloonWindow (IntPtr handle)
5453                 {
5454                         if (balloon_window == null || balloon_window.OwnerHandle != handle)
5455                                 return;
5456
5457                         balloon_window.Close ();
5458                         balloon_window.Dispose ();
5459                         balloon_window = null;
5460                 }
5461
5462                 private const int balloon_iconsize = 16;
5463                 private const int balloon_bordersize = 8; 
5464                 
5465                 public override void DrawBalloonWindow (Graphics dc, Rectangle clip, NotifyIcon.BalloonWindow control) 
5466                 {
5467                         Brush solidbrush = ResPool.GetSolidBrush (this.ColorInfoText);
5468                         Rectangle rect = control.ClientRectangle;
5469                         int iconsize = (control.Icon == ToolTipIcon.None) ? 0 : balloon_iconsize;
5470                         
5471                         // Rectangle borders and background.
5472                         dc.FillRectangle (ResPool.GetSolidBrush (ColorInfo), rect);
5473                         dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), 0, 0, rect.Width - 1, rect.Height - 1);
5474
5475                         // Icon
5476                         Image image;
5477                         switch (control.Icon) {
5478                                 case ToolTipIcon.Info: {
5479                                         image = ThemeEngine.Current.Images(UIIcon.MessageBoxInfo, balloon_iconsize);
5480                                         break;
5481                                 }
5482
5483                                 case ToolTipIcon.Warning: {
5484                                         image = ThemeEngine.Current.Images(UIIcon.MessageBoxError, balloon_iconsize);
5485                                         break;
5486                                 }
5487
5488                                 case ToolTipIcon.Error: {
5489                                         image = ThemeEngine.Current.Images(UIIcon.MessageBoxWarning, balloon_iconsize);
5490                                         break;
5491                                 }
5492                                 
5493                                 default: {
5494                                         image = null;
5495                                         break;
5496                                 }
5497                         }
5498
5499                         if (control.Icon != ToolTipIcon.None)
5500                                 dc.DrawImage (image, new Rectangle (balloon_bordersize, balloon_bordersize, iconsize, iconsize));
5501                         
5502                         // Title
5503                         Rectangle titlerect = new Rectangle (rect.X + balloon_bordersize + iconsize + (iconsize > 0 ? balloon_bordersize : 0), 
5504                                                                                                 rect.Y + balloon_bordersize, 
5505                                                                                                 rect.Width - ((3 * balloon_bordersize) + iconsize), 
5506                                                                                                 rect.Height - (2 * balloon_bordersize));
5507                         
5508                         Font titlefont = new Font (control.Font.FontFamily, control.Font.Size, control.Font.Style | FontStyle.Bold, control.Font.Unit);
5509                         dc.DrawString (control.Title, titlefont, solidbrush, titlerect, control.Format);
5510                         
5511                         // Text
5512                         Rectangle textrect = new Rectangle (rect.X + balloon_bordersize, 
5513                                                                                                 rect.Y + balloon_bordersize, 
5514                                                                                                 rect.Width - (2 * balloon_bordersize), 
5515                                                                                                 rect.Height - (2 * balloon_bordersize));
5516
5517                         StringFormat textformat = control.Format;
5518                         textformat.LineAlignment = StringAlignment.Far;
5519                         dc.DrawString (control.Text, control.Font, solidbrush, textrect, textformat);
5520                 }
5521
5522                 public override Rectangle BalloonWindowRect (NotifyIcon.BalloonWindow control)
5523                 {
5524                         Rectangle deskrect = Screen.GetWorkingArea (control);
5525                         SizeF maxsize = new SizeF (250, 200);
5526
5527                         SizeF titlesize = TextRenderer.MeasureString (control.Title, control.Font, maxsize, control.Format);
5528                         SizeF textsize = TextRenderer.MeasureString (control.Text, control.Font, maxsize, control.Format);
5529                         
5530                         if (titlesize.Height < balloon_iconsize)
5531                                 titlesize.Height = balloon_iconsize;
5532                         
5533                         Rectangle rect = new Rectangle ();
5534                         rect.Height = (int) (titlesize.Height + textsize.Height + (3 * balloon_bordersize));
5535                         rect.Width = (int) ((titlesize.Width > textsize.Width) ? titlesize.Width : textsize.Width) + (2 * balloon_bordersize);
5536                         rect.X = deskrect.Width - rect.Width - 2;
5537                         rect.Y = deskrect.Height - rect.Height - 2;
5538                         
5539                         return rect;
5540                 }
5541                 #endregion      // BalloonWindow
5542
5543                 #region TrackBar
5544                 public override int TrackBarValueFromMousePosition (int x, int y, TrackBar tb)
5545                 {
5546                         int result = tb.Value;
5547                         int value_pos = tb.Value;
5548                         float pixels_betweenticks;
5549                         Rectangle thumb_pos = Rectangle.Empty, thumb_area = Rectangle.Empty;
5550                         Point channel_startpoint = Point.Empty, na_point = Point.Empty;
5551                         
5552                         GetTrackBarDrawingInfo (tb, out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out na_point, out na_point);
5553                         
5554                         /* Convert thumb position from mouse position to value*/
5555                         if (tb.Orientation == Orientation.Vertical) {
5556                                 value_pos = (int)Math.Round (((thumb_area.Bottom - y - (float)thumb_pos.Height / 2) / (float)pixels_betweenticks), 0);
5557
5558                                 if (value_pos + tb.Minimum > tb.Maximum)
5559                                         value_pos = tb.Maximum - tb.Minimum;
5560                                 else if (value_pos + tb.Minimum < tb.Minimum)
5561                                         value_pos = 0;
5562
5563                                 result = value_pos + tb.Minimum;
5564                         } else {
5565                                 value_pos = (int)Math.Round (((x - channel_startpoint.X - (float)thumb_pos.Width / 2) / (float) pixels_betweenticks), 0);
5566
5567                                 if (value_pos + tb.Minimum > tb.Maximum)
5568                                         value_pos = tb.Maximum - tb.Minimum;
5569                                 else if (value_pos + tb.Minimum < tb.Minimum)
5570                                         value_pos = 0;
5571
5572                                 result = value_pos + tb.Minimum;
5573                         }
5574                         
5575                         return result;
5576                 }
5577                 
5578                 private void GetTrackBarDrawingInfo (TrackBar tb, out float pixels_betweenticks, out Rectangle thumb_area, out Rectangle thumb_pos, out Point channel_startpoint, out Point bottomtick_startpoint, out Point toptick_startpoint)
5579                 {
5580                         thumb_area = Rectangle.Empty;
5581                         thumb_pos = Rectangle.Empty;
5582                         
5583                         if (tb.Orientation == Orientation.Vertical) {
5584                                 toptick_startpoint = new Point ();
5585                                 bottomtick_startpoint = new Point ();
5586                                 channel_startpoint = new Point ();
5587                                 float pixel_len;
5588                                 const int space_from_right = 8;
5589                                 const int space_from_left = 8;
5590                                 const int space_from_bottom = 11;
5591                                 Rectangle area = tb.ClientRectangle;
5592
5593                                 switch (tb.TickStyle) {
5594                                 case TickStyle.BottomRight:
5595                                 case TickStyle.None:
5596                                         channel_startpoint.Y = 8;
5597                                         channel_startpoint.X = 9;
5598                                         bottomtick_startpoint.Y = 13;
5599                                         bottomtick_startpoint.X = 24;
5600                                         break;
5601                                 case TickStyle.TopLeft:
5602                                         channel_startpoint.Y = 8;
5603                                         channel_startpoint.X = 19;
5604                                         toptick_startpoint.Y = 13;
5605                                         toptick_startpoint.X = 8;
5606                                         break;
5607                                 case TickStyle.Both:
5608                                         channel_startpoint.Y = 8;
5609                                         channel_startpoint.X = 18;
5610                                         bottomtick_startpoint.Y = 13;
5611                                         bottomtick_startpoint.X = 32;
5612                                         toptick_startpoint.Y = 13;
5613                                         toptick_startpoint.X = 8;
5614                                         break;
5615                                 default:
5616                                         break;
5617                                 }
5618
5619                                 thumb_area.X = area.X + channel_startpoint.X;
5620                                 thumb_area.Y = area.Y + channel_startpoint.Y;
5621                                 thumb_area.Height = area.Height - space_from_right - space_from_left;
5622                                 thumb_area.Width = TrackBarVerticalTrackWidth;
5623
5624                                 pixel_len = thumb_area.Height - 11;
5625                                 if (tb.Maximum == tb.Minimum) {
5626                                         pixels_betweenticks = 0;
5627                                 } else {
5628                                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
5629                                 }
5630
5631                                 thumb_pos.Y = thumb_area.Bottom - space_from_bottom - (int)(pixels_betweenticks * (float)(tb.Value - tb.Minimum));
5632                         } else {        
5633                                 toptick_startpoint = new Point ();
5634                                 bottomtick_startpoint = new Point ();
5635                                 channel_startpoint = new Point ();
5636                                 float pixel_len;
5637                                 const int space_from_right = 8;
5638                                 const int space_from_left = 8;
5639                                 Rectangle area = tb.ClientRectangle;
5640                                                         
5641                                 switch (tb.TickStyle) {
5642                                 case TickStyle.BottomRight:
5643                                 case TickStyle.None:
5644                                         channel_startpoint.X = 8;
5645                                         channel_startpoint.Y = 9;
5646                                         bottomtick_startpoint.X = 13;
5647                                         bottomtick_startpoint.Y = 24;                           
5648                                         break;
5649                                 case TickStyle.TopLeft:
5650                                         channel_startpoint.X = 8;
5651                                         channel_startpoint.Y = 19;
5652                                         toptick_startpoint.X = 13;
5653                                         toptick_startpoint.Y = 8;
5654                                         break;
5655                                 case TickStyle.Both:
5656                                         channel_startpoint.X = 8;
5657                                         channel_startpoint.Y = 18;      
5658                                         bottomtick_startpoint.X = 13;
5659                                         bottomtick_startpoint.Y = 32;                           
5660                                         toptick_startpoint.X = 13;
5661                                         toptick_startpoint.Y = 8;                               
5662                                         break;
5663                                 default:
5664                                         break;
5665                                 }
5666                                                         
5667                                 thumb_area.X = area.X + channel_startpoint.X;
5668                                 thumb_area.Y = area.Y + channel_startpoint.Y;
5669                                 thumb_area.Width = area.Width - space_from_right - space_from_left;
5670                                 thumb_area.Height = TrackBarHorizontalTrackHeight;
5671
5672                                 pixel_len = thumb_area.Width - 11;
5673                                 if (tb.Maximum == tb.Minimum) {
5674                                         pixels_betweenticks = 0;
5675                                 } else {
5676                                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
5677                                 }
5678
5679                                 thumb_pos.X = channel_startpoint.X + (int)(pixels_betweenticks * (float) (tb.Value - tb.Minimum));
5680                         }
5681
5682                         thumb_pos.Size = TrackBarGetThumbSize (tb);
5683                 }
5684
5685                 protected virtual Size TrackBarGetThumbSize (TrackBar trackBar)
5686                 {
5687                         return TrackBarGetThumbSize ();
5688                 }
5689
5690                 public static Size TrackBarGetThumbSize ()
5691                 {
5692                         /* Draw thumb fixed 10x22 size */
5693                         return new Size (10, 22);
5694                 }
5695
5696                 public const int TrackBarVerticalTrackWidth = 4;
5697
5698                 public const int TrackBarHorizontalTrackHeight = 4;
5699
5700                 #region Ticks
5701                 protected interface ITrackBarTickPainter
5702                 {
5703                         void Paint (float x1, float y1, float x2, float y2);
5704                 }
5705
5706                 class TrackBarTickPainter : ITrackBarTickPainter
5707                 {
5708                         readonly Graphics g;
5709                         readonly Pen pen;
5710                         public TrackBarTickPainter (Graphics g, Pen pen)
5711                         {
5712                                 this.g = g;
5713                                 this.pen = pen;
5714                         }
5715                         public void Paint (float x1, float y1, float x2, float y2)
5716                         {
5717                                 g.DrawLine (pen, x1, y1, x2, y2);
5718                         }
5719                 }
5720                 protected virtual ITrackBarTickPainter GetTrackBarTickPainter (Graphics g)
5721                 {
5722                         return new TrackBarTickPainter (g, ResPool.GetPen (pen_ticks_color));
5723                 }
5724                 #endregion
5725
5726                 #region DrawTrackBar_Vertical
5727                 private void DrawTrackBar_Vertical (Graphics dc, Rectangle clip_rectangle, TrackBar tb,
5728                         ref Rectangle thumb_pos, ref Rectangle thumb_area,  Brush br_thumb,
5729                         float ticks, int value_pos, bool mouse_value) {                 
5730
5731                         Point toptick_startpoint = new Point ();
5732                         Point bottomtick_startpoint = new Point ();
5733                         Point channel_startpoint = new Point ();
5734                         float pixel_len;
5735                         float pixels_betweenticks;
5736                         Rectangle area = tb.ClientRectangle;
5737                         
5738                         GetTrackBarDrawingInfo (tb, out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out bottomtick_startpoint, out toptick_startpoint);
5739
5740                         #region Track
5741                         TrackBarDrawVerticalTrack (dc, thumb_area, channel_startpoint, clip_rectangle);
5742                         #endregion
5743
5744                         #region Thumb
5745                         switch (tb.TickStyle)   {
5746                         case TickStyle.BottomRight:
5747                         case TickStyle.None:
5748                                 thumb_pos.X = channel_startpoint.X - 8;
5749                                 TrackBarDrawVerticalThumbRight (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5750                                 break;
5751                         case TickStyle.TopLeft:
5752                                 thumb_pos.X = channel_startpoint.X - 10;
5753                                 TrackBarDrawVerticalThumbLeft (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5754                                 break;
5755                         default:
5756                                 thumb_pos.X = area.X + 10;
5757                                 TrackBarDrawVerticalThumb (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5758                                 break;
5759                         }
5760                         #endregion
5761
5762                         pixel_len = thumb_area.Height - 11;
5763                         pixels_betweenticks = pixel_len / ticks;
5764                         
5765                         thumb_area.X = thumb_pos.X;
5766                         thumb_area.Y = channel_startpoint.Y;
5767                         thumb_area.Width = thumb_pos.Height;
5768
5769                         #region Ticks
5770                         if (pixels_betweenticks <= 0)
5771                                 return;
5772                         if (tb.TickStyle == TickStyle.None)
5773                                 return;
5774                         Region outside = new Region (area);
5775                         outside.Exclude (thumb_area);                   
5776                         
5777                         if (outside.IsVisible (clip_rectangle)) {
5778                                 ITrackBarTickPainter tick_painter = TrackBarGetVerticalTickPainter (dc);
5779
5780                                 if ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight) {
5781                                         float x = area.X + bottomtick_startpoint.X;
5782                                         for (float inc = 0; inc < pixel_len + 1; inc += pixels_betweenticks)    {
5783                                                 float y = area.Y + bottomtick_startpoint.Y + inc;
5784                                                 tick_painter.Paint (
5785                                                         x, y,
5786                                                         x + (inc == 0 || inc + pixels_betweenticks >= pixel_len + 1 ? 3 : 2), y);
5787                                         }
5788                                 }
5789
5790                                 if ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft) {
5791                                         float x = area.X + toptick_startpoint.X; 
5792                                         for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
5793                                                 float y = area.Y + toptick_startpoint.Y + inc;
5794                                                 tick_painter.Paint (
5795                                                         x - (inc == 0 || inc + pixels_betweenticks >= pixel_len + 1 ? 3 : 2), y,
5796                                                         x, y);
5797                                         }                       
5798                                 }
5799                         }
5800                         
5801                         outside.Dispose ();
5802                         #endregion
5803                 }
5804
5805                 #region Track
5806                 protected virtual void TrackBarDrawVerticalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
5807                 {
5808                         dc.FillRectangle (SystemBrushes.ControlDark, channel_startpoint.X, channel_startpoint.Y,
5809                                 1, thumb_area.Height);
5810
5811                         dc.FillRectangle (SystemBrushes.ControlDarkDark, channel_startpoint.X + 1, channel_startpoint.Y,
5812                                 1, thumb_area.Height);
5813
5814                         dc.FillRectangle (SystemBrushes.ControlLight, channel_startpoint.X + 3, channel_startpoint.Y,
5815                                 1, thumb_area.Height);
5816                 }
5817                 #endregion
5818
5819                 #region Thumb
5820                 protected virtual void TrackBarDrawVerticalThumbRight (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
5821                 {
5822                         Pen pen = SystemPens.ControlLightLight;
5823                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 10);
5824                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 16, thumb_pos.Y);
5825                         dc.DrawLine (pen, thumb_pos.X + 16, thumb_pos.Y, thumb_pos.X + 16 + 4, thumb_pos.Y + 4);
5826
5827                         pen = SystemPens.ControlDark;
5828                         dc.DrawLine (pen, thumb_pos.X + 1, thumb_pos.Y + 9, thumb_pos.X + 15, thumb_pos.Y + 9);
5829                         dc.DrawLine (pen, thumb_pos.X + 16, thumb_pos.Y + 9, thumb_pos.X + 16 + 4, thumb_pos.Y + 9 - 4);
5830
5831                         pen = SystemPens.ControlDarkDark;
5832                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 10, thumb_pos.X + 16, thumb_pos.Y + 10);
5833                         dc.DrawLine (pen, thumb_pos.X + 16, thumb_pos.Y + 10, thumb_pos.X + 16 + 5, thumb_pos.Y + 10 - 5);
5834
5835                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 16, 8);
5836                         dc.FillRectangle (br_thumb, thumb_pos.X + 17, thumb_pos.Y + 2, 1, 6);
5837                         dc.FillRectangle (br_thumb, thumb_pos.X + 18, thumb_pos.Y + 3, 1, 4);
5838                         dc.FillRectangle (br_thumb, thumb_pos.X + 19, thumb_pos.Y + 4, 1, 2);
5839                 }
5840
5841                 protected virtual void TrackBarDrawVerticalThumbLeft (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
5842                 {
5843                         Pen pen = SystemPens.ControlLightLight;
5844                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X + 4 + 16, thumb_pos.Y);
5845                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 4);
5846
5847                         pen = SystemPens.ControlDark;
5848                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y + 9, thumb_pos.X + 4 + 16, thumb_pos.Y + 9);
5849                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y + 9, thumb_pos.X, thumb_pos.Y + 5);
5850                         dc.DrawLine (pen, thumb_pos.X + 19, thumb_pos.Y + 9, thumb_pos.X + 19, thumb_pos.Y + 1);
5851
5852                         pen = SystemPens.ControlDarkDark;
5853                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y + 10, thumb_pos.X + 4 + 16, thumb_pos.Y + 10);
5854                         dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y + 10, thumb_pos.X - 1, thumb_pos.Y + 5);
5855                         dc.DrawLine (pen, thumb_pos.X + 20, thumb_pos.Y, thumb_pos.X + 20, thumb_pos.Y + 10);
5856
5857                         dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 15, 8);
5858                         dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 1, 6);
5859                         dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 1, 4);
5860                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 1, 2);
5861                 }
5862
5863                 protected virtual void TrackBarDrawVerticalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
5864                 {
5865                         Pen pen = SystemPens.ControlLightLight;
5866                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 9);
5867                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 19, thumb_pos.Y);
5868
5869                         pen = SystemPens.ControlDark;
5870                         dc.DrawLine (pen, thumb_pos.X + 1, thumb_pos.Y + 9, thumb_pos.X + 19, thumb_pos.Y + 9);
5871                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y + 1, thumb_pos.X + 19, thumb_pos.Y + 8);
5872
5873                         pen = SystemPens.ControlDarkDark;
5874                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 10, thumb_pos.X + 20, thumb_pos.Y + 10);
5875                         dc.DrawLine (pen, thumb_pos.X + 20, thumb_pos.Y, thumb_pos.X + 20, thumb_pos.Y + 9);
5876
5877                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 18, 8);
5878                 }
5879                 #endregion
5880
5881                 #region Ticks
5882                 protected virtual ITrackBarTickPainter TrackBarGetVerticalTickPainter (Graphics g)
5883                 {
5884                         return GetTrackBarTickPainter (g);
5885                 }
5886                 #endregion
5887                 #endregion
5888
5889                 #region DrawTrackBar_Horizontal
5890                 /* 
5891                         Horizontal trackbar 
5892                   
5893                         Does not matter the size of the control, Win32 always draws:
5894                                 - Ticks starting from pixel 13, 8
5895                                 - Channel starting at pos 8, 19 and ends at Width - 8
5896                                 - Autosize makes always the control 45 pixels high
5897                                 - Ticks are draw at (channel.Witdh - 10) / (Maximum - Minimum)
5898                                 
5899                 */
5900                 private void DrawTrackBar_Horizontal (Graphics dc, Rectangle clip_rectangle, TrackBar tb,
5901                         ref Rectangle thumb_pos, ref Rectangle thumb_area, Brush br_thumb,
5902                         float ticks, int value_pos, bool mouse_value) {                 
5903                         Point toptick_startpoint = new Point ();
5904                         Point bottomtick_startpoint = new Point ();
5905                         Point channel_startpoint = new Point ();
5906                         float pixel_len;
5907                         float pixels_betweenticks;
5908                         Rectangle area = tb.ClientRectangle;
5909                         
5910                         GetTrackBarDrawingInfo (tb , out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out bottomtick_startpoint, out toptick_startpoint);
5911
5912                         #region Track
5913                         TrackBarDrawHorizontalTrack (dc, thumb_area, channel_startpoint, clip_rectangle);
5914                         #endregion
5915
5916                         #region Thumb
5917                         switch (tb.TickStyle) {
5918                         case TickStyle.BottomRight:
5919                         case TickStyle.None:
5920                                 thumb_pos.Y = channel_startpoint.Y - 8;
5921                                 TrackBarDrawHorizontalThumbBottom (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5922                                 break;
5923                         case TickStyle.TopLeft:
5924                                 thumb_pos.Y = channel_startpoint.Y - 10;
5925                                 TrackBarDrawHorizontalThumbTop (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5926                                 break;
5927                         default:
5928                                 thumb_pos.Y = area.Y + 10;
5929                                 TrackBarDrawHorizontalThumb (dc, thumb_pos, br_thumb, clip_rectangle, tb);
5930                                 break;
5931                         }
5932                         #endregion
5933
5934                         pixel_len = thumb_area.Width - 11;
5935                         pixels_betweenticks = pixel_len / ticks;
5936
5937                         thumb_area.Y = thumb_pos.Y;
5938                         thumb_area.X = channel_startpoint.X;
5939                         thumb_area.Height = thumb_pos.Height;
5940                         #region Ticks
5941                         if (pixels_betweenticks <= 0)
5942                                 return;
5943                         if (tb.TickStyle == TickStyle.None)
5944                                 return;
5945                         Region outside = new Region (area);
5946                         outside.Exclude (thumb_area);
5947
5948                         if (outside.IsVisible (clip_rectangle)) {
5949                                 ITrackBarTickPainter tick_painter = TrackBarGetHorizontalTickPainter (dc);
5950
5951                                 if ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight) {
5952                                         float y = area.Y + bottomtick_startpoint.Y;
5953                                         for (float inc = 0; inc < pixel_len + 1; inc += pixels_betweenticks) {                                  
5954                                                 float x = area.X + bottomtick_startpoint.X + inc;
5955                                                 tick_painter.Paint (
5956                                                         x, y, 
5957                                                         x, y + (inc == 0 || inc + pixels_betweenticks >= pixel_len + 1 ? 3 : 2));
5958                                         }
5959                                 }
5960
5961                                 if ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft) {
5962                                         float y = area.Y + toptick_startpoint.Y;
5963                                         for (float inc = 0; inc < pixel_len + 1; inc += pixels_betweenticks) {                                  
5964                                                 float x = area.X + toptick_startpoint.X + inc;
5965                                                 tick_painter.Paint (
5966                                                         x, y - (inc == 0 || (inc + pixels_betweenticks) >= pixel_len + 1 ? 3 : 2), 
5967                                                         x, y);
5968                                         }                       
5969                                 }
5970                         }
5971                         
5972                         outside.Dispose ();
5973                         #endregion
5974                 }
5975
5976                 #region Track
5977                 protected virtual void TrackBarDrawHorizontalTrack (Graphics dc, Rectangle thumb_area, Point channel_startpoint, Rectangle clippingArea)
5978                 {
5979                         dc.FillRectangle (SystemBrushes.ControlDark, channel_startpoint.X, channel_startpoint.Y,
5980                                 thumb_area.Width, 1);
5981
5982                         dc.FillRectangle (SystemBrushes.ControlDarkDark, channel_startpoint.X, channel_startpoint.Y + 1,
5983                                 thumb_area.Width, 1);
5984
5985                         dc.FillRectangle (SystemBrushes.ControlLight, channel_startpoint.X, channel_startpoint.Y + 3,
5986                                 thumb_area.Width, 1);
5987                 }
5988                 #endregion
5989
5990                 #region Thumb
5991                 protected virtual void TrackBarDrawHorizontalThumbBottom (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
5992                 {
5993                         Pen pen = SystemPens.ControlLightLight;
5994                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y);
5995                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 16);
5996                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 16, thumb_pos.X + 4, thumb_pos.Y + 16 + 4);
5997
5998                         pen = SystemPens.ControlDark;
5999                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X + 9, thumb_pos.Y + 15);
6000                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 16, thumb_pos.X + 9 - 4, thumb_pos.Y + 16 + 4);
6001
6002                         pen = SystemPens.ControlDarkDark;
6003                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y + 16);
6004                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y + 16, thumb_pos.X + 10 - 5, thumb_pos.Y + 16 + 5);
6005
6006                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 16);
6007                         dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 17, 6, 1);
6008                         dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 18, 4, 1);
6009                         dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 19, 2, 1);
6010                 }
6011
6012                 protected virtual void TrackBarDrawHorizontalThumbTop (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
6013                 {
6014                         Pen pen = SystemPens.ControlLightLight;
6015                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X, thumb_pos.Y + 4 + 16);
6016                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X + 4, thumb_pos.Y);
6017
6018                         pen = SystemPens.ControlDark;
6019                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 9, thumb_pos.Y + 4 + 16);
6020                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y);
6021                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 19, thumb_pos.X + 1, thumb_pos.Y + 19);
6022
6023                         pen = SystemPens.ControlDarkDark;
6024                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 10, thumb_pos.Y + 4 + 16);
6025                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y - 1);
6026                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 10, thumb_pos.Y + 20);
6027
6028                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 8, 15);
6029                         dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 6, 1);
6030                         dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 4, 1);
6031                         dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 2, 1);
6032                 }
6033
6034                 protected virtual void TrackBarDrawHorizontalThumb (Graphics dc, Rectangle thumb_pos, Brush br_thumb, Rectangle clippingArea, TrackBar trackBar)
6035                 {
6036                         Pen pen = SystemPens.ControlLightLight;
6037                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 9, thumb_pos.Y);
6038                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 19);
6039
6040                         pen = SystemPens.ControlDark;
6041                         dc.DrawLine (pen, thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X + 9, thumb_pos.Y + 19);
6042                         dc.DrawLine (pen, thumb_pos.X + 1, thumb_pos.Y + 10, thumb_pos.X + 8, thumb_pos.Y + 19);
6043
6044                         pen = SystemPens.ControlDarkDark;
6045                         dc.DrawLine (pen, thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y + 20);
6046                         dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 9, thumb_pos.Y + 20);
6047
6048                         dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 18);
6049                 }
6050                 #endregion
6051
6052                 #region Ticks
6053                 protected virtual ITrackBarTickPainter TrackBarGetHorizontalTickPainter (Graphics g)
6054                 {
6055                         return GetTrackBarTickPainter (g);
6056                 }
6057                 #endregion
6058                 #endregion
6059
6060                 public override void DrawTrackBar (Graphics dc, Rectangle clip_rectangle, TrackBar tb) 
6061                 {
6062                         Brush           br_thumb;
6063                         int             value_pos;
6064                         bool            mouse_value;
6065                         float           ticks = (tb.Maximum - tb.Minimum) / tb.tickFrequency; /* N of ticks draw*/
6066                         Rectangle       area;
6067                         Rectangle       thumb_pos = tb.ThumbPos;
6068                         Rectangle       thumb_area = tb.ThumbArea;
6069                         
6070                         if (tb.thumb_pressed) {
6071                                 value_pos = tb.thumb_mouseclick;
6072                                 mouse_value = true;
6073                         } else {
6074                                 value_pos = tb.Value - tb.Minimum;
6075                                 mouse_value = false;
6076                         }
6077
6078                         area = tb.ClientRectangle;
6079
6080                         if (!tb.Enabled) {
6081                                 br_thumb = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLightLight, ColorControlLight);
6082                         } else if (tb.thumb_pressed == true) {
6083                                 br_thumb = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLight, ColorControl);
6084                         } else {
6085                                 br_thumb = SystemBrushes.Control;
6086                         }
6087
6088                         
6089                         /* Control Background */
6090                         if (tb.BackColor.ToArgb () == DefaultControlBackColor.ToArgb ()) {
6091                                 dc.FillRectangle (SystemBrushes.Control, clip_rectangle);
6092                         } else {
6093                                 dc.FillRectangle (ResPool.GetSolidBrush (tb.BackColor), clip_rectangle);
6094                         }
6095                         
6096                         if (tb.Focused) {
6097                                 CPDrawFocusRectangle(dc, area, tb.ForeColor, tb.BackColor);
6098                         }
6099
6100                         if (tb.Orientation == Orientation.Vertical) {
6101                                 DrawTrackBar_Vertical (dc, clip_rectangle, tb, ref thumb_pos, ref thumb_area,
6102                                         br_thumb, ticks, value_pos, mouse_value);
6103                         
6104                         } else {
6105                                 DrawTrackBar_Horizontal (dc, clip_rectangle, tb, ref thumb_pos, ref thumb_area,
6106                                         br_thumb, ticks, value_pos, mouse_value);
6107                         }
6108
6109                         tb.ThumbPos = thumb_pos;
6110                         tb.ThumbArea = thumb_area;
6111                 }
6112
6113                 public override Size TrackBarDefaultSize {
6114                         get {
6115                                 return new Size (104, 42);
6116                         }
6117                 }
6118
6119                 public override bool TrackBarHasHotThumbStyle {
6120                         get {
6121                                 return false;
6122                         }
6123                 }
6124                 #endregion      // TrackBar
6125
6126                 #region UpDownBase
6127                 public override void UpDownBaseDrawButton (Graphics g, Rectangle bounds, bool top, VisualStyles.PushButtonState state)
6128                 {
6129                         ControlPaint.DrawScrollButton (g, bounds, top ? ScrollButton.Up : ScrollButton.Down, state == VisualStyles.PushButtonState.Pressed ? ButtonState.Pushed : ButtonState.Normal);
6130                 }
6131
6132                 public override bool UpDownBaseHasHotButtonStyle {
6133                         get {
6134                                 return false;
6135                         }
6136                 }
6137                 #endregion
6138
6139                 #region VScrollBar
6140                 public override Size VScrollBarDefaultSize {
6141                         get {
6142                                 return new Size (this.ScrollBarButtonSize, 80);
6143                         }
6144                 }
6145                 #endregion      // VScrollBar
6146
6147                 #region TreeView
6148                 public override Size TreeViewDefaultSize {
6149                         get {
6150                                 return new Size (121, 97);
6151                         }
6152                 }
6153
6154                 public override void TreeViewDrawNodePlusMinus (TreeView treeView, TreeNode node, Graphics dc, int x, int middle)
6155                 {
6156                         int height = treeView.ActualItemHeight - 2;
6157                         dc.FillRectangle (ResPool.GetSolidBrush (treeView.BackColor), (x + 4) - (height / 2), node.GetY() + 1, height, height);
6158                         
6159                         dc.DrawRectangle (SystemPens.ControlDarkDark, x, middle - 4, 8, 8);
6160
6161                         if (node.IsExpanded) {
6162                                 dc.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle); 
6163                         } else {
6164                                 dc.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
6165                                 dc.DrawLine (SystemPens.ControlDarkDark, x + 4, middle - 2, x + 4, middle + 2);
6166                         }
6167                 }
6168                 #endregion
6169
6170                 #region Managed window
6171                 public override int ManagedWindowTitleBarHeight (InternalWindowManager wm)
6172                 {
6173                         if (wm.IsToolWindow && !wm.IsMinimized)
6174                                 return SystemInformation.ToolWindowCaptionHeight;
6175                         if (wm.Form.FormBorderStyle == FormBorderStyle.None)
6176                                 return 0;
6177                         return SystemInformation.CaptionHeight;
6178                 }
6179
6180                 public override int ManagedWindowBorderWidth (InternalWindowManager wm)
6181                 {
6182                         if ((wm.IsToolWindow && wm.form.FormBorderStyle == FormBorderStyle.FixedToolWindow) ||
6183                                 wm.IsMinimized)
6184                                 return 3;
6185                         else
6186                                 return 4;
6187                 }
6188
6189                 public override int ManagedWindowIconWidth (InternalWindowManager wm)
6190                 {
6191                         return ManagedWindowTitleBarHeight (wm) - 5;
6192                 }
6193
6194                 public override void ManagedWindowSetButtonLocations (InternalWindowManager wm)
6195                 {
6196                         TitleButtons buttons = wm.TitleButtons;
6197                         Form form = wm.form;
6198                         
6199                         buttons.HelpButton.Visible = form.HelpButton;
6200                         
6201                         foreach (TitleButton button in buttons) {
6202                                 button.Visible = false;
6203                         }
6204                         
6205                         switch (form.FormBorderStyle) {
6206                         case FormBorderStyle.None:
6207                                 if (form.WindowState != FormWindowState.Normal)
6208                                         goto case FormBorderStyle.Sizable;
6209                                 break;
6210                         case FormBorderStyle.FixedToolWindow:
6211                         case FormBorderStyle.SizableToolWindow:
6212                                 buttons.CloseButton.Visible = true;
6213                                 if (form.WindowState != FormWindowState.Normal)
6214                                         goto case FormBorderStyle.Sizable;
6215                                 break;
6216                         case FormBorderStyle.FixedSingle:
6217                         case FormBorderStyle.Fixed3D:
6218                         case FormBorderStyle.FixedDialog:
6219                         case FormBorderStyle.Sizable:
6220                                 switch (form.WindowState) {
6221                                         case FormWindowState.Normal:
6222                                                 buttons.MinimizeButton.Visible = true;
6223                                                 buttons.MaximizeButton.Visible = true;
6224                                                 buttons.RestoreButton.Visible = false;
6225                                                 break;
6226                                         case FormWindowState.Maximized:
6227                                                 buttons.MinimizeButton.Visible = true;
6228                                                 buttons.MaximizeButton.Visible = false;
6229                                                 buttons.RestoreButton.Visible = true;
6230                                                 break;
6231                                         case FormWindowState.Minimized:
6232                                                 buttons.MinimizeButton.Visible = false;
6233                                                 buttons.MaximizeButton.Visible = true;
6234                                                 buttons.RestoreButton.Visible = true;
6235                                                 break;
6236                                 }
6237                                 buttons.CloseButton.Visible = true;
6238                                 break;
6239                         }
6240
6241                         // Respect MinimizeBox/MaximizeBox
6242                         if (form.MinimizeBox == false && form.MaximizeBox == false) {
6243                                 buttons.MinimizeButton.Visible = false;
6244                                 buttons.MaximizeButton.Visible = false;
6245                         } else if (form.MinimizeBox == false)
6246                                 buttons.MinimizeButton.State = ButtonState.Inactive;
6247                         else if (form.MaximizeBox == false)
6248                                 buttons.MaximizeButton.State = ButtonState.Inactive;
6249
6250                         int bw = ManagedWindowBorderWidth (wm);
6251                         Size btsize = ManagedWindowButtonSize (wm);
6252                         int btw = btsize.Width;
6253                         int bth = btsize.Height;
6254                         int top = bw + 2;
6255                         int left = form.Width - bw - btw - ManagedWindowSpacingAfterLastTitleButton;
6256                         
6257                         if ((!wm.IsToolWindow || wm.IsMinimized) && wm.HasBorders) {
6258                                 buttons.CloseButton.Rectangle = new Rectangle (left, top, btw, bth);
6259                                 left -= 2 + btw;
6260                                 
6261                                 if (buttons.MaximizeButton.Visible) {
6262                                         buttons.MaximizeButton.Rectangle = new Rectangle (left, top, btw, bth);
6263                                         left -= 2 + btw;
6264                                 } 
6265                                 if (buttons.RestoreButton.Visible) {
6266                                         buttons.RestoreButton.Rectangle = new Rectangle (left, top, btw, bth);
6267                                         left -= 2 + btw;
6268                                 }
6269
6270                                 buttons.MinimizeButton.Rectangle = new Rectangle (left, top, btw, bth);
6271                                 left -= 2 + btw;
6272                         } else if (wm.IsToolWindow) {
6273                                 buttons.CloseButton.Rectangle = new Rectangle (left, top, btw, bth);
6274                                 left -= 2 + btw;
6275                         }
6276                 }
6277
6278                 protected virtual Rectangle ManagedWindowDrawTitleBarAndBorders (Graphics dc, Rectangle clip, InternalWindowManager wm)
6279                 {
6280                         Form form = wm.Form;
6281                         int tbheight = ManagedWindowTitleBarHeight (wm);
6282                         int bdwidth = ManagedWindowBorderWidth (wm);
6283                         Color titlebar_color = Color.FromArgb (255, 10, 36, 106);
6284                         Color titlebar_color2 = Color.FromArgb (255, 166, 202, 240);
6285                         Color color = ThemeEngine.Current.ColorControlDark;
6286                         Color color2 = Color.FromArgb (255, 192, 192, 192);
6287
6288                         Pen pen = ResPool.GetPen (ColorControl);
6289                         Rectangle borders = new Rectangle (0, 0, form.Width, form.Height);
6290                         ControlPaint.DrawBorder3D (dc, borders, Border3DStyle.Raised);
6291                         // The 3d border is only 2 pixels wide, so we draw the innermost pixels ourselves
6292                         borders = new Rectangle (2, 2, form.Width - 5, form.Height - 5);
6293                         for (int i = 2; i < bdwidth; i++) {
6294                                 dc.DrawRectangle (pen, borders);
6295                                 borders.Inflate (-1, -1);
6296                         }                               
6297
6298
6299                         bool draw_titlebar_enabled = false;
6300                         if (wm.Form.Parent != null && wm.Form.Parent is Form) {
6301                                 draw_titlebar_enabled = false;
6302                         } else if (wm.IsActive && !wm.IsMaximized) {
6303                                 draw_titlebar_enabled = true;
6304                         }
6305                         if (draw_titlebar_enabled) {
6306                                 color = titlebar_color;
6307                                 color2 = titlebar_color2;
6308                         }
6309
6310                         Rectangle tb = new Rectangle (bdwidth, bdwidth, form.Width - (bdwidth * 2), tbheight - 1);
6311
6312                         // HACK: For now always draw the titlebar until we get updates better
6313                         if (tb.Width > 0 && tb.Height > 0) {
6314                                 using (System.Drawing.Drawing2D.LinearGradientBrush gradient = new LinearGradientBrush (tb, color, color2, LinearGradientMode.Horizontal))
6315                                 {
6316                                         dc.FillRectangle (gradient, tb);
6317                                 }       
6318                         }
6319                         
6320                         if (!wm.IsMinimized)
6321                                 // Draw the line just beneath the title bar
6322                                 dc.DrawLine (ResPool.GetPen (SystemColors.Control), bdwidth,
6323                                                 tbheight + bdwidth - 1, form.Width - bdwidth - 1,
6324                                                 tbheight + bdwidth - 1);
6325                         return tb;
6326                 }
6327
6328                 public override void DrawManagedWindowDecorations (Graphics dc, Rectangle clip, InternalWindowManager wm)
6329                 {
6330 #if debug
6331                         Console.WriteLine (DateTime.Now.ToLongTimeString () + " DrawManagedWindowDecorations");
6332                         dc.FillRectangle (Brushes.Black, clip);
6333 #endif
6334                         Rectangle tb = ManagedWindowDrawTitleBarAndBorders (dc, clip, wm);
6335
6336                         Form form = wm.Form;
6337                         if (wm.ShowIcon) {
6338                                 Rectangle icon = ManagedWindowGetTitleBarIconArea (wm);
6339                                 if (icon.IntersectsWith (clip))
6340                                         dc.DrawIcon (form.Icon, icon);
6341                                 const int SpacingBetweenIconAndCaption = 2;
6342                                 tb.Width -= icon.Right + SpacingBetweenIconAndCaption - tb.X ;
6343                                 tb.X = icon.Right + SpacingBetweenIconAndCaption;
6344                         }
6345                         
6346                         foreach (TitleButton button in wm.TitleButtons.AllButtons) {
6347                                 tb.Width -= Math.Max (0, tb.Right - DrawTitleButton (dc, button, clip, form));
6348                         }
6349                         const int SpacingBetweenCaptionAndLeftMostButton = 3;
6350                         tb.Width -= SpacingBetweenCaptionAndLeftMostButton;
6351
6352                         string window_caption = form.Text;
6353                         window_caption = window_caption.Replace (Environment.NewLine, string.Empty);
6354
6355                         if (window_caption != null && window_caption != string.Empty) {
6356                                 StringFormat format = new StringFormat ();
6357                                 format.FormatFlags = StringFormatFlags.NoWrap;
6358                                 format.Trimming = StringTrimming.EllipsisCharacter;
6359                                 format.LineAlignment = StringAlignment.Center;
6360
6361                                 if (tb.IntersectsWith (clip))
6362                                         dc.DrawString (window_caption, WindowBorderFont,
6363                                                 ThemeEngine.Current.ResPool.GetSolidBrush (Color.White),
6364                                                 tb, format);
6365                         }
6366                 }
6367
6368                 public override Size ManagedWindowButtonSize (InternalWindowManager wm)
6369                 {
6370                         int height = ManagedWindowTitleBarHeight (wm);
6371                         if (!wm.IsMaximized && !wm.IsMinimized) {
6372                                 if (wm.IsToolWindow)
6373                                         return new Size (SystemInformation.ToolWindowCaptionButtonSize.Width - 2,
6374                                                         height - 5);
6375                                 if (wm.Form.FormBorderStyle == FormBorderStyle.None)
6376                                         return Size.Empty;
6377                         } else
6378                                 height = SystemInformation.CaptionHeight;
6379
6380                         return new Size (SystemInformation.CaptionButtonSize.Width - 2,
6381                                         height - 5);
6382                 }
6383
6384                 private int DrawTitleButton (Graphics dc, TitleButton button, Rectangle clip, Form form)
6385                 {
6386                         if (!button.Visible) {
6387                                 return int.MaxValue;
6388                         }
6389                         
6390                         if (button.Rectangle.IntersectsWith (clip)) {
6391                                 ManagedWindowDrawTitleButton (dc, button, clip, form);
6392                         }
6393                         return button.Rectangle.Left;
6394                 }
6395
6396                 protected virtual void ManagedWindowDrawTitleButton (Graphics dc, TitleButton button, Rectangle clip, Form form)
6397                 {
6398                         dc.FillRectangle (SystemBrushes.Control, button.Rectangle);
6399
6400                         ControlPaint.DrawCaptionButton (dc, button.Rectangle,
6401                                         button.Caption, button.State);
6402                 }
6403
6404                 public override Rectangle ManagedWindowGetTitleBarIconArea (InternalWindowManager wm)
6405                 {
6406                         int bw = ManagedWindowBorderWidth (wm);
6407                         return new Rectangle (bw + 3, bw + 2, wm.IconWidth, wm.IconWidth);
6408                 }
6409
6410                 public override Size ManagedWindowGetMenuButtonSize (InternalWindowManager wm)
6411                 {
6412                         Size result = SystemInformation.MenuButtonSize;
6413                         result.Width -= 2;
6414                         result.Height -= 4;
6415                         return result;
6416                 }
6417
6418                 public override bool ManagedWindowTitleButtonHasHotElementStyle (TitleButton button, Form form)
6419                 {
6420                         return false;
6421                 }
6422
6423                 public override void ManagedWindowDrawMenuButton (Graphics dc, TitleButton button, Rectangle clip, InternalWindowManager wm)
6424                 {
6425                         dc.FillRectangle (SystemBrushes.Control, button.Rectangle);
6426                         ControlPaint.DrawCaptionButton (dc, button.Rectangle,
6427                                         button.Caption, button.State);
6428                 }
6429
6430                 public override void ManagedWindowOnSizeInitializedOrChanged (Form form)
6431                 {
6432                 }
6433                 #endregion
6434
6435                 #region ControlPaint
6436                 public override void CPDrawBorder (Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
6437                         ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
6438                         Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor,
6439                         int bottomWidth, ButtonBorderStyle bottomStyle) {
6440                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom-1, leftWidth, leftColor, leftStyle, Border3DSide.Left);
6441                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Right-1, bounds.Top, topWidth, topColor, topStyle, Border3DSide.Top);
6442                         DrawBorderInternal(graphics, bounds.Right-1, bounds.Top, bounds.Right-1, bounds.Bottom-1, rightWidth, rightColor, rightStyle, Border3DSide.Right);
6443                         DrawBorderInternal(graphics, bounds.Left, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1, bottomWidth, bottomColor, bottomStyle, Border3DSide.Bottom);
6444                 }
6445
6446                 public override void CPDrawBorder (Graphics graphics, RectangleF bounds, Color leftColor, int leftWidth,
6447                         ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
6448                         Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor,
6449                         int bottomWidth, ButtonBorderStyle bottomStyle) {
6450                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom-1, leftWidth, leftColor, leftStyle, Border3DSide.Left);
6451                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Right-1, bounds.Top, topWidth, topColor, topStyle, Border3DSide.Top);
6452                         DrawBorderInternal(graphics, bounds.Right-1, bounds.Top, bounds.Right-1, bounds.Bottom-1, rightWidth, rightColor, rightStyle, Border3DSide.Right);
6453                         DrawBorderInternal(graphics, bounds.Left, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1, bottomWidth, bottomColor, bottomStyle, Border3DSide.Bottom);
6454                 }
6455
6456                 public override void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
6457                         CPDrawBorder3D(graphics, rectangle, style, sides, ColorControl);
6458                 }
6459
6460                 public override void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides, Color control_color)
6461                 {
6462                         Pen             penTopLeft;
6463                         Pen             penTopLeftInner;
6464                         Pen             penBottomRight;
6465                         Pen             penBottomRightInner;
6466                         Rectangle       rect= new Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
6467                         bool is_ColorControl = control_color.ToArgb () == ColorControl.ToArgb () ? true : false;
6468                         
6469                         if ((style & Border3DStyle.Adjust) != 0) {
6470                                 rect.Y -= 2;
6471                                 rect.X -= 2;
6472                                 rect.Width += 4;
6473                                 rect.Height += 4;
6474                         }
6475                         
6476                         penTopLeft = penTopLeftInner = penBottomRight = penBottomRightInner = is_ColorControl ? SystemPens.Control : ResPool.GetPen (control_color);
6477                         
6478                         CPColor cpcolor = CPColor.Empty;
6479                         
6480                         if (!is_ColorControl)
6481                                 cpcolor = ResPool.GetCPColor (control_color);
6482                         
6483                         switch (style) {
6484                         case Border3DStyle.Raised:
6485                                 penTopLeftInner = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
6486                                 penBottomRight = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
6487                                 penBottomRightInner = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6488                                 break;
6489                         case Border3DStyle.Sunken:
6490                                 penTopLeft = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6491                                 penTopLeftInner = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
6492                                 penBottomRight = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
6493                                 break;
6494                         case Border3DStyle.Etched:
6495                                 penTopLeft = penBottomRightInner = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6496                                 penTopLeftInner = penBottomRight = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
6497                                 break;
6498                         case Border3DStyle.RaisedOuter:
6499                                 penBottomRight = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
6500                                 break;
6501                         case Border3DStyle.SunkenOuter:
6502                                 penTopLeft = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6503                                 penBottomRight = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
6504                                 break;
6505                         case Border3DStyle.RaisedInner:
6506                                 penTopLeft = is_ColorControl ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
6507                                 penBottomRight = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6508                                 break;
6509                         case Border3DStyle.SunkenInner:
6510                                 penTopLeft = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
6511                                 break;
6512                         case Border3DStyle.Flat:
6513                                 penTopLeft = penBottomRight = is_ColorControl ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
6514                                 break;
6515                         case Border3DStyle.Bump:
6516                                 penTopLeftInner = penBottomRight = is_ColorControl ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
6517                                 break;
6518                         default:
6519                                 break;
6520                         }
6521                         
6522                         bool inner = ((style != Border3DStyle.RaisedOuter) && (style != Border3DStyle.SunkenOuter));
6523                         
6524                         if ((sides & Border3DSide.Middle) != 0) {
6525                                 Brush brush = is_ColorControl ? SystemBrushes.Control : ResPool.GetSolidBrush (control_color);
6526                                 graphics.FillRectangle (brush, rect);
6527                         }
6528                         
6529                         if ((sides & Border3DSide.Left) != 0) {
6530                                 graphics.DrawLine (penTopLeft, rect.Left, rect.Bottom - 2, rect.Left, rect.Top);
6531                                 if ((rect.Width > 2) && inner)
6532                                         graphics.DrawLine (penTopLeftInner, rect.Left + 1, rect.Bottom - 2, rect.Left + 1, rect.Top);
6533                         }
6534                         
6535                         if ((sides & Border3DSide.Top) != 0) {
6536                                 graphics.DrawLine (penTopLeft, rect.Left, rect.Top, rect.Right - 2, rect.Top);
6537                                 if ((rect.Height > 2) && inner)
6538                                         graphics.DrawLine (penTopLeftInner, rect.Left + 1, rect.Top + 1, rect.Right - 3, rect.Top + 1);
6539                         }
6540                         
6541                         if ((sides & Border3DSide.Right) != 0) {
6542                                 graphics.DrawLine (penBottomRight, rect.Right - 1, rect.Top, rect.Right - 1, rect.Bottom - 1);
6543                                 if ((rect.Width > 3) && inner)
6544                                         graphics.DrawLine (penBottomRightInner, rect.Right - 2, rect.Top + 1, rect.Right - 2, rect.Bottom - 2);
6545                         }
6546                         
6547                         if ((sides & Border3DSide.Bottom) != 0) {
6548                                 graphics.DrawLine (penBottomRight, rect.Left, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
6549                                 if ((rect.Height > 3) && inner)
6550                                         graphics.DrawLine (penBottomRightInner, rect.Left + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
6551                         }
6552                 }
6553
6554                 public override void CPDrawButton (Graphics dc, Rectangle rectangle, ButtonState state)
6555                 {
6556                         CPDrawButtonInternal (dc, rectangle, state, SystemPens.ControlDarkDark, SystemPens.ControlDark, SystemPens.ControlLight);
6557                 }
6558
6559                 private void CPDrawButtonInternal (Graphics dc, Rectangle rectangle, ButtonState state, Pen DarkPen, Pen NormalPen, Pen LightPen)
6560                 {
6561                         // sadly enough, the rectangle gets always filled with a hatchbrush
6562                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50,
6563                                                                  Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
6564                                                                                  ColorControl.G, ColorControl.B),
6565                                                                  ColorControl),
6566                                           rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2);
6567                         
6568                         if ((state & ButtonState.All) == ButtonState.All || ((state & ButtonState.Checked) == ButtonState.Checked && (state & ButtonState.Flat) == ButtonState.Flat)) {
6569                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLight, ColorControl), rectangle.X + 2, rectangle.Y + 2, rectangle.Width - 4, rectangle.Height - 4);
6570                                 
6571                                 dc.DrawRectangle (SystemPens.ControlDark, rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1);
6572                         } else
6573                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
6574                                 dc.DrawRectangle (SystemPens.ControlDark, rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1);
6575                         } else
6576                         if ((state & ButtonState.Checked) == ButtonState.Checked) {
6577                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLight, ColorControl), rectangle.X + 2, rectangle.Y + 2, rectangle.Width - 4, rectangle.Height - 4);
6578                                 
6579                                 Pen pen = DarkPen;
6580                                 dc.DrawLine (pen, rectangle.X, rectangle.Y, rectangle.X, rectangle.Bottom - 2);
6581                                 dc.DrawLine (pen, rectangle.X + 1, rectangle.Y, rectangle.Right - 2, rectangle.Y);
6582                                 
6583                                 pen = NormalPen;
6584                                 dc.DrawLine (pen, rectangle.X + 1, rectangle.Y + 1, rectangle.X + 1, rectangle.Bottom - 3);
6585                                 dc.DrawLine (pen, rectangle.X + 2, rectangle.Y + 1, rectangle.Right - 3, rectangle.Y + 1);
6586                                 
6587                                 pen = LightPen;
6588                                 dc.DrawLine (pen, rectangle.X, rectangle.Bottom - 1, rectangle.Right - 2, rectangle.Bottom - 1);
6589                                 dc.DrawLine (pen, rectangle.Right - 1, rectangle.Y, rectangle.Right - 1, rectangle.Bottom - 1);
6590                         } else
6591                         if (((state & ButtonState.Pushed) == ButtonState.Pushed) && ((state & ButtonState.Normal) == ButtonState.Normal)) {
6592                                 Pen pen = DarkPen;
6593                                 dc.DrawLine (pen, rectangle.X, rectangle.Y, rectangle.X, rectangle.Bottom - 2);
6594                                 dc.DrawLine (pen, rectangle.X + 1, rectangle.Y, rectangle.Right - 2, rectangle.Y);
6595                                 
6596                                 pen = NormalPen;
6597                                 dc.DrawLine (pen, rectangle.X + 1, rectangle.Y + 1, rectangle.X + 1, rectangle.Bottom - 3);
6598                                 dc.DrawLine (pen, rectangle.X + 2, rectangle.Y + 1, rectangle.Right - 3, rectangle.Y + 1);
6599                                 
6600                                 pen = LightPen;
6601                                 dc.DrawLine (pen, rectangle.X, rectangle.Bottom - 1, rectangle.Right - 2, rectangle.Bottom - 1);
6602                                 dc.DrawLine (pen, rectangle.Right - 1, rectangle.Y, rectangle.Right - 1, rectangle.Bottom - 1);
6603                         } else
6604                         if (((state & ButtonState.Inactive) == ButtonState.Inactive) || ((state & ButtonState.Normal) == ButtonState.Normal)) {
6605                                 Pen pen = LightPen;
6606                                 dc.DrawLine (pen, rectangle.X, rectangle.Y, rectangle.Right - 2, rectangle.Y);
6607                                 dc.DrawLine (pen, rectangle.X, rectangle.Y, rectangle.X, rectangle.Bottom - 2);
6608                                 
6609                                 pen = NormalPen;
6610                                 dc.DrawLine (pen, rectangle.X + 1, rectangle.Bottom - 2, rectangle.Right - 2, rectangle.Bottom - 2);
6611                                 dc.DrawLine (pen, rectangle.Right - 2, rectangle.Y + 1, rectangle.Right - 2, rectangle.Bottom - 3);
6612                                 
6613                                 pen = DarkPen;
6614                                 dc.DrawLine (pen, rectangle.X, rectangle.Bottom - 1, rectangle.Right - 1, rectangle.Bottom - 1);
6615                                 dc.DrawLine (pen, rectangle.Right - 1, rectangle.Y, rectangle.Right - 1, rectangle.Bottom - 2);
6616                         }
6617                 }
6618
6619
6620                 public override void CPDrawCaptionButton (Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
6621                         Rectangle       captionRect;
6622                         int                     lineWidth;
6623
6624                         CPDrawButtonInternal (graphics, rectangle, state, SystemPens.ControlDarkDark, SystemPens.ControlDark, SystemPens.ControlLightLight);
6625
6626                         if (rectangle.Width<rectangle.Height) {
6627                                 captionRect=new Rectangle(rectangle.X+1, rectangle.Y+rectangle.Height/2-rectangle.Width/2+1, rectangle.Width-4, rectangle.Width-4);
6628                         } else {
6629                                 captionRect=new Rectangle(rectangle.X+rectangle.Width/2-rectangle.Height/2+1, rectangle.Y+1, rectangle.Height-4, rectangle.Height-4);
6630                         }
6631
6632                         if ((state & ButtonState.Pushed)!=0) {
6633                                 captionRect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-3, rectangle.Height-3);
6634                         }
6635
6636                         /* Make sure we've got at least a line width of 1 */
6637                         lineWidth=Math.Max(1, captionRect.Width/7);
6638
6639                         switch(button) {
6640                         case CaptionButton.Close: {
6641                                 Pen     pen;
6642
6643                                 if ((state & ButtonState.Inactive)!=0) {
6644                                         pen = ResPool.GetSizedPen (ColorControlLight, lineWidth);
6645                                         DrawCaptionHelper(graphics, ColorControlLight, pen, lineWidth, 1, captionRect, button);
6646
6647                                         pen = ResPool.GetSizedPen (ColorControlDark, lineWidth);
6648                                         DrawCaptionHelper(graphics, ColorControlDark, pen, lineWidth, 0, captionRect, button);
6649                                         return;
6650                                 } else {
6651                                         pen = ResPool.GetSizedPen (ColorControlText, lineWidth);
6652                                         DrawCaptionHelper(graphics, ColorControlText, pen, lineWidth, 0, captionRect, button);
6653                                         return;
6654                                 }
6655                         }
6656
6657                         case CaptionButton.Help:
6658                         case CaptionButton.Maximize:
6659                         case CaptionButton.Minimize:
6660                         case CaptionButton.Restore: {
6661                                 if ((state & ButtonState.Inactive)!=0) {
6662                                         DrawCaptionHelper(graphics, ColorControlLight, SystemPens.ControlLightLight, lineWidth, 1, captionRect, button);
6663
6664                                         DrawCaptionHelper(graphics, ColorControlDark, SystemPens.ControlDark, lineWidth, 0, captionRect, button);
6665                                         return;
6666                                 } else {
6667                                         DrawCaptionHelper(graphics, ColorControlText, SystemPens.ControlText, lineWidth, 0, captionRect, button);
6668                                         return;
6669                                 }
6670                         }
6671                         }
6672                 }
6673
6674                 public override void CPDrawCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
6675                 {
6676                         CPDrawCheckBoxInternal (dc, rectangle, state, false /* mixed */);
6677                 }
6678
6679                 private void CPDrawCheckBoxInternal (Graphics dc, Rectangle rectangle, ButtonState state, bool mixed)
6680                 {
6681                         Pen check_pen = (mixed) ? Pens.Gray : Pens.Black;
6682                         
6683                         Rectangle cb_rect = new Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
6684                         
6685                         if ((state & ButtonState.All) == ButtonState.All) {
6686                                 cb_rect.Width -= 2;
6687                                 cb_rect.Height -= 2;
6688                                 
6689                                 dc.FillRectangle (SystemBrushes.Control, cb_rect.X, cb_rect.Y, cb_rect.Width - 1, cb_rect.Height - 1);
6690                                 dc.DrawRectangle (SystemPens.ControlDark, cb_rect.X, cb_rect.Y, cb_rect.Width - 1, cb_rect.Height - 1);
6691                                 
6692                                 check_pen = SystemPens.ControlDark;
6693                         } else
6694                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
6695                                 cb_rect.Width -= 2;
6696                                 cb_rect.Height -= 2;
6697                                 
6698                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
6699                                         dc.FillRectangle (SystemBrushes.ControlLight, cb_rect.X, cb_rect.Y, cb_rect.Width - 1, cb_rect.Height - 1);
6700                                 else
6701                                         dc.FillRectangle (Brushes.White, cb_rect.X, cb_rect.Y, cb_rect.Width - 1, cb_rect.Height - 1);
6702                                 dc.DrawRectangle (SystemPens.ControlDark, cb_rect.X, cb_rect.Y, cb_rect.Width - 1, cb_rect.Height - 1);
6703                         } else {
6704                                 cb_rect.Width -= 1;
6705                                 cb_rect.Height -= 1;
6706                                 
6707                                 int check_box_visible_size = (cb_rect.Height > cb_rect.Width) ? cb_rect.Width : cb_rect.Height;
6708                                 
6709                                 int x_pos = Math.Max (0, cb_rect.X + (cb_rect.Width / 2) - check_box_visible_size / 2);
6710                                 int y_pos = Math.Max (0, cb_rect.Y + (cb_rect.Height / 2) - check_box_visible_size / 2);
6711                                 
6712                                 Rectangle rect = new Rectangle (x_pos, y_pos, check_box_visible_size, check_box_visible_size);
6713                                 
6714                                 if (((state & ButtonState.Pushed) == ButtonState.Pushed) || ((state & ButtonState.Inactive) == ButtonState.Inactive)) {
6715                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50,
6716                                                                                  Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
6717                                                                                                  ColorControl.G, ColorControl.B),
6718                                                                                  ColorControl), rect.X + 2, rect.Y + 2, rect.Width - 3, rect.Height - 3);
6719                                 } else
6720                                         dc.FillRectangle (SystemBrushes.ControlLightLight, rect.X + 2, rect.Y + 2, rect.Width - 3, rect.Height - 3);
6721                                 
6722                                 Pen pen = SystemPens.ControlDark;
6723                                 dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 1);
6724                                 dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 1, rect.Y);
6725                                 
6726                                 pen = SystemPens.ControlDarkDark;
6727                                 dc.DrawLine (pen, rect.X + 1, rect.Y + 1, rect.X + 1, rect.Bottom - 2);
6728                                 dc.DrawLine (pen, rect.X + 2, rect.Y + 1, rect.Right - 2, rect.Y + 1);
6729                                 
6730                                 pen = SystemPens.ControlLightLight;
6731                                 dc.DrawLine (pen, rect.Right, rect.Y, rect.Right, rect.Bottom);
6732                                 dc.DrawLine (pen, rect.X, rect.Bottom, rect.Right, rect.Bottom);
6733                                 
6734                                 // oh boy, matching ms is like fighting against windmills
6735                                 using (Pen h_pen = new Pen (ResPool.GetHatchBrush (HatchStyle.Percent50,
6736                                                                                    Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
6737                                                                                                    ColorControl.G, ColorControl.B), ColorControl))) {
6738                                         dc.DrawLine (h_pen, rect.X + 1, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
6739                                         dc.DrawLine (h_pen, rect.Right - 1, rect.Y + 1, rect.Right - 1, rect.Bottom - 1);
6740                                 }
6741                                 
6742                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
6743                                         check_pen = SystemPens.ControlDark;
6744                         }
6745                         
6746                         if ((state & ButtonState.Checked) == ButtonState.Checked) {
6747                                 int check_size = (cb_rect.Height > cb_rect.Width) ? cb_rect.Width / 2: cb_rect.Height / 2;
6748                                 
6749                                 if (check_size < 7) {
6750                                         int lineWidth = Math.Max (3, check_size / 3);
6751                                         int Scale = Math.Max (1, check_size / 9);
6752                                         
6753                                         Rectangle rect = new Rectangle (cb_rect.X + (cb_rect.Width / 2) - (int)Math.Ceiling ((float)check_size / 2) - 1, cb_rect.Y + (cb_rect.Height / 2) - (check_size / 2) - 1, 
6754                                                                         check_size, check_size);
6755                                         
6756                                         for (int i = 0; i < lineWidth; i++) {
6757                                                 dc.DrawLine (check_pen, rect.Left + lineWidth / 2, rect.Top + lineWidth + i, rect.Left + lineWidth / 2 + 2 * Scale, rect.Top + lineWidth + 2 * Scale + i);
6758                                                 dc.DrawLine (check_pen, rect.Left + lineWidth / 2 + 2 * Scale, rect.Top + lineWidth + 2 * Scale + i, rect.Left + lineWidth / 2 + 6 * Scale, rect.Top + lineWidth - 2 * Scale + i);
6759                                         }
6760                                 } else {
6761                                         int lineWidth = Math.Max (3, check_size / 3) + 1;
6762                                         
6763                                         int x_half = cb_rect.Width / 2;
6764                                         int y_half = cb_rect.Height / 2;
6765                                         
6766                                         Rectangle rect = new Rectangle (cb_rect.X + x_half - (check_size / 2) - 1, cb_rect.Y + y_half - (check_size / 2), 
6767                                                                         check_size, check_size);
6768                                         
6769                                         int gradient_left = check_size / 3;
6770                                         int gradient_right = check_size - gradient_left - 1;
6771                                         
6772                                         
6773                                         for (int i = 0; i < lineWidth; i++) {
6774                                                 dc.DrawLine (check_pen, rect.X, rect.Bottom - 1 - gradient_left - i, rect.X + gradient_left, rect.Bottom - 1 - i);
6775                                                 dc.DrawLine (check_pen, rect.X + gradient_left, rect.Bottom - 1 - i, rect.Right - 1, rect.Bottom - i  - 1 - gradient_right);
6776                                         }
6777                                 }
6778                         }
6779                 }
6780
6781                 public override void CPDrawComboButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
6782                         Point[]                 arrow = new Point[3];
6783                         Point                           P1;
6784                         Point                           P2;
6785                         Point                           P3;
6786                         int                             centerX;
6787                         int                             centerY;
6788                         int                             shiftX;
6789                         int                             shiftY;
6790                         Rectangle               rect;
6791
6792                         if ((state & ButtonState.Checked)!=0) {
6793                                 graphics.FillRectangle(ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLightLight, ColorControlLight),rectangle);                              
6794                         }
6795
6796                         if ((state & ButtonState.Flat)!=0) {
6797                                 ControlPaint.DrawBorder(graphics, rectangle, ColorControlDark, ButtonBorderStyle.Solid);
6798                         } else {
6799                                 if ((state & (ButtonState.Pushed | ButtonState.Checked))!=0) {
6800                                         // this needs to render like a pushed button - jba
6801                                         // CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
6802                                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
6803                                         graphics.DrawRectangle (SystemPens.ControlDark, trace_rectangle);
6804                                 } else {
6805                                         CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
6806                                 }
6807                         }
6808
6809                         rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
6810                         centerX=rect.Left+rect.Width/2;
6811                         centerY=rect.Top+rect.Height/2;
6812                         shiftX=Math.Max(1, rect.Width/8);
6813                         shiftY=Math.Max(1, rect.Height/8);
6814
6815                         if ((state & ButtonState.Pushed)!=0) {
6816                                 shiftX++;
6817                                 shiftY++;
6818                         }
6819
6820                         rect.Y-=shiftY;
6821                         centerY-=shiftY;
6822                         P1=new Point(rect.Left, centerY);
6823                         P2=new Point(rect.Right, centerY);
6824                         P3=new Point(centerX, rect.Bottom);
6825
6826                         arrow[0]=P1;
6827                         arrow[1]=P2;
6828                         arrow[2]=P3;
6829                         
6830                         /* Draw the arrow */
6831                         if ((state & ButtonState.Inactive)!=0) {
6832                                 /* Move away from the shadow */
6833                                 arrow[0].X += 1;        arrow[0].Y += 1;
6834                                 arrow[1].X += 1;        arrow[1].Y += 1;
6835                                 arrow[2].X += 1;        arrow[2].Y += 1;
6836                                 
6837                                 graphics.FillPolygon(SystemBrushes.ControlLightLight, arrow, FillMode.Winding);
6838
6839                                 arrow[0]=P1;
6840                                 arrow[1]=P2;
6841                                 arrow[2]=P3;
6842
6843                                 graphics.FillPolygon(SystemBrushes.ControlDark, arrow, FillMode.Winding);
6844                         } else {
6845                                 graphics.FillPolygon(SystemBrushes.ControlText, arrow, FillMode.Winding);
6846                         }
6847                 }
6848
6849
6850                 public override void CPDrawContainerGrabHandle (Graphics graphics, Rectangle bounds)
6851                 {
6852                         Pen                     pen     = Pens.Black;
6853                         Rectangle       rect    = new Rectangle (bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);      // Dunno why, but MS does it that way, too
6854                         int                     X;
6855                         int                     Y;
6856                         
6857                         graphics.FillRectangle (SystemBrushes.ControlLightLight, rect);
6858                         graphics.DrawRectangle (pen, rect);
6859                         
6860                         X = rect.X + rect.Width / 2;
6861                         Y = rect.Y + rect.Height / 2;
6862                         
6863                         /* Draw the cross */
6864                         graphics.DrawLine (pen, X, rect.Y + 2, X, rect.Bottom - 2);
6865                         graphics.DrawLine (pen, rect.X + 2, Y, rect.Right - 2, Y);
6866                         
6867                         /* Draw 'arrows' for vertical lines */
6868                         graphics.DrawLine (pen, X - 1, rect.Y + 3, X + 1, rect.Y + 3);
6869                         graphics.DrawLine (pen, X - 1, rect.Bottom - 3, X + 1, rect.Bottom - 3);
6870                         
6871                         /* Draw 'arrows' for horizontal lines */
6872                         graphics.DrawLine (pen, rect.X + 3, Y - 1, rect.X + 3, Y + 1);
6873                         graphics.DrawLine (pen, rect.Right - 3, Y - 1, rect.Right - 3, Y + 1);
6874                 }
6875
6876                 public virtual void DrawFlatStyleFocusRectangle (Graphics graphics, Rectangle rectangle, ButtonBase button, Color foreColor, Color backColor) {
6877                         // make a rectange to trace around border of the button
6878                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
6879                         
6880                         Color outerColor = foreColor;
6881                         // adjust focus color according to the flatstyle
6882                         if (button.FlatStyle == FlatStyle.Popup && !button.is_pressed) {
6883                                 outerColor = (backColor.ToArgb () == ColorControl.ToArgb ()) ? ControlPaint.Dark(ColorControl) : ColorControlText;                              
6884                         }
6885                         
6886                         // draw the outer rectangle
6887                         graphics.DrawRectangle (ResPool.GetPen (outerColor), trace_rectangle);                  
6888                         
6889                         // draw the inner rectangle                                             
6890                         if (button.FlatStyle == FlatStyle.Popup) {
6891                                 DrawInnerFocusRectangle (graphics, Rectangle.Inflate (rectangle, -4, -4), backColor);
6892                         } else {
6893                                 // draw a flat inner rectangle
6894                                 Pen pen = ResPool.GetPen (ControlPaint.LightLight (backColor));
6895                                 graphics.DrawRectangle(pen, Rectangle.Inflate (trace_rectangle, -4, -4));                               
6896                         }
6897                 }
6898                 
6899                 public virtual void DrawInnerFocusRectangle(Graphics graphics, Rectangle rectangle, Color backColor)
6900                 {       
6901                         // make a rectange to trace around border of the button
6902                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
6903                         
6904 #if NotUntilCairoIsFixed
6905                         Color colorBackInverted = Color.FromArgb (Math.Abs (backColor.R-255), Math.Abs (backColor.G-255), Math.Abs (backColor.B-255));
6906                         DashStyle oldStyle; // used for caching old penstyle
6907                         Pen pen = ResPool.GetPen (colorBackInverted);
6908
6909                         oldStyle = pen.DashStyle; 
6910                         pen.DashStyle = DashStyle.Dot;
6911
6912                         graphics.DrawRectangle (pen, trace_rectangle);
6913                         pen.DashStyle = oldStyle;
6914 #else
6915                         CPDrawFocusRectangle(graphics, trace_rectangle, Color.Wheat, backColor);
6916 #endif
6917                 }
6918                                 
6919
6920                 public override void CPDrawFocusRectangle (Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) 
6921                 {                       
6922                         Rectangle rect = rectangle;
6923                         Pen pen;
6924                         HatchBrush brush;
6925                                 
6926                         if (backColor.GetBrightness () >= 0.5) {
6927                                 foreColor = Color.Transparent;
6928                                 backColor = Color.Black;
6929                                 
6930                         } else {
6931                                 backColor = Color.FromArgb (Math.Abs (backColor.R-255), Math.Abs (backColor.G-255), Math.Abs (backColor.B-255));
6932                                 foreColor = Color.Black;
6933                         }
6934                                                 
6935                         brush = ResPool.GetHatchBrush (HatchStyle.Percent50, backColor, foreColor);
6936                         pen = new Pen (brush, 1);
6937                                                 
6938                         rect.Width--;
6939                         rect.Height--;                  
6940                         
6941                         graphics.DrawRectangle (pen, rect);
6942                         pen.Dispose ();
6943                 }
6944                 
6945                 public override void CPDrawGrabHandle (Graphics graphics, Rectangle rectangle, bool primary, bool enabled)
6946                 {
6947                         Brush   sb;
6948                         Pen pen;
6949                         
6950                         if (primary == true) {
6951                                 pen = Pens.Black;
6952                                 if (enabled == true) {
6953                                         sb = Brushes.White;
6954                                 } else {
6955                                         sb = SystemBrushes.Control;
6956                                 }
6957                         } else {
6958                                 pen = Pens.White;
6959                                 if (enabled == true) {
6960                                         sb = Brushes.Black;
6961                                 } else {
6962                                         sb = SystemBrushes.Control;
6963                                 }
6964                         }
6965                         graphics.FillRectangle (sb, rectangle);
6966                         graphics.DrawRectangle (pen, rectangle);                        
6967                 }
6968
6969
6970                 public override void CPDrawGrid (Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
6971                         Color   foreColor;
6972                         int     h;
6973                         int     b;
6974                         int     s;
6975
6976                         ControlPaint.Color2HBS(backColor, out h, out b, out s);
6977                         
6978                         if (b>127) {
6979                                 foreColor=Color.Black;
6980                         } else {
6981                                 foreColor=Color.White;
6982                         }
6983
6984                         // still not perfect. it seems that ms calculates the position of the first dot or line
6985
6986                         using (Pen pen = new Pen (foreColor)) {
6987                                 pen.DashPattern = new float [] {1.0f, pixelsBetweenDots.Width - 1};
6988                                 
6989                                 for (int y = area.Top; y < area.Bottom; y += pixelsBetweenDots.Height)
6990                                         graphics.DrawLine (pen, area.X, y, area.Right - 1, y);
6991                         }
6992                 }
6993
6994                 public override void CPDrawImageDisabled (Graphics graphics, Image image, int x, int y, Color background) {
6995                         /*
6996                                 Microsoft seems to ignore the background and simply make
6997                                 the image grayscale. At least when having > 256 colors on
6998                                 the display.
6999                         */
7000                         
7001                         if (imagedisabled_attributes == null) {                         
7002                                 imagedisabled_attributes = new ImageAttributes ();
7003                                 ColorMatrix colorMatrix=new ColorMatrix(new float[][] {
7004                                           // This table would create a perfect grayscale image, based on luminance
7005                                           //                            new float[]{0.3f,0.3f,0.3f,0,0},
7006                                           //                            new float[]{0.59f,0.59f,0.59f,0,0},
7007                                           //                            new float[]{0.11f,0.11f,0.11f,0,0},
7008                                           //                            new float[]{0,0,0,1,0,0},
7009                                           //                            new float[]{0,0,0,0,1,0},
7010                                           //                            new float[]{0,0,0,0,0,1}
7011                 
7012                                           // This table generates a image that is grayscaled and then
7013                                           // brightened up. Seems to match MS close enough.
7014                                           new float[]{0.2f,0.2f,0.2f,0,0},
7015                                           new float[]{0.41f,0.41f,0.41f,0,0},
7016                                           new float[]{0.11f,0.11f,0.11f,0,0},
7017                                           new float[]{0.15f,0.15f,0.15f,1,0,0},
7018                                           new float[]{0.15f,0.15f,0.15f,0,1,0},
7019                                           new float[]{0.15f,0.15f,0.15f,0,0,1}
7020                                   });
7021                                   
7022                                  imagedisabled_attributes.SetColorMatrix (colorMatrix);
7023                         }
7024                         
7025                         graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imagedisabled_attributes);
7026                         
7027                 }
7028
7029
7030                 public override void CPDrawLockedFrame (Graphics graphics, Rectangle rectangle, bool primary) {
7031                         Pen     penBorder;
7032                         Pen     penInside;
7033
7034                         if (primary) {
7035                                 penBorder = ResPool.GetSizedPen (Color.White, 2);
7036                                 penInside = ResPool.GetPen (Color.Black);
7037                         } else {
7038                                 penBorder = ResPool.GetSizedPen (Color.Black, 2);
7039                                 penInside = ResPool.GetPen (Color.White);
7040                         }
7041                         penBorder.Alignment=PenAlignment.Inset;
7042                         penInside.Alignment=PenAlignment.Inset;
7043
7044                         graphics.DrawRectangle(penBorder, rectangle);
7045                         graphics.DrawRectangle(penInside, rectangle.X+2, rectangle.Y+2, rectangle.Width-5, rectangle.Height-5);
7046                 }
7047
7048
7049                 public override void CPDrawMenuGlyph (Graphics graphics, Rectangle rectangle, MenuGlyph glyph, Color color, Color backColor) {
7050                         Rectangle       rect;
7051                         int                     lineWidth;
7052
7053                         if (backColor != Color.Empty)
7054                                 graphics.FillRectangle (ResPool.GetSolidBrush (backColor), rectangle);
7055                                 
7056                         Brush brush = ResPool.GetSolidBrush (color);
7057
7058                         switch(glyph) {
7059                         case MenuGlyph.Arrow: {
7060                                 float height = rectangle.Height * 0.7f;
7061                                 float width  = height / 2.0f;
7062                                 
7063                                 PointF ddCenter = new PointF (rectangle.X + ((rectangle.Width-width) / 2.0f), rectangle.Y + (rectangle.Height / 2.0f));
7064
7065                                 PointF [] vertices = new PointF [3];
7066                                 vertices [0].X = ddCenter.X;
7067                                 vertices [0].Y = ddCenter.Y - (height / 2.0f);
7068                                 vertices [1].X = ddCenter.X;
7069                                 vertices [1].Y = ddCenter.Y + (height / 2.0f);
7070                                 vertices [2].X = ddCenter.X + width + 0.1f;
7071                                 vertices [2].Y = ddCenter.Y;
7072                                 
7073                                 graphics.FillPolygon (brush, vertices);
7074
7075                                 return;
7076                         }
7077
7078                         case MenuGlyph.Bullet: {
7079                                 
7080                                 lineWidth=Math.Max(2, rectangle.Width/3);
7081                                 rect=new Rectangle(rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2);
7082                                 
7083                                 graphics.FillEllipse(brush, rect);
7084                                 
7085                                 return;
7086                         }
7087
7088                         case MenuGlyph.Checkmark: {
7089                                 
7090                                 Pen pen = ResPool.GetPen (color);
7091                                 lineWidth = Math.Max (2, rectangle.Width / 6);
7092                                 rect = new Rectangle(rectangle.X + lineWidth, rectangle.Y + lineWidth, rectangle.Width - lineWidth * 2, rectangle.Height- lineWidth * 2);
7093
7094                                 int Scale = Math.Max (1, rectangle.Width / 12);
7095                                 int top = (rect.Y + lineWidth + ((rect.Height - ((2 * Scale) + lineWidth)) / 2));
7096
7097                                 for (int i=0; i<lineWidth; i++) {
7098                                         graphics.DrawLine (pen, rect.Left+lineWidth/2, top+i, rect.Left+lineWidth/2+2*Scale, top+2*Scale+i);
7099                                         graphics.DrawLine (pen, rect.Left+lineWidth/2+2*Scale, top+2*Scale+i, rect.Left+lineWidth/2+6*Scale, top-2*Scale+i);
7100                                 }
7101                                 return;
7102                         }
7103                         }
7104
7105                 }
7106
7107                 public override void CPDrawMixedCheckBox (Graphics graphics, Rectangle rectangle, ButtonState state)
7108                 {
7109                         CPDrawCheckBoxInternal (graphics, rectangle, state, true /* mixed */);
7110                 }
7111
7112                 public override void CPDrawRadioButton (Graphics dc, Rectangle rectangle, ButtonState state)
7113                 {
7114                         CPColor cpcolor = ResPool.GetCPColor (ColorControl);
7115                         
7116                         Color dot_color = Color.Black;
7117                         
7118                         Color top_left_outer = Color.Black;
7119                         Color top_left_inner = Color.Black;
7120                         Color bottom_right_outer = Color.Black;
7121                         Color bottom_right_inner = Color.Black;
7122                         
7123                         int ellipse_diameter = (rectangle.Width > rectangle.Height) ? (int)(rectangle.Height  * 0.9f) : (int)(rectangle.Width * 0.9f);
7124                         int radius = ellipse_diameter / 2;
7125                         
7126                         Rectangle rb_rect = new Rectangle (rectangle.X + (rectangle.Width / 2) - radius, rectangle.Y + (rectangle.Height / 2) - radius, ellipse_diameter, ellipse_diameter);
7127                         
7128                         Brush brush = null;
7129                         
7130                         if ((state & ButtonState.All) == ButtonState.All) {
7131                                 brush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
7132                                                                                                      ColorControl.G, ColorControl.B), ColorControl);
7133                                 dot_color = cpcolor.Dark;
7134                         } else
7135                         if ((state & ButtonState.Flat) == ButtonState.Flat) {
7136                                 if (((state & ButtonState.Inactive) == ButtonState.Inactive) || ((state & ButtonState.Pushed) == ButtonState.Pushed))
7137                                         brush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255), ColorControl.G, ColorControl.B), ColorControl);
7138                                 else
7139                                         brush = SystemBrushes.ControlLightLight;
7140                         } else {
7141                                 if (((state & ButtonState.Inactive) == ButtonState.Inactive) || ((state & ButtonState.Pushed) == ButtonState.Pushed))
7142                                         brush = ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255), ColorControl.G, ColorControl.B), ColorControl);
7143                                 else
7144                                         brush = SystemBrushes.ControlLightLight;
7145                                 
7146                                 top_left_outer = cpcolor.Dark;
7147                                 top_left_inner = cpcolor.DarkDark;
7148                                 bottom_right_outer = cpcolor.Light;
7149                                 bottom_right_inner = Color.Transparent;
7150                                 
7151                                 if ((state & ButtonState.Inactive) == ButtonState.Inactive)
7152                                         dot_color = cpcolor.Dark;
7153                         }
7154                         
7155                         dc.FillEllipse (brush, rb_rect.X + 1, rb_rect.Y + 1, ellipse_diameter - 1, ellipse_diameter - 1);
7156                         
7157                         int line_width = Math.Max (1, (int)(ellipse_diameter * 0.08f));
7158                         
7159                         dc.DrawArc (ResPool.GetSizedPen (top_left_outer, line_width), rb_rect, 135.0f, 180.0f);
7160                         dc.DrawArc (ResPool.GetSizedPen (top_left_inner, line_width), Rectangle.Inflate (rb_rect, -line_width, -line_width), 135.0f, 180.0f);
7161                         dc.DrawArc (ResPool.GetSizedPen (bottom_right_outer, line_width), rb_rect, 315.0f, 180.0f);
7162                         
7163                         if (bottom_right_inner != Color.Transparent)
7164                                 dc.DrawArc (ResPool.GetSizedPen (bottom_right_inner, line_width), Rectangle.Inflate (rb_rect, -line_width, -line_width), 315.0f, 180.0f);
7165                         else
7166                                 using (Pen h_pen = new Pen (ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255), ColorControl.G, ColorControl.B), ColorControl), line_width)) {
7167                                         dc.DrawArc (h_pen, Rectangle.Inflate (rb_rect, -line_width, -line_width), 315.0f, 180.0f);
7168                                 }
7169                         
7170                         if ((state & ButtonState.Checked) == ButtonState.Checked) {
7171                                 int inflate = line_width * 4;
7172                                 Rectangle tmp = Rectangle.Inflate (rb_rect, -inflate, -inflate);
7173                                 if (rectangle.Height >  13) {
7174                                         tmp.X += 1;
7175                                         tmp.Y += 1;
7176                                         tmp.Height -= 1;
7177                                         dc.FillEllipse (ResPool.GetSolidBrush (dot_color), tmp);
7178                                 } else {
7179                                         Pen pen = ResPool.GetPen (dot_color);
7180                                         dc.DrawLine (pen, tmp.X, tmp.Y + (tmp.Height / 2), tmp.Right, tmp.Y + (tmp.Height / 2));
7181                                         dc.DrawLine (pen, tmp.X, tmp.Y + (tmp.Height / 2) + 1, tmp.Right, tmp.Y + (tmp.Height / 2) + 1);
7182                                         
7183                                         dc.DrawLine (pen, tmp.X + (tmp.Width / 2), tmp.Y, tmp.X + (tmp.Width / 2), tmp.Bottom);
7184                                         dc.DrawLine (pen, tmp.X + (tmp.Width / 2) + 1, tmp.Y, tmp.X + (tmp.Width / 2) + 1, tmp.Bottom);
7185                                 }
7186                         }
7187                 }
7188
7189                 public override void CPDrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
7190
7191                 }
7192
7193
7194                 public override void CPDrawReversibleLine (Point start, Point end, Color backColor) {
7195
7196                 }
7197
7198
7199                 /* Scroll button: regular button + direction arrow */
7200                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state)
7201                 {
7202                         DrawScrollButtonPrimitive (dc, area, state);
7203                         
7204                         bool fill_rect = true;
7205                         int offset = 0;
7206                         
7207                         if ((state & ButtonState.Pushed) != 0)
7208                                 offset = 1;
7209                         
7210                         // skip the border
7211                         Rectangle rect = new Rectangle (area.X + 2 + offset, area.Y + 2 + offset, area.Width - 4, area.Height - 4);
7212                         
7213                         Point [] arrow = new Point [3];
7214                         for (int i = 0; i < 3; i++)
7215                                 arrow [i] = new Point ();
7216                         
7217                         Pen pen = SystemPens.ControlText;
7218                         
7219                         if ((state & ButtonState.Inactive) != 0) {
7220                                 pen = SystemPens.ControlDark;
7221                         }
7222                         
7223                         switch (type) {
7224                                 default:
7225                                 case ScrollButton.Down:
7226                                         int x_middle = (int)Math.Round (rect.Width / 2.0f) - 1;
7227                                         int y_middle = (int)Math.Round (rect.Height / 2.0f) - 1;
7228                                         if (x_middle == 1)
7229                                                 x_middle = 2;
7230                                         
7231                                         int triangle_height;
7232                                         
7233                                         if (rect.Height < 8) {
7234                                                 triangle_height = 2;
7235                                                 fill_rect = false;
7236                                         } else if (rect.Height == 11) {
7237                                                 triangle_height = 3;
7238                                         } else {
7239                                                 triangle_height = (int)Math.Round (rect.Height / 3.0f);
7240                                         }
7241                                         
7242                                         arrow [0].X = rect.X + x_middle;
7243                                         arrow [0].Y = rect.Y + y_middle + triangle_height / 2;
7244                                         
7245                                         arrow [1].X = arrow [0].X + triangle_height - 1;
7246                                         arrow [1].Y = arrow [0].Y - triangle_height + 1;
7247                                         arrow [2].X = arrow [0].X - triangle_height + 1;
7248                                         arrow [2].Y = arrow [1].Y;
7249                                         
7250                                         dc.DrawPolygon (pen, arrow);
7251                                         
7252                                         if ((state & ButtonState.Inactive) != 0) {
7253                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [1].X + 1, arrow [1].Y + 1, arrow [0].X + 1, arrow [0].Y + 1);
7254                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [1].X, arrow [1].Y + 1, arrow [0].X + 1, arrow [0].Y);
7255                                         }
7256                                         
7257                                         if (fill_rect) {
7258                                                 for (int i = 0; i < arrow [0].Y - arrow [1].Y; i++) {
7259                                                         dc.DrawLine (pen, arrow [1].X, arrow [1].Y + i, arrow [2].X, arrow [1].Y + i);
7260                                                         arrow [1].X -= 1;
7261                                                         arrow [2].X += 1;
7262                                                 }
7263                                         }
7264                                         break;
7265                                         
7266                                 case ScrollButton.Up:
7267                                         x_middle = (int)Math.Round (rect.Width / 2.0f) - 1;
7268                                         y_middle = (int)Math.Round (rect.Height / 2.0f);
7269                                         if (x_middle == 1)
7270                                                 x_middle = 2;
7271                                         
7272                                         if (y_middle == 1)
7273                                                 y_middle = 2;
7274                                         
7275                                         if (rect.Height < 8) {
7276                                                 triangle_height = 2;
7277                                                 fill_rect = false;
7278                                         } else if (rect.Height == 11) {
7279                                                 triangle_height = 3;
7280                                         } else {
7281                                                 triangle_height = (int)Math.Round (rect.Height / 3.0f);
7282                                         }
7283                                         
7284                                         arrow [0].X = rect.X + x_middle;
7285                                         arrow [0].Y = rect.Y + y_middle - triangle_height / 2;
7286                                         
7287                                         arrow [1].X = arrow [0].X + triangle_height - 1;
7288                                         arrow [1].Y = arrow [0].Y + triangle_height - 1;
7289                                         arrow [2].X = arrow [0].X - triangle_height + 1;
7290                                         arrow [2].Y = arrow [1].Y;
7291                                         
7292                                         dc.DrawPolygon (pen, arrow);
7293                                         
7294                                         if ((state & ButtonState.Inactive) != 0) {
7295                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [1].X + 1, arrow [1].Y + 1, arrow [2].X + 1, arrow [1].Y + 1);
7296                                         }
7297                                         
7298                                         if (fill_rect) {
7299                                                 for (int i = 0; i < arrow [1].Y - arrow [0].Y; i++) {
7300                                                         dc.DrawLine (pen, arrow [2].X, arrow [1].Y - i, arrow [1].X, arrow [1].Y - i);
7301                                                         arrow [1].X -= 1;
7302                                                         arrow [2].X += 1;
7303                                                 }
7304                                         }
7305                                         break;
7306                                         
7307                                 case ScrollButton.Left:
7308                                         y_middle = (int)Math.Round (rect.Height / 2.0f) - 1;
7309                                         if (y_middle == 1)
7310                                                 y_middle = 2;
7311                                         
7312                                         int triangle_width;
7313                                         
7314                                         if (rect.Width < 8) {
7315                                                 triangle_width = 2;
7316                                                 fill_rect = false;
7317                                         } else if (rect.Width == 11) {
7318                                                 triangle_width = 3;
7319                                         } else {
7320                                                 triangle_width = (int)Math.Round (rect.Width / 3.0f);
7321                                         }
7322                                         
7323                                         arrow [0].X = rect.Left + triangle_width - 1;
7324                                         arrow [0].Y = rect.Y + y_middle;
7325                                         
7326                                         if (arrow [0].X - 1 == rect.X)
7327                                                 arrow [0].X += 1;
7328                                         
7329                                         arrow [1].X = arrow [0].X + triangle_width - 1;
7330                                         arrow [1].Y = arrow [0].Y - triangle_width + 1;
7331                                         arrow [2].X = arrow [1].X;
7332                                         arrow [2].Y = arrow [0].Y + triangle_width - 1;
7333                                         
7334                                         dc.DrawPolygon (pen, arrow);
7335                                         
7336                                         if ((state & ButtonState.Inactive) != 0) {
7337                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [1].X + 1, arrow [1].Y + 1, arrow [2].X + 1, arrow [2].Y + 1);
7338                                         }
7339                                         
7340                                         if (fill_rect) {
7341                                                 for (int i = 0; i < arrow [2].X - arrow [0].X; i++) {
7342                                                         dc.DrawLine (pen, arrow [2].X - i, arrow [1].Y, arrow [2].X - i, arrow [2].Y);
7343                                                         arrow [1].Y += 1;
7344                                                         arrow [2].Y -= 1;
7345                                                 }
7346                                         }
7347                                         break;
7348                                         
7349                                 case ScrollButton.Right:
7350                                         y_middle = (int)Math.Round (rect.Height / 2.0f) - 1;
7351                                         if (y_middle == 1)
7352                                                 y_middle = 2;
7353                                         
7354                                         if (rect.Width < 8) {
7355                                                 triangle_width = 2;
7356                                                 fill_rect = false;
7357                                         } else if (rect.Width == 11) {
7358                                                 triangle_width = 3;
7359                                         } else {
7360                                                 triangle_width = (int)Math.Round (rect.Width / 3.0f);
7361                                         }
7362                                         
7363                                         arrow [0].X = rect.Right - triangle_width - 1;
7364                                         arrow [0].Y = rect.Y + y_middle;
7365                                         
7366                                         if (arrow [0].X - 1 == rect.X)
7367                                                 arrow [0].X += 1;
7368                                         
7369                                         arrow [1].X = arrow [0].X - triangle_width + 1;
7370                                         arrow [1].Y = arrow [0].Y - triangle_width + 1;
7371                                         arrow [2].X = arrow [1].X;
7372                                         arrow [2].Y = arrow [0].Y + triangle_width - 1;
7373                                         
7374                                         dc.DrawPolygon (pen, arrow);
7375                                         
7376                                         if ((state & ButtonState.Inactive) != 0) {
7377                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [0].X + 1, arrow [0].Y + 1, arrow [2].X + 1, arrow [2].Y + 1);
7378                                                 dc.DrawLine (SystemPens.ControlLightLight, arrow [0].X, arrow [0].Y + 1, arrow [2].X + 1, arrow [2].Y);
7379                                         }
7380                                         
7381                                         if (fill_rect) {
7382                                                 for (int i = 0; i < arrow [0].X - arrow [1].X; i++) {
7383                                                         dc.DrawLine (pen, arrow [2].X + i, arrow [1].Y, arrow [2].X + i, arrow [2].Y);
7384                                                         arrow [1].Y += 1;
7385                                                         arrow [2].Y -= 1;
7386                                                 }
7387                                         }
7388                                         break;
7389                         }
7390                 }
7391
7392                 public  override void CPDrawSelectionFrame (Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect,
7393                         Color backColor) {
7394
7395                 }
7396
7397
7398                 public override void CPDrawSizeGrip (Graphics dc, Color backColor, Rectangle bounds)
7399                 {
7400                         Pen pen_dark = ResPool.GetPen(ControlPaint.Dark(backColor));
7401                         Pen pen_light_light = ResPool.GetPen(ControlPaint.LightLight(backColor));
7402                         
7403                         for (int i = 2; i < bounds.Width - 2; i += 4) {
7404                                 dc.DrawLine (pen_light_light, bounds.X + i, bounds.Bottom - 2, bounds.Right - 1, bounds.Y + i - 1);
7405                                 dc.DrawLine (pen_dark, bounds.X + i + 1, bounds.Bottom - 2, bounds.Right - 1, bounds.Y + i);
7406                                 dc.DrawLine (pen_dark, bounds.X + i + 2, bounds.Bottom - 2, bounds.Right - 1, bounds.Y + i + 1);
7407                         }
7408                 }
7409
7410                 private void DrawStringDisabled20 (Graphics g, string s, Font font, Rectangle layoutRectangle, Color color, TextFormatFlags flags, bool useDrawString)
7411                 {
7412                         CPColor cpcolor = ResPool.GetCPColor (color);
7413
7414                         layoutRectangle.Offset (1, 1);
7415                         TextRenderer.DrawTextInternal (g, s, font, layoutRectangle, cpcolor.LightLight, flags, useDrawString);
7416
7417                         layoutRectangle.Offset (-1, -1);
7418                         TextRenderer.DrawTextInternal (g, s, font, layoutRectangle, cpcolor.Dark, flags, useDrawString);
7419                 }
7420
7421                 public  override void CPDrawStringDisabled (Graphics dc, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format)
7422                 {
7423                         CPColor cpcolor = ResPool.GetCPColor (color);
7424                         
7425                         dc.DrawString (s, font, ResPool.GetSolidBrush(cpcolor.LightLight), 
7426                                        new RectangleF(layoutRectangle.X + 1, layoutRectangle.Y + 1, layoutRectangle.Width, layoutRectangle.Height),
7427                                        format);
7428                         dc.DrawString (s, font, ResPool.GetSolidBrush (cpcolor.Dark), layoutRectangle, format);
7429                 }
7430
7431                 public override void CPDrawStringDisabled (IDeviceContext dc, string s, Font font, Color color, Rectangle layoutRectangle, TextFormatFlags format)
7432                 {
7433                         CPColor cpcolor = ResPool.GetCPColor (color);
7434
7435                         layoutRectangle.Offset (1, 1);
7436                         TextRenderer.DrawText (dc, s, font, layoutRectangle, cpcolor.LightLight, format);
7437
7438                         layoutRectangle.Offset (-1, -1);
7439                         TextRenderer.DrawText (dc, s, font, layoutRectangle, cpcolor.Dark, format);
7440                 }
7441
7442                 public override void CPDrawVisualStyleBorder (Graphics graphics, Rectangle bounds)
7443                 {
7444                         graphics.DrawRectangle (SystemPens.ControlDarkDark, bounds);
7445                 }
7446
7447                 private static void DrawBorderInternal (Graphics graphics, int startX, int startY, int endX, int endY,
7448                         int width, Color color, ButtonBorderStyle style, Border3DSide side) 
7449                 {
7450                         DrawBorderInternal (graphics, (float) startX, (float) startY, (float) endX, (float) endY, 
7451                                 width, color, style, side);
7452                 }
7453
7454                 private static void DrawBorderInternal (Graphics graphics, float startX, float startY, float endX, float endY,
7455                         int width, Color color, ButtonBorderStyle style, Border3DSide side) {
7456
7457                         Pen pen = null;
7458
7459                         switch (style) {
7460                         case ButtonBorderStyle.Solid:
7461                         case ButtonBorderStyle.Inset:
7462                         case ButtonBorderStyle.Outset:
7463                                         pen = ThemeEngine.Current.ResPool.GetDashPen (color, DashStyle.Solid);
7464                                         break;
7465                         case ButtonBorderStyle.Dashed:
7466                                         pen = ThemeEngine.Current.ResPool.GetDashPen (color, DashStyle.Dash);
7467                                         break;
7468                         case ButtonBorderStyle.Dotted:
7469                                         pen = ThemeEngine.Current.ResPool.GetDashPen (color, DashStyle.Dot);
7470                                         break;
7471                         default:
7472                         case ButtonBorderStyle.None:
7473                                         return;
7474                         }
7475
7476                         switch(style) {
7477                         case ButtonBorderStyle.Outset: {
7478                                 Color           colorGrade;
7479                                 int             hue, brightness, saturation;
7480                                 int             brightnessSteps;
7481                                 int             brightnessDownSteps;
7482
7483                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
7484
7485                                 brightnessDownSteps=brightness/width;
7486                                 if (brightness>127) {
7487                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
7488                                 } else {
7489                                         brightnessSteps=(127-brightness)/width;
7490                                 }
7491
7492                                 for (int i=0; i<width; i++) {
7493                                         switch(side) {
7494                                         case Border3DSide.Left: {
7495                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
7496                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7497                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
7498                                                 break;
7499                                         }
7500
7501                                         case Border3DSide.Right: {
7502                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
7503                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7504                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
7505                                                 break;
7506                                         }
7507
7508                                         case Border3DSide.Top: {
7509                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
7510                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7511                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
7512                                                 break;
7513                                         }
7514
7515                                         case Border3DSide.Bottom: {
7516                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
7517                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7518                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
7519                                                 break;
7520                                         }
7521                                         }
7522                                 }
7523                                 break;
7524                         }
7525
7526                         case ButtonBorderStyle.Inset: {
7527                                 Color           colorGrade;
7528                                 int             hue, brightness, saturation;
7529                                 int             brightnessSteps;
7530                                 int             brightnessDownSteps;
7531
7532                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
7533
7534                                 brightnessDownSteps=brightness/width;
7535                                 if (brightness>127) {
7536                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
7537                                 } else {
7538                                         brightnessSteps=(127-brightness)/width;
7539                                 }
7540
7541                                 for (int i=0; i<width; i++) {
7542                                         switch(side) {
7543                                         case Border3DSide.Left: {
7544                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
7545                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7546                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
7547                                                 break;
7548                                         }
7549
7550                                         case Border3DSide.Right: {
7551                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
7552                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7553                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
7554                                                 break;
7555                                         }
7556
7557                                         case Border3DSide.Top: {
7558                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
7559                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7560                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
7561                                                 break;
7562                                         }
7563
7564                                         case Border3DSide.Bottom: {
7565                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
7566                                                 pen = ThemeEngine.Current.ResPool.GetPen (colorGrade);
7567                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
7568                                                 break;
7569                                         }
7570                                         }
7571                                 }
7572                                 break;
7573                         }
7574
7575                                 /*
7576                                         I decided to have the for-loop duplicated for speed reasons;
7577                                         that way we only have to switch once (as opposed to have the
7578                                         for-loop around the switch)
7579                                 */
7580                         default: {
7581                                 switch(side) {
7582                                 case Border3DSide.Left: {
7583                                         for (int i=0; i<width; i++) {
7584                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
7585                                         }
7586                                         break;
7587                                 }
7588
7589                                 case Border3DSide.Right: {
7590                                         for (int i=0; i<width; i++) {
7591                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
7592                                         }
7593                                         break;
7594                                 }
7595
7596                                 case Border3DSide.Top: {
7597                                         for (int i=0; i<width; i++) {
7598                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
7599                                         }
7600                                         break;
7601                                 }
7602
7603                                 case Border3DSide.Bottom: {
7604                                         for (int i=0; i<width; i++) {
7605                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
7606                                         }
7607                                         break;
7608                                 }
7609                                 }
7610                                 break;
7611                         }
7612                         }
7613                 }
7614
7615                 /*
7616                         This function actually draws the various caption elements.
7617                         This way we can scale them nicely, no matter what size, and they
7618                         still look like MS's scaled caption buttons. (as opposed to scaling a bitmap)
7619                 */
7620
7621                 private void DrawCaptionHelper(Graphics graphics, Color color, Pen pen, int lineWidth, int shift, Rectangle captionRect, CaptionButton button) {
7622                         switch(button) {
7623                         case CaptionButton.Close: {
7624                                 if (lineWidth<2) {
7625                                         graphics.DrawLine(pen, captionRect.Left+2*lineWidth+1+shift, captionRect.Top+2*lineWidth+shift, captionRect.Right-2*lineWidth+1+shift, captionRect.Bottom-2*lineWidth+shift);
7626                                         graphics.DrawLine(pen, captionRect.Right-2*lineWidth+1+shift, captionRect.Top+2*lineWidth+shift, captionRect.Left+2*lineWidth+1+shift, captionRect.Bottom-2*lineWidth+shift);
7627                                 }
7628
7629                                 graphics.DrawLine(pen, captionRect.Left+2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Right-2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
7630                                 graphics.DrawLine(pen, captionRect.Right-2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Left+2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
7631                                 return;
7632                         }
7633
7634                         case CaptionButton.Help: {
7635                                 StringFormat    sf = new StringFormat();                                
7636                                 Font                            font = new Font("Microsoft Sans Serif", captionRect.Height, FontStyle.Bold, GraphicsUnit.Pixel);
7637
7638                                 sf.Alignment=StringAlignment.Center;
7639                                 sf.LineAlignment=StringAlignment.Center;
7640
7641
7642                                 graphics.DrawString("?", font, ResPool.GetSolidBrush (color), captionRect.X+captionRect.Width/2+shift, captionRect.Y+captionRect.Height/2+shift+lineWidth/2, sf);
7643
7644                                 sf.Dispose();                           
7645                                 font.Dispose();
7646
7647                                 return;
7648                         }
7649
7650                         case CaptionButton.Maximize: {
7651                                 /* Top 'caption bar' line */
7652                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
7653                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Top+2*lineWidth+shift+i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Top+2*lineWidth+shift+i);
7654                                 }
7655
7656                                 /* Left side line */
7657                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7658                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+2*lineWidth+shift, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
7659                                 }
7660
7661                                 /* Right side line */
7662                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7663                                         graphics.DrawLine(pen, captionRect.Right-lineWidth-lineWidth/2+shift+i, captionRect.Top+2*lineWidth+shift, captionRect.Right-lineWidth-lineWidth/2+shift+i, captionRect.Bottom-lineWidth+shift);
7664                                 }
7665
7666                                 /* Bottom line */
7667                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7668                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
7669                                 }
7670                                 return;
7671                         }
7672
7673                         case CaptionButton.Minimize: {
7674                                 /* Bottom line */
7675                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
7676                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth+shift, captionRect.Bottom-lineWidth+shift-i);
7677                                 }
7678                                 return;
7679                         }
7680
7681                         case CaptionButton.Restore: {
7682                                 /** First 'window' **/
7683                                 /* Top 'caption bar' line */
7684                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
7685                                         graphics.DrawLine(pen, captionRect.Left+3*lineWidth+shift, captionRect.Top+2*lineWidth+shift-i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Top+2*lineWidth+shift-i);
7686                                 }
7687
7688                                 /* Left side line */
7689                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7690                                         graphics.DrawLine(pen, captionRect.Left+3*lineWidth+shift+i, captionRect.Top+2*lineWidth+shift, captionRect.Left+3*lineWidth+shift+i, captionRect.Top+4*lineWidth+shift);
7691                                 }
7692
7693                                 /* Right side line */
7694                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7695                                         graphics.DrawLine(pen, captionRect.Right-lineWidth-lineWidth/2+shift-i, captionRect.Top+2*lineWidth+shift, captionRect.Right-lineWidth-lineWidth/2+shift-i, captionRect.Top+5*lineWidth-lineWidth/2+shift);
7696                                 }
7697
7698                                 /* Bottom line */
7699                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7700                                         graphics.DrawLine(pen, captionRect.Right-3*lineWidth-lineWidth/2+shift, captionRect.Top+5*lineWidth-lineWidth/2+shift+1+i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Top+5*lineWidth-lineWidth/2+shift+1+i);
7701                                 }
7702
7703                                 /** Second 'window' **/
7704                                 /* Top 'caption bar' line */
7705                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
7706                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Top+4*lineWidth+shift+1-i, captionRect.Right-3*lineWidth-lineWidth/2+shift, captionRect.Top+4*lineWidth+shift+1-i);
7707                                 }
7708
7709                                 /* Left side line */
7710                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7711                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+4*lineWidth+shift+1, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
7712                                 }
7713
7714                                 /* Right side line */
7715                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7716                                         graphics.DrawLine(pen, captionRect.Right-3*lineWidth-lineWidth/2+shift-i, captionRect.Top+4*lineWidth+shift+1, captionRect.Right-3*lineWidth-lineWidth/2+shift-i, captionRect.Bottom-lineWidth+shift);
7717                                 }
7718
7719                                 /* Bottom line */
7720                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
7721                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
7722                                 }
7723
7724                                 return;
7725                         }
7726
7727                         }
7728                 }
7729
7730                 /* Generic scroll button */
7731                 public void DrawScrollButtonPrimitive (Graphics dc, Rectangle area, ButtonState state) {
7732                         if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
7733                                 dc.FillRectangle (SystemBrushes.Control, area.X + 1,
7734                                         area.Y + 1, area.Width - 2 , area.Height - 2);
7735
7736                                 dc.DrawRectangle (SystemPens.ControlDark, area.X,
7737                                         area.Y, area.Width, area.Height);
7738
7739                                 return;
7740                         }                       
7741         
7742                         Brush sb_control = SystemBrushes.Control;
7743                         Brush sb_lightlight = SystemBrushes.ControlLightLight;
7744                         Brush sb_dark = SystemBrushes.ControlDark;
7745                         Brush sb_darkdark = SystemBrushes.ControlDarkDark;
7746                         
7747                         dc.FillRectangle (sb_control, area.X, area.Y, area.Width, 1);
7748                         dc.FillRectangle (sb_control, area.X, area.Y, 1, area.Height);
7749
7750                         dc.FillRectangle (sb_lightlight, area.X + 1, area.Y + 1, area.Width - 1, 1);
7751                         dc.FillRectangle (sb_lightlight, area.X + 1, area.Y + 2, 1,
7752                                 area.Height - 4);
7753                         
7754                         dc.FillRectangle (sb_dark, area.X + 1, area.Y + area.Height - 2,
7755                                 area.Width - 2, 1);
7756
7757                         dc.FillRectangle (sb_darkdark, area.X, area.Y + area.Height -1,
7758                                 area.Width , 1);
7759
7760                         dc.FillRectangle (sb_dark, area.X + area.Width - 2,
7761                                 area.Y + 1, 1, area.Height -3);
7762
7763                         dc.FillRectangle (sb_darkdark, area.X + area.Width -1,
7764                                 area.Y, 1, area.Height - 1);
7765
7766                         dc.FillRectangle (sb_control, area.X + 2,
7767                                 area.Y + 2, area.Width - 4, area.Height - 4);
7768                         
7769                 }
7770                 
7771                 public override void CPDrawBorderStyle (Graphics dc, Rectangle area, BorderStyle border_style) {
7772                         switch (border_style){
7773                         case BorderStyle.Fixed3D:
7774                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), area.X, area.Y, area.X +area.Width, area.Y);
7775                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), area.X, area.Y, area.X, area.Y + area.Height);
7776                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), area.X , area.Y + area.Height - 1, area.X + area.Width , 
7777                                         area.Y + area.Height - 1);
7778                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), area.X + area.Width -1 , area.Y, area.X + area.Width -1, 
7779                                         area.Y + area.Height);
7780
7781                                 dc.DrawLine (ResPool.GetPen (ColorActiveBorder), area.X + 1, area.Bottom - 2, area.Right - 2, area.Bottom - 2);
7782                                 dc.DrawLine (ResPool.GetPen (ColorActiveBorder), area.Right - 2, area.Top + 1, area.Right - 2, area.Bottom - 2);
7783                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), area.X + 1, area.Top + 1, area.X + 1, area.Bottom - 3);
7784                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), area.X + 1, area.Top + 1, area.Right - 3, area.Top + 1);
7785                                 break;
7786                         case BorderStyle.FixedSingle:
7787                                 dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), area.X, area.Y, area.Width - 1, area.Height - 1);
7788                                 break;
7789                         case BorderStyle.None:
7790                         default:
7791                                 break;
7792                         }
7793                         
7794                 }
7795                 #endregion      // ControlPaint
7796
7797
7798         } //class
7799 }