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