* Theme.cs: Added a little helper to SystemResPool to get the Dark,
[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
31 using System.Drawing;
32 using System.Drawing.Drawing2D;
33 using System.Drawing.Imaging;
34 using System.Drawing.Text;
35
36 namespace System.Windows.Forms
37 {
38
39         internal class ThemeWin32Classic : Theme
40         {               
41                 public override Version Version {
42                         get {
43                                 return new Version(0, 1, 0, 0);
44                         }
45                 }
46
47                 /* Hardcoded colour values not exposed in the API constants in all configurations */
48                 protected static readonly Color arrow_color = Color.Black;
49                 protected static readonly Color pen_ticks_color = Color.Black;
50                 protected static readonly Color progressbarblock_color = Color.FromArgb (255, 0, 0, 128);
51                 protected static StringFormat string_format_menu_text;
52                 protected static StringFormat string_format_menu_shortcut;
53                 protected static StringFormat string_format_menu_menubar_text;
54                 static readonly Rectangle checkbox_rect = new Rectangle (2, 2, 11,11); // Position of the checkbox relative to the item
55                 static ImageAttributes imagedisabled_attributes = null;
56                 const int SEPARATOR_HEIGHT = 5;
57                 const int SM_CXBORDER = 1;
58                 const int SM_CYBORDER = 1;              
59                 const int MENU_TAB_SPACE = 8;           // Pixels added to the width of an item because of a tab
60                 const int MENU_BAR_ITEMS_SPACE = 8;     // Space between menu bar items
61
62                 #region Principal Theme Methods
63                 public ThemeWin32Classic ()
64                 {                       
65                         defaultWindowBackColor = this.ColorWindow;
66                         defaultWindowForeColor = this.ColorControlText;
67                         default_font =  new Font (FontFamily.GenericSansSerif, 8.25f);
68                         
69                         /* Menu string formats */
70                         string_format_menu_text = new StringFormat ();
71                         string_format_menu_text.LineAlignment = StringAlignment.Center;
72                         string_format_menu_text.Alignment = StringAlignment.Near;
73                         string_format_menu_text.HotkeyPrefix = HotkeyPrefix.Show;
74
75                         string_format_menu_shortcut = new StringFormat ();      
76                         string_format_menu_shortcut.LineAlignment = StringAlignment.Center;
77                         string_format_menu_shortcut.Alignment = StringAlignment.Far;
78
79                         string_format_menu_menubar_text = new StringFormat ();
80                         string_format_menu_menubar_text.LineAlignment = StringAlignment.Center;
81                         string_format_menu_menubar_text.Alignment = StringAlignment.Center;
82                         string_format_menu_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
83                         always_draw_hotkeys = false;
84                 }       
85
86                 public override void ResetDefaults() {
87                         throw new NotImplementedException("Need to implement ResetDefaults() for Win32 theme");
88                 }
89
90                 public override bool DoubleBufferingSupported {
91                         get {return true; }
92                 }
93                 #endregion      // Principal Theme Methods
94
95                 #region Internal Methods
96                 protected SolidBrush GetControlBackBrush (Color c) {
97                         if (c == DefaultControlBackColor)
98                                 return ResPool.GetSolidBrush (ColorControl);
99                         return ResPool.GetSolidBrush (c);
100                 }
101
102                 protected SolidBrush GetControlForeBrush (Color c) {
103                         if (c == DefaultControlForeColor)
104                                 return ResPool.GetSolidBrush (ColorControlText);
105                         return ResPool.GetSolidBrush (c);
106                 }
107                 #endregion      // Internal Methods
108
109                 #region OwnerDraw Support
110                 public  override void DrawOwnerDrawBackground (DrawItemEventArgs e)
111                 {
112                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
113                                 e.Graphics.FillRectangle (SystemBrushes.Highlight, e.Bounds);
114                                 return;
115                         }
116
117                         e.Graphics.FillRectangle (GetControlBackBrush (e.BackColor), e.Bounds);
118                 }
119
120                 public  override void DrawOwnerDrawFocusRectangle (DrawItemEventArgs e)
121                 {
122                         if (e.State == DrawItemState.Focus)
123                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, e.ForeColor, e.BackColor);
124                 }
125                 #endregion      // OwnerDraw Support
126
127                 #region ButtonBase
128                 public override void DrawButtonBase(Graphics dc, Rectangle clip_area, ButtonBase button) {
129                         // Draw the button: fill rectangle, draw border, etc.
130                         ButtonBase_DrawButton(button, dc);
131                         
132                         // First, draw the image
133                         if ((button.image != null) || (button.image_list != null))
134                                 ButtonBase_DrawImage(button, dc);
135                         
136                         // Draw the focus rectangle
137                         if ((button.has_focus || button.paint_as_acceptbutton) && button.is_enabled)
138                                 ButtonBase_DrawFocus(button, dc);
139                         
140                         // Now the text
141                         if (button.text != null && button.text != String.Empty)
142                                 ButtonBase_DrawText(button, dc);
143                 }
144
145                 protected virtual void ButtonBase_DrawButton (ButtonBase button, Graphics dc)
146                 {
147                         Rectangle borderRectangle;
148                         bool check_or_radio = false;
149                         bool check_or_radio_checked = false;
150                         
151                         CPColor cpcolor = ResPool.GetCPColor (button.BackColor);
152                         
153                         dc.FillRectangle (ResPool.GetSolidBrush (button.BackColor), button.ClientRectangle);
154                         
155                         if (button is CheckBox) {
156                                 check_or_radio = true;
157                                 check_or_radio_checked = ((CheckBox)button).Checked;
158                         } else if (button is RadioButton) {
159                                 check_or_radio = true;
160                                 check_or_radio_checked = ((RadioButton)button).Checked;
161                         }
162                         
163                         if ((button.has_focus || button.paint_as_acceptbutton) && button.is_enabled && !check_or_radio) {
164                                 // shrink the rectangle for the normal button drawing inside the focus rectangle
165                                 borderRectangle = Rectangle.Inflate (button.ClientRectangle, -1, -1);
166                         } else {
167                                 borderRectangle = button.ClientRectangle;
168                         }
169                         
170                         if (button.FlatStyle == FlatStyle.Popup) {
171                                 if (!button.is_pressed && !button.is_entered && !check_or_radio_checked)
172                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor);
173                                 else if (!button.is_pressed && button.is_entered &&!check_or_radio_checked)
174                                         Internal_DrawButton (dc, borderRectangle, 2, cpcolor);
175                                 else if (button.is_pressed || check_or_radio_checked)
176                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor);
177                         } else if (button.FlatStyle == FlatStyle.Flat) {
178                                 if (button.is_entered && !button.is_pressed && !check_or_radio_checked)
179                                         dc.FillRectangle (ResPool.GetSolidBrush (cpcolor.Dark), borderRectangle);
180                                 else if (button.is_pressed || check_or_radio_checked) {
181                                         dc.FillRectangle (ResPool.GetSolidBrush (cpcolor.Light), borderRectangle);
182                                         
183                                         dc.DrawRectangle (ResPool.GetPen (cpcolor.Dark), borderRectangle.X + 4, borderRectangle.Y + 4,
184                                                           borderRectangle.Width - 9, borderRectangle.Height - 9);
185                                 }
186                                 
187                                 Internal_DrawButton (dc, borderRectangle, 3, cpcolor);
188                         } else {
189                                 if ((!button.is_pressed || !button.is_enabled) && !check_or_radio_checked)
190                                         Internal_DrawButton (dc, borderRectangle, 0, cpcolor);
191                                 else
192                                         Internal_DrawButton (dc, borderRectangle, 1, cpcolor);
193                         }
194                 }
195                 
196                 private void Internal_DrawButton (Graphics dc, Rectangle rect, int state, CPColor cpcolor)
197                 {
198                         switch (state) {
199                         case 0: // normal or normal disabled button
200                                 Pen pen = ResPool.GetPen (cpcolor.Light);
201                                 dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
202                                 dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
203                                 
204                                 pen = ResPool.GetPen (cpcolor.Dark);
205                                 dc.DrawLine (pen, rect.X + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
206                                 dc.DrawLine (pen, rect.Right - 2, rect.Y + 1, rect.Right - 2, rect.Bottom - 3);
207                                 
208                                 pen = ResPool.GetPen (cpcolor.DarkDark);
209                                 dc.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
210                                 dc.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 2);
211                                 break;
212                         case 1: // popup button normal (or pressed normal or popup button)
213                                 pen = ResPool.GetPen (cpcolor.Dark);
214                                 dc.DrawRectangle (pen, new Rectangle (rect.X, rect.Y, rect.Width - 1, rect.Height - 1));
215                                 break;
216                         case 2: // popup button poped up
217                                 pen = ResPool.GetPen (cpcolor.Light);
218                                 dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
219                                 dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
220                                 
221                                 pen = ResPool.GetPen (cpcolor.Dark);
222                                 dc.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
223                                 dc.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 2);
224                                 break;
225                         case 3: // flat button not entered
226                                 pen = ResPool.GetPen (cpcolor.DarkDark);
227                                 dc.DrawRectangle (pen, new Rectangle (rect.X, rect.Y, rect.Width - 1, rect.Height - 1));
228                                 break;
229                         default:
230                                 break;
231                         }
232                 }
233                 
234                 protected virtual void ButtonBase_DrawImage(ButtonBase button, Graphics dc)
235                 {
236                         // Need to draw a picture
237                         Image   i;
238                         int     image_x;
239                         int     image_y;
240                         int     image_width;
241                         int     image_height;
242                         
243                         int width = button.ClientSize.Width;
244                         int height = button.ClientSize.Height;
245                         
246                         if (button.ImageIndex != -1) {   // We use ImageIndex instead of image_index since it will return -1 if image_list is null
247                                 i = button.image_list.Images[button.image_index];
248                         } else {
249                                 i = button.image;
250                         }
251                         
252                         image_width = button.image.Width;
253                         image_height = button.image.Height;
254                         
255                         switch (button.image_alignment) {
256                                 case ContentAlignment.TopLeft: {
257                                         image_x=0;
258                                         image_y=0;
259                                         break;
260                                 }
261                                         
262                                 case ContentAlignment.TopCenter: {
263                                         image_x=(width-image_width)/2;
264                                         image_y=0;
265                                         break;
266                                 }
267                                         
268                                 case ContentAlignment.TopRight: {
269                                         image_x=width-image_width;
270                                         image_y=0;
271                                         break;
272                                 }
273                                         
274                                 case ContentAlignment.MiddleLeft: {
275                                         image_x=0;
276                                         image_y=(height-image_height)/2;
277                                         break;
278                                 }
279                                         
280                                 case ContentAlignment.MiddleCenter: {
281                                         image_x=(width-image_width)/2;
282                                         image_y=(height-image_height)/2;
283                                         break;
284                                 }
285                                         
286                                 case ContentAlignment.MiddleRight: {
287                                         image_x=width-image_width;
288                                         image_y=(height-image_height)/2;
289                                         break;
290                                 }
291                                         
292                                 case ContentAlignment.BottomLeft: {
293                                         image_x=0;
294                                         image_y=height-image_height;
295                                         break;
296                                 }
297                                         
298                                 case ContentAlignment.BottomCenter: {
299                                         image_x=(width-image_width)/2;
300                                         image_y=height-image_height;
301                                         break;
302                                 }
303                                         
304                                 case ContentAlignment.BottomRight: {
305                                         image_x=width-image_width;
306                                         image_y=height-image_height;
307                                         break;
308                                 }
309                                         
310                                 default: {
311                                         image_x=0;
312                                         image_y=0;
313                                         break;
314                                 }
315                         }
316                         
317                         if (button.is_pressed) {
318                                 image_x+=1;
319                                 image_y+=1;
320                         }
321                         
322                         if (button.is_enabled) {
323                                 dc.DrawImage(i, image_x, image_y); 
324                         }
325                         else {
326                                 CPDrawImageDisabled(dc, i, image_x, image_y, ColorControl);
327                         }
328                 }
329                 
330                 protected virtual void ButtonBase_DrawFocus(ButtonBase button, Graphics dc)
331                 {
332                         Color focus_color = button.ForeColor;
333                         
334                         if (button.FlatStyle == FlatStyle.Popup)
335                                 if (!button.is_pressed)
336                                         focus_color = ControlPaint.Dark(button.BackColor);
337                         
338                         dc.DrawRectangle (ResPool.GetPen (focus_color), new Rectangle (button.ClientRectangle.X, button.ClientRectangle.Y, 
339                                                                                        button.ClientRectangle.Width - 1, button.ClientRectangle.Height - 1));
340                 }
341                 
342                 protected virtual void ButtonBase_DrawText(ButtonBase button, Graphics dc)
343                 {
344                         Rectangle buttonRectangle = button.ClientRectangle;
345                         Rectangle text_rect = Rectangle.Inflate(buttonRectangle, -4, -4);
346                         
347                         if (button.is_pressed) {
348                                 text_rect.X++;
349                                 text_rect.Y++;
350                         }
351                         
352                         if (button.is_enabled) {                                        
353                                 dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (button.ForeColor), text_rect, button.text_format);
354                         } else {
355                                 if (button.FlatStyle == FlatStyle.Flat || button.FlatStyle == FlatStyle.Popup) {
356                                         dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (ColorGrayText), text_rect, button.text_format);
357                                 } else {
358                                         Rectangle rect_white = new Rectangle (text_rect.X + 1, text_rect.Y + 1, text_rect.Width, text_rect.Height);
359                                         
360                                         dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (ColorControlLightLight), rect_white, button.text_format);
361                                         dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (ColorGrayText), text_rect, button.text_format);
362                                 }
363                         }
364                 }
365                 
366                 // FIXME: Not needed anymore. Will be removed when the other themes are updated
367                 // draw the flat style part of the rectangle
368                 public void DrawFlatStyleButton (Graphics graphics, Rectangle rectangle, ButtonBase button) {
369                         Color rect_back_color = button.BackColor;
370                         Color rect_fore_color = button.ForeColor;
371                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
372                                 
373                         if (button.Enabled) {
374                                 if (button.Capture || button.is_entered) {
375                                         if (button.FlatStyle == FlatStyle.Flat) {
376                                                 // fill the rectangle
377                                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
378                                                 
379                                                 // now draw the outer border
380                                                 if (button.Capture && button.is_entered) {
381                                                         rect_back_color = ControlPaint.LightLight (rect_back_color);
382                                                 } else {
383                                                         rect_back_color = ControlPaint.Light (rect_back_color);
384                                                 }
385                                                 
386                                                 // draw rectangle and fill it
387                                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
388                                                 graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
389                                         } else {
390                                                 // else it must be a popup button
391                                                 
392                                                 if (button.Capture && button.is_entered) {
393                                                         graphics.DrawRectangle(ResPool.GetPen (this.ColorControlText), trace_rectangle);
394                                                 } else {
395                                                         // draw a 3d border
396                                                         CPDrawBorder3D (graphics, rectangle, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top, button.BackColor); 
397                                                         graphics.DrawLine ( ResPool.GetPen (this.ColorControlText), trace_rectangle.X, trace_rectangle.Bottom, trace_rectangle.Right, trace_rectangle.Bottom);
398                                                         graphics.DrawLine ( ResPool.GetPen (this.ColorControlText), trace_rectangle.Right, trace_rectangle.Y, trace_rectangle.Right, trace_rectangle.Bottom);
399                                                 }
400                                         }
401                                         
402                                         // TODO: draw inner focus rectangle
403                                         
404                                 } else {
405                                         // popup has a ButtonColorText forecolor, not a button.ForeCOlor
406                                         if (button.FlatStyle == FlatStyle.Popup) {
407                                                 rect_fore_color = ColorGrayText;
408                                         }
409                                         
410                                         // fill then draw outer rect
411                                         graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
412                                         graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
413                                 }
414                                 
415                                 // finally some small tweaks to render radiobutton and checkbox
416                                 CheckBox checkbox = button as CheckBox;
417                                 RadioButton radiobutton = button as RadioButton;
418                                 if ((checkbox != null && checkbox.Checked) ||
419                                         (radiobutton != null && radiobutton.Checked)) {
420                                         if (button.FlatStyle == FlatStyle.Flat && button.is_entered && !button.Capture) {
421                                                 // render the hover for flat flatstyle and cheked
422                                                 graphics.DrawRectangle(ResPool.GetPen (this.ColorControlText), trace_rectangle);
423                                         } else if (!button.is_entered && !button.Capture) {
424                                                 // render the checked state for popup when unhovered
425                                                 CPDrawBorder3D (graphics, rectangle, Border3DStyle.SunkenInner, Border3DSide.Right | Border3DSide.Bottom, button.BackColor); 
426                                         }
427                                 }
428                         } else {
429                                 // rendering checkbox or radio button style buttons
430                                 CheckBox checkbox = button as CheckBox;
431                                 RadioButton radiobutton = button as RadioButton;
432                                 bool draw_popup_checked = false;
433                                 
434                                 if (button.FlatStyle == FlatStyle.Popup) {
435                                         rect_fore_color = ColorGrayText;
436                                 
437                                         // see if we should draw a disabled checked popup button
438                                         draw_popup_checked = ((checkbox != null && checkbox.Checked) ||
439                                                 (radiobutton != null && radiobutton.Checked));
440                                 }
441                                 
442                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
443                                 graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
444                                 
445                                 // finally draw the flatstyle checked effect if need
446                                 if (draw_popup_checked) {
447                                         // render the checked state for popup when unhovered
448                                         CPDrawBorder3D (graphics, rectangle, Border3DStyle.SunkenInner, Border3DSide.Right | Border3DSide.Bottom, button.BackColor);
449                                 }
450                         }
451                 }
452
453                 public override Size ButtonBaseDefaultSize {
454                         get {
455                                 return new Size (75, 23);
456                         }
457                 }
458                 #endregion      // ButtonBase
459
460                 #region CheckBox
461                 public override void DrawCheckBox(Graphics dc, Rectangle clip_area, CheckBox checkbox) {
462                         StringFormat            text_format;
463                         Rectangle               client_rectangle;
464                         Rectangle               text_rectangle;
465                         Rectangle               checkbox_rectangle;
466                         int                     checkmark_size=13;
467                         int                     checkmark_space = 4;
468
469                         client_rectangle = checkbox.ClientRectangle;
470                         text_rectangle = client_rectangle;
471                         checkbox_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, checkmark_size, checkmark_size);
472
473                         text_format = new StringFormat();
474                         text_format.Alignment=StringAlignment.Near;
475                         text_format.LineAlignment=StringAlignment.Center;
476                         text_format.HotkeyPrefix = HotkeyPrefix.Show;
477
478                         /* Calculate the position of text and checkbox rectangle */
479                         if (checkbox.appearance!=Appearance.Button) {
480                                 switch(checkbox.check_alignment) {
481                                         case ContentAlignment.BottomCenter: {
482                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
483                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
484                                                 text_rectangle.X=client_rectangle.X;
485                                                 text_rectangle.Width=client_rectangle.Width;
486                                                 text_rectangle.Height=client_rectangle.Height-checkbox_rectangle.Y-checkmark_space;
487                                                 break;
488                                         }
489
490                                         case ContentAlignment.BottomLeft: {
491                                                 checkbox_rectangle.X=client_rectangle.Left;
492                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
493                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
494                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
495                                                 break;
496                                         }
497
498                                         case ContentAlignment.BottomRight: {
499                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
500                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
501                                                 text_rectangle.X=client_rectangle.X;
502                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
503                                                 break;
504                                         }
505
506                                         case ContentAlignment.MiddleCenter: {
507                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
508                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
509                                                 text_rectangle.X=client_rectangle.X;
510                                                 text_rectangle.Width=client_rectangle.Width;
511                                                 break;
512                                         }
513
514                                         default:
515                                         case ContentAlignment.MiddleLeft: {
516                                                 checkbox_rectangle.X=client_rectangle.Left;
517                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
518                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
519                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                                                                             
520                                                 break;
521                                         }
522
523                                         case ContentAlignment.MiddleRight: {
524                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
525                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
526                                                 text_rectangle.X=client_rectangle.X;
527                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
528                                                 break;
529                                         }
530
531                                         case ContentAlignment.TopCenter: {
532                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
533                                                 checkbox_rectangle.Y=client_rectangle.Top;
534                                                 text_rectangle.X=client_rectangle.X;
535                                                 text_rectangle.Width=client_rectangle.Width;
536                                                 text_rectangle.Y=checkmark_size+checkmark_space;
537                                                 text_rectangle.Height=client_rectangle.Height-checkmark_size-checkmark_space;
538                                                 break;
539                                         }
540
541                                         case ContentAlignment.TopLeft: {
542                                                 checkbox_rectangle.X=client_rectangle.Left;
543                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
544                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
545                                                 break;
546                                         }
547
548                                         case ContentAlignment.TopRight: {
549                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
550                                                 text_rectangle.X=client_rectangle.X;
551                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
552                                                 break;
553                                         }
554                                 }
555                         } else {
556                                 text_rectangle.X=client_rectangle.X;
557                                 text_rectangle.Width=client_rectangle.Width;
558                         }
559                         
560                         /* Set the horizontal alignment of our text */
561                         switch(checkbox.text_alignment) {
562                                 case ContentAlignment.BottomLeft:
563                                 case ContentAlignment.MiddleLeft:
564                                 case ContentAlignment.TopLeft: {
565                                         text_format.Alignment=StringAlignment.Near;
566                                         break;
567                                 }
568
569                                 case ContentAlignment.BottomCenter:
570                                 case ContentAlignment.MiddleCenter:
571                                 case ContentAlignment.TopCenter: {
572                                         text_format.Alignment=StringAlignment.Center;
573                                         break;
574                                 }
575
576                                 case ContentAlignment.BottomRight:
577                                 case ContentAlignment.MiddleRight:
578                                 case ContentAlignment.TopRight: {
579                                         text_format.Alignment=StringAlignment.Far;
580                                         break;
581                                 }
582                         }
583
584                         /* Set the vertical alignment of our text */
585                         switch(checkbox.text_alignment) {
586                                 case ContentAlignment.TopLeft: 
587                                 case ContentAlignment.TopCenter: 
588                                 case ContentAlignment.TopRight: {
589                                         text_format.LineAlignment=StringAlignment.Near;
590                                         break;
591                                 }
592
593                                 case ContentAlignment.BottomLeft:
594                                 case ContentAlignment.BottomCenter:
595                                 case ContentAlignment.BottomRight: {
596                                         text_format.LineAlignment=StringAlignment.Far;
597                                         break;
598                                 }
599
600                                 case ContentAlignment.MiddleLeft:
601                                 case ContentAlignment.MiddleCenter:
602                                 case ContentAlignment.MiddleRight: {
603                                         text_format.LineAlignment=StringAlignment.Center;
604                                         break;
605                                 }
606                         }
607
608                         ButtonState state = ButtonState.Normal;
609                         if (checkbox.FlatStyle == FlatStyle.Flat) {
610                                 state |= ButtonState.Flat;
611                         }
612                         
613                         if (checkbox.Checked) {
614                                 state |= ButtonState.Checked;
615                         }
616                         
617                         if (checkbox.ThreeState && (checkbox.CheckState == CheckState.Indeterminate)) {
618                                 state |= ButtonState.Checked;
619                                 state |= ButtonState.Pushed;                            
620                         }
621                         
622                         // FIXME: can be removed when other themes are updated ->
623                         // finally make sure the pushed and inavtive states are rendered
624                         if (!checkbox.Enabled) {
625                                 state |= ButtonState.Inactive;
626                         }
627                         else if (checkbox.is_pressed) {
628                                 state |= ButtonState.Pushed;
629                         }
630                         // <- until here
631                         
632                         // Start drawing
633                         
634                         CheckBox_DrawCheckBox(dc, checkbox, state, checkbox_rectangle);
635                         
636                         if ((checkbox.image != null) || (checkbox.image_list != null))
637                                 ButtonBase_DrawImage(checkbox, dc);
638                         
639                         CheckBox_DrawText(checkbox, text_rectangle, dc, text_format);
640
641                         CheckBox_DrawFocus(checkbox, dc, text_rectangle);
642
643                         text_format.Dispose ();
644                 }
645
646                 protected virtual void CheckBox_DrawCheckBox( Graphics dc, CheckBox checkbox, ButtonState state, Rectangle checkbox_rectangle )
647                 {
648                         dc.FillRectangle (ResPool.GetSolidBrush (checkbox.BackColor), checkbox.ClientRectangle);                        
649                         // render as per normal button
650                         if (checkbox.appearance==Appearance.Button) {
651                                 ButtonBase_DrawButton (checkbox, dc);
652                         } else {
653                                 // establish if we are rendering a flat style of some sort
654                                 if (checkbox.FlatStyle == FlatStyle.Flat || checkbox.FlatStyle == FlatStyle.Popup) {
655                                         DrawFlatStyleCheckBox (dc, checkbox_rectangle, checkbox);
656                                 } else {
657                                         DrawNormalCheckBox (dc, checkbox_rectangle, checkbox);
658                                 }
659                         }
660                 }
661                 
662                 protected virtual void CheckBox_DrawText( CheckBox checkbox, Rectangle text_rectangle, Graphics dc, StringFormat text_format )
663                 {
664                         DrawCheckBox_and_RadioButtonText (checkbox, text_rectangle, dc, 
665                                                           text_format, checkbox.Appearance, checkbox.Checked);
666                 }
667                 
668                 protected virtual void CheckBox_DrawFocus( CheckBox checkbox, Graphics dc, Rectangle text_rectangle )
669                 {
670                         // do nothing here. maybe an other theme needs it
671                 }
672                 
673                 protected virtual void DrawNormalCheckBox (Graphics graphics, Rectangle rectangle, CheckBox checkbox)
674                 {
675                         Pen                     pen;
676                         int                     lineWidth;
677                         Rectangle       rect;
678                         int                     Scale;
679                         
680                         /* Goes first, affects the background */
681                         if (checkbox.is_pressed || !checkbox.is_enabled) {
682                                 graphics.FillRectangle(SystemBrushes.Control, rectangle);
683                         } else {
684                                 graphics.FillRectangle(SystemBrushes.Window, rectangle);
685                         }
686                         
687                         /* Draw the sunken frame */
688                         if (checkbox.FlatStyle == FlatStyle.Flat) {
689                                 ControlPaint.DrawBorder(graphics, rectangle, ColorControlDark, ButtonBorderStyle.Solid);
690                         } else {
691                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
692                         }
693                         
694                         /* Make sure we've got at least a line width of 1 */
695                         lineWidth=Math.Max(3, rectangle.Width/6);
696                         Scale=Math.Max(1, rectangle.Width/12);
697                         
698                         // define a rectangle inside the border area
699                         rect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-4, rectangle.Height-4);
700                         if (!checkbox.is_enabled) {
701                                 pen=SystemPens.ControlDark;
702                         } else {
703                                 pen=SystemPens.ControlText;
704                         }
705                         
706                         if (checkbox.Checked) {
707                                 /* Need to draw a check-mark */
708                                 for (int i=0; i<lineWidth; i++) {
709                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
710                                         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);
711                                 }
712                         }
713                 }
714                 
715                 // renders a checkBox with the Flat and Popup FlatStyle
716                 protected virtual void DrawFlatStyleCheckBox (Graphics graphics, Rectangle rectangle, CheckBox checkbox)
717                 {
718                         Pen                     pen;                    
719                         Rectangle       rect;
720                         Rectangle       checkbox_rectangle;
721                         Rectangle       fill_rectangle;
722                         int                     lineWidth;
723                         int                     Scale;
724                         
725                         // set up our rectangles first
726                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered) {
727                                 // clip one pixel from bottom right for non popup rendered checkboxes
728                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-1, 0), Math.Max(rectangle.Height-1,0));
729                                 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));
730                         } else {
731                                 // clip two pixels from bottom right for non popup rendered checkboxes
732                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-2, 0), Math.Max(rectangle.Height-2,0));
733                                 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));
734                         }       
735                                 
736                         // if disabled render in disabled state
737                         if (checkbox.Enabled) {
738                                 // process the state of the checkbox
739                                 if (checkbox.is_entered || checkbox.Capture) {
740                                         // decide on which background color to use
741                                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered && checkbox.Capture) {
742                                                 graphics.FillRectangle(ResPool.GetSolidBrush (checkbox.BackColor), fill_rectangle);
743                                         } else if (checkbox.FlatStyle == FlatStyle.Flat && !(checkbox.is_entered && checkbox.Capture)) {
744                                                 graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.Light(checkbox.BackColor)), fill_rectangle);
745                                         } else {
746                                                 // use regular window background color
747                                                 graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);
748                                         }
749                                         
750                                         // render the outer border
751                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
752                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
753                                         } else {
754                                                 // draw sunken effect
755                                                 CPDrawBorder3D (graphics, checkbox_rectangle, Border3DStyle.SunkenInner, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, checkbox.BackColor);
756                                         }
757                                 } else {
758                                         graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);                           
759                                         
760                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
761                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
762                                         } else {
763                                                 // draw the outer border
764                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ControlPaint.DarkDark (checkbox.BackColor), ButtonBorderStyle.Solid);
765                                         }                       
766                                 }
767                         } else {
768                                 if (checkbox.FlatStyle == FlatStyle.Popup) {
769                                         graphics.FillRectangle(SystemBrushes.Control, fill_rectangle);
770                                 }       
771                         
772                                 // draw disabled state,
773                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ColorControlDark, ButtonBorderStyle.Solid);
774                         }               
775                         
776                         /* Make sure we've got at least a line width of 1 */
777                         lineWidth = Math.Max(3, fill_rectangle.Width/3);
778                         Scale=Math.Max(1, fill_rectangle.Width/9);
779                         
780                         // flat style check box is rendered inside a rectangle shifted down by one
781                         rect=new Rectangle(fill_rectangle.X, fill_rectangle.Y+1, fill_rectangle.Width, fill_rectangle.Height);
782                         if (checkbox.Enabled) {
783                                 pen=ResPool.GetPen(checkbox.ForeColor);
784                         } else {
785                                 pen=SystemPens.ControlDark;
786                         }
787
788                         if (checkbox.Checked) {
789                                 /* Need to draw a check-mark */
790                                 for (int i=0; i<lineWidth; i++) {
791                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
792                                         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);
793                                 }
794                         }                                       
795                 }
796
797                 private void DrawCheckBox_and_RadioButtonText (ButtonBase button_base, Rectangle text_rectangle, Graphics dc, 
798                                                                StringFormat text_format, Appearance appearance, bool ischecked)
799                 {
800                         // offset the text if it's pressed and a button
801                         if (appearance == Appearance.Button) {
802                                 if (ischecked || (button_base.Capture && button_base.FlatStyle != FlatStyle.Flat)) {
803                                         text_rectangle.X ++;
804                                         text_rectangle.Y ++;
805                                 }
806                                 
807                                 text_rectangle.Inflate (-4, -4);
808                         }
809                         
810                         /* Place the text; to be compatible with Windows place it after the checkbox has been drawn */
811                         
812                         // Windows seems to not wrap text in certain situations, this matches as close as I could get it
813                         if ((float)(button_base.Font.Height * 1.5f) > text_rectangle.Height) {
814                                 text_format.FormatFlags |= StringFormatFlags.NoWrap;
815                         }
816                         if (button_base.Enabled) {
817                                 dc.DrawString (button_base.Text, button_base.Font, ResPool.GetSolidBrush (button_base.ForeColor), text_rectangle, text_format);                 
818                         } else if (button_base.FlatStyle == FlatStyle.Flat || button_base.FlatStyle == FlatStyle.Popup) {
819                                 dc.DrawString (button_base.Text, button_base.Font, ResPool.GetSolidBrush (ControlPaint.DarkDark (this.ColorControl)), text_rectangle, text_format);
820                         } else {
821                                 CPDrawStringDisabled (dc, button_base.Text, button_base.Font, ColorControlText, text_rectangle, text_format);
822                         }
823                 }
824                 #endregion      // CheckBox
825                 
826                 #region CheckedListBox
827                 
828                 public override Rectangle CheckedListBoxCheckRectangle ()
829                 {
830                         return checkbox_rect;
831                 }
832                 
833                 public override void DrawCheckedListBoxItem (CheckedListBox ctrl, DrawItemEventArgs e)
834                 {                       
835                         Color back_color, fore_color;
836                         Rectangle item_rect = e.Bounds;
837                         ButtonState state;
838                         StringFormat string_format = ctrl.GetFormatString ();
839
840                         /* Draw checkbox */             
841
842                         if ((ctrl.Items.GetListBoxItem (e.Index)).State == CheckState.Checked)
843                                 state = ButtonState.Checked;
844                         else
845                                 state = ButtonState.Normal;
846
847                         if (ctrl.ThreeDCheckBoxes == false)
848                                 state |= ButtonState.Flat;
849
850                         ControlPaint.DrawCheckBox (e.Graphics,
851                                 item_rect.X + checkbox_rect.X, item_rect.Y + checkbox_rect.Y,
852                                 checkbox_rect.Width, checkbox_rect.Height,
853                                 state);
854
855                         item_rect.X += checkbox_rect.Width + checkbox_rect.X * 2;
856                         item_rect.Width -= checkbox_rect.Width + checkbox_rect.X * 2;
857                         
858                         /* Draw text*/
859                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
860                                 back_color = ColorHighlight;
861                                 fore_color = ColorHighlightText;
862                         }
863                         else {
864                                 back_color = e.BackColor;
865                                 fore_color = e.ForeColor;
866                         }
867                         
868                         e.Graphics.FillRectangle (ResPool.GetSolidBrush
869                                 (back_color), item_rect);
870
871                         e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
872                                 ResPool.GetSolidBrush (fore_color),
873                                 item_rect, string_format);
874                                         
875                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
876                                 CPDrawFocusRectangle (e.Graphics, item_rect,
877                                         fore_color, back_color);
878                         }
879                 }
880                 
881                 #endregion // CheckedListBox
882                 
883                 #region ComboBox
884                 
885                 // Drawing
886                 // TODO: get rid of that and use BorderStyle Fixed3D
887                 public override void DrawComboBoxEditDecorations (Graphics dc, ComboBox ctrl, Rectangle cl)
888                 {                               
889                         dc.DrawLine (ResPool.GetPen (ColorControlDark), cl.X, cl.Y, cl.X + cl.Width, cl.Y); //top 
890                         dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), cl.X + 1, cl.Y + 1, cl.X + cl.Width - 2, cl.Y + 1);
891                         dc.DrawLine (ResPool.GetPen (ColorControl), cl.X, cl.Y + cl.Height - 2, cl.X + cl.Width, cl.Y + cl.Height - 2); //down
892                         dc.DrawLine (ResPool.GetPen (ColorControlLight), cl.X, cl.Y + cl.Height - 1, cl.X + cl.Width, cl.Y + cl.Height - 1);
893                         dc.DrawLine (ResPool.GetPen (ColorControlDark), cl.X, cl.Y, cl.X, cl.Y + cl.Height); //left
894                         dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), cl.X + 1, cl.Y + 1, cl.X + 1, cl.Y + cl.Height - 2); 
895                         dc.DrawLine (ResPool.GetPen (ColorControl), cl.X + cl.Width - 1, cl.Y, cl.X + cl.Width - 1, cl.Y + cl.Height); //right
896                         dc.DrawLine (ResPool.GetPen (ColorControlLight), cl.X + cl.Width - 2, cl.Y + 1 , cl.X + cl.Width - 2, cl.Y + cl.Height - 1);                            
897                 }               
898                 
899                 // Sizing                               
900                 public override int DrawComboBoxEditDecorationTop () { return 2;}
901                 public override int DrawComboBoxEditDecorationBottom () { return 2;}
902                 public override int DrawComboBoxEditDecorationRight () { return 2;}
903                 public override int DrawComboBoxEditDecorationLeft () { return 2;}
904                 
905                 private int DrawComboListBoxDecoration (ComboBoxStyle style)
906                 {
907                         if (style == ComboBoxStyle.Simple)
908                                 return 2;
909                         else
910                                 return 1;
911                 }
912                                 
913                 public override int DrawComboListBoxDecorationTop (ComboBoxStyle style) 
914                 {
915                         return DrawComboListBoxDecoration (style);
916                 }
917                 
918                 public override int DrawComboListBoxDecorationBottom (ComboBoxStyle style)
919                 {
920                         return DrawComboListBoxDecoration (style);
921                 }
922                 
923                 public override int DrawComboListBoxDecorationRight (ComboBoxStyle style)
924                 {
925                         return DrawComboListBoxDecoration (style);
926                 }
927                 
928                 public override int DrawComboListBoxDecorationLeft (ComboBoxStyle style)
929                 {
930                         return DrawComboListBoxDecoration (style);
931                 }
932                 
933                 public override void DrawComboListBoxDecorations (Graphics dc, ComboBox ctrl, Rectangle cl)
934                 {
935                         if (ctrl.DropDownStyle == ComboBoxStyle.Simple) {
936                                 DrawComboBoxEditDecorations (dc, ctrl, cl);
937                         }
938                         else {                  
939                                 dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame),
940                                         cl.X, cl.Y, cl.Width - 1, cl.Height - 1);
941                         }                       
942                 }
943                 
944                 public override void DrawComboBoxItem (ComboBox ctrl, DrawItemEventArgs e)
945                 {
946                         Color back_color, fore_color;
947                         Rectangle text_draw = e.Bounds;
948                         StringFormat string_format = new StringFormat ();
949                         string_format.FormatFlags = StringFormatFlags.LineLimit;
950                         
951                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
952                                 back_color = ColorHighlight;
953                                 fore_color = ColorHighlightText;
954                         }
955                         else {
956                                 back_color = e.BackColor;
957                                 fore_color = e.ForeColor;
958                         }                       
959                                                         
960                         e.Graphics.FillRectangle (ResPool.GetSolidBrush (back_color), e.Bounds);
961
962                         if (e.Index != -1) {
963                                 e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
964                                         ResPool.GetSolidBrush (fore_color),
965                                         text_draw, string_format);
966                         }
967                         
968                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
969                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, fore_color, back_color);
970                         }
971
972                         string_format.Dispose ();
973                 }
974                 
975                 #endregion ComboBox
976                 
977                 #region Datagrid
978                 public override int DataGridPreferredColumnWidth { get { return 75;} }
979                 public override int DataGridMinimumColumnCheckBoxHeight { get { return 16;} }
980                 public override int DataGridMinimumColumnCheckBoxWidth { get { return 16;} }
981                 public override Color DataGridAlternatingBackColor { get { return ColorWindow;} }
982                 public override Color DataGridBackColor { get  { return  ColorWindow;} }                
983                 public override Color DataGridBackgroundColor { get  { return  ColorAppWorkspace;} }
984                 public override Color DataGridCaptionBackColor { get  { return ColorActiveCaption;} }
985                 public override Color DataGridCaptionForeColor { get  { return ColorActiveCaptionText;} }
986                 public override Color DataGridGridLineColor { get { return ColorControl;} }
987                 public override Color DataGridHeaderBackColor { get  { return ColorControl;} }
988                 public override Color DataGridHeaderForeColor { get  { return ColorControlText;} }
989                 public override Color DataGridLinkColor { get  { return ColorHotTrack;} }
990                 public override Color DataGridLinkHoverColor { get  { return ColorHotTrack;} }
991                 public override Color DataGridParentRowsBackColor { get  { return ColorControl;} }
992                 public override Color DataGridParentRowsForeColor { get  { return ColorWindowText;} }
993                 public override Color DataGridSelectionBackColor { get  { return ColorActiveCaption;} }
994                 public override Color DataGridSelectionForeColor { get  { return ColorActiveCaptionText;} }
995                 
996                 public override void DataGridPaint (PaintEventArgs pe, DataGrid grid)
997                 {
998                         // Draw parent rows
999                         if (pe.ClipRectangle.IntersectsWith (grid.grid_drawing.parent_rows)) {
1000                                 pe.Graphics.FillRectangle (ResPool.GetSolidBrush (grid.ParentRowsBackColor), grid.grid_drawing.parent_rows);
1001                         }
1002
1003                         DataGridPaintCaption (pe.Graphics, pe.ClipRectangle, grid);
1004                         DataGridPaintColumnsHdrs (pe.Graphics, pe.ClipRectangle, grid);
1005                         DataGridPaintRowsHeaders (pe.Graphics, pe.ClipRectangle, grid);
1006                         DataGridPaintRows (pe.Graphics, grid.grid_drawing.cells_area, pe.ClipRectangle, grid);
1007
1008                         // Paint scrollBar corner
1009                         if (grid.vert_scrollbar.Visible && grid.horiz_scrollbar.Visible) {
1010
1011                                 Rectangle corner = new Rectangle (grid.ClientRectangle.X + grid.ClientRectangle.Width - grid.horiz_scrollbar.Height,
1012                                          grid.ClientRectangle.Y + grid.ClientRectangle.Height - grid.horiz_scrollbar.Height,
1013                                          grid.horiz_scrollbar.Height, grid.horiz_scrollbar.Height);
1014
1015                                 if (pe.ClipRectangle.IntersectsWith (corner)) {
1016                                         pe.Graphics.FillRectangle (ResPool.GetSolidBrush (grid.ParentRowsBackColor),
1017                                                 corner);
1018                                 }
1019                         }
1020                 }
1021                 
1022                 public override void DataGridPaintCaption (Graphics g, Rectangle clip, DataGrid grid)
1023                 {
1024                         Rectangle modified_area = clip;
1025                         modified_area.Intersect (grid.grid_drawing.caption_area);
1026
1027                         g.FillRectangle (ResPool.GetSolidBrush (grid.CaptionBackColor),
1028                                 modified_area);
1029
1030                         g.DrawString (grid.CaptionText, grid.CaptionFont,
1031                                 ResPool.GetSolidBrush (grid.CaptionForeColor),
1032                                 grid.grid_drawing.caption_area);                
1033                 }
1034
1035                 public override void DataGridPaintColumnsHdrs (Graphics g, Rectangle clip, DataGrid grid)
1036                 {
1037                         Rectangle columns_area = grid.grid_drawing.columnshdrs_area;
1038
1039                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) { // Paint corner shared between row and column header
1040                                 Rectangle rect_bloc = grid.grid_drawing.columnshdrs_area;
1041                                 rect_bloc.Width = grid.RowHeaderWidth;
1042                                 rect_bloc.Height = grid.grid_drawing.columnshdrs_area.Height;
1043                                 if (clip.IntersectsWith (rect_bloc)) {
1044                                         if (grid.visiblecolumn_count > 0) {
1045                                                 g.FillRectangle (ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderBackColor), rect_bloc);
1046                                         }else {
1047                                                 g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), rect_bloc);
1048                                         }
1049                                 }
1050
1051                                 columns_area.X += grid.RowHeaderWidth;
1052                                 columns_area.Width -= grid.RowHeaderWidth;
1053                         }
1054
1055                         // Set unused area
1056                         Rectangle columnshdrs_area_complete = columns_area;
1057                         columnshdrs_area_complete.Width = grid.grid_drawing.columnshdrs_maxwidth;
1058                         
1059                         if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
1060                                 columnshdrs_area_complete.Width -= grid.RowHeaderWidth;
1061                         }               
1062
1063                         // Set column painting
1064                         Rectangle rect_columnhdr = new Rectangle ();
1065                         int col_pixel;
1066                         Region current_clip;
1067                         rect_columnhdr.Y = columns_area.Y;
1068                         rect_columnhdr.Height = columns_area.Height;
1069                         
1070                         current_clip = new Region (columns_area);
1071                         g.Clip = current_clip;
1072                         int column_cnt = grid.first_visiblecolumn + grid.visiblecolumn_count;
1073                         for (int column = grid.first_visiblecolumn; column < column_cnt; column++) {
1074                                 
1075                                 col_pixel = grid.grid_drawing.GetColumnStartingPixel (column);
1076                                 rect_columnhdr.X = columns_area.X + col_pixel - grid.horz_pixeloffset;
1077                                 rect_columnhdr.Width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
1078
1079                                 if (clip.IntersectsWith (rect_columnhdr) == false)
1080                                         continue;
1081
1082                                 grid.CurrentTableStyle.GridColumnStyles[column].PaintHeader (g, rect_columnhdr, column);
1083
1084                                 
1085                         }
1086
1087                         current_clip.Dispose ();
1088                         g.ResetClip ();
1089                                 
1090                         Rectangle not_usedarea = columnshdrs_area_complete;                             
1091                         not_usedarea.X = rect_columnhdr.X + rect_columnhdr.Width;
1092                         not_usedarea.Width = grid.ClientRectangle.X + grid.ClientRectangle.Width - rect_columnhdr.X - rect_columnhdr.Height;            
1093                         g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
1094                         
1095                 }
1096
1097                 public override void DataGridPaintRowsHeaders (Graphics g, Rectangle clip, DataGrid grid)
1098                 {
1099                         Rectangle rowshdrs_area_complete = grid.grid_drawing.rowshdrs_area;
1100                         rowshdrs_area_complete.Height = grid.grid_drawing.rowshdrs_maxheight;
1101                         Rectangle rect_row = new Rectangle ();
1102                         rect_row.X = grid.grid_drawing.rowshdrs_area.X;                 
1103                         int rowcnt = grid.FirstVisibleRow + grid.VisibleRowCount;
1104                         Rectangle not_usedarea = rowshdrs_area_complete;                        
1105
1106                         if (rowcnt < grid.RowsCount) { // Paint one row more for partial rows
1107                                 rowcnt++;
1108                         }
1109
1110                         g.SetClip (grid.grid_drawing.rowshdrs_area);
1111                         for (int row = grid.FirstVisibleRow; row < rowcnt; row++) {
1112
1113                                 rect_row.Width = grid.grid_drawing.rowshdrs_area.Width;
1114                                 rect_row.Height = grid.RowHeight;
1115                                 rect_row.Y = grid.grid_drawing.rowshdrs_area.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
1116
1117                                 if (clip.IntersectsWith (rect_row)) {
1118                                         DataGridPaintRowHeader (g, rect_row, row, grid);                                        
1119                                 }
1120                         }
1121                         
1122                         g.ResetClip ();
1123                         not_usedarea.Height = grid.grid_drawing.rowshdrs_maxheight - grid.grid_drawing.rowshdrs_area.Height;
1124                         not_usedarea.Y = grid.grid_drawing.rowshdrs_area.Y + grid.grid_drawing.rowshdrs_area.Height;
1125                         g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
1126                 }
1127                 
1128                 public override void DataGridPaintRowHeaderArrow (Graphics g, Rectangle bounds, DataGrid grid) 
1129                 {               
1130                         Point[] arrow = new Point[3];
1131                         Point P1, P2, P3;
1132                         int centerX, centerY, shiftX;                   
1133                         Rectangle rect;
1134                         
1135                         rect = new Rectangle (bounds.X + bounds.Width /4, 
1136                                 bounds.Y + bounds.Height/4, bounds.Width / 2, bounds.Height / 2);
1137                         
1138                         centerX = rect.Left + rect.Width / 2;
1139                         centerY = rect.Top + rect.Height / 2;
1140                         shiftX = Math.Max (1, rect.Width / 8);                  
1141                         rect.X -= shiftX;
1142                         centerX -= shiftX;                      
1143                         P1 = new Point (centerX, rect.Top - 1);
1144                         P2 = new Point (centerX, rect.Bottom);
1145                         P3 = new Point (rect.Right, centerY);                   
1146                         arrow[0] = P1;
1147                         arrow[1] = P2;
1148                         arrow[2] = P3;
1149                         
1150                         g.FillPolygon (ResPool.GetSolidBrush 
1151                                 (grid.CurrentTableStyle.CurrentHeaderForeColor), arrow, FillMode.Winding);
1152                 }
1153
1154                 public override void DataGridPaintRowHeader (Graphics g, Rectangle bounds, int row, DataGrid grid)
1155                 {
1156                         // Background
1157                         g.FillRectangle (ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderBackColor),
1158                                 bounds);
1159                                 
1160                         if (grid.FlatMode == false) {
1161                                 
1162                                 // Paint Borders
1163                                 g.DrawLine (ResPool.GetPen (ColorControlLight),
1164                                         bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
1165         
1166                                 g.DrawLine (ResPool.GetPen (ColorControlLight),
1167                                         bounds.X, bounds.Y + 1, bounds.X, bounds.Y + bounds.Height - 1);
1168         
1169                                 g.DrawLine (ResPool.GetPen (ColorControlDark),
1170                                         bounds.X + bounds.Width - 1, bounds.Y + 1 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
1171         
1172                                 g.DrawLine (ResPool.GetPen (ColorControlDark),
1173                                         bounds.X, bounds.Y + bounds.Height -1, bounds.X + bounds.Width, bounds.Y  + bounds.Height -1);
1174                         }
1175
1176                         if (grid.ShowEditRow && grid.RowsCount > 0 && row == grid.RowsCount  && !(row == grid.CurrentCell.RowNumber && grid.is_changing == true)) {
1177                                 
1178                                 g.DrawString ("*", grid.grid_drawing.font_newrow, ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderForeColor),
1179                                         bounds);
1180                                 
1181                         } else {
1182                                 // Draw arrow
1183                                 if (row == grid.CurrentCell.RowNumber) {
1184         
1185                                         if (grid.is_changing == true) {
1186                                                 g.DrawString ("...", grid.Font,
1187                                                         ResPool.GetSolidBrush (grid.CurrentTableStyle.CurrentHeaderForeColor),
1188                                                         bounds);
1189         
1190                                         } else {
1191                                                 
1192                                                 Rectangle rect = new Rectangle (bounds.X - 2, bounds.Y, 18, 18);                                                                                        
1193                                                 DataGridPaintRowHeaderArrow (g, rect, grid);
1194                                         }
1195                                 }
1196                         }
1197                 }
1198                 
1199                 public override void DataGridPaintRows (Graphics g, Rectangle cells, Rectangle clip, DataGrid grid)
1200                 {
1201                         Rectangle rect_row = new Rectangle ();
1202                         Rectangle not_usedarea = new Rectangle ();
1203                         rect_row.X = cells.X;
1204
1205                         int rowcnt = grid.FirstVisibleRow + grid.VisibleRowCount;
1206                         
1207                         if (grid.ShowEditRow && grid.RowsCount > 0) {
1208                                 rowcnt--;
1209                         }                       
1210
1211                         if (rowcnt < grid.RowsCount) { // Paint one row more for partial rows
1212                                 rowcnt++;
1213                         }                       
1214                         
1215                         rect_row.Height = grid.RowHeight;
1216                         rect_row.Width = cells.Width;
1217                         for (int row = grid.FirstVisibleRow; row < rowcnt; row++) {                                                             
1218                                 rect_row.Y = cells.Y + ((row - grid.FirstVisibleRow) * grid.RowHeight);
1219                                 if (clip.IntersectsWith (rect_row)) {
1220                                         DataGridPaintRow (g, row, rect_row, false, grid);
1221                                 }
1222                         }
1223                         
1224                         if (grid.ShowEditRow && grid.RowsCount > 0 && grid.FirstVisibleRow + grid.VisibleRowCount == grid.RowsCount + 1) {
1225                                 rect_row.Y = cells.Y + ((rowcnt - grid.FirstVisibleRow) * grid.RowHeight);
1226                                 if (clip.IntersectsWith (rect_row)) {
1227                                         DataGridPaintRow (g, rowcnt, rect_row, true, grid);
1228                                 }
1229                         }                       
1230
1231                         not_usedarea.Height = cells.Y + cells.Height - rect_row.Y - rect_row.Height;
1232                         not_usedarea.Y = rect_row.Y + rect_row.Height;
1233                         not_usedarea.Width = rect_row.Width = cells.Width;
1234                         not_usedarea.X = cells.X;
1235                         
1236                         g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
1237                 }
1238                 
1239                 public override void DataGridPaintRow (Graphics g, int row, Rectangle row_rect, bool is_newrow, DataGrid grid)
1240                 {                       
1241                         Rectangle rect_cell = new Rectangle ();
1242                         int col_pixel;
1243                         Color backcolor, forecolor;
1244                         Region prev_clip = g.Clip;
1245                         Region current_clip;
1246                         Rectangle not_usedarea = new Rectangle ();
1247
1248                         rect_cell.Y = row_rect.Y;
1249                         rect_cell.Height = row_rect.Height;
1250
1251                         // PaintCells at row, column
1252                         int column_cnt = grid.first_visiblecolumn + grid.visiblecolumn_count;
1253                         for (int column = grid.first_visiblecolumn; column < column_cnt; column++) {
1254
1255                                 col_pixel = grid.grid_drawing.GetColumnStartingPixel (column);
1256
1257                                 rect_cell.X = row_rect.X + col_pixel - grid.horz_pixeloffset;
1258                                 rect_cell.Width = grid.CurrentTableStyle.GridColumnStyles[column].Width;
1259
1260                                 current_clip = new Region (row_rect);
1261                                 g.Clip = current_clip;
1262
1263                                 if (grid.IsSelected (row)) {
1264                                         backcolor =  grid.SelectionBackColor;
1265                                         forecolor =  grid.SelectionForeColor;
1266                                 } else {
1267                                         if (row % 2 == 0) {
1268                                                 backcolor =  grid.BackColor;
1269                                         } else {
1270                                                 backcolor =  grid.AlternatingBackColor;
1271                                         }
1272                                         
1273                                         forecolor =  grid.ForeColor;
1274                                 }                       
1275
1276                                 if (is_newrow) {
1277                                         grid.CurrentTableStyle.GridColumnStyles[column].PaintNewRow (g, rect_cell, 
1278                                                 ResPool.GetSolidBrush (backcolor),
1279                                                 ResPool.GetSolidBrush (forecolor));                                             
1280                                         
1281                                 } else {
1282                                         grid.CurrentTableStyle.GridColumnStyles[column].Paint (g, rect_cell, grid.ListManager, row,
1283                                                 ResPool.GetSolidBrush (backcolor),
1284                                                 ResPool.GetSolidBrush (forecolor),
1285                                                 grid.RightToLeft == RightToLeft.Yes);
1286                                 }
1287
1288                                 g.Clip = prev_clip;
1289                                 current_clip.Dispose ();
1290                         }
1291                         
1292                         if (row_rect.X + row_rect.Width > rect_cell.X + rect_cell.Width) {
1293
1294                                 not_usedarea.X = rect_cell.X + rect_cell.Width;
1295                                 not_usedarea.Width = row_rect.X + row_rect.Width - rect_cell.X - rect_cell.Width;
1296                                 not_usedarea.Y = row_rect.Y;
1297                                 not_usedarea.Height = row_rect.Height;
1298                                 g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor),
1299                                         not_usedarea);
1300                         }
1301                 }
1302                 
1303                 #endregion // Datagrid
1304                 
1305                 #region DateTimePicker
1306         
1307                 public override void DrawDateTimePicker (Graphics dc,  Rectangle clip_rectangle, DateTimePicker dtp) {
1308                         // if not showing the numeric updown control then render border
1309                         if (!dtp.ShowUpDown && clip_rectangle.IntersectsWith (dtp.ClientRectangle)) {
1310                                 // draw the outer border
1311                                 Rectangle button_bounds = dtp.ClientRectangle;
1312                                 this.CPDrawBorder3D (dc, button_bounds, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, dtp.BackColor);
1313                                 
1314                                 // deflate by the border width
1315                                 if (clip_rectangle.IntersectsWith (dtp.drop_down_arrow_rect)) {
1316                                         button_bounds.Inflate (-2,-2);
1317                                         ButtonState state = dtp.is_drop_down_visible ? ButtonState.Pushed : ButtonState.Normal;
1318                                         this.CPDrawComboButton ( 
1319                                           dc, 
1320                                           dtp.drop_down_arrow_rect, 
1321                                           state);
1322                                 }
1323                         }
1324
1325                         // render the date part
1326                         if (clip_rectangle.IntersectsWith (dtp.date_area_rect)) {
1327                                 // fill the background
1328                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorWindow), dtp.date_area_rect);
1329                                 
1330                                 // fill the currently highlighted area
1331                                 if (dtp.hilight_date_area != Rectangle.Empty) {
1332                                         dc.FillRectangle (ResPool.GetSolidBrush (ColorHighlight), dtp.hilight_date_area);
1333                                 }
1334                                 
1335                                 // draw the text part
1336                                 // TODO: if date format is CUstom then we need to draw the dates as separate parts
1337                                 StringFormat text_format = new StringFormat();
1338                                 text_format.LineAlignment = StringAlignment.Center;
1339                                 text_format.Alignment = StringAlignment.Near;                                   
1340                                 dc.DrawString (dtp.Text, dtp.Font, ResPool.GetSolidBrush (dtp.ForeColor), Rectangle.Inflate(dtp.date_area_rect, -1, -1), text_format);
1341                                 text_format.Dispose ();
1342                         }
1343                 }
1344                 
1345                 #endregion // DateTimePicker
1346
1347                 #region GroupBox
1348                 public override void DrawGroupBox (Graphics dc,  Rectangle area, GroupBox box) {
1349                         StringFormat    text_format;
1350                         SizeF           size;
1351                         int             width;
1352                         int             y;
1353                         Rectangle       rect;
1354
1355                         rect = box.ClientRectangle;
1356
1357                         // Needed once the Dark/Light code below is enabled again
1358                         //Color disabled = ColorGrayText;
1359                         
1360                         Pen pen_light = ResPool.GetPen (Color.FromArgb (255,255,255,255));
1361                         Pen pen_dark = ResPool.GetPen (Color.FromArgb (255, 128, 128,128));
1362                         
1363                         // TODO: When the Light and Dark methods work this code should be activate it
1364                         //Pen pen_light = new Pen (ControlPaint.Light (disabled, 1));
1365                         //Pen pen_dark = new Pen (ControlPaint.Dark (disabled, 0));
1366
1367                         dc.FillRectangle (ResPool.GetSolidBrush (box.BackColor), rect);
1368
1369                         text_format = new StringFormat();
1370                         text_format.HotkeyPrefix = HotkeyPrefix.Show;
1371
1372                         size = dc.MeasureString (box.Text, box.Font);
1373                         width = ((int) size.Width) + 7;
1374                         
1375                         if (width > box.Width - 16)
1376                                 width = box.Width - 16;
1377                         
1378                         y = box.Font.Height / 2;
1379                         
1380                         /* Draw group box*/
1381                         dc.DrawLine (pen_dark, 0, y, 8, y); // top 
1382                         dc.DrawLine (pen_light, 0, y + 1, 8, y + 1);                    
1383                         dc.DrawLine (pen_dark, 8 + width, y, box.Width, y);                     
1384                         dc.DrawLine (pen_light, 8 + width, y + 1, box.Width, y + 1);
1385                         
1386                         dc.DrawLine (pen_dark, 0, y + 1, 0, box.Height); // left
1387                         dc.DrawLine (pen_light, 1, y + 1, 1, box.Height);                       
1388                         
1389                         dc.DrawLine (pen_dark, 0, box.Height - 2, box.Width,  box.Height - 2); // bottom
1390                         dc.DrawLine (pen_light, 0, box.Height - 1, box.Width,  box.Height - 1);
1391                         
1392                         dc.DrawLine (pen_dark, box.Width - 2, y,  box.Width - 2, box.Height - 2); // right
1393                         dc.DrawLine (pen_light, box.Width - 1, y, box.Width - 1, box.Height - 2);
1394                         
1395                         
1396                         /* Text */
1397                         if (box.Enabled) {
1398                                 dc.DrawString (box.Text, box.Font, ResPool.GetSolidBrush (box.ForeColor), 10, 0, text_format);
1399                         } else {
1400                                 CPDrawStringDisabled (dc, box.Text, box.Font, box.ForeColor, 
1401                                         new RectangleF (10, 0, width,  box.Font.Height), text_format);
1402                         }
1403                         text_format.Dispose (); 
1404                 }
1405
1406                 public override Size GroupBoxDefaultSize {
1407                         get {
1408                                 return new Size (200,100);
1409                         }
1410                 }
1411                 #endregion
1412
1413                 #region HScrollBar
1414                 public override Size HScrollBarDefaultSize {
1415                         get {
1416                                 return new Size (80, this.ScrollBarButtonSize);
1417                         }
1418                 }
1419
1420                 #endregion      // HScrollBar
1421
1422                 #region Label
1423                 public  override void DrawLabel (Graphics dc, Rectangle clip_rectangle, Label label) 
1424                 {               
1425                         dc.FillRectangle (ResPool.GetSolidBrush (label.BackColor), clip_rectangle);
1426
1427                         if (label.Enabled) {
1428                                 dc.DrawString (label.Text, label.Font, ResPool.GetSolidBrush (label.ForeColor), clip_rectangle, label.string_format);
1429                         } else {
1430                                 ControlPaint.DrawStringDisabled (dc, label.Text, label.Font, label.ForeColor, clip_rectangle, label.string_format);
1431                         }
1432                 
1433                 }
1434
1435                 public override Size LabelDefaultSize {
1436                         get {
1437                                 return new Size (100, 23);
1438                         }
1439                 }
1440                 #endregion      // Label
1441
1442                 #region LinkLabel
1443                 public  override void DrawLinkLabel (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
1444                 {
1445                         Color color;
1446
1447                         dc.FillRectangle (ResPool.GetSolidBrush (label.BackColor), clip_rectangle);
1448
1449                         for (int i = 0; i < label.num_pieces; i++) {
1450                                 
1451                                 if (clip_rectangle.IntersectsWith (label.pieces[i].rect) == false) {
1452                                         continue;
1453                                 }                               
1454                                 
1455                                 color = label.GetLinkColor (label.pieces[i], i);
1456
1457                                 if (label.pieces[i].link == null)
1458                                         dc.DrawString (label.pieces[i].text, label.GetPieceFont (label.pieces[i]), ResPool.GetSolidBrush (Color.Black),
1459                                                 label.pieces[i].rect.X, label.pieces[i].rect.Y);
1460                                 else
1461                                         dc.DrawString (label.pieces[i].text, label.GetPieceFont (label.pieces[i]), ResPool.GetSolidBrush (color),
1462                                                 label.pieces[i].rect.X, label.pieces[i].rect.Y);
1463
1464                                 if (label.pieces[i].focused) {                                  
1465                                         CPDrawFocusRectangle (dc, label.pieces[i].rect, label.ForeColor, label.BackColor);
1466                                 }
1467                         }                       
1468                         
1469                 }
1470                 #endregion      // LinkLabel
1471                 #region ListBox
1472                 
1473                 // Drawing              
1474                 
1475                 private int DrawListBoxDecorationSize (BorderStyle border_style)
1476                 {
1477                         switch (border_style) {
1478                                 case BorderStyle.Fixed3D:
1479                                         return 2;
1480                                 case BorderStyle.FixedSingle:                                   
1481                                         return 1;
1482                                 case BorderStyle.None:
1483                                 default:
1484                                         break;
1485                                 }
1486                                 
1487                         return 0;
1488                 }                       
1489                 
1490                 // Sizing                               
1491                 public override void DrawListBoxItem (ListBox ctrl, DrawItemEventArgs e)
1492                 {
1493                         Color back_color, fore_color;
1494                         StringFormat string_format = ctrl.GetFormatString ();
1495                         
1496                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
1497                                 back_color = ColorHighlight;
1498                                 fore_color = ColorHighlightText;
1499                         }
1500                         else {
1501                                 back_color = e.BackColor;
1502                                 fore_color = e.ForeColor;
1503                         }
1504
1505                         e.Graphics.FillRectangle (ResPool.GetSolidBrush
1506                                 (back_color), e.Bounds);
1507
1508                         e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
1509                                 ResPool.GetSolidBrush (fore_color),
1510                                 e.Bounds.X, e.Bounds.Y, string_format);
1511                                         
1512                         if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1513                                 CPDrawFocusRectangle (e.Graphics, e.Bounds,
1514                                         fore_color, back_color);
1515                         }
1516                 }
1517                 
1518                 #endregion ListBox
1519
1520                 #region ListView
1521                 // Drawing
1522                 public override void DrawListViewItems (Graphics dc, Rectangle clip, ListView control)
1523                 {
1524                         bool details = control.View == View.Details;
1525
1526                         dc.FillRectangle (ResPool.GetSolidBrush (control.BackColor), clip);                                             
1527                         int first = control.FirstVisibleIndex;  
1528
1529                         for (int i = first; i <= control.LastVisibleIndex; i ++) {                                      
1530                                 if (clip.IntersectsWith (control.Items[i].GetBounds (ItemBoundsPortion.Entire)))
1531                                         DrawListViewItem (dc, control, control.Items [i]);
1532                         }       
1533                         
1534                         // draw the gridlines
1535                         if (details && control.GridLines) {
1536                                 int top = (control.HeaderStyle == ColumnHeaderStyle.None) ?
1537                                         2 : control.Font.Height + 2;
1538
1539                                 // draw vertical gridlines
1540                                 foreach (ColumnHeader col in control.Columns)
1541                                         dc.DrawLine (this.ResPool.GetPen (this.ColorControl),
1542                                                      col.Rect.Right, top,
1543                                                      col.Rect.Right, control.TotalHeight);
1544                                 // draw horizontal gridlines
1545                                 ListViewItem last_item = null;
1546                                 foreach (ListViewItem item in control.Items) {
1547                                         dc.DrawLine (this.ResPool.GetPen (this.ColorControl),
1548                                                      item.GetBounds (ItemBoundsPortion.Entire).Left, item.GetBounds (ItemBoundsPortion.Entire).Top,
1549                                                      control.TotalWidth, item.GetBounds (ItemBoundsPortion.Entire).Top);
1550                                         last_item = item;
1551                                 }
1552
1553                                 // draw a line after at the bottom of the last item
1554                                 if (last_item != null) {
1555                                         dc.DrawLine (this.ResPool.GetPen (this.ColorControl),
1556                                                      last_item.GetBounds (ItemBoundsPortion.Entire).Left,
1557                                                      last_item.GetBounds (ItemBoundsPortion.Entire).Bottom,
1558                                                      control.TotalWidth,
1559                                                      last_item.GetBounds (ItemBoundsPortion.Entire).Bottom);
1560                                 }
1561                         }                       
1562                         
1563                         dc.ResetClip ();
1564                         
1565                         // Draw corner between the two scrollbars
1566                         if (control.h_scroll.Visible == true && control.h_scroll.Visible == true) {
1567                                 Rectangle rect = new Rectangle ();
1568                                 rect.X = control.h_scroll.Location.X + control.h_scroll.Width;
1569                                 rect.Width = control.v_scroll.Width;
1570                                 rect.Y = control.v_scroll.Location.Y + control.v_scroll.Height;
1571                                 rect.Height = control.h_scroll.Height;
1572                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), rect);
1573                         }
1574
1575                 }
1576                 
1577                 public override void DrawListViewHeader (Graphics dc, Rectangle clip, ListView control)
1578                 {       
1579                         bool details = (control.View == View.Details);
1580                                 
1581                         // border is drawn directly in the Paint method
1582                         if (details && control.HeaderStyle != ColumnHeaderStyle.None) {                         
1583                                 dc.FillRectangle (ResPool.GetSolidBrush (control.BackColor),
1584                                                   0, 0, control.TotalWidth, control.Font.Height + 5);
1585                                 if (control.Columns.Count > 0) {
1586                                         foreach (ColumnHeader col in control.Columns) {
1587                                                 Rectangle rect = col.Rect;
1588                                                 rect.X -= control.h_marker;
1589                                                 ButtonState state;
1590                                                 if (control.HeaderStyle == ColumnHeaderStyle.Clickable)
1591                                                         state = col.Pressed ? ButtonState.Pushed : ButtonState.Normal;
1592                                                 else
1593                                                         state = ButtonState.Flat;
1594                                                 this.CPDrawButton (dc, rect, state);
1595                                                 rect.X += 3;
1596                                                 rect.Width -= 8;
1597                                                 if (rect.Width <= 0)
1598                                                         continue;
1599                                                 dc.DrawString (col.Text, DefaultFont,
1600                                                                ResPool.GetSolidBrush (ColorControlText),
1601                                                                rect, col.Format);
1602                                         }
1603                                 }
1604                         }
1605                 }
1606
1607                 public override void DrawListViewHeaderDragDetails (Graphics dc, ListView view, ColumnHeader col, int target_x)
1608                 {
1609                         Rectangle rect = col.Rect;
1610                         rect.X -= view.h_marker;
1611                         Color color = Color.FromArgb (0x7f, ColorControlDark.R, ColorControlDark.G, ColorControlDark.B);
1612                         dc.FillRectangle (ResPool.GetSolidBrush (color), rect);
1613                         rect.X += 3;
1614                         rect.Width -= 8;
1615                         if (rect.Width <= 0)
1616                                 return;
1617                         color = Color.FromArgb (0x7f, ColorControlText.R, ColorControlText.G, ColorControlText.B);
1618                         dc.DrawString (col.Text, DefaultFont, ResPool.GetSolidBrush (color), rect, col.Format);
1619                         Pen pen = new Pen (ColorHighlight, 2);
1620                         dc.DrawLine (pen, target_x, 0, target_x, col.Rect.Height);
1621                 }
1622
1623                 protected virtual void DrawListViewItem (Graphics dc, ListView control, ListViewItem item)
1624                 {                               
1625                         int col_offset;
1626                         if (control.View == View.Details && control.Columns.Count > 0)
1627                                 col_offset = control.Columns [0].Rect.X;
1628                         else
1629                                 col_offset = 0;
1630                         
1631                         Rectangle rect_checkrect = item.CheckRectReal;
1632                         rect_checkrect.X += col_offset;
1633                         Rectangle icon_rect = item.GetBounds (ItemBoundsPortion.Icon);
1634                         icon_rect.X += col_offset;
1635                         Rectangle full_rect = item.GetBounds (ItemBoundsPortion.Entire);
1636                         full_rect.X += col_offset;
1637                         Rectangle text_rect = item.GetBounds (ItemBoundsPortion.Label);                 
1638                         text_rect.X += col_offset;
1639
1640                         if (control.CheckBoxes) {
1641                                 if (control.StateImageList == null) {
1642                                         // Make sure we've got at least a line width of 1
1643                                         int check_wd = Math.Max (3, rect_checkrect.Width / 6);
1644                                         int scale = Math.Max (1, rect_checkrect.Width / 12);
1645
1646                                         // set the checkbox background
1647                                         dc.FillRectangle (this.ResPool.GetSolidBrush (this.ColorWindow),
1648                                                           rect_checkrect);
1649                                         // define a rectangle inside the border area
1650                                         Rectangle rect = new Rectangle (rect_checkrect.X + 2,
1651                                                                         rect_checkrect.Y + 2,
1652                                                                         rect_checkrect.Width - 4,
1653                                                                         rect_checkrect.Height - 4);
1654                                         Pen pen = new Pen (this.ColorWindowText, 2);
1655                                         dc.DrawRectangle (pen, rect);
1656
1657                                         // Need to draw a check-mark
1658                                         if (item.Checked) {
1659                                                 pen.Width = 1;
1660                                                 // adjustments to get the check-mark at the right place
1661                                                 rect.X ++; rect.Y ++;
1662                                                 // following logic is taken from DrawFrameControl method
1663                                                 for (int i = 0; i < check_wd; i++) {
1664                                                         dc.DrawLine (pen, rect.Left + check_wd / 2,
1665                                                                      rect.Top + check_wd + i,
1666                                                                      rect.Left + check_wd / 2 + 2 * scale,
1667                                                                      rect.Top + check_wd + 2 * scale + i);
1668                                                         dc.DrawLine (pen,
1669                                                                      rect.Left + check_wd / 2 + 2 * scale,
1670                                                                      rect.Top + check_wd + 2 * scale + i,
1671                                                                      rect.Left + check_wd / 2 + 6 * scale,
1672                                                                      rect.Top + check_wd - 2 * scale + i);
1673                                                 }
1674                                         }
1675                                 }
1676                                 else {
1677                                         if (item.Checked && control.StateImageList.Images.Count > 1)
1678                                                 control.StateImageList.Draw (dc,
1679                                                                              rect_checkrect.Location, 1);
1680                                         else if (! item.Checked && control.StateImageList.Images.Count > 0)
1681                                                 control.StateImageList.Draw (dc,
1682                                                                              rect_checkrect.Location, 0);
1683                                 }
1684                         }
1685
1686                         if (control.View == View.LargeIcon) {
1687                                 if (control.LargeImageList == null) {
1688                                         Pen pen = new Pen (ColorWindowText, 2);
1689                                         dc.DrawLine (pen, icon_rect.Left, icon_rect.Y, icon_rect.Left + 11, icon_rect.Y);
1690                                 } else if (item.ImageIndex > -1 && item.ImageIndex < control.LargeImageList.Images.Count)
1691                                         control.LargeImageList.Draw (dc, icon_rect.Location, item.ImageIndex);
1692                         } else {
1693                                 if (item.ImageIndex > -1 && control.SmallImageList != null &&
1694                                     item.ImageIndex < control.SmallImageList.Images.Count)
1695                                         control.SmallImageList.Draw (dc, icon_rect.Location, item.ImageIndex);
1696                         }
1697
1698                         // draw the item text                   
1699                         // format for the item text
1700                         StringFormat format = new StringFormat ();
1701                         if (control.View == View.SmallIcon)
1702                                 format.LineAlignment = StringAlignment.Near;
1703                         else
1704                                 format.LineAlignment = StringAlignment.Center;
1705                         if (control.View == View.LargeIcon)
1706                                 format.Alignment = StringAlignment.Center;
1707                         else
1708                                 format.Alignment = StringAlignment.Near;
1709                         
1710                         if (!control.LabelWrap)
1711                                 format.FormatFlags = StringFormatFlags.NoWrap;
1712                         
1713                         if (item.Selected) {
1714                                 if (control.View == View.Details) {
1715                                         if (control.FullRowSelect) {
1716                                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorHighlight), text_rect);
1717                                         }
1718                                         else {
1719                                                 Size text_size = Size.Ceiling (dc.MeasureString (item.Text,
1720                                                                                                 item.Font));
1721                                                 text_rect.Width = text_size.Width;
1722                                                 dc.FillRectangle (this.ResPool.GetSolidBrush
1723                                                                   (this.ColorHighlight), text_rect);
1724                                         }
1725                                 }
1726                                 else {
1727                                         /*Size text_size = Size.Ceiling (dc.MeasureString (item.Text,
1728                                           item.Font));
1729                                           Point loc = text_rect.Location;
1730                                           loc.X += (text_rect.Width - text_size.Width) / 2;
1731                                           text_rect.Width = text_size.Width;*/
1732                                         dc.FillRectangle (this.ResPool.GetSolidBrush (this.ColorHighlight),
1733                                                           text_rect);
1734                                 }
1735                         }
1736                         else
1737                                 dc.FillRectangle (ResPool.GetSolidBrush (item.BackColor), text_rect);
1738
1739                         if (item.Text != null && item.Text.Length > 0) {
1740                                 if (item.Selected)
1741                                         dc.DrawString (item.Text, item.Font, this.ResPool.GetSolidBrush
1742                                                        (this.ColorHighlightText), text_rect, format);
1743                                 else
1744                                         dc.DrawString (item.Text, item.Font, this.ResPool.GetSolidBrush
1745                                                        (item.ForeColor), text_rect, format);
1746                         }
1747
1748                         if (control.View == View.Details && control.Columns.Count > 0) {
1749                                 // draw subitems for details view
1750                                 ListViewItem.ListViewSubItemCollection subItems = item.SubItems;
1751                                 int count = (control.Columns.Count < subItems.Count ? 
1752                                              control.Columns.Count : subItems.Count);
1753
1754                                 if (count > 0) {
1755                                         ColumnHeader col;
1756                                         ListViewItem.ListViewSubItem subItem;
1757                                         Rectangle sub_item_rect = text_rect; 
1758
1759                                         // set the format for subitems
1760                                         format.FormatFlags = StringFormatFlags.NoWrap;
1761
1762                                         // 0th subitem is the item already drawn
1763                                         for (int index = 1; index < count; index++) {
1764                                                 subItem = subItems [index];
1765                                                 col = control.Columns [index];
1766                                                 format.Alignment = col.Format.Alignment;
1767                                                 sub_item_rect.X = col.Rect.X - control.h_marker;
1768                                                 sub_item_rect.Width = col.Wd;
1769                                                 Rectangle sub_item_text_rect = sub_item_rect;
1770                                                 sub_item_text_rect.X += 3;
1771                                                 sub_item_text_rect.Width -= 6;
1772
1773                                                 SolidBrush sub_item_back_br = null;
1774                                                 SolidBrush sub_item_fore_br = null;
1775                                                 Font sub_item_font = null;
1776
1777                                                 if (item.UseItemStyleForSubItems) {
1778                                                         sub_item_back_br = ResPool.GetSolidBrush (item.BackColor);
1779                                                         sub_item_fore_br = ResPool.GetSolidBrush (item.ForeColor);
1780                                                         sub_item_font = item.Font;
1781                                                 } else {
1782                                                         sub_item_back_br = ResPool.GetSolidBrush (subItem.BackColor);
1783                                                         sub_item_fore_br = ResPool.GetSolidBrush (subItem.ForeColor);
1784                                                         sub_item_font = subItem.Font;
1785                                                 }
1786
1787                                                 if (item.Selected && control.FullRowSelect) {
1788                                                         dc.FillRectangle (ResPool.GetSolidBrush (ColorHighlight), sub_item_rect);
1789                                                         if (subItem.Text != null && subItem.Text.Length > 0)
1790                                                                 dc.DrawString (subItem.Text, sub_item_font,
1791                                                                                this.ResPool.GetSolidBrush
1792                                                                                (this.ColorHighlightText),
1793                                                                                sub_item_text_rect, format);
1794                                                 } else {
1795                                                         dc.FillRectangle (sub_item_back_br, sub_item_rect);
1796                                                         if (subItem.Text != null && subItem.Text.Length > 0)
1797                                                                 dc.DrawString (subItem.Text, sub_item_font,
1798                                                                                sub_item_fore_br,
1799                                                                                sub_item_text_rect, format);
1800                                                 }
1801                                         }
1802                                 }
1803                         }
1804                         
1805                         if (item.Focused) {                             
1806                                 Rectangle focus_rect = text_rect;
1807                                 if (control.FullRowSelect && control.View == View.Details) {
1808                                         int width = 0;
1809                                         foreach (ColumnHeader col in control.Columns)
1810                                                 width += col.Width;
1811                                         focus_rect = new Rectangle (0, full_rect.Y, width, full_rect.Height);
1812                                 }
1813                                 if (item.Selected)
1814                                         CPDrawFocusRectangle (dc, focus_rect, ColorHighlightText, ColorHighlight);
1815                                 else
1816                                         CPDrawFocusRectangle (dc, focus_rect, control.ForeColor, control.BackColor);
1817                         }
1818
1819                         format.Dispose ();
1820                 }
1821
1822                 // Sizing
1823                 public override Size ListViewCheckBoxSize {
1824                         get { return new Size (16, 16); }
1825                 }
1826
1827                 public override int ListViewColumnHeaderHeight {
1828                         get { return 16; }
1829                 }
1830
1831                 public override int ListViewDefaultColumnWidth {
1832                         get { return 60; }
1833                 }
1834
1835                 public override int ListViewVerticalSpacing {
1836                         get { return 22; }
1837                 }
1838
1839                 public override int ListViewEmptyColumnWidth {
1840                         get { return 10; }
1841                 }
1842
1843                 public override int ListViewHorizontalSpacing {
1844                         get { return 10; }
1845                 }
1846
1847                 public override Size ListViewDefaultSize {
1848                         get { return new Size (121, 97); }
1849                 }
1850                 #endregion      // ListView
1851                 
1852                 #region Menus
1853                 public override void CalcItemSize (Graphics dc, MenuItem item, int y, int x, bool menuBar)
1854                 {
1855                         item.X = x;
1856                         item.Y = y;
1857
1858                         if (item.Visible == false) {
1859                                 item.Width = 0;
1860                                 item.Height = 0;
1861                                 return;
1862                         }
1863
1864                         if (item.Separator == true) {
1865                                 item.Height = SEPARATOR_HEIGHT / 2;
1866                                 item.Width = -1;
1867                                 return;
1868                         }
1869                         
1870                         if (item.MeasureEventDefined) {
1871                                 MeasureItemEventArgs mi = new MeasureItemEventArgs (dc, item.Index);
1872                                 item.PerformMeasureItem (mi);
1873                                 item.Height = mi.ItemHeight;
1874                                 item.Width = mi.ItemWidth;
1875                                 return;
1876                         } else {                
1877                                 SizeF size;
1878                                 size =  dc.MeasureString (item.Text, MenuFont);
1879                                 item.Width = (int) size.Width;
1880                                 item.Height = (int) size.Height;
1881         
1882                                 if (!menuBar) {
1883                                         if (item.Shortcut != Shortcut.None && item.ShowShortcut) {
1884                                                 item.XTab = MenuCheckSize.Width + MENU_TAB_SPACE + (int) size.Width;
1885                                                 size =  dc.MeasureString (" " + item.GetShortCutText (), MenuFont);
1886                                                 item.Width += MENU_TAB_SPACE + (int) size.Width;
1887                                         }
1888         
1889                                         item.Width += 4 + (MenuCheckSize.Width * 2);
1890                                 } else {
1891                                         item.Width += MENU_BAR_ITEMS_SPACE;
1892                                         x += item.Width;
1893                                 }
1894         
1895                                 if (item.Height < MenuHeight)
1896                                         item.Height = MenuHeight;
1897                         }
1898                 }
1899                 
1900                 // Updates the menu rect and returns the height
1901                 public override int CalcMenuBarSize (Graphics dc, Menu menu, int width)
1902                 {
1903                         int x = 0;
1904                         int y = 0;
1905                         menu.Height = 0;
1906
1907                         foreach (MenuItem item in menu.MenuItems) {
1908
1909                                 CalcItemSize (dc, item, y, x, true);
1910
1911                                 if (x + item.Width > width) {
1912                                         item.X = 0;
1913                                         y += item.Height;
1914                                         item.Y = y;
1915                                         x = 0;
1916                                 }
1917
1918                                 x += item.Width;
1919                                 item.MenuBar = true;                            
1920
1921                                 if (y + item.Height > menu.Height)
1922                                         menu.Height = item.Height + y;
1923                         }
1924
1925                         menu.Width = width;                                             
1926                         return menu.Height;
1927                 }
1928
1929                 public override void CalcPopupMenuSize (Graphics dc, Menu menu)
1930                 {
1931                         int x = 3;
1932                         int start = 0;
1933                         int i, n, y, max;
1934
1935                         menu.Height = 0;
1936
1937                         while (start < menu.MenuItems.Count) {
1938                                 y = 2;
1939                                 max = 0;
1940                                 for (i = start; i < menu.MenuItems.Count; i++) {
1941                                         MenuItem item = menu.MenuItems [i];
1942
1943                                         if ((i != start) && (item.Break || item.BarBreak))
1944                                                 break;
1945
1946                                         CalcItemSize (dc, item, y, x, false);
1947                                         y += item.Height;
1948
1949                                         if (item.Width > max)
1950                                                 max = item.Width;
1951                                 }
1952
1953                                 // Replace the -1 by the menu width (separators)
1954                                 for (n = start; n < i; n++, start++)
1955                                         menu.MenuItems [n].Width = max;
1956
1957                                 if (y > menu.Height)
1958                                         menu.Height = y;
1959
1960                                 x+= max;
1961                         }
1962
1963                         menu.Width = x;
1964
1965                         //space for border
1966                         menu.Width += 2;
1967                         menu.Height += 2;
1968
1969                         menu.Width += SM_CXBORDER;
1970                         menu.Height += SM_CYBORDER;
1971                 }
1972                 
1973                 // Draws a menu bar in a window
1974                 public override void DrawMenuBar (Graphics dc, Menu menu, Rectangle rect)
1975                 {
1976                         if (menu.Height == 0)
1977                                 CalcMenuBarSize (dc, menu, rect.Width);
1978                                 
1979                         bool keynav = (menu as MainMenu).tracker.Navigating;
1980                         HotkeyPrefix hp = always_draw_hotkeys || keynav ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
1981                         string_format_menu_menubar_text.HotkeyPrefix = hp;
1982                         string_format_menu_text.HotkeyPrefix = hp;
1983
1984                         rect.Height = menu.Height;
1985                         dc.FillRectangle (ResPool.GetSolidBrush(ColorMenu), rect);
1986                                                 
1987                         for (int i = 0; i < menu.MenuItems.Count; i++) {
1988                                 MenuItem item = menu.MenuItems [i];
1989                                 Rectangle item_rect = item.bounds;
1990                                 item_rect.X += rect.X;
1991                                 item_rect.Y += rect.Y;
1992                                 item.MenuHeight = menu.Height;
1993                                 item.PerformDrawItem (new DrawItemEventArgs (dc, MenuFont, item_rect, i, item.Status));                 
1994                         }                               
1995                 }               
1996                 
1997                 Bitmap CreateGlyphBitmap (Size size, MenuGlyph glyph, Color color)
1998                 {
1999                         Color bg_color;
2000                         if (color.R == 0 && color.G == 0 && color.B == 0)
2001                                 bg_color = Color.White;
2002                         else
2003                                 bg_color = Color.Black;
2004                         Bitmap  bmp = new Bitmap (size.Width, size.Height);
2005                         Graphics gr = Graphics.FromImage (bmp);
2006                         Rectangle rect = new Rectangle (Point.Empty, size);
2007                         gr.FillRectangle (ResPool.GetSolidBrush (bg_color), rect);
2008                         CPDrawMenuGlyph (gr, rect, glyph, color);
2009                         bmp.MakeTransparent (bg_color);
2010                         gr.Dispose ();
2011                         return bmp;
2012                 }
2013
2014                 public override void DrawMenuItem (MenuItem item, DrawItemEventArgs e)
2015                 {
2016                         StringFormat string_format;
2017                         Rectangle rect_text = e.Bounds;
2018
2019                         if (item.Visible == false)
2020                                 return;
2021
2022                         if (item.MenuBar)
2023                                 string_format = string_format_menu_menubar_text;
2024                         else
2025                                 string_format = string_format_menu_text;
2026
2027                         if (item.Separator == true) {
2028                                 e.Graphics.DrawLine (ResPool.GetPen (ColorControlDark),
2029                                         e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);
2030
2031                                 e.Graphics.DrawLine (ResPool.GetPen (ColorControlLight),
2032                                         e.Bounds.X, e.Bounds.Y + 1, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + 1);
2033
2034                                 return;
2035                         }
2036
2037                         if (!item.MenuBar)
2038                                 rect_text.X += MenuCheckSize.Width;
2039
2040                         if (item.BarBreak) { /* Draw vertical break bar*/
2041                                 Rectangle rect = e.Bounds;
2042                                 rect.Y++;
2043                                 rect.Width = 3;
2044                                 rect.Height = item.MenuHeight - 6;
2045
2046                                 e.Graphics.DrawLine (ResPool.GetPen (ColorControlDark),
2047                                         rect.X, rect.Y , rect.X, rect.Y + rect.Height);
2048
2049                                 e.Graphics.DrawLine (ResPool.GetPen (ColorControlLight),
2050                                         rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
2051                         }                       
2052                         
2053                         Color color_text;
2054                         Color color_back;
2055                         
2056                         if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
2057                                 color_text = ColorHighlightText;
2058                                 color_back = ColorHighlight;
2059                         } else {
2060                                 color_text = ColorMenuText;
2061                                 color_back = ColorMenu;
2062                         }
2063
2064                         /* Draw background */
2065                         Rectangle rect_back = e.Bounds;
2066                         rect_back.X++;
2067                         rect_back.Width -=2;
2068                         e.Graphics.FillRectangle (ResPool.GetSolidBrush (color_back), rect_back);
2069                         
2070                         if (item.Enabled) {
2071                                 e.Graphics.DrawString (item.Text, e.Font,
2072                                         ResPool.GetSolidBrush (color_text),
2073                                         rect_text, string_format);
2074
2075                                 if (!item.MenuBar && item.Shortcut != Shortcut.None && item.ShowShortcut) {
2076                                         string str = item.GetShortCutText ();
2077                                         Rectangle rect = rect_text;
2078                                         rect.X = item.XTab;
2079                                         rect.Width -= item.XTab;
2080
2081                                         e.Graphics.DrawString (str, e.Font, ResPool.GetSolidBrush (color_text),
2082                                                 rect, string_format_menu_shortcut);
2083                                 }
2084                         } else {
2085                                 ControlPaint.DrawStringDisabled (e.Graphics, item.Text, e.Font, 
2086                                         Color.Black, rect_text, string_format);
2087                         }
2088
2089                         /* Draw arrow */
2090                         if (item.MenuBar == false && item.IsPopup || item.MdiList) {
2091
2092                                 int cx = MenuCheckSize.Width;
2093                                 int cy = MenuCheckSize.Height;
2094                                 Bitmap  bmp = CreateGlyphBitmap (new Size (cx, cy), MenuGlyph.Arrow, color_text);
2095                                 
2096                                 if (item.Enabled) {
2097                                         e.Graphics.DrawImage (bmp, e.Bounds.X + e.Bounds.Width - cx,
2098                                                 e.Bounds.Y + ((e.Bounds.Height - cy) /2));
2099                                 } else {
2100                                         ControlPaint.DrawImageDisabled (e.Graphics, bmp, e.Bounds.X + e.Bounds.Width - cx,
2101                                                 e.Bounds.Y + ((e.Bounds.Height - cy) /2),  color_back);
2102                                 }
2103  
2104                                 bmp.Dispose ();
2105                         }
2106
2107                         /* Draw checked or radio */
2108                         if (item.MenuBar == false && item.Checked) {
2109
2110                                 Rectangle area = e.Bounds;
2111                                 int cx = MenuCheckSize.Width;
2112                                 int cy = MenuCheckSize.Height;
2113                                 Bitmap  bmp = CreateGlyphBitmap (new Size (cx, cy), item.RadioCheck ? MenuGlyph.Bullet : MenuGlyph.Checkmark, color_text);
2114
2115                                 e.Graphics.DrawImage (bmp, area.X, e.Bounds.Y + ((e.Bounds.Height - cy) / 2));
2116
2117                                 bmp.Dispose ();
2118                         }                       
2119                 }               
2120                         
2121                 public override void DrawPopupMenu (Graphics dc, Menu menu, Rectangle cliparea, Rectangle rect)
2122                 {
2123
2124                         dc.FillRectangle (ResPool.GetSolidBrush
2125                                 (ColorMenu), cliparea);
2126
2127                         /* Draw menu borders */
2128                         dc.DrawLine (ResPool.GetPen (ColorHighlightText),
2129                                 rect.X, rect.Y, rect.X + rect.Width, rect.Y);
2130
2131                         dc.DrawLine (ResPool.GetPen (ColorHighlightText),
2132                                 rect.X, rect.Y, rect.X, rect.Y + rect.Height);
2133
2134                         dc.DrawLine (ResPool.GetPen (ColorControlDark),
2135                                 rect.X + rect.Width - 1 , rect.Y , rect.X + rect.Width - 1, rect.Y + rect.Height);
2136
2137                         dc.DrawLine (ResPool.GetPen (ColorControlDarkDark),
2138                                 rect.X + rect.Width, rect.Y , rect.X + rect.Width, rect.Y + rect.Height);
2139
2140                         dc.DrawLine (ResPool.GetPen (ColorControlDark),
2141                                 rect.X , rect.Y + rect.Height - 1 , rect.X + rect.Width - 1, rect.Y + rect.Height -1);
2142
2143                         dc.DrawLine (ResPool.GetPen (ColorControlDarkDark),
2144                                 rect.X , rect.Y + rect.Height, rect.X + rect.Width - 1, rect.Y + rect.Height);
2145
2146                         for (int i = 0; i < menu.MenuItems.Count; i++)
2147                                 if (cliparea.IntersectsWith (menu.MenuItems [i].bounds)) {
2148                                         MenuItem item = menu.MenuItems [i];
2149                                         item.MenuHeight = menu.Height;
2150                                         item.PerformDrawItem (new DrawItemEventArgs (dc, MenuFont,
2151                                                 item.bounds, i, item.Status));
2152                         }
2153                 }
2154                 
2155                 #endregion // Menus
2156
2157                 #region MonthCalendar
2158
2159                 // draw the month calendar
2160                 public override void DrawMonthCalendar(Graphics dc, Rectangle clip_rectangle, MonthCalendar mc) 
2161                 {
2162                         Rectangle client_rectangle = mc.ClientRectangle;
2163                         Size month_size = mc.SingleMonthSize;
2164                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
2165                         Size calendar_spacing = (Size)((object)mc.calendar_spacing);
2166                         Size date_cell_size = (Size)((object)mc.date_cell_size);
2167                         
2168                         // draw the singlecalendars
2169                         int x_offset = 1;
2170                         int y_offset = 1;
2171                         // adjust for the position of the specific month
2172                         for (int i=0; i < mc.CalendarDimensions.Height; i++) 
2173                         {
2174                                 if (i > 0) 
2175                                 {
2176                                         y_offset += month_size.Height + calendar_spacing.Height;
2177                                 }
2178                                 // now adjust for x position    
2179                                 for (int j=0; j < mc.CalendarDimensions.Width; j++) 
2180                                 {
2181                                         if (j > 0) 
2182                                         {
2183                                                 x_offset += month_size.Width + calendar_spacing.Width;
2184                                         } 
2185                                         else 
2186                                         {
2187                                                 x_offset = 1;
2188                                         }
2189
2190                                         Rectangle month_rect = new Rectangle (x_offset, y_offset, month_size.Width, month_size.Height);
2191                                         if (month_rect.IntersectsWith (clip_rectangle)) {
2192                                                 DrawSingleMonth (
2193                                                         dc,
2194                                                         clip_rectangle,
2195                                                         month_rect,
2196                                                         mc,
2197                                                         i,
2198                                                         j);
2199                                         }
2200                                 }
2201                         }
2202                         
2203                         Rectangle bottom_rect = new Rectangle (
2204                                                 client_rectangle.X,
2205                                                 Math.Max(client_rectangle.Bottom - date_cell_size.Height - 3, 0),
2206                                                 client_rectangle.Width,
2207                                                 date_cell_size.Height + 2);
2208                         // draw the today date if it's set
2209                         if (mc.ShowToday && bottom_rect.IntersectsWith (clip_rectangle)) 
2210                         {
2211                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), bottom_rect);
2212                                 if (mc.ShowToday) {
2213                                         int today_offset = 5;
2214                                         if (mc.ShowTodayCircle) 
2215                                         {
2216                                                 Rectangle today_circle_rect = new Rectangle (
2217                                                         client_rectangle.X + 5,
2218                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height - 2, 0),
2219                                                         date_cell_size.Width,
2220                                                         date_cell_size.Height);
2221                                                         DrawTodayCircle (dc, today_circle_rect);
2222                                                 today_offset += date_cell_size.Width + 5;
2223                                         }
2224                                         // draw today's date
2225                                         StringFormat text_format = new StringFormat();
2226                                         text_format.LineAlignment = StringAlignment.Center;
2227                                         text_format.Alignment = StringAlignment.Near;
2228                                         Font bold_font = new Font (mc.Font.FontFamily, mc.Font.Size, mc.Font.Style | FontStyle.Bold);
2229                                         Rectangle today_rect = new Rectangle (
2230                                                         today_offset + client_rectangle.X,
2231                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height, 0),
2232                                                         Math.Max(client_rectangle.Width - today_offset, 0),
2233                                                         date_cell_size.Height);
2234                                         dc.DrawString ("Today: " + DateTime.Now.ToShortDateString(), bold_font, ResPool.GetSolidBrush (mc.ForeColor), today_rect, text_format);
2235                                         text_format.Dispose ();
2236                                         bold_font.Dispose ();
2237                                 }                               
2238                         }
2239                         
2240                         // finally paint the borders of the calendars as required
2241                         for (int i = 0; i <= mc.CalendarDimensions.Width; i++) {
2242                                 if (i == 0 && clip_rectangle.X == client_rectangle.X) {
2243                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Y, 1, client_rectangle.Height));
2244                                 } else if (i == mc.CalendarDimensions.Width && clip_rectangle.Right == client_rectangle.Right) {
2245                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.Right-1, client_rectangle.Y, 1, client_rectangle.Height));
2246                                 } else { 
2247                                         Rectangle rect = new Rectangle (
2248                                                 client_rectangle.X + (month_size.Width*i) + (calendar_spacing.Width * (i-1)) + 1,
2249                                                 client_rectangle.Y,
2250                                                 calendar_spacing.Width,
2251                                                 client_rectangle.Height);
2252                                         if (i < mc.CalendarDimensions.Width && i > 0 && clip_rectangle.IntersectsWith (rect)) {
2253                                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), rect);
2254                                         }
2255                                 }
2256                         }
2257                         for (int i = 0; i <= mc.CalendarDimensions.Height; i++) {
2258                                 if (i == 0 && clip_rectangle.Y == client_rectangle.Y) {
2259                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Y, client_rectangle.Width, 1));
2260                                 } else if (i == mc.CalendarDimensions.Height && clip_rectangle.Bottom == client_rectangle.Bottom) {
2261                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Bottom-1, client_rectangle.Width, 1));
2262                                 } else { 
2263                                         Rectangle rect = new Rectangle (
2264                                                 client_rectangle.X,
2265                                                 client_rectangle.Y + (month_size.Height*i) + (calendar_spacing.Height*(i-1)) + 1,
2266                                                 client_rectangle.Width,
2267                                                 calendar_spacing.Height);
2268                                         if (i < mc.CalendarDimensions.Height && i > 0 && clip_rectangle.IntersectsWith (rect)) {
2269                                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), rect);
2270                                         }
2271                                 }
2272                         }
2273                         
2274                         // draw the drop down border if need
2275                         if (mc.owner != null) {
2276                                 Rectangle bounds = mc.ClientRectangle;
2277                                 if (clip_rectangle.Contains (mc.Location)) {
2278                                         // find out if top or left line to draw
2279                                         if(clip_rectangle.Contains (new Point (bounds.Left, bounds.Bottom))) {
2280                                         
2281                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Y, bounds.X, bounds.Bottom-1);
2282                                         }
2283                                         if(clip_rectangle.Contains (new Point (bounds.Right, bounds.Y))) {
2284                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Y, bounds.Right-1, bounds.Y);
2285                                         }
2286                                 }
2287                                 if (clip_rectangle.Contains (new Point(bounds.Right, bounds.Bottom))) {
2288                                         // find out if bottom or right line to draw
2289                                         if(clip_rectangle.Contains (new Point (bounds.Left, bounds.Bottom))) {
2290                                                 dc.DrawLine (SystemPens.ControlText, bounds.X, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1);
2291                                         }
2292                                         if(clip_rectangle.Contains (new Point (bounds.Right, bounds.Y))) {
2293                                                 dc.DrawLine (SystemPens.ControlText, bounds.Right-1, bounds.Y, bounds.Right-1, bounds.Bottom-1);
2294                                         }
2295                                 }
2296                         }
2297                 }
2298
2299                 // darws a single part of the month calendar (with one month)
2300                 private void DrawSingleMonth(Graphics dc, Rectangle clip_rectangle, Rectangle rectangle, MonthCalendar mc, int row, int col) 
2301                 {
2302                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
2303                         Size title_size = (Size)((object)mc.title_size);
2304                         Size date_cell_size = (Size)((object)mc.date_cell_size);
2305                         DateTime current_month = (DateTime)((object)mc.current_month);
2306                         
2307                         // set up some standard string formating variables
2308                         StringFormat text_format = new StringFormat();
2309                         text_format.LineAlignment = StringAlignment.Center;
2310                         text_format.Alignment = StringAlignment.Center;
2311                         
2312
2313                         // draw the title back ground
2314                         DateTime this_month = current_month.AddMonths (row*mc.CalendarDimensions.Width+col);
2315                         Rectangle title_rect = new Rectangle(rectangle.X, rectangle.Y, title_size.Width, title_size.Height);
2316                         if (title_rect.IntersectsWith (clip_rectangle)) {
2317                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), title_rect);
2318                                 // draw the title                               
2319                                 string title_text = this_month.ToString ("MMMM yyyy");
2320                                 dc.DrawString (title_text, mc.Font, ResPool.GetSolidBrush (mc.TitleForeColor), title_rect, text_format);
2321
2322                                 // draw previous and next buttons if it's time
2323                                 if (row == 0 && col == 0) 
2324                                 {
2325                                         // draw previous button
2326                                         DrawMonthCalendarButton (
2327                                                 dc,
2328                                                 rectangle,
2329                                                 mc,
2330                                                 title_size,
2331                                                 mc.button_x_offset,
2332                                                 (System.Drawing.Size)((object)mc.button_size),
2333                                                 true);
2334                                 }
2335                                 if (row == 0 && col == mc.CalendarDimensions.Width-1) 
2336                                 {
2337                                         // draw next button
2338                                         DrawMonthCalendarButton (
2339                                                 dc,
2340                                                 rectangle,
2341                                                 mc,
2342                                                 title_size,
2343                                                 mc.button_x_offset,
2344                                                 (System.Drawing.Size)((object)mc.button_size),
2345                                                 false);
2346                                 }
2347                         }
2348                         
2349                         // set the week offset and draw week nums if needed
2350                         int col_offset = (mc.ShowWeekNumbers) ? 1 : 0;
2351                         Rectangle day_name_rect = new Rectangle(
2352                                 rectangle.X,
2353                                 rectangle.Y + title_size.Height,
2354                                 (7 + col_offset) * date_cell_size.Width,
2355                                 date_cell_size.Height);
2356                         if (day_name_rect.IntersectsWith (clip_rectangle)) {
2357                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), day_name_rect);
2358                                 // draw the day names 
2359                                 DayOfWeek first_day_of_week = mc.GetDayOfWeek(mc.FirstDayOfWeek);
2360                                 for (int i=0; i < 7; i++) 
2361                                 {
2362                                         int position = i - (int) first_day_of_week;
2363                                         if (position < 0) 
2364                                         {
2365                                                 position = 7 + position;
2366                                         }
2367                                         // draw it
2368                                         Rectangle day_rect = new Rectangle(
2369                                                 day_name_rect.X + ((i + col_offset)* date_cell_size.Width),
2370                                                 day_name_rect.Y,
2371                                                 date_cell_size.Width,
2372                                                 date_cell_size.Height);
2373                                         dc.DrawString (((DayOfWeek)i).ToString().Substring(0, 3), mc.Font, ResPool.GetSolidBrush (mc.TitleBackColor), day_rect, text_format);
2374                                 }
2375                                 
2376                                 // draw the vertical divider
2377                                 int vert_divider_y = Math.Max(title_size.Height+ date_cell_size.Height-1, 0);
2378                                 dc.DrawLine (
2379                                         ResPool.GetPen (mc.ForeColor),
2380                                         rectangle.X + (col_offset * date_cell_size.Width) + mc.divider_line_offset,
2381                                         rectangle.Y + vert_divider_y,
2382                                         rectangle.Right - mc.divider_line_offset,
2383                                         rectangle.Y + vert_divider_y);
2384                         }
2385
2386
2387                         // draw the actual date items in the grid (including the week numbers)
2388                         Rectangle date_rect = new Rectangle (
2389                                 rectangle.X,
2390                                 rectangle.Y + title_size.Height + date_cell_size.Height,
2391                                 date_cell_size.Width,
2392                                 date_cell_size.Height);
2393                         int month_row_count = 0;
2394                         bool draw_week_num_divider = false;
2395                         DateTime current_date = mc.GetFirstDateInMonthGrid ( new DateTime (this_month.Year, this_month.Month, 1));
2396                         for (int i=0; i < 6; i++) 
2397                         {
2398                                 // establish if this row is in our clip_area
2399                                 Rectangle row_rect = new Rectangle (
2400                                         rectangle.X,
2401                                         rectangle.Y + title_size.Height + (date_cell_size.Height * (i+1)),
2402                                         date_cell_size.Width * 7,
2403                                         date_cell_size.Height);
2404                                 if (mc.ShowWeekNumbers) {
2405                                         row_rect.Width += date_cell_size.Width;
2406                                 }
2407                 
2408                                 bool draw_row = row_rect.IntersectsWith (clip_rectangle);
2409                                 if (draw_row) {
2410                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), row_rect);
2411                                 }
2412                                 // establish if this is a valid week to draw
2413                                 if (mc.IsValidWeekToDraw (this_month, current_date, row, col)) {
2414                                         month_row_count = i;
2415                                 }
2416                                 
2417                                 // draw the week number if required
2418                                 if (mc.ShowWeekNumbers && month_row_count == i) {
2419                                         if (!draw_week_num_divider) {
2420                                                 draw_week_num_divider = draw_row;
2421                                         }
2422                                         // get the week for this row
2423                                         int week = mc.GetWeekOfYear (current_date);     
2424
2425                                         if (draw_row) {
2426                                                 dc.DrawString (
2427                                                         week.ToString(),
2428                                                         mc.Font,
2429                                                         ResPool.GetSolidBrush (mc.TitleBackColor),
2430                                                         date_rect,
2431                                                         text_format);
2432                                         }
2433                                         date_rect.Offset(date_cell_size.Width, 0);
2434                                 }
2435                                                                 
2436                                 // only draw the days if we have to
2437                                 if(month_row_count == i) {
2438                                         for (int j=0; j < 7; j++) 
2439                                         {
2440                                                 if (draw_row) {
2441                                                         DrawMonthCalendarDate (
2442                                                                 dc,
2443                                                                 date_rect,
2444                                                                 mc,
2445                                                                 current_date,
2446                                                                 this_month,
2447                                                                 row,
2448                                                                 col);
2449                                                 }
2450
2451                                                 // move the day on
2452                                                 current_date = current_date.AddDays(1);
2453                                                 date_rect.Offset(date_cell_size.Width, 0);
2454                                         }
2455
2456                                         // shift the rectangle down one row
2457                                         int offset = (mc.ShowWeekNumbers) ? -8 : -7;
2458                                         date_rect.Offset(offset*date_cell_size.Width, date_cell_size.Height);
2459                                 }
2460                         }
2461
2462                         // month_row_count is zero based, so add one
2463                         month_row_count++;
2464
2465                         // draw week numbers if required
2466                         if (draw_week_num_divider) {
2467                                 col_offset = 1;
2468                                 dc.DrawLine (
2469                                         ResPool.GetPen (mc.ForeColor),
2470                                         rectangle.X + date_cell_size.Width - 1,
2471                                         rectangle.Y + title_size.Height + date_cell_size.Height + mc.divider_line_offset,
2472                                         rectangle.X + date_cell_size.Width - 1,
2473                                         rectangle.Y + title_size.Height + date_cell_size.Height + (month_row_count * date_cell_size.Height) - mc.divider_line_offset);
2474                         }
2475                         text_format.Dispose ();
2476                 }
2477
2478                 // draws the pervious or next button
2479                 private void DrawMonthCalendarButton (Graphics dc, Rectangle rectangle, MonthCalendar mc, Size title_size, int x_offset, Size button_size, bool is_previous) 
2480                 {
2481                         bool is_clicked = false;
2482                         Rectangle button_rect;
2483                         Rectangle arrow_rect = new Rectangle (rectangle.X, rectangle.Y, 4, 7);
2484                         Point[] arrow_path = new Point[3];
2485                         // prepare the button
2486                         if (is_previous) 
2487                         {
2488                                 is_clicked = mc.is_previous_clicked;
2489                                 button_rect = new Rectangle (
2490                                         rectangle.X + 1 + x_offset,
2491                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
2492                                         Math.Max(button_size.Width - 1, 0),
2493                                         Math.Max(button_size.Height - 1, 0));
2494                                 arrow_rect.X = button_rect.X + ((button_rect.Width - arrow_rect.Width)/2);
2495                                 arrow_rect.Y = button_rect.Y + ((button_rect.Height - arrow_rect.Height)/2);
2496                                 if (is_clicked) {
2497                                         arrow_rect.Offset(1,1);
2498                                 }
2499                                 arrow_path[0] = new Point (arrow_rect.Right, arrow_rect.Y);
2500                                 arrow_path[1] = new Point (arrow_rect.X, arrow_rect.Y + arrow_rect.Height/2);
2501                                 arrow_path[2] = new Point (arrow_rect.Right, arrow_rect.Bottom);
2502                         }
2503                         else
2504                         {
2505                                 is_clicked = mc.is_next_clicked;
2506                                 button_rect = new Rectangle (
2507                                         rectangle.Right - 1 - x_offset - button_size.Width,
2508                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
2509                                         Math.Max(button_size.Width - 1, 0),
2510                                         Math.Max(button_size.Height - 1, 0));
2511                                 arrow_rect.X = button_rect.X + ((button_rect.Width - arrow_rect.Width)/2);
2512                                 arrow_rect.Y = button_rect.Y + ((button_rect.Height - arrow_rect.Height)/2);
2513                                 if (is_clicked) {
2514                                         arrow_rect.Offset(1,1);
2515                                 }
2516                                 arrow_path[0] = new Point (arrow_rect.X, arrow_rect.Y);
2517                                 arrow_path[1] = new Point (arrow_rect.Right, arrow_rect.Y + arrow_rect.Height/2);
2518                                 arrow_path[2] = new Point (arrow_rect.X, arrow_rect.Bottom);                            
2519                         }
2520
2521                         // fill the background
2522                         dc.FillRectangle (SystemBrushes.Control, button_rect);
2523                         // draw the border
2524                         if (is_clicked) {
2525                                 dc.DrawRectangle (SystemPens.ControlDark, button_rect);
2526                         }
2527                         else {
2528                                 CPDrawBorder3D (dc, button_rect, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
2529                         }
2530                         // draw the arrow
2531                         dc.FillPolygon (SystemBrushes.ControlText, arrow_path);                 
2532                 }
2533                 
2534
2535                 // draws one day in the calendar grid
2536                 private void DrawMonthCalendarDate (Graphics dc, Rectangle rectangle, MonthCalendar mc, DateTime date, DateTime month, int row, int col) {
2537                         Color date_color = mc.ForeColor;
2538                         Rectangle interior = new Rectangle (rectangle.X, rectangle.Y, Math.Max(rectangle.Width - 1, 0), Math.Max(rectangle.Height - 1, 0));
2539
2540                         // find out if we are the lead of the first calendar or the trail of the last calendar                                          
2541                         if (date.Year != month.Year || date.Month != month.Month) {
2542                                 DateTime check_date = month.AddMonths (-1);
2543                                 // check if it's the month before 
2544                                 if (check_date.Year == date.Year && check_date.Month == date.Month && row == 0 && col == 0) {
2545                                         date_color = mc.TrailingForeColor;
2546                                 } else {
2547                                         // check if it's the month after
2548                                         check_date = month.AddMonths (1);
2549                                         if (check_date.Year == date.Year && check_date.Month == date.Month && row == mc.CalendarDimensions.Height-1 && col == mc.CalendarDimensions.Width-1) {
2550                                                 date_color = mc.TrailingForeColor;
2551                                         } else {
2552                                                 return;
2553                                         }
2554                                 }
2555                         } else {
2556                                 date_color = mc.ForeColor;
2557                         }
2558
2559
2560                         if (date == mc.SelectionStart && date == mc.SelectionEnd) {
2561                                 // see if the date is in the start of selection
2562                                 date_color = mc.BackColor;
2563                                 // draw the left hand of the back ground
2564                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);                                
2565                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 0, 359);
2566                         } else if (date == mc.SelectionStart) {
2567                                 // see if the date is in the start of selection
2568                                 date_color = mc.BackColor;
2569                                 // draw the left hand of the back ground
2570                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);                                
2571                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 90, 180);
2572                                 // fill the other side as a straight rect
2573                                 if (date < mc.SelectionEnd) 
2574                                 {
2575                                         // use rectangle instead of rectangle to go all the way to edge of rect
2576                                         selection_rect.X = (int) Math.Floor((double)(rectangle.X + rectangle.Width / 2));
2577                                         selection_rect.Width = Math.Max(rectangle.Right - selection_rect.X, 0);
2578                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
2579                                 }
2580                         } else if (date == mc.SelectionEnd) {
2581                                 // see if it is the end of selection
2582                                 date_color = mc.BackColor;
2583                                 // draw the left hand of the back ground
2584                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);
2585                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 270, 180);
2586                                 // fill the other side as a straight rect
2587                                 if (date > mc.SelectionStart) {
2588                                         selection_rect.X = rectangle.X;
2589                                         selection_rect.Width = rectangle.Width - (rectangle.Width / 2);
2590                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
2591                                 }
2592                         } else if (date > mc.SelectionStart && date < mc.SelectionEnd) {
2593                                 // now see if it's in the middle
2594                                 date_color = mc.BackColor;
2595                                 // draw the left hand of the back ground
2596                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, 0, -3);
2597                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
2598                         }
2599
2600                         // set up some standard string formating variables
2601                         StringFormat text_format = new StringFormat();
2602                         text_format.LineAlignment = StringAlignment.Center;
2603                         text_format.Alignment = StringAlignment.Center;
2604                         
2605
2606                         // establish if it's a bolded font
2607                         Font font;
2608                         if (mc.IsBoldedDate (date)) {
2609                                 font = new Font (mc.Font.FontFamily, mc.Font.Size, mc.Font.Style | FontStyle.Bold);
2610                         } else {
2611                                 font = mc.Font;
2612                         }
2613
2614                         // just draw the date now
2615                         dc.DrawString (date.Day.ToString(), font, ResPool.GetSolidBrush (date_color), rectangle, text_format);
2616
2617                         // today circle if needed
2618                         if (mc.ShowTodayCircle && date == DateTime.Now.Date) {
2619                                 DrawTodayCircle (dc, interior);
2620                         }
2621
2622                         // draw the selection grid
2623                         if (mc.is_date_clicked && mc.clicked_date == date) {                            
2624                                 using (Pen pen = new Pen (Color.Black, 1) ) {
2625                                         pen.DashStyle = DashStyle.Dot;
2626                                         dc.DrawRectangle (pen, interior);
2627                                 }
2628                         }
2629                         text_format.Dispose ();
2630                 }
2631
2632                 private void DrawTodayCircle (Graphics dc, Rectangle rectangle) {
2633                         Color circle_color = Color.FromArgb (248, 0, 0);
2634                         // draw the left hand of the circle 
2635                         Rectangle lhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 4, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 5, 0));
2636                         Rectangle rhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 1, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 2, 0));
2637                         Point [] curve_points = new Point [3];
2638                         curve_points [0] = new Point (lhs_circle_rect.X, rhs_circle_rect.Y + rhs_circle_rect.Height/12);
2639                         curve_points [1] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/9, rhs_circle_rect.Y);
2640                         curve_points [2] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/2 + 1, rhs_circle_rect.Y);
2641
2642                         using (Pen pen = new Pen (circle_color, 2)) {
2643                                 dc.DrawArc (pen, lhs_circle_rect, 90, 180);
2644                                 dc.DrawArc (pen, rhs_circle_rect, 270, 180);                                    
2645                                 dc.DrawCurve (pen, curve_points);
2646                                 dc.DrawLine (ResPool.GetPen (circle_color), curve_points [2], new Point (curve_points [2].X, lhs_circle_rect.Y));
2647                         }
2648                 }
2649
2650                 #endregion      // MonthCalendar
2651
2652                 #region Panel
2653                 public override Size PanelDefaultSize {
2654                         get {
2655                                 return new Size (200, 100);
2656                         }
2657                 }
2658                 #endregion      // Panel
2659
2660                 #region PictureBox
2661                 public override void DrawPictureBox (Graphics dc, Rectangle clip, PictureBox pb) {
2662                         Rectangle client = pb.ClientRectangle;
2663
2664                         // FIXME - instead of drawing the whole picturebox every time
2665                         // intersect the clip rectangle with the drawn picture and only draw what's needed,
2666                         // Also, we only need a background fill where no image goes
2667                         if (pb.Image != null) {
2668                                 switch (pb.SizeMode) {
2669                                 case PictureBoxSizeMode.StretchImage:
2670                                         dc.DrawImage (pb.Image, 0, 0, client.Width, client.Height);
2671                                         break;
2672
2673                                 case PictureBoxSizeMode.CenterImage:
2674                                         dc.FillRectangle(ResPool.GetSolidBrush(pb.BackColor), clip);
2675                                         dc.DrawImage (pb.Image, (client.Width / 2) - (pb.Image.Width / 2), (client.Height / 2) - (pb.Image.Height / 2));
2676                                         break;
2677                                 default:
2678                                         dc.FillRectangle(ResPool.GetSolidBrush(pb.BackColor), clip);
2679                                         // Normal, AutoSize
2680                                         dc.DrawImage(pb.Image, 0, 0, pb.Image.Width, pb.Image.Height);
2681                                         break;
2682                                 }
2683
2684                                 return;
2685                         }
2686
2687                         // We only get here if no image is set. At least paint the background
2688                         dc.FillRectangle(ResPool.GetSolidBrush(pb.BackColor), clip);
2689                 }
2690
2691                 public override Size PictureBoxDefaultSize {
2692                         get {
2693                                 return new Size (100, 50);
2694                         }
2695                 }
2696                 #endregion      // PictureBox
2697
2698                 #region ProgressBar
2699                 public override void DrawProgressBar (Graphics dc, Rectangle clip_rect, ProgressBar ctrl) 
2700                 {
2701                         Rectangle       block_rect;
2702                         Rectangle       client_area = ctrl.client_area;
2703                         int             space_betweenblocks     = 2;                    
2704                         int             block_width;
2705                         int             increment;
2706                         int             barpos_pixels;
2707                         
2708                         block_width = (client_area.Height * 2 ) / 3;
2709                         barpos_pixels = ((ctrl.Value - ctrl.Minimum) * client_area.Width) / (ctrl.Maximum - ctrl.Minimum);
2710                         increment = block_width + space_betweenblocks;
2711
2712                         /* Draw border */
2713                         CPDrawBorder3D (dc, ctrl.ClientRectangle, Border3DStyle.SunkenOuter, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom & ~Border3DSide.Middle, ColorControl);
2714                         
2715                         /* Draw Blocks */
2716                         block_rect = new Rectangle (client_area.X, client_area.Y, block_width, client_area.Height);
2717                         while ((block_rect.X - client_area.X) < barpos_pixels) {
2718                                 
2719                                 if (clip_rect.IntersectsWith (block_rect) == true) {                            
2720                                         dc.FillRectangle (ResPool.GetSolidBrush (progressbarblock_color), block_rect);
2721                                 }                               
2722                                 
2723                                 block_rect.X  += increment;
2724                         }
2725                 }
2726                 
2727                 public override Size ProgressBarDefaultSize {
2728                         get {
2729                                 return new Size (100, 23);
2730                         }
2731                 }
2732
2733                 #endregion      // ProgressBar
2734
2735                 #region RadioButton
2736                 public override void DrawRadioButton (Graphics dc, Rectangle clip_rectangle, RadioButton radio_button) {
2737                         StringFormat    text_format;
2738                         Rectangle       client_rectangle;
2739                         Rectangle       text_rectangle;
2740                         Rectangle       radiobutton_rectangle;
2741                         int             radiobutton_size = 12;
2742                         int     radiobutton_space = 4;
2743
2744                         client_rectangle = radio_button.ClientRectangle;
2745                         text_rectangle = client_rectangle;
2746                         radiobutton_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, radiobutton_size, radiobutton_size);
2747
2748                         text_format = new StringFormat();
2749                         text_format.Alignment = StringAlignment.Near;
2750                         text_format.LineAlignment = StringAlignment.Center;
2751                         text_format.HotkeyPrefix = HotkeyPrefix.Show;
2752
2753                         /* Calculate the position of text and checkbox rectangle */
2754                         if (radio_button.appearance!=Appearance.Button) {
2755                                 switch(radio_button.radiobutton_alignment) {
2756                                 case ContentAlignment.BottomCenter: {
2757                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
2758                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
2759                                         text_rectangle.X=client_rectangle.X;
2760                                         text_rectangle.Width=client_rectangle.Width;
2761                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
2762                                         break;
2763                                 }
2764
2765                                 case ContentAlignment.BottomLeft: {
2766                                         radiobutton_rectangle.X=client_rectangle.Left;
2767                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
2768                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
2769                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;                                 
2770                                         break;
2771                                 }
2772
2773                                 case ContentAlignment.BottomRight: {
2774                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
2775                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
2776                                         text_rectangle.X=client_rectangle.X;
2777                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
2778                                         break;
2779                                 }
2780
2781                                 case ContentAlignment.MiddleCenter: {
2782                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
2783                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
2784                                         text_rectangle.X=client_rectangle.X;
2785                                         text_rectangle.Width=client_rectangle.Width;
2786                                         break;
2787                                 }
2788
2789                                 default:
2790                                 case ContentAlignment.MiddleLeft: {
2791                                         radiobutton_rectangle.X=client_rectangle.Left;
2792                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
2793                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
2794                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
2795                                         break;
2796                                 }
2797
2798                                 case ContentAlignment.MiddleRight: {
2799                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
2800                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
2801                                         text_rectangle.X=client_rectangle.X;
2802                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
2803                                         break;
2804                                 }
2805
2806                                 case ContentAlignment.TopCenter: {
2807                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
2808                                         radiobutton_rectangle.Y=client_rectangle.Top;
2809                                         text_rectangle.X=client_rectangle.X;
2810                                         text_rectangle.Y=radiobutton_size+radiobutton_space;
2811                                         text_rectangle.Width=client_rectangle.Width;
2812                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
2813                                         break;
2814                                 }
2815
2816                                 case ContentAlignment.TopLeft: {
2817                                         radiobutton_rectangle.X=client_rectangle.Left;
2818                                         radiobutton_rectangle.Y=client_rectangle.Top;
2819                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
2820                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
2821                                         break;
2822                                 }
2823
2824                                 case ContentAlignment.TopRight: {
2825                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
2826                                         radiobutton_rectangle.Y=client_rectangle.Top;
2827                                         text_rectangle.X=client_rectangle.X;
2828                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
2829                                         break;
2830                                 }
2831                                 }
2832                         } else {
2833                                 text_rectangle.X=client_rectangle.X;
2834                                 text_rectangle.Width=client_rectangle.Width;
2835                         }
2836                         
2837                         /* Set the horizontal alignment of our text */
2838                         switch(radio_button.text_alignment) {
2839                                 case ContentAlignment.BottomLeft:
2840                                 case ContentAlignment.MiddleLeft:
2841                                 case ContentAlignment.TopLeft: {
2842                                         text_format.Alignment=StringAlignment.Near;
2843                                         break;
2844                                 }
2845
2846                                 case ContentAlignment.BottomCenter:
2847                                 case ContentAlignment.MiddleCenter:
2848                                 case ContentAlignment.TopCenter: {
2849                                         text_format.Alignment=StringAlignment.Center;
2850                                         break;
2851                                 }
2852
2853                                 case ContentAlignment.BottomRight:
2854                                 case ContentAlignment.MiddleRight:
2855                                 case ContentAlignment.TopRight: {
2856                                         text_format.Alignment=StringAlignment.Far;
2857                                         break;
2858                                 }
2859                         }
2860
2861                         /* Set the vertical alignment of our text */
2862                         switch(radio_button.text_alignment) {
2863                                 case ContentAlignment.TopLeft: 
2864                                 case ContentAlignment.TopCenter: 
2865                                 case ContentAlignment.TopRight: {
2866                                         text_format.LineAlignment=StringAlignment.Near;
2867                                         break;
2868                                 }
2869
2870                                 case ContentAlignment.BottomLeft:
2871                                 case ContentAlignment.BottomCenter:
2872                                 case ContentAlignment.BottomRight: {
2873                                         text_format.LineAlignment=StringAlignment.Far;
2874                                         break;
2875                                 }
2876
2877                                 case ContentAlignment.MiddleLeft:
2878                                 case ContentAlignment.MiddleCenter:
2879                                 case ContentAlignment.MiddleRight: {
2880                                         text_format.LineAlignment=StringAlignment.Center;
2881                                         break;
2882                                 }
2883                         }
2884
2885                         // FIXME: can be removed if themes are updated ->
2886                         ButtonState state = ButtonState.Normal;
2887                         if (radio_button.FlatStyle == FlatStyle.Flat) {
2888                                 state |= ButtonState.Flat;
2889                         }
2890                         
2891                         if (radio_button.Checked) {
2892                                 state |= ButtonState.Checked;
2893                         }
2894                         // <- until here
2895
2896                         // Start drawing
2897                         RadioButton_DrawButton(radio_button, dc, state, radiobutton_rectangle);
2898                         
2899                         if ((radio_button.image != null) || (radio_button.image_list != null))
2900                                 ButtonBase_DrawImage(radio_button, dc);
2901                         
2902                         RadioButton_DrawText(radio_button, text_rectangle, dc, text_format);
2903
2904                         RadioButton_DrawFocus(radio_button, dc, text_rectangle);
2905                         
2906                         text_format.Dispose ();
2907                 }
2908
2909                 protected virtual void RadioButton_DrawButton(RadioButton radio_button, Graphics dc, ButtonState state, Rectangle radiobutton_rectangle)
2910                 {
2911                         dc.FillRectangle(ResPool.GetSolidBrush(radio_button.BackColor), radio_button.ClientRectangle);
2912                         
2913                         if (radio_button.appearance==Appearance.Button) {
2914                                 ButtonBase_DrawButton (radio_button, dc);
2915                         } else {
2916                                 // establish if we are rendering a flat style of some sort
2917                                 if (radio_button.FlatStyle == FlatStyle.Flat || radio_button.FlatStyle == FlatStyle.Popup) {
2918                                         DrawFlatStyleRadioButton (dc, radiobutton_rectangle, radio_button);
2919                                 } else {
2920                                         DrawNormalRadioButton (dc, radiobutton_rectangle, radio_button);
2921                                 }
2922                         }
2923                 }
2924                 
2925                 protected virtual void RadioButton_DrawText(RadioButton radio_button, Rectangle text_rectangle, Graphics dc, StringFormat text_format)
2926                 {
2927                         DrawCheckBox_and_RadioButtonText (radio_button, text_rectangle, dc, 
2928                                                           text_format, radio_button.Appearance, radio_button.Checked);
2929                 }
2930                 
2931                 protected virtual void RadioButton_DrawFocus(RadioButton radio_button, Graphics dc, Rectangle text_rectangle)
2932                 {
2933                         // do nothing here. maybe an other theme needs it
2934                 }
2935                 
2936                 protected virtual void DrawNormalRadioButton (Graphics graphics, Rectangle rectangle, RadioButton radio_button)
2937                 {
2938                         Pen                     penFatDark      = new Pen(ColorControlDark, 1);
2939                         Pen                     penFatLight     = new Pen(ColorControlLightLight, 1);
2940                         int                     lineWidth;
2941                         
2942                         graphics.FillPie (ResPool.GetSolidBrush (this.ColorWindow), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2943                         
2944                         graphics.DrawArc(penFatDark, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 135, 180);
2945                         graphics.DrawArc(penFatLight, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 315, 180);
2946                         
2947                         graphics.DrawArc(SystemPens.ControlDark, rectangle, 135, 180);
2948                         graphics.DrawArc(SystemPens.ControlLightLight, rectangle, 315, 180);
2949                         
2950                         if (radio_button.Checked) {
2951                                 lineWidth=Math.Max(1, Math.Min(rectangle.Width, rectangle.Height)/3);
2952                                 
2953                                 SolidBrush      buttonBrush;
2954                                 
2955                                 if (!radio_button.is_enabled) {
2956                                         buttonBrush=(SolidBrush)SystemBrushes.ControlDark;
2957                                 } else {
2958                                         buttonBrush=(SolidBrush)SystemBrushes.ControlText;
2959                                 }
2960                                 graphics.FillPie(buttonBrush, rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2, 0, 359);
2961                         }
2962                         
2963                         penFatDark.Dispose();
2964                         penFatLight.Dispose();
2965                 }
2966                 
2967                 // renders a radio button with the Flat and Popup FlatStyle
2968                 protected void DrawFlatStyleRadioButton (Graphics graphics, Rectangle rectangle, RadioButton radio_button)
2969                 {
2970                         int     lineWidth;
2971                         
2972                         if (radio_button.Enabled) {
2973                                 CPColor cpcolor = ResPool.GetCPColor (radio_button.BackColor);
2974                                 
2975                                 // draw the outer flatstyle arcs
2976                                 if (radio_button.FlatStyle == FlatStyle.Flat) {
2977                                         graphics.DrawArc (ResPool.GetPen (radio_button.ForeColor), rectangle, 0, 359);
2978                                         
2979                                         // fill in the area depending on whether or not the mouse is hovering
2980                                         if (radio_button.is_entered && radio_button.Capture) {
2981                                                 graphics.FillPie (ResPool.GetSolidBrush (cpcolor.Light), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2982                                         } else {
2983                                                 graphics.FillPie (ResPool.GetSolidBrush (cpcolor.LightLight), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2984                                         }
2985                                 } else {
2986                                         // must be a popup radio button
2987                                         // fill the control
2988                                         graphics.FillPie (ResPool.GetSolidBrush (cpcolor.LightLight), rectangle, 0, 359);
2989
2990                                         if (radio_button.is_entered || radio_button.Capture) {
2991                                                 // draw the popup 3d button knob
2992                                                 graphics.DrawArc (ResPool.GetPen (cpcolor.Light), rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 0, 359);
2993
2994                                                 graphics.DrawArc (ResPool.GetPen (cpcolor.Dark), rectangle, 135, 180);
2995                                                 graphics.DrawArc (ResPool.GetPen (cpcolor.LightLight), rectangle, 315, 180);
2996                                                 
2997                                         } else {
2998                                                 // just draw lighter flatstyle outer circle
2999                                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Dark (this.ColorControl)), rectangle, 0, 359);                                           
3000                                         }                                                                               
3001                                 }
3002                         } else {
3003                                 // disabled
3004                                 // fill control background color regardless of actual backcolor
3005                                 graphics.FillPie (ResPool.GetSolidBrush (this.ColorControl), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
3006                                 // draw the ark as control dark
3007                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Dark(this.ColorControl)), rectangle, 0, 359);
3008                         }
3009
3010                         // draw the check
3011                         if (radio_button.Checked) {
3012                                 lineWidth = Math.Max (1, Math.Min(rectangle.Width, rectangle.Height)/3);
3013                                 
3014                                 SolidBrush buttonBrush;
3015
3016                                 if (!radio_button.Enabled) {
3017                                         buttonBrush = ResPool.GetSolidBrush (ControlPaint.Dark (this.ColorControl));
3018                                 } else if (radio_button.FlatStyle == FlatStyle.Popup && radio_button.is_entered && radio_button.Capture) {
3019                                         buttonBrush = ResPool.GetSolidBrush (this.ColorControlText);
3020                                 } else {
3021                                         buttonBrush = ResPool.GetSolidBrush (radio_button.ForeColor);
3022                                 }
3023                                 
3024                                 graphics.FillPie (buttonBrush, rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2, 0, 359);
3025                         }
3026                 }
3027
3028                 public override Size RadioButtonDefaultSize {
3029                         get {
3030                                 return new Size (104,24);
3031                         }
3032                 }
3033                 #endregion      // RadioButton
3034
3035                 #region ScrollBar
3036                 public override void DrawScrollBar (Graphics dc, Rectangle clip, ScrollBar bar)
3037                 {
3038                         int             scrollbutton_width = bar.scrollbutton_width;
3039                         int             scrollbutton_height = bar.scrollbutton_height;
3040                         Rectangle       first_arrow_area;
3041                         Rectangle       second_arrow_area;                      
3042                         Rectangle       thumb_pos;
3043                         
3044                         thumb_pos = bar.ThumbPos;
3045
3046                         if (bar.vert) {
3047                                 first_arrow_area = new Rectangle(0, 0, bar.Width, scrollbutton_height);
3048                                 bar.FirstArrowArea = first_arrow_area;
3049
3050                                 second_arrow_area = new Rectangle(0, bar.ClientRectangle.Height - scrollbutton_height, bar.Width, scrollbutton_height);
3051                                 bar.SecondArrowArea = second_arrow_area;
3052
3053                                 thumb_pos.Width = bar.Width;
3054                                 bar.ThumbPos = thumb_pos;
3055
3056                                 /* Buttons */
3057                                 if (clip.IntersectsWith (first_arrow_area))
3058                                         CPDrawScrollButton (dc, first_arrow_area, ScrollButton.Up, bar.firstbutton_state);
3059                                 if (clip.IntersectsWith (second_arrow_area))
3060                                         CPDrawScrollButton (dc, second_arrow_area, ScrollButton.Down, bar.secondbutton_state);
3061
3062                                 /* Background */
3063                                 switch (bar.thumb_moving) {
3064                                 case ScrollBar.ThumbMoving.None: {
3065                                         ScrollBar_Vertical_Draw_ThumbMoving_None(scrollbutton_height, bar, clip, dc);
3066                                         break;
3067                                 }
3068                                 case ScrollBar.ThumbMoving.Forward: {
3069                                         ScrollBar_Vertical_Draw_ThumbMoving_Forward(scrollbutton_height, bar, thumb_pos, clip, dc);
3070                                         break;
3071                                 }
3072                                 
3073                                 case ScrollBar.ThumbMoving.Backwards: {
3074                                         ScrollBar_Vertical_Draw_ThumbMoving_Backwards(scrollbutton_height, bar, thumb_pos, clip, dc);
3075                                         break;
3076                                 }
3077                                 
3078                                 default:
3079                                         break;
3080                                 }
3081                         } else {
3082                                 first_arrow_area = new Rectangle(0, 0, scrollbutton_width, bar.Height);
3083                                 bar.FirstArrowArea = first_arrow_area;
3084
3085                                 second_arrow_area = new Rectangle (bar.ClientRectangle.Width - scrollbutton_width, 0, scrollbutton_width, bar.Height);
3086                                 bar.SecondArrowArea = second_arrow_area;
3087
3088                                 thumb_pos.Height = bar.Height;
3089                                 bar.ThumbPos = thumb_pos;
3090
3091                                 /* Buttons */
3092                                 if (clip.IntersectsWith (first_arrow_area))
3093                                         CPDrawScrollButton (dc, first_arrow_area, ScrollButton.Left, bar.firstbutton_state);
3094                                 if (clip.IntersectsWith (second_arrow_area))
3095                                         CPDrawScrollButton (dc, second_arrow_area, ScrollButton.Right, bar.secondbutton_state);
3096
3097                                 /* Background */                                        
3098                                 switch (bar.thumb_moving) {
3099                                 case ScrollBar.ThumbMoving.None: {
3100                                         ScrollBar_Horizontal_Draw_ThumbMoving_None(scrollbutton_width, bar, clip, dc);
3101                                         break;
3102                                 }
3103                                 
3104                                 case ScrollBar.ThumbMoving.Forward: {
3105                                         ScrollBar_Horizontal_Draw_ThumbMoving_Forward(scrollbutton_width, thumb_pos, bar, clip, dc);
3106                                         break;
3107                                 }
3108                                 
3109                                 case ScrollBar.ThumbMoving.Backwards: {
3110                                         ScrollBar_Horizontal_Draw_ThumbMoving_Backwards(scrollbutton_width, thumb_pos, bar, clip, dc);
3111                                         break;
3112                                 }
3113                                 }
3114                         }
3115
3116                         /* Thumb */
3117                         ScrollBar_DrawThumb(bar, thumb_pos, clip, dc);                          
3118                 }
3119
3120                 protected virtual void ScrollBar_DrawThumb(ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc)
3121                 {
3122                         if (bar.Enabled && thumb_pos.Width > 0 && thumb_pos.Height > 0 && clip.IntersectsWith(thumb_pos))
3123                                 DrawScrollButtonPrimitive(dc, thumb_pos, ButtonState.Normal);
3124                 }
3125
3126                 protected virtual void ScrollBar_Vertical_Draw_ThumbMoving_None( int scrollbutton_height, ScrollBar bar, Rectangle clip, Graphics dc )
3127                 {
3128                         Rectangle r = new Rectangle( 0,  
3129                                                     scrollbutton_height, bar.ClientRectangle.Width, bar.ClientRectangle.Height - ( scrollbutton_height * 2 ) );
3130                         Rectangle intersect = Rectangle.Intersect( clip, r );
3131                         
3132                         if ( intersect != Rectangle.Empty )
3133                         {
3134                                 Brush h = ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White);
3135                                 dc.FillRectangle( h, intersect );
3136                         }
3137                 }
3138                 
3139                 protected virtual void ScrollBar_Vertical_Draw_ThumbMoving_Forward( int scrollbutton_height, ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc )
3140                 {
3141                         Rectangle r = new Rectangle( 0,  scrollbutton_height,
3142                                                     bar.ClientRectangle.Width, thumb_pos.Y - scrollbutton_height );
3143                         Rectangle intersect = Rectangle.Intersect( clip, r );
3144                         
3145                         if ( intersect != Rectangle.Empty )
3146                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White ), intersect );
3147                         
3148                         r.X = 0;
3149                         r.Y = thumb_pos.Y + thumb_pos.Height;
3150                         r.Width = bar.ClientRectangle.Width;
3151                         r.Height = bar.ClientRectangle.Height -  ( thumb_pos.Y + thumb_pos.Height ) - scrollbutton_height;
3152                         
3153                         intersect = Rectangle.Intersect( clip, r );
3154                         if ( intersect != Rectangle.Empty )
3155                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, Color.FromArgb( 255, 63, 63, 63 ), Color.Black ), intersect );
3156                 }
3157                 
3158                 protected virtual void ScrollBar_Vertical_Draw_ThumbMoving_Backwards( int scrollbutton_height, ScrollBar bar, Rectangle thumb_pos, Rectangle clip, Graphics dc )
3159                 {
3160                         Rectangle r = new Rectangle( 0,  scrollbutton_height,
3161                                                     bar.ClientRectangle.Width, thumb_pos.Y - scrollbutton_height );
3162                         Rectangle intersect = Rectangle.Intersect( clip, r );
3163                         
3164                         if ( intersect != Rectangle.Empty )
3165                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, Color.FromArgb( 255, 63, 63, 63 ), Color.Black ), intersect );
3166                         
3167                         r.X = 0;
3168                         r.Y = thumb_pos.Y + thumb_pos.Height;
3169                         r.Width = bar.ClientRectangle.Width; 
3170                         r.Height = bar.ClientRectangle.Height -  ( thumb_pos.Y + thumb_pos.Height ) - scrollbutton_height;
3171                         
3172                         intersect = Rectangle.Intersect( clip, r );
3173                         if ( intersect != Rectangle.Empty )
3174                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White), intersect );
3175                 }
3176                 
3177                 protected virtual void ScrollBar_Horizontal_Draw_ThumbMoving_None( int scrollbutton_width, ScrollBar bar, Rectangle clip, Graphics dc )
3178                 {
3179                         Rectangle r = new Rectangle( scrollbutton_width,
3180                                                     0, bar.ClientRectangle.Width - ( scrollbutton_width * 2 ), bar.ClientRectangle.Height );
3181                         Rectangle intersect = Rectangle.Intersect( clip, r );
3182                         
3183                         if ( intersect != Rectangle.Empty )
3184                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White), intersect );
3185                 }
3186                 
3187                 protected virtual void ScrollBar_Horizontal_Draw_ThumbMoving_Forward( int scrollbutton_width, Rectangle thumb_pos, ScrollBar bar, Rectangle clip, Graphics dc )
3188                 {
3189                         Rectangle r = new Rectangle( scrollbutton_width,  0,
3190                                                     thumb_pos.X - scrollbutton_width, bar.ClientRectangle.Height );
3191                         Rectangle intersect = Rectangle.Intersect( clip, r );
3192                         
3193                         if ( intersect != Rectangle.Empty )
3194                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White), intersect );
3195                         
3196                         r.X = thumb_pos.X + thumb_pos.Width;
3197                         r.Y = 0;
3198                         r.Width = bar.ClientRectangle.Width -  ( thumb_pos.X + thumb_pos.Width ) - scrollbutton_width;
3199                         r.Height = bar.ClientRectangle.Height;
3200                         
3201                         intersect = Rectangle.Intersect( clip, r );
3202                         if ( intersect != Rectangle.Empty )
3203                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, Color.FromArgb( 255, 63, 63, 63 ), Color.Black ), intersect );
3204                 }
3205                 
3206                 protected virtual void ScrollBar_Horizontal_Draw_ThumbMoving_Backwards( int scrollbutton_width, Rectangle thumb_pos, ScrollBar bar, Rectangle clip, Graphics dc )
3207                 {
3208                         Rectangle r = new Rectangle( scrollbutton_width,  0,
3209                                                     thumb_pos.X - scrollbutton_width, bar.ClientRectangle.Height );
3210                         Rectangle intersect = Rectangle.Intersect( clip, r );
3211                         
3212                         if ( intersect != Rectangle.Empty )
3213                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, Color.FromArgb( 255, 63, 63, 63 ), Color.Black ), intersect );
3214                         
3215                         r.X = thumb_pos.X + thumb_pos.Width;
3216                         r.Y = 0;
3217                         r.Width = bar.ClientRectangle.Width -  ( thumb_pos.X + thumb_pos.Width ) - scrollbutton_width;
3218                         r.Height = bar.ClientRectangle.Height;
3219                         
3220                         intersect = Rectangle.Intersect( clip, r );
3221                         if ( intersect != Rectangle.Empty )
3222                                 dc.FillRectangle( ResPool.GetHatchBrush( HatchStyle.Percent50, ColorScrollBar, Color.White), intersect );
3223                 }
3224
3225                 public override int ScrollBarButtonSize {
3226                         get { return 16; }
3227                 }
3228                 #endregion      // ScrollBar
3229
3230                 #region StatusBar
3231                 public  override void DrawStatusBar (Graphics dc, Rectangle clip, StatusBar sb) {
3232                         Rectangle area = sb.ClientRectangle;
3233                         int horz_border = 2;
3234                         int vert_border = 2;
3235
3236                         dc.FillRectangle (GetControlBackBrush (sb.BackColor), clip);
3237                         
3238                         if (sb.Panels.Count == 0 && sb.Text != String.Empty) {
3239                                 string text = sb.Text;
3240                                 StringFormat string_format = new StringFormat ();
3241                                 string_format.Trimming = StringTrimming.Character;
3242                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
3243
3244                                 if (text [0] == '\t') {
3245                                         string_format.Alignment = StringAlignment.Center;
3246                                         text = text.Substring (1);
3247                                         if (text [0] == '\t') {
3248                                                 string_format.Alignment = StringAlignment.Far;
3249                                                 text = text.Substring (1);
3250                                         }
3251                                 }
3252                 
3253                                 dc.DrawString (text, sb.Font, ResPool.GetSolidBrush (sb.ForeColor),
3254                                                 new Rectangle(area.X + 2, area.Y + 2, area.Width - 4, area.Height - 4), string_format);
3255                                 string_format.Dispose ();
3256                         } else if (sb.ShowPanels) {
3257                                 SolidBrush br_forecolor = GetControlForeBrush (sb.ForeColor);
3258                                 int prev_x = area.X + horz_border;
3259                                 int y = area.Y + vert_border;
3260                                 for (int i = 0; i < sb.Panels.Count; i++) {
3261                                         Rectangle pr = new Rectangle (prev_x, y,
3262                                                 sb.Panels [i].Width, area.Height);
3263                                         prev_x += pr.Width + StatusBarHorzGapWidth;
3264                                         if (pr.IntersectsWith (clip))
3265                                                 DrawStatusBarPanel (dc, pr, i, br_forecolor, sb.Panels [i]);
3266                                 }
3267                         }
3268
3269                         if (sb.SizingGrip)
3270                                 CPDrawSizeGrip (dc, ColorControl, area);
3271
3272                 }
3273
3274
3275                 protected virtual void DrawStatusBarPanel (Graphics dc, Rectangle area, int index,
3276                         SolidBrush br_forecolor, StatusBarPanel panel) {
3277                         int border_size = 3; // this is actually const, even if the border style is none
3278
3279                         area.Height -= border_size;
3280                         
3281                         if (panel.BorderStyle != StatusBarPanelBorderStyle.None) {
3282                                 Border3DStyle border_style = Border3DStyle.SunkenOuter;
3283                                 if (panel.BorderStyle == StatusBarPanelBorderStyle.Raised)
3284                                         border_style = Border3DStyle.RaisedInner;
3285                                         
3286                                 CPDrawBorder3D(dc, area, border_style, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, panel.Parent.BackColor);
3287                         }
3288                         
3289                         if (panel.Style == StatusBarPanelStyle.OwnerDraw) {
3290                                 StatusBarDrawItemEventArgs e = new StatusBarDrawItemEventArgs (
3291                                         dc, panel.Parent.Font, area, index, DrawItemState.Default,
3292                                         panel, panel.Parent.ForeColor, panel.Parent.BackColor);
3293                                 panel.Parent.OnDrawItemInternal (e);
3294                                 return;
3295                         }
3296
3297                         int left = area.Left;
3298                         if (panel.Icon != null) {
3299                                 left += 2;
3300                                 dc.DrawIcon (panel.Icon, left, area.Top);
3301                                 left += panel.Icon.Width;
3302                         }
3303
3304                         if (panel.Text == String.Empty)
3305                                 return;
3306
3307                         string text = panel.Text;
3308                         StringFormat string_format = new StringFormat ();
3309                         string_format.Trimming = StringTrimming.Character;
3310                         string_format.FormatFlags = StringFormatFlags.NoWrap;
3311
3312                         if (text [0] == '\t') {
3313                                 string_format.Alignment = StringAlignment.Center;
3314                                 text = text.Substring (1);
3315                                 if (text [0] == '\t') {
3316                                         string_format.Alignment = StringAlignment.Far;
3317                                         text = text.Substring (1);
3318                                 }
3319                         }
3320
3321                         int x = left + border_size;
3322                         int y = border_size + 2;
3323                         Rectangle r = new Rectangle (x, y, 
3324                                 area.Right - x - border_size,
3325                                 area.Bottom - y - border_size);
3326                         
3327                         dc.DrawString (text, panel.Parent.Font, br_forecolor, r, string_format);
3328                         string_format.Dispose ();
3329                 }
3330
3331                 public override int StatusBarSizeGripWidth {
3332                         get { return 15; }
3333                 }
3334
3335                 public override int StatusBarHorzGapWidth {
3336                         get { return 3; }
3337                 }
3338
3339                 public override Size StatusBarDefaultSize {
3340                         get {
3341                                 return new Size (100, 22);
3342                         }
3343                 }
3344                 #endregion      // StatusBar
3345
3346                 public override void DrawTabControl (Graphics dc, Rectangle area, TabControl tab)
3347                 {
3348                         // Do we need to fill the back color? It can't be changed...
3349                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), area);
3350                         Rectangle panel_rect = GetTabPanelRectExt (tab);
3351
3352                         if (tab.Appearance == TabAppearance.Normal) {
3353                                 CPDrawBorder3D (dc, panel_rect, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top, ColorControl);
3354                                 CPDrawBorder3D (dc, panel_rect, Border3DStyle.Raised, Border3DSide.Right | Border3DSide.Bottom, ColorControl);
3355                         }
3356
3357                         if (tab.Alignment == TabAlignment.Top) {
3358                                 for (int r = tab.TabPages.Count; r > 0; r--) {
3359                                         for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
3360                                                 if (i == tab.SelectedIndex)
3361                                                         continue;
3362                                                 if (r != tab.TabPages [i].Row)
3363                                                         continue;
3364                                                 Rectangle rect = tab.GetTabRect (i);
3365                                                 if (!rect.IntersectsWith (area))
3366                                                         continue;
3367                                                 DrawTab (dc, tab.TabPages [i], tab, rect, false);
3368                                         }
3369                                 }
3370                         } else {
3371                                 for (int r = 0; r < tab.TabPages.Count; r++) {
3372                                         for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
3373                                                 if (i == tab.SelectedIndex)
3374                                                         continue;
3375                                                 if (r != tab.TabPages [i].Row)
3376                                                         continue;
3377                                                 Rectangle rect = tab.GetTabRect (i);
3378                                                 if (!rect.IntersectsWith (area))
3379                                                         continue;
3380                                                 DrawTab (dc, tab.TabPages [i], tab, rect, false);
3381                                         }
3382                                 }
3383                         }
3384
3385                         if (tab.SelectedIndex != -1 && tab.SelectedIndex >= tab.SliderPos) {
3386                                 Rectangle rect = tab.GetTabRect (tab.SelectedIndex);
3387                                 if (rect.IntersectsWith (area))
3388                                         DrawTab (dc, tab.TabPages [tab.SelectedIndex], tab, rect, true);
3389                         }
3390
3391                         if (tab.ShowSlider) {
3392                                 Rectangle right = GetTabControlRightScrollRect (tab);
3393                                 Rectangle left = GetTabControlLeftScrollRect (tab);
3394                                 CPDrawScrollButton (dc, right, ScrollButton.Right, tab.RightSliderState);
3395                                 CPDrawScrollButton (dc, left, ScrollButton.Left, tab.LeftSliderState);
3396                         }
3397                 }
3398
3399                 public override Rectangle GetTabControlLeftScrollRect (TabControl tab)
3400                 {
3401                         switch (tab.Alignment) {
3402                         case TabAlignment.Top:
3403                                 return new Rectangle (tab.ClientRectangle.Right - 34, tab.ClientRectangle.Top + 1, 17, 17);
3404                         default:
3405                                 Rectangle panel_rect = GetTabPanelRectExt (tab);
3406                                 return new Rectangle (tab.ClientRectangle.Right - 34, panel_rect.Bottom + 2, 17, 17);
3407                         }
3408                 }
3409
3410                 public override Rectangle GetTabControlRightScrollRect (TabControl tab)
3411                 {
3412                         switch (tab.Alignment) {
3413                         case TabAlignment.Top:
3414                                 return new Rectangle (tab.ClientRectangle.Right - 17, tab.ClientRectangle.Top + 1, 17, 17);
3415                         default:
3416                                 Rectangle panel_rect = GetTabPanelRectExt (tab);
3417                                 return new Rectangle (tab.ClientRectangle.Right - 17, panel_rect.Bottom + 2, 17, 17);
3418                         }
3419                 }
3420
3421                 public override Size TabControlDefaultItemSize {
3422                         get { return new Size (42, 21); }
3423                 }
3424
3425                 public override Point TabControlDefaultPadding {
3426                         get { return new Point (6, 3); }
3427                 }
3428
3429                 public override int TabControlMinimumTabWidth {
3430                         get { return 42; }
3431                 }
3432
3433                 public override Rectangle GetTabControlDisplayRectangle (TabControl tab)
3434                 {
3435                         Rectangle ext = GetTabPanelRectExt (tab);
3436                         // Account for border size
3437                         return new Rectangle (ext.Left + 2, ext.Top + 1, ext.Width - 6, ext.Height - 4);
3438                 }
3439
3440                 public override Size TabControlGetSpacing (TabControl tab) {
3441                         switch (tab.Appearance) {
3442                                 case TabAppearance.Normal:
3443                                         return new Size (1, -2);
3444                                 case TabAppearance.Buttons:
3445                                         return new Size (3, 3);
3446                                 case TabAppearance.FlatButtons:
3447                                         return new Size (9, 3);
3448                                 default:
3449                                         throw new Exception ("Invalid Appearance value: " + tab.Appearance);
3450                                 }
3451                 }
3452
3453                 protected virtual Rectangle GetTabPanelRectExt (TabControl tab)
3454                 {
3455                         // Offset the tab from the top corner
3456                         Rectangle res = new Rectangle (tab.ClientRectangle.X + 2,
3457                                         tab.ClientRectangle.Y,
3458                                         tab.ClientRectangle.Width - 2,
3459                                         tab.ClientRectangle.Height - 1);
3460
3461                         if (tab.TabCount == 0)
3462                                 return res;
3463
3464                         int spacing = TabControlGetSpacing (tab).Height;
3465                         int offset = (tab.ItemSize.Height + spacing) * tab.RowCount + 3;
3466
3467                         switch (tab.Alignment) {
3468                         case TabAlignment.Left:
3469                                 res.X += offset;
3470                                 res.Width -= offset;
3471                                 break;
3472                         case TabAlignment.Right:
3473                                 res.Width -= offset;
3474                                 break;
3475                         case TabAlignment.Top:
3476                                 res.Y += offset;
3477                                 res.Height -= offset;
3478                                 break;
3479                         case TabAlignment.Bottom:
3480                                 res.Height -= offset;
3481                                 break;
3482                         }
3483
3484                         return res;
3485                 }
3486
3487                 protected virtual int DrawTab (Graphics dc, TabPage page, TabControl tab, Rectangle bounds, bool is_selected)
3488                 {
3489                         int FlatButtonSpacing = 8;
3490                         Rectangle interior;
3491                         int res = bounds.Width;
3492
3493                         
3494                         
3495                         // we can't fill the background right away because the bounds might be adjusted if the tab is selected
3496
3497                         StringFormat string_format = new StringFormat ();
3498                         if (tab.Appearance == TabAppearance.Buttons || tab.Appearance == TabAppearance.FlatButtons) {
3499                                 dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
3500
3501                                 // Separators
3502                                 if (tab.Appearance == TabAppearance.FlatButtons) {
3503                                         int width = bounds.Width;
3504                                         bounds.Width += (FlatButtonSpacing - 2);
3505                                         res = bounds.Width;
3506                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Etched, Border3DSide.Right);
3507                                         bounds.Width = width;
3508                                 }
3509
3510                                 if (is_selected) {
3511                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
3512                                 } else if (tab.Appearance != TabAppearance.FlatButtons) {
3513                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
3514                                 }
3515
3516                                 interior = new Rectangle (bounds.Left + 2, bounds.Top + 2, bounds.Width - 4, bounds.Height - 4);
3517
3518                                 
3519                                 string_format.Alignment = StringAlignment.Center;
3520                                 string_format.LineAlignment = StringAlignment.Center;
3521                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
3522                         } else {
3523                                 Pen light = ResPool.GetPen (ControlPaint.LightLight (tab.BackColor));
3524
3525                                 switch (tab.Alignment) {
3526                                         
3527                                 case TabAlignment.Top:
3528
3529
3530                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
3531
3532                                         dc.DrawLine (light, bounds.Left, bounds.Bottom, bounds.Left, bounds.Top + 3);
3533                                         dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
3534                                         dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right - 3, bounds.Top);
3535
3536                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom);
3537                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 1, bounds.Top + 2, bounds.Right, bounds.Top + 3);
3538                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom);
3539
3540                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
3541
3542                                         string_format.Alignment = StringAlignment.Center;
3543                                         string_format.LineAlignment = StringAlignment.Center;
3544                                         string_format.FormatFlags = StringFormatFlags.NoWrap;
3545
3546                                         break;
3547
3548                                 case TabAlignment.Bottom:
3549
3550                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
3551
3552                                         dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 3);
3553                                         dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left + 2, bounds.Bottom - 1);
3554
3555                                         dc.DrawLine (SystemPens.ControlDark, bounds.Left + 3, bounds.Bottom - 1, bounds.Right - 3, bounds.Bottom - 1);
3556                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Bottom - 3, bounds.Right - 1, bounds.Top);
3557
3558                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 3, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
3559                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 3, bounds.Bottom, bounds.Right, bounds.Bottom - 3);
3560                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom - 3, bounds.Right, bounds.Top);
3561
3562                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
3563
3564                                         string_format.Alignment = StringAlignment.Center;
3565                                         string_format.LineAlignment = StringAlignment.Center;
3566                                         string_format.FormatFlags = StringFormatFlags.NoWrap;
3567
3568                                         break;
3569
3570                                 case TabAlignment.Left:
3571
3572                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
3573
3574                                         dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left, bounds.Top + 3);
3575                                         dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
3576                                         dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right, bounds.Top);
3577
3578                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right, bounds.Bottom - 1, bounds.Left + 2, bounds.Bottom - 1);
3579
3580                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom, bounds.Left + 2, bounds.Bottom);
3581                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 2, bounds.Bottom, bounds.Left, bounds.Bottom - 3);
3582
3583                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
3584
3585                                         string_format.Alignment = StringAlignment.Center;
3586                                         string_format.LineAlignment = StringAlignment.Center;
3587                                         string_format.FormatFlags = StringFormatFlags.NoWrap;
3588                                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
3589
3590                                         break;
3591
3592                                 default:
3593                                         // TabAlignment.Right
3594
3595                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
3596
3597                                         dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Right - 3, bounds.Top);
3598                                         dc.DrawLine (light, bounds.Right - 3, bounds.Top, bounds.Right, bounds.Top + 3);
3599
3600                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);
3601                                         dc.DrawLine (SystemPens.ControlDark, bounds.Left, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
3602
3603                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
3604                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
3605
3606                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
3607
3608                                         string_format.Alignment = StringAlignment.Center;
3609                                         string_format.LineAlignment = StringAlignment.Center;
3610                                         string_format.FormatFlags = StringFormatFlags.NoWrap;
3611                                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
3612
3613                                         break;
3614                                 }
3615                         }
3616
3617                         if (tab.DrawMode == TabDrawMode.Normal && page.Text != null) {
3618                                 if (tab.Alignment == TabAlignment.Left) {
3619                                         int wo = interior.Width / 2;
3620                                         int ho = interior.Height / 2;
3621                                         dc.TranslateTransform (interior.X + wo, interior.Y + ho);
3622                                         dc.RotateTransform (180);
3623                                         dc.DrawString (page.Text, page.Font, ResPool.GetSolidBrush (SystemColors.ControlText), 0, 0, string_format);
3624                                         dc.ResetTransform ();
3625                                 } else {
3626                                         dc.DrawString (page.Text, page.Font,
3627                                                         ResPool.GetSolidBrush (SystemColors.ControlText),
3628                                                         interior, string_format);
3629                                 }
3630                         } else if (page.Text != null) {
3631                                 DrawItemState state = DrawItemState.None;
3632                                 if (page == tab.SelectedTab)
3633                                         state |= DrawItemState.Selected;
3634                                 DrawItemEventArgs e = new DrawItemEventArgs (dc,
3635                                                 tab.Font, bounds, tab.IndexForTabPage (page),
3636                                                 state, page.ForeColor, page.BackColor);
3637                                 tab.OnDrawItemInternal (e);
3638                                 return res;
3639                         }
3640
3641                         if (page.parent.Focused && is_selected) {
3642                                 CPDrawFocusRectangle (dc, interior, tab.ForeColor, tab.BackColor);
3643                         }
3644
3645                         return res;
3646                 }
3647
3648                 #region ToolBar
3649                 public  override void DrawToolBar (Graphics dc, Rectangle clip_rectangle, ToolBar control) 
3650                 {
3651                         StringFormat format = new StringFormat ();
3652                         format.Trimming = StringTrimming.EllipsisWord;
3653                         format.LineAlignment = StringAlignment.Center;
3654                         if (control.TextAlign == ToolBarTextAlign.Underneath)
3655                                 format.Alignment = StringAlignment.Center;
3656                         else
3657                                 format.Alignment = StringAlignment.Near;
3658                         
3659                         dc.FillRectangle (ResPool.GetSolidBrush( DefaultControlBackColor ), clip_rectangle);
3660
3661                         foreach (ToolBarButton button in control.Buttons)
3662                                 if (button.Visible && clip_rectangle.IntersectsWith (button.Rectangle))
3663                                         DrawToolBarButton (dc, control, button, format);
3664
3665                         format.Dispose ();
3666                 }
3667
3668                 void DrawToolBarButton (Graphics dc, ToolBar control, ToolBarButton button, StringFormat format)
3669                 {
3670                         bool is_flat = control.Appearance == ToolBarAppearance.Flat;
3671
3672                         DrawToolBarButtonBorder (dc, button, is_flat);
3673
3674                         switch (button.Style) {
3675                         case ToolBarButtonStyle.DropDownButton:
3676                                 if (control.DropDownArrows)
3677                                         DrawToolBarDropDownArrow (dc, button, is_flat);
3678                                 DrawToolBarButtonContents (dc, control, button, format);
3679                                 break;
3680
3681                         case ToolBarButtonStyle.Separator:
3682                                 if (is_flat)
3683                                         DrawToolBarSeparator (dc, button);
3684                                 break;
3685
3686                         case ToolBarButtonStyle.ToggleButton:
3687                                 DrawToolBarToggleButtonBackground (dc, button);
3688                                 DrawToolBarButtonContents (dc, control, button, format);
3689                                 break;
3690
3691                         default:
3692                                 DrawToolBarButtonContents (dc, control, button, format);
3693                                 break;
3694                         }
3695                 }
3696
3697                 const Border3DSide all_sides = Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom;
3698
3699                 void DrawToolBarButtonBorder (Graphics dc, ToolBarButton button, bool is_flat)
3700                 {
3701                         if (button.Style == ToolBarButtonStyle.Separator)
3702                                 return;
3703
3704                         Border3DStyle style;
3705
3706                         if (is_flat) {
3707                                 if (button.Pushed || button.Pressed)
3708                                         style = Border3DStyle.SunkenOuter;
3709                                 else if (button.Hilight)
3710                                         style = Border3DStyle.RaisedInner;
3711                                 else
3712                                         return;
3713
3714                         } else {
3715                                 if (button.Pushed || button.Pressed)
3716                                         style = Border3DStyle.Sunken;
3717                                 else 
3718                                         style = Border3DStyle.Raised;
3719                         }
3720
3721                         CPDrawBorder3D (dc, button.Rectangle, style, all_sides);
3722                 }
3723
3724                 void DrawToolBarSeparator (Graphics dc, ToolBarButton button)
3725                 {
3726                         Rectangle area = button.Rectangle;
3727                         int offset = (int) ResPool.GetPen (ColorControl).Width + 1;
3728                         dc.DrawLine (ResPool.GetPen (ColorControlDark), area.X + 1, area.Y, area.X + 1, area.Bottom);
3729                         dc.DrawLine (ResPool.GetPen (ColorControlLight), area.X + offset, area.Y, area.X + offset, area.Bottom);
3730                 }
3731
3732                 void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarButton button)
3733                 {
3734                         Rectangle area = button.Rectangle;
3735                         area.X += ToolBarImageGripWidth;
3736                         area.Y += ToolBarImageGripWidth;
3737                         area.Width -= 2 * ToolBarImageGripWidth;
3738                         area.Height -= 2 * ToolBarImageGripWidth;
3739
3740                         if (button.Pushed)
3741                                 dc.FillRectangle (SystemBrushes.ControlLightLight, area);
3742                         else if (button.PartialPush)
3743                                 dc.FillRectangle (SystemBrushes.ControlLight, area);
3744                         else
3745                                 dc.FillRectangle (SystemBrushes.Control, area);
3746                 }
3747
3748                 void DrawToolBarDropDownArrow (Graphics dc, ToolBarButton button, bool is_flat)
3749                 {
3750                         Rectangle rect = button.Rectangle;
3751                         rect.X = button.Rectangle.Right - ToolBarDropDownWidth;
3752                         rect.Width = ToolBarDropDownWidth;
3753
3754                         if (button.dd_pressed) {
3755                                 CPDrawBorder3D (dc, rect, Border3DStyle.SunkenOuter, all_sides);
3756                                 CPDrawBorder3D (dc, rect, Border3DStyle.SunkenInner, Border3DSide.Bottom | Border3DSide.Right);
3757                         } else if (button.Pushed || button.Pressed)
3758                                 CPDrawBorder3D (dc, rect, Border3DStyle.Sunken, all_sides);
3759                         else if (is_flat) {
3760                                 if (button.Hilight)
3761                                         CPDrawBorder3D (dc, rect, Border3DStyle.RaisedOuter, all_sides);
3762                         } else
3763                                 CPDrawBorder3D (dc, rect, Border3DStyle.Raised, all_sides);
3764
3765                         PointF [] vertices = new PointF [3];
3766                         PointF ddCenter = new PointF (rect.X + (rect.Width/2.0f), rect.Y + (rect.Height/2.0f));
3767                         vertices [0].X = ddCenter.X - ToolBarDropDownArrowWidth / 2.0f + 0.5f;
3768                         vertices [0].Y = ddCenter.Y;
3769                         vertices [1].X = ddCenter.X + ToolBarDropDownArrowWidth / 2.0f + 0.5f;
3770                         vertices [1].Y = ddCenter.Y;
3771                         vertices [2].X = ddCenter.X + 0.5f; // 0.5 is added for adjustment
3772                         vertices [2].Y = ddCenter.Y + ToolBarDropDownArrowHeight;
3773                         dc.FillPolygon (SystemBrushes.ControlText, vertices);
3774                 }
3775
3776                 void DrawToolBarButtonContents (Graphics dc, ToolBar control, ToolBarButton button, StringFormat format)
3777                 {
3778                         if (button.Image != null) {
3779                                 int x = button.ImageRectangle.X + ToolBarImageGripWidth;
3780                                 int y = button.ImageRectangle.Y + ToolBarImageGripWidth;
3781                                 if (button.Enabled)
3782                                         dc.DrawImage (button.Image, x, y);
3783                                 else 
3784                                         CPDrawImageDisabled (dc, button.Image, x, y, ColorControl);
3785                         }
3786
3787                         if (button.Enabled)
3788                                 dc.DrawString (button.Text, control.Font, ResPool.GetSolidBrush (ColorControlText), button.TextRectangle, format);
3789                         else
3790                                 CPDrawStringDisabled (dc, button.Text, control.Font, ColorControlLight, button.TextRectangle, format);
3791                 }
3792
3793                 // Grip width for the ToolBar
3794                 public override int ToolBarGripWidth {
3795                         get { return 2;}
3796                 }
3797
3798                 // Grip width for the Image on the ToolBarButton
3799                 public override int ToolBarImageGripWidth {
3800                         get { return 2;}
3801                 }
3802
3803                 // width of the separator
3804                 public override int ToolBarSeparatorWidth {
3805                         get { return 4; }
3806                 }
3807
3808                 // width of the dropdown arrow rect
3809                 public override int ToolBarDropDownWidth {
3810                         get { return 13; }
3811                 }
3812
3813                 // width for the dropdown arrow on the ToolBarButton
3814                 public override int ToolBarDropDownArrowWidth {
3815                         get { return 5;}
3816                 }
3817
3818                 // height for the dropdown arrow on the ToolBarButton
3819                 public override int ToolBarDropDownArrowHeight {
3820                         get { return 3;}
3821                 }
3822
3823                 public override Size ToolBarDefaultSize {
3824                         get {
3825                                 return new Size (100, 42);
3826                         }
3827                 }
3828                 #endregion      // ToolBar
3829
3830                 #region ToolTip
3831                 public override void DrawToolTip(Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control) {
3832                         dc.FillRectangle(ResPool.GetSolidBrush(this.ColorInfo), control.client_rect);
3833                         dc.DrawRectangle(ResPool.GetPen(this.ColorWindowFrame), 0, 0, control.Width-1, control.Height-1);
3834                         dc.DrawString(control.text, control.Font, ResPool.GetSolidBrush(this.ColorInfoText), control.client_rect, control.string_format);
3835                 }
3836
3837                 public override Size ToolTipSize(ToolTip.ToolTipWindow tt, string text) {
3838                         SizeF   sizef;
3839
3840                         sizef = tt.DeviceContext.MeasureString(text, tt.Font);
3841                         return new Size((int)sizef.Width+2, (int)sizef.Height+3);               // Need space for the border
3842                 }
3843                 #endregion      // ToolTip
3844
3845                 #region TrackBar
3846                 private void DrawTrackBar_Vertical (Graphics dc, Rectangle clip_rectangle, TrackBar tb,
3847                         ref Rectangle thumb_pos, ref Rectangle thumb_area,  Brush br_thumb,
3848                         float ticks, int value_pos, bool mouse_value) {                 
3849
3850                         Point toptick_startpoint = new Point ();
3851                         Point bottomtick_startpoint = new Point ();
3852                         Point channel_startpoint = new Point ();
3853                         float pixel_len;
3854                         float pixels_betweenticks;
3855                         const int space_from_right = 8;
3856                         const int space_from_left = 8;
3857                         const int space_from_bottom = 11;
3858                         Rectangle area = tb.ClientRectangle;
3859                         
3860                         switch (tb.TickStyle)   {
3861                         case TickStyle.BottomRight:
3862                         case TickStyle.None:
3863                                 channel_startpoint.Y = 8;
3864                                 channel_startpoint.X = 9;
3865                                 bottomtick_startpoint.Y = 13;
3866                                 bottomtick_startpoint.X = 24;                           
3867                                 break;
3868                         case TickStyle.TopLeft:
3869                                 channel_startpoint.Y = 8;
3870                                 channel_startpoint.X = 19;
3871                                 toptick_startpoint.Y = 13;
3872                                 toptick_startpoint.X = 8;
3873                                 break;
3874                         case TickStyle.Both:
3875                                 channel_startpoint.Y = 8;
3876                                 channel_startpoint.X = 18;      
3877                                 bottomtick_startpoint.Y = 13;
3878                                 bottomtick_startpoint.X = 32;                           
3879                                 toptick_startpoint.Y = 13;
3880                                 toptick_startpoint.X = 8;                               
3881                                 break;
3882                         default:
3883                                 break;
3884                         }
3885                         
3886                         thumb_area.X = area.X + channel_startpoint.X;
3887                         thumb_area.Y = area.Y + channel_startpoint.Y;
3888                         thumb_area.Height = area.Height - space_from_right - space_from_left;
3889                         thumb_area.Width = 4;
3890
3891                         /* Draw channel */
3892                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDark), channel_startpoint.X, channel_startpoint.Y,
3893                                 1, thumb_area.Height);
3894                         
3895                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDarkDark), channel_startpoint.X + 1, channel_startpoint.Y,
3896                                 1, thumb_area.Height);
3897
3898                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlLight), channel_startpoint.X + 3, channel_startpoint.Y,
3899                                 1, thumb_area.Height);
3900
3901                         pixel_len = thumb_area.Height - 11;
3902                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
3903                         
3904                         /* Convert thumb position from mouse position to value*/
3905                         if (mouse_value) {
3906                                 if (value_pos < thumb_area.Bottom)
3907                                         value_pos = (int) ((thumb_area.Bottom - value_pos) / pixels_betweenticks);
3908                                 else
3909                                         value_pos = 0;                  
3910
3911                                 if (value_pos + tb.Minimum > tb.Maximum)
3912                                         value_pos = tb.Maximum - tb.Minimum;
3913
3914                                 tb.Value = value_pos + tb.Minimum;
3915                         }                       
3916
3917                         // thumb_pos.Y = channel_startpoint.Y ; // + (int) (pixels_betweenticks * (float) value_pos);
3918                         thumb_pos.Y = thumb_area.Bottom - space_from_bottom - (int) (pixels_betweenticks * (float) value_pos);
3919                         
3920                         /* Draw thumb fixed 10x22 size */
3921                         thumb_pos.Width = 10;
3922                         thumb_pos.Height = 22;
3923
3924                         switch (tb.TickStyle)   {
3925                         case TickStyle.BottomRight:
3926                         case TickStyle.None: {
3927                                 thumb_pos.X = channel_startpoint.X - 8;
3928
3929                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X , thumb_pos.Y + 10);
3930                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 16, thumb_pos.Y);
3931                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X + 16, thumb_pos.Y, thumb_pos.X + 16 + 4, thumb_pos.Y + 4);
3932                                 
3933                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X +1, thumb_pos.Y + 9, thumb_pos.X +15, thumb_pos.Y  +9);
3934                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 16, thumb_pos.Y + 9, thumb_pos.X +16 + 4, thumb_pos.Y  +9 - 4);
3935
3936                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X, thumb_pos.Y  + 10, thumb_pos.X +16, thumb_pos.Y +10);
3937                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 16, thumb_pos.Y  + 10, thumb_pos.X  +16 + 5, thumb_pos.Y +10 - 5);
3938
3939                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 16, 8);
3940                                 dc.FillRectangle (br_thumb, thumb_pos.X + 17, thumb_pos.Y + 2, 1, 6);
3941                                 dc.FillRectangle (br_thumb, thumb_pos.X + 18, thumb_pos.Y + 3, 1, 4);
3942                                 dc.FillRectangle (br_thumb, thumb_pos.X + 19, thumb_pos.Y + 4, 1, 2);
3943
3944                                 break;
3945                         }
3946                         case TickStyle.TopLeft: {
3947                                 thumb_pos.X = channel_startpoint.X - 10;
3948
3949                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X + 4 + 16, thumb_pos.Y);
3950                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 4);
3951
3952                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X  + 4, thumb_pos.Y + 9, thumb_pos.X + 4 + 16 , thumb_pos.Y+ 9);
3953                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 4, thumb_pos.Y  + 9, thumb_pos.X, thumb_pos.Y + 5);
3954                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X  + 19, thumb_pos.Y + 9, thumb_pos.X  +19 , thumb_pos.Y+ 1);
3955
3956                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X  + 4, thumb_pos.Y+ 10, thumb_pos.X  + 4 + 16, thumb_pos.Y+ 10);
3957                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X  + 4, thumb_pos.Y + 10, thumb_pos.X  -1, thumb_pos.Y+ 5);
3958                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 20, thumb_pos.Y, thumb_pos.X+ 20, thumb_pos.Y + 10);
3959
3960                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 15, 8);
3961                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 1, 6);
3962                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 1, 4);
3963                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 1, 2);
3964
3965                                 break;
3966                         }
3967
3968                         case TickStyle.Both: {
3969                                 thumb_pos.X = area.X + 10;
3970                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 9);
3971                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 19, thumb_pos.Y);
3972
3973                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 1, thumb_pos.Y + 9, thumb_pos.X+ 19, thumb_pos.Y  + 9);
3974                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X  + 10, thumb_pos.Y+ 1, thumb_pos.X + 19, thumb_pos.Y  + 8);
3975
3976                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X, thumb_pos.Y + 10, thumb_pos.X+ 20, thumb_pos.Y  +10);
3977                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X  + 20, thumb_pos.Y, thumb_pos.X  + 20, thumb_pos.Y+ 9);
3978
3979                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 18, 8);
3980
3981                                 break;
3982                         }
3983
3984                         default:
3985                                 break;
3986                         }
3987
3988                         pixel_len = thumb_area.Height - 11;
3989                         pixels_betweenticks = pixel_len / ticks;
3990                         
3991                         thumb_area.X = thumb_pos.X;
3992                         thumb_area.Y = channel_startpoint.Y;
3993                         thumb_area.Width = thumb_pos.Height;
3994                         
3995                         /* Draw ticks*/
3996                         Region outside = new Region (area);
3997                         outside.Exclude (thumb_area);                   
3998                         
3999                         if (outside.IsVisible (clip_rectangle)) {                               
4000                                 if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight ||
4001                                         ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) { 
4002                                         
4003                                         for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks)  {                                       
4004                                                 if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
4005                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X , area.Y + bottomtick_startpoint.Y  + inc, 
4006                                                                 area.X + bottomtick_startpoint.X  + 3, area.Y + bottomtick_startpoint.Y + inc);
4007                                                 else
4008                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X, area.Y + bottomtick_startpoint.Y  + inc, 
4009                                                                 area.X + bottomtick_startpoint.X  + 2, area.Y + bottomtick_startpoint.Y + inc);
4010                                         }
4011                                 }
4012         
4013                                 if (pixels_betweenticks > 0 &&  ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft ||
4014                                         ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {
4015         
4016                                         pixel_len = thumb_area.Height - 11;
4017                                         pixels_betweenticks = pixel_len / ticks;
4018                                         
4019                                         for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
4020                                                 if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
4021                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X  - 3 , area.Y + toptick_startpoint.Y + inc, 
4022                                                                 area.X + toptick_startpoint.X, area.Y + toptick_startpoint.Y + inc);
4023                                                 else
4024                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X  - 2, area.Y + toptick_startpoint.Y + inc, 
4025                                                                 area.X + toptick_startpoint.X, area.Y + toptick_startpoint.Y  + inc);
4026                                         }                       
4027                                 }
4028                         }
4029                         
4030                         outside.Dispose ();
4031                         
4032                 }
4033
4034                 /* 
4035                         Horizontal trackbar 
4036                   
4037                         Does not matter the size of the control, Win32 always draws:
4038                                 - Ticks starting from pixel 13, 8
4039                                 - Channel starting at pos 8, 19 and ends at Width - 8
4040                                 - Autosize makes always the control 40 pixels height
4041                                 - Ticks are draw at (channel.Witdh - 10) / (Maximum - Minimum)
4042                                 
4043                 */
4044                 private void DrawTrackBar_Horizontal (Graphics dc, Rectangle clip_rectangle, TrackBar tb,
4045                         ref Rectangle thumb_pos, ref Rectangle thumb_area, Brush br_thumb,
4046                         float ticks, int value_pos, bool mouse_value) {                 
4047                         Point toptick_startpoint = new Point ();
4048                         Point bottomtick_startpoint = new Point ();
4049                         Point channel_startpoint = new Point ();
4050                         float pixel_len;
4051                         float pixels_betweenticks;
4052                         const int space_from_right = 8;
4053                         const int space_from_left = 8;
4054                         Rectangle area = tb.ClientRectangle;
4055                                                 
4056                         switch (tb.TickStyle) {
4057                         case TickStyle.BottomRight:
4058                         case TickStyle.None:
4059                                 channel_startpoint.X = 8;
4060                                 channel_startpoint.Y = 9;
4061                                 bottomtick_startpoint.X = 13;
4062                                 bottomtick_startpoint.Y = 24;                           
4063                                 break;
4064                         case TickStyle.TopLeft:
4065                                 channel_startpoint.X = 8;
4066                                 channel_startpoint.Y = 19;
4067                                 toptick_startpoint.X = 13;
4068                                 toptick_startpoint.Y = 8;
4069                                 break;
4070                         case TickStyle.Both:
4071                                 channel_startpoint.X = 8;
4072                                 channel_startpoint.Y = 18;      
4073                                 bottomtick_startpoint.X = 13;
4074                                 bottomtick_startpoint.Y = 32;                           
4075                                 toptick_startpoint.X = 13;
4076                                 toptick_startpoint.Y = 8;                               
4077                                 break;
4078                         default:
4079                                 break;
4080                         }
4081                                                 
4082                         thumb_area.X = area.X + channel_startpoint.X;
4083                         thumb_area.Y = area.Y + channel_startpoint.Y;
4084                         thumb_area.Width = area.Width - space_from_right - space_from_left;
4085                         thumb_area.Height = 4;
4086                         
4087                         /* Draw channel */
4088                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDark), channel_startpoint.X, channel_startpoint.Y,
4089                                 thumb_area.Width, 1);
4090                         
4091                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDarkDark), channel_startpoint.X, channel_startpoint.Y + 1,
4092                                 thumb_area.Width, 1);
4093
4094                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlLight), channel_startpoint.X, channel_startpoint.Y +3,
4095                                 thumb_area.Width, 1);
4096
4097                         pixel_len = thumb_area.Width - 11;
4098                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
4099
4100                         /* Convert thumb position from mouse position to value*/
4101                         if (mouse_value) {                      
4102                                 if (value_pos >= channel_startpoint.X)
4103                                         value_pos = (int)(((float) (value_pos - channel_startpoint.X)) / pixels_betweenticks);
4104                                 else
4105                                         value_pos = 0;                          
4106
4107                                 if (value_pos + tb.Minimum > tb.Maximum)
4108                                         value_pos = tb.Maximum - tb.Minimum;
4109                                 
4110                                 tb.Value = value_pos + tb.Minimum;
4111                         }                       
4112                         
4113                         thumb_pos.X = channel_startpoint.X + (int) (pixels_betweenticks * (float) value_pos);
4114                         
4115                         /* Draw thumb fixed 10x22 size */
4116                         thumb_pos.Width = 10;
4117                         thumb_pos.Height = 22;
4118
4119                         switch (tb.TickStyle) {
4120                         case TickStyle.BottomRight:
4121                         case TickStyle.None: {
4122                                 thumb_pos.Y = channel_startpoint.Y - 8;
4123
4124                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y);
4125                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 16);
4126                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y + 16, thumb_pos.X + 4, thumb_pos.Y + 16 + 4);
4127
4128                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X +9, thumb_pos.Y +15);
4129                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 16, thumb_pos.X +9 - 4, thumb_pos.Y +16 + 4);
4130
4131                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X +10, thumb_pos.Y +16);
4132                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 10, thumb_pos.Y + 16, thumb_pos.X +10 - 5, thumb_pos.Y +16 + 5);
4133
4134                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 16);
4135                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 17, 6, 1);
4136                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 18, 4, 1);
4137                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 19, 2, 1);
4138                                 break;
4139                         }
4140                         case TickStyle.TopLeft: {
4141                                 thumb_pos.Y = channel_startpoint.Y - 10;
4142
4143                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X, thumb_pos.Y + 4 + 16);
4144                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X + 4, thumb_pos.Y);
4145
4146                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 9, thumb_pos.Y + 4 + 16);
4147                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y);
4148                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 19, thumb_pos.X + 1 , thumb_pos.Y +19);
4149
4150                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 10, thumb_pos.Y + 4 + 16);
4151                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y -1);
4152                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 10, thumb_pos.Y + 20);
4153
4154                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 8, 15);
4155                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 6, 1);
4156                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 4, 1);
4157                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 2, 1);
4158                                 break;
4159                         }
4160
4161                         case TickStyle.Both: {
4162                                 thumb_pos.Y = area.Y + 10;
4163                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 9, thumb_pos.Y);
4164                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 19);
4165
4166                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X + 9, thumb_pos.Y + 19);
4167                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), thumb_pos.X + 1, thumb_pos.Y + 10, thumb_pos.X + 8, thumb_pos.Y + 19);
4168
4169                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X +10, thumb_pos.Y + 20);
4170                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 9, thumb_pos.Y + 20);
4171
4172                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 18);
4173
4174                                 break;
4175                         }
4176
4177                         default:
4178                                 break;
4179                         }
4180
4181                         pixel_len = thumb_area.Width - 11;
4182                         pixels_betweenticks = pixel_len / ticks;
4183
4184                         /* Draw ticks*/
4185                         thumb_area.Y = thumb_pos.Y;
4186                         thumb_area.X = channel_startpoint.X;
4187                         thumb_area.Height = thumb_pos.Height;
4188                         Region outside = new Region (area);
4189                         outside.Exclude (thumb_area);                   
4190                         
4191                         if (outside.IsVisible (clip_rectangle)) {                               
4192                                 if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight ||
4193                                         ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {                         
4194                                         
4195                                         for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
4196                                                 if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
4197                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X + inc , area.Y + bottomtick_startpoint.Y, 
4198                                                                 area.X + bottomtick_startpoint.X + inc , area.Y + bottomtick_startpoint.Y + 3);
4199                                                 else
4200                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X + inc, area.Y + bottomtick_startpoint.Y, 
4201                                                                 area.X + bottomtick_startpoint.X + inc, area.Y + bottomtick_startpoint.Y + 2);
4202                                         }
4203                                 }
4204         
4205                                 if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft ||
4206                                         ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {
4207                                         
4208                                         for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
4209                                                 if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
4210                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X + inc , area.Y + toptick_startpoint.Y - 3, 
4211                                                                 area.X + toptick_startpoint.X + inc , area.Y + toptick_startpoint.Y);
4212                                                 else
4213                                                         dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X + inc, area.Y + toptick_startpoint.Y - 2, 
4214                                                                 area.X + toptick_startpoint.X + inc, area.Y + toptick_startpoint.Y );
4215                                         }                       
4216                                 }
4217                         }
4218                         
4219                         outside.Dispose ();                     
4220                 }
4221
4222                 public override void DrawTrackBar (Graphics dc, Rectangle clip_rectangle, TrackBar tb) 
4223                 {
4224                         Brush           br_thumb;
4225                         int             value_pos;
4226                         bool            mouse_value;
4227                         float           ticks = (tb.Maximum - tb.Minimum) / tb.tickFrequency; /* N of ticks draw*/
4228                         Rectangle       area;
4229                         Rectangle       thumb_pos = tb.ThumbPos;
4230                         Rectangle       thumb_area = tb.ThumbArea;
4231                         
4232                         if (tb.thumb_pressed) {
4233                                 value_pos = tb.thumb_mouseclick;
4234                                 mouse_value = true;
4235                         } else {
4236                                 value_pos = tb.Value - tb.Minimum;
4237                                 mouse_value = false;
4238                         }
4239
4240                         area = tb.ClientRectangle;
4241
4242                         if (tb.thumb_pressed == true) {
4243                                 br_thumb = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLight, ColorControl);
4244                         } else {
4245                                 br_thumb = ResPool.GetSolidBrush (ColorControl);
4246                         }
4247
4248                         
4249                         /* Control Background */
4250                         if (tb.BackColor == DefaultControlBackColor) {
4251                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), clip_rectangle);
4252                         } else {
4253                                 dc.FillRectangle (ResPool.GetSolidBrush (tb.BackColor), clip_rectangle);
4254                         }
4255                         
4256
4257                         if (tb.Focused) {
4258                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControl, Color.Black), area.X, area.Y, area.Width - 1, 1);
4259                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControl, Color.Black), area.X, area.Y + area.Height - 1, area.Width - 1, 1);
4260                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControl, Color.Black), area.X, area.Y, 1, area.Height - 1);
4261                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControl, Color.Black), area.X + area.Width - 1, area.Y, 1, area.Height - 1);
4262                         }
4263
4264                         if (tb.Orientation == Orientation.Vertical) {
4265                                 DrawTrackBar_Vertical (dc, clip_rectangle, tb, ref thumb_pos, ref thumb_area,
4266                                         br_thumb, ticks, value_pos, mouse_value);
4267                         
4268                         } else {
4269                                 DrawTrackBar_Horizontal (dc, clip_rectangle, tb, ref thumb_pos, ref thumb_area,
4270                                         br_thumb, ticks, value_pos, mouse_value);
4271                         }
4272
4273                         tb.ThumbPos = thumb_pos;
4274                         tb.ThumbArea = thumb_area;
4275                 }
4276
4277                 public override Size TrackBarDefaultSize {
4278                         get {
4279                                 return new Size (104, 42);
4280                         }
4281                 }
4282
4283                 #endregion      // TrackBar
4284
4285                 #region VScrollBar
4286                 public override Size VScrollBarDefaultSize {
4287                         get {
4288                                 return new Size (this.ScrollBarButtonSize, 80);
4289                         }
4290                 }
4291                 #endregion      // VScrollBar
4292
4293                 #region TreeView
4294                 public override Size TreeViewDefaultSize {
4295                         get {
4296                                 return new Size (121, 97);
4297                         }
4298                 }
4299
4300                 #endregion
4301
4302                 #region ControlPaint
4303                 private enum DrawFrameControlStates {
4304                         ButtonCheck             = 0x0000,
4305                         ButtonRadioImage        = 0x0001,
4306                         ButtonRadioMask         = 0x0002,
4307                         ButtonRadio             = 0x0004,
4308                         Button3State            = 0x0008,
4309                         ButtonPush              = 0x0010,
4310
4311                         CaptionClose            = 0x0000,
4312                         CaptionMin              = 0x0001,
4313                         CaptionMax              = 0x0002,
4314                         CaptionRestore          = 0x0004,
4315                         CaptionHelp             = 0x0008,
4316
4317                         MenuArrow               = 0x0000,
4318                         MenuCheck               = 0x0001,
4319                         MenuBullet              = 0x0002,
4320                         MenuArrowRight          = 0x0004,
4321
4322                         ScrollUp                = 0x0000,
4323                         ScrollDown              = 0x0001,
4324                         ScrollLeft              = 0x0002,
4325                         ScrollRight             = 0x0003,
4326                         ScrollComboBox          = 0x0005,
4327                         ScrollSizeGrip          = 0x0008,
4328                         ScrollSizeGripRight     = 0x0010,
4329
4330                         Inactive                = 0x0100,
4331                         Pushed                  = 0x0200,
4332                         Checked                 = 0x0400,
4333                         Transparent             = 0x0800,
4334                         Hot                     = 0x1000,
4335                         AdjustRect              = 0x2000,
4336                         Flat                    = 0x4000,
4337                         Mono                    = 0x8000
4338
4339                 }
4340
4341                 private enum DrawFrameControlTypes {
4342                         Caption = 1,
4343                         Menu    = 2,
4344                         Scroll  = 3,
4345                         Button  = 4
4346                 }
4347
4348                 public override void CPDrawBorder (Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
4349                         ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
4350                         Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor,
4351                         int bottomWidth, ButtonBorderStyle bottomStyle) {
4352                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom-1, leftWidth, leftColor, leftStyle, Border3DSide.Left);
4353                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Right-1, bounds.Top, topWidth, topColor, topStyle, Border3DSide.Top);
4354                         DrawBorderInternal(graphics, bounds.Right-1, bounds.Top, bounds.Right-1, bounds.Bottom-1, rightWidth, rightColor, rightStyle, Border3DSide.Right);
4355                         DrawBorderInternal(graphics, bounds.Left, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1, bottomWidth, bottomColor, bottomStyle, Border3DSide.Bottom);
4356                 }
4357
4358                 public override void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
4359                         CPDrawBorder3D(graphics, rectangle, style, sides, ColorControl);
4360                 }
4361
4362                 private void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides, Color control_color)
4363                 {
4364                         Pen             penTopLeft;
4365                         Pen             penTopLeftInner;
4366                         Pen             penBottomRight;
4367                         Pen             penBottomRightInner;
4368                         Rectangle       rect= new Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
4369                         
4370                         if ((style & Border3DStyle.Adjust) != 0) {
4371                                 rect.Y -= 2;
4372                                 rect.X -= 2;
4373                                 rect.Width += 4;
4374                                 rect.Height += 4;
4375                         }
4376                         
4377                         penTopLeft = penTopLeftInner = penBottomRight = penBottomRightInner = ResPool.GetPen (control_color);
4378                         
4379                         CPColor cpcolor = ResPool.GetCPColor (control_color);
4380                         
4381                         switch (style) {
4382                         case Border3DStyle.Raised:
4383                                 penTopLeftInner = ResPool.GetPen (cpcolor.LightLight);
4384                                 penBottomRight = ResPool.GetPen (cpcolor.DarkDark);
4385                                 penBottomRightInner = ResPool.GetPen (cpcolor.Dark);
4386                                 break;
4387                         case Border3DStyle.Sunken:
4388                                 penTopLeft = ResPool.GetPen (cpcolor.Dark);
4389                                 penTopLeftInner = ResPool.GetPen (cpcolor.DarkDark);
4390                                 penBottomRight = ResPool.GetPen (cpcolor.LightLight);
4391                                 break;
4392                         case Border3DStyle.Etched:
4393                                 penTopLeft = penBottomRightInner = ResPool.GetPen (cpcolor.Dark);
4394                                 penTopLeftInner = penBottomRight = ResPool.GetPen (cpcolor.LightLight);
4395                                 break;
4396                         case Border3DStyle.RaisedOuter:
4397                                 penBottomRight = ResPool.GetPen (cpcolor.DarkDark);
4398                                 break;
4399                         case Border3DStyle.SunkenOuter:
4400                                 penTopLeft = ResPool.GetPen (cpcolor.Dark);
4401                                 penBottomRight = ResPool.GetPen (cpcolor.LightLight);
4402                                 break;
4403                         case Border3DStyle.RaisedInner:
4404                                 penTopLeft = ResPool.GetPen (cpcolor.LightLight);
4405                                 penBottomRight = ResPool.GetPen (cpcolor.Dark);
4406                                 break;
4407                         case Border3DStyle.SunkenInner:
4408                                 penTopLeft = ResPool.GetPen (cpcolor.DarkDark);
4409                                 break;
4410                         case Border3DStyle.Flat:
4411                                 penTopLeft = penBottomRight = ResPool.GetPen (cpcolor.Dark);
4412                                 break;
4413                         case Border3DStyle.Bump:
4414                                 penTopLeftInner = penBottomRight = ResPool.GetPen (cpcolor.DarkDark);
4415                                 break;
4416                         default:
4417                                 break;
4418                         }
4419                         
4420                         if ((sides & Border3DSide.Middle) != 0) {
4421                                 graphics.FillRectangle (ResPool.GetSolidBrush (control_color), rect);
4422                         }
4423                         
4424                         if ((sides & Border3DSide.Left) != 0) {
4425                                 graphics.DrawLine (penTopLeft, rect.Left, rect.Bottom - 2, rect.Left, rect.Top);
4426                                 graphics.DrawLine (penTopLeftInner, rect.Left + 1, rect.Bottom - 2, rect.Left + 1, rect.Top);
4427                         }
4428                         
4429                         if ((sides & Border3DSide.Top) != 0) {
4430                                 graphics.DrawLine (penTopLeft, rect.Left, rect.Top, rect.Right - 2, rect.Top);
4431                                 graphics.DrawLine (penTopLeftInner, rect.Left + 1, rect.Top + 1, rect.Right - 3, rect.Top + 1);
4432                         }
4433                         
4434                         if ((sides & Border3DSide.Right) != 0) {
4435                                 graphics.DrawLine (penBottomRight, rect.Right - 1, rect.Top, rect.Right - 1, rect.Bottom - 1);
4436                                 graphics.DrawLine (penBottomRightInner, rect.Right - 2, rect.Top + 1, rect.Right - 2, rect.Bottom - 2);
4437                         }
4438                         
4439                         if ((sides & Border3DSide.Bottom) != 0) {
4440                                 graphics.DrawLine (penBottomRight, rect.Left, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
4441                                 graphics.DrawLine (penBottomRightInner, rect.Left + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
4442                         }
4443                 }
4444
4445                 public override void CPDrawButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
4446                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonPush;
4447
4448                         if ((state & ButtonState.Pushed)!=0) {
4449                                 dfcs |= DrawFrameControlStates.Pushed;
4450                         }
4451
4452                         if ((state & ButtonState.Checked)!=0) {
4453                                 dfcs |= DrawFrameControlStates.Checked;
4454                         }
4455
4456                         if ((state & ButtonState.Flat)!=0) {
4457                                 dfcs |= DrawFrameControlStates.Flat;
4458                         }
4459
4460                         if ((state & ButtonState.Inactive)!=0) {
4461                                 dfcs |= DrawFrameControlStates.Inactive;
4462                         }
4463                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
4464                 }
4465
4466
4467                 public override void CPDrawCaptionButton (Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
4468                         Rectangle       captionRect;
4469                         int                     lineWidth;
4470
4471                         CPDrawButton(graphics, rectangle, state);
4472
4473                         if (rectangle.Width<rectangle.Height) {
4474                                 captionRect=new Rectangle(rectangle.X+1, rectangle.Y+rectangle.Height/2-rectangle.Width/2+1, rectangle.Width-4, rectangle.Width-4);
4475                         } else {
4476                                 captionRect=new Rectangle(rectangle.X+rectangle.Width/2-rectangle.Height/2+1, rectangle.Y+1, rectangle.Height-4, rectangle.Height-4);
4477                         }
4478
4479                         if ((state & ButtonState.Pushed)!=0) {
4480                                 captionRect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-3, rectangle.Height-3);
4481                         }
4482
4483                         /* Make sure we've got at least a line width of 1 */
4484                         lineWidth=Math.Max(1, captionRect.Width/7);
4485
4486                         switch(button) {
4487                         case CaptionButton.Close: {
4488                                 Pen     pen;
4489
4490                                 if ((state & ButtonState.Inactive)!=0) {
4491                                         pen=new Pen(ColorControlLight, lineWidth);
4492                                         DrawCaptionHelper(graphics, ColorControlLight, pen, lineWidth, 1, captionRect, button);
4493                                         pen.Dispose();
4494
4495                                         pen=new Pen(ColorControlDark, lineWidth);
4496                                         DrawCaptionHelper(graphics, ColorControlDark, pen, lineWidth, 0, captionRect, button);
4497                                         pen.Dispose();
4498                                         return;
4499                                 } else {
4500                                         pen=new Pen(ColorControlText, lineWidth);
4501                                         DrawCaptionHelper(graphics, ColorControlText, pen, lineWidth, 0, captionRect, button);
4502                                         pen.Dispose();
4503                                         return;
4504                                 }
4505                         }
4506
4507                         case CaptionButton.Help:
4508                         case CaptionButton.Maximize:
4509                         case CaptionButton.Minimize:
4510                         case CaptionButton.Restore: {
4511                                 if ((state & ButtonState.Inactive)!=0) {
4512                                         DrawCaptionHelper(graphics, ColorControlLight, SystemPens.ControlLightLight, lineWidth, 1, captionRect, button);
4513
4514                                         DrawCaptionHelper(graphics, ColorControlDark, SystemPens.ControlDark, lineWidth, 0, captionRect, button);
4515                                         return;
4516                                 } else {
4517                                         DrawCaptionHelper(graphics, ColorControlText, SystemPens.ControlText, lineWidth, 0, captionRect, button);
4518                                         return;
4519                                 }
4520                         }
4521                         }
4522                 }
4523
4524
4525                 public override void CPDrawCheckBox (Graphics graphics, Rectangle rectangle, ButtonState state) {
4526                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonCheck;
4527
4528                         if ((state & ButtonState.Pushed)!=0) {
4529                                 dfcs |= DrawFrameControlStates.Pushed;
4530                         }
4531
4532                         if ((state & ButtonState.Checked)!=0) {
4533                                 dfcs |= DrawFrameControlStates.Checked;
4534                         }
4535
4536                         if ((state & ButtonState.Flat)!=0) {
4537                                 dfcs |= DrawFrameControlStates.Flat;
4538                         }
4539
4540                         if ((state & ButtonState.Inactive)!=0) {
4541                                 dfcs |= DrawFrameControlStates.Inactive;
4542                         }
4543
4544                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
4545
4546                 }
4547
4548                 public override void CPDrawComboButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
4549                         Point[]                 arrow = new Point[3];
4550                         Point                           P1;
4551                         Point                           P2;
4552                         Point                           P3;
4553                         int                             centerX;
4554                         int                             centerY;
4555                         int                             shiftX;
4556                         int                             shiftY;
4557                         Rectangle               rect;
4558
4559                         if ((state & ButtonState.Checked)!=0) {
4560                                 graphics.FillRectangle(ResPool.GetHatchBrush (HatchStyle.Percent50, ColorControlLightLight, ColorControlLight),rectangle);                              
4561                         }
4562
4563                         if ((state & ButtonState.Flat)!=0) {
4564                                 ControlPaint.DrawBorder(graphics, rectangle, ColorControlDark, ButtonBorderStyle.Solid);
4565                         } else {
4566                                 if ((state & (ButtonState.Pushed | ButtonState.Checked))!=0) {
4567                                         // this needs to render like a pushed button - jba
4568                                         // CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
4569                                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
4570                                         graphics.DrawRectangle (ResPool.GetPen (ControlPaint.Dark (ColorControl)), trace_rectangle);
4571                                 } else {
4572                                         CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
4573                                 }
4574                         }
4575
4576                         rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
4577                         centerX=rect.Left+rect.Width/2;
4578                         centerY=rect.Top+rect.Height/2;
4579                         shiftX=Math.Max(1, rect.Width/8);
4580                         shiftY=Math.Max(1, rect.Height/8);
4581
4582                         if ((state & ButtonState.Pushed)!=0) {
4583                                 shiftX++;
4584                                 shiftY++;
4585                         }
4586
4587                         rect.Y-=shiftY;
4588                         centerY-=shiftY;
4589                         P1=new Point(rect.Left, centerY);
4590                         P2=new Point(rect.Right, centerY);
4591                         P3=new Point(centerX, rect.Bottom);
4592
4593                         arrow[0]=P1;
4594                         arrow[1]=P2;
4595                         arrow[2]=P3;
4596
4597                         /* Draw the arrow */
4598                         if ((state & ButtonState.Inactive)!=0) {
4599                                 graphics.FillPolygon(SystemBrushes.ControlLightLight, arrow, FillMode.Winding);
4600
4601                                 /* Move away from the shadow */
4602                                 P1.X-=1;                P1.Y-=1;
4603                                 P2.X-=1;                P2.Y-=1;
4604                                 P3.X-=1;                P3.Y-=1;
4605
4606                                 arrow[0]=P1;
4607                                 arrow[1]=P2;
4608                                 arrow[2]=P3;
4609
4610
4611                                 graphics.FillPolygon(SystemBrushes.ControlDark, arrow, FillMode.Winding);
4612                         } else {
4613                                 graphics.FillPolygon(SystemBrushes.ControlText, arrow, FillMode.Winding);
4614                         }
4615                 }
4616
4617
4618                 public override void CPDrawContainerGrabHandle (Graphics graphics, Rectangle bounds) {
4619                         
4620                         Pen                     pen     = new Pen(Color.Black, 1);
4621                         Rectangle       rect    = new Rectangle(bounds.X, bounds.Y, bounds.Width-1, bounds.Height-1);   // Dunno why, but MS does it that way, too
4622                         int                     X;
4623                         int                     Y;
4624
4625                         graphics.FillRectangle(ResPool.GetSolidBrush (ColorControlText), rect);
4626                         graphics.DrawRectangle(pen, rect);
4627
4628                         X=rect.X+rect.Width/2;
4629                         Y=rect.Y+rect.Height/2;
4630
4631                         /* Draw the cross */
4632                         graphics.DrawLine(pen, X, rect.Y+2, X, rect.Bottom-2);
4633                         graphics.DrawLine(pen, rect.X+2, Y, rect.Right-2, Y);
4634
4635                         /* Draw 'arrows' for vertical lines */
4636                         graphics.DrawLine(pen, X-1, rect.Y+3, X+1, rect.Y+3);
4637                         graphics.DrawLine(pen, X-1, rect.Bottom-3, X+1, rect.Bottom-3);
4638
4639                         /* Draw 'arrows' for horizontal lines */
4640                         graphics.DrawLine(pen, rect.X+3, Y-1, rect.X+3, Y+1);
4641                         graphics.DrawLine(pen, rect.Right-3, Y-1, rect.Right-3, Y+1);
4642                         pen.Dispose ();
4643
4644                 }
4645
4646                 public virtual void DrawFlatStyleFocusRectangle (Graphics graphics, Rectangle rectangle, ButtonBase button, Color foreColor, Color backColor) {
4647                         // make a rectange to trace around border of the button
4648                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
4649                         
4650                         Color outerColor = foreColor;
4651                         // adjust focus color according to the flatstyle
4652                         if (button.FlatStyle == FlatStyle.Popup && !button.is_pressed) {
4653                                 outerColor = (backColor == ColorControl) ? ControlPaint.Dark(ColorControl) : ColorControlText;                          
4654                         }
4655                         
4656                         // draw the outer rectangle
4657                         graphics.DrawRectangle (ResPool.GetPen (outerColor), trace_rectangle);                  
4658                         
4659                         // draw the inner rectangle                                             
4660                         if (button.FlatStyle == FlatStyle.Popup) {
4661                                 DrawInnerFocusRectangle (graphics, Rectangle.Inflate (rectangle, -4, -4), backColor);
4662                         } else {
4663                                 // draw a flat inner rectangle
4664                                 Pen pen = ResPool.GetPen (ControlPaint.LightLight (backColor));
4665                                 graphics.DrawRectangle(pen, Rectangle.Inflate (trace_rectangle, -4, -4));                               
4666                         }
4667                 }
4668                 
4669                 public virtual void DrawInnerFocusRectangle(Graphics graphics, Rectangle rectangle, Color backColor)
4670                 {       
4671                         // make a rectange to trace around border of the button
4672                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
4673                         
4674 #if NotUntilCairoIsFixed
4675                         Color colorBackInverted = Color.FromArgb (Math.Abs (backColor.R-255), Math.Abs (backColor.G-255), Math.Abs (backColor.B-255));
4676                         DashStyle oldStyle; // used for caching old penstyle
4677                         Pen pen = ResPool.GetPen (colorBackInverted);
4678
4679                         oldStyle = pen.DashStyle; 
4680                         pen.DashStyle = DashStyle.Dot;
4681
4682                         graphics.DrawRectangle (pen, trace_rectangle);
4683                         pen.DashStyle = oldStyle;
4684 #else
4685                         CPDrawFocusRectangle(graphics, trace_rectangle, Color.Wheat, backColor);
4686 #endif
4687                 }
4688                                 
4689
4690                 public override void CPDrawFocusRectangle (Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) 
4691                 {                       
4692                         Rectangle rect = rectangle;
4693                         Pen pen;
4694                         HatchBrush brush;
4695                                 
4696                         if (backColor.GetBrightness () >= 0.5) {
4697                                 foreColor = Color.Transparent;
4698                                 backColor = Color.Black;
4699                                 
4700                         } else {
4701                                 backColor = Color.FromArgb (Math.Abs (backColor.R-255), Math.Abs (backColor.G-255), Math.Abs (backColor.B-255));
4702                                 foreColor = Color.Black;
4703                         }
4704                                                 
4705                         brush = ResPool.GetHatchBrush (HatchStyle.Percent50, backColor, foreColor);
4706                         pen = new Pen (brush, 1);
4707                                                 
4708                         rect.Width--;
4709                         rect.Height--;                  
4710                         
4711                         graphics.DrawRectangle (pen, rect);
4712                         pen.Dispose ();
4713                 }
4714                 
4715                 public override void CPDrawGrabHandle (Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
4716                         SolidBrush      sb;
4717                         Pen                     pen;
4718
4719                         if (primary==true) {
4720                                 pen=new Pen(Color.Black, 1);
4721                                 if (enabled==true) {
4722                                         sb=ResPool.GetSolidBrush (ColorControlText);
4723                                 } else {
4724                                         sb=ResPool.GetSolidBrush (ColorControl);
4725                                 }
4726                         } else {
4727                                 pen=new Pen(Color.White, 1);
4728                                 if (enabled==true) {
4729                                         sb=ResPool.GetSolidBrush (Color.Black);
4730                                 } else {
4731                                         sb=ResPool.GetSolidBrush (ColorControl);
4732                                 }
4733                         }
4734                         graphics.FillRectangle(sb, rectangle);
4735                         graphics.DrawRectangle(pen, rectangle);                 
4736                         pen.Dispose();
4737                 }
4738
4739
4740                 public override void CPDrawGrid (Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
4741                         Color   foreColor;
4742                         int     h;
4743                         int     b;
4744                         int     s;
4745
4746                         ControlPaint.Color2HBS(backColor, out h, out b, out s);
4747
4748                         if (b>127) {
4749                                 foreColor=Color.Black;
4750                         } else {
4751                                 foreColor=Color.White;
4752                         }
4753
4754 #if false
4755                         /* Commented out until I take the time and figure out
4756                                 which HatchStyle will match requirements. The code below
4757                                 is only correct for Percent50.
4758                         */
4759                         if (pixelsBetweenDots.Width==pixelsBetweenDots.Height) {
4760                                 HatchBrush      brush=null;
4761
4762                                 switch(pixelsBetweenDots.Width) {
4763                                         case 2: brush=new HatchBrush(HatchStyle.Percent50, foreColor, backColor); break;
4764                                         case 4: brush=new HatchBrush(HatchStyle.Percent25, foreColor, backColor); break;
4765                                         case 5: brush=new HatchBrush(HatchStyle.Percent20, foreColor, backColor); break;
4766                                         default: {
4767                                                 /* Have to do it the slow way */
4768                                                 break;
4769                                         }
4770                                 }
4771                                 if (brush!=null) {
4772                                         graphics.FillRectangle(brush, area);
4773                                         pen.Dispose();
4774                                         brush.Dispose();
4775                                         return;
4776                                 }
4777                         }
4778 #endif
4779                         /* Slow method */
4780
4781                         Bitmap bitmap = new Bitmap(area.Width, area.Height, graphics);
4782
4783                         for (int x=0; x<area.Width; x+=pixelsBetweenDots.Width) {
4784                                 for (int y=0; y<area.Height; y+=pixelsBetweenDots.Height) {
4785                                         bitmap.SetPixel(x, y, foreColor);
4786                                 }
4787                         }
4788                         graphics.DrawImage(bitmap, area.X, area.Y, area.Width, area.Height);
4789                         bitmap.Dispose();
4790                 }
4791
4792                 public override void CPDrawImageDisabled (Graphics graphics, Image image, int x, int y, Color background) {
4793                         /*
4794                                 Microsoft seems to ignore the background and simply make
4795                                 the image grayscale. At least when having > 256 colors on
4796                                 the display.
4797                         */
4798                         
4799                         if (imagedisabled_attributes == null) {                         
4800                                 imagedisabled_attributes = new ImageAttributes ();
4801                                 ColorMatrix colorMatrix=new ColorMatrix(new float[][] {
4802                                           // This table would create a perfect grayscale image, based on luminance
4803                                           //                            new float[]{0.3f,0.3f,0.3f,0,0},
4804                                           //                            new float[]{0.59f,0.59f,0.59f,0,0},
4805                                           //                            new float[]{0.11f,0.11f,0.11f,0,0},
4806                                           //                            new float[]{0,0,0,1,0,0},
4807                                           //                            new float[]{0,0,0,0,1,0},
4808                                           //                            new float[]{0,0,0,0,0,1}
4809                 
4810                                           // This table generates a image that is grayscaled and then
4811                                           // brightened up. Seems to match MS close enough.
4812                                           new float[]{0.2f,0.2f,0.2f,0,0},
4813                                           new float[]{0.41f,0.41f,0.41f,0,0},
4814                                           new float[]{0.11f,0.11f,0.11f,0,0},
4815                                           new float[]{0.15f,0.15f,0.15f,1,0,0},
4816                                           new float[]{0.15f,0.15f,0.15f,0,1,0},
4817                                           new float[]{0.15f,0.15f,0.15f,0,0,1}
4818                                   });
4819                                   
4820                                  imagedisabled_attributes.SetColorMatrix (colorMatrix);
4821                         }
4822                         
4823                         graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imagedisabled_attributes);
4824                         
4825                 }
4826
4827
4828                 public override void CPDrawLockedFrame (Graphics graphics, Rectangle rectangle, bool primary) {
4829                         Pen     penBorder;
4830                         Pen     penInside;
4831
4832                         if (primary) {
4833                                 penBorder=new Pen(Color.White, 2);
4834                                 penInside=new Pen(Color.Black, 1);
4835                         } else {
4836                                 penBorder=new Pen(Color.Black, 2);
4837                                 penInside=new Pen(Color.White, 1);
4838                         }
4839                         penBorder.Alignment=PenAlignment.Inset;
4840                         penInside.Alignment=PenAlignment.Inset;
4841
4842                         graphics.DrawRectangle(penBorder, rectangle);
4843                         graphics.DrawRectangle(penInside, rectangle.X+2, rectangle.Y+2, rectangle.Width-5, rectangle.Height-5);
4844                         penBorder.Dispose();
4845                         penInside.Dispose();
4846                 }
4847
4848
4849                 public override void CPDrawMenuGlyph (Graphics graphics, Rectangle rectangle, MenuGlyph glyph, Color color) {
4850                         Rectangle       rect;
4851                         int                     lineWidth;
4852
4853                         Brush brush = ResPool.GetSolidBrush (color);
4854
4855                         switch(glyph) {
4856                         case MenuGlyph.Arrow: {
4857                                 Point[]                 arrow = new Point[3];
4858                                 Point                           P1;
4859                                 Point                           P2;
4860                                 Point                           P3;
4861                                 int                             centerX;
4862                                 int                             centerY;
4863                                 int                             shiftX;
4864
4865                                 rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
4866                                 centerX=rect.Left+rect.Width/2;
4867                                 centerY=rect.Top+rect.Height/2;
4868                                 shiftX=Math.Max(1, rect.Width/8);
4869
4870                                 rect.X-=shiftX;
4871                                 centerX-=shiftX;
4872
4873                                 P1=new Point(centerX, rect.Top-1);
4874                                 P2=new Point(centerX, rect.Bottom);
4875                                 P3=new Point(rect.Right, centerY);
4876
4877                                 arrow[0]=P1;
4878                                 arrow[1]=P2;
4879                                 arrow[2]=P3;
4880
4881                                 graphics.FillPolygon(brush, arrow, FillMode.Winding);
4882
4883                                 return;
4884                         }
4885
4886                         case MenuGlyph.Bullet: {
4887                                 
4888                                 lineWidth=Math.Max(2, rectangle.Width/3);
4889                                 rect=new Rectangle(rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2);
4890                                 
4891                                 graphics.FillEllipse(brush, rect);
4892                                 
4893                                 return;
4894                         }
4895
4896                         case MenuGlyph.Checkmark: {
4897                                 int                     Scale;
4898                                 Pen pen = ResPool.GetPen (color);
4899
4900                                 lineWidth=Math.Max(2, rectangle.Width/6);
4901                                 Scale=Math.Max(1, rectangle.Width/12);
4902
4903                                 rect=new Rectangle(rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2);
4904
4905                                 for (int i=0; i<lineWidth; i++) {
4906                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
4907                                         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);
4908                                 }
4909                                 return;
4910                         }
4911                         }
4912
4913                 }
4914
4915                 public override void CPDrawRadioButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
4916                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonRadio;
4917
4918                         if ((state & ButtonState.Pushed)!=0) {
4919                                 dfcs |= DrawFrameControlStates.Pushed;
4920                         }
4921
4922                         if ((state & ButtonState.Checked)!=0) {
4923                                 dfcs |= DrawFrameControlStates.Checked;
4924                         }
4925
4926                         if ((state & ButtonState.Flat)!=0) {
4927                                 dfcs |= DrawFrameControlStates.Flat;
4928                         }
4929
4930                         if ((state & ButtonState.Inactive)!=0) {
4931                                 dfcs |= DrawFrameControlStates.Inactive;
4932                         }
4933                         
4934                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
4935
4936                 }
4937
4938
4939                 public override void CPDrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
4940
4941                 }
4942
4943
4944                 public override void CPDrawReversibleLine (Point start, Point end, Color backColor) {
4945
4946                 }
4947
4948
4949                 /* Scroll button: regular button + direction arrow */
4950                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state) {
4951                         DrawScrollButtonPrimitive (dc, area, state);
4952
4953                         int arrow_y_pos_diff = 3;
4954                         
4955                         switch (type) {
4956                         case ScrollButton.Up:
4957                                 arrow_y_pos_diff = 2;
4958                                 break;
4959                         case ScrollButton.Down:
4960                                 arrow_y_pos_diff = 4;
4961                                 break;
4962                         default:
4963                                 break;
4964                         }
4965                         
4966                         // A lot of the following is adapted from the rewind project
4967                         Rectangle rect = new Rectangle (area.X - 3, area.Y - arrow_y_pos_diff,
4968                                         area.Width + 6, area.Height + 6);
4969                         int small_diam = rect.Width > rect.Height ? rect.Height : rect.Width;
4970                         if (rect.Width < rect.Height) {
4971                                 rect.Y += (rect.Height - rect.Width) / 2;
4972                                 rect.Height = small_diam;
4973                         } else if (rect.Width > rect.Height) {
4974                                 rect.X += (rect.Width - rect.Height) / 2;
4975                                 rect.Width = small_diam;
4976                         }
4977
4978                         small_diam -= 2;
4979
4980                         int tri = 290 * small_diam / 1000 - 1;
4981                         if (tri == 0)
4982                                 tri = 1;
4983
4984                         Point [] arrow = new Point [3];
4985                         for (int i = 0; i < 3; i++)
4986                                 arrow [i] = new Point ();
4987
4988                         switch(type) {
4989                         default:
4990                         case ScrollButton.Down:
4991                                 arrow [2].X = rect.Left + 470 * small_diam / 1000 + 2;
4992                                 arrow [2].Y = rect.Top + 687 * small_diam / 1000 + 1;
4993                                 arrow [0].X = arrow [2].X - tri;
4994                                 arrow [1].X = arrow [2].X + tri;
4995                                 arrow [0].Y = arrow [1].Y = arrow [2].Y - tri;
4996                                 break;
4997
4998                         case ScrollButton.Up:
4999                                 arrow [2].X = rect.Left + 470 * small_diam / 1000 + 2;
5000                                 arrow [2].Y = rect.Bottom - (687 * small_diam / 1000 + 1);
5001                                 arrow [0].X = arrow [2].X - tri;
5002                                 arrow [1].X = arrow [2].X + tri;
5003                                 arrow [0].Y = arrow [1].Y = arrow [2].Y + tri;
5004                                 break;
5005
5006                         case ScrollButton.Left:
5007                                 arrow [2].X = rect.Right - (687 * small_diam / 1000 + 1);
5008                                 arrow [2].Y = rect.Top + 470 * small_diam / 1000 + 2;
5009                                 arrow [0].Y = arrow [2].Y - tri;
5010                                 arrow [1].Y = arrow [2].Y + tri;
5011                                 arrow [0].X = arrow [1].X = arrow [2].X + tri;
5012                                 break;
5013                                 // Left and Right are not drawn correctly because of libgdiplus problems
5014                                 // once that is solved change it to the code below to match ms
5015 //                      case ScrollButton.Left:
5016 //                              arrow [2].X = rect.Right - (687 * small_diam / 1000 + 1);
5017 //                              arrow [2].Y = (rect.Top + 470 * small_diam / 1000 + 2) - 1;
5018 //                              arrow [1].Y = arrow [2].Y + tri;
5019 //                              arrow [0].Y = arrow [2].Y - tri + 1;
5020 //                              arrow [0].X = arrow [1].X = arrow [2].X + tri;
5021 //                              break;
5022                         case ScrollButton.Right:
5023                                 arrow [2].X = rect.Left + 687 * small_diam / 1000 + 1;
5024                                 arrow [2].Y = rect.Top + 470 * small_diam / 1000 + 2;
5025                                 arrow [0].Y = arrow [2].Y - tri;
5026                                 arrow [1].Y = arrow [2].Y + tri;
5027                                 arrow [0].X = arrow [1].X = arrow [2].X - tri;
5028                                 break;
5029                         }
5030
5031                         /* Draw the arrow */
5032                         if ((state & ButtonState.Inactive)!=0) {
5033                                 dc.FillPolygon (SystemBrushes.ControlLightLight, arrow, FillMode.Winding);
5034
5035                                 for (int i = 0; i < 3; i++) {
5036                                         arrow [i].X--;
5037                                         arrow [i].Y--;
5038                                 }
5039                                 
5040                                 dc.FillPolygon (SystemBrushes.ControlDark, arrow, FillMode.Winding);
5041                         } else {
5042                                 dc.FillPolygon (SystemBrushes.ControlText, arrow, FillMode.Winding);
5043                         }
5044                 }
5045
5046                 public  override void CPDrawSelectionFrame (Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect,
5047                         Color backColor) {
5048
5049                 }
5050
5051
5052                 public override void CPDrawSizeGrip (Graphics dc, Color backColor, Rectangle bounds) {
5053                         
5054                         Point pt = new Point (bounds.Right - 2, bounds.Bottom - 1);
5055
5056                         Pen pen = ResPool.GetPen (ColorControlDark);
5057                         
5058                         dc.DrawLine (pen, pt.X - 11, pt.Y, pt.X, pt.Y - 11);
5059                         dc.DrawLine (pen, pt.X - 10, pt.Y, pt.X, pt.Y - 10);
5060                         
5061                         dc.DrawLine (pen, pt.X - 7, pt.Y, pt.X, pt.Y - 7);
5062                         dc.DrawLine (pen, pt.X - 6, pt.Y, pt.X, pt.Y - 6);
5063                         
5064                         dc.DrawLine (pen, pt.X - 3, pt.Y, pt.X, pt.Y - 3);
5065                         dc.DrawLine (pen, pt.X - 2, pt.Y, pt.X, pt.Y - 2);
5066                         
5067                         pen = ResPool.GetPen (ColorControlLight);
5068                         
5069                         dc.DrawLine (pen, pt.X - 12, pt.Y, pt.X, pt.Y - 12);
5070                         dc.DrawLine (pen, pt.X - 8, pt.Y, pt.X, pt.Y - 8);
5071                         dc.DrawLine (pen, pt.X - 4, pt.Y, pt.X, pt.Y - 4);
5072                         
5073                         dc.DrawLine (ResPool.GetPen (ColorControl), pt.X - 12, pt.Y, pt.X, pt.Y);
5074                 }
5075
5076
5077                 public  override void CPDrawStringDisabled (Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle,
5078                         StringFormat format) {                  
5079
5080                         graphics.DrawString(s, font, ResPool.GetSolidBrush (ColorGrayText), layoutRectangle, format);
5081                         
5082                 }
5083
5084                 private static void DrawBorderInternal(Graphics graphics, int startX, int startY, int endX, int endY,
5085                         int width, Color color, ButtonBorderStyle style, Border3DSide side) {
5086
5087                         Pen     pen=new Pen(color, 1);
5088
5089                         switch(style) {
5090                         case ButtonBorderStyle.Solid: {
5091                                 pen.DashStyle=DashStyle.Solid;
5092                                 break;
5093                         }
5094
5095                         case ButtonBorderStyle.Dashed: {
5096                                 pen.DashStyle=DashStyle.Dash;
5097                                 break;
5098                         }
5099
5100                         case ButtonBorderStyle.Dotted: {
5101                                 pen.DashStyle=DashStyle.Dot;
5102                                 break;
5103                         }
5104
5105                         case ButtonBorderStyle.Inset: {
5106                                 pen.DashStyle=DashStyle.Solid;
5107                                 break;
5108                         }
5109
5110                         case ButtonBorderStyle.Outset: {
5111                                 pen.DashStyle=DashStyle.Solid;
5112                                 break;
5113                         }
5114
5115                         default:
5116                         case ButtonBorderStyle.None: {
5117                                 pen.Dispose();
5118                                 return;
5119                         }
5120                         }
5121
5122
5123                         switch(style) {
5124                         case ButtonBorderStyle.Outset: {
5125                                 Color           colorGrade;
5126                                 int             hue, brightness, saturation;
5127                                 int             brightnessSteps;
5128                                 int             brightnessDownSteps;
5129
5130                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
5131
5132                                 brightnessDownSteps=brightness/width;
5133                                 if (brightness>127) {
5134                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
5135                                 } else {
5136                                         brightnessSteps=(127-brightness)/width;
5137                                 }
5138
5139                                 for (int i=0; i<width; i++) {
5140                                         switch(side) {
5141                                         case Border3DSide.Left: {
5142                                                 pen.Dispose();
5143                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
5144                                                 pen=new Pen(colorGrade, 1);
5145                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
5146                                                 break;
5147                                         }
5148
5149                                         case Border3DSide.Right: {
5150                                                 pen.Dispose();
5151                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
5152                                                 pen=new Pen(colorGrade, 1);
5153                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
5154                                                 break;
5155                                         }
5156
5157                                         case Border3DSide.Top: {
5158                                                 pen.Dispose();
5159                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
5160                                                 pen=new Pen(colorGrade, 1);
5161                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
5162                                                 break;
5163                                         }
5164
5165                                         case Border3DSide.Bottom: {
5166                                                 pen.Dispose();
5167                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
5168                                                 pen=new Pen(colorGrade, 1);
5169                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
5170                                                 break;
5171                                         }
5172                                         }
5173                                 }
5174                                 break;
5175                         }
5176
5177                         case ButtonBorderStyle.Inset: {
5178                                 Color           colorGrade;
5179                                 int             hue, brightness, saturation;
5180                                 int             brightnessSteps;
5181                                 int             brightnessDownSteps;
5182
5183                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
5184
5185                                 brightnessDownSteps=brightness/width;
5186                                 if (brightness>127) {
5187                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
5188                                 } else {
5189                                         brightnessSteps=(127-brightness)/width;
5190                                 }
5191
5192                                 for (int i=0; i<width; i++) {
5193                                         switch(side) {
5194                                         case Border3DSide.Left: {
5195                                                 pen.Dispose();
5196                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
5197                                                 pen=new Pen(colorGrade, 1);
5198                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
5199                                                 break;
5200                                         }
5201
5202                                         case Border3DSide.Right: {
5203                                                 pen.Dispose();
5204                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
5205                                                 pen=new Pen(colorGrade, 1);
5206                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
5207                                                 break;
5208                                         }
5209
5210                                         case Border3DSide.Top: {
5211                                                 pen.Dispose();
5212                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
5213                                                 pen=new Pen(colorGrade, 1);
5214                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
5215                                                 break;
5216                                         }
5217
5218                                         case Border3DSide.Bottom: {
5219                                                 pen.Dispose();
5220                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
5221                                                 pen=new Pen(colorGrade, 1);
5222                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
5223                                                 break;
5224                                         }
5225                                         }
5226                                 }
5227                                 break;
5228                         }
5229
5230                                 /*
5231                                         I decided to have the for-loop duplicated for speed reasons;
5232                                         that way we only have to switch once (as opposed to have the
5233                                         for-loop around the switch)
5234                                 */
5235                         default: {
5236                                 switch(side) {
5237                                 case Border3DSide.Left: {
5238                                         for (int i=0; i<width; i++) {
5239                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
5240                                         }
5241                                         break;
5242                                 }
5243
5244                                 case Border3DSide.Right: {
5245                                         for (int i=0; i<width; i++) {
5246                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
5247                                         }
5248                                         break;
5249                                 }
5250
5251                                 case Border3DSide.Top: {
5252                                         for (int i=0; i<width; i++) {
5253                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
5254                                         }
5255                                         break;
5256                                 }
5257
5258                                 case Border3DSide.Bottom: {
5259                                         for (int i=0; i<width; i++) {
5260                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
5261                                         }
5262                                         break;
5263                                 }
5264                                 }
5265                                 break;
5266                         }
5267                         }
5268                         pen.Dispose();
5269                 }
5270
5271                 /*
5272                         This function actually draws the various caption elements.
5273                         This way we can scale them nicely, no matter what size, and they
5274                         still look like MS's scaled caption buttons. (as opposed to scaling a bitmap)
5275                 */
5276
5277                 private void DrawCaptionHelper(Graphics graphics, Color color, Pen pen, int lineWidth, int shift, Rectangle captionRect, CaptionButton button) {
5278                         switch(button) {
5279                         case CaptionButton.Close: {
5280                                 pen.StartCap=LineCap.Triangle;
5281                                 pen.EndCap=LineCap.Triangle;
5282                                 if (lineWidth<2) {
5283                                         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);
5284                                         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);
5285                                 }
5286
5287                                 graphics.DrawLine(pen, captionRect.Left+2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Right-2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
5288                                 graphics.DrawLine(pen, captionRect.Right-2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Left+2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
5289                                 return;
5290                         }
5291
5292                         case CaptionButton.Help: {
5293                                 StringFormat    sf = new StringFormat();                                
5294                                 Font                            font = new Font("Microsoft Sans Serif", captionRect.Height, FontStyle.Bold, GraphicsUnit.Pixel);
5295
5296                                 sf.Alignment=StringAlignment.Center;
5297                                 sf.LineAlignment=StringAlignment.Center;
5298
5299
5300                                 graphics.DrawString("?", font, ResPool.GetSolidBrush (color), captionRect.X+captionRect.Width/2+shift, captionRect.Y+captionRect.Height/2+shift+lineWidth/2, sf);
5301
5302                                 sf.Dispose();                           
5303                                 font.Dispose();
5304
5305                                 return;
5306                         }
5307
5308                         case CaptionButton.Maximize: {
5309                                 /* Top 'caption bar' line */
5310                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
5311                                         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);
5312                                 }
5313
5314                                 /* Left side line */
5315                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5316                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+2*lineWidth+shift, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
5317                                 }
5318
5319                                 /* Right side line */
5320                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5321                                         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);
5322                                 }
5323
5324                                 /* Bottom line */
5325                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5326                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
5327                                 }
5328                                 return;
5329                         }
5330
5331                         case CaptionButton.Minimize: {
5332                                 /* Bottom line */
5333                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
5334                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth+shift, captionRect.Bottom-lineWidth+shift-i);
5335                                 }
5336                                 return;
5337                         }
5338
5339                         case CaptionButton.Restore: {
5340                                 /** First 'window' **/
5341                                 /* Top 'caption bar' line */
5342                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
5343                                         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);
5344                                 }
5345
5346                                 /* Left side line */
5347                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5348                                         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);
5349                                 }
5350
5351                                 /* Right side line */
5352                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5353                                         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);
5354                                 }
5355
5356                                 /* Bottom line */
5357                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5358                                         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);
5359                                 }
5360
5361                                 /** Second 'window' **/
5362                                 /* Top 'caption bar' line */
5363                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
5364                                         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);
5365                                 }
5366
5367                                 /* Left side line */
5368                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5369                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+4*lineWidth+shift+1, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
5370                                 }
5371
5372                                 /* Right side line */
5373                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5374                                         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);
5375                                 }
5376
5377                                 /* Bottom line */
5378                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
5379                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
5380                                 }
5381
5382                                 return;
5383                         }
5384
5385                         }
5386                 }
5387
5388                 [MonoTODO("Finish drawing code for Caption, Menu and Scroll")]
5389                 private void DrawFrameControl(Graphics graphics, Rectangle rectangle, DrawFrameControlTypes Type, DrawFrameControlStates State) {
5390                         // make a rectange to trace around border of the button
5391                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
5392                         switch(Type) {
5393                         case DrawFrameControlTypes.Button: {
5394
5395                                 if ((State & DrawFrameControlStates.ButtonPush)!=0) {
5396 // JBA 31 oct 2004 - I don't think that button style should be rendered like this
5397 //                                      /* Goes first, affects the background */
5398 //                                      if ((State & DrawFrameControlStates.Checked)!=0) {
5399 //                                              HatchBrush      hatchBrush=new HatchBrush(HatchStyle.Percent50, ColorControlLightLight, ColorControlLight);
5400 //                                              graphics.FillRectangle(hatchBrush,rectangle);
5401 //                                              hatchBrush.Dispose();
5402 //                                      }
5403
5404                                         if ((State & DrawFrameControlStates.Pushed)!=0 || (State & DrawFrameControlStates.Checked)!=0) {
5405                                                 graphics.DrawRectangle (ResPool.GetPen (ControlPaint.Dark (ColorControl)), trace_rectangle);
5406                                         } else if ((State & DrawFrameControlStates.Flat)!=0) {
5407                                                 ControlPaint.DrawBorder(graphics, rectangle, ColorControlDark, ButtonBorderStyle.Solid);
5408                                         } else 
5409                                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);                                       
5410                                 } else if ((State & DrawFrameControlStates.ButtonRadio)!=0) {
5411                                         Pen                     penFatDark      = new Pen(ColorControlDark, 1);
5412                                         Pen                     penFatLight     = new Pen(ColorControlLightLight, 1);
5413                                         int                     lineWidth;
5414
5415                                         graphics.FillPie (ResPool.GetSolidBrush (this.ColorWindow), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
5416
5417                                         graphics.DrawArc(penFatDark, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 135, 180);
5418                                         graphics.DrawArc(penFatLight, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 315, 180);
5419
5420                                         graphics.DrawArc(SystemPens.ControlDark, rectangle, 135, 180);
5421                                         graphics.DrawArc(SystemPens.ControlLightLight, rectangle, 315, 180);
5422
5423                                         lineWidth=Math.Max(1, Math.Min(rectangle.Width, rectangle.Height)/3);
5424
5425                                         if ((State & DrawFrameControlStates.Checked)!=0) {
5426                                                 SolidBrush      buttonBrush;
5427
5428                                                 if ((State & DrawFrameControlStates.Inactive)!=0) {
5429                                                         buttonBrush=(SolidBrush)SystemBrushes.ControlDark;
5430                                                 } else {
5431                                                         buttonBrush=(SolidBrush)SystemBrushes.ControlText;
5432                                                 }
5433                                                 graphics.FillPie(buttonBrush, rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2, 0, 359);
5434                                         }
5435                                         penFatDark.Dispose();
5436                                         penFatLight.Dispose();
5437                                 } else if ((State & DrawFrameControlStates.ButtonRadioImage)!=0) {
5438                                         throw new NotImplementedException () ;
5439                                 } else if ((State & DrawFrameControlStates.ButtonRadioMask)!=0) {
5440                                         throw new NotImplementedException ();
5441                                 } else {        /* Must be Checkbox */
5442                                         Pen                     pen;
5443                                         int                     lineWidth;
5444                                         Rectangle       rect;
5445                                         int                     Scale;
5446
5447                                         /* Goes first, affects the background */
5448                                         if ((State & DrawFrameControlStates.Pushed)!=0 ||
5449                                                 (State & DrawFrameControlStates.Inactive)!=0) {
5450                                                 graphics.FillRectangle(SystemBrushes.Control, rectangle);
5451                                         } else {
5452                                                 graphics.FillRectangle(SystemBrushes.Window, rectangle);
5453                                         }
5454
5455                                         /* Draw the sunken frame */
5456                                         if ((State & DrawFrameControlStates.Flat)!=0) {
5457                                                 ControlPaint.DrawBorder(graphics, rectangle, ColorControlDark, ButtonBorderStyle.Solid);
5458                                         } else {
5459                                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorControl);
5460                                         }
5461
5462                                         /* Make sure we've got at least a line width of 1 */
5463                                         lineWidth=Math.Max(3, rectangle.Width/6);
5464                                         Scale=Math.Max(1, rectangle.Width/12);
5465
5466                                         // define a rectangle inside the border area
5467                                         rect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-4, rectangle.Height-4);
5468                                         if ((State & DrawFrameControlStates.Inactive)!=0) {
5469                                                 pen=SystemPens.ControlDark;
5470                                         } else {
5471                                                 pen=SystemPens.ControlText;
5472                                         }
5473
5474                                         if ((State & DrawFrameControlStates.Checked)!=0) {
5475                                                 /* Need to draw a check-mark */
5476                                                 for (int i=0; i<lineWidth; i++) {
5477                                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
5478                                                         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);
5479                                                 }
5480
5481                                         }
5482                                 }
5483                                 return;
5484                         }
5485
5486                         case DrawFrameControlTypes.Caption: {
5487                                 // FIXME:
5488                                 break;
5489                         }
5490
5491                         case DrawFrameControlTypes.Menu: {
5492                                 // FIXME:
5493                                 break;
5494                         }
5495
5496                         case DrawFrameControlTypes.Scroll: {
5497                                 // FIXME:
5498                                 break;
5499                         }
5500                         }
5501                 }
5502
5503                 /* Generic scroll button */
5504                 public void DrawScrollButtonPrimitive (Graphics dc, Rectangle area, ButtonState state) {
5505                         if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
5506                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), area.X + 1,
5507                                         area.Y + 1, area.Width - 2 , area.Height - 2);
5508
5509                                 dc.DrawRectangle (ResPool.GetPen (ColorControlDark), area.X,
5510                                         area.Y, area.Width, area.Height);
5511
5512                                 return;
5513                         }                       
5514         
5515                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), area.X, area.Y, area.Width, 1);
5516                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), area.X, area.Y, 1, area.Height);
5517
5518                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlLight), area.X + 1, area.Y + 1, area.Width - 1, 1);
5519                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlLight), area.X + 1, area.Y + 2, 1,
5520                                 area.Height - 4);
5521
5522                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDark), area.X + 1, area.Y + area.Height - 2,
5523                                 area.Width - 2, 1);
5524
5525                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDarkDark), area.X, area.Y + area.Height -1,
5526                                 area.Width , 1);
5527
5528                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDark), area.X + area.Width - 2,
5529                                 area.Y + 1, 1, area.Height -3);
5530
5531                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControlDarkDark), area.X + area.Width -1,
5532                                 area.Y, 1, area.Height - 1);
5533
5534                         dc.FillRectangle (ResPool.GetSolidBrush (ColorControl), area.X + 2,
5535                                 area.Y + 2, area.Width - 4, area.Height - 4);
5536                         
5537                 }
5538                 
5539                 public override void CPDrawBorderStyle (Graphics dc, Rectangle area, BorderStyle border_style) {
5540                         switch (border_style){
5541                         case BorderStyle.Fixed3D:
5542                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), area.X, area.Y, area.X +area.Width, area.Y);
5543                                 dc.DrawLine (ResPool.GetPen (ColorControlDark), area.X, area.Y, area.X, area.Y + area.Height);
5544                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), area.X , area.Y + area.Height - 1, area.X + area.Width , 
5545                                         area.Y + area.Height - 1);
5546                                 dc.DrawLine (ResPool.GetPen (ColorControlLight), area.X + area.Width -1 , area.Y, area.X + area.Width -1, 
5547                                         area.Y + area.Height);
5548
5549                                 dc.DrawLine (ResPool.GetPen (ColorActiveBorder), area.X + 1, area.Bottom - 2, area.Right - 2, area.Bottom - 2);
5550                                 dc.DrawLine (ResPool.GetPen (ColorActiveBorder), area.Right - 2, area.Top + 1, area.Right - 2, area.Bottom - 2);
5551                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), area.X + 1, area.Top + 1, area.X + 1, area.Bottom - 3);
5552                                 dc.DrawLine (ResPool.GetPen (ColorControlDarkDark), area.X + 1, area.Top + 1, area.Right - 3, area.Top + 1);
5553                                 break;
5554                         case BorderStyle.FixedSingle:
5555                                 dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), area.X, area.Y, area.Width - 1, area.Height - 1);
5556                                 break;
5557                         case BorderStyle.None:
5558                         default:
5559                                 break;
5560                         }
5561                         
5562                 }
5563                 #endregion      // ControlPaint
5564
5565
5566         } //class
5567 }