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