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