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