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