2007-05-18 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThemeWin32Classic.cs
index 6856534c3cc37efe5de51886fe1115b0475e3e7f..be62bc6f707b27af3395c2baf0e9c0f28a2116de 100644 (file)
@@ -35,6 +35,7 @@ using System.Drawing.Imaging;
 using System.Drawing.Printing;
 using System.Drawing.Text;
 using System.Text;
+using System.Windows.Forms.Theming;
 
 namespace System.Windows.Forms
 {
@@ -83,7 +84,6 @@ namespace System.Windows.Forms
                        string_format_menu_menubar_text.LineAlignment = StringAlignment.Center;
                        string_format_menu_menubar_text.Alignment = StringAlignment.Center;
                        string_format_menu_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
-                       always_draw_hotkeys = false;
                }
 
                public override void ResetDefaults() {
@@ -123,6 +123,13 @@ namespace System.Windows.Forms
                }
                #endregion      // Internal Methods
 
+               #region Control
+               public override Font GetLinkFont (Control control) 
+               {
+                       return new Font (control.Font.FontFamily, control.Font.Size, control.Font.Style | FontStyle.Underline, control.Font.Unit); 
+               }
+               #endregion      // Control
+
                #region OwnerDraw Support
                public  override void DrawOwnerDrawBackground (DrawItemEventArgs e)
                {
@@ -141,19 +148,436 @@ namespace System.Windows.Forms
                }
                #endregion      // OwnerDraw Support
 
-               #region ButtonBase
-               public override void DrawButtonBase(Graphics dc, Rectangle clip_area, ButtonBase button) {
-                       
-                       // Fill the button with the correct color
-                       bool is_ColorControl = button.BackColor.ToArgb () == ColorControl.ToArgb () ? true : false;
-                       dc.FillRectangle (is_ColorControl ? SystemBrushes.Control : ResPool.GetSolidBrush (button.BackColor), button.ClientRectangle);
+               #region Button
+               #region Standard Button Style
+               public override void DrawButton (Graphics g, Button b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
+               {
+                       // Draw Button Background
+                       DrawButtonBackground (g, b, clipRectangle);
+
+                       // If we have an image, draw it
+                       if (imageBounds.Size != Size.Empty)
+                               DrawButtonImage (g, b, imageBounds);
+
+                       // If we're focused, draw a focus rectangle
+                       if (b.Focused && b.Enabled)
+                               DrawButtonFocus (g, b);
+
+                       // If we have text, draw it
+                       if (textBounds != Rectangle.Empty)
+                               DrawButtonText (g, b, textBounds);
+               }
+
+               public virtual void DrawButtonBackground (Graphics g, Button button, Rectangle clipArea) 
+               {
+                       if (button.Pressed)
+                               ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor);
+                       else if (button.InternalSelected)
+                               ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor);
+                       else if (button.Entered)
+                               ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor);
+                       else if (!button.Enabled)
+                               ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor);
+                       else
+                               ThemeElements.DrawButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor);
+               }
+
+               public virtual void DrawButtonFocus (Graphics g, Button button)
+               {
+                       ControlPaint.DrawFocusRectangle (g, Rectangle.Inflate (button.ClientRectangle, -4, -4));
+               }
+
+               public virtual void DrawButtonImage (Graphics g, Button button, Rectangle imageBounds)
+               {
+                       if (button.Enabled)
+                               g.DrawImage (button.Image, imageBounds);
+                       else
+                               CPDrawImageDisabled (g, button.Image, imageBounds.Left, imageBounds.Top, ColorControl);
+               }
+
+               public virtual void DrawButtonText (Graphics g, Button button, Rectangle textBounds)
+               {
+                       if (button.Enabled)
+                               TextRenderer.DrawTextInternal (g, button.Text, button.Font, textBounds, button.ForeColor, button.TextFormatFlags, button.UseCompatibleTextRendering);
+                       else
+                               DrawStringDisabled20 (g, button.Text, button.Font, textBounds, button.BackColor, button.TextFormatFlags, button.UseCompatibleTextRendering);
+               }
+               #endregion
+
+               #region FlatStyle Button Style
+               public override void DrawFlatButton (Graphics g, Button b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
+               {
+                       // Draw Button Background
+                       DrawFlatButtonBackground (g, b, clipRectangle);
+
+                       // If we have an image, draw it
+                       if (imageBounds.Size != Size.Empty)
+                               DrawFlatButtonImage (g, b, imageBounds);
+
+                       // If we're focused, draw a focus rectangle
+                       if (b.Focused && b.Enabled)
+                               DrawFlatButtonFocus (g, b);
+
+                       // If we have text, draw it
+                       if (textBounds != Rectangle.Empty)
+                               DrawFlatButtonText (g, b, textBounds);
+               }
+
+               public virtual void DrawFlatButtonBackground (Graphics g, Button button, Rectangle clipArea)
+               {
+                       if (button.Pressed)
+                               ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor, button.FlatAppearance);
+                       else if (button.InternalSelected)
+                               ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor, button.FlatAppearance);
+                       else if (button.Entered)
+                               ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor, button.FlatAppearance);
+                       else if (!button.Enabled)
+                               ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor, button.FlatAppearance);
+                       else
+                               ThemeElements.DrawFlatButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor, button.FlatAppearance);
+               }
+
+               public virtual void DrawFlatButtonFocus (Graphics g, Button button)
+               {
+                       if (!button.Pressed) {
+                               Color focus_color = ControlPaint.Light(button.BackColor);
+                               g.DrawRectangle (ResPool.GetPen (focus_color), new Rectangle (button.ClientRectangle.Left + 4, button.ClientRectangle.Top + 4, button.ClientRectangle.Width - 9, button.ClientRectangle.Height - 9));
+                       }
+               }
+
+               public virtual void DrawFlatButtonImage (Graphics g, Button button, Rectangle imageBounds)
+               {
+                       // No changes from Standard for image for this theme
+                       DrawButtonImage (g, button, imageBounds);
+               }
+
+               public virtual void DrawFlatButtonText (Graphics g, Button button, Rectangle textBounds)
+               {
+                       // No changes from Standard for image for this theme
+                       DrawButtonText (g, button, textBounds);
+               }
+               #endregion
+
+               #region Popup Button Style
+               public override void DrawPopupButton (Graphics g, Button b, Rectangle textBounds, Rectangle imageBounds, Rectangle clipRectangle)
+               {
+                       // Draw Button Background
+                       DrawPopupButtonBackground (g, b, clipRectangle);
+
+                       // If we have an image, draw it
+                       if (imageBounds.Size != Size.Empty)
+                               DrawPopupButtonImage (g, b, imageBounds);
+
+                       // If we're focused, draw a focus rectangle
+                       if (b.Focused && b.Enabled)
+                               DrawPopupButtonFocus (g, b);
+
+                       // If we have text, draw it
+                       if (textBounds != Rectangle.Empty)
+                               DrawPopupButtonText (g, b, textBounds);
+               }
+
+               public virtual void DrawPopupButtonBackground (Graphics g, Button button, Rectangle clipArea)
+               {
+                       if (button.Pressed)
+                               ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Pressed, button.BackColor, button.ForeColor);
+                       else if (button.Entered)
+                               ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Entered, button.BackColor, button.ForeColor);
+                       else if (button.InternalSelected)
+                               ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Default, button.BackColor, button.ForeColor);
+                       else if (!button.Enabled)
+                               ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Disabled, button.BackColor, button.ForeColor);
+                       else
+                               ThemeElements.DrawPopupButton (g, button.ClientRectangle, ButtonThemeState.Normal, button.BackColor, button.ForeColor);
+               }
+
+               public virtual void DrawPopupButtonFocus (Graphics g, Button button)
+               {
+                       // No changes from Standard for image for this theme
+                       DrawButtonFocus (g, button);
+               }
+
+               public virtual void DrawPopupButtonImage (Graphics g, Button button, Rectangle imageBounds)
+               {
+                       // No changes from Standard for image for this theme
+                       DrawButtonImage (g, button, imageBounds);
+               }
+
+               public virtual void DrawPopupButtonText (Graphics g, Button button, Rectangle textBounds)
+               {
+                       // No changes from Standard for image for this theme
+                       DrawButtonText (g, button, textBounds);
+               }
+               #endregion
+
+               #region Button Layout Calculations
+               public override void CalculateButtonTextAndImageLayout (Button button, out Rectangle textRectangle, out Rectangle imageRectangle)
+               {
+                       Image image = button.Image;
+                       string text = button.Text;
+                       Rectangle content_rect = button.ClientRectangle;
+                       Size text_size = TextRenderer.MeasureTextInternal (text, button.Font, content_rect.Size, button.TextFormatFlags, button.UseCompatibleTextRendering);
+                       Size image_size = image == null ? Size.Empty : image.Size;
+
+                       textRectangle = Rectangle.Empty;
+                       imageRectangle = Rectangle.Empty;
+                       
+                       switch (button.TextImageRelation) {
+                               case TextImageRelation.Overlay:
+                                       // Overlay is easy, text always goes here
+                                       textRectangle = Rectangle.Inflate (content_rect, -4, -4);
+
+                                       if (button.Pressed)
+                                               textRectangle.Offset (1, 1);
+                                               
+                                       // Image is dependent on ImageAlign
+                                       if (image == null)
+                                               return;
+                                               
+                                       int image_x = 0;
+                                       int image_y = 0;
+                                       int image_height = image.Height;
+                                       int image_width = image.Width;
+                                       
+                                       switch (button.ImageAlign) {
+                                               case System.Drawing.ContentAlignment.TopLeft:
+                                                       image_x = 5;
+                                                       image_y = 5;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.TopCenter:
+                                                       image_x = (content_rect.Width - image_width) / 2;
+                                                       image_y = 5;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.TopRight:
+                                                       image_x = content_rect.Width - image_width - 5;
+                                                       image_y = 5;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.MiddleLeft:
+                                                       image_x = 5;
+                                                       image_y = (content_rect.Height - image_height) / 2;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.MiddleCenter:
+                                                       image_x = (content_rect.Width - image_width) / 2;
+                                                       image_y = (content_rect.Height - image_height) / 2;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.MiddleRight:
+                                                       image_x = content_rect.Width - image_width - 4;
+                                                       image_y = (content_rect.Height - image_height) / 2;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.BottomLeft:
+                                                       image_x = 5;
+                                                       image_y = content_rect.Height - image_height - 4;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.BottomCenter:
+                                                       image_x = (content_rect.Width - image_width) / 2;
+                                                       image_y = content_rect.Height - image_height - 4;
+                                                       break;
+                                               case System.Drawing.ContentAlignment.BottomRight:
+                                                       image_x = content_rect.Width - image_width - 4;
+                                                       image_y = content_rect.Height - image_height - 4;
+                                                       break;
+                                               default:
+                                                       image_x = 5;
+                                                       image_y = 5;
+                                                       break;
+                                       }
+                                       
+                                       imageRectangle = new Rectangle (image_x, image_y, image_width, image_height);
+                                       break;
+                               case TextImageRelation.ImageAboveText:
+                                       content_rect.Inflate (-4, -4);
+                                       LayoutTextAboveOrBelowImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+                                       break;
+                               case TextImageRelation.TextAboveImage:
+                                       content_rect.Inflate (-4, -4);
+                                       LayoutTextAboveOrBelowImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+                                       break;
+                               case TextImageRelation.ImageBeforeText:
+                                       content_rect.Inflate (-4, -4);
+                                       LayoutTextBeforeOrAfterImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+                                       break;
+                               case TextImageRelation.TextBeforeImage:
+                                       content_rect.Inflate (-4, -4);
+                                       LayoutTextBeforeOrAfterImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+                                       break;
+                       }
+               }
+
+               private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, System.Drawing.ContentAlignment textAlign, System.Drawing.ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
+               {
+                       int element_spacing = 0;        // Spacing between the Text and the Image
+                       int total_width = textSize.Width + element_spacing + imageSize.Width;
                        
-                       // First, draw the image
-                       if (button.FlatStyle != FlatStyle.System && ((button.image != null) || (button.image_list != null)))
-                               ButtonBase_DrawImage(button, dc);
+                       if (!textFirst)
+                               element_spacing += 2;
+                               
+                       // If the text is too big, chop it down to the size we have available to it
+                       if (total_width > totalArea.Width) {
+                               textSize.Width = totalArea.Width - element_spacing - imageSize.Width;
+                               total_width = totalArea.Width;
+                       }
                        
+                       int excess_width = totalArea.Width - total_width;
+                       int offset = 0;
+
+                       Rectangle final_text_rect;
+                       Rectangle final_image_rect;
+
+                       HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
+                       HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
+
+                       if (h_image == HorizontalAlignment.Left)
+                               offset = 0;
+                       else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
+                               offset = excess_width;
+                       else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
+                               offset += (int)(excess_width / 3);
+                       else
+                               offset += (int)(2 * (excess_width / 3));
+
+                       if (textFirst) {
+                               final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
+                               final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
+                       }
+                       else {
+                               final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
+                               final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
+                       }
+
+                       textRect = final_text_rect;
+                       imageRect = final_image_rect;
+               }
+
+               private void LayoutTextAboveOrBelowImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, System.Drawing.ContentAlignment textAlign, System.Drawing.ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
+               {
+                       int element_spacing = 0;        // Spacing between the Text and the Image
+                       int total_height = textSize.Height + element_spacing + imageSize.Height;
+
+                       if (textFirst)
+                               element_spacing += 2;
+
+                       if (textSize.Width > totalArea.Width)
+                               textSize.Width = totalArea.Width;
+                               
+                       // If the there isn't enough room and we're text first, cut out the image
+                       if (total_height > totalArea.Height && textFirst) {
+                               imageSize = Size.Empty;
+                               total_height = totalArea.Height;
+                       }
+
+                       int excess_height = totalArea.Height - total_height;
+                       int offset = 0;
+
+                       Rectangle final_text_rect;
+                       Rectangle final_image_rect;
+
+                       VerticalAlignment v_text = GetVerticalAlignment (textAlign);
+                       VerticalAlignment v_image = GetVerticalAlignment (imageAlign);
+
+                       if (v_image == VerticalAlignment.Top)
+                               offset = 0;
+                       else if (v_image == VerticalAlignment.Bottom && v_text == VerticalAlignment.Bottom)
+                               offset = excess_height;
+                       else if (v_image == VerticalAlignment.Center && (v_text == VerticalAlignment.Top || v_text == VerticalAlignment.Center))
+                               offset += (int)(excess_height / 3);
+                       else
+                               offset += (int)(2 * (excess_height / 3));
+
+                       if (textFirst) {
+                               final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, totalArea.Top + offset, textSize.Width, textSize.Height);
+                               final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, final_text_rect.Bottom + element_spacing, imageSize.Width, imageSize.Height);
+                       }
+                       else {
+                               final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, totalArea.Top + offset, imageSize.Width, imageSize.Height);
+                               final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, final_image_rect.Bottom + element_spacing, textSize.Width, textSize.Height);
+                               
+                               if (final_text_rect.Bottom > totalArea.Bottom)
+                                       final_text_rect.Y = totalArea.Top;
+                       }
+
+                       textRect = final_text_rect;
+                       imageRect = final_image_rect;
+               }
+               
+               private HorizontalAlignment GetHorizontalAlignment (System.Drawing.ContentAlignment align)
+               {
+                       switch (align) {
+                               case System.Drawing.ContentAlignment.BottomLeft:
+                               case System.Drawing.ContentAlignment.MiddleLeft:
+                               case System.Drawing.ContentAlignment.TopLeft:
+                                       return HorizontalAlignment.Left;
+                               case System.Drawing.ContentAlignment.BottomCenter:
+                               case System.Drawing.ContentAlignment.MiddleCenter:
+                               case System.Drawing.ContentAlignment.TopCenter:
+                                       return HorizontalAlignment.Center;
+                               case System.Drawing.ContentAlignment.BottomRight:
+                               case System.Drawing.ContentAlignment.MiddleRight:
+                               case System.Drawing.ContentAlignment.TopRight:
+                                       return HorizontalAlignment.Right;
+                       }
+
+                       return HorizontalAlignment.Left;
+               }
+
+               private enum VerticalAlignment
+               {
+                       Top = 0,
+                       Center = 1,
+                       Bottom = 2
+               }
+               
+               private VerticalAlignment GetVerticalAlignment (System.Drawing.ContentAlignment align)
+               {
+                       switch (align) {
+                               case System.Drawing.ContentAlignment.TopLeft:
+                               case System.Drawing.ContentAlignment.TopCenter:
+                               case System.Drawing.ContentAlignment.TopRight:
+                                       return VerticalAlignment.Top;
+                               case System.Drawing.ContentAlignment.MiddleLeft:
+                               case System.Drawing.ContentAlignment.MiddleCenter:
+                               case System.Drawing.ContentAlignment.MiddleRight:
+                                       return VerticalAlignment.Center;
+                               case System.Drawing.ContentAlignment.BottomLeft:
+                               case System.Drawing.ContentAlignment.BottomCenter:
+                               case System.Drawing.ContentAlignment.BottomRight:
+                                       return VerticalAlignment.Bottom;
+                       }
+
+                       return VerticalAlignment.Top;
+               }
+
+               internal Rectangle AlignInRectangle (Rectangle outer, Size inner, System.Drawing.ContentAlignment align)
+               {
+                       int x = 0;
+                       int y = 0;
+
+                       if (align == System.Drawing.ContentAlignment.BottomLeft || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.TopLeft)
+                               x = outer.X;
+                       else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.TopCenter)
+                               x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
+                       else if (align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.MiddleRight || align == System.Drawing.ContentAlignment.TopRight)
+                               x = outer.Right - inner.Width;
+                       if (align == System.Drawing.ContentAlignment.TopCenter || align == System.Drawing.ContentAlignment.TopLeft || align == System.Drawing.ContentAlignment.TopRight)
+                               y = outer.Y;
+                       else if (align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.MiddleRight)
+                               y = outer.Y + (outer.Height - inner.Height) / 2;
+                       else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.BottomLeft)
+                               y = outer.Bottom - inner.Height;
+
+                       return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
+               }
+               #endregion
+               #endregion
+
+               #region ButtonBase
+               public override void DrawButtonBase(Graphics dc, Rectangle clip_area, ButtonBase button)
+               {
                        // Draw the button: Draw border, etc.
                        ButtonBase_DrawButton(button, dc);
+
+                       // Draw the image
+                       if (button.FlatStyle != FlatStyle.System && ((button.image != null) || (button.image_list != null)))
+                               ButtonBase_DrawImage(button, dc);
                        
                        // Draw the focus rectangle
                        if ((button.Focused || button.paint_as_acceptbutton) && button.Enabled)
@@ -277,7 +701,7 @@ namespace System.Windows.Forms
                        int height = button.ClientSize.Height;
 
                        if (button.ImageIndex != -1) {   // We use ImageIndex instead of image_index since it will return -1 if image_list is null
-                               i = button.image_list.Images[button.image_index];
+                               i = button.image_list.Images[button.ImageIndex];
                        } else {
                                i = button.image;
                        }
@@ -285,7 +709,7 @@ namespace System.Windows.Forms
                        image_width = i.Width;
                        image_height = i.Height;
                        
-                       switch (button.image_alignment) {
+                       switch (button.ImageAlign) {
                                case ContentAlignment.TopLeft: {
                                        image_x = 5;
                                        image_y = 5;
@@ -347,12 +771,14 @@ namespace System.Windows.Forms
                                }
                        }
                        
-                       if (button.Enabled) {
-                               dc.DrawImage(i, image_x, image_y, image_width, image_height); 
-                       }
-                       else {
-                               CPDrawImageDisabled(dc, i, image_x, image_y, ColorControl);
-                       }
+                       dc.SetClip (new Rectangle(3, 3, width - 5, height - 5));
+
+                       if (button.Enabled)
+                               dc.DrawImage (i, image_x, image_y, image_width, image_height);
+                       else
+                               CPDrawImageDisabled (dc, i, image_x, image_y, ColorControl);
+
+                       dc.ResetClip ();
                }
                
                protected virtual void ButtonBase_DrawFocus(ButtonBase button, Graphics dc)
@@ -419,9 +845,12 @@ namespace System.Windows.Forms
                        checkbox_rectangle = new Rectangle(text_rectangle.X, text_rectangle.Y, checkmark_size, checkmark_size);
 
                        text_format = new StringFormat();
-                       text_format.Alignment=StringAlignment.Near;
-                       text_format.LineAlignment=StringAlignment.Center;
-                       text_format.HotkeyPrefix = HotkeyPrefix.Show;
+                       text_format.Alignment = StringAlignment.Near;
+                       text_format.LineAlignment = StringAlignment.Center;
+                       if (checkbox.ShowKeyboardCuesInternal)
+                               text_format.HotkeyPrefix = HotkeyPrefix.Show;
+                       else
+                               text_format.HotkeyPrefix = HotkeyPrefix.Hide;
 
                        /* Calculate the position of text and checkbox rectangle */
                        if (checkbox.appearance!=Appearance.Button) {
@@ -951,10 +1380,10 @@ namespace System.Windows.Forms
 
                public override void DataGridPaintColumnHeaders (Graphics g, Rectangle clip, DataGrid grid)
                {
-                       Rectangle columns_area = grid.columnhdrs_area;
+                       Rectangle columns_area = grid.column_headers_area;
 
                        if (grid.CurrentTableStyle.CurrentRowHeadersVisible) { // Paint corner shared between row and column header
-                               Rectangle rect_bloc = grid.columnhdrs_area;
+                               Rectangle rect_bloc = grid.column_headers_area;
                                rect_bloc.Width = grid.RowHeaderWidth;
                                if (clip.IntersectsWith (rect_bloc)) {
                                        if (grid.VisibleColumnCount > 0)
@@ -968,11 +1397,11 @@ namespace System.Windows.Forms
                        }
 
                        // Set unused area
-                       Rectangle columnhdrs_area_complete = columns_area;
-                       columnhdrs_area_complete.Width = grid.columnhdrs_maxwidth;
+                       Rectangle column_headers_area_complete = columns_area;
+                       column_headers_area_complete.Width = grid.column_headers_max_width;
                        
                        if (grid.CurrentTableStyle.CurrentRowHeadersVisible) {
-                               columnhdrs_area_complete.Width -= grid.RowHeaderWidth;
+                               column_headers_area_complete.Width -= grid.RowHeaderWidth;
                        }               
 
                        // Set column painting
@@ -1007,7 +1436,7 @@ namespace System.Windows.Forms
 
                        g.Clip = prev_clip;
                                
-                       Rectangle not_usedarea = columnhdrs_area_complete;                              
+                       Rectangle not_usedarea = column_headers_area_complete;                          
                        not_usedarea.X = rect_columnhdr.X + rect_columnhdr.Width;
                        not_usedarea.Width = grid.ClientRectangle.X + grid.ClientRectangle.Width - rect_columnhdr.X - rect_columnhdr.Height;            
                        g.FillRectangle (ResPool.GetSolidBrush (grid.BackgroundColor), not_usedarea);
@@ -1022,7 +1451,7 @@ namespace System.Windows.Forms
                        rect_row.Width = grid.ParentRowsArea.Width;
                        rect_row.Height = (grid.CaptionFont.Height + 3);
 
-                       object[] parentRows = grid.dataSourceStack.ToArray();
+                       object[] parentRows = grid.data_source_stack.ToArray();
                        
                        Region current_clip;
                        Region prev_clip = g.Clip;
@@ -1672,33 +2101,85 @@ namespace System.Windows.Forms
                #endregion      // Label
 
                #region LinkLabel
-               public  override void DrawLinkLabel (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
+
+               private Color LinkLabelGetPieceColor (LinkLabel label, LinkLabel.Piece piece, int i)
+               {
+                       if (!label.Enabled)
+                               return label.DisabledLinkColor;
+
+                       if (piece.link == null)
+                               return label.ForeColor;
+
+                       if (!piece.link.Enabled)
+                               return label.DisabledLinkColor;
+                               
+                       if (piece.link.Active)
+                               return label.ActiveLinkColor;
+                               
+                       if ((label.LinkVisited && i == 0) || piece.link.Visited)
+                               return label.VisitedLinkColor;
+                       
+                       return label.LinkColor;
+               }
+               
+               public override void DrawLinkLabel (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
                {
                        dc.FillRectangle (GetControlBackBrush (label.BackColor), clip_rectangle);
 
                        if (label.pieces == null)
                                return;
 
-                       for (int i = 0; i < label.pieces.Length; i ++) {
-                               RectangleF clipf = new RectangleF (clip_rectangle.X, clip_rectangle.Y,
-                                                                  clip_rectangle.Width, clip_rectangle.Height);
-                               RectangleF rectf = label.pieces[i].region.GetBounds (dc);
+                       // Paint all text as disabled.
+                       if (!label.Enabled) {
+                               dc.SetClip (clip_rectangle);
+                               CPDrawStringDisabled (dc, label.Text, label.Font, label.BackColor, label.ClientRectangle, label.string_format);
+                               return;
+                       }
+
+                       Font font, link_font = GetLinkFont (label);
+                       
+                       Region text_region = new Region (new Rectangle());
 
-                               if (!clipf.IntersectsWith (rectf))
+                       // Draw links.
+                       for (int i = 0; i < label.pieces.Length; i ++) {
+                               LinkLabel.Piece piece = label.pieces[i];
+                               
+                               if (piece.link == null) {
+                                       text_region.Union (piece.region);
                                        continue;
+                               }
 
-                               dc.DrawString (label.pieces[i].text, label.GetPieceFont (label.pieces[i]), ResPool.GetSolidBrush (label.GetPieceColor (label.pieces[i], i)),
-                                              rectf, label.string_format);
+                               Color color = LinkLabelGetPieceColor (label, piece, i);
 
-                               LinkLabel.Link link = label.pieces[i].link;
-                               if (link != null && link.Focused) {
-                                       Rectangle rect = new Rectangle ((int)rectf.X, (int)rectf.Y,
-                                                                       (int)rectf.Width, (int)rectf.Height);
-                                       CPDrawFocusRectangle (dc, rect, label.ForeColor, label.BackColor);
+                               if ( (label.LinkBehavior == LinkBehavior.AlwaysUnderline) || 
+                                        (label.LinkBehavior == LinkBehavior.SystemDefault) ||
+                                        ((label.LinkBehavior == LinkBehavior.HoverUnderline) && piece.link.Hovered) )
+                                       font = link_font;
+                               else
+                                       font = label.Font;
+                               
+                               dc.Clip = piece.region;
+                               dc.Clip.Intersect (clip_rectangle);
+                               dc.DrawString (label.Text, font, ResPool.GetSolidBrush (color), label.ClientRectangle, label.string_format);
+                       
+                               // Draw focus rectangle
+                               if ((piece.link != null) && piece.link.Focused) {
+                                       foreach (RectangleF rect in piece.region.GetRegionScans (dc.Transform))
+                                               ControlPaint.DrawFocusRectangle (dc, Rectangle.Round (rect), label.ForeColor, label.BackColor);
                                }
                        }
+                       
+                       // Draw normal text (without links).
+                       if (!text_region.IsEmpty (dc)) {
+                               dc.Clip = text_region;
+                               dc.Clip.Intersect (clip_rectangle);
+                               if (!dc.Clip.IsEmpty (dc))
+                                       dc.DrawString(label.Text, label.Font, ResPool.GetSolidBrush(label.ForeColor), label.ClientRectangle, label.string_format);
+                       }
                }
+               
                #endregion      // LinkLabel
+
                #region ListBox
 
                public override void DrawListBoxItem (ListBox ctrl, DrawItemEventArgs e)
@@ -1794,7 +2275,7 @@ namespace System.Windows.Forms
                                
                        // border is drawn directly in the Paint method
                        if (details && control.HeaderStyle != ColumnHeaderStyle.None) {                         
-                               dc.FillRectangle (GetControlBackBrush (control.BackColor),
+                               dc.FillRectangle (SystemBrushes.Control,
                                                  0, 0, control.TotalWidth, control.Font.Height + 5);
                                if (control.Columns.Count > 0) {
                                        foreach (ColumnHeader col in control.Columns) {
@@ -1848,20 +2329,10 @@ namespace System.Windows.Forms
 
                protected virtual void DrawListViewItem (Graphics dc, ListView control, ListViewItem item)
                {                               
-                       int col_offset;
-                       if (control.View == View.Details && control.Columns.Count > 0)
-                               col_offset = control.Columns [0].Rect.X;
-                       else
-                               col_offset = 0;
-                       
                        Rectangle rect_checkrect = item.CheckRectReal;
-                       rect_checkrect.X += col_offset;
                        Rectangle icon_rect = item.GetBounds (ItemBoundsPortion.Icon);
-                       icon_rect.X += col_offset;
                        Rectangle full_rect = item.GetBounds (ItemBoundsPortion.Entire);
-                       full_rect.X += col_offset;
                        Rectangle text_rect = item.GetBounds (ItemBoundsPortion.Label);                 
-                       text_rect.X += col_offset;
 
 #if NET_2_0
                        // Tile view doesn't support CheckBoxes
@@ -1891,26 +2362,34 @@ namespace System.Windows.Forms
                                                // adjustments to get the check-mark at the right place
                                                rect.X ++; rect.Y ++;
                                                // following logic is taken from DrawFrameControl method
+                                               int x_offset = rect.Width / 5;
+                                               int y_offset = rect.Height / 3;
                                                for (int i = 0; i < check_wd; i++) {
-                                                       dc.DrawLine (check_pen, rect.Left + check_wd / 2,
-                                                                    rect.Top + check_wd + i,
-                                                                    rect.Left + check_wd / 2 + 2 * scale,
-                                                                    rect.Top + check_wd + 2 * scale + i);
+                                                       dc.DrawLine (check_pen, rect.Left + x_offset,
+                                                                    rect.Top + y_offset + i,
+                                                                    rect.Left + x_offset + 2 * scale,
+                                                                    rect.Top + y_offset + 2 * scale + i);
                                                        dc.DrawLine (check_pen,
-                                                                    rect.Left + check_wd / 2 + 2 * scale,
-                                                                    rect.Top + check_wd + 2 * scale + i,
-                                                                    rect.Left + check_wd / 2 + 6 * scale,
-                                                                    rect.Top + check_wd - 2 * scale + i);
+                                                                    rect.Left + x_offset + 2 * scale,
+                                                                    rect.Top + y_offset + 2 * scale + i,
+                                                                    rect.Left + x_offset + 6 * scale,
+                                                                    rect.Top + y_offset - 2 * scale + i);
                                                }
                                        }
                                }
                                else {
-                                       if (item.Checked && control.StateImageList.Images.Count > 1)
-                                               control.StateImageList.Draw (dc,
-                                                                            rect_checkrect.Location, 1);
-                                       else if (! item.Checked && control.StateImageList.Images.Count > 0)
-                                               control.StateImageList.Draw (dc,
-                                                                            rect_checkrect.Location, 0);
+                                       int simage_idx;
+                                       if (item.Checked)
+#if NET_2_0
+                                               simage_idx = control.StateImageList.Images.Count > 1 ? 1 : -1;
+#else
+                                               simage_idx = control.StateImageList.Images.Count > 1 ? 1 : 0;
+#endif
+                                       else
+                                               simage_idx = control.StateImageList.Images.Count > 0 ? 0 : -1;
+
+                                       if (simage_idx > -1)
+                                               control.StateImageList.Draw (dc, rect_checkrect.Location, simage_idx);
                                }
                        }
 
@@ -1936,7 +2415,7 @@ namespace System.Windows.Forms
                        // draw the item text                   
                        // format for the item text
                        StringFormat format = new StringFormat ();
-                       if (control.View == View.SmallIcon)
+                       if (control.View == View.SmallIcon || control.View == View.LargeIcon)
                                format.LineAlignment = StringAlignment.Near;
                        else
                                format.LineAlignment = StringAlignment.Center;
@@ -1950,15 +2429,20 @@ namespace System.Windows.Forms
                        else
                                format.FormatFlags = StringFormatFlags.NoWrap;
 
+                       if ((control.View == View.LargeIcon && !item.Focused)
+                                       || control.View == View.Details 
+#if NET_2_0
+                                       || control.View == View.Tile
+#endif
+                          )
+                               format.Trimming = StringTrimming.EllipsisCharacter;
+
                        Rectangle highlight_rect = text_rect;
                        if (control.View == View.Details) { // Adjustments for Details view
                                Size text_size = Size.Ceiling (dc.MeasureString (item.Text, item.Font));
 
                                if (!control.FullRowSelect) // Selection shouldn't be outside the item bounds
                                        highlight_rect.Width = Math.Min (text_size.Width + 4, text_rect.Width);
-
-                               if (text_size.Width > text_rect.Width)
-                                       format.Trimming = StringTrimming.EllipsisCharacter;
                        }
 
                        if (item.Selected && control.Focused)
@@ -2143,6 +2627,7 @@ namespace System.Windows.Forms
                #endregion      // ListView
                
                #region Menus
+               
                public override void CalcItemSize (Graphics dc, MenuItem item, int y, int x, bool menuBar)
                {
                        item.X = x;
@@ -2269,8 +2754,8 @@ namespace System.Windows.Forms
                        if (menu.Height == 0)
                                CalcMenuBarSize (dc, menu, rect.Width);
 
-                       bool keynav = (menu as MainMenu).tracker.Navigating;
-                       HotkeyPrefix hp = always_draw_hotkeys || keynav ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
+                       bool keynav = (menu as MainMenu).tracker.hotkey_active;
+                       HotkeyPrefix hp = MenuAccessKeysUnderlined || keynav ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
                        string_format_menu_menubar_text.HotkeyPrefix = hp;
                        string_format_menu_text.HotkeyPrefix = hp;
 
@@ -2968,6 +3453,20 @@ namespace System.Windows.Forms
                                        dc.FillRectangle(GetControlBackBrush (pb.BackColor), clip);
                                        dc.DrawImage (pb.Image, (client.Width / 2) - (pb.Image.Width / 2), (client.Height / 2) - (pb.Image.Height / 2));
                                        break;
+#if NET_2_0
+                               case PictureBoxSizeMode.Zoom:
+                                       dc.FillRectangle (GetControlBackBrush (pb.BackColor), clip);
+                                       
+                                       Size image_size;
+                                       
+                                       if (((float)pb.Image.Width / (float)pb.Image.Height) >= ((float)client.Width / (float)client.Height))
+                                               image_size = new Size (client.Width, (pb.Image.Height * client.Width) / pb.Image.Width);
+                                       else
+                                               image_size = new Size ((pb.Image.Width * client.Height) / pb.Image.Height, client.Height);
+
+                                       dc.DrawImage (pb.Image, (client.Width / 2) - (image_size.Width / 2), (client.Height / 2) - (image_size.Height / 2), image_size.Width, image_size.Height);
+                                       break;
+#endif
                                default:
                                        dc.FillRectangle(GetControlBackBrush (pb.BackColor), clip);
                                        // Normal, AutoSize
@@ -3075,29 +3574,70 @@ namespace System.Windows.Forms
                #region ProgressBar
                public override void DrawProgressBar (Graphics dc, Rectangle clip_rect, ProgressBar ctrl) 
                {
-                       Rectangle       block_rect;
-                       Rectangle       client_area = ctrl.client_area;
-                       int             space_betweenblocks     = 2;                    
-                       int             block_width;
-                       int             increment;
-                       int             barpos_pixels;
+                       Rectangle client_area = ctrl.client_area;
                        
-                       block_width = (client_area.Height * 2 ) / 3;
-                       barpos_pixels = ((ctrl.Value - ctrl.Minimum) * client_area.Width) / (ctrl.Maximum - ctrl.Minimum);
-                       increment = block_width + space_betweenblocks;
-
                        /* Draw border */
                        CPDrawBorder3D (dc, ctrl.ClientRectangle, Border3DStyle.SunkenOuter, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom & ~Border3DSide.Middle, ColorControl);
                        
                        /* Draw Blocks */
-                       block_rect = new Rectangle (client_area.X, client_area.Y, block_width, client_area.Height);
-                       while ((block_rect.X - client_area.X) < barpos_pixels) {
+                       int draw_mode = 0;
+                       int max_blocks = int.MaxValue;
+                       int start_pixel = client_area.X;
+#if NET_2_0
+                       draw_mode = (int) ctrl.Style;
+#endif
+                       switch (draw_mode) {
+#if NET_2_0
+                       case 1: { // Continuous
+                               int pixels_to_draw;
+                               pixels_to_draw = (int)(client_area.Width * ((double)(ctrl.Value - ctrl.Minimum) / (double)(ctrl.Maximum - ctrl.Minimum)));
+                               dc.FillRectangle (ResPool.GetSolidBrush (progressbarblock_color), new Rectangle (client_area.X, client_area.Y, pixels_to_draw, client_area.Height));
+                               break;
+                       }
+                       case 2: // Marquee
+                               if (XplatUI.ThemesEnabled) {
+                                       int ms_diff = (int) (DateTime.Now - ctrl.start).TotalMilliseconds;
+                                       double percent_done = (double) ms_diff % (double)ctrl.MarqueeAnimationSpeed / (double)ctrl.MarqueeAnimationSpeed;
+                                       max_blocks = 5;
+                                       start_pixel = client_area.X + (int) (client_area.Width * percent_done);
+                               }
                                
-                               if (clip_rect.IntersectsWith (block_rect) == true) {                            
-                                       dc.FillRectangle (ResPool.GetSolidBrush (progressbarblock_color), block_rect);
-                               }                               
+                               goto case 0;
+#endif
+                       case 0:
+                       default:  // Blocks
+                               Rectangle block_rect;
+                               int space_betweenblocks = 2;
+                               int block_width;
+                               int increment;
+                               int barpos_pixels;
+                               int block_count = 0;
                                
-                               block_rect.X  += increment;
+                               block_width = (client_area.Height * 2) / 3;
+                               barpos_pixels = ((ctrl.Value - ctrl.Minimum) * client_area.Width) / (ctrl.Maximum - ctrl.Minimum);
+                               increment = block_width + space_betweenblocks;
+                               
+                               block_rect = new Rectangle (start_pixel, client_area.Y, block_width, client_area.Height);
+                               while (true) {
+                                       if (max_blocks != int.MaxValue) {
+                                               if (block_count >= max_blocks)
+                                                       break;
+                                               if (block_rect.X > client_area.Width)
+                                                       block_rect.X -= client_area.Width;
+                                       } else {
+                                               if ((block_rect.X - client_area.X) >= barpos_pixels)
+                                                       break;
+                                       }
+                                       
+                                       if (clip_rect.IntersectsWith (block_rect) == true) {                            
+                                               dc.FillRectangle (ResPool.GetSolidBrush (progressbarblock_color), block_rect);
+                                       }                               
+                                       
+                                       block_rect.X  += increment;
+                                       block_count++;
+                               }
+                               break;
+                       
                        }
                }
                
@@ -3589,10 +4129,13 @@ namespace System.Windows.Forms
                #endregion      // ScrollBar
 
                #region StatusBar
-               public  override void DrawStatusBar (Graphics dc, Rectangle clip, StatusBar sb) {
+               public  override void DrawStatusBar (Graphics real_dc, Rectangle clip, StatusBar sb) {
                        Rectangle area = sb.ClientRectangle;
                        int horz_border = 2;
                        int vert_border = 2;
+
+                       Image backbuffer = new Bitmap (sb.ClientSize.Width, sb.ClientSize.Height, real_dc);
+                       Graphics dc = Graphics.FromImage (backbuffer);
                        
                        bool is_color_control = sb.BackColor.ToArgb () == ColorControl.ToArgb ();
 
@@ -3637,6 +4180,10 @@ namespace System.Windows.Forms
                                area = new Rectangle (area.Right - 16 - 2, area.Bottom - 12 - 1, 16, 16);
                                CPDrawSizeGrip (dc, ColorControl, area);
                        }
+                       
+                       real_dc.DrawImage (backbuffer, 0, 0);
+                       dc.Dispose ();
+                       backbuffer.Dispose ();
 
                }
 
@@ -3751,322 +4298,99 @@ namespace System.Windows.Forms
                }
                #endregion      // StatusBar
 
-               public override void DrawTabControl (Graphics dc, Rectangle area, TabControl tab)
-               {
-                       Brush brush = SystemBrushes.Control;
-                       dc.FillRectangle (brush, area);
-                       Rectangle panel_rect = GetTabPanelRectExt (tab);
-
-                       if (tab.Appearance == TabAppearance.Normal) {
-                               CPDrawBorder3D (dc, panel_rect, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top, ColorControl);
-                               CPDrawBorder3D (dc, panel_rect, Border3DStyle.Raised, Border3DSide.Right | Border3DSide.Bottom, ColorControl);
-                       }
-
-                       if (tab.Alignment == TabAlignment.Top) {
-                               for (int r = tab.TabPages.Count; r > 0; r--) {
-                                       for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
-                                               if (i == tab.SelectedIndex)
-                                                       continue;
-                                               if (r != tab.TabPages [i].Row)
-                                                       continue;
-                                               Rectangle rect = tab.GetTabRect (i);
-                                               if (!rect.IntersectsWith (area))
-                                                       continue;
-                                               DrawTab (dc, tab.TabPages [i], tab, rect, false);
-                                       }
-                               }
-                       } else {
-                               for (int r = 0; r < tab.TabPages.Count; r++) {
-                                       for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {
-                                               if (i == tab.SelectedIndex)
-                                                       continue;
-                                               if (r != tab.TabPages [i].Row)
-                                                       continue;
-                                               Rectangle rect = tab.GetTabRect (i);
-                                               if (!rect.IntersectsWith (area))
-                                                       continue;
-                                               DrawTab (dc, tab.TabPages [i], tab, rect, false);
-                                       }
-                               }
-                       }
+               #region TabControl
 
-                       if (tab.SelectedIndex != -1 && tab.SelectedIndex >= tab.SliderPos) {
-                               Rectangle rect = tab.GetTabRect (tab.SelectedIndex);
-                               if (rect.IntersectsWith (area))
-                                       DrawTab (dc, tab.TabPages [tab.SelectedIndex], tab, rect, true);
-                       }
+               #region TabControl settings
 
-                       if (tab.ShowSlider) {
-                               Rectangle right = GetTabControlRightScrollRect (tab);
-                               Rectangle left = GetTabControlLeftScrollRect (tab);
-                               CPDrawScrollButton (dc, right, ScrollButton.Right, tab.RightSliderState);
-                               CPDrawScrollButton (dc, left, ScrollButton.Left, tab.LeftSliderState);
-                       }
+               public override Size TabControlDefaultItemSize {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.DefaultItemSize; }
                }
 
-               public override Rectangle GetTabControlLeftScrollRect (TabControl tab)
-               {
-                       switch (tab.Alignment) {
-                       case TabAlignment.Top:
-                               return new Rectangle (tab.ClientRectangle.Right - 34, tab.ClientRectangle.Top + 1, 17, 17);
-                       default:
-                               Rectangle panel_rect = GetTabPanelRectExt (tab);
-                               return new Rectangle (tab.ClientRectangle.Right - 34, panel_rect.Bottom + 2, 17, 17);
-                       }
+               public override Point TabControlDefaultPadding {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.DefaultPadding; }
                }
 
-               public override Rectangle GetTabControlRightScrollRect (TabControl tab)
-               {
-                       switch (tab.Alignment) {
-                       case TabAlignment.Top:
-                               return new Rectangle (tab.ClientRectangle.Right - 17, tab.ClientRectangle.Top + 1, 17, 17);
-                       default:
-                               Rectangle panel_rect = GetTabPanelRectExt (tab);
-                               return new Rectangle (tab.ClientRectangle.Right - 17, panel_rect.Bottom + 2, 17, 17);
-                       }
+               public override int TabControlMinimumTabWidth {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.MinimumTabWidth; }
                }
 
-               public override Size TabControlDefaultItemSize {
-                       get { return new Size (42, 21); }
+               public override Rectangle TabControlSelectedDelta {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.SelectedTabDelta; }
                }
 
-               public override Point TabControlDefaultPadding {
-                       get { return new Point (6, 3); }
+               public override int TabControlSelectedSpacing {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.SelectedSpacing; }
                }
-
-               public override int TabControlMinimumTabWidth {
-                       get { return 42; }
+               
+               public override int TabPanelOffsetX {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.TabPanelOffset.X; }
                }
-
-               public override Rectangle GetTabControlDisplayRectangle (TabControl tab)
-               {
-                       Rectangle ext = GetTabPanelRectExt (tab);
-                       // Account for border size
-                       return new Rectangle (ext.Left + 2, ext.Top + 1, ext.Width - 6, ext.Height - 4);
-               }
-
-               public override Size TabControlGetSpacing (TabControl tab) {
-                       switch (tab.Appearance) {
-                               case TabAppearance.Normal:
-                                       return new Size (1, -2);
-                               case TabAppearance.Buttons:
-                                       return new Size (3, 3);
-                               case TabAppearance.FlatButtons:
-                                       return new Size (9, 3);
-                               default:
-                                       throw new Exception ("Invalid Appearance value: " + tab.Appearance);
-                               }
+               
+               public override int TabPanelOffsetY {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.TabPanelOffset.Y; }
                }
 
-               protected virtual Rectangle GetTabPanelRectExt (TabControl tab)
-               {
-                       // Offset the tab from the top corner
-                       Rectangle res = new Rectangle (tab.ClientRectangle.X + 2,
-                                       tab.ClientRectangle.Y,
-                                       tab.ClientRectangle.Width - 2,
-                                       tab.ClientRectangle.Height - 1);
-
-                       if (tab.TabCount == 0)
-                               return res;
-
-                       int spacing = TabControlGetSpacing (tab).Height;
-                       int offset = (tab.ItemSize.Height + spacing) * tab.RowCount + 3;
-
-                       switch (tab.Alignment) {
-                       case TabAlignment.Left:
-                               res.X += offset;
-                               res.Width -= offset;
-                               break;
-                       case TabAlignment.Right:
-                               res.Width -= offset;
-                               break;
-                       case TabAlignment.Top:
-                               res.Y += offset;
-                               res.Height -= offset;
-                               break;
-                       case TabAlignment.Bottom:
-                               res.Height -= offset;
-                               break;
-                       }
-
-                       return res;
+               public override int TabControlColSpacing {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.ColSpacing; }
                }
 
-               protected virtual int DrawTab (Graphics dc, TabPage page, TabControl tab, Rectangle bounds, bool is_selected)
-               {
-                       int FlatButtonSpacing = 8;
-                       Rectangle interior;
-                       int res = bounds.Width;
-
-                       
-                       
-                       // we can't fill the background right away because the bounds might be adjusted if the tab is selected
-
-                       StringFormat string_format = new StringFormat ();
-                       if (tab.Appearance == TabAppearance.Buttons || tab.Appearance == TabAppearance.FlatButtons) {
-                               dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);
-
-                               // Separators
-                               if (tab.Appearance == TabAppearance.FlatButtons) {
-                                       int width = bounds.Width;
-                                       bounds.Width += (FlatButtonSpacing - 2);
-                                       res = bounds.Width;
-                                       CPDrawBorder3D (dc, bounds, Border3DStyle.Etched, Border3DSide.Right);
-                                       bounds.Width = width;
-                               }
-
-                               if (is_selected) {
-                                       CPDrawBorder3D (dc, bounds, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
-                               } else if (tab.Appearance != TabAppearance.FlatButtons) {
-                                       CPDrawBorder3D (dc, bounds, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
-                               }
-
-                               interior = new Rectangle (bounds.Left + 2, bounds.Top + 2, bounds.Width - 4, bounds.Height - 4);
-
-                                
-                               string_format.Alignment = StringAlignment.Center;
-                               string_format.LineAlignment = StringAlignment.Center;
-                               string_format.FormatFlags = StringFormatFlags.NoWrap;
-                       } else {
-                               CPColor cpcolor = ResPool.GetCPColor (tab.BackColor);
-                               
-                               Pen light = ResPool.GetPen (cpcolor.LightLight);
-
-                               switch (tab.Alignment) {
-                                       
-                               case TabAlignment.Top:
-
-                                       dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);
-
-                                       dc.DrawLine (light, bounds.Left, bounds.Bottom, bounds.Left, bounds.Top + 3);
-                                       dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
-                                       dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right - 3, bounds.Top);
-
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 1, bounds.Top + 2, bounds.Right, bounds.Top + 3);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom);
-
-                                       interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
-
-                                       string_format.Alignment = StringAlignment.Center;
-                                       string_format.LineAlignment = StringAlignment.Center;
-                                       string_format.FormatFlags = StringFormatFlags.NoWrap;
-
-                                       break;
-
-                               case TabAlignment.Bottom:
-
-                                       dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);
-
-                                       dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 3);
-                                       dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left + 2, bounds.Bottom - 1);
-
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Left + 3, bounds.Bottom - 1, bounds.Right - 3, bounds.Bottom - 1);
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Bottom - 3, bounds.Right - 1, bounds.Top);
-
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 3, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 3, bounds.Bottom, bounds.Right, bounds.Bottom - 3);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom - 3, bounds.Right, bounds.Top);
-
-                                       interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
-
-                                       string_format.Alignment = StringAlignment.Center;
-                                       string_format.LineAlignment = StringAlignment.Center;
-                                       string_format.FormatFlags = StringFormatFlags.NoWrap;
-
-                                       break;
-
-                               case TabAlignment.Left:
-
-                                       dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);
-
-                                       dc.DrawLine (light, bounds.Left, bounds.Bottom - 3, bounds.Left, bounds.Top + 3);
-                                       dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 3, bounds.Top);
-                                       dc.DrawLine (light, bounds.Left + 3, bounds.Top, bounds.Right, bounds.Top);
-
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Right, bounds.Bottom - 1, bounds.Left + 2, bounds.Bottom - 1);
-
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Bottom, bounds.Left + 2, bounds.Bottom);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 2, bounds.Bottom, bounds.Left, bounds.Bottom - 3);
-
-                                       interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
-
-                                       string_format.Alignment = StringAlignment.Center;
-                                       string_format.LineAlignment = StringAlignment.Center;
-                                       string_format.FormatFlags = StringFormatFlags.NoWrap;
-                                       string_format.FormatFlags = StringFormatFlags.DirectionVertical;
-
-                                       break;
-
-                               default:
-                                       // TabAlignment.Right
-
-                                       dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);
-
-                                       dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Right - 3, bounds.Top);
-                                       dc.DrawLine (light, bounds.Right - 3, bounds.Top, bounds.Right, bounds.Top + 3);
-
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);
-                                       dc.DrawLine (SystemPens.ControlDark, bounds.Left, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
-
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
-                                       dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left, bounds.Bottom, bounds.Right - 3, bounds.Bottom);
+               public override Point TabControlImagePadding {
+                       get { return ThemeElements.CurrentTheme.TabControlPainter.ImagePadding; }
+               }
 
-                                       interior = new Rectangle (bounds.Left + 4, bounds.Top + 4, bounds.Width - 8, bounds.Height - 8);
+               public override int TabControlScrollerWidth {
+                       get {return ThemeElements.CurrentTheme.TabControlPainter.ScrollerWidth; }
+               }
 
-                                       string_format.Alignment = StringAlignment.Center;
-                                       string_format.LineAlignment = StringAlignment.Center;
-                                       string_format.FormatFlags = StringFormatFlags.NoWrap;
-                                       string_format.FormatFlags = StringFormatFlags.DirectionVertical;
 
-                                       break;
-                               }
+               public override Size TabControlGetSpacing (TabControl tab) 
+               {
+                       try {
+                               return ThemeElements.CurrentTheme.TabControlPainter.RowSpacing (tab);
+                       } catch {
+                               throw new Exception ("Invalid Appearance value: " + tab.Appearance);
                        }
+               }
+               #endregion
 
-                       if (tab.DrawMode == TabDrawMode.Normal && page.Text != null) {
-                               if (tab.Alignment == TabAlignment.Left) {
-                                       int wo = interior.Width / 2;
-                                       int ho = interior.Height / 2;
-                                       dc.TranslateTransform (interior.X + wo, interior.Y + ho);
-                                       dc.RotateTransform (180);
-                                       dc.DrawString (page.Text, page.Font, SystemBrushes.ControlText, 0, 0, string_format);
-                                       dc.ResetTransform ();
-                               } else {
-                                       Rectangle str_rect = interior;
+               public override void DrawTabControl (Graphics dc, Rectangle area, TabControl tab)
+               {
+                       ThemeElements.CurrentTheme.TabControlPainter.Draw (dc, area, tab);
+               }
 
-                                       if (tab.ImageList != null && page.ImageIndex >= 0 && page.ImageIndex < tab.ImageList.Images.Count) {
-                                               tab.ImageList.Draw (dc, new Point (interior.X, interior.Y), page.ImageIndex);
-                                               str_rect.X += tab.ImageList.ImageSize.Width + 2;
-                                               str_rect.Width -= tab.ImageList.ImageSize.Width + 2;
-                                       }
-                                       dc.DrawString (page.Text, page.Font,
-                                                      SystemBrushes.ControlText,
-                                                       str_rect, string_format);
-                               }
-                       } else if (page.Text != null) {
-                               DrawItemState state = DrawItemState.None;
-                               if (page == tab.SelectedTab)
-                                       state |= DrawItemState.Selected;
-                               DrawItemEventArgs e = new DrawItemEventArgs (dc,
-                                               tab.Font, bounds, tab.IndexForTabPage (page),
-                                               state, page.ForeColor, page.BackColor);
-                               tab.OnDrawItemInternal (e);
-                               return res;
-                       }
+               public override Rectangle TabControlGetLeftScrollRect (TabControl tab)
+               {
+                       return ThemeElements.CurrentTheme.TabControlPainter.GetLeftScrollRect (tab);
+               }
 
-                       if (page.Parent.Focused && is_selected) {
-                               CPDrawFocusRectangle (dc, interior, tab.ForeColor, tab.BackColor);
-                       }
+               public override Rectangle TabControlGetRightScrollRect (TabControl tab)
+               {
+                       return ThemeElements.CurrentTheme.TabControlPainter.GetRightScrollRect (tab);
+               }
+
+               public override Rectangle TabControlGetDisplayRectangle (TabControl tab)
+               {
+                       return ThemeElements.CurrentTheme.TabControlPainter.GetDisplayRectangle (tab);
+               }
 
-                       return res;
+               public override Rectangle TabControlGetPanelRect (TabControl tab)
+               {
+                       return ThemeElements.CurrentTheme.TabControlPainter.GetTabPanelRect (tab);
                }
 
+               #endregion
+
                #region ToolBar
                public  override void DrawToolBar (Graphics dc, Rectangle clip_rectangle, ToolBar control) 
                {
                        StringFormat format = new StringFormat ();
                        format.Trimming = StringTrimming.EllipsisCharacter;
                        format.LineAlignment = StringAlignment.Center;
+                       if (control.ShowKeyboardCuesInternal)
+                               format.HotkeyPrefix = HotkeyPrefix.Show;
+                       else
+                               format.HotkeyPrefix = HotkeyPrefix.Hide;
+
                        if (control.TextAlign == ToolBarTextAlign.Underneath)
                                format.Alignment = StringAlignment.Center;
                        else
@@ -4111,93 +4435,93 @@ namespace System.Windows.Forms
                                }
                        }
 
-                       foreach (ToolBarButton button in control.Buttons)
-                               if (button.Visible && clip_rectangle.IntersectsWith (button.Rectangle))
-                                       DrawToolBarButton (dc, control, button, format);
+                       foreach (ToolBarItem item in control.items)
+                               if (item.Button.Visible && clip_rectangle.IntersectsWith (item.Rectangle))
+                                       DrawToolBarButton (dc, control, item, format);
 
                        format.Dispose ();
                }
 
-               protected virtual void DrawToolBarButton (Graphics dc, ToolBar control, ToolBarButton button, StringFormat format)
+               protected virtual void DrawToolBarButton (Graphics dc, ToolBar control, ToolBarItem item, StringFormat format)
                {
-                       bool is_flat = control.Appearance == ToolBarAppearance.Flat;
-
-                       DrawToolBarButtonBorder (dc, button, is_flat);
+                       bool is_flat = (control.Appearance == ToolBarAppearance.Flat);
+                       
+                       DrawToolBarButtonBorder (dc, item, is_flat);
 
-                       switch (button.Style) {
+                       switch (item.Button.Style) {
                        case ToolBarButtonStyle.DropDownButton:
                                if (control.DropDownArrows)
-                                       DrawToolBarDropDownArrow (dc, button, is_flat);
-                               DrawToolBarButtonContents (dc, control, button, format);
+                                       DrawToolBarDropDownArrow (dc, item, is_flat);
+                               DrawToolBarButtonContents (dc, control, item, format);
                                break;
 
                        case ToolBarButtonStyle.Separator:
                                if (is_flat)
-                                       DrawToolBarSeparator (dc, button);
+                                       DrawToolBarSeparator (dc, item);
                                break;
 
                        case ToolBarButtonStyle.ToggleButton:
-                               DrawToolBarToggleButtonBackground (dc, button);
-                               DrawToolBarButtonContents (dc, control, button, format);
+                               DrawToolBarToggleButtonBackground (dc, item);
+                               DrawToolBarButtonContents (dc, control, item, format);
                                break;
 
                        default:
-                               DrawToolBarButtonContents (dc, control, button, format);
+                               DrawToolBarButtonContents (dc, control, item, format);
                                break;
                        }
                }
 
                const Border3DSide all_sides = Border3DSide.Left | Border3DSide.Top | Border3DSide.Right | Border3DSide.Bottom;
 
-               protected virtual void DrawToolBarButtonBorder (Graphics dc, ToolBarButton button, bool is_flat)
+               protected virtual void DrawToolBarButtonBorder (Graphics dc, ToolBarItem item, bool is_flat)
                {
-                       if (button.Style == ToolBarButtonStyle.Separator)
+                       if (item.Button.Style == ToolBarButtonStyle.Separator)
                                return;
 
                        Border3DStyle style;
 
                        if (is_flat) {
-                               if (button.Pushed || button.Pressed)
+                               if (item.Button.Pushed || item.Pressed)
                                        style = Border3DStyle.SunkenOuter;
-                               else if (button.Hilight)
+                               else if (item.Hilight)
                                        style = Border3DStyle.RaisedInner;
                                else
                                        return;
 
                        } else {
-                               if (button.Pushed || button.Pressed)
+                               if (item.Button.Pushed || item.Pressed)
                                        style = Border3DStyle.Sunken;
                                else 
                                        style = Border3DStyle.Raised;
                        }
                        
-                       Rectangle rect = button.Rectangle;
-                       if ((button.Style == ToolBarButtonStyle.DropDownButton) && (button.Parent.DropDownArrows) && is_flat)
+                       Rectangle rect = item.Rectangle;
+                       if ((item.Button.Style == ToolBarButtonStyle.DropDownButton) && (item.Button.Parent.DropDownArrows) && is_flat)
                                rect.Width -= ToolBarDropDownWidth;
 
                        CPDrawBorder3D (dc, rect, style, all_sides);
                }
 
-               protected virtual void DrawToolBarSeparator (Graphics dc, ToolBarButton button)
+               protected virtual void DrawToolBarSeparator (Graphics dc, ToolBarItem item)
                {
-                       Rectangle area = button.Rectangle;
+                       Rectangle area = item.Rectangle;
                        int offset = (int) SystemPens.Control.Width + 1;
                        dc.DrawLine (SystemPens.ControlDark, area.X + 1, area.Y, area.X + 1, area.Bottom);
                        dc.DrawLine (SystemPens.ControlLight, area.X + offset, area.Y, area.X + offset, area.Bottom);
                }
 
-               protected virtual void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarButton button)
+               protected virtual void DrawToolBarToggleButtonBackground (Graphics dc, ToolBarItem item)
                {
                        Brush brush;
-                       Rectangle area = button.Rectangle;
+                       Rectangle area = item.Rectangle;
                        area.X += ToolBarImageGripWidth;
                        area.Y += ToolBarImageGripWidth;
                        area.Width -= 2 * ToolBarImageGripWidth;
                        area.Height -= 2 * ToolBarImageGripWidth;
                        
-                       if (button.Pushed)
+                       if (item.Button.Pushed)
                                brush = (Brush) ResPool.GetHatchBrush (HatchStyle.Percent50, ColorScrollBar, ColorControlLightLight);
-                       else if (button.PartialPush)
+                       else if (item.Button.PartialPush)
                                brush = SystemBrushes.ControlLight;
                        else
                                brush = SystemBrushes.Control;
@@ -4205,23 +4529,23 @@ namespace System.Windows.Forms
                        dc.FillRectangle (brush, area);
                }
 
-               protected virtual void DrawToolBarDropDownArrow (Graphics dc, ToolBarButton button, bool is_flat)
+               protected virtual void DrawToolBarDropDownArrow (Graphics dc, ToolBarItem item, bool is_flat)
                {
-                       Rectangle rect = button.Rectangle;
-                       rect.X = button.Rectangle.Right - ToolBarDropDownWidth;
+                       Rectangle rect = item.Rectangle;
+                       rect.X = item.Rectangle.Right - ToolBarDropDownWidth;
                        rect.Width = ToolBarDropDownWidth;
                        
                        if (is_flat) {
-                               if (button.dd_pressed)
+                               if (item.DDPressed)
                                        CPDrawBorder3D (dc, rect, Border3DStyle.SunkenOuter, all_sides);
-                               else if (button.Pushed || button.Pressed)
+                               else if (item.Button.Pushed || item.Pressed)
                                        CPDrawBorder3D (dc, rect, Border3DStyle.SunkenOuter, all_sides);
-                               else if (button.Hilight)
+                               else if (item.Hilight)
                                        CPDrawBorder3D (dc, rect, Border3DStyle.RaisedInner, all_sides);
                        } else {
-                               if (button.dd_pressed)
+                               if (item.DDPressed)
                                        CPDrawBorder3D (dc, rect, Border3DStyle.Flat, all_sides);
-                               else if (button.Pushed || button.Pressed)
+                               else if (item.Button.Pushed || item.Pressed)
                                        CPDrawBorder3D (dc, Rectangle.Inflate(rect, -1, -1), Border3DStyle.SunkenOuter, all_sides);
                                else
                                        CPDrawBorder3D (dc, rect, Border3DStyle.Raised, all_sides);
@@ -4231,7 +4555,7 @@ namespace System.Windows.Forms
                        PointF ddCenter = new PointF (rect.X + (rect.Width/2.0f), rect.Y + (rect.Height / 2));
                        
                        // Increase vertical and horizontal position by 1 when button is pressed
-                       if (button.Pressed || button.Pushed || button.dd_pressed) {
+                       if (item.Pressed || item.Button.Pushed || item.DDPressed) {
                            ddCenter.X += 1;
                            ddCenter.Y += 1;
                        }
@@ -4245,37 +4569,37 @@ namespace System.Windows.Forms
                        dc.FillPolygon (SystemBrushes.ControlText, vertices);
                }
 
-               protected virtual void DrawToolBarButtonContents (Graphics dc, ToolBar control, ToolBarButton button, StringFormat format)
+               protected virtual void DrawToolBarButtonContents (Graphics dc, ToolBar control, ToolBarItem item, StringFormat format)
                {
-                       if (button.Image != null) {
-                               int x = button.ImageRectangle.X + ToolBarImageGripWidth;
-                               int y = button.ImageRectangle.Y + ToolBarImageGripWidth;
+                       if (item.Button.Image != null) {
+                               int x = item.ImageRectangle.X + ToolBarImageGripWidth;
+                               int y = item.ImageRectangle.Y + ToolBarImageGripWidth;
                                
                                // Increase vertical and horizontal position by 1 when button is pressed
-                               if (button.Pressed || button.Pushed) {
+                               if (item.Pressed || item.Button.Pushed) {
                                    x += 1;
                                    y += 1;
                                }
                                
-                               if (button.Enabled)
-                                       dc.DrawImage (button.Image, x, y);
+                               if (item.Button.Enabled)
+                                       dc.DrawImage (item.Button.Image, x, y);
                                else 
-                                       CPDrawImageDisabled (dc, button.Image, x, y, ColorControl);
+                                       CPDrawImageDisabled (dc, item.Button.Image, x, y, ColorControl);
                        }
 
-                       Rectangle text_rect = button.TextRectangle;
+                       Rectangle text_rect = item.TextRectangle;
                        if (text_rect.Width <= 0 || text_rect.Height <= 0)
                                return;
 
-                       if (button.Pressed || button.Pushed) {
+                       if (item.Pressed || item.Button.Pushed) {
                                text_rect.X += 1;
                                text_rect.Y += 1;
                        }
                        
-                       if (button.Enabled)
-                               dc.DrawString (button.Text, control.Font, SystemBrushes.ControlText, text_rect, format);
+                       if (item.Button.Enabled)
+                               dc.DrawString (item.Button.Text, control.Font, SystemBrushes.ControlText, text_rect, format);
                        else
-                               CPDrawStringDisabled (dc, button.Text, control.Font, control.BackColor, text_rect, format);
+                               CPDrawStringDisabled (dc, item.Button.Text, control.Font, control.BackColor, text_rect, format);
                }
 
                // Grip width for the ToolBar
@@ -4317,21 +4641,278 @@ namespace System.Windows.Forms
                #endregion      // ToolBar
 
                #region ToolTip
-               public override void DrawToolTip(Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control) {
+               public override void DrawToolTip(Graphics dc, Rectangle clip_rectangle, ToolTip.ToolTipWindow control)
+               {
+                       Rectangle text_rect = Rectangle.Inflate (control.ClientRectangle, -2, -1);
+                       
                        dc.FillRectangle(SystemBrushes.Info, control.ClientRectangle);
                        dc.DrawRectangle(SystemPens.WindowFrame, 0, 0, control.Width-1, control.Height-1);
-                       dc.DrawString(control.Text, control.Font, ResPool.GetSolidBrush(this.ColorInfoText), control.ClientRectangle, control.string_format);
+                       dc.DrawString(control.Text, control.Font, ResPool.GetSolidBrush(this.ColorInfoText), text_rect, control.string_format);
                }
 
-               public override Size ToolTipSize(ToolTip.ToolTipWindow tt, string text) {
+               public override Size ToolTipSize(ToolTip.ToolTipWindow tt, string text)
+               {
                        SizeF   sizef;
 
-                       sizef = tt.DeviceContext.MeasureString(text, tt.Font);
+                       sizef = tt.DeviceContext.MeasureString(text, tt.Font, SizeF.Empty, tt.string_format);
                        return new Size((int)sizef.Width+8, (int)sizef.Height+3);       // Need space for the border
                }
                #endregion      // ToolTip
 
+               #region BalloonWindow
+#if NET_2_0
+               NotifyIcon.BalloonWindow balloon_window;
+               
+               public override void ShowBalloonWindow (IntPtr handle, int timeout, string title, string text, ToolTipIcon icon)
+               {
+                       Control control = Control.FromHandle(handle);
+                       
+                       if (control == null)
+                               return;
+
+                       if (balloon_window != null) {
+                               balloon_window.Close ();
+                               balloon_window.Dispose ();
+                       }
+
+                       balloon_window = new NotifyIcon.BalloonWindow (handle);
+                       balloon_window.Title = title;
+                       balloon_window.Text = text;
+                       balloon_window.Icon = icon;
+                       balloon_window.Timeout = timeout;
+                       balloon_window.Show ();
+               }
+
+               private const int balloon_iconsize = 16;
+               private const int balloon_bordersize = 8; 
+               
+               public override void DrawBalloonWindow (Graphics dc, Rectangle clip, NotifyIcon.BalloonWindow control) 
+               {
+                       Brush solidbrush = ResPool.GetSolidBrush (this.ColorInfoText);
+                       Rectangle rect = control.ClientRectangle;
+                       int iconsize = (control.Icon == ToolTipIcon.None) ? 0 : balloon_iconsize;
+                       
+                       // Rectangle borders and background.
+                       dc.FillRectangle (ResPool.GetSolidBrush (ColorInfo), rect);
+                       dc.DrawRectangle (ResPool.GetPen (ColorWindowFrame), 0, 0, rect.Width - 1, rect.Height - 1);
+
+                       // Icon
+                       Image image;
+                       switch (control.Icon) {
+                               case ToolTipIcon.Info: {
+                                       image = ThemeEngine.Current.Images(UIIcon.MessageBoxInfo, balloon_iconsize);
+                                       break;
+                               }
+
+                               case ToolTipIcon.Warning: {
+                                       image = ThemeEngine.Current.Images(UIIcon.MessageBoxError, balloon_iconsize);
+                                       break;
+                               }
+
+                               case ToolTipIcon.Error: {
+                                       image = ThemeEngine.Current.Images(UIIcon.MessageBoxWarning, balloon_iconsize);
+                                       break;
+                               }
+                               
+                               default: {
+                                       image = null;
+                                       break;
+                               }
+                       }
+
+                       if (control.Icon != ToolTipIcon.None)
+                               dc.DrawImage (image, new Rectangle (balloon_bordersize, balloon_bordersize, iconsize, iconsize));
+                       
+                       // Title
+                       Rectangle titlerect = new Rectangle (rect.X + balloon_bordersize + iconsize + (iconsize > 0 ? balloon_bordersize : 0), 
+                                                                                               rect.Y + balloon_bordersize, 
+                                                                                               rect.Width - ((3 * balloon_bordersize) + iconsize), 
+                                                                                               rect.Height - (2 * balloon_bordersize));
+                       
+                       Font titlefont = new Font (control.Font.FontFamily, control.Font.Size, control.Font.Style | FontStyle.Bold, control.Font.Unit);
+                       dc.DrawString (control.Title, titlefont, solidbrush, titlerect, control.Format);
+                       
+                       // Text
+                       Rectangle textrect = new Rectangle (rect.X + balloon_bordersize, 
+                                                                                               rect.Y + balloon_bordersize, 
+                                                                                               rect.Width - (2 * balloon_bordersize), 
+                                                                                               rect.Height - (2 * balloon_bordersize));
+
+                       StringFormat textformat = control.Format;
+                       textformat.LineAlignment = StringAlignment.Far;
+                       dc.DrawString (control.Text, control.Font, solidbrush, textrect, textformat);
+               }
+
+               public override Rectangle BalloonWindowRect (NotifyIcon.BalloonWindow control)
+               {
+                       Rectangle deskrect = Screen.GetWorkingArea (control);
+                       SizeF maxsize = new SizeF (250, 200);
+
+                       SizeF titlesize = control.DeviceContext.MeasureString (control.Title, control.Font, maxsize, control.Format);
+                       SizeF textsize = control.DeviceContext.MeasureString (control.Text, control.Font, maxsize, control.Format);
+                       
+                       if (titlesize.Height < balloon_iconsize)
+                               titlesize.Height = balloon_iconsize;
+                       
+                       Rectangle rect = new Rectangle ();
+                       rect.Height = (int) (titlesize.Height + textsize.Height + (3 * balloon_bordersize));
+                       rect.Width = (int) ((titlesize.Width > textsize.Width) ? titlesize.Width : textsize.Width) + (2 * balloon_bordersize);
+                       rect.X = deskrect.Width - rect.Width - 2;
+                       rect.Y = deskrect.Height - rect.Height - 2;
+                       
+                       return rect;
+               }
+#endif
+               #endregion      // BalloonWindow
+
                #region TrackBar
+               public override int TrackBarValueFromMousePosition (int x, int y, TrackBar tb)
+               {
+                       int result = tb.Value;
+                       int value_pos = tb.Value;
+                       float pixels_betweenticks;
+                       Rectangle thumb_pos = Rectangle.Empty, thumb_area = Rectangle.Empty;
+                       Point channel_startpoint = Point.Empty, na_point = Point.Empty;
+                       
+                       GetTrackBarDrawingInfo (tb, out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out na_point, out na_point);
+                       
+                       /* Convert thumb position from mouse position to value*/
+                       if (tb.Orientation == Orientation.Vertical) {
+                               value_pos = (int)((thumb_area.Bottom - y -(float)pixels_betweenticks / 2) / (float)pixels_betweenticks);
+
+                               if (value_pos + tb.Minimum > tb.Maximum)
+                                       value_pos = tb.Maximum - tb.Minimum;
+                               else if (value_pos + tb.Minimum < tb.Minimum)
+                                       value_pos = 0;
+
+                               result = value_pos + tb.Minimum;
+                       } else {
+                               value_pos = (int) ((x - channel_startpoint.X - (float) pixels_betweenticks / 2) / (float) pixels_betweenticks);
+
+                               if (value_pos + tb.Minimum > tb.Maximum)
+                                       value_pos = tb.Maximum - tb.Minimum;
+                               else if (value_pos + tb.Minimum < tb.Minimum)
+                                       value_pos = 0;
+
+                               result = value_pos + tb.Minimum;
+                       }
+                       
+                       return result;
+               }
+               
+               private void GetTrackBarDrawingInfo (TrackBar tb, out float pixels_betweenticks, out Rectangle thumb_area, out Rectangle thumb_pos, out Point channel_startpoint, out Point bottomtick_startpoint, out Point toptick_startpoint)
+               {
+                       thumb_area = Rectangle.Empty;
+                       thumb_pos = Rectangle.Empty;
+                       
+                       if (tb.Orientation == Orientation.Vertical) {
+                               toptick_startpoint = new Point ();
+                               bottomtick_startpoint = new Point ();
+                               channel_startpoint = new Point ();
+                               float pixel_len;
+                               const int space_from_right = 8;
+                               const int space_from_left = 8;
+                               const int space_from_bottom = 11;
+                               Rectangle area = tb.ClientRectangle;
+
+                               switch (tb.TickStyle) {
+                               case TickStyle.BottomRight:
+                               case TickStyle.None:
+                                       channel_startpoint.Y = 8;
+                                       channel_startpoint.X = 9;
+                                       bottomtick_startpoint.Y = 13;
+                                       bottomtick_startpoint.X = 24;
+                                       break;
+                               case TickStyle.TopLeft:
+                                       channel_startpoint.Y = 8;
+                                       channel_startpoint.X = 19;
+                                       toptick_startpoint.Y = 13;
+                                       toptick_startpoint.X = 8;
+                                       break;
+                               case TickStyle.Both:
+                                       channel_startpoint.Y = 8;
+                                       channel_startpoint.X = 18;
+                                       bottomtick_startpoint.Y = 13;
+                                       bottomtick_startpoint.X = 32;
+                                       toptick_startpoint.Y = 13;
+                                       toptick_startpoint.X = 8;
+                                       break;
+                               default:
+                                       break;
+                               }
+
+                               thumb_area.X = area.X + channel_startpoint.X;
+                               thumb_area.Y = area.Y + channel_startpoint.Y;
+                               thumb_area.Height = area.Height - space_from_right - space_from_left;
+                               thumb_area.Width = 4;
+
+                               pixel_len = thumb_area.Height - 11;
+                               if (tb.Maximum == tb.Minimum) {
+                                       pixels_betweenticks = 0;
+                               } else {
+                                       pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
+                               }
+
+                               thumb_pos.Y = thumb_area.Bottom - space_from_bottom - (int)(pixels_betweenticks * (float)(tb.Value - tb.Minimum));
+
+                               /* Draw thumb fixed 10x22 size */
+                               thumb_pos.Width = 10;
+                               thumb_pos.Height = 22;
+                       } else {        
+                               toptick_startpoint = new Point ();
+                               bottomtick_startpoint = new Point ();
+                               channel_startpoint = new Point ();
+                               float pixel_len;
+                               const int space_from_right = 8;
+                               const int space_from_left = 8;
+                               Rectangle area = tb.ClientRectangle;
+                                                       
+                               switch (tb.TickStyle) {
+                               case TickStyle.BottomRight:
+                               case TickStyle.None:
+                                       channel_startpoint.X = 8;
+                                       channel_startpoint.Y = 9;
+                                       bottomtick_startpoint.X = 13;
+                                       bottomtick_startpoint.Y = 24;                           
+                                       break;
+                               case TickStyle.TopLeft:
+                                       channel_startpoint.X = 8;
+                                       channel_startpoint.Y = 19;
+                                       toptick_startpoint.X = 13;
+                                       toptick_startpoint.Y = 8;
+                                       break;
+                               case TickStyle.Both:
+                                       channel_startpoint.X = 8;
+                                       channel_startpoint.Y = 18;      
+                                       bottomtick_startpoint.X = 13;
+                                       bottomtick_startpoint.Y = 32;                           
+                                       toptick_startpoint.X = 13;
+                                       toptick_startpoint.Y = 8;                               
+                                       break;
+                               default:
+                                       break;
+                               }
+                                                       
+                               thumb_area.X = area.X + channel_startpoint.X;
+                               thumb_area.Y = area.Y + channel_startpoint.Y;
+                               thumb_area.Width = area.Width - space_from_right - space_from_left;
+                               thumb_area.Height = 4;
+
+                               pixel_len = thumb_area.Width - 11;
+                               if (tb.Maximum == tb.Minimum) {
+                                       pixels_betweenticks = 0;
+                               } else {
+                                       pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
+                               }
+
+                               thumb_pos.X = channel_startpoint.X + (int)(pixels_betweenticks * (float) (tb.Value - tb.Minimum));
+
+                               /* Draw thumb fixed 10x22 size */
+                               thumb_pos.Width = 10;
+                               thumb_pos.Height = 22;
+                       }
+               }
+               
                private void DrawTrackBar_Vertical (Graphics dc, Rectangle clip_rectangle, TrackBar tb,
                        ref Rectangle thumb_pos, ref Rectangle thumb_area,  Brush br_thumb,
                        float ticks, int value_pos, bool mouse_value) {                 
@@ -4341,41 +4922,9 @@ namespace System.Windows.Forms
                        Point channel_startpoint = new Point ();
                        float pixel_len;
                        float pixels_betweenticks;
-                       const int space_from_right = 8;
-                       const int space_from_left = 8;
-                       const int space_from_bottom = 11;
                        Rectangle area = tb.ClientRectangle;
                        
-                       switch (tb.TickStyle)   {
-                       case TickStyle.BottomRight:
-                       case TickStyle.None:
-                               channel_startpoint.Y = 8;
-                               channel_startpoint.X = 9;
-                               bottomtick_startpoint.Y = 13;
-                               bottomtick_startpoint.X = 24;                           
-                               break;
-                       case TickStyle.TopLeft:
-                               channel_startpoint.Y = 8;
-                               channel_startpoint.X = 19;
-                               toptick_startpoint.Y = 13;
-                               toptick_startpoint.X = 8;
-                               break;
-                       case TickStyle.Both:
-                               channel_startpoint.Y = 8;
-                               channel_startpoint.X = 18;      
-                               bottomtick_startpoint.Y = 13;
-                               bottomtick_startpoint.X = 32;                           
-                               toptick_startpoint.Y = 13;
-                               toptick_startpoint.X = 8;                               
-                               break;
-                       default:
-                               break;
-                       }
-                       
-                       thumb_area.X = area.X + channel_startpoint.X;
-                       thumb_area.Y = area.Y + channel_startpoint.Y;
-                       thumb_area.Height = area.Height - space_from_right - space_from_left;
-                       thumb_area.Width = 4;
+                       GetTrackBarDrawingInfo (tb, out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out bottomtick_startpoint, out toptick_startpoint);
 
                        /* Draw channel */
                        dc.FillRectangle (SystemBrushes.ControlDark, channel_startpoint.X, channel_startpoint.Y,
@@ -4387,43 +4936,12 @@ namespace System.Windows.Forms
                        dc.FillRectangle (SystemBrushes.ControlLight, channel_startpoint.X + 3, channel_startpoint.Y,
                                1, thumb_area.Height);
 
-                       pixel_len = thumb_area.Height - 11;
-                       pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
-                       
-                       /* Convert thumb position from mouse position to value*/
-                       if (mouse_value) {
-                               if (tb.mouse_moved) {
-                                       value_pos += (int) pixels_betweenticks / 2;
-                                       if (value_pos < thumb_area.Bottom) {
-                                               value_pos = (int) ((thumb_area.Bottom - value_pos - (int)(thumb_pos.Width / 2)) / pixels_betweenticks);
-                                       } else {
-                                               value_pos = 0;                  
-                                       }
-       
-                                       if (value_pos + tb.Minimum > tb.Maximum)
-                                               value_pos = tb.Maximum - tb.Minimum;
-                                       else if (value_pos + tb.Minimum < tb.Minimum)
-                                               value_pos = 0;
-
-                                       tb.Value = value_pos + tb.Minimum;      
-                               } else {
-                                       value_pos = tb.Value - tb.Minimum;
-                               }
-                       }                       
-
-                       // thumb_pos.Y = channel_startpoint.Y ; // + (int) (pixels_betweenticks * (float) value_pos);
-                       thumb_pos.Y = thumb_area.Bottom - space_from_bottom - (int) (pixels_betweenticks * (float) value_pos);
-                       
-                       /* Draw thumb fixed 10x22 size */
-                       thumb_pos.Width = 10;
-                       thumb_pos.Height = 22;
-
                        switch (tb.TickStyle)   {
                        case TickStyle.BottomRight:
                        case TickStyle.None: {
                                thumb_pos.X = channel_startpoint.X - 8;
 
-                               Pen pen = SystemPens.ControlLight;
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X , thumb_pos.Y + 10);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 16, thumb_pos.Y);
                                dc.DrawLine (pen, thumb_pos.X + 16, thumb_pos.Y, thumb_pos.X + 16 + 4, thumb_pos.Y + 4);
@@ -4446,7 +4964,7 @@ namespace System.Windows.Forms
                        case TickStyle.TopLeft: {
                                thumb_pos.X = channel_startpoint.X - 10;
 
-                               Pen pen = SystemPens.ControlLight;
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X + 4 + 16, thumb_pos.Y);
                                dc.DrawLine (pen, thumb_pos.X + 4, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 4);
 
@@ -4470,8 +4988,8 @@ namespace System.Windows.Forms
 
                        case TickStyle.Both: {
                                thumb_pos.X = area.X + 10;
-                                       
-                               Pen pen = SystemPens.ControlLight;
+
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 9);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 19, thumb_pos.Y);
 
@@ -4556,40 +5074,9 @@ namespace System.Windows.Forms
                        Point channel_startpoint = new Point ();
                        float pixel_len;
                        float pixels_betweenticks;
-                       const int space_from_right = 8;
-                       const int space_from_left = 8;
                        Rectangle area = tb.ClientRectangle;
-                                               
-                       switch (tb.TickStyle) {
-                       case TickStyle.BottomRight:
-                       case TickStyle.None:
-                               channel_startpoint.X = 8;
-                               channel_startpoint.Y = 9;
-                               bottomtick_startpoint.X = 13;
-                               bottomtick_startpoint.Y = 24;                           
-                               break;
-                       case TickStyle.TopLeft:
-                               channel_startpoint.X = 8;
-                               channel_startpoint.Y = 19;
-                               toptick_startpoint.X = 13;
-                               toptick_startpoint.Y = 8;
-                               break;
-                       case TickStyle.Both:
-                               channel_startpoint.X = 8;
-                               channel_startpoint.Y = 18;      
-                               bottomtick_startpoint.X = 13;
-                               bottomtick_startpoint.Y = 32;                           
-                               toptick_startpoint.X = 13;
-                               toptick_startpoint.Y = 8;                               
-                               break;
-                       default:
-                               break;
-                       }
-                                               
-                       thumb_area.X = area.X + channel_startpoint.X;
-                       thumb_area.Y = area.Y + channel_startpoint.Y;
-                       thumb_area.Width = area.Width - space_from_right - space_from_left;
-                       thumb_area.Height = 4;
+                       
+                       GetTrackBarDrawingInfo (tb , out pixels_betweenticks, out thumb_area, out thumb_pos, out channel_startpoint, out bottomtick_startpoint, out toptick_startpoint);
                        
                        /* Draw channel */
                        dc.FillRectangle (SystemBrushes.ControlDark, channel_startpoint.X, channel_startpoint.Y,
@@ -4601,41 +5088,12 @@ namespace System.Windows.Forms
                        dc.FillRectangle (SystemBrushes.ControlLight, channel_startpoint.X, channel_startpoint.Y +3,
                                thumb_area.Width, 1);
 
-                       pixel_len = thumb_area.Width - 11;
-                       pixels_betweenticks = pixel_len / (tb.Maximum - tb.Minimum);
-
-                       /* Convert thumb position from mouse position to value*/
-                       if (mouse_value) {                      
-                               if (tb.mouse_moved) {
-                                       value_pos += (int) pixels_betweenticks / 2;
-                                       if (value_pos >= channel_startpoint.X) {
-                                               value_pos = (int)(((float) (value_pos - channel_startpoint.X - (int)(thumb_pos.Width / 2))) / pixels_betweenticks);
-                                       } else
-                                               value_pos = 0;                          
-       
-                                       if (value_pos + tb.Minimum > tb.Maximum)
-                                               value_pos = tb.Maximum - tb.Minimum;
-                                       else if(value_pos + tb.Minimum < tb.Minimum)
-                                               value_pos = 0;
-
-                                       tb.Value = value_pos + tb.Minimum;
-                               } else {
-                                       value_pos = tb.Value - tb.Minimum;
-                               }
-                       }                       
-                       
-                       thumb_pos.X = channel_startpoint.X + (int) (pixels_betweenticks * (float) value_pos);
-                       
-                       /* Draw thumb fixed 10x22 size */
-                       thumb_pos.Width = 10;
-                       thumb_pos.Height = 22;
-
                        switch (tb.TickStyle) {
                        case TickStyle.BottomRight:
                        case TickStyle.None: {
                                thumb_pos.Y = channel_startpoint.Y - 8;
 
-                               Pen pen = SystemPens.ControlLight;
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 10, thumb_pos.Y);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 16);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 16, thumb_pos.X + 4, thumb_pos.Y + 16 + 4);
@@ -4657,7 +5115,7 @@ namespace System.Windows.Forms
                        case TickStyle.TopLeft: {
                                thumb_pos.Y = channel_startpoint.Y - 10;
 
-                               Pen pen = SystemPens.ControlLight;
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X, thumb_pos.Y + 4 + 16);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y + 4, thumb_pos.X + 4, thumb_pos.Y);
 
@@ -4681,7 +5139,7 @@ namespace System.Windows.Forms
                        case TickStyle.Both: {
                                thumb_pos.Y = area.Y + 10;
                                        
-                               Pen pen = SystemPens.ControlLight;
+                               Pen pen = SystemPens.ControlLightLight;
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X + 9, thumb_pos.Y);
                                dc.DrawLine (pen, thumb_pos.X, thumb_pos.Y, thumb_pos.X, thumb_pos.Y + 19);
 
@@ -4842,24 +5300,76 @@ namespace System.Windows.Forms
 
                public override void ManagedWindowSetButtonLocations (InternalWindowManager wm)
                {
+                       TitleButtons buttons = wm.TitleButtons;
+                       Form form = wm.form;
+                       
+                       buttons.HelpButton.Visible = form.HelpButton;
+                       
+                       foreach (TitleButton button in buttons) {
+                               button.Visible = false;
+                       }
+                       
+                       switch (form.FormBorderStyle) {
+                       case FormBorderStyle.None:
+                               if (form.WindowState != FormWindowState.Normal)
+                                       goto case FormBorderStyle.Sizable;
+                               break;
+                       case FormBorderStyle.FixedToolWindow:
+                       case FormBorderStyle.SizableToolWindow:
+                               buttons.CloseButton.Visible = true;
+                               if (form.WindowState != FormWindowState.Normal)
+                                       goto case FormBorderStyle.Sizable;
+                               break;
+                       case FormBorderStyle.FixedSingle:
+                       case FormBorderStyle.Fixed3D:
+                       case FormBorderStyle.FixedDialog:
+                       case FormBorderStyle.Sizable:
+                               switch (form.WindowState) {
+                                       case FormWindowState.Normal:
+                                               buttons.MinimizeButton.Visible = true;
+                                               buttons.MaximizeButton.Visible = true;
+                                               buttons.RestoreButton.Visible = false;
+                                               break;
+                                       case FormWindowState.Maximized:
+                                               buttons.MinimizeButton.Visible = true;
+                                               buttons.MaximizeButton.Visible = false;
+                                               buttons.RestoreButton.Visible = true;
+                                               break;
+                                       case FormWindowState.Minimized:
+                                               buttons.MinimizeButton.Visible = false;
+                                               buttons.MaximizeButton.Visible = true;
+                                               buttons.RestoreButton.Visible = true;
+                                               break;
+                               }
+                               buttons.CloseButton.Visible = true;
+                               break;
+                       }
+
                        int bw = ManagedWindowBorderWidth (wm);
                        Size btsize = ManagedWindowButtonSize (wm);
                        int btw = btsize.Width;
                        int bth = btsize.Height;
-
+                       int top = bw + 2;
+                       int left = form.Width - bw - btw - 2;
+                       
                        if ((!wm.IsToolWindow || wm.IsMinimized) && wm.HasBorders) {
-                               if (!wm.IsMaximized) {
-                                       wm.close_button.Rectangle = new Rectangle (wm.Form.Width - bw - btw - 2,
-                                                       bw + 2, btw, bth);
-                                       wm.maximize_button.Rectangle = new Rectangle (wm.close_button.Rectangle.Left - 2 - btw,
-                                                       bw + 2, btw, bth);
-                                       wm.minimize_button.Rectangle = new Rectangle (wm.maximize_button.Rectangle.Left - btw,
-                                                       bw + 2, btw, bth);
-                               } else {
-                                       // Maximized
+                               buttons.CloseButton.Rectangle = new Rectangle (left, top, btw, bth);
+                               left -= 2 + btw;
+                               
+                               if (buttons.MaximizeButton.Visible) {
+                                       buttons.MaximizeButton.Rectangle = new Rectangle (left, top, btw, bth);
+                                       left -= 2 + btw;
+                               } 
+                               if (buttons.RestoreButton.Visible) {
+                                       buttons.RestoreButton.Rectangle = new Rectangle (left, top, btw, bth);
+                                       left -= 2 + btw;
                                }
+
+                               buttons.MinimizeButton.Rectangle = new Rectangle (left, top, btw, bth);
+                               left -= 2 + btw;
                        } else if (wm.IsToolWindow) {
-                               wm.close_button.Rectangle = new Rectangle (wm.Form.Width - bw - 2 - btw, bw + 2, btw, bth);
+                               buttons.CloseButton.Rectangle = new Rectangle (left, top, btw, bth);
+                               left -= 2 + btw;
                        }
                }
 
@@ -4874,7 +5384,8 @@ namespace System.Windows.Forms
                        Color color2 = Color.FromArgb (255, 192, 192, 192);
 
 #if debug
-                               dc.FillRectangle (Brushes.Black, clip);
+                       Console.WriteLine (DateTime.Now.ToLongTimeString () + " DrawManagedWindowDecorations");
+                       dc.FillRectangle (Brushes.Black, clip);
 #endif
                        
                        if (wm.HasBorders) {
@@ -4889,7 +5400,14 @@ namespace System.Windows.Forms
                                }                               
                        }
 
-                       if (wm.IsActive () && !wm.IsMaximized) {
+
+                       bool draw_titlebar_enabled = false;
+                       if (wm.Form.Parent != null && wm.Form.Parent is Form) {
+                               draw_titlebar_enabled = false;
+                       } else if (wm.IsActive && !wm.IsMaximized) {
+                               draw_titlebar_enabled = true;
+                       }
+                       if (draw_titlebar_enabled) {
                                color = titlebar_color;
                                color2 = titlebar_color2;
                        }
@@ -4897,8 +5415,6 @@ namespace System.Windows.Forms
                        Rectangle tb = new Rectangle (bdwidth, bdwidth, form.Width - (bdwidth * 2), tbheight - 1);
 
                        // HACK: For now always draw the titlebar until we get updates better
-                       // Rectangle vis = Rectangle.Intersect (tb, pe.ClipRectangle);  
-                       //if (vis != Rectangle.Empty)
                        if (tb.Width > 0 && tb.Height > 0) {
                                using (System.Drawing.Drawing2D.LinearGradientBrush gradient = new LinearGradientBrush (tb, color, color2, LinearGradientMode.Horizontal))
                                {
@@ -4916,7 +5432,7 @@ namespace System.Windows.Forms
                                tb.Width = (form.Width - 62) - tb.X;
                        }
 
-                       if (form.Text != null) {
+                       if (form.Text != null && form.Text != string.Empty) {
                                StringFormat format = new StringFormat ();
                                format.FormatFlags = StringFormatFlags.NoWrap;
                                format.Trimming = StringTrimming.EllipsisCharacter;
@@ -4929,21 +5445,21 @@ namespace System.Windows.Forms
                        }
 
                        if (wm.HasBorders) {
-                               if (!wm.IsToolWindow && form.Icon != null) {
+                               bool draw_icon = false;
+#if NET_2_0
+                               draw_icon = !wm.IsToolWindow && form.Icon != null && form.ShowIcon;
+#else
+                               draw_icon = !wm.IsToolWindow && form.Icon != null;
+#endif
+                               if (draw_icon) {
                                        Rectangle icon = new Rectangle (bdwidth + 3,
                                                        bdwidth + 2, wm.IconWidth, wm.IconWidth);
                                        if (icon.IntersectsWith (clip))
                                                dc.DrawIcon (form.Icon, icon);
                                }
-
-                               if (!wm.IsMaximized) {
-                                       if (!wm.IsToolWindow || wm.IsMinimized) {
-                                               DrawTitleButton (dc, wm.minimize_button, clip);
-                                               DrawTitleButton (dc, wm.maximize_button, clip);
-                                       }
-                                       DrawTitleButton (dc, wm.close_button, clip);
-                               } else {
-                                       //      DrawMaximizedButtons (pe, form.ActiveMenu);
+                               
+                               foreach (TitleButton button in wm.TitleButtons.AllButtons) {
+                                       DrawTitleButton (dc, button, clip);
                                }
                        }
                }
@@ -4964,8 +5480,12 @@ namespace System.Windows.Forms
                                        height - 5);
                }
 
-               private void DrawTitleButton (Graphics dc, InternalWindowManager.TitleButton button, Rectangle clip)
+               private void DrawTitleButton (Graphics dc, TitleButton button, Rectangle clip)
                {
+                       if (!button.Visible) {
+                               return;
+                       }
+                       
                        if (!button.Rectangle.IntersectsWith (clip))
                                return;
 
@@ -4990,7 +5510,7 @@ namespace System.Windows.Forms
                        CPDrawBorder3D(graphics, rectangle, style, sides, ColorControl);
                }
 
-               protected virtual void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides, Color control_color)
+               public override void CPDrawBorder3D (Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides, Color control_color)
                {
                        Pen             penTopLeft;
                        Pen             penTopLeftInner;
@@ -5206,7 +5726,7 @@ namespace System.Windows.Forms
 
                public override void CPDrawCheckBox (Graphics dc, Rectangle rectangle, ButtonState state)
                {
-                       Pen check_pen = SystemPens.ControlDarkDark;
+                       Pen check_pen = Pens.Black;
                        
                        Rectangle cb_rect = new Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
                        
@@ -5924,6 +6444,17 @@ namespace System.Windows.Forms
                        }
                }
 
+               private void DrawStringDisabled20 (Graphics g, string s, Font font, Rectangle layoutRectangle, Color color, TextFormatFlags flags, bool useDrawString)
+               {
+                       CPColor cpcolor = ResPool.GetCPColor (color);
+
+                       layoutRectangle.Offset (1, 1);
+                       TextRenderer.DrawTextInternal (g, s, font, layoutRectangle, cpcolor.LightLight, flags, useDrawString);
+
+                       layoutRectangle.Offset (-1, -1);
+                       TextRenderer.DrawTextInternal (g, s, font, layoutRectangle, cpcolor.Dark, flags, useDrawString);
+               }
+
                public  override void CPDrawStringDisabled (Graphics dc, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format)
                {
                        CPColor cpcolor = ResPool.GetCPColor (color);