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