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