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