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