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