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