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