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