* X11Keyboard.cs: Detect and use the num lock mask.
[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 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 //
27 //
28 //
29 // $Revision: 1.65 $
30 // $Modtime: $
31 // $Log: ThemeWin32Classic.cs,v $
32 // Revision 1.65  2004/11/10 20:10:38  jackson
33 //      * ThemeWin32Classic.cs: Do not draw the selected tab if its not in
34 //      the clip area.
35 //
36 // Revision 1.64  2004/11/10 19:22:26  jackson
37 //      * ThemeWin32Classic.cs: Don't bother drawing tabs outside of the
38 //      clip area.
39 //
40 // Revision 1.63  2004/11/10 01:04:28  jackson
41 //      * TabControl.cs (CalcXPos): New helper method so we can determine
42 //      the proper place to start drawing vertical tabs.
43 //      * ThemeWin32Classic.cs (DrawTab): Draw right aligned tabs.
44 //
45 // Revision 1.62  2004/11/09 21:44:54  jackson
46 //      * TabControl.cs: Calculate sizing and rects for left aligned tabs.
47 //      * ThemeWin32Classic.cs (GetTabControl*ScrollRect): Only handle Top
48 //      and Bottom, left and right are illegal values for this and
49 //      multiline is enabled when the alignment is set to left or right.
50 //      (DrawTab): Each alignment block should draw the text itself now
51 //      because Left requires special love. Also add rendering for Left
52 //      aligned tabs.
53 //
54 // Revision 1.61  2004/11/09 11:06:21  jba
55 // - (DrawButtonBase): Fix verticle text rect clipping in windows
56 // - (DrawCheckBox): Fix CheckAlign.TopCenter and CheckAlign.BottomCenter
57 //   rendering and incorrect text rect clipping
58 // - (DrawRadioButton): Fix CheckAlign.TopCenter and CheckAlign.BottomCenter
59 //   rendering and incorrect text rect clipping
60 //
61 // Revision 1.60  2004/11/09 03:12:00  jackson
62 //      * ThemeWin32Classic.cs (DrawTabControl): Render tabs from top to
63 //      bottom when they are bottom aligned so the bottoms of the tabs get
64 //      displayed.
65 //      * TabControl.cs (DropRow): Move rows up instead of down when the
66 //      tab control is bottom aligned.
67 //
68 // Revision 1.59  2004/11/08 20:40:08  jackson
69 // Render the little scrolling jimmi in the correct location with bottom aligned tabs
70 //
71 // Revision 1.58  2004/11/08 14:15:00  jordi
72 // fixes vertical scrollbar and removes dead code
73 //
74 // Revision 1.57  2004/11/05 05:47:34  jba
75 // - Fix Button rendering for FlatStyle = Flat or Popup
76 // - Fix RadioButton and CheckBox rendering when Appearance = Button (normal and
77 //   flatstyle).
78 // - Correct outer rectangle color when drawing focus rectangle
79 // - Adjust button bounds to be 1 px smaller when focused
80 // - Make button not draw sunken 3d border when pushed (windows compat)
81 // - Fix CPDrawBorder3D to not make bottom right hand corner rounded
82 // - Offset the text in RadioButton and Checkbox when being rendered as a button.
83 // - Hover and Click behaviour for Colored FlatStyle.Flat and Popup radiobuttons
84 // - Fixed disabled rendering for colored flatstyle radiobuttons (both)
85 // - Fixed disabled text rendering for normally rendered radiobuttons
86 //
87 // Revision 1.56  2004/11/04 11:26:09  ravindra
88 //      - Changed default ListView values signatures (prefixed all with ListView).
89 //      - Fixed default size values for VScrollBar and HScrollBar.
90 //      - Fixed DrawListViewItem method.
91 //
92 // Revision 1.55  2004/11/03 18:52:14  jackson
93 // Initial implementation of the scrolly widgerywoo
94 //
95 // Revision 1.54  2004/11/02 20:40:54  jackson
96 // Move the row with the selected tab to the bottom
97 //
98 // Revision 1.53  2004/11/02 02:47:55  jackson
99 // New rendering and sizing code for tab controls
100 //
101 // Revision 1.52  2004/10/30 10:23:02  ravindra
102 // Drawing ListView and some default values.
103 //
104 // Revision 1.51  2004/10/26 09:55:48  ravindra
105 // Some formatting for my last checkins.
106 //
107 // Revision 1.50  2004/10/26 09:36:32  ravindra
108 // Implemented DetailView drawing for ListView control and default values.
109 //
110 // Revision 1.49  2004/10/18 04:49:25  pbartok
111 // - Added ToolTip drawing code
112 //
113 // Revision 1.48  2004/10/15 15:08:49  ravindra
114 // Added ColumnHeaderHeight property in Theme for ListView.
115 //
116 // Revision 1.47  2004/10/13 15:06:37  pbartok
117 // - Path from John BouAntoun:
118 //   * Fix check rendering (centre correctly for normal style, offset
119 //     correctly for FlatStyle).
120 //   * Fix border color usage (use backcolor) for FlatStyle.Popup
121 //   * Use checkbox.Capture instead of checkbox.is_pressed when rendering
122 //     flatstyle states.
123 //
124 // Revision 1.46  2004/10/13 03:48:15  pbartok
125 // - Removed all occurences of SystemColors and replaced them with the
126 //   matching theme color
127 //
128 // Revision 1.45  2004/10/13 03:41:45  pbartok
129 // - From John BouAntoun: Added an overload to CPDrawBorder3D to allow him
130 //   using the function for flatstyle drawing
131 // - Changed functions to use the new version of CPDrawBorder3D
132 //
133 // Revision 1.44  2004/10/13 02:45:21  pbartok
134 // - Fixes from John BouAntoun: now handles forecolors and backcolors for
135 //   flatstyle rendered controls much better; It also fixes normal checkbox
136 //   rendering when pushed or disabled.
137 //
138 // Revision 1.43  2004/10/07 14:56:51  jordi
139 // Removes deletion of cached brushes
140 //
141 // Revision 1.42  2004/10/06 09:59:05  jordi
142 // removes warnings from compilation
143 //
144 // Revision 1.41  2004/10/05 16:15:30  jackson
145 // Improve rendering of the radio button patch by John BouAntoun
146 //
147 // Revision 1.40  2004/10/05 09:04:31  ravindra
148 //      - Added DrawListView method and ListViewDefaultSize property.
149 //      - Changed ControlPaint method calls to CPDrawXXX wherever possible.
150 //      - Changed DOS style CRLF to Unix format (dos2unix).
151 //
152 // Revision 1.39  2004/10/04 07:09:37  jordi
153 // fixes right button position causing right button not showing on horizontal scrollbars
154 //
155 // Revision 1.38  2004/09/28 18:44:25  pbartok
156 // - Streamlined Theme interfaces:
157 //   * Each DrawXXX method for a control now is passed the object for the
158 //     control to be drawn in order to allow accessing any state the theme
159 //     might require
160 //
161 //   * ControlPaint methods for the theme now have a CP prefix to avoid
162 //     name clashes with the Draw methods for controls
163 //
164 //   * Every control now retrieves it's DefaultSize from the current theme
165 //
166 // Revision 1.37  2004/09/09 08:28:11  pbartok
167 // - Improve disabled string look
168 //
169 // Revision 1.36  2004/09/09 03:03:49  ravindra
170 // PictureBox would not draw a null image to avoid crash.
171 //
172 // Revision 1.35  2004/09/07 17:12:26  jordi
173 // GroupBox control
174 //
175 // Revision 1.34  2004/09/07 09:40:15  jordi
176 // LinkLabel fixes, methods, multiple links
177 //
178 // Revision 1.33  2004/09/05 08:03:51  jordi
179 // fixes bugs, adds flashing on certain situations
180 //
181 // Revision 1.32  2004/09/02 16:32:54  jordi
182 // implements resource pool for pens, brushes, and hatchbruses
183 //
184 // Revision 1.31  2004/08/25 20:04:40  ravindra
185 // Added the missing divider code and grip for ToolBar Control.
186 //
187 // Revision 1.30  2004/08/25 18:29:14  jordi
188 // new methods, properties, and fixes for progressbar
189 //
190 // Revision 1.29  2004/08/25 00:43:13  ravindra
191 // Fixed wrapping related issues in ToolBar control.
192 //
193 // Revision 1.28  2004/08/24 18:37:02  jordi
194 // fixes formmating, methods signature, and adds missing events
195 //
196 // Revision 1.27  2004/08/24 16:16:46  jackson
197 // Handle drawing picture boxes in the theme now. Draw picture box borders and obey sizing modes
198 //
199 // Revision 1.26  2004/08/21 01:52:08  ravindra
200 // Improvments in mouse event handling in the ToolBar control.
201 //
202 // Revision 1.25  2004/08/20 00:12:51  jordi
203 // fixes methods signature
204 //
205 // Revision 1.24  2004/08/19 22:25:31  jordi
206 // theme enhancaments
207 //
208 // Revision 1.23  2004/08/18 19:16:53  jordi
209 // Move colors to a table
210 //
211 // Revision 1.22  2004/08/17 19:29:11  jackson
212 // Don't use KnownColor to create colours. It has a large startup time.
213 //
214 // Revision 1.21  2004/08/15 23:20:54  ravindra
215 // Changes to Theme for ToolBar control and also dos2unix format.
216 //
217 // Revision 1.20  2004/08/13 21:22:18  jordi
218 // removes redundant code and fixes issues with tickposition
219 //
220 // Revision 1.19  2004/08/12 20:29:01  jordi
221 // Trackbar enhancement, fix mouse problems, highli thumb, etc
222 //
223 // Revision 1.18  2004/08/12 18:54:37  jackson
224 // Handle owner draw status bars
225 //
226 // Revision 1.17  2004/08/11 01:31:35  jackson
227 // Create Brushes as little as possible
228 //
229 // Revision 1.16  2004/08/10 19:21:27  jordi
230 // scrollbar enhancements and standarize on win colors defaults
231 //
232 // Revision 1.15  2004/08/10 18:52:30  jackson
233 // Implement DrawItem functionality
234 //
235 // Revision 1.14  2004/08/09 21:34:54  jackson
236 // Add support for drawing status bar and get status bar item sizes
237 //
238 // Revision 1.13  2004/08/09 21:21:49  jackson
239 // Use known colors for default control colours
240 //
241 // Revision 1.12  2004/08/09 21:12:15  jackson
242 // Make the default font static, it is static in control so this doesn't change functionality and creating fonts is sloooooow.
243 //
244 // Revision 1.11  2004/08/09 17:31:13  jackson
245 // New names for control properties
246 //
247 // Revision 1.10  2004/08/09 17:00:00  jackson
248 // Add default window color properties
249 //
250 // Revision 1.9  2004/08/09 16:17:19  jackson
251 // Use correct default back color
252 //
253 // Revision 1.8  2004/08/09 15:53:12  jackson
254 // Themes now handle default control properties so coloring will be consistent
255 //
256 // Revision 1.7  2004/08/08 22:54:21  jordi
257 // Label BorderStyles
258 //
259 // Revision 1.6  2004/08/08 18:09:53  jackson
260 // Add pen_buttonface
261 //
262 // Revision 1.5  2004/08/08 17:34:28  jordi
263 // Use Windows Standard Colours
264 //
265 // Revision 1.4  2004/08/07 23:31:15  jordi
266 // fixes label bug and draw method name
267 //
268 // Revision 1.3  2004/08/07 19:05:44  jordi
269 // Theme colour support and GetSysColor defines
270 //
271 // Revision 1.2  2004/08/07 00:01:39  pbartok
272 // - Fixed some rounding issues with float/int
273 //
274 // Revision 1.1  2004/07/26 17:42:03  jordi
275 // Theme support
276 //
277 //
278
279 using System.Drawing;
280 using System.Drawing.Drawing2D;
281 using System.Drawing.Imaging;
282
283 namespace System.Windows.Forms
284 {
285
286         internal class ThemeWin32Classic : Theme
287         {               
288
289                 /* Default colors for Win32 classic theme */
290                 uint [] theme_colors = {                                                        /* AARRGGBB */
291                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_SCROLLBAR,                   0xffc0c0c0,
292                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_BACKGROUND,                  0xff008080,
293                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_ACTIVECAPTION,               0xff000080,
294                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_INACTIVECAPTION,             0xff808080,
295                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_MENU,                        0xffc0c0c0,
296                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_WINDOW,                      0xffffffff,
297                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_WINDOWFRAME,                 0xff000000,
298                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_MENUTEXT,                    0xff000000,
299                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_WINDOWTEXT,                  0xff000000,
300                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_CAPTIONTEXT,                 0xffffffff,
301                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_ACTIVEBORDER,                0xffc0c0c0,
302                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_INACTIVEBORDER,              0xffc0c0c0,
303                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_APPWORKSPACE,                0xff808080,
304                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_HIGHLIGHT,                   0xff000080,
305                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_HIGHLIGHTTEXT,               0xffffffff,
306                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_BTNFACE,                     0xffc0c0c0,
307                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_BTNSHADOW,                   0xff808080,
308                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_GRAYTEXT,                    0xff808080,
309                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_BTNTEXT,                     0xff000000,
310                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_INACTIVECAPTIONTEXT,         0xffc0c0c0,
311                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_BTNHIGHLIGHT,                0xffffffff,
312                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_3DDKSHADOW,                  0xff000000,                     
313                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_3DLIGHT,                     0xffe0e0e0,
314                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_INFOTEXT,                    0xff000000,
315                         (uint) XplatUIWin32.GetSysColorIndex.COLOR_INFOBK,                      0xffffffff,
316                   
317                 };              
318                                 
319                 /* Hardcoded colour values not exposed in the API constants in all configurations */
320                 static readonly Color arrow_color = Color.Black;
321                 static readonly Color pen_ticks_color = Color.Black;
322                 static readonly Color progressbarblock_color = Color.FromArgb (255, 0, 0, 128);
323
324                 #region Principal Theme Methods
325                 public ThemeWin32Classic ()
326                 {                       
327                         /* Init Default colour array*/
328                         syscolors =  Array.CreateInstance (typeof (Color), (uint) XplatUIWin32.GetSysColorIndex.COLOR_MAXVALUE+1);
329                         
330                         for (int i = 0; i < theme_colors.Length; i +=2) 
331                                 syscolors.SetValue (Color.FromArgb ((int)theme_colors[i+1]), (int) theme_colors[i]);                    
332
333                         defaultWindowBackColor = SystemColors.Window;
334                         defaultWindowForeColor = ColorButtonText;
335                         default_font =  new Font (FontFamily.GenericSansSerif, 8.25f);
336                 }       
337
338                 public override bool DoubleBufferingSupported {
339                         get {return true; }
340                 }
341                 #endregion      // Principal Theme Methods
342
343                 #region Internal Methods
344                 protected SolidBrush GetControlBackBrush (Color c) {
345                         if (c == DefaultControlBackColor)
346                                 return ResPool.GetSolidBrush (ColorButtonFace);
347                         return new SolidBrush (c);
348                 }
349
350                 protected SolidBrush GetControlForeBrush (Color c) {
351                         if (c == DefaultControlForeColor)
352                                 return ResPool.GetSolidBrush (ColorButtonText);
353                         return new SolidBrush (c);
354                 }
355                 #endregion      // Internal Methods
356
357                 #region OwnerDraw Support
358                 public  override void DrawOwnerDrawBackground (DrawItemEventArgs e)
359                 {
360                         if (e.State == DrawItemState.Selected) {
361                                 e.Graphics.FillRectangle (SystemBrushes.Highlight, e.Bounds);
362                                 return;
363                         }
364
365                         e.Graphics.FillRectangle (GetControlBackBrush (e.BackColor), e.Bounds);
366                 }
367
368                 public  override void DrawOwnerDrawFocusRectangle (DrawItemEventArgs e)
369                 {
370                         if (e.State == DrawItemState.Focus)
371                                 CPDrawFocusRectangle (e.Graphics, e.Bounds, e.ForeColor, e.BackColor);
372                 }
373                 #endregion      // OwnerDraw Support
374
375                 #region ButtonBase
376                 public override void DrawButtonBase(Graphics dc, Rectangle clip_area, ButtonBase button) {
377                         int             width;
378                         int             height;
379                         Rectangle buttonRectangle;
380                         Rectangle borderRectangle;
381
382                         width = button.ClientSize.Width;
383                         height = button.ClientSize.Height;
384                         
385                         dc.FillRectangle(ResPool.GetSolidBrush (button.BackColor), button.ClientRectangle);                     
386                         
387                         // set up the button rectangle
388                         buttonRectangle = button.ClientRectangle;
389                         if (button.has_focus) {
390                                 // shrink the rectangle for the normal button drawing inside the focus rectangle
391                                 borderRectangle = Rectangle.Inflate(buttonRectangle, -1, -1);
392                         } else {
393                                 borderRectangle = buttonRectangle;
394                         }
395
396                         if (button.FlatStyle == FlatStyle.Flat || button.FlatStyle == FlatStyle.Popup) {
397                                 DrawFlatStyleButton (dc, borderRectangle, button);
398                         } else {
399                                 CPDrawButton(dc, borderRectangle, button.ButtonState);                          
400                         }
401                         
402                         // First, draw the image
403                         if ((button.image != null) || (button.image_list != null)) {
404                                 // Need to draw a picture
405                                 Image   i;
406                                 int     image_x;
407                                 int     image_y;
408                                 int     image_width;
409                                 int     image_height;
410
411                                 if (button.ImageIndex!=-1) {    // We use ImageIndex instead of image_index since it will return -1 if image_list is null
412                                         i = button.image_list.Images[button.image_index];
413                                 } else {
414                                         i = button.image;
415                                 }
416
417                                 image_width = button.image.Width;
418                                 image_height = button.image.Height;
419
420                                 switch(button.image_alignment) {
421                                 case ContentAlignment.TopLeft: {
422                                         image_x=0;
423                                         image_y=0;
424                                         break;
425                                 }
426
427                                 case ContentAlignment.TopCenter: {
428                                         image_x=(width-image_width)/2;
429                                         image_y=0;
430                                         break;
431                                 }
432
433                                 case ContentAlignment.TopRight: {
434                                         image_x=width-image_width;
435                                         image_y=0;
436                                         break;
437                                 }
438
439                                 case ContentAlignment.MiddleLeft: {
440                                         image_x=0;
441                                         image_y=(height-image_height)/2;
442                                         break;
443                                 }
444
445                                 case ContentAlignment.MiddleCenter: {
446                                         image_x=(width-image_width)/2;
447                                         image_y=(height-image_height)/2;
448                                         break;
449                                 }
450
451                                 case ContentAlignment.MiddleRight: {
452                                         image_x=width-image_width;
453                                         image_y=(height-image_height)/2;
454                                         break;
455                                 }
456
457                                 case ContentAlignment.BottomLeft: {
458                                         image_x=0;
459                                         image_y=height-image_height;
460                                         break;
461                                 }
462
463                                 case ContentAlignment.BottomCenter: {
464                                         image_x=(width-image_width)/2;
465                                         image_y=height-image_height;
466                                         break;
467                                 }
468
469                                 case ContentAlignment.BottomRight: {
470                                         image_x=width-image_width;
471                                         image_y=height-image_height;
472                                         break;
473                                 }
474
475                                 default: {
476                                         image_x=0;
477                                         image_y=0;
478                                         break;
479                                 }
480                                 }
481
482                                 if (button.is_pressed) {
483                                         image_x+=1;
484                                         image_y+=1;
485                                 }
486
487                                 if (button.is_enabled) {
488                                         dc.DrawImage(i, image_x, image_y); 
489                                 } else {
490                                         CPDrawImageDisabled(dc, i, image_x, image_y, ColorButtonFace);
491                                 }
492                         }
493                         
494                         // Draw the focus rectangle
495                         if (button.has_focus) {
496                                 if (button.FlatStyle == FlatStyle.Flat || button.FlatStyle == FlatStyle.Popup) {
497                                         DrawFlatStyleFocusRectangle (dc, button.ClientRectangle, button, button.ForeColor, button.BackColor);
498                                 } else { 
499                                         CPDrawFocusRectangle(dc, button.ClientRectangle, button.ForeColor, button.BackColor);
500                                 }
501                         }
502                         
503                         // Now the text
504                         if (button.text != null && button.text != String.Empty) {
505                                 Rectangle text_rect = Rectangle.Inflate(buttonRectangle, -4, -4);
506
507                                 if (button.is_pressed) {
508                                         text_rect.X++;
509                                         text_rect.Y++;
510                                 }
511
512                                 if (button.is_enabled) {                                        
513                                         dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (button.ForeColor), text_rect, button.text_format);
514                                         
515                                 } else {
516                                         if (button.FlatStyle == FlatStyle.Flat || button.FlatStyle == FlatStyle.Popup) {
517                                                 dc.DrawString(button.text, button.Font, ResPool.GetSolidBrush (ControlPaint.DarkDark (this.ColorButtonFace)), text_rect, button.text_format);
518                                         } else {
519                                                 CPDrawStringDisabled(dc, button.text, button.Font, ColorButtonText, text_rect, button.text_format);
520                                         }
521                                 }
522                         }
523                 }
524                 
525                 // draw the flat style part of the rectangle
526                 public void DrawFlatStyleButton (Graphics graphics, Rectangle rectangle, ButtonBase button) {
527                         Color rect_back_color = button.BackColor;
528                         Color rect_fore_color = button.ForeColor;
529                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
530                                 
531                         if (button.Enabled) {
532                                 if (button.Capture || button.is_entered) {
533                                         if (button.FlatStyle == FlatStyle.Flat) {
534                                                 // fill the rectangle
535                                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
536                                                 
537                                                 // now draw the outer border
538                                                 if (button.Capture && button.is_entered) {
539                                                         rect_back_color = ControlPaint.LightLight (rect_back_color);
540                                                 } else {
541                                                         rect_back_color = ControlPaint.Light (rect_back_color);
542                                                 }
543                                                 
544                                                 // draw rectangle and fill it
545                                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
546                                                 graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
547                                         } else {
548                                                 // else it must be a popup button
549                                                 
550                                                 if (button.Capture && button.is_entered) {
551                                                         graphics.DrawRectangle(ResPool.GetPen (this.ColorButtonText), trace_rectangle);
552                                                 } else {
553                                                         // draw a 3d border
554                                                         CPDrawBorder3D (graphics, rectangle, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top, button.BackColor); 
555                                                         graphics.DrawLine ( ResPool.GetPen (this.ColorButtonText), trace_rectangle.X, trace_rectangle.Bottom, trace_rectangle.Right, trace_rectangle.Bottom);
556                                                         graphics.DrawLine ( ResPool.GetPen (this.ColorButtonText), trace_rectangle.Right, trace_rectangle.Y, trace_rectangle.Right, trace_rectangle.Bottom);
557                                                 }
558                                         }
559                                         
560                                         // TODO: draw inner focus rectangle
561                                         
562                                 } else {
563                                         // popup has a ButtonColorText forecolor, not a button.ForeCOlor
564                                         if (button.FlatStyle == FlatStyle.Popup) {
565                                                 rect_fore_color = this.ColorButtonText;
566                                         }
567                                         
568                                         // fill then draw outer rect
569                                         graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
570                                         graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
571                                 }
572                                 
573                                 // finally some small tweaks to render radiobutton and checkbox
574                                 CheckBox checkbox = button as CheckBox;
575                                 RadioButton radiobutton = button as RadioButton;
576                                 if ((checkbox != null && checkbox.Checked) ||
577                                         (radiobutton != null && radiobutton.Checked)) {
578                                         if (button.FlatStyle == FlatStyle.Flat && button.is_entered && !button.Capture) {
579                                                 // render the hover for flat flatstyle and cheked
580                                                 graphics.DrawRectangle(ResPool.GetPen (this.ColorButtonText), trace_rectangle);
581                                         } else if (!button.is_entered && !button.Capture) {
582                                                 // render the checked state for popup when unhovered
583                                                 CPDrawBorder3D (graphics, rectangle, Border3DStyle.SunkenInner, Border3DSide.Right | Border3DSide.Bottom, button.BackColor); 
584                                         }
585                                 }
586                         } else {
587                                 // rendering checkbox or radio button style buttons
588                                 CheckBox checkbox = button as CheckBox;
589                                 RadioButton radiobutton = button as RadioButton;
590                                 bool draw_popup_checked = false;
591                                 
592                                 if (button.FlatStyle == FlatStyle.Popup) {
593                                         rect_fore_color = this.ColorButtonText;
594                                 
595                                         // see if we should draw a disabled checked popup button
596                                         draw_popup_checked = ((checkbox != null && checkbox.Checked) ||
597                                                 (radiobutton != null && radiobutton.Checked));
598                                 }
599                                 
600                                 graphics.FillRectangle (ResPool.GetSolidBrush (rect_back_color), rectangle);
601                                 graphics.DrawRectangle(ResPool.GetPen (rect_fore_color), trace_rectangle);
602                                 
603                                 // finally draw the flatstyle checked effect if need
604                                 if (draw_popup_checked) {
605                                         // render the checked state for popup when unhovered
606                                         CPDrawBorder3D (graphics, rectangle, Border3DStyle.SunkenInner, Border3DSide.Right | Border3DSide.Bottom, button.BackColor);
607                                 }
608                         }
609                 }
610
611                 public override Size ButtonBaseDefaultSize {
612                         get {
613                                 return new Size (75, 23);
614                         }
615                 }
616                 #endregion      // ButtonBase
617
618                 #region CheckBox
619                 public override void DrawCheckBox(Graphics dc, Rectangle clip_area, CheckBox checkbox) {
620                         StringFormat            text_format;
621                         Rectangle               client_rectangle;
622                         Rectangle               text_rectangle;
623                         Rectangle               checkbox_rectangle;
624                         SolidBrush              sb;
625                         int                     checkmark_size=13;
626                         int                     checkmark_space = 4;
627
628                         client_rectangle = checkbox.ClientRectangle;
629                         text_rectangle = client_rectangle;
630                         checkbox_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, checkmark_size, checkmark_size);
631
632                         text_format = new StringFormat();
633                         text_format.Alignment=StringAlignment.Near;
634                         text_format.LineAlignment=StringAlignment.Center;
635
636                         /* Calculate the position of text and checkbox rectangle */
637                         if (checkbox.appearance!=Appearance.Button) {
638                                 switch(checkbox.check_alignment) {
639                                         case ContentAlignment.BottomCenter: {
640                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
641                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
642                                                 text_rectangle.X=client_rectangle.X;
643                                                 text_rectangle.Width=client_rectangle.Width;
644                                                 text_rectangle.Height=client_rectangle.Height-checkbox_rectangle.Y-checkmark_space;
645                                                 break;
646                                         }
647
648                                         case ContentAlignment.BottomLeft: {
649                                                 checkbox_rectangle.X=client_rectangle.Left;
650                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
651                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
652                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
653                                                 break;
654                                         }
655
656                                         case ContentAlignment.BottomRight: {
657                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
658                                                 checkbox_rectangle.Y=client_rectangle.Bottom-checkmark_size;
659                                                 text_rectangle.X=client_rectangle.X;
660                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                             
661                                                 break;
662                                         }
663
664                                         case ContentAlignment.MiddleCenter: {
665                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
666                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
667                                                 text_rectangle.X=client_rectangle.X;
668                                                 text_rectangle.Width=client_rectangle.Width;
669                                                 break;
670                                         }
671
672                                         default:
673                                         case ContentAlignment.MiddleLeft: {
674                                                 checkbox_rectangle.X=client_rectangle.Left;
675                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
676                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
677                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;                                                                                             
678                                                 break;
679                                         }
680
681                                         case ContentAlignment.MiddleRight: {
682                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
683                                                 checkbox_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-checkmark_size/2;
684                                                 text_rectangle.X=client_rectangle.X;
685                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
686                                                 break;
687                                         }
688
689                                         case ContentAlignment.TopCenter: {
690                                                 checkbox_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-checkmark_size/2;
691                                                 checkbox_rectangle.Y=client_rectangle.Top;
692                                                 text_rectangle.X=client_rectangle.X;
693                                                 text_rectangle.Width=client_rectangle.Width;
694                                                 text_rectangle.Y=checkmark_size+checkmark_space;
695                                                 text_rectangle.Height=client_rectangle.Height-checkmark_size-checkmark_space;
696                                                 break;
697                                         }
698
699                                         case ContentAlignment.TopLeft: {
700                                                 checkbox_rectangle.X=client_rectangle.Left;
701                                                 text_rectangle.X=client_rectangle.X+checkmark_size+checkmark_space;
702                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
703                                                 break;
704                                         }
705
706                                         case ContentAlignment.TopRight: {
707                                                 checkbox_rectangle.X=client_rectangle.Right-checkmark_size;
708                                                 text_rectangle.X=client_rectangle.X;
709                                                 text_rectangle.Width=client_rectangle.Width-checkmark_size-checkmark_space;
710                                                 break;
711                                         }
712                                 }
713                         } else {
714                                 text_rectangle.X=client_rectangle.X;
715                                 text_rectangle.Width=client_rectangle.Width;
716                         }
717                         
718                         /* Set the horizontal alignment of our text */
719                         switch(checkbox.text_alignment) {
720                                 case ContentAlignment.BottomLeft:
721                                 case ContentAlignment.MiddleLeft:
722                                 case ContentAlignment.TopLeft: {
723                                         text_format.Alignment=StringAlignment.Near;
724                                         break;
725                                 }
726
727                                 case ContentAlignment.BottomCenter:
728                                 case ContentAlignment.MiddleCenter:
729                                 case ContentAlignment.TopCenter: {
730                                         text_format.Alignment=StringAlignment.Center;
731                                         break;
732                                 }
733
734                                 case ContentAlignment.BottomRight:
735                                 case ContentAlignment.MiddleRight:
736                                 case ContentAlignment.TopRight: {
737                                         text_format.Alignment=StringAlignment.Far;
738                                         break;
739                                 }
740                         }
741
742                         /* Set the vertical alignment of our text */
743                         switch(checkbox.text_alignment) {
744                                 case ContentAlignment.TopLeft: 
745                                 case ContentAlignment.TopCenter: 
746                                 case ContentAlignment.TopRight: {
747                                         text_format.LineAlignment=StringAlignment.Near;
748                                         break;
749                                 }
750
751                                 case ContentAlignment.BottomLeft:
752                                 case ContentAlignment.BottomCenter:
753                                 case ContentAlignment.BottomRight: {
754                                         text_format.LineAlignment=StringAlignment.Far;
755                                         break;
756                                 }
757
758                                 case ContentAlignment.MiddleLeft:
759                                 case ContentAlignment.MiddleCenter:
760                                 case ContentAlignment.MiddleRight: {
761                                         text_format.LineAlignment=StringAlignment.Center;
762                                         break;
763                                 }
764                         }
765
766                         ButtonState state = ButtonState.Normal;
767                         if (checkbox.FlatStyle == FlatStyle.Flat) {
768                                 state |= ButtonState.Flat;
769                         }
770                         
771                         if (checkbox.Checked) {
772                                 state |= ButtonState.Checked;
773                         }
774                         
775                         if (checkbox.ThreeState && (checkbox.CheckState == CheckState.Indeterminate)) {
776                                 state |= ButtonState.Checked;
777                                 state |= ButtonState.Pushed;                            
778                         }
779                         
780                         // finally make sure the pushed and inavtive states are rendered
781                         if (!checkbox.Enabled) {
782                                 state |= ButtonState.Inactive;
783                         }
784                         else if (checkbox.is_pressed) {
785                                 state |= ButtonState.Pushed;
786                         }
787                         
788                         
789                         // Start drawing
790
791                         sb=new SolidBrush(checkbox.BackColor);
792                         dc.FillRectangle(sb, checkbox.ClientRectangle);
793                         sb.Dispose();
794                         
795                         // render as per normal button
796                         if (checkbox.appearance==Appearance.Button) {
797                                 if (checkbox.FlatStyle == FlatStyle.Flat || checkbox.FlatStyle == FlatStyle.Popup) {
798                                         DrawFlatStyleButton(dc, checkbox.ClientRectangle, checkbox);
799                                 } else {
800                                         CPDrawButton(dc, checkbox.ClientRectangle, state);
801                                 }
802                         } else {
803                                 // establish if we are rendering a flat style of some sort
804                                 if (checkbox.FlatStyle == FlatStyle.Flat || checkbox.FlatStyle == FlatStyle.Popup) {
805                                         DrawFlatStyleCheckBox (dc, checkbox_rectangle, checkbox);
806                                 } else {
807                                         ControlPaint.DrawCheckBox(dc, checkbox_rectangle, state);
808                                 }
809                         }
810                         
811                         // offset the text if it's pressed and a button
812                         if (checkbox.Appearance == Appearance.Button) {
813                                 if (checkbox.Checked || (checkbox.Capture && checkbox.FlatStyle != FlatStyle.Flat)) {
814                                         text_rectangle.X ++;
815                                         text_rectangle.Y ++;
816                                 }
817                                 
818                                 text_rectangle.Inflate(-4, -4);
819                         }
820                         
821                         /* Place the text; to be compatible with Windows place it after the checkbox has been drawn */
822                         if (checkbox.Enabled) {
823                                 sb = ResPool.GetSolidBrush(checkbox.ForeColor);
824                                 dc.DrawString(checkbox.Text, checkbox.Font, sb, text_rectangle, text_format);                           
825                         } else if (checkbox.FlatStyle == FlatStyle.Flat || checkbox.FlatStyle == FlatStyle.Popup) {
826                                 dc.DrawString(checkbox.Text, checkbox.Font, ResPool.GetSolidBrush (ControlPaint.DarkDark (this.ColorButtonFace)), text_rectangle, text_format);
827                         } else {
828                                 CPDrawStringDisabled(dc, checkbox.Text, checkbox.Font, ColorButtonText, text_rectangle, text_format);
829                         }
830
831                         if (checkbox.Focused) {
832                                 if (checkbox.FlatStyle != FlatStyle.Flat) {
833                                         DrawInnerFocusRectangle (dc, Rectangle.Inflate (client_rectangle, -4, -4), checkbox.BackColor);
834                                 } else {
835                                         dc.DrawRectangle (ResPool.GetPen (checkbox.ForeColor), Rectangle.Inflate (client_rectangle, -4, -4));
836                                 }
837                         }
838                 }
839
840                 // renders a checkBox with the Flat and Popup FlatStyle
841                 private void DrawFlatStyleCheckBox (Graphics graphics, Rectangle rectangle, CheckBox checkbox)
842                 {
843                         Pen                     pen;                    
844                         Rectangle       rect;
845                         Rectangle       checkbox_rectangle;
846                         Rectangle       fill_rectangle;
847                         int                     lineWidth;
848                         int                     Scale;
849                         
850                         // set up our rectangles first
851                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered) {
852                                 // clip one pixel from bottom right for non popup rendered checkboxes
853                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-1, 0), Math.Max(rectangle.Height-1,0));
854                                 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));
855                         } else {
856                                 // clip two pixels from bottom right for non popup rendered checkboxes
857                                 checkbox_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max(rectangle.Width-2, 0), Math.Max(rectangle.Height-2,0));
858                                 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));
859                         }       
860                                 
861                         // if disabled render in disabled state
862                         if (checkbox.Enabled) {
863                                 // process the state of the checkbox
864                                 if (checkbox.is_entered || checkbox.Capture) {
865                                         // decide on which background color to use
866                                         if (checkbox.FlatStyle == FlatStyle.Popup && checkbox.is_entered && checkbox.Capture) {
867                                                 graphics.FillRectangle(ResPool.GetSolidBrush (checkbox.BackColor), fill_rectangle);
868                                         } else if (checkbox.FlatStyle == FlatStyle.Flat && !(checkbox.is_entered && checkbox.Capture)) {
869                                                 graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.Light(checkbox.BackColor)), fill_rectangle);
870                                         } else {
871                                                 // use regular window background color
872                                                 graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);
873                                         }
874                                         
875                                         // render the outer border
876                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
877                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
878                                         } else {
879                                                 // draw sunken effect
880                                                 CPDrawBorder3D (graphics, checkbox_rectangle, Border3DStyle.SunkenInner, Border3DSide.All, checkbox.BackColor);
881                                         }
882                                 } else {
883                                         graphics.FillRectangle(ResPool.GetSolidBrush (ControlPaint.LightLight (checkbox.BackColor)), fill_rectangle);                           
884                                         
885                                         if (checkbox.FlatStyle == FlatStyle.Flat) {
886                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, checkbox.ForeColor, ButtonBorderStyle.Solid);
887                                         } else {
888                                                 // draw the outer border
889                                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ControlPaint.DarkDark (checkbox.BackColor), ButtonBorderStyle.Solid);
890                                         }                       
891                                 }
892                         } else {
893                                 if (checkbox.FlatStyle == FlatStyle.Popup) {
894                                         graphics.FillRectangle(SystemBrushes.Control, fill_rectangle);
895                                 }       
896                         
897                                 // draw disabled state,
898                                 ControlPaint.DrawBorder(graphics, checkbox_rectangle, ColorButtonShadow, ButtonBorderStyle.Solid);
899                         }               
900                         
901                         /* Make sure we've got at least a line width of 1 */
902                         lineWidth = Math.Max(3, fill_rectangle.Width/3);
903                         Scale=Math.Max(1, fill_rectangle.Width/9);
904                         
905                         // flat style check box is rendered inside a rectangle shifted down by one
906                         rect=new Rectangle(fill_rectangle.X, fill_rectangle.Y+1, fill_rectangle.Width, fill_rectangle.Height);
907                         if (checkbox.Enabled) {
908                                 pen=ResPool.GetPen(checkbox.ForeColor);
909                         } else {
910                                 pen=SystemPens.ControlDark;
911                         }
912
913                         if (checkbox.Checked) {
914                                 /* Need to draw a check-mark */
915                                 for (int i=0; i<lineWidth; i++) {
916                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
917                                         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);
918                                 }
919                         }                                       
920                 }
921
922
923                 #endregion      // CheckBox
924
925                 #region GroupBox
926                 public override void DrawGroupBox (Graphics dc,  Rectangle area, GroupBox box) {
927                         SizeF size;
928                         int width, y;
929                         Rectangle rect = box.ClientRectangle;
930                         Color disabled = ColorGrayText;
931                         
932                         Pen pen_light = ResPool.GetPen (Color.FromArgb (255,255,255,255));
933                         Pen pen_dark = ResPool.GetPen (Color.FromArgb (255, 128, 128,128));
934                         
935                         // TODO: When the Light and Dark methods work this code should be activate it
936                         //Pen pen_light = new Pen (ControlPaint.Light (disabled, 1));
937                         //Pen pen_dark = new Pen (ControlPaint.Dark (disabled, 0));
938
939                         dc.FillRectangle (ResPool.GetSolidBrush (box.BackColor), rect);
940
941                         size = dc.MeasureString (box.Text, box.Font);
942                         width = (int) size.Width;
943                         
944                         if (width > box.Width - 16)
945                                 width = box.Width - 16;
946                         
947                         y = box.Font.Height / 2;
948                         
949                         /* Draw group box*/
950                         dc.DrawLine (pen_dark, 0, y, 8, y); // top 
951                         dc.DrawLine (pen_light, 0, y + 1, 8, y + 1);                    
952                         dc.DrawLine (pen_dark, 8 + width, y, box.Width, y);                     
953                         dc.DrawLine (pen_light, 8 + width, y + 1, box.Width, y + 1);
954                         
955                         dc.DrawLine (pen_dark, 0, y + 1, 0, box.Height); // left
956                         dc.DrawLine (pen_light, 1, y + 1, 1, box.Height);                       
957                         
958                         dc.DrawLine (pen_dark, 0, box.Height - 2, box.Width,  box.Height - 2); // bottom
959                         dc.DrawLine (pen_light, 0, box.Height - 1, box.Width,  box.Height - 1);
960                         
961                         dc.DrawLine (pen_dark, box.Width - 2, y,  box.Width - 2, box.Height - 2); // right
962                         dc.DrawLine (pen_light, box.Width - 1, y, box.Width - 1, box.Height - 2);
963                         
964                         
965                         /* Text */
966                         if (box.Enabled)
967                                 dc.DrawString (box.Text, box.Font, new SolidBrush (box.ForeColor), 10, 0);
968                         else
969                                 CPDrawStringDisabled (dc, box.Text, box.Font, box.ForeColor, 
970                                         new RectangleF (10, 0, width,  box.Font.Height), new StringFormat ());                                  
971                                 
972                 }
973
974                 public override Size GroupBoxDefaultSize {
975                         get {
976                                 return new Size (200,100);
977                         }
978                 }
979                 #endregion
980
981                 #region HScrollBar
982                 public override Size HScrollBarDefaultSize {
983                         get {
984                                 return new Size (80, this.ScrollBarButtonSize);
985                         }
986                 }
987
988                 #endregion      // HScrollBar
989
990                 #region Label
991                 public  override void DrawLabel (Graphics dc, Rectangle clip_rectangle, Label label) 
992                 {               
993                         dc.FillRectangle (ResPool.GetSolidBrush (label.BackColor), clip_rectangle);
994                         
995                         CPDrawBorderStyle (dc, clip_rectangle, label.BorderStyle);              
996
997                         if (label.Enabled) {
998                                 dc.DrawString (label.Text, label.Font, ResPool.GetSolidBrush (label.ForeColor), clip_rectangle, label.string_format);
999                         } else {
1000                                 ControlPaint.DrawStringDisabled (dc, label.Text, label.Font, label.ForeColor, clip_rectangle, label.string_format);
1001                         }
1002                 
1003                 }
1004
1005                 public override Size LabelDefaultSize {
1006                         get {
1007                                 return new Size (100, 23);
1008                         }
1009                 }
1010                 #endregion      // Label
1011                 
1012                 #region ListBox
1013                 
1014                 // Drawing
1015                 
1016                 public override void DrawListBoxDecorations (Graphics dc, ListBox ctrl)
1017                 {                       
1018                         Rectangle cl = ctrl.LBoxInfo.client_rect;
1019                         
1020                         // Draw decorations
1021                         switch (ctrl.BorderStyle) {
1022                         case BorderStyle.Fixed3D: {                             
1023                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), cl.X, cl.Y, cl.X + cl.Width, cl.Y); //top 
1024                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), cl.X + 1, cl.Y + 1, cl.X + cl.Width - 2, cl.Y + 1);
1025                                 dc.DrawLine (ResPool.GetPen (ColorButtonFace), cl.X, cl.Y + cl.Height - 2, cl.X + cl.Width, cl.Y + cl.Height - 2); //down
1026                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), cl.X, cl.Y + cl.Height - 1, cl.X + cl.Width, cl.Y + cl.Height - 1);
1027                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), cl.X, cl.Y, cl.X, cl.Y + cl.Height); //left
1028                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), cl.X + 1, cl.Y + 1, cl.X + 1, cl.Y + cl.Height - 2); 
1029                                 dc.DrawLine (ResPool.GetPen (ColorButtonFace), cl.X + cl.Width - 2, cl.Y, cl.X + cl.Width - 2, cl.Y + cl.Height); //right
1030                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), cl.X + cl.Width - 1, cl.Y + 1 , cl.X + cl.Width - 1, cl.Y + cl.Height - 1);           
1031                                 break;
1032                         }
1033                         case BorderStyle.FixedSingle:
1034                                 dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), cl.X, cl.Y, cl.Width - 1, cl.Height - 1);
1035                                 break;
1036                         case BorderStyle.None:
1037                         default:
1038                                 break;
1039                         }               
1040                 }               
1041                 
1042                 private int DrawListBoxDecorationSize (BorderStyle border_style)
1043                 {
1044                         switch (border_style) {
1045                                 case BorderStyle.Fixed3D:
1046                                         return 2;
1047                                 case BorderStyle.FixedSingle:                                   
1048                                         return 1;
1049                                 case BorderStyle.None:
1050                                 default:
1051                                         break;
1052                                 }
1053                                 
1054                         return 0;
1055                 }                       
1056                 
1057                 // Sizing                               
1058                 public override int DrawListBoxDecorationTop  (BorderStyle border_style) { return DrawListBoxDecorationSize (border_style);}
1059                 public override int DrawListBoxDecorationBottom  (BorderStyle border_style) { return DrawListBoxDecorationSize (border_style);}
1060                 public override int DrawListBoxDecorationRight (BorderStyle border_style) { return DrawListBoxDecorationSize (border_style);}
1061                 public override int DrawListBoxDecorationLeft (BorderStyle border_style) { return DrawListBoxDecorationSize (border_style);}
1062                 
1063                 #endregion ListBox
1064
1065                 #region ListView
1066                 // Drawing
1067                 public override void DrawListView (Graphics dc, Rectangle clip_rectangle, ListView control)
1068                 {
1069                         bool details = (control.View == View.Details);
1070
1071                         // Clear the graphics context
1072                         dc.Clear (control.BackColor);
1073
1074                         // border is drawn directly in the Paint method
1075                         if (details && control.HeaderStyle != ColumnHeaderStyle.None) {
1076                                 dc.FillRectangle (ResPool.GetSolidBrush (SystemColors.Control),
1077                                                   0, 0, control.TotalWidth, control.Font.Height);
1078                                 if (control.Columns.Count > 0) {
1079                                         if (control.HeaderStyle == ColumnHeaderStyle.Clickable) {
1080                                                 foreach (ColumnHeader col in control.Columns) {
1081                                                         this.CPDrawButton (dc, col.Rect,
1082                                                                            (col.Pressed ?
1083                                                                             ButtonState.Pushed :
1084                                                                             ButtonState.Normal));
1085                                                         dc.DrawString (col.Text, control.Font,
1086                                                                        ResPool.GetSolidBrush
1087                                                                        (this.ColorButtonText),
1088                                                                        col.Rect, col.Format);
1089                                                 }
1090                                         }
1091                                         // Non-clickable columns
1092                                         else {
1093                                                 foreach (ColumnHeader col in control.Columns) {
1094                                                         this.CPDrawButton (dc, col.Rect, ButtonState.Flat);
1095                                                         dc.DrawString (col.Text, control.Font,
1096                                                                        ResPool.GetSolidBrush
1097                                                                        (this.ColorButtonText),
1098                                                                        col.Rect, col.Format);
1099                                                 }
1100                                         }
1101                                 }
1102                         }
1103
1104                         // In case of details view draw the items only if
1105                         // columns are non-zero
1106                         if (!details || control.Columns.Count > 0)
1107                                 foreach (ListViewItem item in control.Items)
1108                                         this.DrawListViewItem (dc, control, item);
1109
1110                         // draw the gridlines
1111                         if (details && control.GridLines) {
1112                                 int top = (control.HeaderStyle == ColumnHeaderStyle.None) ?
1113                                         2 : control.Font.Height + 2;
1114
1115                                 // draw vertical gridlines
1116                                 foreach (ColumnHeader col in control.Columns)
1117                                         dc.DrawLine (this.ResPool.GetPen (this.ColorButtonFace),
1118                                                      col.Rect.Right, top,
1119                                                      col.Rect.Right, control.TotalHeight);
1120                                 // draw horizontal gridlines
1121                                 ListViewItem last_item = null;
1122                                 foreach (ListViewItem item in control.Items) {
1123                                         dc.DrawLine (this.ResPool.GetPen (this.ColorButtonFace),
1124                                                      item.EntireRect.Left, item.EntireRect.Top,
1125                                                      control.TotalWidth, item.EntireRect.Top);
1126                                         last_item = item;
1127                                 }
1128
1129                                 // draw a line after at the bottom of the last item
1130                                 if (last_item != null) {
1131                                         dc.DrawLine (this.ResPool.GetPen (this.ColorButtonFace),
1132                                                      last_item.EntireRect.Left,
1133                                                      last_item.EntireRect.Bottom,
1134                                                      control.TotalWidth,
1135                                                      last_item.EntireRect.Bottom);
1136                                 }
1137                         }
1138                 }
1139
1140                 // draws the ListViewItem of the given index
1141                 private void DrawListViewItem (Graphics dc, ListView control, ListViewItem item)
1142                 {
1143                         if (control.CheckBoxes) {
1144                                 if (control.StateImageList == null) {
1145                                         // Make sure we've got at least a line width of 1
1146                                         int check_wd = Math.Max (3, item.CheckRect.Width / 6);
1147                                         int scale = Math.Max (1, item.CheckRect.Width / 12);
1148
1149                                         // set the checkbox background
1150                                         dc.FillRectangle (this.ResPool.GetSolidBrush (this.ColorWindow),
1151                                                           item.CheckRect);
1152                                         // define a rectangle inside the border area
1153                                         Rectangle rect = new Rectangle (item.CheckRect.X + 2,
1154                                                                         item.CheckRect.Y + 2,
1155                                                                         item.CheckRect.Width - 4,
1156                                                                         item.CheckRect.Height - 4);
1157                                         Pen pen = new Pen (this.ColorWindowText, 2);
1158                                         dc.DrawRectangle (pen, rect);
1159
1160                                         // Need to draw a check-mark
1161                                         if (item.Checked) {
1162                                                 pen.Width = 1;
1163                                                 // adjustments to get the check-mark at the right place
1164                                                 rect.X ++; rect.Y ++;
1165                                                 // following logic is taken from DrawFrameControl method
1166                                                 for (int i = 0; i < check_wd; i++) {
1167                                                         dc.DrawLine (pen, rect.Left + check_wd / 2,
1168                                                                      rect.Top + check_wd + i,
1169                                                                      rect.Left + check_wd / 2 + 2 * scale,
1170                                                                      rect.Top + check_wd + 2 * scale + i);
1171                                                         dc.DrawLine (pen,
1172                                                                      rect.Left + check_wd / 2 + 2 * scale,
1173                                                                      rect.Top + check_wd + 2 * scale + i,
1174                                                                      rect.Left + check_wd / 2 + 6 * scale,
1175                                                                      rect.Top + check_wd - 2 * scale + i);
1176                                                 }
1177                                         }
1178                                 }
1179                                 else {
1180                                         if (item.Checked && control.StateImageList.Images.Count > 1)
1181                                                 control.StateImageList.Draw (dc,
1182                                                                              item.CheckRect.Location, 1);
1183                                         else if (! item.Checked && control.StateImageList.Images.Count > 0)
1184                                                 control.StateImageList.Draw (dc,
1185                                                                              item.CheckRect.Location, 0);
1186                                 }
1187                         }
1188
1189                         // Item is drawn as a special case, as it is not just text
1190                         if (control.View == View.LargeIcon) {
1191                                 if (item.ImageIndex > -1 &&
1192                                     control.LargeImageList != null &&
1193                                     item.ImageIndex < control.LargeImageList.Images.Count)
1194                                         control.LargeImageList.Draw (dc, item.IconRect.Location,
1195                                                                      item.ImageIndex);
1196                         }
1197                         else {
1198                                 if (item.ImageIndex > -1 &&
1199                                     control.SmallImageList != null &&
1200                                     item.ImageIndex < control.SmallImageList.Images.Count)
1201                                         control.SmallImageList.Draw (dc, item.IconRect.Location,
1202                                                                      item.ImageIndex);
1203                         }
1204
1205                         // draw the item text
1206                         Rectangle text_rect = item.LabelRect;
1207                         // format for the item text
1208                         StringFormat format = new StringFormat ();
1209                         format.LineAlignment = StringAlignment.Center;
1210                         if (control.View == View.LargeIcon)
1211                                 format.Alignment = StringAlignment.Center;
1212                         else
1213                                 format.Alignment = StringAlignment.Near;
1214                         
1215                         if (!control.LabelWrap)
1216                                 format.FormatFlags = StringFormatFlags.NoWrap;
1217                         
1218                         if (item.Selected) {
1219                                 if (control.View == View.Details) {
1220                                         if (control.FullRowSelect) {
1221                                                 // fill the entire rect excluding the checkbox
1222                                                 Rectangle full_rect = item.EntireRect;
1223                                                 full_rect.Location = item.LabelRect.Location;
1224                                                 dc.FillRectangle (this.ResPool.GetSolidBrush
1225                                                                   (this.ColorHilight), full_rect);
1226                                         }
1227                                         else {
1228                                                 Size text_size = Size.Ceiling (dc.MeasureString (item.Text,
1229                                                                                                 item.Font));
1230                                                 text_rect.Width = text_size.Width;
1231                                                 dc.FillRectangle (this.ResPool.GetSolidBrush
1232                                                                   (this.ColorHilight), text_rect);
1233                                         }
1234                                 }
1235                                 else {
1236                                         /*Size text_size = Size.Ceiling (dc.MeasureString (item.Text,
1237                                           item.Font));
1238                                           Point loc = text_rect.Location;
1239                                           loc.X += (text_rect.Width - text_size.Width) / 2;
1240                                           text_rect.Width = text_size.Width;*/
1241                                         dc.FillRectangle (this.ResPool.GetSolidBrush (this.ColorHilight),
1242                                                           text_rect);
1243                                 }
1244                         }
1245                         else
1246                                 dc.FillRectangle (ResPool.GetSolidBrush (item.BackColor), text_rect);
1247
1248                         if (item.Text != null && item.Text.Length > 0) {
1249                                 if (item.Selected)
1250                                         dc.DrawString (item.Text, item.Font, this.ResPool.GetSolidBrush
1251                                                        (this.ColorHilightText), text_rect, format);
1252                                 else
1253                                         dc.DrawString (item.Text, item.Font, this.ResPool.GetSolidBrush
1254                                                        (item.ForeColor), text_rect, format);
1255                         }
1256
1257                         if (control.View == View.Details && control.Columns.Count > 0) {
1258                                 // draw subitems for details view
1259                                 ListViewItem.ListViewSubItemCollection subItems = item.SubItems;
1260                                 int count = (control.Columns.Count < subItems.Count ? 
1261                                              control.Columns.Count : subItems.Count);
1262
1263                                 if (count > 0) {
1264                                         ColumnHeader col;
1265                                         ListViewItem.ListViewSubItem subItem;
1266                                         Rectangle sub_item_rect = item.LabelRect;
1267
1268                                         // set the format for subitems
1269                                         format.FormatFlags = StringFormatFlags.NoWrap;
1270                                         format.Alignment = StringAlignment.Near;
1271
1272                                         // 0th subitem is the item already drawn
1273                                         for (int index = 1; index < count; index++) {
1274                                                 subItem = subItems [index];
1275                                                 col = control.Columns [index];
1276                                                 sub_item_rect.X = col.Rect.Left;
1277                                                 sub_item_rect.Width = col.Wd;
1278
1279                                                 SolidBrush sub_item_back_br = null;
1280                                                 SolidBrush sub_item_fore_br = null;
1281                                                 Font sub_item_font = null;
1282
1283                                                 if (item.UseItemStyleForSubItems) {
1284                                                         sub_item_back_br = this.ResPool.GetSolidBrush
1285                                                                 (item.BackColor);
1286                                                         sub_item_fore_br = this.ResPool.GetSolidBrush
1287                                                                 (item.ForeColor);
1288                                                         sub_item_font = item.Font;
1289                                                 }
1290                                                 else {
1291                                                         sub_item_back_br = this.ResPool.GetSolidBrush
1292                                                                 (subItem.BackColor);
1293                                                         sub_item_fore_br = this.ResPool.GetSolidBrush
1294                                                                 (subItem.ForeColor);
1295                                                         sub_item_font = subItem.Font;
1296                                                 }
1297
1298                                                 // In case of fullrowselect, background is filled
1299                                                 // for the entire rect above
1300                                                 if (item.Selected && control.FullRowSelect) {
1301                                                         if (subItem.Text != null && subItem.Text.Length > 0)
1302                                                                 dc.DrawString (subItem.Text, sub_item_font,
1303                                                                                this.ResPool.GetSolidBrush
1304                                                                                (this.ColorHilightText),
1305                                                                                sub_item_rect, format);
1306                                                 }
1307                                                 else {
1308                                                         dc.FillRectangle (sub_item_back_br, sub_item_rect);
1309                                                         if (subItem.Text != null && subItem.Text.Length > 0)
1310                                                                 dc.DrawString (subItem.Text, sub_item_font,
1311                                                                                sub_item_fore_br,
1312                                                                                sub_item_rect, format);
1313                                                 }
1314                                                 sub_item_rect.X += col.Wd;
1315                                         }
1316                                 }
1317                         }
1318                 }
1319
1320                 // Sizing
1321                 public override Size ListViewCheckBoxSize {
1322                         get { return new Size (16, 16); }
1323                 }
1324
1325                 public override int ListViewColumnHeaderHeight {
1326                         get { return 16; }
1327                 }
1328
1329                 public override int ListViewDefaultColumnWidth {
1330                         get { return 60; }
1331                 }
1332
1333                 public override int ListViewVerticalSpacing {
1334                         get { return 22; }
1335                 }
1336
1337                 public override int ListViewEmptyColumnWidth {
1338                         get { return 10; }
1339                 }
1340
1341                 public override int ListViewHorizontalSpacing {
1342                         get { return 10; }
1343                 }
1344
1345                 public override Size ListViewDefaultSize {
1346                         get { return new Size (121, 97); }
1347                 }
1348                 #endregion      // ListView
1349
1350                 #region MonthCalendar
1351
1352                 // draw the month calendar
1353                 public override void DrawMonthCalendar(Graphics dc, Rectangle clip_rectangle, MonthCalendar mc) \r
1354                 {
1355                         Rectangle client_rectangle = mc.ClientRectangle;
1356                         Size month_size = mc.SingleMonthSize;
1357                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
1358                         Size calendar_spacing = (Size)((object)mc.calendar_spacing);
1359                         Size date_cell_size = (Size)((object)mc.date_cell_size);
1360
1361                         // draw the singlecalendars
1362                         int x_offset = 1;
1363                         int y_offset = 1;
1364                         // adjust for the position of the specific month
1365                         for (int i=0; i < mc.CalendarDimensions.Height; i++) \r
1366                         {
1367                                 if (i > 0) \r
1368                                 {
1369                                         y_offset += month_size.Height + calendar_spacing.Height;
1370                                 }
1371                                 // now adjust for x position    
1372                                 for (int j=0; j < mc.CalendarDimensions.Width; j++) \r
1373                                 {
1374                                         if (j > 0) \r
1375                                         {
1376                                                 x_offset += month_size.Width + calendar_spacing.Width;
1377                                         } \r
1378                                         else \r
1379                                         {
1380                                                 x_offset = 1;
1381                                         }
1382
1383                                         Rectangle month_rect = new Rectangle (x_offset, y_offset, month_size.Width, month_size.Height);
1384                                         if (month_rect.IntersectsWith (clip_rectangle)) {
1385                                                 DrawSingleMonth (
1386                                                         dc,
1387                                                         clip_rectangle,
1388                                                         month_rect,
1389                                                         mc,
1390                                                         i,
1391                                                         j);
1392                                         }
1393                                 }
1394                         }
1395                         
1396                         Rectangle bottom_rect = new Rectangle (
1397                                                 client_rectangle.X,
1398                                                 Math.Max(client_rectangle.Bottom - date_cell_size.Height - 3, 0),
1399                                                 client_rectangle.Width,
1400                                                 date_cell_size.Height + 2);
1401                         // draw the today date if it's set
1402                         if (mc.ShowToday && bottom_rect.IntersectsWith (clip_rectangle)) \r
1403                         {
1404                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), bottom_rect);
1405                                 if (mc.ShowToday) {
1406                                         int today_offset = 5;
1407                                         if (mc.ShowTodayCircle) \r
1408                                         {
1409                                                 Rectangle today_circle_rect = new Rectangle (
1410                                                         client_rectangle.X + 5,
1411                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height - 2, 0),
1412                                                         date_cell_size.Width,
1413                                                         date_cell_size.Height);
1414                                                         DrawTodayCircle (dc, today_circle_rect);
1415                                                 today_offset += date_cell_size.Width + 5;
1416                                         }
1417                                         // draw today's date
1418                                         StringFormat text_format = new StringFormat();
1419                                         text_format.LineAlignment = StringAlignment.Center;
1420                                         text_format.Alignment = StringAlignment.Near;
1421                                         Font bold_font = new Font (mc.Font.FontFamily, mc.Font.Size, mc.Font.Style | FontStyle.Bold);
1422                                         Rectangle today_rect = new Rectangle (
1423                                                         today_offset + client_rectangle.X,
1424                                                         Math.Max(client_rectangle.Bottom - date_cell_size.Height, 0),
1425                                                         Math.Max(client_rectangle.Width - today_offset, 0),
1426                                                         date_cell_size.Height);
1427                                         dc.DrawString ("Today: " + DateTime.Now.ToShortDateString(), bold_font, ResPool.GetSolidBrush (mc.ForeColor), today_rect, text_format);
1428                                 }                               
1429                         }
1430                         
1431                         // finally paint the borders of the calendars as required
1432                         for (int i = 0; i <= mc.CalendarDimensions.Width; i++) {
1433                                 if (i == 0 && clip_rectangle.X == client_rectangle.X) {
1434                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Y, 1, client_rectangle.Height));
1435                                 } else if (i == mc.CalendarDimensions.Width && clip_rectangle.Right == client_rectangle.Right) {
1436                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.Right-1, client_rectangle.Y, 1, client_rectangle.Height));
1437                                 } else { 
1438                                         Rectangle rect = new Rectangle (
1439                                                 client_rectangle.X + (month_size.Width*i) + (calendar_spacing.Width * (i-1)) + 1,
1440                                                 client_rectangle.Y,
1441                                                 calendar_spacing.Width,
1442                                                 client_rectangle.Height);
1443                                         if (i < mc.CalendarDimensions.Width && i > 0 && clip_rectangle.IntersectsWith (rect)) {
1444                                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), rect);
1445                                         }
1446                                 }
1447                         }
1448                         for (int i = 0; i <= mc.CalendarDimensions.Height; i++) {
1449                                 if (i == 0 && clip_rectangle.Y == client_rectangle.Y) {
1450                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Y, client_rectangle.Width, 1));
1451                                 } else if (i == mc.CalendarDimensions.Height && clip_rectangle.Bottom == client_rectangle.Bottom) {
1452                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), new Rectangle (client_rectangle.X, client_rectangle.Bottom-1, client_rectangle.Width, 1));
1453                                 } else { 
1454                                         Rectangle rect = new Rectangle (
1455                                                 client_rectangle.X,
1456                                                 client_rectangle.Y + (month_size.Height*i) + (calendar_spacing.Height*(i-1)) + 1,
1457                                                 client_rectangle.Width,
1458                                                 calendar_spacing.Height);
1459                                         if (i < mc.CalendarDimensions.Height && i > 0 && clip_rectangle.IntersectsWith (rect)) {
1460                                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), rect);
1461                                         }
1462                                 }
1463                         }
1464                 }
1465
1466                 // darws a single part of the month calendar (with one month)
1467                 private void DrawSingleMonth(Graphics dc, Rectangle clip_rectangle, Rectangle rectangle, MonthCalendar mc, int row, int col) \r
1468                 {
1469                         // cache local copies of Marshal-by-ref internal members (gets around error CS0197)
1470                         Size title_size = (Size)((object)mc.title_size);
1471                         Size date_cell_size = (Size)((object)mc.date_cell_size);
1472                         DateTime current_month = (DateTime)((object)mc.current_month);
1473                         
1474                         // set up some standard string formating variables
1475                         StringFormat text_format = new StringFormat();
1476                         text_format.LineAlignment = StringAlignment.Center;
1477                         text_format.Alignment = StringAlignment.Center;
1478                         
1479
1480                         // draw the title back ground
1481                         DateTime this_month = current_month.AddMonths (row*mc.CalendarDimensions.Width+col);
1482                         Rectangle title_rect = new Rectangle(rectangle.X, rectangle.Y, title_size.Width, title_size.Height);
1483                         if (title_rect.IntersectsWith (clip_rectangle)) {
1484                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), title_rect);
1485                                 // draw the title                               
1486                                 string title_text = this_month.ToString ("MMMM yyyy");
1487                                 dc.DrawString (title_text, mc.Font, ResPool.GetSolidBrush (mc.TitleForeColor), title_rect, text_format);
1488
1489                                 // draw previous and next buttons if it's time
1490                                 if (row == 0 && col == 0) \r
1491                                 {
1492                                         // draw previous button
1493                                         DrawMonthCalendarButton (
1494                                                 dc,
1495                                                 rectangle,
1496                                                 mc,
1497                                                 title_size,
1498                                                 mc.button_x_offset,
1499                                                 (System.Drawing.Size)((object)mc.button_size),
1500                                                 true);
1501                                 }
1502                                 if (row == 0 && col == mc.CalendarDimensions.Width-1) \r
1503                                 {
1504                                         // draw next button
1505                                         DrawMonthCalendarButton (
1506                                                 dc,
1507                                                 rectangle,
1508                                                 mc,
1509                                                 title_size,
1510                                                 mc.button_x_offset,
1511                                                 (System.Drawing.Size)((object)mc.button_size),
1512                                                 false);
1513                                 }
1514                         }
1515                         
1516                         // set the week offset and draw week nums if needed
1517                         int col_offset = (mc.ShowWeekNumbers) ? 1 : 0;
1518                         Rectangle day_name_rect = new Rectangle(
1519                                 rectangle.X,
1520                                 rectangle.Y + title_size.Height,
1521                                 (7 + col_offset) * date_cell_size.Width,
1522                                 date_cell_size.Height);
1523                         if (day_name_rect.IntersectsWith (clip_rectangle)) {
1524                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), day_name_rect);
1525                                 // draw the day names 
1526                                 DayOfWeek first_day_of_week = mc.GetDayOfWeek(mc.FirstDayOfWeek);
1527                                 for (int i=0; i < 7; i++) \r
1528                                 {
1529                                         int position = i - (int) first_day_of_week;
1530                                         if (position < 0) \r
1531                                         {
1532                                                 position = 7 + position;
1533                                         }
1534                                         // draw it
1535                                         Rectangle day_rect = new Rectangle(
1536                                                 day_name_rect.X + ((i + col_offset)* date_cell_size.Width),
1537                                                 day_name_rect.Y,
1538                                                 date_cell_size.Width,
1539                                                 date_cell_size.Height);
1540                                         dc.DrawString (((DayOfWeek)i).ToString().Substring(0, 3), mc.Font, ResPool.GetSolidBrush (mc.TitleBackColor), day_rect, text_format);
1541                                 }
1542                                 
1543                                 // draw the vertical divider
1544                                 int vert_divider_y = Math.Max(title_size.Height+ date_cell_size.Height-1, 0);
1545                                 dc.DrawLine (
1546                                         ResPool.GetPen (mc.ForeColor),
1547                                         rectangle.X + (col_offset * date_cell_size.Width) + mc.divider_line_offset,
1548                                         rectangle.Y + vert_divider_y,
1549                                         rectangle.Right - mc.divider_line_offset,
1550                                         rectangle.Y + vert_divider_y);
1551                         }
1552
1553
1554                         // draw the actual date items in the grid (including the week numbers)
1555                         Rectangle date_rect = new Rectangle (
1556                                 rectangle.X,
1557                                 rectangle.Y + title_size.Height + date_cell_size.Height,
1558                                 date_cell_size.Width,
1559                                 date_cell_size.Height);
1560                         int month_row_count = 0;
1561                         bool draw_week_num_divider = false;
1562                         bool no_more_content = false;   // required to enable us to fill the background rect and not draw any dates on it (if none are required)
1563                         DateTime current_date = mc.GetFirstDateInMonthGrid ( new DateTime (this_month.Year, this_month.Month, 1));
1564                         for (int i=0; i < 6; i++) \r
1565                         {
1566                                 // establish if this row is in our clip_area
1567                                 Rectangle row_rect = new Rectangle (
1568                                         rectangle.X,
1569                                         rectangle.Y + title_size.Height + (date_cell_size.Height * (i+1)),
1570                                         date_cell_size.Width * 7,
1571                                         date_cell_size.Height);
1572                                 if (mc.ShowWeekNumbers) {
1573                                         row_rect.Width += date_cell_size.Width;
1574                                 }
1575                 
1576                                 bool draw_row = row_rect.IntersectsWith (clip_rectangle);
1577                                 if (draw_row) {
1578                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.BackColor), row_rect);
1579                                 }
1580                                 // establish if this is a valid week to draw
1581                                 if (mc.IsValidWeekToDraw (this_month, current_date, row, col)) {
1582                                         month_row_count = i;
1583                                 }
1584                                 
1585                                 // draw the week number if required
1586                                 if (mc.ShowWeekNumbers && month_row_count == i) {
1587                                         if (!draw_week_num_divider) {
1588                                                 draw_week_num_divider = draw_row;
1589                                         }
1590                                         // get the week for this row
1591                                         int week = mc.GetWeekOfYear (current_date);     
1592
1593                                         if (draw_row) {
1594                                                 dc.DrawString (
1595                                                         week.ToString(),
1596                                                         mc.Font,
1597                                                         ResPool.GetSolidBrush (mc.TitleBackColor),
1598                                                         date_rect,
1599                                                         text_format);
1600                                         }
1601                                         date_rect.Offset(date_cell_size.Width, 0);
1602                                 }
1603                                                                 
1604                                 // only draw the days if we have to
1605                                 if(month_row_count == i) {
1606                                         for (int j=0; j < 7; j++) \r
1607                                         {
1608                                                 if (draw_row) {
1609                                                         DrawMonthCalendarDate (
1610                                                                 dc,
1611                                                                 date_rect,
1612                                                                 mc,
1613                                                                 current_date,
1614                                                                 this_month,
1615                                                                 row,
1616                                                                 col);
1617                                                 }
1618
1619                                                 // move the day on
1620                                                 current_date = current_date.AddDays(1);
1621                                                 date_rect.Offset(date_cell_size.Width, 0);
1622                                         }
1623
1624                                         // shift the rectangle down one row
1625                                         int offset = (mc.ShowWeekNumbers) ? -8 : -7;
1626                                         date_rect.Offset(offset*date_cell_size.Width, date_cell_size.Height);
1627                                 }
1628                         }
1629
1630                         // month_row_count is zero based, so add one
1631                         month_row_count++;
1632
1633                         // draw week numbers if required
1634                         if (draw_week_num_divider) {
1635                                 col_offset = 1;
1636                                 dc.DrawLine (
1637                                         ResPool.GetPen (mc.ForeColor),
1638                                         rectangle.X + date_cell_size.Width - 1,
1639                                         rectangle.Y + title_size.Height + date_cell_size.Height + mc.divider_line_offset,
1640                                         rectangle.X + date_cell_size.Width - 1,
1641                                         rectangle.Y + title_size.Height + date_cell_size.Height + (month_row_count * date_cell_size.Height) - mc.divider_line_offset);
1642                         }
1643                 }
1644
1645                 // draws the pervious or next button
1646                 private void DrawMonthCalendarButton (Graphics dc, Rectangle rectangle, MonthCalendar mc, Size title_size, int x_offset, Size button_size, bool is_previous) \r
1647                 {
1648                         bool is_clicked = false;
1649                         Rectangle button_rect;
1650                         Rectangle arrow_rect = new Rectangle (rectangle.X, rectangle.Y, 4, 7);
1651                         Point[] arrow_path = new Point[3];
1652                         // prepare the button
1653                         if (is_previous) \r
1654                         {
1655                                 is_clicked = mc.is_previous_clicked;
1656                                 button_rect = new Rectangle (
1657                                         rectangle.X + 1 + x_offset,
1658                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
1659                                         Math.Max(button_size.Width - 1, 0),
1660                                         Math.Max(button_size.Height - 1, 0));
1661                                 arrow_rect.X = button_rect.X + ((button_rect.Width - arrow_rect.Width)/2);
1662                                 arrow_rect.Y = button_rect.Y + ((button_rect.Height - arrow_rect.Height)/2);
1663                                 if (is_clicked) {
1664                                         arrow_rect.Offset(1,1);
1665                                 }
1666                                 arrow_path[0] = new Point (arrow_rect.Right, arrow_rect.Y);
1667                                 arrow_path[1] = new Point (arrow_rect.X, arrow_rect.Y + arrow_rect.Height/2);
1668                                 arrow_path[2] = new Point (arrow_rect.Right, arrow_rect.Bottom);
1669                         }
1670                         else
1671                         {
1672                                 is_clicked = mc.is_next_clicked;
1673                                 button_rect = new Rectangle (
1674                                         rectangle.Right - 1 - x_offset - button_size.Width,
1675                                         rectangle.Y + 1 + ((title_size.Height - button_size.Height)/2),
1676                                         Math.Max(button_size.Width - 1, 0),
1677                                         Math.Max(button_size.Height - 1, 0));
1678                                 arrow_rect.X = button_rect.X + ((button_rect.Width - arrow_rect.Width)/2);
1679                                 arrow_rect.Y = button_rect.Y + ((button_rect.Height - arrow_rect.Height)/2);
1680                                 if (is_clicked) {
1681                                         arrow_rect.Offset(1,1);
1682                                 }
1683                                 arrow_path[0] = new Point (arrow_rect.X, arrow_rect.Y);
1684                                 arrow_path[1] = new Point (arrow_rect.Right, arrow_rect.Y + arrow_rect.Height/2);
1685                                 arrow_path[2] = new Point (arrow_rect.X, arrow_rect.Bottom);                            
1686                         }
1687
1688                         // fill the background
1689                         dc.FillRectangle (SystemBrushes.Control, button_rect);
1690                         // draw the border
1691                         if (is_clicked) {
1692                                 dc.DrawRectangle (SystemPens.ControlDark, button_rect);
1693                         }
1694                         else {
1695                                 CPDrawBorder3D (dc, button_rect, Border3DStyle.Raised, Border3DSide.All);
1696                         }
1697                         // draw the arrow
1698                         dc.FillPolygon (SystemBrushes.ControlText, arrow_path);                 
1699                 }
1700                 
1701
1702                 // draws one day in the calendar grid
1703                 private void DrawMonthCalendarDate (Graphics dc, Rectangle rectangle, MonthCalendar mc, DateTime date, DateTime month, int row, int col) {
1704                         Color date_color = mc.ForeColor;
1705                         Rectangle interior = new Rectangle (rectangle.X, rectangle.Y, Math.Max(rectangle.Width - 1, 0), Math.Max(rectangle.Height - 1, 0));
1706
1707                         // find out if we are the lead of the first calendar or the trail of the last calendar                                          
1708                         if (date.Year != month.Year || date.Month != month.Month) {
1709                                 DateTime check_date = month.AddMonths (-1);
1710                                 // check if it's the month before 
1711                                 if (check_date.Year == date.Year && check_date.Month == date.Month && row == 0 && col == 0) {
1712                                         date_color = mc.TrailingForeColor;
1713                                 } else {
1714                                         // check if it's the month after
1715                                         check_date = month.AddMonths (1);
1716                                         if (check_date.Year == date.Year && check_date.Month == date.Month && row == mc.CalendarDimensions.Height-1 && col == mc.CalendarDimensions.Width-1) {
1717                                                 date_color = mc.TrailingForeColor;
1718                                         } else {
1719                                                 return;
1720                                         }
1721                                 }
1722                         } else {
1723                                 date_color = mc.ForeColor;
1724                         }
1725
1726
1727                         if (date == mc.SelectionStart && date == mc.SelectionEnd) {
1728                                 // see if the date is in the start of selection
1729                                 date_color = mc.BackColor;
1730                                 // draw the left hand of the back ground
1731                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);                                
1732                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 0, 359);
1733                         } else if (date == mc.SelectionStart) {
1734                                 // see if the date is in the start of selection
1735                                 date_color = mc.BackColor;
1736                                 // draw the left hand of the back ground
1737                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);                                
1738                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 90, 180);
1739                                 // fill the other side as a straight rect
1740                                 if (date < mc.SelectionEnd) \r
1741                                 {
1742                                         // use rectangle instead of rectangle to go all the way to edge of rect
1743                                         selection_rect.X = (int) Math.Floor(rectangle.X + rectangle.Width / 2);
1744                                         selection_rect.Width = Math.Max(rectangle.Right - selection_rect.X, 0);
1745                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
1746                                 }
1747                         } else if (date == mc.SelectionEnd) {
1748                                 // see if it is the end of selection
1749                                 date_color = mc.BackColor;
1750                                 // draw the left hand of the back ground
1751                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, -3, -3);
1752                                 dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 270, 180);
1753                                 // fill the other side as a straight rect
1754                                 if (date > mc.SelectionStart) {
1755                                         selection_rect.X = rectangle.X;
1756                                         selection_rect.Width = rectangle.Width - (rectangle.Width / 2);
1757                                         dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
1758                                 }
1759                         } else if (date > mc.SelectionStart && date < mc.SelectionEnd) {
1760                                 // now see if it's in the middle
1761                                 date_color = mc.BackColor;
1762                                 // draw the left hand of the back ground
1763                                 Rectangle selection_rect = Rectangle.Inflate(rectangle, 0, -3);
1764                                 dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
1765                         }
1766
1767                         // set up some standard string formating variables
1768                         StringFormat text_format = new StringFormat();
1769                         text_format.LineAlignment = StringAlignment.Center;
1770                         text_format.Alignment = StringAlignment.Center;
1771                         
1772
1773                         // establish if it's a bolded font
1774                         Font font;
1775                         if (mc.IsBoldedDate (date)) {
1776                                 font = new Font (mc.Font.FontFamily, mc.Font.Size, mc.Font.Style | FontStyle.Bold);
1777                         } else {
1778                                 font = mc.Font;
1779                         }
1780
1781                         // just draw the date now
1782                         dc.DrawString (date.Day.ToString(), font, ResPool.GetSolidBrush (date_color), rectangle, text_format);
1783
1784                         // today circle if needed
1785                         if (mc.ShowTodayCircle && date == DateTime.Now.Date) {
1786                                 DrawTodayCircle (dc, interior);
1787                         }
1788
1789                         // draw the selection grid
1790                         if (mc.is_date_clicked && mc.clicked_date == date) {                            
1791                                 using (Pen pen = new Pen (Color.Black, 1) ) {
1792                                         pen.DashStyle = DashStyle.Dot;
1793                                         dc.DrawRectangle (pen, interior);
1794                                 }
1795                         }
1796                 }
1797
1798                 private void DrawTodayCircle (Graphics dc, Rectangle rectangle) {
1799                         Color circle_color = Color.FromArgb (248, 0, 0);
1800                         // draw the left hand of the circle 
1801                         Rectangle lhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 4, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 5, 0));
1802                         Rectangle rhs_circle_rect = new Rectangle (rectangle.X + 1, rectangle.Y + 1, Math.Max(rectangle.Width - 2, 0), Math.Max(rectangle.Height - 2, 0));
1803                         Point [] curve_points = new Point [3];
1804                         curve_points [0] = new Point (lhs_circle_rect.X, rhs_circle_rect.Y + rhs_circle_rect.Height/12);
1805                         curve_points [1] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/9, rhs_circle_rect.Y);
1806                         curve_points [2] = new Point (lhs_circle_rect.X + lhs_circle_rect.Width/2 + 1, rhs_circle_rect.Y);
1807
1808                         using (Pen pen = new Pen (circle_color, 2)) {
1809                                 dc.DrawArc (pen, lhs_circle_rect, 90, 180);
1810                                 dc.DrawArc (pen, rhs_circle_rect, 270, 180);                                    
1811                                 dc.DrawCurve (pen, curve_points);
1812                                 dc.DrawLine (ResPool.GetPen (circle_color), curve_points [2], new Point (curve_points [2].X, lhs_circle_rect.Y));
1813                         }
1814                 }
1815
1816                 #endregion      // MonthCalendar
1817
1818                 #region Panel
1819                 public override Size PanelDefaultSize {
1820                         get {
1821                                 return new Size (200, 100);
1822                         }
1823                 }
1824                 #endregion      // Panel
1825
1826                 #region PictureBox
1827                 public override void DrawPictureBox (Graphics dc, PictureBox pb) {
1828                         Rectangle client = pb.ClientRectangle;
1829                         int x, y, width, height;
1830
1831                         dc.FillRectangle (new SolidBrush (pb.BackColor), client);
1832
1833                         x = y = 0;
1834                         if (pb.Image != null) {
1835                                 switch (pb.SizeMode) {
1836                                 case PictureBoxSizeMode.StretchImage:
1837                                         width = client.Width;
1838                                         height = client.Height;
1839                                         break;
1840                                 case PictureBoxSizeMode.CenterImage:
1841                                         width = client.Width;
1842                                         height = client.Height;
1843                                         x = width / 2;
1844                                         y = (height - pb.Image.Height) / 2;
1845                                         break;
1846                                 default:
1847                                         // Normal, AutoSize
1848                                         width = client.Width;
1849                                         height = client.Height;
1850                                         break;
1851                                 }
1852                                 dc.DrawImage (pb.Image, x, y, width, height);
1853                         }
1854                         CPDrawBorderStyle (dc, client, pb.BorderStyle);
1855                 }
1856
1857                 public override Size PictureBoxDefaultSize {
1858                         get {
1859                                 return new Size (100, 50);
1860                         }
1861                 }
1862                 #endregion      // PictureBox
1863
1864                 #region ProgressBar
1865                 public override void DrawProgressBar (Graphics dc, Rectangle clip_rectangle, ProgressBar progress_bar) {
1866                         Rectangle       client_area             = progress_bar.client_area;
1867                         Rectangle       paint_area              = progress_bar.paint_area;
1868                         int             steps                   = (progress_bar.Maximum - progress_bar.Minimum) / progress_bar.step;
1869                         int             space_betweenblocks     = 2;
1870                         int             x;
1871                         int             block_width;
1872                         int             increment;
1873                         int             barpos_pixels;
1874
1875                         x = client_area.X;
1876                         block_width = ((client_area.Height) * 2 ) / 3;
1877                         barpos_pixels = ((progress_bar.Value - progress_bar.Minimum) * client_area.Width) / (progress_bar.Maximum - progress_bar.Minimum);
1878                         increment = block_width + space_betweenblocks;
1879
1880                         /* Draw border */
1881                         CPDrawBorder3D (dc, progress_bar.paint_area, Border3DStyle.SunkenInner, Border3DSide.All, ColorButtonFace);
1882                         
1883                         /* Draw Blocks */
1884                         while ((x - client_area.X) < barpos_pixels) {
1885                                 dc.FillRectangle (ResPool.GetSolidBrush (progressbarblock_color), x, client_area.Y, block_width, client_area.Height);
1886                                 x  = x + increment;
1887                         }
1888                 }
1889                 
1890                 public override Size ProgressBarDefaultSize {
1891                         get {
1892                                 return new Size (100, 23);
1893                         }
1894                 }
1895
1896                 #endregion      // ProgressBar
1897
1898                 #region RadioButton
1899                 public override void DrawRadioButton (Graphics dc, Rectangle clip_rectangle, RadioButton radio_button) {
1900                         StringFormat    text_format;
1901                         Rectangle       client_rectangle;
1902                         Rectangle       text_rectangle;
1903                         Rectangle       radiobutton_rectangle;
1904                         SolidBrush      sb;
1905                         int             radiobutton_size = 12;
1906                         int     radiobutton_space = 4;
1907
1908                         client_rectangle = radio_button.ClientRectangle;
1909                         text_rectangle = client_rectangle;
1910                         radiobutton_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, radiobutton_size, radiobutton_size);
1911
1912                         text_format = new StringFormat();
1913                         text_format.Alignment = StringAlignment.Near;
1914                         text_format.LineAlignment = StringAlignment.Center;
1915
1916                         /* Calculate the position of text and checkbox rectangle */
1917                         if (radio_button.appearance!=Appearance.Button) {
1918                                 switch(radio_button.radiobutton_alignment) {
1919                                 case ContentAlignment.BottomCenter: {
1920                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
1921                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
1922                                         text_rectangle.X=client_rectangle.X;
1923                                         text_rectangle.Width=client_rectangle.Width;
1924                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
1925                                         break;
1926                                 }
1927
1928                                 case ContentAlignment.BottomLeft: {
1929                                         radiobutton_rectangle.X=client_rectangle.Left;
1930                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
1931                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
1932                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;                                 
1933                                         break;
1934                                 }
1935
1936                                 case ContentAlignment.BottomRight: {
1937                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
1938                                         radiobutton_rectangle.Y=client_rectangle.Bottom-radiobutton_size;
1939                                         text_rectangle.X=client_rectangle.X;
1940                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
1941                                         break;
1942                                 }
1943
1944                                 case ContentAlignment.MiddleCenter: {
1945                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
1946                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
1947                                         text_rectangle.X=client_rectangle.X;
1948                                         text_rectangle.Width=client_rectangle.Width;
1949                                         break;
1950                                 }
1951
1952                                 default:
1953                                 case ContentAlignment.MiddleLeft: {
1954                                         radiobutton_rectangle.X=client_rectangle.Left;
1955                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
1956                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
1957                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
1958                                         break;
1959                                 }
1960
1961                                 case ContentAlignment.MiddleRight: {
1962                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
1963                                         radiobutton_rectangle.Y=(client_rectangle.Bottom-client_rectangle.Top)/2-radiobutton_size/2;
1964                                         text_rectangle.X=client_rectangle.X;
1965                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
1966                                         break;
1967                                 }
1968
1969                                 case ContentAlignment.TopCenter: {
1970                                         radiobutton_rectangle.X=(client_rectangle.Right-client_rectangle.Left)/2-radiobutton_size/2;
1971                                         radiobutton_rectangle.Y=client_rectangle.Top;
1972                                         text_rectangle.X=client_rectangle.X;
1973                                         text_rectangle.Y=radiobutton_size+radiobutton_space;
1974                                         text_rectangle.Width=client_rectangle.Width;
1975                                         text_rectangle.Height=client_rectangle.Height-radiobutton_size-radiobutton_space;
1976                                         break;
1977                                 }
1978
1979                                 case ContentAlignment.TopLeft: {
1980                                         radiobutton_rectangle.X=client_rectangle.Left;
1981                                         radiobutton_rectangle.Y=client_rectangle.Top;
1982                                         text_rectangle.X=client_rectangle.X+radiobutton_size+radiobutton_space;
1983                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
1984                                         break;
1985                                 }
1986
1987                                 case ContentAlignment.TopRight: {
1988                                         radiobutton_rectangle.X=client_rectangle.Right-radiobutton_size;
1989                                         radiobutton_rectangle.Y=client_rectangle.Top;
1990                                         text_rectangle.X=client_rectangle.X;
1991                                         text_rectangle.Width=client_rectangle.Width-radiobutton_size-radiobutton_space;
1992                                         break;
1993                                 }
1994                                 }
1995                         } else {
1996                                 text_rectangle.X=client_rectangle.X;
1997                                 text_rectangle.Width=client_rectangle.Width;
1998                         }
1999                         
2000                         /* Set the horizontal alignment of our text */
2001                         switch(radio_button.text_alignment) {
2002                                 case ContentAlignment.BottomLeft:
2003                                 case ContentAlignment.MiddleLeft:
2004                                 case ContentAlignment.TopLeft: {
2005                                         text_format.Alignment=StringAlignment.Near;
2006                                         break;
2007                                 }
2008
2009                                 case ContentAlignment.BottomCenter:
2010                                 case ContentAlignment.MiddleCenter:
2011                                 case ContentAlignment.TopCenter: {
2012                                         text_format.Alignment=StringAlignment.Center;
2013                                         break;
2014                                 }
2015
2016                                 case ContentAlignment.BottomRight:
2017                                 case ContentAlignment.MiddleRight:
2018                                 case ContentAlignment.TopRight: {
2019                                         text_format.Alignment=StringAlignment.Far;
2020                                         break;
2021                                 }
2022                         }
2023
2024                         /* Set the vertical alignment of our text */
2025                         switch(radio_button.text_alignment) {
2026                                 case ContentAlignment.TopLeft: 
2027                                 case ContentAlignment.TopCenter: 
2028                                 case ContentAlignment.TopRight: {
2029                                         text_format.LineAlignment=StringAlignment.Near;
2030                                         break;
2031                                 }
2032
2033                                 case ContentAlignment.BottomLeft:
2034                                 case ContentAlignment.BottomCenter:
2035                                 case ContentAlignment.BottomRight: {
2036                                         text_format.LineAlignment=StringAlignment.Far;
2037                                         break;
2038                                 }
2039
2040                                 case ContentAlignment.MiddleLeft:
2041                                 case ContentAlignment.MiddleCenter:
2042                                 case ContentAlignment.MiddleRight: {
2043                                         text_format.LineAlignment=StringAlignment.Center;
2044                                         break;
2045                                 }
2046                         }
2047
2048                         ButtonState state = ButtonState.Normal;
2049                         if (radio_button.FlatStyle == FlatStyle.Flat) {
2050                                 state |= ButtonState.Flat;
2051                         }
2052                         
2053                         if (radio_button.Checked) {
2054                                 state |= ButtonState.Checked;
2055                         }
2056
2057                         // Start drawing
2058
2059                         sb=new SolidBrush(radio_button.BackColor);
2060                         dc.FillRectangle(sb, radio_button.ClientRectangle);
2061                         sb.Dispose();
2062                         
2063                 
2064                         
2065                         if (radio_button.appearance==Appearance.Button) {
2066                                 if (radio_button.FlatStyle == FlatStyle.Flat || radio_button.FlatStyle == FlatStyle.Popup) {
2067                                         DrawFlatStyleButton(dc, radio_button.ClientRectangle, radio_button);
2068                                 } else {                                
2069                                         CPDrawButton(dc, radio_button.ClientRectangle, state);
2070                                 }               
2071                         } else {
2072                                 // establish if we are rendering a flat style of some sort
2073                                 if (radio_button.FlatStyle == FlatStyle.Flat || radio_button.FlatStyle == FlatStyle.Popup) {
2074                                         DrawFlatStyleRadioButton (dc, radiobutton_rectangle, radio_button);
2075                                 } else {
2076                                         ControlPaint.DrawRadioButton (dc, radiobutton_rectangle, state);
2077                                 }
2078                         }
2079                         
2080                         // offset the text if it's pressed and a button
2081                         if (radio_button.Appearance == Appearance.Button) {
2082                                 if (radio_button.Checked || (radio_button.Capture && radio_button.FlatStyle != FlatStyle.Flat)) {
2083                                         text_rectangle.X ++;
2084                                         text_rectangle.Y ++;
2085                                 }
2086                                 
2087                                 text_rectangle.Inflate(-4,-4);
2088                         } 
2089                         
2090                         /* Place the text; to be compatible with Windows place it after the radiobutton has been drawn */
2091                         sb=new SolidBrush(radio_button.ForeColor);
2092                         dc.DrawString (radio_button.Text, radio_button.Font, sb, text_rectangle, text_format);
2093                         sb.Dispose();
2094                         if (radio_button.Enabled) {
2095                                 sb = ResPool.GetSolidBrush(radio_button.ForeColor);
2096                                 dc.DrawString(radio_button.Text, radio_button.Font, sb, text_rectangle, text_format);                           
2097                         } else if (radio_button.FlatStyle == FlatStyle.Flat) {
2098                                 dc.DrawString(radio_button.Text, radio_button.Font, ResPool.GetSolidBrush (ControlPaint.DarkDark (this.ColorButtonFace)), text_rectangle, text_format);
2099                         } else {
2100                                 CPDrawStringDisabled(dc, radio_button.Text, radio_button.Font, this.ColorButtonText, text_rectangle, text_format);
2101                         }
2102
2103                         if (radio_button.Focused) {
2104                                 if (radio_button.FlatStyle != FlatStyle.Flat) {
2105                                         DrawInnerFocusRectangle (dc, text_rectangle, radio_button.BackColor);
2106                                 } else {
2107                                         dc.DrawRectangle (ResPool.GetPen (radio_button.ForeColor), text_rectangle);
2108                                 }
2109                         }                       
2110                 }
2111
2112                 // renders a radio button with the Flat and Popup FlatStyle
2113                 private void DrawFlatStyleRadioButton (Graphics graphics, Rectangle rectangle, RadioButton radio_button)
2114                 {
2115                         int     lineWidth;
2116                         
2117                         if (radio_button.Enabled) {
2118                                 // draw the outer flatstyle arcs
2119                                 if (radio_button.FlatStyle == FlatStyle.Flat) {
2120                                         graphics.DrawArc (ResPool.GetPen (radio_button.ForeColor), rectangle, 0, 359);
2121                                         
2122                                         // fill in the area depending on whether or not the mouse is hovering
2123                                         if (radio_button.is_entered && radio_button.Capture) {
2124                                                 graphics.FillPie (ResPool.GetSolidBrush (ControlPaint.Light (radio_button.BackColor)), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2125                                         } else {
2126                                                 graphics.FillPie (ResPool.GetSolidBrush (ControlPaint.LightLight (radio_button.BackColor)), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2127                                         }
2128                                 } else {
2129                                         // must be a popup radio button
2130                                         // fill the control
2131                                         graphics.FillPie (ResPool.GetSolidBrush (ControlPaint.LightLight (radio_button.BackColor)), rectangle, 0, 359);
2132
2133                                         if (radio_button.is_entered || radio_button.Capture) {
2134                                                 // draw the popup 3d button knob
2135                                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Light (radio_button.BackColor)), rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 0, 359);
2136
2137                                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Dark (radio_button.BackColor)), rectangle, 135, 180);
2138                                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.LightLight (radio_button.BackColor)), rectangle, 315, 180);
2139                                                 
2140                                         } else {
2141                                                 // just draw lighter flatstyle outer circle
2142                                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Dark (this.ColorButtonFace)), rectangle, 0, 359);                                                
2143                                         }                                                                               
2144                                 }
2145                         } else {
2146                                 // disabled
2147                                 // fill control background color regardless of actual backcolor
2148                                 graphics.FillPie (ResPool.GetSolidBrush (this.ColorButtonFace), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
2149                                 // draw the ark as control dark
2150                                 graphics.DrawArc (ResPool.GetPen (ControlPaint.Dark(this.ColorButtonFace)), rectangle, 0, 359);
2151                         }
2152
2153                         // draw the check
2154                         lineWidth = Math.Max (1, Math.Min(rectangle.Width, rectangle.Height)/3);
2155                         if (radio_button.Checked) {
2156                                 SolidBrush buttonBrush;
2157
2158                                 if (!radio_button.Enabled) {
2159                                         buttonBrush = ResPool.GetSolidBrush (ControlPaint.Dark (this.ColorButtonFace));
2160                                 } else if (radio_button.FlatStyle == FlatStyle.Popup && radio_button.is_entered && radio_button.Capture) {
2161                                         buttonBrush = ResPool.GetSolidBrush (this.ColorButtonText);
2162                                 } else {
2163                                         buttonBrush = ResPool.GetSolidBrush (radio_button.ForeColor);
2164                                 }
2165                                 graphics.FillPie (buttonBrush, rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2, 0, 359);
2166                         }       
2167                 }
2168
2169                 public override Size RadioButtonDefaultSize {
2170                         get {
2171                                 return new Size (104,24);
2172                         }
2173                 }
2174                 #endregion      // RadioButton
2175
2176                 #region ScrollBar
2177                 public override void DrawScrollBar(Graphics dc, Rectangle clip_rectangle, ScrollBar bar) {
2178                         int             scrollbutton_width = bar.scrollbutton_width;
2179                         int             scrollbutton_height = bar.scrollbutton_height;
2180                         Rectangle       first_arrow_area;
2181                         Rectangle       second_arrow_area;                      
2182                         Rectangle       thumb_pos;
2183                         
2184                         thumb_pos = bar.ThumbPos;
2185
2186                         if (bar.vert) {
2187                                 first_arrow_area = new Rectangle(0, 0, bar.Width, scrollbutton_height);
2188                                 bar.FirstArrowArea = first_arrow_area;
2189
2190                                 second_arrow_area = new Rectangle(0, bar.ClientRectangle.Height - scrollbutton_height, bar.Width, scrollbutton_height);
2191                                 bar.SecondArrowArea = second_arrow_area;
2192
2193                                 thumb_pos.Width = bar.Width;
2194                                 bar.ThumbPos = thumb_pos;
2195
2196                                 /* Buttons */
2197                                 CPDrawScrollButton (dc, first_arrow_area, ScrollButton.Up, bar.firstbutton_state);
2198                                 CPDrawScrollButton (dc, second_arrow_area, ScrollButton.Down, bar.secondbutton_state);                          
2199
2200                                 /* Background */
2201                                 switch (bar.thumb_moving) {
2202                                 case ScrollBar.ThumbMoving.None: {
2203                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace), 0,  
2204                                                 scrollbutton_height, bar.ClientRectangle.Width, bar.ClientRectangle.Height - (scrollbutton_height * 2));
2205                                         
2206                                         break;
2207                                 }
2208                                 case ScrollBar.ThumbMoving.Forward: {
2209                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace),
2210                                                 0,  scrollbutton_height,
2211                                                 bar.ClientRectangle.Width, thumb_pos.Y - scrollbutton_height);
2212                                                                                                 
2213                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63,63,63), Color.Black),
2214                                                 0, thumb_pos.Y + thumb_pos.Height,
2215                                                 bar.ClientRectangle.Width, bar.ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) - scrollbutton_height);
2216                                                 
2217                                         break;
2218                                 }
2219                                 
2220                                 case ScrollBar.ThumbMoving.Backwards: {
2221                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63,63,63), Color.Black),
2222                                                 0,  scrollbutton_height,
2223                                                 bar.ClientRectangle.Width, thumb_pos.Y - scrollbutton_height);
2224                                                                                                 
2225                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace),
2226                                                 0, thumb_pos.Y + thumb_pos.Height,
2227                                                 bar.ClientRectangle.Width, bar.ClientRectangle.Height -  (thumb_pos.Y + thumb_pos.Height) - scrollbutton_height);                                               
2228                                                 
2229                                         break;
2230                                 }
2231                                 
2232                                 default:
2233                                         break;
2234                                 }
2235                                         
2236                         }
2237                         else {
2238                                 first_arrow_area = new Rectangle(0, 0, scrollbutton_width, bar.Height);
2239                                 bar.FirstArrowArea = first_arrow_area;
2240
2241                                 second_arrow_area = new Rectangle (bar.ClientRectangle.Width - scrollbutton_width, 0, scrollbutton_width, bar.Height);
2242                                 bar.SecondArrowArea = second_arrow_area;
2243
2244                                 thumb_pos.Height = bar.Height;
2245                                 bar.ThumbPos = thumb_pos;
2246
2247                                 /* Buttons */
2248                                 CPDrawScrollButton (dc, bar.FirstArrowArea, ScrollButton.Left, bar.firstbutton_state);
2249                                 CPDrawScrollButton (dc, bar.SecondArrowArea, ScrollButton.Right, bar.secondbutton_state);
2250
2251                                 /* Background */                                        
2252                                 switch (bar.thumb_moving) {
2253                                 case ScrollBar.ThumbMoving.None: {
2254                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace), scrollbutton_width,
2255                                                 0, bar.ClientRectangle.Width - (scrollbutton_width * 2), bar.ClientRectangle.Height);
2256                                         
2257                                         break;
2258                                 }
2259                                 
2260                                 case ScrollBar.ThumbMoving.Forward: {
2261                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace),
2262                                                 scrollbutton_width,  0,
2263                                                 thumb_pos.X - scrollbutton_width, bar.ClientRectangle.Height);
2264                                                                                                 
2265                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63,63,63), Color.Black),
2266                                                 thumb_pos.X + thumb_pos.Width, 0,
2267                                                 bar.ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) - scrollbutton_width, bar.ClientRectangle.Height);
2268                                                 
2269                                         break;
2270                                 }
2271                                 
2272                                 case ScrollBar.ThumbMoving.Backwards: {
2273                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, Color.FromArgb (255, 63,63,63), Color.Black),
2274                                                 scrollbutton_width,  0,
2275                                                 thumb_pos.X - scrollbutton_width, bar.ClientRectangle.Height);
2276                                                                                                 
2277                                         dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace),
2278                                                 thumb_pos.X + thumb_pos.Width, 0,
2279                                                 bar.ClientRectangle.Width -  (thumb_pos.X + thumb_pos.Width) - scrollbutton_width, bar.ClientRectangle.Height);
2280
2281                                                 
2282                                         break;
2283                                 }
2284                                 
2285                                 default:
2286                                         break;
2287                                 }
2288                         }
2289
2290                         /* Thumb */
2291                         if (bar.Enabled && thumb_pos.Width > 0 && thumb_pos.Height > 0)
2292                                 DrawScrollButtonPrimitive (dc, thumb_pos, ButtonState.Normal);                          
2293                 }
2294
2295                 public override int ScrollBarButtonSize {
2296                         get { return 16; }
2297                 }
2298                 #endregion      // ScrollBar
2299
2300                 #region StatusBar
2301                 public  override void DrawStatusBar (Graphics dc, Rectangle clip_rectangle, StatusBar sb) {
2302                         Rectangle       area        = sb.paint_area;
2303                         int             horz_border = 2;
2304                         int             vert_border = 2;
2305
2306                         dc.FillRectangle (GetControlBackBrush (sb.BackColor), area);
2307                         
2308                         if (sb.ShowPanels && sb.Panels.Count == 0) {
2309                                 // Create a default panel.
2310                                 SolidBrush br_forecolor = GetControlForeBrush (sb.ForeColor);
2311                                 
2312                                 StatusBarPanel panel = new StatusBarPanel ();
2313                                 Rectangle new_area = new Rectangle (area.X + horz_border,
2314                                         area.Y + horz_border,
2315                                         area.Width - StatusBarSizeGripWidth - horz_border,
2316                                         area.Height - horz_border);
2317                                 DrawStatusBarPanel (dc, new_area, -1, br_forecolor, panel);
2318                         } else if (sb.ShowPanels) {
2319                                 SolidBrush br_forecolor = GetControlForeBrush (sb.ForeColor);
2320                                 int prev_x = area.X + horz_border;
2321                                 int y = area.Y + vert_border;
2322                                 for (int i = 0; i < sb.Panels.Count; i++) {
2323                                         Rectangle pr = new Rectangle (prev_x, y,
2324                                                 sb.Panels [i].Width, area.Height);
2325                                         prev_x += pr.Width + StatusBarHorzGapWidth;
2326                                         DrawStatusBarPanel (dc, pr, i, br_forecolor, sb.Panels [i]);
2327                                 }
2328                         }
2329
2330                         if (sb.SizingGrip)
2331                                 CPDrawSizeGrip (dc, ColorButtonFace, area);
2332
2333                 }
2334
2335
2336                 private void DrawStatusBarPanel (Graphics dc, Rectangle area, int index,
2337                         SolidBrush br_forecolor, StatusBarPanel panel) {
2338                         int border_size = 3; // this is actually const, even if the border style is none
2339
2340                         area.Height -= border_size;
2341                         if (panel.BorderStyle != StatusBarPanelBorderStyle.None) {
2342                                 Border3DStyle border_style = Border3DStyle.SunkenInner;
2343                                 if (panel.BorderStyle == StatusBarPanelBorderStyle.Raised)
2344                                         border_style = Border3DStyle.RaisedOuter;
2345                                 CPDrawBorder3D(dc, area, border_style, Border3DSide.All, ColorButtonFace);
2346                         }
2347
2348                         if (panel.Style == StatusBarPanelStyle.OwnerDraw) {
2349                                 StatusBarDrawItemEventArgs e = new StatusBarDrawItemEventArgs (
2350                                         dc, panel.Parent.Font, area, index, DrawItemState.Default,
2351                                         panel, panel.Parent.ForeColor, panel.Parent.BackColor);
2352                                 panel.Parent.OnDrawItemInternal (e);
2353                                 return;
2354                         }
2355
2356                         int left = area.Left;
2357                         if (panel.Icon != null) {
2358                                 left += 2;
2359                                 int size = area.Height - border_size;
2360                                 Rectangle ia = new Rectangle (left, border_size, size, size);
2361                                 dc.DrawIcon (panel.Icon, left, area.Top);
2362                                 left += panel.Icon.Width;
2363                         }
2364
2365                         if (panel.Text == String.Empty)
2366                                 return;
2367
2368                         string text = panel.Text;
2369                         StringFormat string_format = new StringFormat ();
2370                         string_format.LineAlignment = StringAlignment.Center;
2371                         string_format.Alignment = StringAlignment.Near;
2372                         string_format.FormatFlags = StringFormatFlags.NoWrap;
2373
2374                         if (text [0] == '\t') {
2375                                 string_format.Alignment = StringAlignment.Center;
2376                                 text = text.Substring (1);
2377                                 if (text [0] == '\t') {
2378                                         string_format.Alignment = StringAlignment.Far;
2379                                         text = text.Substring (1);
2380                                 }
2381                         }
2382
2383                         float x = left + border_size;
2384                         float y = ((area.Bottom - area.Top) / 2.0F) + border_size;
2385
2386                         dc.DrawString (text, panel.Parent.Font, br_forecolor, x, y, string_format);
2387                 }
2388
2389                 public override int StatusBarSizeGripWidth {
2390                         get { return 15; }
2391                 }
2392
2393                 public override int StatusBarHorzGapWidth {
2394                         get { return 3; }
2395                 }
2396
2397                 public override Size StatusBarDefaultSize {
2398                         get {
2399                                 return new Size (100, 22);
2400                         }
2401                 }
2402                 #endregion      // StatusBar
2403
2404                 public override void DrawTabControl (Graphics dc, Rectangle area, TabControl tab)
2405                 {
2406                         // Do we need to fill the back color? It can't be changed...
2407                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), area);
2408                         Rectangle panel_rect = GetTabPanelRectExt (tab);
2409
2410                         if (tab.Appearance == TabAppearance.Normal) {
2411                                 CPDrawBorder3D (dc, panel_rect, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top, ColorButtonFace);
2412                                 CPDrawBorder3D (dc, panel_rect, Border3DStyle.Raised, Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
2413                         }
2414
2415                         if (tab.Alignment == TabAlignment.Top) {
2416                                 for (int r = tab.TabPages.Count; r > 0; r--) {
2417                                         for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
2418                                                 if (i == tab.SelectedIndex)
2419                                                         continue;
2420                                                 if (r != tab.TabPages [i].Row)
2421                                                         continue;
2422                                                 Rectangle rect = tab.GetTabRect (i);
2423                                                 if (!rect.IntersectsWith (area))
2424                                                         continue;
2425                                                 DrawTab (dc, tab.TabPages [i], tab, rect, false);
2426                                         }
2427                                 }
2428                         } else {
2429                                 for (int r = 0; r < tab.TabPages.Count; r++) {
2430                                         for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
2431                                                 if (i == tab.SelectedIndex)
2432                                                         continue;
2433                                                 if (r != tab.TabPages [i].Row)
2434                                                         continue;
2435                                                 Rectangle rect = tab.GetTabRect (i);
2436                                                 if (!rect.IntersectsWith (area))
2437                                                         continue;
2438                                                 DrawTab (dc, tab.TabPages [i], tab, rect, false);
2439                                         }
2440                                 }
2441                         }
2442
2443                         if (tab.SelectedIndex != -1 && tab.SelectedIndex >= tab.SliderPos) {
2444                                 Rectangle rect = tab.GetTabRect (tab.SelectedIndex);
2445                                 if (rect.IntersectsWith (area))
2446                                         DrawTab (dc, tab.TabPages [tab.SelectedIndex], tab, rect, true);
2447                         }
2448
2449                         if (tab.ShowSlider) {
2450                                 Rectangle right = GetTabControlRightScrollRect (tab);
2451                                 Rectangle left = GetTabControlLeftScrollRect (tab);
2452                                 CPDrawScrollButton (dc, right, ScrollButton.Right, tab.RightSliderState);
2453                                 CPDrawScrollButton (dc, left, ScrollButton.Left, tab.LeftSliderState);
2454                         }
2455                 }
2456
2457                 public override Rectangle GetTabControlLeftScrollRect (TabControl tab)
2458                 {
2459                         switch (tab.Alignment) {
2460                         case TabAlignment.Top:
2461                                 return new Rectangle (tab.ClientRectangle.Right - 34, tab.ClientRectangle.Top + 1, 17, 17);
2462                         default:
2463                                 Rectangle panel_rect = GetTabPanelRectExt (tab);
2464                                 return new Rectangle (tab.ClientRectangle.Right - 34, panel_rect.Bottom + 2, 17, 17);
2465                         }
2466                 }
2467
2468                 public override Rectangle GetTabControlRightScrollRect (TabControl tab)
2469                 {
2470                         switch (tab.Alignment) {
2471                         case TabAlignment.Top:
2472                                 return new Rectangle (tab.ClientRectangle.Right - 17, tab.ClientRectangle.Top + 1, 17, 17);
2473                         default:
2474                                 Rectangle panel_rect = GetTabPanelRectExt (tab);
2475                                 return new Rectangle (tab.ClientRectangle.Right - 17, panel_rect.Bottom + 2, 17, 17);
2476                         }
2477                 }
2478
2479                 public override Size TabControlDefaultItemSize {
2480                         get { return new Size (42, 21); }
2481                 }
2482
2483                 public override Point TabControlDefaultPadding {
2484                         get { return new Point (6, 3); }
2485                 }
2486
2487                 public override int TabControlMinimumTabWidth {
2488                         get { return 42; }
2489                 }
2490
2491                 public override Rectangle GetTabControlDisplayRectangle (TabControl tab)
2492                 {
2493                         Rectangle ext = GetTabPanelRectExt (tab);
2494                         // Account for border size
2495                         return new Rectangle (ext.Left + 2, ext.Top + 1, ext.Width - 6, ext.Height - 4);
2496                 }
2497
2498                 public override Size TabControlGetSpacing (TabControl tab) {
2499                         switch (tab.Appearance) {
2500                                 case TabAppearance.Normal:
2501                                         return new Size (1, -2);
2502                                 case TabAppearance.Buttons:
2503                                         return new Size (3, 3);
2504                                 case TabAppearance.FlatButtons:
2505                                         return new Size (9, 3);
2506                                 default:
2507                                         throw new Exception ("Invalid Appearance value: " + tab.Appearance);
2508                                 }
2509                 }
2510
2511                 private Rectangle GetTabPanelRectExt (TabControl tab)
2512                 {
2513                         // Offset the tab from the top corner
2514                         Rectangle res = new Rectangle (tab.ClientRectangle.X + 2,
2515                                         tab.ClientRectangle.Y,
2516                                         tab.ClientRectangle.Width - 2,
2517                                         tab.ClientRectangle.Height - 1);
2518
2519                         if (tab.TabCount == 0)
2520                                 return res;
2521
2522                         int spacing = TabControlGetSpacing (tab).Height;
2523                         int offset = (tab.ItemSize.Height + spacing) * tab.RowCount + 3;
2524
2525                         switch (tab.Alignment) {
2526                         case TabAlignment.Left:
2527                                 res.X += offset;
2528                                 res.Width -= offset;
2529                                 break;
2530                         case TabAlignment.Right:
2531                                 res.Width -= offset;
2532                                 break;
2533                         case TabAlignment.Top:
2534                                 res.Y += offset;
2535                                 res.Height -= offset;
2536                                 break;
2537                         case TabAlignment.Bottom:
2538                                 res.Height -= offset;
2539                                 break;
2540                         }
2541
2542                         return res;
2543                 }
2544
2545                 private int DrawTab (Graphics dc, TabPage page, TabControl tab, Rectangle bounds, bool is_selected)
2546                 {
2547                         int FlatButtonSpacing = 8;                      
2548                         Rectangle interior;
2549                         int res = bounds.Width;
2550
2551                         // we can't fill the background right away because the bounds might be adjusted if the tab is selected
2552
2553                         if (tab.Appearance == TabAppearance.Buttons || tab.Appearance == TabAppearance.FlatButtons) {
2554
2555                                 dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
2556
2557                                 // Separators
2558                                 if (tab.Appearance == TabAppearance.FlatButtons) {
2559                                         int width = bounds.Width;
2560                                         bounds.Width += (FlatButtonSpacing - 2);
2561                                         res = bounds.Width;
2562                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Etched, Border3DSide.Right);
2563                                         bounds.Width = width;
2564                                 }
2565
2566                                 if (is_selected) {
2567                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Sunken, Border3DSide.All);
2568                                 } else if (tab.Appearance != TabAppearance.FlatButtons) {
2569                                         CPDrawBorder3D (dc, bounds, Border3DStyle.Raised, Border3DSide.All);
2570                                 }
2571
2572                                 interior = new Rectangle (bounds.Left + 2, bounds.Top + 2, bounds.Width - 4, bounds.Height - 4);
2573                         } else {
2574                                 Pen light = ResPool.GetPen (ControlPaint.LightLight (tab.BackColor));
2575
2576                                 switch (tab.Alignment) {
2577                                         
2578                                 case TabAlignment.Top:
2579
2580
2581                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
2582
2583                                         dc.DrawLine (light, bounds.Left, bounds.Bottom, bounds.Left, bounds.Top + 3);
2584                                         dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
2585                                         dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right - 3, bounds.Top);
2586
2587                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom);
2588                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 1, bounds.Top + 2, bounds.Right, bounds.Top + 3);
2589                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom);
2590
2591                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
2592
2593                                         if (page.Text != String.Empty) {
2594                                                 StringFormat string_format = new StringFormat ();
2595                                                 string_format.Alignment = StringAlignment.Center;
2596                                                 string_format.LineAlignment = StringAlignment.Center;
2597                                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
2598                                                 interior.Y++;
2599                                                 dc.DrawString (page.Text, page.Font, new SolidBrush (SystemColors.ControlText), interior, string_format);
2600                                         }
2601
2602                                         break;
2603
2604                                 case TabAlignment.Bottom:
2605
2606                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
2607
2608                                         dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 3);
2609                                         dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left + 2, bounds.Bottom - 1);
2610
2611                                         dc.DrawLine (SystemPens.ControlDark, bounds.Left + 3, bounds.Bottom - 1, bounds.Right - 3, bounds.Bottom - 1);
2612                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Bottom - 3, bounds.Right - 1, bounds.Top);
2613
2614                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 3, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
2615                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 3, bounds.Bottom, bounds.Right, bounds.Bottom - 3);
2616                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom - 3, bounds.Right, bounds.Top);
2617
2618                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
2619
2620                                         if (page.Text != String.Empty) {
2621                                                 StringFormat string_format = new StringFormat ();
2622                                                 string_format.Alignment = StringAlignment.Center;
2623                                                 string_format.LineAlignment = StringAlignment.Center;
2624                                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
2625                                                 interior.Y++;
2626                                                 dc.DrawString (page.Text, page.Font, new SolidBrush (SystemColors.ControlText), interior, string_format);
2627                                         }
2628
2629                                         break;
2630
2631                                 case TabAlignment.Left:
2632
2633                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
2634
2635                                         dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left, bounds.Top + 3);
2636                                         dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
2637                                         dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right, bounds.Top);
2638
2639                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right, bounds.Bottom - 1, bounds.Left + 2, bounds.Bottom - 1);
2640
2641                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom, bounds.Left + 2, bounds.Bottom);
2642                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 2, bounds.Bottom, bounds.Left, bounds.Bottom - 3);
2643
2644                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
2645
2646                                         if (page.Text != String.Empty) {
2647                                                 StringFormat string_format = new StringFormat ();
2648                                                 // Flip the text around
2649                                                 string_format.Alignment = StringAlignment.Center;
2650                                                 string_format.LineAlignment = StringAlignment.Center;
2651                                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
2652                                                 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2653                                                 int wo = interior.Width / 2;
2654                                                 int ho = interior.Height / 2;
2655                                                 dc.TranslateTransform (interior.X + wo, interior.Y + ho);
2656                                                 dc.RotateTransform (180);
2657                                                 dc.DrawString (page.Text, page.Font, new SolidBrush (SystemColors.ControlText), 0, 0, string_format);
2658                                                 dc.ResetTransform ();
2659                                         }
2660
2661                                         break;
2662
2663                                 default:
2664                                         // TabAlignment.Right
2665
2666                                         dc.FillRectangle (GetControlBackBrush (tab.BackColor), bounds);
2667
2668                                         dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Right - 3, bounds.Top);
2669                                         dc.DrawLine (light, bounds.Right - 3, bounds.Top, bounds.Right, bounds.Top + 3);
2670
2671                                         dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);
2672                                         dc.DrawLine (SystemPens.ControlDark, bounds.Left, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
2673
2674                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
2675                                         dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
2676
2677                                         interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
2678
2679                                         if (page.Text != String.Empty) {
2680                                                 StringFormat string_format = new StringFormat ();
2681                                                 string_format.Alignment = StringAlignment.Center;
2682                                                 string_format.LineAlignment = StringAlignment.Center;
2683                                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
2684                                                 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2685                                                 interior.X++;
2686                                                 dc.DrawString (page.Text, page.Font, new SolidBrush (SystemColors.ControlText), interior, string_format);
2687                                         }
2688
2689                                         break;
2690                                 }
2691                         }
2692
2693                         
2694                         if (page.Focused) {
2695                                 using (Pen pen = new Pen (Color.Black, 1) ) {
2696                                         pen.DashStyle = DashStyle.Dot;
2697                                         dc.DrawRectangle (pen, interior);
2698                                 }
2699                         }
2700
2701                         return res;
2702                 }
2703
2704                 #region ToolBar
2705                 public  override void DrawToolBar (Graphics dc, Rectangle clip_rectangle, ToolBar control) {
2706                         StringFormat    format = new StringFormat ();
2707                         format.Trimming = StringTrimming.EllipsisWord;
2708                         if (control.textAlignment == ToolBarTextAlign.Underneath) {
2709                                 format.LineAlignment = StringAlignment.Center;
2710                                 format.Alignment = StringAlignment.Center;
2711                         } else {
2712                                 format.LineAlignment = StringAlignment.Center;
2713                                 format.Alignment = StringAlignment.Near;
2714                         }
2715                         
2716                         // Exclude the area for divider
2717                         Rectangle paint_area = new Rectangle (0, ToolBarGripWidth / 2, 
2718                                 control.Width, control.Height - ToolBarGripWidth / 2);
2719                         bool flat = (control.Appearance == ToolBarAppearance.Flat);
2720                         dc.FillRectangle (SystemBrushes.Control, paint_area);
2721                         CPDrawBorderStyle (dc, paint_area, control.BorderStyle);
2722
2723                         if (control.Divider)
2724                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), 0, 0, paint_area.Width, 0);
2725
2726                         foreach (ToolBarButton button in control.Buttons) {
2727
2728                                 Image image = null;
2729                                 Rectangle buttonArea = button.Rectangle;
2730                                 Rectangle imgRect = Rectangle.Empty;  // rect to draw the image
2731                                 Rectangle txtRect = buttonArea;       // rect to draw the text
2732                                 Rectangle ddRect = Rectangle.Empty;   // rect for the drop down arrow
2733
2734                                 // calculate different rects and draw the frame if its not separator button
2735                                 if (button.Style != ToolBarButtonStyle.Separator) {
2736                                         /* Adjustment for drop down arrow */
2737                                         if (button.Style == ToolBarButtonStyle.DropDownButton && control.DropDownArrows) {
2738                                                 ddRect.X = buttonArea.X + buttonArea.Width - this.ToolBarDropDownWidth;
2739                                                 ddRect.Y = buttonArea.Y;
2740                                                 ddRect.Width = this.ToolBarDropDownWidth;
2741                                                 ddRect.Height = buttonArea.Height;
2742                                         }
2743
2744                                         // calculate txtRect and imgRect, if imageIndex and imageList are present
2745                                         if (button.ImageIndex > -1 && control.ImageList != null) {
2746                                                 if (button.ImageIndex < control.ImageList.Images.Count)
2747                                                         image = control.ImageList.Images [button.ImageIndex];
2748                                                 // draw the image at the centre if textalignment is underneath
2749                                                 if (control.TextAlign == ToolBarTextAlign.Underneath) {
2750                                                         imgRect.X = buttonArea.X + ((buttonArea.Width - ddRect.Width 
2751                                                                 - control.ImageSize.Width) / 2) 
2752                                                                 + this.ToolBarImageGripWidth;
2753                                                         imgRect.Y = buttonArea.Y + this.ToolBarImageGripWidth;
2754                                                         imgRect.Width = control.ImageSize.Width;
2755                                                         imgRect.Height = control.ImageSize.Height;
2756
2757                                                         txtRect.X = buttonArea.X;
2758                                                         txtRect.Y = buttonArea.Y + imgRect.Height + 2 * this.ToolBarImageGripWidth;
2759                                                         txtRect.Width = buttonArea.Width - ddRect.Width;
2760                                                         txtRect.Height = buttonArea.Height - imgRect.Height 
2761                                                                 - 2 * this.ToolBarImageGripWidth;
2762                                                 }
2763                                                 else {
2764                                                         imgRect.X = buttonArea.X + this.ToolBarImageGripWidth;
2765                                                         imgRect.Y = buttonArea.Y + this.ToolBarImageGripWidth;
2766                                                         imgRect.Width = control.ImageSize.Width;
2767                                                         imgRect.Height = control.ImageSize.Height;
2768
2769                                                         txtRect.X = buttonArea.X + imgRect.Width + 2 * this.ToolBarImageGripWidth;
2770                                                         txtRect.Y = buttonArea.Y;
2771                                                         txtRect.Width = buttonArea.Width - imgRect.Width 
2772                                                                 - 2 * this.ToolBarImageGripWidth - ddRect.Width;
2773                                                         txtRect.Height = buttonArea.Height;
2774                                                 }
2775                                         }
2776                                         /* Draw the button frame, only if it is not a separator */
2777                                         if (flat) { 
2778                                                 if (button.Pushed || button.Pressed) {
2779                                                         CPDrawBorder3D (dc, buttonArea, Border3DStyle.SunkenOuter, Border3DSide.All, ColorButtonFace);
2780                                                 } else if (button.Hilight) {
2781                                                         dc.DrawRectangle (ResPool.GetPen (ColorButtonText), buttonArea);
2782                                                         if (! ddRect.IsEmpty) {
2783                                                                 dc.DrawLine (ResPool.GetPen (ColorButtonText), ddRect.X, ddRect.Y, ddRect.X, 
2784                                                                         ddRect.Y + ddRect.Height);
2785                                                                 buttonArea.Width -= this.ToolBarDropDownWidth;
2786                                                         }
2787                                                 }
2788                                         }
2789                                         else { // normal toolbar
2790                                                 if (button.Pushed || button.Pressed) {
2791                                                         CPDrawBorder3D (dc, buttonArea, Border3DStyle.SunkenInner,
2792                                                                 Border3DSide.All, ColorButtonFace);
2793                                                         if (! ddRect.IsEmpty) {
2794                                                                 CPDrawBorder3D (dc, ddRect, Border3DStyle.SunkenInner,
2795                                                                         Border3DSide.Left, ColorButtonFace);
2796                                                                 buttonArea.Width -= this.ToolBarDropDownWidth;
2797                                                         }
2798                                                 }
2799                                                 else {
2800                                                         CPDrawBorder3D (dc, buttonArea, Border3DStyle.RaisedInner,
2801                                                                 Border3DSide.All, ColorButtonFace);
2802                                                         if (! ddRect.IsEmpty) {
2803                                                                 CPDrawBorder3D (dc, ddRect, Border3DStyle.RaisedInner,
2804                                                                         Border3DSide.Left, ColorButtonFace);
2805                                                                 buttonArea.Width -= this.ToolBarDropDownWidth;
2806                                                         }
2807                                                 }
2808                                         }
2809                                 }
2810                                 DrawToolBarButton (dc, button, control.Font, format, paint_area, buttonArea,
2811                                         imgRect, image, txtRect, ddRect, flat);
2812                         }
2813                 }
2814
2815                 private void DrawToolBarButton (Graphics dc, ToolBarButton button, Font font, StringFormat format,
2816                         Rectangle controlArea, Rectangle buttonArea, Rectangle imgRect, 
2817                         Image image, Rectangle txtRect, Rectangle ddRect, bool flat) {
2818                         if (! button.Visible)
2819                                 return;
2820
2821                         switch (button.Style) {
2822
2823                         case ToolBarButtonStyle.Separator:
2824                                 // separator is drawn only in the case of flat appearance
2825                                 if (flat) {
2826                                         dc.DrawLine (ResPool.GetPen (ColorButtonShadow), buttonArea.X + 1, buttonArea.Y, 
2827                                                 buttonArea.X + 1, buttonArea.Height);
2828                                         dc.DrawLine (ResPool.GetPen (ColorButtonHilight), buttonArea.X + 1 + (int) ResPool.GetPen (ColorButtonFace).Width,
2829                                                 buttonArea.Y, buttonArea.X + 1 + (int) ResPool.GetPen (ColorButtonFace).Width, buttonArea.Height);
2830                                         /* draw a horizontal separator */
2831                                         if (button.Wrapper) {
2832                                                 int y = buttonArea.Height + this.ToolBarSeparatorWidth / 2;
2833                                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), 0, y, controlArea.Width, y);
2834                                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), 0, y + 1 + (int) ResPool.GetPen (ColorButtonFace).Width, controlArea.Width,
2835                                                         y + 1 + (int) ResPool.GetPen (ColorButtonFace).Width);
2836                                         }
2837                                 }
2838                                 break;
2839
2840                         case ToolBarButtonStyle.ToggleButton:
2841                                 Rectangle toggleArea = Rectangle.Empty;
2842                                 toggleArea.X = buttonArea.X + this.ToolBarImageGripWidth;
2843                                 toggleArea.Y = buttonArea.Y + this.ToolBarImageGripWidth;
2844                                 toggleArea.Width = buttonArea.Width - 2 * this.ToolBarImageGripWidth;
2845                                 toggleArea.Height = buttonArea.Height - 2 * this.ToolBarImageGripWidth;
2846                                 if (button.PartialPush && button.Pushed) {
2847                                         dc.FillRectangle (SystemBrushes.ControlLightLight, toggleArea);
2848                                         if (! imgRect.IsEmpty) {
2849                                                 if (button.Enabled && image != null)
2850                                                         button.Parent.ImageList.Draw (dc, imgRect.X, imgRect.Y, imgRect.Width, 
2851                                                                 imgRect.Height, button.ImageIndex);
2852                                                 else {
2853                                                         dc.FillRectangle (new SolidBrush (ColorGrayText), imgRect);
2854                                                         ControlPaint.DrawBorder3D (dc, imgRect, Border3DStyle.SunkenOuter,
2855                                                                 Border3DSide.Right | Border3DSide.Bottom);
2856                                                 }
2857                                         }
2858                                         if (button.Enabled)
2859                                                 dc.DrawString (button.Text, font, SystemBrushes.ControlText, txtRect, format);
2860                                         else
2861                                                 CPDrawStringDisabled (dc, button.Text, font, ColorButtonHilight, txtRect, format);
2862                                 }
2863
2864                                 else if (button.PartialPush) {
2865                                         dc.FillRectangle (SystemBrushes.ControlLight, toggleArea);
2866                                         if (! imgRect.IsEmpty) {
2867                                                 if (button.Enabled && image != null)
2868                                                         button.Parent.ImageList.Draw (dc, imgRect.X, imgRect.Y, imgRect.Width,
2869                                                                 imgRect.Height, button.ImageIndex);
2870                                                 else {
2871                                                         dc.FillRectangle (new SolidBrush (ColorGrayText), imgRect);
2872                                                         ControlPaint.DrawBorder3D (dc, imgRect, Border3DStyle.SunkenOuter,
2873                                                                 Border3DSide.Right | Border3DSide.Bottom);
2874                                                 }
2875                                         }
2876                                         if (button.Enabled)
2877                                                 dc.DrawString (button.Text, font, SystemBrushes.ControlText, txtRect, format);
2878                                         else
2879                                                 CPDrawStringDisabled (dc, button.Text, font, ColorButtonHilight,
2880                                                         txtRect, format);
2881                                 }
2882
2883                                 else if (button.Pushed) {
2884                                         dc.FillRectangle (SystemBrushes.ControlLightLight, toggleArea);
2885                                         if (! imgRect.IsEmpty) {
2886                                                 if (button.Enabled && image != null)
2887                                                         button.Parent.ImageList.Draw (dc, imgRect.X, imgRect.Y, imgRect.Width,
2888                                                                 imgRect.Height, button.ImageIndex);
2889                                                 else {
2890                                                         dc.FillRectangle (new SolidBrush (ColorGrayText), imgRect);
2891                                                         CPDrawBorder3D (dc, imgRect, Border3DStyle.SunkenOuter,
2892                                                                 Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
2893                                                 }
2894                                         }
2895                                         if (button.Enabled)
2896                                                 dc.DrawString (button.Text, font, SystemBrushes.ControlText, txtRect, format);
2897                                         else
2898                                                 CPDrawStringDisabled (dc, button.Text, font, ColorButtonHilight,
2899                                                         txtRect, format);
2900                                 }
2901
2902                                 else {
2903                                         dc.FillRectangle (SystemBrushes.Control, toggleArea);
2904                                         if (! imgRect.IsEmpty) {
2905                                                 if (button.Enabled && image != null)
2906                                                         button.Parent.ImageList.Draw (dc, imgRect.X, imgRect.Y, imgRect.Width,
2907                                                                 imgRect.Height, button.ImageIndex);
2908                                                 else {
2909                                                         dc.FillRectangle (new SolidBrush (ColorGrayText), imgRect);
2910                                                         CPDrawBorder3D (dc, imgRect, Border3DStyle.SunkenOuter,
2911                                                                 Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
2912                                                 }
2913                                         }
2914                                         if (button.Enabled)
2915                                                 dc.DrawString (button.Text, font, SystemBrushes.ControlText, txtRect, format);
2916                                         else
2917                                                 CPDrawStringDisabled (dc, button.Text, font, ColorButtonHilight,
2918                                                         txtRect, format);
2919                                 }
2920                                 break;
2921
2922                         case ToolBarButtonStyle.DropDownButton:
2923                                 // draw the dropdown arrow
2924                                 if (! ddRect.IsEmpty) {
2925                                         PointF [] vertices = new PointF [3];
2926                                         PointF ddCenter = new PointF (ddRect.X + (ddRect.Width/2.0f), ddRect.Y + (ddRect.Height/2.0f));
2927                                         vertices [0].X = ddCenter.X - this.ToolBarDropDownArrowWidth / 2.0f + 0.5f;
2928                                         vertices [0].Y = ddCenter.Y;
2929                                         vertices [1].X = ddCenter.X + this.ToolBarDropDownArrowWidth / 2.0f + 0.5f;
2930                                         vertices [1].Y = ddCenter.Y;
2931                                         vertices [2].X = ddCenter.X + 0.5f; // 0.5 is added for adjustment
2932                                         vertices [2].Y = ddCenter.Y + this.ToolBarDropDownArrowHeight;
2933                                         dc.FillPolygon (SystemBrushes.ControlText, vertices);
2934                                 }
2935                                 goto case ToolBarButtonStyle.PushButton;
2936
2937                         case ToolBarButtonStyle.PushButton:
2938                                 if (! imgRect.IsEmpty){
2939                                         if (button.Enabled && image != null)
2940                                                 button.Parent.ImageList.Draw (dc, imgRect.X, imgRect.Y, imgRect.Width, imgRect.Height,
2941                                                         button.ImageIndex);
2942                                         else {
2943                                                 dc.FillRectangle (new SolidBrush (ColorGrayText), imgRect);
2944                                                 CPDrawBorder3D (dc, imgRect, Border3DStyle.SunkenOuter,
2945                                                         Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
2946                                         }
2947                                 }
2948                                 if (button.Enabled)
2949                                         dc.DrawString (button.Text, font, SystemBrushes.ControlText, txtRect, format);
2950                                 else
2951                                         CPDrawStringDisabled (dc, button.Text, font, ColorButtonHilight,
2952                                                 txtRect, format);
2953                                 break;
2954                         }
2955                 }
2956
2957                 // Grip width for the ToolBar
2958                 public override int ToolBarGripWidth {
2959                         get { return 2;}
2960                 }
2961
2962                 // Grip width for the Image on the ToolBarButton
2963                 public override int ToolBarImageGripWidth {
2964                         get { return 2;}
2965                 }
2966
2967                 // width of the separator
2968                 public override int ToolBarSeparatorWidth {
2969                         get { return 4; }
2970                 }
2971
2972                 // width of the dropdown arrow rect
2973                 public override int ToolBarDropDownWidth {
2974                         get { return 13; }
2975                 }
2976
2977                 // width for the dropdown arrow on the ToolBarButton
2978                 public override int ToolBarDropDownArrowWidth {
2979                         get { return 5;}
2980                 }
2981
2982                 // height for the dropdown arrow on the ToolBarButton
2983                 public override int ToolBarDropDownArrowHeight {
2984                         get { return 3;}
2985                 }
2986
2987                 public override Size ToolBarDefaultSize {
2988                         get {
2989                                 return new Size (100, 42);
2990                         }
2991                 }
2992                 #endregion      // ToolBar
2993
2994                 #region ToolTip
2995                 public override void DrawToolTip(Graphics dc, Rectangle clip_rectangle, ToolTip tt) {
2996                         Control control;
2997
2998                         control = tt.tooltip_window;
2999                         dc.FillRectangle(ResPool.GetSolidBrush(this.ColorInfoWindow), control.client_rect);
3000                         dc.DrawRectangle(ResPool.GetPen(this.ColorWindowFrame), 0, 0, control.Width-1, control.Height-1);
3001                         dc.DrawString(control.text, control.Font, ResPool.GetSolidBrush(this.ColorInfoText), control.client_rect, tt.tooltip_window.string_format);
3002                 }
3003
3004                 public override Size ToolTipSize(ToolTip tt, string text) {
3005                         SizeF   sizef;
3006
3007                         sizef = tt.tooltip_window.DeviceContext.MeasureString(text, tt.tooltip_window.Font);
3008                         return new Size((int)sizef.Width+2, (int)sizef.Height+3);               // Need space for the border
3009                 }
3010                 #endregion      // ToolTip
3011
3012                 #region TrackBar
3013                 private void DrawTrackBar_Vertical (Graphics dc, Rectangle area, TrackBar tb,
3014                         ref Rectangle thumb_pos, ref Rectangle thumb_area,  Brush br_thumb,
3015                         float ticks, int value_pos, bool mouse_value) {                 
3016
3017                         Point toptick_startpoint = new Point ();
3018                         Point bottomtick_startpoint = new Point ();
3019                         Point channel_startpoint = new Point ();
3020                         float pixel_len;
3021                         float pixels_betweenticks;
3022                         const int space_from_right = 8;
3023                         const int space_from_left = 8;                  
3024                         
3025                         switch (tb.TickStyle)   {
3026                         case TickStyle.BottomRight:
3027                         case TickStyle.None:
3028                                 channel_startpoint.Y = 8;
3029                                 channel_startpoint.X = 9;
3030                                 bottomtick_startpoint.Y = 13;
3031                                 bottomtick_startpoint.X = 24;                           
3032                                 break;
3033                         case TickStyle.TopLeft:
3034                                 channel_startpoint.Y = 8;
3035                                 channel_startpoint.X = 19;
3036                                 toptick_startpoint.Y = 13;
3037                                 toptick_startpoint.X = 8;
3038                                 break;
3039                         case TickStyle.Both:
3040                                 channel_startpoint.Y = 8;
3041                                 channel_startpoint.X = 18;      
3042                                 bottomtick_startpoint.Y = 13;
3043                                 bottomtick_startpoint.X = 32;                           
3044                                 toptick_startpoint.Y = 13;
3045                                 toptick_startpoint.X = 8;                               
3046                                 break;
3047                         default:
3048                                 break;
3049                         }
3050                         
3051                         thumb_area.X = area.X + channel_startpoint.X;
3052                         thumb_area.Y = area.Y + channel_startpoint.Y;
3053                         thumb_area.Height = area.Height - space_from_right - space_from_left;
3054                         thumb_area.Width = 4;
3055
3056                         /* Draw channel */
3057                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonShadow), channel_startpoint.X, channel_startpoint.Y,
3058                                 1, thumb_area.Height);
3059                         
3060                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonDkShadow), channel_startpoint.X + 1, channel_startpoint.Y,
3061                                 1, thumb_area.Height);
3062
3063                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonHilight), channel_startpoint.X + 3, channel_startpoint.Y,
3064                                 1, thumb_area.Height);
3065
3066                         pixel_len = thumb_area.Height - 11;
3067                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
3068                         
3069                         /* Convert thumb position from mouse position to value*/
3070                         if (mouse_value) {
3071                                 
3072                                 if (value_pos >= channel_startpoint.Y)
3073                                         value_pos = (int)(((float) (value_pos - channel_startpoint.Y)) / pixels_betweenticks);
3074                                 else
3075                                         value_pos = 0;                  
3076
3077                                 if (value_pos + tb.Minimum > tb.Maximum)
3078                                         value_pos = tb.Maximum - tb.Minimum;
3079                                 
3080                                 tb.Value = value_pos + tb.Minimum;
3081                         }                       
3082                         
3083                         thumb_pos.Y = channel_startpoint.Y + (int) (pixels_betweenticks * (float) value_pos);
3084                         
3085                         /* Draw thumb fixed 10x22 size */
3086                         thumb_pos.Width = 10;
3087                         thumb_pos.Height = 22;
3088
3089                         switch (tb.TickStyle)   {
3090                         case TickStyle.BottomRight:
3091                         case TickStyle.None: {
3092                                 thumb_pos.X = channel_startpoint.X - 8;
3093
3094                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X , thumb_pos.Y + 10);
3095                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 16, thumb_pos.Y);
3096                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X + 16, thumb_pos.Y, thumb_pos.X + 16 + 4, thumb_pos.Y + 4);
3097                                 
3098                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X +1, thumb_pos.Y + 9, thumb_pos.X +15, thumb_pos.Y  +9);
3099                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 16, thumb_pos.Y + 9, thumb_pos.X +16 + 4, thumb_pos.Y  +9 - 4);
3100
3101                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X, thumb_pos.Y  + 10, thumb_pos.X +16, thumb_pos.Y +10);
3102                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 16, thumb_pos.Y  + 10, thumb_pos.X  +16 + 5, thumb_pos.Y +10 - 5);
3103
3104                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 16, 8);
3105                                 dc.FillRectangle (br_thumb, thumb_pos.X + 17, thumb_pos.Y + 2, 1, 6);
3106                                 dc.FillRectangle (br_thumb, thumb_pos.X + 18, thumb_pos.Y + 3, 1, 4);
3107                                 dc.FillRectangle (br_thumb, thumb_pos.X + 19, thumb_pos.Y + 4, 1, 2);
3108
3109                                 break;
3110                         }
3111                         case TickStyle.TopLeft: {
3112                                 thumb_pos.X = channel_startpoint.X - 10;
3113
3114                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X + 4 + 16, thumb_pos.Y);
3115                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 4);
3116
3117                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X  + 4, thumb_pos.Y + 9, thumb_pos.X + 4 + 16 , thumb_pos.Y+ 9);
3118                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 4, thumb_pos.Y  + 9, thumb_pos.X, thumb_pos.Y + 5);
3119                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X  + 19, thumb_pos.Y + 9, thumb_pos.X  +19 , thumb_pos.Y+ 1);
3120
3121                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X  + 4, thumb_pos.Y+ 10, thumb_pos.X  + 4 + 16, thumb_pos.Y+ 10);
3122                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X  + 4, thumb_pos.Y + 10, thumb_pos.X  -1, thumb_pos.Y+ 5);
3123                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 20, thumb_pos.Y, thumb_pos.X+ 20, thumb_pos.Y + 10);
3124
3125                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 15, 8);
3126                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 1, 6);
3127                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 1, 4);
3128                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 1, 2);
3129
3130                                 break;
3131                         }
3132
3133                         case TickStyle.Both: {
3134                                 thumb_pos.X = area.X + 10;
3135                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 9);
3136                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 19, thumb_pos.Y);
3137
3138                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 1, thumb_pos.Y + 9, thumb_pos.X+ 19, thumb_pos.Y  + 9);
3139                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X  + 10, thumb_pos.Y+ 1, thumb_pos.X + 19, thumb_pos.Y  + 8);
3140
3141                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X, thumb_pos.Y + 10, thumb_pos.X+ 20, thumb_pos.Y  +10);
3142                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X  + 20, thumb_pos.Y, thumb_pos.X  + 20, thumb_pos.Y+ 9);
3143
3144                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 18, 8);
3145
3146                                 break;
3147                         }
3148
3149                         default:
3150                                 break;
3151                         }
3152
3153                         pixel_len = thumb_area.Height - 11;
3154                         pixels_betweenticks = pixel_len / ticks;                                
3155                         
3156                         /* Draw ticks*/
3157                         if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight ||
3158                                 ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) { 
3159                                 
3160                                 for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks)  {                                       
3161                                         if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
3162                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X , area.Y + bottomtick_startpoint.Y  + inc, 
3163                                                         area.X + bottomtick_startpoint.X  + 3, area.Y + bottomtick_startpoint.Y + inc);
3164                                         else
3165                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X, area.Y + bottomtick_startpoint.Y  + inc, 
3166                                                         area.X + bottomtick_startpoint.X  + 2, area.Y + bottomtick_startpoint.Y + inc);
3167                                 }
3168                         }
3169
3170                         if (pixels_betweenticks > 0 &&  ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft ||
3171                                 ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {
3172
3173                                 pixel_len = thumb_area.Height - 11;
3174                                 pixels_betweenticks = pixel_len / ticks;
3175                                 
3176                                 for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
3177                                         if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
3178                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X  - 3 , area.Y + toptick_startpoint.Y + inc, 
3179                                                         area.X + toptick_startpoint.X, area.Y + toptick_startpoint.Y + inc);
3180                                         else
3181                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X  - 2, area.Y + toptick_startpoint.Y + inc, 
3182                                                         area.X + toptick_startpoint.X, area.Y + toptick_startpoint.Y  + inc);
3183                                 }                       
3184                         }
3185                 }
3186
3187                 /* 
3188                         Horizontal trackbar 
3189                   
3190                         Does not matter the size of the control, Win32 always draws:
3191                                 - Ticks starting from pixel 13, 8
3192                                 - Channel starting at pos 8, 19 and ends at Width - 8
3193                                 - Autosize makes always the control 40 pixels height
3194                                 - Ticks are draw at (channel.Witdh - 10) / (Maximum - Minimum)
3195                                 
3196                 */
3197                 private void DrawTrackBar_Horizontal (Graphics dc, Rectangle area, TrackBar tb,
3198                         ref Rectangle thumb_pos, ref Rectangle thumb_area, Brush br_thumb,
3199                         float ticks, int value_pos, bool mouse_value) {                 
3200                         Point toptick_startpoint = new Point ();
3201                         Point bottomtick_startpoint = new Point ();
3202                         Point channel_startpoint = new Point ();
3203                         float pixel_len;
3204                         float pixels_betweenticks;
3205                         const int space_from_right = 8;
3206                         const int space_from_left = 8;          
3207                                                 
3208                         switch (tb.TickStyle) {
3209                         case TickStyle.BottomRight:
3210                         case TickStyle.None:
3211                                 channel_startpoint.X = 8;
3212                                 channel_startpoint.Y = 9;
3213                                 bottomtick_startpoint.X = 13;
3214                                 bottomtick_startpoint.Y = 24;                           
3215                                 break;
3216                         case TickStyle.TopLeft:
3217                                 channel_startpoint.X = 8;
3218                                 channel_startpoint.Y = 19;
3219                                 toptick_startpoint.X = 13;
3220                                 toptick_startpoint.Y = 8;
3221                                 break;
3222                         case TickStyle.Both:
3223                                 channel_startpoint.X = 8;
3224                                 channel_startpoint.Y = 18;      
3225                                 bottomtick_startpoint.X = 13;
3226                                 bottomtick_startpoint.Y = 32;                           
3227                                 toptick_startpoint.X = 13;
3228                                 toptick_startpoint.Y = 8;                               
3229                                 break;
3230                         default:
3231                                 break;
3232                         }
3233                                                 
3234                         thumb_area.X = area.X + channel_startpoint.X;
3235                         thumb_area.Y = area.Y + channel_startpoint.Y;
3236                         thumb_area.Width = area.Width - space_from_right - space_from_left;
3237                         thumb_area.Height = 4;
3238                         
3239                         /* Draw channel */
3240                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonShadow), channel_startpoint.X, channel_startpoint.Y,
3241                                 thumb_area.Width, 1);
3242                         
3243                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonDkShadow), channel_startpoint.X, channel_startpoint.Y + 1,
3244                                 thumb_area.Width, 1);
3245
3246                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonHilight), channel_startpoint.X, channel_startpoint.Y +3,
3247                                 thumb_area.Width, 1);
3248
3249                         pixel_len = thumb_area.Width - 11;
3250                         pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
3251
3252                         /* Convert thumb position from mouse position to value*/
3253                         if (mouse_value) {                      
3254                                 if (value_pos >= channel_startpoint.X)
3255                                         value_pos = (int)(((float) (value_pos - channel_startpoint.X)) / pixels_betweenticks);
3256                                 else
3257                                         value_pos = 0;                          
3258
3259                                 if (value_pos + tb.Minimum > tb.Maximum)
3260                                         value_pos = tb.Maximum - tb.Minimum;
3261                                 
3262                                 tb.Value = value_pos + tb.Minimum;
3263                         }                       
3264                         
3265                         thumb_pos.X = channel_startpoint.X + (int) (pixels_betweenticks * (float) value_pos);
3266                         
3267                         /* Draw thumb fixed 10x22 size */
3268                         thumb_pos.Width = 10;
3269                         thumb_pos.Height = 22;
3270
3271                         switch (tb.TickStyle) {
3272                         case TickStyle.BottomRight:
3273                         case TickStyle.None: {
3274                                 thumb_pos.Y = channel_startpoint.Y - 8;
3275
3276                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y);
3277                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 16);
3278                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y + 16, thumb_pos.X + 4, thumb_pos.Y + 16 + 4);
3279
3280                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X +9, thumb_pos.Y +15);
3281                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 16, thumb_pos.X +9 - 4, thumb_pos.Y +16 + 4);
3282
3283                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X +10, thumb_pos.Y +16);
3284                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 10, thumb_pos.Y + 16, thumb_pos.X +10 - 5, thumb_pos.Y +16 + 5);
3285
3286                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 16);
3287                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 17, 6, 1);
3288                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 18, 4, 1);
3289                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 19, 2, 1);
3290                                 break;
3291                         }
3292                         case TickStyle.TopLeft: {
3293                                 thumb_pos.Y = channel_startpoint.Y - 10;
3294
3295                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X, thumb_pos.Y + 4 + 16);
3296                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X + 4, thumb_pos.Y);
3297
3298                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 9, thumb_pos.Y + 4 + 16);
3299                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y);
3300                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 19, thumb_pos.X + 1 , thumb_pos.Y +19);
3301
3302                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 10, thumb_pos.Y + 4 + 16);
3303                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 10, thumb_pos.Y + 4, thumb_pos.X + 5, thumb_pos.Y -1);
3304                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 10, thumb_pos.Y + 20);
3305
3306                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 4, 8, 15);
3307                                 dc.FillRectangle (br_thumb, thumb_pos.X + 2, thumb_pos.Y + 3, 6, 1);
3308                                 dc.FillRectangle (br_thumb, thumb_pos.X + 3, thumb_pos.Y + 2, 4, 1);
3309                                 dc.FillRectangle (br_thumb, thumb_pos.X + 4, thumb_pos.Y + 1, 2, 1);
3310                                 break;
3311                         }
3312
3313                         case TickStyle.Both: {
3314                                 thumb_pos.Y = area.Y + 10;
3315                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X + 9, thumb_pos.Y);
3316                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 19);
3317
3318                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 9, thumb_pos.Y + 1, thumb_pos.X + 9, thumb_pos.Y + 19);
3319                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), thumb_pos.X + 1, thumb_pos.Y + 10, thumb_pos.X + 8, thumb_pos.Y + 19);
3320
3321                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X + 10, thumb_pos.Y, thumb_pos.X +10, thumb_pos.Y + 20);
3322                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), thumb_pos.X, thumb_pos.Y + 20, thumb_pos.X + 9, thumb_pos.Y + 20);
3323
3324                                 dc.FillRectangle (br_thumb, thumb_pos.X + 1, thumb_pos.Y + 1, 8, 18);
3325
3326                                 break;
3327                         }
3328
3329                         default:
3330                                 break;
3331                         }
3332
3333                         pixel_len = thumb_area.Width - 11;
3334                         pixels_betweenticks = pixel_len / ticks;
3335
3336                         /* Draw ticks*/
3337                         if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.BottomRight) == TickStyle.BottomRight ||
3338                                 ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {                         
3339                                 
3340                                 for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
3341                                         if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
3342                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X + inc , area.Y + bottomtick_startpoint.Y, 
3343                                                         area.X + bottomtick_startpoint.X + inc , area.Y + bottomtick_startpoint.Y + 3);
3344                                         else
3345                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + bottomtick_startpoint.X + inc, area.Y + bottomtick_startpoint.Y, 
3346                                                         area.X + bottomtick_startpoint.X + inc, area.Y + bottomtick_startpoint.Y + 2);
3347                                 }
3348                         }
3349
3350                         if (pixels_betweenticks > 0 && ((tb.TickStyle & TickStyle.TopLeft) == TickStyle.TopLeft ||
3351                                 ((tb.TickStyle & TickStyle.Both) == TickStyle.Both))) {
3352                                 
3353                                 for (float inc = 0; inc < (pixel_len + 1); inc += pixels_betweenticks) {                                        
3354                                         if (inc == 0 || (inc +  pixels_betweenticks) >= pixel_len +1)
3355                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X + inc , area.Y + toptick_startpoint.Y - 3, 
3356                                                         area.X + toptick_startpoint.X + inc , area.Y + toptick_startpoint.Y);
3357                                         else
3358                                                 dc.DrawLine (ResPool.GetPen (pen_ticks_color), area.X + toptick_startpoint.X + inc, area.Y + toptick_startpoint.Y - 2, 
3359                                                         area.X + toptick_startpoint.X + inc, area.Y + toptick_startpoint.Y );
3360                                 }                       
3361                         }
3362                 }
3363
3364                 public override void DrawTrackBar (Graphics dc, Rectangle clip_rectangle, TrackBar tb) {
3365 //public override void DrawTrackBar (Graphics dc, 
3366 //Rectangle area, 
3367 //TrackBar tb,
3368 //ref Rectangle thumb_pos, 
3369 //ref Rectangle thumb_area,  
3370                         Brush           br_thumb;
3371                         int             value_pos;
3372                         bool            mouse_value;
3373                         float           ticks = (tb.Maximum - tb.Minimum) / tb.tickFrequency; /* N of ticks draw*/
3374                         Rectangle       area;
3375                         Rectangle       thumb_pos = tb.ThumbPos;
3376                         Rectangle       thumb_area = tb.ThumbArea;
3377
3378                         if (tb.thumb_pressed) {
3379                                 value_pos = tb.thumb_mouseclick;
3380                                 mouse_value = true;
3381                         } else {
3382                                 value_pos = tb.Value - tb.Minimum;
3383                                 mouse_value = false;
3384                         }
3385
3386                         area = tb.paint_area;
3387
3388                         if (tb.thumb_pressed == true) {
3389                                 br_thumb = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonHilight, ColorButtonFace);
3390                         } else {
3391                                 br_thumb = ResPool.GetSolidBrush (ColorButtonFace);
3392                         }
3393
3394                         
3395                         /* Control Background */
3396                         if (tb.BackColor == DefaultControlBackColor) {
3397                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonFace), area);
3398                         } else {
3399                                 dc.FillRectangle (ResPool.GetSolidBrush (tb.BackColor), area);
3400                         }
3401                         
3402
3403                         if (tb.Focused) {
3404                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonFace, Color.Black), area.X, area.Y, area.Width - 1, 1);
3405                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonFace, Color.Black), area.X, area.Y + area.Height - 1, area.Width - 1, 1);
3406                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonFace, Color.Black), area.X, area.Y, 1, area.Height - 1);
3407                                 dc.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonFace, Color.Black), area.X + area.Width - 1, area.Y, 1, area.Height - 1);
3408                         }
3409
3410                         if (tb.Orientation == Orientation.Vertical) {
3411                                 DrawTrackBar_Vertical (dc, area, tb, ref thumb_pos, ref thumb_area,
3412                                         br_thumb, ticks, value_pos, mouse_value);
3413                         
3414                         } else {
3415                                 DrawTrackBar_Horizontal (dc, area, tb, ref thumb_pos, ref thumb_area,
3416                                         br_thumb, ticks, value_pos, mouse_value);
3417                         }
3418
3419                         tb.ThumbPos = thumb_pos;
3420                         tb.ThumbArea = thumb_area;
3421                 }
3422
3423                 public override Size TrackBarDefaultSize {
3424                         get {
3425                                 return new Size (104, 42);
3426                         }
3427                 }
3428
3429                 #endregion      // TrackBar
3430
3431                 #region VScrollBar
3432                 public override Size VScrollBarDefaultSize {
3433                         get {
3434                                 return new Size (this.ScrollBarButtonSize, 80);
3435                         }
3436                 }
3437                 #endregion      // VScrollBar
3438
3439                 #region TreeView
3440                 public override Size TreeViewDefaultSize {\r
3441                         get {\r
3442                                 return new Size (121, 97);\r
3443                         }\r
3444                 }\r
3445
3446                 #endregion
3447
3448                 #region ControlPaint
3449                 private enum DrawFrameControlStates {
3450                         ButtonCheck             = 0x0000,
3451                         ButtonRadioImage        = 0x0001,
3452                         ButtonRadioMask         = 0x0002,
3453                         ButtonRadio             = 0x0004,
3454                         Button3State            = 0x0008,
3455                         ButtonPush              = 0x0010,
3456
3457                         CaptionClose            = 0x0000,
3458                         CaptionMin              = 0x0001,
3459                         CaptionMax              = 0x0002,
3460                         CaptionRestore          = 0x0004,
3461                         CaptionHelp             = 0x0008,
3462
3463                         MenuArrow               = 0x0000,
3464                         MenuCheck               = 0x0001,
3465                         MenuBullet              = 0x0002,
3466                         MenuArrowRight          = 0x0004,
3467
3468                         ScrollUp                = 0x0000,
3469                         ScrollDown              = 0x0001,
3470                         ScrollLeft              = 0x0002,
3471                         ScrollRight             = 0x0003,
3472                         ScrollComboBox          = 0x0005,
3473                         ScrollSizeGrip          = 0x0008,
3474                         ScrollSizeGripRight     = 0x0010,
3475
3476                         Inactive                = 0x0100,
3477                         Pushed                  = 0x0200,
3478                         Checked                 = 0x0400,
3479                         Transparent             = 0x0800,
3480                         Hot                     = 0x1000,
3481                         AdjustRect              = 0x2000,
3482                         Flat                    = 0x4000,
3483                         Mono                    = 0x8000
3484
3485                 }
3486
3487                 private enum DrawFrameControlTypes {
3488                         Caption = 1,
3489                         Menu    = 2,
3490                         Scroll  = 3,
3491                         Button  = 4
3492                 }
3493
3494                 public override void CPDrawBorder (Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
3495                         ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
3496                         Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor,
3497                         int bottomWidth, ButtonBorderStyle bottomStyle) {
3498                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom-1, leftWidth, leftColor, leftStyle, Border3DSide.Left);
3499                         DrawBorderInternal(graphics, bounds.Left, bounds.Top, bounds.Right-1, bounds.Top, topWidth, topColor, topStyle, Border3DSide.Top);
3500                         DrawBorderInternal(graphics, bounds.Right-1, bounds.Top, bounds.Right-1, bounds.Bottom-1, rightWidth, rightColor, rightStyle, Border3DSide.Right);
3501                         DrawBorderInternal(graphics, bounds.Left, bounds.Bottom-1, bounds.Right-1, bounds.Bottom-1, bottomWidth, bottomColor, bottomStyle, Border3DSide.Bottom);
3502                 }
3503
3504                 public override void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
3505                         CPDrawBorder3D(graphics, rectangle, style, sides, ColorButtonFace);
3506                 }
3507
3508                 private void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides, Color control_color) {
3509                         Pen             penTopLeft;
3510                         Pen             penTopLeftInner;
3511                         Pen             penBottomRight;
3512                         Pen             penBottomRightInner;
3513                         Rectangle       rect= new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
3514                         bool            doInner = false;
3515
3516                         if ((style & Border3DStyle.Adjust)!=0) {
3517                                 rect.Y-=2;
3518                                 rect.X-=2;
3519                                 rect.Width+=4;
3520                                 rect.Height+=4;
3521                         }
3522
3523                         /* default to flat */
3524                         penTopLeft=ResPool.GetPen(ControlPaint.Dark(control_color));
3525                         penTopLeftInner=ResPool.GetPen(ControlPaint.Dark(control_color));
3526                         penBottomRight=ResPool.GetPen(ControlPaint.Dark(control_color));
3527                         penBottomRightInner=ResPool.GetPen(ControlPaint.Dark(control_color));
3528
3529                         if ((style & Border3DStyle.RaisedOuter)!=0) {
3530                                 penTopLeft=ResPool.GetPen(ControlPaint.LightLight(control_color));
3531                                 penBottomRight=ResPool.GetPen(ControlPaint.DarkDark(control_color));
3532                                 if ((style & (Border3DStyle.RaisedInner | Border3DStyle.SunkenInner))!=0) {
3533                                         doInner=true;
3534                                 }
3535                         } else if ((style & Border3DStyle.SunkenOuter)!=0) {
3536                                 penTopLeft=ResPool.GetPen(ControlPaint.DarkDark(control_color));
3537                                 penBottomRight=ResPool.GetPen(ControlPaint.LightLight(control_color));
3538                                 if ((style & (Border3DStyle.RaisedInner | Border3DStyle.SunkenInner))!=0) {
3539                                         doInner=true;
3540                                 }
3541                         }
3542
3543                         if ((style & Border3DStyle.RaisedInner)!=0) {
3544                                 if (doInner) {
3545                                         penTopLeftInner=ResPool.GetPen(control_color);
3546                                         penBottomRightInner=ResPool.GetPen(ControlPaint.Dark(control_color));
3547                                 } else {
3548                                         penTopLeft=ResPool.GetPen(ControlPaint.LightLight(control_color));
3549                                         penBottomRight=ResPool.GetPen(ControlPaint.DarkDark(control_color));
3550                                 }
3551                         } else if ((style & Border3DStyle.SunkenInner)!=0) {
3552                                 if (doInner) {
3553                                         penTopLeftInner=ResPool.GetPen(ControlPaint.Dark(control_color));
3554                                         penBottomRightInner=ResPool.GetPen(control_color);
3555                                 } else {
3556                                         penTopLeft=ResPool.GetPen(ControlPaint.DarkDark(control_color));
3557                                         penBottomRight=ResPool.GetPen(ControlPaint.LightLight(control_color));
3558                                 }
3559                         }
3560
3561                         if ((sides & Border3DSide.Middle)!=0) {
3562                                 graphics.FillRectangle(ResPool.GetSolidBrush(control_color), rect);
3563                         }
3564
3565                         if ((sides & Border3DSide.Left)!=0) {
3566                                 graphics.DrawLine(penTopLeft, rect.Left, rect.Bottom-2, rect.Left, rect.Top);
3567                                 if (doInner) {
3568                                         graphics.DrawLine(penTopLeftInner, rect.Left+1, rect.Bottom-2, rect.Left+1, rect.Top);
3569                                 }
3570                         }
3571
3572                         if ((sides & Border3DSide.Top)!=0) {
3573                                 graphics.DrawLine(penTopLeft, rect.Left, rect.Top, rect.Right-2, rect.Top);
3574
3575                                 if (doInner) {
3576                                         if ((sides & Border3DSide.Left)!=0) {
3577                                                 graphics.DrawLine(penTopLeftInner, rect.Left+1, rect.Top+1, rect.Right-3, rect.Top+1);
3578                                         } else {
3579                                                 graphics.DrawLine(penTopLeftInner, rect.Left, rect.Top+1, rect.Right-3, rect.Top+1);
3580                                         }
3581                                 }
3582                         }
3583
3584                         if ((sides & Border3DSide.Right)!=0) {
3585                                 graphics.DrawLine(penBottomRight, rect.Right-1, rect.Top, rect.Right-1, rect.Bottom-1);
3586
3587                                 if (doInner) {
3588                                         if ((sides & Border3DSide.Top)!=0) {
3589                                                 graphics.DrawLine(penBottomRightInner, rect.Right-2, rect.Top+1, rect.Right-2, rect.Bottom-2);
3590                                         } else {
3591                                                 graphics.DrawLine(penBottomRightInner, rect.Right-2, rect.Top, rect.Right-2, rect.Bottom-2);
3592                                         }
3593                                 }
3594                         }
3595
3596                         if ((sides & Border3DSide.Bottom)!=0) {
3597                                 int     left=rect.Left;
3598
3599                                 if ((sides & Border3DSide.Left)!=0) {
3600                                         left+=1;
3601                                 }
3602
3603                                 graphics.DrawLine(penBottomRight, rect.Left, rect.Bottom-1, rect.Right-1, rect.Bottom-1);
3604
3605                                 if (doInner) {
3606                                         if ((sides & Border3DSide.Right)!=0) {
3607                                                 graphics.DrawLine(penBottomRightInner, left, rect.Bottom-2, rect.Right-2, rect.Bottom-2);
3608                                         } else {
3609                                                 graphics.DrawLine(penBottomRightInner, left, rect.Bottom-2, rect.Right-2, rect.Bottom-2);
3610                                         }
3611                                 }
3612                         }
3613
3614                 }
3615
3616
3617                 public override void CPDrawButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
3618                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonPush;
3619
3620                         if ((state & ButtonState.Pushed)!=0) {
3621                                 dfcs |= DrawFrameControlStates.Pushed;
3622                         }
3623
3624                         if ((state & ButtonState.Checked)!=0) {
3625                                 dfcs |= DrawFrameControlStates.Checked;
3626                         }
3627
3628                         if ((state & ButtonState.Flat)!=0) {
3629                                 dfcs |= DrawFrameControlStates.Flat;
3630                         }
3631
3632                         if ((state & ButtonState.Inactive)!=0) {
3633                                 dfcs |= DrawFrameControlStates.Inactive;
3634                         }
3635                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
3636                 }
3637
3638
3639                 public override void CPDrawCaptionButton (Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
3640                         Rectangle       captionRect;
3641                         int                     lineWidth;
3642
3643                         CPDrawButton(graphics, rectangle, state);
3644
3645                         if (rectangle.Width<rectangle.Height) {
3646                                 captionRect=new Rectangle(rectangle.X+1, rectangle.Y+rectangle.Height/2-rectangle.Width/2+1, rectangle.Width-4, rectangle.Width-4);
3647                         } else {
3648                                 captionRect=new Rectangle(rectangle.X+rectangle.Width/2-rectangle.Height/2+1, rectangle.Y+1, rectangle.Height-4, rectangle.Height-4);
3649                         }
3650
3651                         if ((state & ButtonState.Pushed)!=0) {
3652                                 captionRect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-3, rectangle.Height-3);
3653                         }
3654
3655                         /* Make sure we've got at least a line width of 1 */
3656                         lineWidth=Math.Max(1, captionRect.Width/7);
3657
3658                         switch(button) {
3659                         case CaptionButton.Close: {
3660                                 Pen     pen;
3661
3662                                 if ((state & ButtonState.Inactive)!=0) {
3663                                         pen=new Pen(ColorButtonHilight, lineWidth);
3664                                         DrawCaptionHelper(graphics, ColorButtonHilight, pen, lineWidth, 1, captionRect, button);
3665                                         pen.Dispose();
3666
3667                                         pen=new Pen(ColorButtonShadow, lineWidth);
3668                                         DrawCaptionHelper(graphics, ColorButtonShadow, pen, lineWidth, 0, captionRect, button);
3669                                         pen.Dispose();
3670                                         return;
3671                                 } else {
3672                                         pen=new Pen(ColorButtonText, lineWidth);
3673                                         DrawCaptionHelper(graphics, ColorButtonText, pen, lineWidth, 0, captionRect, button);
3674                                         pen.Dispose();
3675                                         return;
3676                                 }
3677                         }
3678
3679                         case CaptionButton.Help:
3680                         case CaptionButton.Maximize:
3681                         case CaptionButton.Minimize:
3682                         case CaptionButton.Restore: {
3683                                 if ((state & ButtonState.Inactive)!=0) {
3684                                         DrawCaptionHelper(graphics, ColorButtonHilight, SystemPens.ControlLightLight, lineWidth, 1, captionRect, button);
3685
3686                                         DrawCaptionHelper(graphics, ColorButtonShadow, SystemPens.ControlDark, lineWidth, 0, captionRect, button);
3687                                         return;
3688                                 } else {
3689                                         DrawCaptionHelper(graphics, ColorButtonText, SystemPens.ControlText, lineWidth, 0, captionRect, button);
3690                                         return;
3691                                 }
3692                         }
3693                         }
3694                 }
3695
3696
3697                 public override void CPDrawCheckBox (Graphics graphics, Rectangle rectangle, ButtonState state) {
3698                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonCheck;
3699
3700                         if ((state & ButtonState.Pushed)!=0) {
3701                                 dfcs |= DrawFrameControlStates.Pushed;
3702                         }
3703
3704                         if ((state & ButtonState.Checked)!=0) {
3705                                 dfcs |= DrawFrameControlStates.Checked;
3706                         }
3707
3708                         if ((state & ButtonState.Flat)!=0) {
3709                                 dfcs |= DrawFrameControlStates.Flat;
3710                         }
3711
3712                         if ((state & ButtonState.Inactive)!=0) {
3713                                 dfcs |= DrawFrameControlStates.Inactive;
3714                         }
3715
3716                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
3717
3718                 }
3719
3720                 public override void CPDrawComboButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
3721                         Point[]                 arrow = new Point[3];
3722                         Point                           P1;
3723                         Point                           P2;
3724                         Point                           P3;
3725                         int                             centerX;
3726                         int                             centerY;
3727                         int                             shiftX;
3728                         int                             shiftY;
3729                         Rectangle               rect;
3730
3731                         if ((state & ButtonState.Checked)!=0) {
3732                                 graphics.FillRectangle(ResPool.GetHatchBrush (HatchStyle.Percent50, ColorButtonLight, ColorButtonHilight),rectangle);                           
3733                         }
3734
3735                         if ((state & ButtonState.Flat)!=0) {
3736                                 ControlPaint.DrawBorder(graphics, rectangle, ColorButtonShadow, ButtonBorderStyle.Solid);
3737                         } else {
3738                                 if ((state & (ButtonState.Pushed | ButtonState.Checked))!=0) {
3739                                         CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
3740                                 } else {
3741                                         CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
3742                                 }
3743                         }
3744
3745                         rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
3746                         centerX=rect.Left+rect.Width/2;
3747                         centerY=rect.Top+rect.Height/2;
3748                         shiftX=Math.Max(1, rect.Width/8);
3749                         shiftY=Math.Max(1, rect.Height/8);
3750
3751                         if ((state & ButtonState.Pushed)!=0) {
3752                                 shiftX++;
3753                                 shiftY++;
3754                         }
3755
3756                         rect.Y-=shiftY;
3757                         centerY-=shiftY;
3758                         P1=new Point(rect.Left, centerY);
3759                         P2=new Point(rect.Right, centerY);
3760                         P3=new Point(centerX, rect.Bottom);
3761
3762                         arrow[0]=P1;
3763                         arrow[1]=P2;
3764                         arrow[2]=P3;
3765
3766                         /* Draw the arrow */
3767                         if ((state & ButtonState.Inactive)!=0) {
3768                                 graphics.FillPolygon(SystemBrushes.ControlLightLight, arrow, FillMode.Winding);
3769
3770                                 /* Move away from the shadow */
3771                                 P1.X-=1;                P1.Y-=1;
3772                                 P2.X-=1;                P2.Y-=1;
3773                                 P3.X-=1;                P3.Y-=1;
3774
3775                                 arrow[0]=P1;
3776                                 arrow[1]=P2;
3777                                 arrow[2]=P3;
3778
3779
3780                                 graphics.FillPolygon(SystemBrushes.ControlDark, arrow, FillMode.Winding);
3781                         } else {
3782                                 graphics.FillPolygon(SystemBrushes.ControlText, arrow, FillMode.Winding);
3783                         }
3784                 }
3785
3786
3787                 public override void CPDrawContainerGrabHandle (Graphics graphics, Rectangle bounds) {
3788                         
3789                         Pen                     pen     = new Pen(Color.Black, 1);
3790                         Rectangle       rect    = new Rectangle(bounds.X, bounds.Y, bounds.Width-1, bounds.Height-1);   // Dunno why, but MS does it that way, too
3791                         int                     X;
3792                         int                     Y;
3793
3794                         graphics.FillRectangle(ResPool.GetSolidBrush (ColorButtonText), rect);
3795                         graphics.DrawRectangle(pen, rect);
3796
3797                         X=rect.X+rect.Width/2;
3798                         Y=rect.Y+rect.Height/2;
3799
3800                         /* Draw the cross */
3801                         graphics.DrawLine(pen, X, rect.Y+2, X, rect.Bottom-2);
3802                         graphics.DrawLine(pen, rect.X+2, Y, rect.Right-2, Y);
3803
3804                         /* Draw 'arrows' for vertical lines */
3805                         graphics.DrawLine(pen, X-1, rect.Y+3, X+1, rect.Y+3);
3806                         graphics.DrawLine(pen, X-1, rect.Bottom-3, X+1, rect.Bottom-3);
3807
3808                         /* Draw 'arrows' for horizontal lines */
3809                         graphics.DrawLine(pen, rect.X+3, Y-1, rect.X+3, Y+1);
3810                         graphics.DrawLine(pen, rect.Right-3, Y-1, rect.Right-3, Y+1);
3811
3812                 }
3813
3814                 public virtual void DrawFlatStyleFocusRectangle (Graphics graphics, Rectangle rectangle, ButtonBase button, Color foreColor, Color backColor) {
3815                         // make a rectange to trace around border of the button
3816                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
3817                         
3818                         Color outerColor = foreColor;
3819                         // adjust focus color according to the flatstyle
3820                         if (button.FlatStyle == FlatStyle.Popup && !button.is_pressed) {
3821                                 outerColor = (backColor == ColorButtonFace) ? ControlPaint.Dark(ColorButtonFace) : ColorButtonText;                             
3822                         }
3823                         
3824                         // draw the outer rectangle
3825                         graphics.DrawRectangle (ResPool.GetPen (outerColor), trace_rectangle);                  
3826                         
3827                         // draw the inner rectangle                                             
3828                         if (button.FlatStyle == FlatStyle.Popup) {
3829                                 DrawInnerFocusRectangle (graphics, Rectangle.Inflate (rectangle, -4, -4), backColor);
3830                         } else {
3831                                 // draw a flat inner rectangle
3832                                 Pen pen = ResPool.GetPen (ControlPaint.LightLight (backColor));
3833                                 graphics.DrawRectangle(pen, Rectangle.Inflate (trace_rectangle, -4, -4));                               
3834                         }
3835                 }
3836                 
3837                 public virtual void DrawInnerFocusRectangle(Graphics graphics, Rectangle rectangle, Color backColor)
3838                 {       
3839                         // make a rectange to trace around border of the button
3840                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
3841                         
3842                         Color colorBackInverted = Color.FromArgb (Math.Abs (backColor.R-255), Math.Abs (backColor.G-255), Math.Abs (backColor.B-255));
3843                         DashStyle oldStyle; // used for caching old penstyle
3844                         Pen pen = ResPool.GetPen (colorBackInverted);
3845                         
3846                         oldStyle = pen.DashStyle; 
3847                         pen.DashStyle = DashStyle.Dot;
3848                         graphics.DrawRectangle (pen, trace_rectangle);
3849                         pen.DashStyle = oldStyle;
3850                 }
3851                                 
3852
3853                 public override void CPDrawFocusRectangle (Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
3854                         // make a rectange to trace around border of the button
3855                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
3856                         
3857                         //Color                 colorForeInverted;
3858                         Pen                     pen;
3859
3860                         //colorForeInverted=Color.FromArgb(Math.Abs(foreColor.R-255), Math.Abs(foreColor.G-255), Math.Abs(foreColor.B-255));
3861                         //pen=new Pen(colorForeInverted, 1);
3862                         // MS seems to always use black
3863                         pen = ResPool.GetPen (Color.Black);
3864                         graphics.DrawRectangle(pen, trace_rectangle);
3865                         
3866                         DrawInnerFocusRectangle (graphics, Rectangle.Inflate (rectangle, -4, -4), backColor);
3867                 }
3868                 
3869                 public override void CPDrawGrabHandle (Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
3870                         SolidBrush      sb;
3871                         Pen                     pen;
3872
3873                         if (primary==true) {
3874                                 pen=new Pen(Color.Black, 1);
3875                                 if (enabled==true) {
3876                                         sb=ResPool.GetSolidBrush (ColorButtonText);
3877                                 } else {
3878                                         sb=ResPool.GetSolidBrush (ColorButtonFace);
3879                                 }
3880                         } else {
3881                                 pen=new Pen(Color.White, 1);
3882                                 if (enabled==true) {
3883                                         sb=new SolidBrush(Color.Black);
3884                                 } else {
3885                                         sb=ResPool.GetSolidBrush (ColorButtonFace);
3886                                 }
3887                         }
3888                         graphics.FillRectangle(sb, rectangle);
3889                         graphics.DrawRectangle(pen, rectangle);                 
3890                         pen.Dispose();
3891                 }
3892
3893
3894                 public override void CPDrawGrid (Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
3895                         Color   foreColor;
3896                         int     h;
3897                         int     b;
3898                         int     s;
3899
3900                         ControlPaint.Color2HBS(backColor, out h, out b, out s);
3901
3902                         if (b>127) {
3903                                 foreColor=Color.Black;
3904                         } else {
3905                                 foreColor=Color.White;
3906                         }
3907
3908 #if false
3909                         /* Commented out until I take the time and figure out
3910                                 which HatchStyle will match requirements. The code below
3911                                 is only correct for Percent50.
3912                         */
3913                         if (pixelsBetweenDots.Width==pixelsBetweenDots.Height) {
3914                                 HatchBrush      brush=null;
3915
3916                                 switch(pixelsBetweenDots.Width) {
3917                                         case 2: brush=new HatchBrush(HatchStyle.Percent50, foreColor, backColor); break;
3918                                         case 4: brush=new HatchBrush(HatchStyle.Percent25, foreColor, backColor); break;
3919                                         case 5: brush=new HatchBrush(HatchStyle.Percent20, foreColor, backColor); break;
3920                                         default: {
3921                                                 /* Have to do it the slow way */
3922                                                 break;
3923                                         }
3924                                 }
3925                                 if (brush!=null) {
3926                                         graphics.FillRectangle(brush, area);
3927                                         pen.Dispose();
3928                                         brush.Dispose();
3929                                         return;
3930                                 }
3931                         }
3932 #endif
3933                         /* Slow method */
3934
3935                         Bitmap bitmap = new Bitmap(area.Width, area.Height, graphics);
3936
3937                         for (int x=0; x<area.Width; x+=pixelsBetweenDots.Width) {
3938                                 for (int y=0; y<area.Height; y+=pixelsBetweenDots.Height) {
3939                                         bitmap.SetPixel(x, y, foreColor);
3940                                 }
3941                         }
3942                         graphics.DrawImage(bitmap, area.X, area.Y, area.Width, area.Height);
3943                         bitmap.Dispose();
3944                 }
3945
3946                 public override void CPDrawImageDisabled (Graphics graphics, Image image, int x, int y, Color background) {
3947                         /*
3948                                 Microsoft seems to ignore the background and simply make
3949                                 the image grayscale. At least when having > 256 colors on
3950                                 the display.
3951                         */
3952
3953                         ImageAttributes imageAttributes=new ImageAttributes();
3954                         ColorMatrix                     colorMatrix=new ColorMatrix(new float[][] {
3955                                                                                                           // This table would create a perfect grayscale image, based on luminance
3956                                                                                                           //                            new float[]{0.3f,0.3f,0.3f,0,0},
3957                                                                                                           //                            new float[]{0.59f,0.59f,0.59f,0,0},
3958                                                                                                           //                            new float[]{0.11f,0.11f,0.11f,0,0},
3959                                                                                                           //                            new float[]{0,0,0,1,0,0},
3960                                                                                                           //                            new float[]{0,0,0,0,1,0},
3961                                                                                                           //                            new float[]{0,0,0,0,0,1}
3962
3963                                                                                                           // This table generates a image that is grayscaled and then
3964                                                                                                           // brightened up. Seems to match MS close enough.
3965                                                                                                           new float[]{0.2f,0.2f,0.2f,0,0},
3966                                                                                                           new float[]{0.41f,0.41f,0.41f,0,0},
3967                                                                                                           new float[]{0.11f,0.11f,0.11f,0,0},
3968                                                                                                           new float[]{0.15f,0.15f,0.15f,1,0,0},
3969                                                                                                           new float[]{0.15f,0.15f,0.15f,0,1,0},
3970                                                                                                           new float[]{0.15f,0.15f,0.15f,0,0,1}
3971                                                                                                   });
3972
3973                         imageAttributes.SetColorMatrix(colorMatrix);
3974                         graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
3975                         imageAttributes.Dispose();
3976                 }
3977
3978
3979                 public override void CPDrawLockedFrame (Graphics graphics, Rectangle rectangle, bool primary) {
3980                         Pen     penBorder;
3981                         Pen     penInside;
3982
3983                         if (primary) {
3984                                 penBorder=new Pen(Color.White, 2);
3985                                 penInside=new Pen(Color.Black, 1);
3986                         } else {
3987                                 penBorder=new Pen(Color.Black, 2);
3988                                 penInside=new Pen(Color.White, 1);
3989                         }
3990                         penBorder.Alignment=PenAlignment.Inset;
3991                         penInside.Alignment=PenAlignment.Inset;
3992
3993                         graphics.DrawRectangle(penBorder, rectangle);
3994                         graphics.DrawRectangle(penInside, rectangle.X+2, rectangle.Y+2, rectangle.Width-5, rectangle.Height-5);
3995                         penBorder.Dispose();
3996                         penInside.Dispose();
3997                 }
3998
3999
4000                 public override void CPDrawMenuGlyph (Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
4001                         Rectangle       rect;
4002                         int                     lineWidth;
4003
4004                         // MS draws always the background white
4005                         graphics.FillRectangle(ResPool.GetSolidBrush (Color.White), rectangle);
4006
4007                         switch(glyph) {
4008                         case MenuGlyph.Arrow: {
4009                                 Point[]                 arrow = new Point[3];
4010                                 Point                           P1;
4011                                 Point                           P2;
4012                                 Point                           P3;
4013                                 int                             centerX;
4014                                 int                             centerY;
4015                                 int                             shiftX;
4016                                 int                             shiftY;
4017
4018                                 rect=new Rectangle(rectangle.X+rectangle.Width/4, rectangle.Y+rectangle.Height/4, rectangle.Width/2, rectangle.Height/2);
4019                                 centerX=rect.Left+rect.Width/2;
4020                                 centerY=rect.Top+rect.Height/2;
4021                                 shiftX=Math.Max(1, rect.Width/8);
4022                                 shiftY=Math.Max(1, rect.Height/8);
4023
4024                                 rect.X-=shiftX;
4025                                 centerX-=shiftX;
4026
4027                                 P1=new Point(centerX, rect.Top-1);
4028                                 P2=new Point(centerX, rect.Bottom);
4029                                 P3=new Point(rect.Right, centerY);
4030
4031                                 arrow[0]=P1;
4032                                 arrow[1]=P2;
4033                                 arrow[2]=P3;
4034
4035                                 graphics.FillPolygon(SystemBrushes.ControlText, arrow, FillMode.Winding);
4036
4037                                 return;
4038                         }
4039
4040                         case MenuGlyph.Bullet: {
4041                                 
4042                                 lineWidth=Math.Max(2, rectangle.Width/3);
4043                                 rect=new Rectangle(rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2);
4044                                 
4045                                 graphics.FillEllipse(ResPool.GetSolidBrush (ColorButtonText), rect);
4046                                 
4047                                 return;
4048                         }
4049
4050                         case MenuGlyph.Checkmark: {
4051                                 int                     Scale;
4052
4053                                 lineWidth=Math.Max(2, rectangle.Width/6);
4054                                 Scale=Math.Max(1, rectangle.Width/12);
4055
4056                                 rect=new Rectangle(rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2);
4057
4058                                 for (int i=0; i<lineWidth; i++) {
4059                                         graphics.DrawLine(SystemPens.MenuText, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
4060                                         graphics.DrawLine(SystemPens.MenuText, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i, rect.Left+lineWidth/2+6*Scale, rect.Top+lineWidth-2*Scale+i);
4061                                 }
4062                                 return;
4063                         }
4064                         }
4065
4066                 }
4067
4068                 public override void CPDrawRadioButton (Graphics graphics, Rectangle rectangle, ButtonState state) {
4069                         DrawFrameControlStates  dfcs=DrawFrameControlStates.ButtonRadio;
4070
4071                         if ((state & ButtonState.Pushed)!=0) {
4072                                 dfcs |= DrawFrameControlStates.Pushed;
4073                         }
4074
4075                         if ((state & ButtonState.Checked)!=0) {
4076                                 dfcs |= DrawFrameControlStates.Checked;
4077                         }
4078
4079                         if ((state & ButtonState.Flat)!=0) {
4080                                 dfcs |= DrawFrameControlStates.Flat;
4081                         }
4082
4083                         if ((state & ButtonState.Inactive)!=0) {
4084                                 dfcs |= DrawFrameControlStates.Inactive;
4085                         }
4086                         DrawFrameControl(graphics, rectangle, DrawFrameControlTypes.Button, dfcs);
4087
4088                 }
4089
4090
4091                 public override void CPDrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
4092
4093                 }
4094
4095
4096                 public override void CPDrawReversibleLine (Point start, Point end, Color backColor) {
4097
4098                 }
4099
4100
4101                 /* Scroll button: regular button + direction arrow */
4102                 public override void CPDrawScrollButton (Graphics dc, Rectangle area, ScrollButton type, ButtonState state) {
4103                         bool enabled = (state == ButtonState.Inactive) ? false: true;                   
4104                                         
4105                         DrawScrollButtonPrimitive (dc, area, state);
4106                                                 
4107                         if (area.Width < 12 || area.Height < 12) /* Cannot see a thing at smaller sizes */
4108                                 return;
4109
4110                         /* Paint arrows */
4111                         switch (type) {
4112                         case ScrollButton.Up: {
4113                                 int x = area.X +  (area.Width / 2) - 4;
4114                                 int y = area.Y + 9;
4115
4116                                 for (int i = 0; i < 3; i++)
4117                                         if (enabled)
4118                                                 dc.DrawLine (ResPool.GetPen (arrow_color), x + i, y - i, x + i + 6 - 2*i, y - i);
4119                                         else
4120                                                 dc.DrawLine (ResPool.GetPen (ColorGrayText), x + i, y - i, x + i + 6 - 2*i, y - i);
4121
4122                                 
4123                                 dc.FillRectangle (enabled ? ResPool.GetSolidBrush (arrow_color) :  ResPool.GetSolidBrush (ColorGrayText),
4124                                         x + 3, area.Y + 6, 1, 1);
4125                                         
4126                                 break;
4127                         }
4128                         case ScrollButton.Down: {
4129                                 int x = area.X +  (area.Width / 2) - 5;
4130                                 int y = area.Y + 5;
4131
4132                                 for (int i = 4; i != 0; i--)
4133                                         if (enabled)
4134                                                 dc.DrawLine (ResPool.GetPen (arrow_color), x + i, y + i, x + i + 8 - 2*i, y + i);
4135                                         else
4136                                                 dc.DrawLine (ResPool.GetPen (ColorGrayText), x + i, y + i, x + i + 8 - 2*i, y + i);
4137
4138                                 
4139                                 dc.FillRectangle (enabled ? ResPool.GetSolidBrush (arrow_color) :  ResPool.GetSolidBrush (ColorGrayText),
4140                                         x + 4, y + 4, 1, 1);
4141                                 break;
4142                         }
4143
4144                         case ScrollButton.Left: {
4145                                 int y = area.Y +  (area.Height / 2) - 4;
4146                                 int x = area.X + 9;
4147
4148                                 for (int i = 0; i < 3; i++)
4149                                         if (enabled)
4150                                                 dc.DrawLine (ResPool.GetPen (arrow_color), x - i, y + i, x - i, y + i + 6 - 2*i);
4151                                         else
4152                                                 dc.DrawLine (ResPool.GetPen (ColorGrayText), x - i, y + i, x - i, y + i + 6 - 2*i);
4153
4154                                 dc.FillRectangle (enabled ? ResPool.GetSolidBrush (arrow_color) :  ResPool.GetSolidBrush (ColorGrayText),
4155                                         x - 3, y + 3, 1, 1);
4156                                 break;
4157                         }
4158
4159                         case ScrollButton.Right: {
4160                                 int y = area.Y +  (area.Height / 2) - 5;
4161                                 int x = area.X + 5;
4162
4163                                 for (int i = 4; i != 0; i--)
4164                                         if (enabled)
4165                                                 dc.DrawLine (ResPool.GetPen (arrow_color), x + i, y + i, x + i, y + i + 8 - 2*i);
4166                                         else
4167                                                 dc.DrawLine (ResPool.GetPen (ColorGrayText), x + i, y + i, x + i, y + i + 8 - 2*i);
4168
4169                                 dc.FillRectangle (enabled ? ResPool.GetSolidBrush (arrow_color) :  ResPool.GetSolidBrush (ColorGrayText),
4170                                         x + 4, y + 4, 1, 1);
4171                                 break;
4172                         }
4173
4174                         default:
4175                                 break;
4176
4177                         }
4178                 }
4179
4180
4181                 public  override void CPDrawSelectionFrame (Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect,
4182                         Color backColor) {
4183
4184                 }
4185
4186
4187                 public override void CPDrawSizeGrip (Graphics dc, Color backColor, Rectangle bounds) {
4188                         Point pt = new Point (bounds.Right - 2, bounds.Bottom - 1);
4189
4190                         dc.DrawLine (ResPool.GetPen (ColorButtonFace), pt.X - 12, pt.Y, pt.X, pt.Y);
4191                         dc.DrawLine (ResPool.GetPen (ColorButtonFace), pt.X, pt.Y, pt.X, pt.Y - 13);
4192
4193                         // diagonals
4194                         for (int i = 0; i < 11; i += 4) {
4195                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), pt.X - i, pt.Y, pt.X + 1, pt.Y - i - 2);
4196                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), pt.X - i - 1, pt.Y, pt.X + 1, pt.Y - i - 2);
4197                         }
4198
4199                         for (int i = 3; i < 13; i += 4)
4200                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), pt.X - i, pt.Y, pt.X + 1, pt.Y - i - 1);
4201                 }
4202
4203
4204                 public  override void CPDrawStringDisabled (Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle,
4205                         StringFormat format) {
4206                         SolidBrush      brush;
4207
4208                         brush=new SolidBrush(ControlPaint.Light(color, 95));
4209
4210                         layoutRectangle.Offset(1.0f, 1.0f);
4211                         graphics.DrawString(s, font, brush, layoutRectangle, format);
4212
4213                         brush.Color=ControlPaint.Light(color, 50);
4214                         layoutRectangle.Offset(-1.0f, -1.0f);
4215                         graphics.DrawString(s, font, brush, layoutRectangle, format);
4216
4217                         brush.Dispose();
4218                 }
4219
4220                 private static void DrawBorderInternal(Graphics graphics, int startX, int startY, int endX, int endY,
4221                         int width, Color color, ButtonBorderStyle style, Border3DSide side) {
4222
4223                         Pen     pen=new Pen(color, 1);
4224
4225                         switch(style) {
4226                         case ButtonBorderStyle.Solid: {
4227                                 pen.DashStyle=DashStyle.Solid;
4228                                 break;
4229                         }
4230
4231                         case ButtonBorderStyle.Dashed: {
4232                                 pen.DashStyle=DashStyle.Dash;
4233                                 break;
4234                         }
4235
4236                         case ButtonBorderStyle.Dotted: {
4237                                 pen.DashStyle=DashStyle.Dot;
4238                                 break;
4239                         }
4240
4241                         case ButtonBorderStyle.Inset: {
4242                                 pen.DashStyle=DashStyle.Solid;
4243                                 break;
4244                         }
4245
4246                         case ButtonBorderStyle.Outset: {
4247                                 pen.DashStyle=DashStyle.Solid;
4248                                 break;
4249                         }
4250
4251                         default:
4252                         case ButtonBorderStyle.None: {
4253                                 pen.Dispose();
4254                                 return;
4255                         }
4256                         }
4257
4258
4259                         switch(style) {
4260                         case ButtonBorderStyle.Outset: {
4261                                 Color           colorGrade;
4262                                 int             hue, brightness, saturation;
4263                                 int             brightnessSteps;
4264                                 int             brightnessDownSteps;
4265
4266                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
4267
4268                                 brightnessDownSteps=brightness/width;
4269                                 if (brightness>127) {
4270                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
4271                                 } else {
4272                                         brightnessSteps=(127-brightness)/width;
4273                                 }
4274
4275                                 for (int i=0; i<width; i++) {
4276                                         switch(side) {
4277                                         case Border3DSide.Left: {
4278                                                 pen.Dispose();
4279                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
4280                                                 pen=new Pen(colorGrade, 1);
4281                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
4282                                                 break;
4283                                         }
4284
4285                                         case Border3DSide.Right: {
4286                                                 pen.Dispose();
4287                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
4288                                                 pen=new Pen(colorGrade, 1);
4289                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
4290                                                 break;
4291                                         }
4292
4293                                         case Border3DSide.Top: {
4294                                                 pen.Dispose();
4295                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
4296                                                 pen=new Pen(colorGrade, 1);
4297                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
4298                                                 break;
4299                                         }
4300
4301                                         case Border3DSide.Bottom: {
4302                                                 pen.Dispose();
4303                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
4304                                                 pen=new Pen(colorGrade, 1);
4305                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
4306                                                 break;
4307                                         }
4308                                         }
4309                                 }
4310                                 break;
4311                         }
4312
4313                         case ButtonBorderStyle.Inset: {
4314                                 Color           colorGrade;
4315                                 int             hue, brightness, saturation;
4316                                 int             brightnessSteps;
4317                                 int             brightnessDownSteps;
4318
4319                                 ControlPaint.Color2HBS(color, out hue, out brightness, out saturation);
4320
4321                                 brightnessDownSteps=brightness/width;
4322                                 if (brightness>127) {
4323                                         brightnessSteps=Math.Max(6, (160-brightness)/width);
4324                                 } else {
4325                                         brightnessSteps=(127-brightness)/width;
4326                                 }
4327
4328                                 for (int i=0; i<width; i++) {
4329                                         switch(side) {
4330                                         case Border3DSide.Left: {
4331                                                 pen.Dispose();
4332                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
4333                                                 pen=new Pen(colorGrade, 1);
4334                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
4335                                                 break;
4336                                         }
4337
4338                                         case Border3DSide.Right: {
4339                                                 pen.Dispose();
4340                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
4341                                                 pen=new Pen(colorGrade, 1);
4342                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
4343                                                 break;
4344                                         }
4345
4346                                         case Border3DSide.Top: {
4347                                                 pen.Dispose();
4348                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Max(0, brightness-brightnessDownSteps*(width-i)), saturation);
4349                                                 pen=new Pen(colorGrade, 1);
4350                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
4351                                                 break;
4352                                         }
4353
4354                                         case Border3DSide.Bottom: {
4355                                                 pen.Dispose();
4356                                                 colorGrade=ControlPaint.HBS2Color(hue, Math.Min(255, brightness+brightnessSteps*(width-i)), saturation);
4357                                                 pen=new Pen(colorGrade, 1);
4358                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
4359                                                 break;
4360                                         }
4361                                         }
4362                                 }
4363                                 break;
4364                         }
4365
4366                                 /*
4367                                         I decided to have the for-loop duplicated for speed reasons;
4368                                         that way we only have to switch once (as opposed to have the
4369                                         for-loop around the switch)
4370                                 */
4371                         default: {
4372                                 switch(side) {
4373                                 case Border3DSide.Left: {
4374                                         for (int i=0; i<width; i++) {
4375                                                 graphics.DrawLine(pen, startX+i, startY+i, endX+i, endY-i);
4376                                         }
4377                                         break;
4378                                 }
4379
4380                                 case Border3DSide.Right: {
4381                                         for (int i=0; i<width; i++) {
4382                                                 graphics.DrawLine(pen, startX-i, startY+i, endX-i, endY-i);
4383                                         }
4384                                         break;
4385                                 }
4386
4387                                 case Border3DSide.Top: {
4388                                         for (int i=0; i<width; i++) {
4389                                                 graphics.DrawLine(pen, startX+i, startY+i, endX-i, endY+i);
4390                                         }
4391                                         break;
4392                                 }
4393
4394                                 case Border3DSide.Bottom: {
4395                                         for (int i=0; i<width; i++) {
4396                                                 graphics.DrawLine(pen, startX+i, startY-i, endX-i, endY-i);
4397                                         }
4398                                         break;
4399                                 }
4400                                 }
4401                                 break;
4402                         }
4403                         }
4404                         pen.Dispose();
4405                 }
4406
4407                 /*
4408                         This function actually draws the various caption elements.
4409                         This way we can scale them nicely, no matter what size, and they
4410                         still look like MS's scaled caption buttons. (as opposed to scaling a bitmap)
4411                 */
4412
4413                 private static void DrawCaptionHelper(Graphics graphics, Color color, Pen pen, int lineWidth, int shift, Rectangle captionRect, CaptionButton button) {
4414                         switch(button) {
4415                         case CaptionButton.Close: {
4416                                 pen.StartCap=LineCap.Triangle;
4417                                 pen.EndCap=LineCap.Triangle;
4418                                 if (lineWidth<2) {
4419                                         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);
4420                                         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);
4421                                 }
4422
4423                                 graphics.DrawLine(pen, captionRect.Left+2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Right-2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
4424                                 graphics.DrawLine(pen, captionRect.Right-2*lineWidth+shift, captionRect.Top+2*lineWidth+shift, captionRect.Left+2*lineWidth+shift, captionRect.Bottom-2*lineWidth+shift);
4425                                 return;
4426                         }
4427
4428                         case CaptionButton.Help: {
4429                                 StringFormat    sf = new StringFormat();
4430                                 SolidBrush              sb = new SolidBrush(color);
4431                                 Font                            font = new Font("Microsoft Sans Serif", captionRect.Height, FontStyle.Bold, GraphicsUnit.Pixel);
4432
4433                                 sf.Alignment=StringAlignment.Center;
4434                                 sf.LineAlignment=StringAlignment.Center;
4435
4436
4437                                 graphics.DrawString("?", font, sb, captionRect.X+captionRect.Width/2+shift, captionRect.Y+captionRect.Height/2+shift+lineWidth/2, sf);
4438
4439                                 sf.Dispose();
4440                                 sb.Dispose();
4441                                 font.Dispose();
4442
4443                                 return;
4444                         }
4445
4446                         case CaptionButton.Maximize: {
4447                                 /* Top 'caption bar' line */
4448                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
4449                                         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);
4450                                 }
4451
4452                                 /* Left side line */
4453                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4454                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+2*lineWidth+shift, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
4455                                 }
4456
4457                                 /* Right side line */
4458                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4459                                         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);
4460                                 }
4461
4462                                 /* Bottom line */
4463                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4464                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
4465                                 }
4466                                 return;
4467                         }
4468
4469                         case CaptionButton.Minimize: {
4470                                 /* Bottom line */
4471                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
4472                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth+shift, captionRect.Bottom-lineWidth+shift-i);
4473                                 }
4474                                 return;
4475                         }
4476
4477                         case CaptionButton.Restore: {
4478                                 /** First 'window' **/
4479                                 /* Top 'caption bar' line */
4480                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
4481                                         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);
4482                                 }
4483
4484                                 /* Left side line */
4485                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4486                                         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);
4487                                 }
4488
4489                                 /* Right side line */
4490                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4491                                         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);
4492                                 }
4493
4494                                 /* Bottom line */
4495                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4496                                         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);
4497                                 }
4498
4499                                 /** Second 'window' **/
4500                                 /* Top 'caption bar' line */
4501                                 for (int i=0; i<Math.Max(2, lineWidth); i++) {
4502                                         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);
4503                                 }
4504
4505                                 /* Left side line */
4506                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4507                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift+i, captionRect.Top+4*lineWidth+shift+1, captionRect.Left+lineWidth+shift+i, captionRect.Bottom-lineWidth+shift);
4508                                 }
4509
4510                                 /* Right side line */
4511                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4512                                         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);
4513                                 }
4514
4515                                 /* Bottom line */
4516                                 for (int i=0; i<Math.Max(1, lineWidth/2); i++) {
4517                                         graphics.DrawLine(pen, captionRect.Left+lineWidth+shift, captionRect.Bottom-lineWidth+shift-i, captionRect.Right-3*lineWidth-lineWidth/2+shift, captionRect.Bottom-lineWidth+shift-i);
4518                                 }
4519
4520                                 return;
4521                         }
4522
4523                         }
4524                 }
4525
4526                 [MonoTODO("Finish drawing code for Caption, Menu and Scroll")]
4527                 private void DrawFrameControl(Graphics graphics, Rectangle rectangle, DrawFrameControlTypes Type, DrawFrameControlStates State) {
4528                         // make a rectange to trace around border of the button
4529                         Rectangle trace_rectangle = new Rectangle(rectangle.X, rectangle.Y, Math.Max (rectangle.Width-1, 0), Math.Max (rectangle.Height-1, 0));
4530                         switch(Type) {
4531                         case DrawFrameControlTypes.Button: {
4532
4533                                 if ((State & DrawFrameControlStates.ButtonPush)!=0) {
4534 // JBA 31 oct 2004 - I don't think that button style should be rendered like this
4535 //                                      /* Goes first, affects the background */
4536 //                                      if ((State & DrawFrameControlStates.Checked)!=0) {
4537 //                                              HatchBrush      hatchBrush=new HatchBrush(HatchStyle.Percent50, ColorButtonLight, ColorButtonHilight);
4538 //                                              graphics.FillRectangle(hatchBrush,rectangle);
4539 //                                              hatchBrush.Dispose();
4540 //                                      }
4541
4542                                         if ((State & DrawFrameControlStates.Pushed)!=0 || (State & DrawFrameControlStates.Checked)!=0) {
4543                                                 graphics.DrawRectangle (ResPool.GetPen (ControlPaint.Dark (ColorButtonFace)), trace_rectangle);
4544                                         } else if ((State & DrawFrameControlStates.Flat)!=0) {
4545                                                 ControlPaint.DrawBorder(graphics, rectangle, ColorButtonShadow, ButtonBorderStyle.Solid);
4546                                         } else if ((State & DrawFrameControlStates.Inactive)!=0) {
4547                                                 /* Same as normal, it would seem */
4548                                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
4549                                         } else {
4550                                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
4551                                         }
4552                                 } else if ((State & DrawFrameControlStates.ButtonRadio)!=0) {
4553                                         Pen                     penFatDark      = new Pen(ColorButtonShadow, 1);
4554                                         Pen                     penFatLight     = new Pen(ColorButtonLight, 1);
4555                                         int                     lineWidth;
4556
4557                                         graphics.FillPie (ResPool.GetSolidBrush (this.ColorWindow), rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2, 0, 359);
4558
4559                                         graphics.DrawArc(penFatDark, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 135, 180);
4560                                         graphics.DrawArc(penFatLight, rectangle.X+1, rectangle.Y+1, rectangle.Width-2, rectangle.Height-2, 315, 180);
4561
4562                                         graphics.DrawArc(SystemPens.ControlDark, rectangle, 135, 180);
4563                                         graphics.DrawArc(SystemPens.ControlLightLight, rectangle, 315, 180);
4564
4565                                         lineWidth=Math.Max(1, Math.Min(rectangle.Width, rectangle.Height)/3);
4566
4567                                         if ((State & DrawFrameControlStates.Checked)!=0) {
4568                                                 SolidBrush      buttonBrush;
4569
4570                                                 if ((State & DrawFrameControlStates.Inactive)!=0) {
4571                                                         buttonBrush=(SolidBrush)SystemBrushes.ControlDark;
4572                                                 } else {
4573                                                         buttonBrush=(SolidBrush)SystemBrushes.ControlText;
4574                                                 }
4575                                                 graphics.FillPie(buttonBrush, rectangle.X+lineWidth, rectangle.Y+lineWidth, rectangle.Width-lineWidth*2, rectangle.Height-lineWidth*2, 0, 359);
4576                                         }
4577                                         penFatDark.Dispose();
4578                                         penFatLight.Dispose();
4579                                 } else if ((State & DrawFrameControlStates.ButtonRadioImage)!=0) {
4580                                         throw new NotImplementedException () ;
4581                                 } else if ((State & DrawFrameControlStates.ButtonRadioMask)!=0) {
4582                                         throw new NotImplementedException ();
4583                                 } else {        /* Must be Checkbox */
4584                                         Pen                     pen;
4585                                         int                     lineWidth;
4586                                         Rectangle       rect;
4587                                         int                     Scale;
4588
4589                                         /* Goes first, affects the background */
4590                                         if ((State & DrawFrameControlStates.Pushed)!=0 ||
4591                                                 (State & DrawFrameControlStates.Inactive)!=0) {
4592                                                 graphics.FillRectangle(SystemBrushes.Control, rectangle);
4593                                         } else {
4594                                                 graphics.FillRectangle(SystemBrushes.Window, rectangle);
4595                                         }
4596
4597                                         /* Draw the sunken frame */
4598                                         if ((State & DrawFrameControlStates.Flat)!=0) {
4599                                                 ControlPaint.DrawBorder(graphics, rectangle, ColorButtonShadow, ButtonBorderStyle.Solid);
4600                                         } else {
4601                                                 CPDrawBorder3D(graphics, rectangle, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom, ColorButtonFace);
4602                                         }
4603
4604                                         /* Make sure we've got at least a line width of 1 */
4605                                         lineWidth=Math.Max(3, rectangle.Width/6);
4606                                         Scale=Math.Max(1, rectangle.Width/12);
4607
4608                                         // define a rectangle inside the border area
4609                                         rect=new Rectangle(rectangle.X+2, rectangle.Y+2, rectangle.Width-4, rectangle.Height-4);
4610                                         if ((State & DrawFrameControlStates.Inactive)!=0) {
4611                                                 pen=SystemPens.ControlDark;
4612                                         } else {
4613                                                 pen=SystemPens.ControlText;
4614                                         }
4615
4616                                         if ((State & DrawFrameControlStates.Checked)!=0) {
4617                                                 /* Need to draw a check-mark */
4618                                                 for (int i=0; i<lineWidth; i++) {
4619                                                         graphics.DrawLine(pen, rect.Left+lineWidth/2, rect.Top+lineWidth+i, rect.Left+lineWidth/2+2*Scale, rect.Top+lineWidth+2*Scale+i);
4620                                                         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);
4621                                                 }
4622
4623                                         }
4624                                 }
4625                                 return;
4626                         }
4627
4628                         case DrawFrameControlTypes.Caption: {
4629                                 // FIXME:
4630                                 break;
4631                         }
4632
4633                         case DrawFrameControlTypes.Menu: {
4634                                 // FIXME:
4635                                 break;
4636                         }
4637
4638                         case DrawFrameControlTypes.Scroll: {
4639                                 // FIXME:
4640                                 break;
4641                         }
4642                         }
4643                 }
4644
4645                 /* Generic scroll button */
4646                 public void DrawScrollButtonPrimitive (Graphics dc, Rectangle area, ButtonState state) {
4647                         if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
4648                                 dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonFace), area.X + 1,
4649                                         area.Y + 1, area.Width - 2 , area.Height - 2);
4650
4651                                 dc.DrawRectangle (ResPool.GetPen (ColorButtonShadow), area.X,
4652                                         area.Y, area.Width, area.Height);
4653
4654                                 return;
4655                         }                       
4656
4657                         dc.FillRectangle (new SolidBrush (Color.Blue), area);
4658                         
4659                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonFace), area.X, area.Y, area.Width, 1);
4660                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonFace), area.X, area.Y, 1, area.Height);
4661
4662                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonHilight), area.X + 1, area.Y + 1, area.Width - 1, 1);
4663                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonHilight), area.X + 1, area.Y + 2, 1,
4664                                 area.Height - 4);
4665
4666                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonShadow), area.X + 1, area.Y + area.Height - 2,
4667                                 area.Width - 2, 1);
4668
4669                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonDkShadow), area.X, area.Y + area.Height -1,
4670                                 area.Width , 1);
4671
4672                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonShadow), area.X + area.Width - 2,
4673                                 area.Y + 1, 1, area.Height -3);
4674
4675                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonDkShadow), area.X + area.Width -1,
4676                                 area.Y, 1, area.Height - 1);
4677
4678                         dc.FillRectangle (ResPool.GetSolidBrush (ColorButtonFace), area.X + 2,
4679                                 area.Y + 2, area.Width - 4, area.Height - 4);
4680                         
4681                 }
4682                 
4683                 public override void CPDrawBorderStyle (Graphics dc, Rectangle area, BorderStyle border_style) {
4684                         switch (border_style){
4685                         case BorderStyle.Fixed3D:
4686                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), area.X, area.Y, area.X +area.Width, area.Y);
4687                                 dc.DrawLine (ResPool.GetPen (ColorButtonShadow), area.X, area.Y, area.X, area.Y + area.Height);
4688                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), area.X , area.Y + area.Height - 1, area.X + area.Width , 
4689                                         area.Y + area.Height - 1);
4690                                 dc.DrawLine (ResPool.GetPen (ColorButtonHilight), area.X + area.Width -1 , area.Y, area.X + area.Width -1, 
4691                                         area.Y + area.Height);
4692
4693                                 // FIXME: These colors are not correct. Microsoft uses different ones but I didn't find them.
4694                                 dc.DrawLine (ResPool.GetPen (ColorButtonLight), area.X + 1, area.Bottom - 2, area.Right - 2, area.Bottom - 2);
4695                                 dc.DrawLine (ResPool.GetPen (ColorButtonLight), area.Right - 2, area.Top + 1, area.Right - 2, area.Bottom - 2);
4696                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), area.X + 1, area.Top + 1, area.X + 1, area.Bottom - 3);
4697                                 dc.DrawLine (ResPool.GetPen (ColorButtonDkShadow), area.X + 1, area.Top + 1, area.Right - 3, area.Top + 1);
4698                                 break;
4699                         case BorderStyle.FixedSingle:
4700                                 dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), area.X, area.Y, area.Width - 1, area.Height - 1);
4701                                 break;
4702                         case BorderStyle.None:
4703                         default:
4704                                 break;
4705                         }
4706                         
4707                 }
4708                 #endregion      // ControlPaint
4709
4710
4711         } //class
4712 }