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