2006-05-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PropertyGridView.cs
index 0ee17b6993ab39f220253d216ae96a9b11732399..21ef041b0e6a56cfc1a96157855399b3e37109e9 100644 (file)
-// Permission is hereby granted, free of charge, to any person obtaining\r
-// a copy of this software and associated documentation files (the\r
-// "Software"), to deal in the Software without restriction, including\r
-// without limitation the rights to use, copy, modify, merge, publish,\r
-// distribute, sublicense, and/or sell copies of the Software, and to\r
-// permit persons to whom the Software is furnished to do so, subject to\r
-// the following conditions:\r
-// \r
-// The above copyright notice and this permission notice shall be\r
-// included in all copies or substantial portions of the Software.\r
-// \r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)\r
-//\r
-// Authors:\r
-//      Jonathan Chambers      (jonathan.chambers@ansys.com)\r
-//\r
-//\r
-\r
-// NOT COMPLETE\r
-\r
-using System;\r
-using System.Collections;\r
-using System.Drawing;\r
-using System.Drawing.Design;\r
-using System.ComponentModel;\r
-using System.Windows.Forms.Design;\r
-\r
-namespace System.Windows.Forms.PropertyGridInternal \r
-{\r
-       internal class PropertyGridView : System.Windows.Forms.ScrollableControl, IWindowsFormsEditorService\r
-       {\r
-\r
-               #region Private Members\r
-               private const int V_INDENT = 16;\r
-               private const int ROW_HEIGHT = 16;\r
-               private const int RESIZE_WIDTH = 3;\r
-               private const int BUTTON_WIDTH = 25;\r
-               private PropertyGridTextBox grid_textbox;\r
-               private PropertyGrid property_grid;\r
-               private bool resizing_grid;\r
-               private int splitter_location;
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+//
+// Authors:
+//      Jonathan Chambers      (jonathan.chambers@ansys.com)
+//
+//
+
+// NOT COMPLETE
+
+using System;
+using System.Collections;
+using System.Drawing;
+using System.Drawing.Design;
+using System.ComponentModel;
+using System.Threading;
+using System.Windows.Forms.Design;
+
+namespace System.Windows.Forms.PropertyGridInternal {
+       internal class PropertyGridView : System.Windows.Forms.ScrollableControl, IWindowsFormsEditorService {
+
+               #region Private Members
+               private double splitter_percent = .5;
+               private const int V_INDENT = 16;
+               private int row_height;
+               private int font_height_padding = 3;
+               private const int RESIZE_WIDTH = 3;
+               private const int BUTTON_WIDTH = 25;
+               private PropertyGridTextBox grid_textbox;
+               private PropertyGrid property_grid;
+               private bool resizing_grid;
                private int open_grid_item_count = -1;
-               private int skipped_grid_items;\r
-               private PropertyGridDropDown dropdown_form;\r
-               private bool dropdown_form_showing;\r
-               private Form dialog_form;\r
-               private VScrollBar vbar;\r
-               #endregion\r
-\r
-               #region Contructors\r
-               public PropertyGridView (PropertyGrid propertyGrid) \r
-               {\r
-                       property_grid = propertyGrid;\r
-\r
-                       property_grid.SelectedGridItemChanged+=new SelectedGridItemChangedEventHandler(HandleSelectedGridItemChanged);\r
-                       property_grid.PropertyValueChanged+=new PropertyValueChangedEventHandler(HandlePropertyValueChanged);\r
-\r
-                       this.BackColor = Color.Beige;\r
-                       grid_textbox = new PropertyGridTextBox();\r
-                       grid_textbox.DropDownButtonClicked +=new EventHandler(DropDownButtonClicked);\r
-                       grid_textbox.DialogButtonClicked +=new EventHandler(DialogButtonClicked);\r
-\r
-                       \r
-                       dropdown_form = new PropertyGridDropDown();\r
+               private int skipped_grid_items;
+               private PropertyGridDropDown dropdown_form;
+               private bool dropdown_form_showing;
+               private Form dialog_form;
+               private ImplicitVScrollBar vbar;
+               private StringFormat string_format;
+               private Font bold_font;
+               #endregion
+
+               #region Contructors
+               public PropertyGridView (PropertyGrid propertyGrid) {
+                       property_grid = propertyGrid;
+
+                       property_grid.SelectedGridItemChanged+=new SelectedGridItemChangedEventHandler(HandleSelectedGridItemChanged);
+                       property_grid.PropertyValueChanged+=new PropertyValueChangedEventHandler(HandlePropertyValueChanged);
+
+                       string_format = new StringFormat();
+                       string_format.FormatFlags = StringFormatFlags.NoWrap;
+                       string_format.Trimming = StringTrimming.None;
+
+                       grid_textbox = new PropertyGridTextBox();
+                       grid_textbox.DropDownButtonClicked +=new EventHandler(DropDownButtonClicked);
+                       grid_textbox.DialogButtonClicked +=new EventHandler(DialogButtonClicked);
+
+                       
+                       dropdown_form = new PropertyGridDropDown();
                        dropdown_form.FormBorderStyle = FormBorderStyle.None;
                        dropdown_form.ShowInTaskbar = false;
-                       \r
-                       dialog_form = new Form();\r
-                       //dialog_form.FormBorderStyle = FormBorderStyle.None;\r
-\r
-                       \r
-\r
-                       grid_textbox.Visible = true;\r
-                       grid_textbox.Font = this.Font;//new Font(this.Font,FontStyle.Bold);\r
-                       grid_textbox.BackColor = this.BackColor;\r
-                       // Not working at all, used to??\r
-                       grid_textbox.Validating += new CancelEventHandler(TextBoxValidating);\r
-\r
-                       vbar = new VScrollBar();\r
-                       vbar.Visible = false;\r
-                       vbar.Scroll+=new ScrollEventHandler(HandleScroll);\r
-                       vbar.Dock = DockStyle.Right;\r
-                       this.Controls.Add(vbar);\r
-\r
-                       splitter_location = 65;\r
-                       resizing_grid = false;\r
-\r
-                       ForeColorChanged+=new EventHandler(RedrawEvent);\r
-                       BackColorChanged+=new System.EventHandler(RedrawEvent);\r
-                       FontChanged+=new EventHandler(RedrawEvent);\r
-                       SizeChanged+=new EventHandler(RedrawEvent);\r
-                       \r
-                       SetStyle(ControlStyles.DoubleBuffer, true);\r
-                       SetStyle(ControlStyles.UserPaint, true);\r
-                       SetStyle(ControlStyles.AllPaintingInWmPaint, true);\r
-                       SetStyle(ControlStyles.ResizeRedraw, false);\r
-                       SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);\r
-               }\r
-\r
-               #endregion\r
-\r
-               #region Protected Instance Methods\r
-               \r
-               protected override void OnPaint(PaintEventArgs e)\r
-               {\r
-                       if (property_grid.SelectedGridItem != null && property_grid.SelectedGridItem.GridItemType == GridItemType.Property) \r
-                       {\r
-                               this.Controls.Add(grid_textbox);\r
-                               if (!grid_textbox.Focused)\r
-                                       grid_textbox.Focus();\r
-                       }\r
-                       else \r
-                       {\r
-                               this.Controls.Remove(grid_textbox);\r
-                       }\r
-\r
+                       
+                       dialog_form = new Form();
+                       dialog_form.FormBorderStyle = FormBorderStyle.None;
+                       dialog_form.ShowInTaskbar = false;
+
+                       skipped_grid_items = 0;
+                       row_height = Font.Height + font_height_padding;
+
+                       grid_textbox.Visible = false;
+                       grid_textbox.Font = this.Font;
+                       grid_textbox.BackColor = this.BackColor;
+                       // Not working at all, used to??
+                       grid_textbox.Validating += new CancelEventHandler(TextBoxValidating);
+                       grid_textbox.ToggleValue+=new EventHandler(grid_textbox_ToggleValue);
+                       this.Controls.Add(grid_textbox);
+
+                       vbar = new ImplicitVScrollBar();
+                       vbar.Visible = false;
+                       vbar.ValueChanged+=new EventHandler(HandleValueChanged);
+                       vbar.Dock = DockStyle.Right;
+                       this.Controls.AddImplicit(vbar);
+
+                       resizing_grid = false;
+
+                       bold_font = new Font(this.Font, FontStyle.Bold);
+
+                       ForeColorChanged+=new EventHandler(RedrawEvent);
+                       BackColorChanged+=new System.EventHandler(RedrawEvent);
+                       FontChanged+=new EventHandler(RedrawEvent);
+                       SizeChanged+=new EventHandler(RedrawEvent);
+                       
+                       SetStyle(ControlStyles.DoubleBuffer, true);
+                       SetStyle(ControlStyles.UserPaint, true);
+                       SetStyle(ControlStyles.AllPaintingInWmPaint, true);
+                       SetStyle(ControlStyles.ResizeRedraw, false);
+               }
+
+               #endregion
+
+               #region Protected Instance Methods
+
+               protected override void OnFontChanged(EventArgs e) {
+                       bold_font = new Font(this.Font, FontStyle.Bold);
+
+                       
+                       row_height = Font.Height + font_height_padding;
+                       base.OnFontChanged (e);
+               }
+
+               protected override void OnDoubleClick(EventArgs e) {
+                       if (property_grid.SelectedGridItem.Expandable)
+                               property_grid.SelectedGridItem.Expanded = !property_grid.SelectedGridItem.Expanded;
+                       else 
+                               ToggleValue();
+                       Invalidate();
+                       base.OnDoubleClick (e);
+               }
+
+               protected override void OnPaint(PaintEventArgs e) {
                        // Decide if we need a scrollbar
-                       open_grid_item_count = 0;\r
-\r
-                       // draw grid outline
-                       //DrawBackground(e);\r
+                       open_grid_item_count = 0;
+
+                       // Background
+                       e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
+                       
+                       // Left column
+                       e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), 0,0,V_INDENT, ClientRectangle.Height);
+                       
+
+                       int yLoc = -vbar.Value*row_height;
+                       DrawGridItems(property_grid.grid_items, e, 1, ref yLoc);
+
+                       // Grid
+                       DrawGrid(e);
+
+                       UpdateScrollBar();
+                       
+                       base.OnPaint(e);
+               }
+
+               protected override void OnMouseWheel(MouseEventArgs e) {
+                       if (vbar == null || !vbar.Visible) {
+                               return;
+                       }
+
+                       if (e.Delta < 0) {
+                               vbar.Value = Math.Min(vbar.Value + SystemInformation.MouseWheelScrollLines, vbar.Maximum);
+                       } else {
+                               vbar.Value = Math.Max(0, vbar.Value - SystemInformation.MouseWheelScrollLines);
+                       }
+                       base.OnMouseWheel (e);
+               }
+
+
+               protected override void OnMouseMove (MouseEventArgs e) {
+
+                       if (resizing_grid) {
+                               int loc = Math.Max(e.X,2*V_INDENT);
+                               SplitterPercent = 1.0*loc/Width;
+                               Refresh();
+                       }
+                       if (e.X > SplitterLocation - RESIZE_WIDTH && e.X < SplitterLocation + RESIZE_WIDTH) 
+                               this.Cursor = Cursors.SizeWE;
+                       else
+                               this.Cursor = Cursors.Default;
+                       base.OnMouseMove (e);
+               }
+
+               protected override void OnMouseDown (MouseEventArgs e) {
+                       if (e.X > SplitterLocation - RESIZE_WIDTH && e.X < SplitterLocation + RESIZE_WIDTH) {
+                               resizing_grid = true;
+                       }
+                       else {
+                               int offset = -vbar.Value*row_height;
+                               GridItem foundItem = GetSelectedGridItem(property_grid.grid_items, e.Y, ref offset);
+                               
+                               if (foundItem != null) {
+                                       if (foundItem.Expandable) {
+                                               if (e.X >=3 && e.X <= 11 && (e.Y % row_height >= row_height/2-2 && e.Y % row_height <= row_height/2+4)) {
+                                                       foundItem.Expanded = !foundItem.Expanded;
+                                                       Invalidate();
+                                               }
+                                       }
+                                       this.property_grid.SelectedGridItem = foundItem;
+                               }
+                               
+                               base.OnMouseDown (e);
+                       }
+               }
+
+               protected override void OnMouseUp (MouseEventArgs e) {
+                       resizing_grid = false;
+                       base.OnMouseUp (e);
+               }
+
+               protected override void OnKeyPress(KeyPressEventArgs e) {
+                       if (property_grid.SelectedGridItem.Expandable)
+                               property_grid.SelectedGridItem.Expanded = !property_grid.SelectedGridItem.Expanded;
+                       base.OnKeyPress (e);
+               }
+
+               #endregion
+
+               #region Private Helper Methods
+
+               private int SplitterLocation{
+                       get {
+                               return (int)(splitter_percent*Width);
+                       }
+               }
+
+               private double SplitterPercent{
+                       set {
+                               splitter_percent = Math.Max(Math.Min(value, .9),.1);
+                       }
+               }
+
+               private GridItem GetSelectedGridItem (GridItemCollection grid_items, int y, ref int current) {
+                       foreach (GridItem child_grid_item in grid_items) {
+                               if (y > current && y < current + row_height) {
+                                       return child_grid_item;
+                               }
+                               current += row_height;
+                               if (child_grid_item.Expanded) {
+                                       GridItem foundItem = GetSelectedGridItem(child_grid_item.GridItems, y, ref current);
+                                       if (foundItem != null)
+                                               return foundItem;
+                               }
+                       }
+                       return null;
+               }
+
+               private void UpdateScrollBar() {
+                       int visible_rows = this.ClientRectangle.Height/row_height;
+                       if (open_grid_item_count > visible_rows) {
+                               vbar.Visible = true;
+                               vbar.SmallChange = 1;
+                               vbar.Minimum = 0;
+                               vbar.Maximum = open_grid_item_count-1;
+                               vbar.LargeChange = visible_rows;
+                       }
+                       else {
+                               vbar.Visible = false;
+                       }
+
+               }
+
+               #region Drawing Code
+
+               private void DrawGrid(PaintEventArgs pevent) {
+                       Pen pen = ThemeEngine.Current.ResPool.GetPen(property_grid.LineColor);
+                       // vertical divider line
+                       pevent.Graphics.DrawLine(pen, SplitterLocation, 0, SplitterLocation, (open_grid_item_count-skipped_grid_items)*row_height);
                        
-                       e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);\r
-\r
-                       // draw grid items
-                       // can we use the transform
-                       //pevent.Graphics.TranslateTransform(0, -vbar.Value*ROW_HEIGHT);
-                       int yLoc = -vbar.Value*ROW_HEIGHT;
-                       DrawGridItems(property_grid.grid_items, e, 1, ref yLoc);\r
-\r
-                       DrawGrid(e, yLoc);\r
-                       e.Graphics.DrawRectangle(SystemPens.ControlDark, 0,0,Width-1,Height-1 );\r
-                       \r
-                       \r
-
-\r
-                       UpdateScrollBar();\r
-                       \r
-                       base.OnPaint(e);\r
-               }\r
-\r
-               protected override void OnMouseMove (MouseEventArgs e) \r
-               {\r
-                       if (resizing_grid) \r
-                       {\r
-                               splitter_location = Math.Max(e.X,V_INDENT);\r
-                               Refresh();\r
-                       }\r
-                       base.OnMouseMove (e);\r
-               }\r
-\r
-               private GridItem GetSelectedGridItem (GridItemCollection grid_items, int y) \r
-               {\r
-                       foreach (GridItem child_grid_item in grid_items) \r
-                       {\r
-                               if (y > child_grid_item.Top && y < child_grid_item.Top + ROW_HEIGHT) \r
-                               {\r
-                                       return child_grid_item;\r
-                               }\r
-                               GridItem foundItem = GetSelectedGridItem(child_grid_item.GridItems, y);\r
-                               if (foundItem != null)\r
-                                       return foundItem;\r
-                       }\r
-                       return null;\r
-               }\r
-\r
-               protected override void OnMouseDown (MouseEventArgs e) \r
-               {\r
-                       if (e.X > splitter_location - RESIZE_WIDTH && e.X < splitter_location + RESIZE_WIDTH) \r
-                       {\r
-                               resizing_grid = true;\r
-                       }\r
-                       else \r
-                       {\r
-                               GridItem foundItem = GetSelectedGridItem(property_grid.grid_items, e.Y);\r
-                               \r
-                               if (foundItem != null)\r
-                               {\r
-                                       if (foundItem.Expandable) \r
-                                       {\r
-                                               if (foundItem.PlusMinusBounds.Contains(e.X,e.Y))\r
-                                               {\r
-                                                       foundItem.Expanded = !foundItem.Expanded;\r
-                                               }\r
-                                       }\r
-                                       this.property_grid.SelectedGridItem = foundItem;\r
-                               }\r
-                               \r
-                               base.OnMouseDown (e);\r
-                       }\r
-               }\r
-\r
-               protected override void OnMouseUp (MouseEventArgs e) \r
-               {\r
-                       resizing_grid = false;\r
-                       base.OnMouseUp (e);\r
-               }\r
-\r
-               protected override void OnKeyDown (KeyEventArgs e) \r
-               {\r
-                       base.OnKeyDown (e);\r
-               }\r
-\r
-               #endregion\r
-\r
-               #region Private Helper Methods\r
-\r
-               private void UpdateScrollBar()\r
-               {\r
-                       int visible_rows = this.ClientRectangle.Height/ROW_HEIGHT;\r
-                       if (open_grid_item_count > visible_rows)\r
-                       {\r
-                               vbar.Visible = true;\r
-                               vbar.SmallChange = 1;\r
-                               vbar.Minimum = 0;\r
-                               vbar.Maximum = open_grid_item_count-1;\r
-                               vbar.LargeChange = visible_rows;\r
-                       }\r
-                       else\r
-                       {\r
-                               vbar.Visible = false;\r
-                       }\r
-\r
-               }\r
-\r
-               private GridItem GetGridItemAt (int y) \r
-               {\r
-                       return null;\r
-               }\r
-\r
-               #region Drawing Code\r
-\r
-               private void DrawGrid(PaintEventArgs pevent, int yLoc)\r
-               {\r
-                       Pen pen = ThemeEngine.Current.ResPool.GetPen(property_grid.LineColor);\r
-                       // vertical divider line\r
-                       pevent.Graphics.DrawLine(pen, splitter_location, 0, splitter_location, yLoc);\r
-                       \r
-                       while (yLoc >= 0)\r
-                       {\r
-                               // horizontal lines\r
-                               pevent.Graphics.DrawLine(pen, 0, yLoc, ClientRectangle.Width, yLoc);\r
-                               yLoc -= ROW_HEIGHT;\r
-                       }\r
-               }\r
-\r
-               private void DrawGridItems(GridItemCollection grid_items, PaintEventArgs pevent, int depth, ref int yLoc)\r
-               {\r
-                       foreach (GridItem grid_item in grid_items) \r
-                       {
+                       int y = 0;
+                       while (y < ClientRectangle.Height && y/row_height + skipped_grid_items < open_grid_item_count) {
+                               // horizontal lines
+                               pevent.Graphics.DrawLine(pen, 0, y, ClientRectangle.Width, y);
+                               y += row_height;
+                       }
+               }
+
+               private void DrawGridItems(GridItemCollection grid_items, PaintEventArgs pevent, int depth, ref int yLoc) {
+                       foreach (GridItem grid_item in grid_items) {
                                DrawGridItem (grid_item, pevent, depth, ref yLoc);
                                if (grid_item.Expanded)
-                                       DrawGridItems(grid_item.GridItems, pevent, depth, ref yLoc);
-                       }\r
-               }\r
-\r
-               private void DrawGridItemLabel(GridItem grid_item, PaintEventArgs pevent, Rectangle rect)\r
-               {\r
-                       int x = rect.X+1;\r
-                       if (grid_item.Parent != null && grid_item.Parent.GridItemType != GridItemType.Category)\r
-                               x += V_INDENT;\r
-\r
-                       Font font = this.Font;\r
-                       Brush brush = SystemBrushes.WindowText;\r
-                       if (grid_item.GridItemType == GridItemType.Category)\r
-                       {\r
-                               font = new Font(font, FontStyle.Bold);\r
-                               brush = SystemBrushes.ControlDark;\r
-                       }\r
-\r
-                       if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category)\r
-                       {\r
-                               pevent.Graphics.FillRectangle (SystemBrushes.Highlight, rect);\r
-                               // Label\r
-                               brush = SystemBrushes.HighlightText;\r
-                       }\r
-\r
-                       \r
-                       pevent.Graphics.DrawString(grid_item.Label,font,brush,new Rectangle(x, rect.Y + 2,x-rect.X+rect.Width,rect.Height-2));\r
-               }\r
-\r
-               private void DrawGridItemValue(GridItem grid_item, PaintEventArgs pevent, Rectangle rect)\r
-               {\r
-                       // Value\r
-                       if (grid_item.PropertyDescriptor != null)\r
-                       {\r
-\r
-                               bool paintsValue = false;\r
-                               UITypeEditor editor = null;\r
-                               object temp = grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));\r
-                               editor = (UITypeEditor)temp;//grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));\r
-                               if (editor != null) \r
-                               {\r
-                                       paintsValue = editor.GetPaintValueSupported();\r
-                               }\r
-\r
-                               if (grid_item == property_grid.SelectedGridItem)\r
-                               {\r
-                                       grid_textbox.DropDownButtonVisible = false;\r
-                                       grid_textbox.DialogButtonVisible = false;\r
-                                       if (editor != null) \r
-                                       {\r
-                                               UITypeEditorEditStyle style = editor.GetEditStyle();\r
-                                       \r
-                                               switch (style)\r
-                                               {\r
-                                                       case UITypeEditorEditStyle.DropDown:\r
-                                                               grid_textbox.DropDownButtonVisible = true;\r
-                                                               break;\r
-                                                       case UITypeEditorEditStyle.Modal:\r
-                                                               grid_textbox.DialogButtonVisible = true;\r
-                                                               break;\r
-                                               }\r
-                                       }\r
-                                       else \r
-                                       {
-                                               try
-                                               {\r
-                                                       if (grid_item.PropertyDescriptor.Converter != null)\r
-                                                       {\r
-                                                               if (grid_item.PropertyDescriptor.Converter.GetStandardValuesSupported()) \r
-                                                               {\r
-                                                       \r
-                                                                       grid_textbox.DropDownButtonVisible = true;\r
+                                       DrawGridItems(grid_item.GridItems, pevent, (grid_item.GridItemType == GridItemType.Category) ? depth : depth+1, ref yLoc);
+                       }
+               }
+
+               private void DrawGridItemLabel(GridItem grid_item, PaintEventArgs pevent, Rectangle rect) {
+                       int x = rect.X+1;
+                       if (grid_item.Parent != null && grid_item.Parent.GridItemType != GridItemType.Category)
+                               x += V_INDENT;
+
+                       Font font = this.Font;
+                       Brush brush = SystemBrushes.WindowText;
+                       if (grid_item.GridItemType == GridItemType.Category) {
+                               font = bold_font;
+                               brush = SystemBrushes.ControlDark;
+                       }
+
+                       if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category) {
+                               pevent.Graphics.FillRectangle (SystemBrushes.Highlight, rect);
+                               // Label
+                               brush = SystemBrushes.HighlightText;
+                       }
+
+                       
+                       if (grid_item.GridItemType == GridItemType.Category) {
+                               pevent.Graphics.DrawString(grid_item.Label,font,brush,x, rect.Y + 2);
+                               if (grid_item == property_grid.SelectedGridItem) {
+                                       SizeF size = pevent.Graphics.MeasureString(grid_item.Label, bold_font);
+                                       ControlPaint.DrawFocusRectangle(pevent.Graphics, new Rectangle(x,rect.Y+2, (int)size.Width, (int)size.Height));
+                               }
+                       }
+                       else
+                               pevent.Graphics.DrawString(grid_item.Label,font,brush,new Rectangle(x, rect.Y + 2,x-rect.X+rect.Width-2,rect.Height-2),string_format);
+               }
+
+               private void DrawGridItemValue(GridItem grid_item, PaintEventArgs pevent, Rectangle rect) {
+                       // Value
+                       if (grid_item.PropertyDescriptor != null) {
+
+                               bool paintsValue = false;
+                               UITypeEditor editor = null;
+                               object temp = grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
+                               editor = (UITypeEditor)temp;//grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
+                               if (editor != null) {
+                                       paintsValue = editor.GetPaintValueSupported();
+                               }
+
+                               if (grid_item == property_grid.SelectedGridItem) {
+                                       grid_textbox.ReadOnly = false;
+                                       grid_textbox.DropDownButtonVisible = false;
+                                       grid_textbox.DialogButtonVisible = false;
+                                       if (editor != null) {
+                                               UITypeEditorEditStyle style = editor.GetEditStyle();
+                                       
+                                               switch (style) {
+                                                       case UITypeEditorEditStyle.DropDown:
+                                                               grid_textbox.DropDownButtonVisible = true;
+                                                               break;
+                                                       case UITypeEditorEditStyle.Modal:
+                                                               grid_textbox.DialogButtonVisible = true;
+                                                               break;
+                                               }
+                                       }
+                                       else {
+                                               try {
+                                                       if (grid_item.PropertyDescriptor.Converter != null) {
+                                                               if (grid_item.PropertyDescriptor.Converter.GetStandardValuesSupported()) {
+                                                       
+                                                                       grid_textbox.DropDownButtonVisible = true;
                                                                        grid_textbox.ReadOnly = true;
-                                                               }\r
-                                                       }\r
-                                                       else\r
-                                                       {\r
-                                                               System.Console.WriteLine("Converter not available for type {0}",grid_item.PropertyDescriptor.PropertyType);\r
-                                                       }\r
-                                               \r
+                                                               }
+                                                       }
+                                                       else {
+                                                               System.Console.WriteLine("Converter not available for type {0}",grid_item.PropertyDescriptor.PropertyType);
+                                                       }
+                                               
                                                }
-                                               catch (Exception ex)
-                                               {
+                                               catch (Exception) {
                                                }
-                                       }\r
-                               }\r
-\r
-                               \r
-\r
-                               int xLoc = splitter_location+1;\r
-                               if (paintsValue)\r
-                               {\r
-                                       pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(Color.Black), splitter_location+2,rect.Y+2, 20, ROW_HEIGHT-4);\r
-                                       try\r
-                                       {\r
-                                               editor.PaintValue(grid_item.Value, pevent.Graphics, new Rectangle(splitter_location+3,rect.Y+3, 19, ROW_HEIGHT-5));\r
-                                       }\r
-                                       catch (Exception ex)\r
-                                       {\r
-                                               System.Console.WriteLine(ex.Message);\r
-                                               System.Console.WriteLine("Paint Value failed for type {0}",grid_item.PropertyDescriptor.PropertyType);\r
-                                               // design time stuff is not playing nice\r
-                                       }\r
-                                       xLoc += 27;\r
-                               }\r
-\r
-                               Font font = this.Font;\r
-                               try \r
-                               {
-                                       if (grid_item.PropertyDescriptor.Converter != null)
-                                       {
-                                               string value = grid_item.PropertyDescriptor.Converter.ConvertToString(grid_item.Value);\r
-                                               if (grid_item.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))\r
-                                                       font = new Font(font, FontStyle.Bold);\r
+                                       }
+                               }
+
+                               
+
+                               int xLoc = SplitterLocation+1;
+                               if (paintsValue) {
+                                       pevent.Graphics.DrawRectangle(Pens.Black, SplitterLocation+2,rect.Y+2, 20, row_height-4);
+                                       try {
+                                               editor.PaintValue(grid_item.Value, pevent.Graphics, new Rectangle(SplitterLocation+3,rect.Y+3, 19, row_height-5));
+                                       }
+                                       catch (Exception ex) {
+                                               System.Console.WriteLine(ex.Message);
+                                               System.Console.WriteLine("Paint Value failed for type {0}",grid_item.PropertyDescriptor.PropertyType);
+                                               // design time stuff is not playing nice
+                                       }
+                                       xLoc += 27;
+                               }
+
+                               Font font = this.Font;
+                               try {
+                                       if (grid_item.PropertyDescriptor.Converter != null) {
+                                               string value = grid_item.PropertyDescriptor.Converter.ConvertToString(grid_item.Value);
+                                               if (grid_item.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
+                                                       font = bold_font;
                                
-                                               pevent.Graphics.DrawString(value,font,SystemBrushes.WindowText,new RectangleF(xLoc,rect.Y+2, ClientRectangle.Width-(xLoc), ROW_HEIGHT));\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               System.Console.WriteLine("No converter for type {0}",grid_item.PropertyDescriptor.PropertyType);\r
-                                       }\r
-\r
+                                               pevent.Graphics.DrawString(value,font,SystemBrushes.WindowText,new RectangleF(xLoc,rect.Y+2, ClientRectangle.Width-(xLoc), row_height),string_format);
+                                       }
+                                       else {
+                                               System.Console.WriteLine("No converter for type {0}",grid_item.PropertyDescriptor.PropertyType);
+                                       }
+
+                               }
+                               catch (Exception) {
                                }
-                               catch (Exception e)
-                               {
+                               if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category) {
+                                       grid_textbox.SetBounds(xLoc, rect.Top, ClientRectangle.Width-xLoc - (vbar.Visible ? vbar.Width: 0),row_height);
                                }
-                               if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category)\r
-                               {\r
-                                       grid_textbox.Location = new Point(xLoc, rect.Top);\r
-                                       grid_textbox.Size = new Size(ClientRectangle.Width-xLoc,ROW_HEIGHT);\r
-                               }\r
-                       }\r
-               }\r
-\r
-               private void DrawGridItem (GridItem grid_item, PaintEventArgs pevent, int depth, ref int yLoc) \r
-               {\r
-                       if (yLoc > -ROW_HEIGHT && yLoc < ClientRectangle.Height)\r
-                       {\r
-                       \r
-                               // left column\r
-                               pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), 0,yLoc,V_INDENT, ROW_HEIGHT);\r
-\r
-                               if (grid_item.Expandable) \r
-                               {\r
-                                       grid_item.PlusMinusBounds = DrawPlusMinus(pevent, 3, yLoc+3, grid_item.Expanded, grid_item.GridItemType == GridItemType.Category);\r
-                               }\r
-                       \r
-                               if (grid_item.GridItemType == GridItemType.Category)\r
-                               {\r
-                                       pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), depth*V_INDENT,yLoc,ClientRectangle.Width-(depth*V_INDENT), ROW_HEIGHT);\r
-                               }\r
-\r
-                               DrawGridItemLabel(grid_item, pevent, new Rectangle(V_INDENT,yLoc, splitter_location-V_INDENT, ROW_HEIGHT));\r
-                               DrawGridItemValue(grid_item, pevent, new Rectangle(splitter_location+2,yLoc, ClientRectangle.Width-splitter_location-2, ROW_HEIGHT));\r
-                       \r
-                               \r
-                               \r
-                       }\r
+                       }
+               }
+
+               private void DrawGridItem (GridItem grid_item, PaintEventArgs pevent, int depth, ref int yLoc) {
+                       if (yLoc > -row_height && yLoc < ClientRectangle.Height) {
+                               if (grid_item.Expandable) {
+                                       grid_item.PlusMinusBounds = DrawPlusMinus(pevent.Graphics, 3, yLoc+row_height/2-3, grid_item.Expanded, grid_item.GridItemType == GridItemType.Category);
+                               }
+                       
+                               if (grid_item.GridItemType == GridItemType.Category) {
+                                       pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), depth*V_INDENT,yLoc,ClientRectangle.Width-(depth*V_INDENT), row_height);
+                               }
+
+                               DrawGridItemLabel(grid_item, pevent, new Rectangle(depth*V_INDENT,yLoc, SplitterLocation-depth*V_INDENT, row_height));
+                               DrawGridItemValue(grid_item, pevent, new Rectangle(SplitterLocation+2,yLoc, ClientRectangle.Width-SplitterLocation-2, row_height));
+                       }
                        grid_item.Top = yLoc;
-                       yLoc += ROW_HEIGHT;
-                       open_grid_item_count++;\r
-               }\r
-\r
-               private Rectangle DrawPlusMinus (PaintEventArgs pevent, int x, int y, bool expanded, bool category)
-               {
+                       yLoc += row_height;
+                       open_grid_item_count++;
+               }
+
+               private Rectangle DrawPlusMinus (Graphics g, int x, int y, bool expanded, bool category) {
                        Rectangle bounds = new Rectangle(x, y, 8, 8);
-                       if (!category) pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush(Color.White), bounds);
-                       pevent.Graphics.DrawRectangle (SystemPens.ControlDark, bounds);
-                       pevent.Graphics.DrawLine (SystemPens.ControlDark, x+2, y+4, x + 6, y+4);
+                       if (!category) g.FillRectangle (Brushes.White, bounds);
+                       Pen pen = ThemeEngine.Current.ResPool.GetPen (property_grid.ViewForeColor);
+                       g.DrawRectangle (pen, bounds);
+                       g.DrawLine (pen, x+2, y+4, x + 6, y+4);
                        if (!expanded)
-                               pevent.Graphics.DrawLine (SystemPens.ControlDark, x+4, y+2, x+4, y+6);
+                               g.DrawLine (pen, x+4, y+2, x+4, y+6);
 
                        return bounds;
-               }\r
-\r
-               #endregion\r
-\r
-               #region Event Handling\r
-               private void RedrawEvent (object sender, System.EventArgs e) \r
-               {\r
-                       Refresh();\r
-               }
-\r
-               private void TextBoxValidating (object sender, CancelEventArgs e) \r
-               {\r
-                       if (this.property_grid.SelectedGridItem != null) \r
-                       {\r
-                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;\r
-                               if (desc != null) \r
-                               {\r
-                                       try \r
-                                       {\r
-                                               if (desc.Converter != null)\r
-                                               {\r
-                                                       SetPropertyValue(desc.Converter.ConvertFromString(grid_textbox.Text));\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       System.Console.WriteLine("No converter for type {0}",desc.PropertyType);\r
-                                               }\r
-                                       }\r
-                                       catch (Exception ex)\r
-                                       {\r
-                                               Console.WriteLine("Error converting string");\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
+               }
 
                #endregion
-\r
-               \r
-               #endregion\r
-\r
-               private void dropdown_form_Deactivate (object sender, EventArgs e) \r
-               {\r
-                       dropdown_form_showing = false;\r
-                       dropdown_form.Hide();\r
-                       //dropdown_form = new Form();\r
-               }\r
-\r
-               private void listBox_SelectedIndexChanged (object sender, EventArgs e) \r
-               {\r
-                       if (this.property_grid.SelectedGridItem != null) \r
-                       {\r
-                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;\r
-                               if (desc != null) \r
-                               {\r
-                                       SetPropertyValue(((ListBox)sender).SelectedItem);\r
-                               }\r
-                       }\r
-                       dropdown_form.Hide();\r
-                       //dropdown_form = new Form();\r
-                       Refresh();\r
-               }\r
-\r
-               private void SetPropertyValue(object newVal)\r
-               {\r
-                       if (this.property_grid.SelectedGridItem != null) \r
-                       {\r
-                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;\r
-                               if (desc != null) \r
-                               {\r
-                                       desc.SetValue(property_grid.SelectedObject, newVal);\r
-                               }\r
-                       }\r
-               }\r
-\r
-               private void DropDownButtonClicked (object sender, EventArgs e) \r
-               {\r
-                       if (property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) == null)\r
-                       {\r
-                               //dropdown_form.FormBorderStyle = FormBorderStyle.None;\r
-                               dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);\r
-                               ListBox listBox = new ListBox();\r
-                               listBox.Dock = DockStyle.Fill;\r
-                               listBox.SelectedIndexChanged +=new EventHandler(listBox_SelectedIndexChanged);\r
-                               foreach (object obj in property_grid.SelectedGridItem.PropertyDescriptor.Converter.GetStandardValues())\r
-                                       listBox.Items.Add(obj);\r
-                               dropdown_form.Controls.Clear();\r
-                               dropdown_form.Controls.Add(listBox);\r
-                               dropdown_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));\r
-                               dropdown_form.Width = grid_textbox.Width;\r
-                               dropdown_form.Show();\r
-                       }\r
-                       else // use editor\r
-                       {\r
-                               UITypeEditor editor = (UITypeEditor)property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor));\r
-                               System.ComponentModel.Design.ServiceContainer service_container = new System.ComponentModel.Design.ServiceContainer();\r
-                               service_container.AddService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService), this);\r
-                               SetPropertyValue(editor.EditValue(new ITypeDescriptorContextImpl(this.property_grid), service_container,property_grid.SelectedGridItem.Value));\r
-                       }\r
-               }\r
-               \r
-               private void DialogButtonClicked(object sender, EventArgs e) \r
-               {\r
-                       //dialog_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));\r
-                       //dropdown_form.Width = grid_textbox.Width;\r
-                       dialog_form.Show();\r
-               }\r
-\r
-               private void HandleScroll(object sender, ScrollEventArgs e)\r
-               {\r
-                       if (e.NewValue <= 0)\r
-                       {\r
-                               e.NewValue = 0;\r
-                               if (e.NewValue == vbar.Value)return;\r
-                       }\r
-                       if (e.NewValue > vbar.Maximum-ClientRectangle.Height/ROW_HEIGHT)\r
-                       {\r
-                               e.NewValue = vbar.Maximum-ClientRectangle.Height/ROW_HEIGHT+1;\r
-                               if (e.NewValue == vbar.Value)return;\r
-                       }\r
-\r
-                       switch (e.Type)\r
-                       {\r
-                               case ScrollEventType.SmallDecrement:\r
-                                       XplatUI.ScrollWindow(Handle, 0, ROW_HEIGHT, false);\r
-                                       grid_textbox.Top += ROW_HEIGHT;\r
-                                       Invalidate(ClientRectangle);\r
-                                       //Invalidate(new Rectangle(0,0,ClientRectangle.Width,ROW_HEIGHT));\r
-                                       break;\r
-                               case ScrollEventType.SmallIncrement:\r
-                                       XplatUI.ScrollWindow(Handle, 0, -ROW_HEIGHT, false);\r
-                                       grid_textbox.Top -= ROW_HEIGHT;\r
-                                       Invalidate(ClientRectangle);\r
-                                       //Invalidate(new Rectangle(0,ClientRectangle.Bottom-ROW_HEIGHT,ClientRectangle.Width,ROW_HEIGHT));\r
-                                       break;\r
-                               case ScrollEventType.LargeDecrement:\r
-                                       XplatUI.ScrollWindow(Handle, 0, ROW_HEIGHT, false);\r
-                                       Invalidate(ClientRectangle);\r
-                                       break;\r
-                               case ScrollEventType.LargeIncrement:\r
-                                       XplatUI.ScrollWindow(Handle, 0, -ROW_HEIGHT, false);\r
-                                       Invalidate(ClientRectangle);\r
-                                       break;\r
-                               case ScrollEventType.ThumbTrack:\r
-                                       XplatUI.ScrollWindow(Handle, 0, -(vbar.Value-e.NewValue), false);\r
-                                       Invalidate(ClientRectangle);\r
-                                       break;\r
-                               case ScrollEventType.ThumbPosition:\r
-                                       Invalidate(ClientRectangle);\r
-                                       break;\r
-                       }\r
-               }\r
-\r
-\r
-               private void HandleSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)\r
-               {                       \r
-                       // Region not working correctly\r
-                       //Region clip = new Region();\r
-                       //if (property_grid.SelectedGridItem != null)\r
-                       //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, ROW_HEIGHT));\r
-                       //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, ROW_HEIGHT));\r
-\r
-                       if (e.NewSelection.PropertyDescriptor != null)\r
-                       {\r
-                               grid_textbox.Text = e.NewSelection.PropertyDescriptor.Converter.ConvertToString(e.NewSelection.Value);\r
-                               if (e.NewSelection.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))\r
-                                       grid_textbox.Font = new Font(this.Font, FontStyle.Bold);\r
-                               else\r
-                                       grid_textbox.Font = this.Font;\r
-                       }\r
-\r
-                       Invalidate(/*clip*/this.ClientRectangle);\r
-                       Update();\r
-               }\r
-\r
-               private void HandlePropertyValueChanged(object s, PropertyValueChangedEventArgs e)\r
-               {\r
-                       if (e.ChangedItem.PropertyDescriptor != null)\r
-                       {\r
-                               grid_textbox.Text = e.ChangedItem.PropertyDescriptor.Converter.ConvertToString(e.ChangedItem.Value);\r
-                               if (e.ChangedItem.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))\r
-                                       grid_textbox.Font = new Font(this.Font, FontStyle.Bold);\r
-                               else\r
-                                       grid_textbox.Font = this.Font;\r
-                       }\r
-               }\r
-               \r
-               #region IWindowsFormsEditorService Members\r
-\r
-               public void CloseDropDown()\r
-               {\r
-                       dropdown_form_showing = false;\r
-                       dropdown_form.Hide();\r
-               }\r
-\r
-               public void DropDownControl(Control control)\r
-               {\r
-                       //dropdown_form.FormBorderStyle = FormBorderStyle.None;\r
-                       dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);\r
-                       dropdown_form.Size = control.Size;\r
-                       control.Dock = DockStyle.Fill;\r
-                       dropdown_form.Controls.Clear();\r
-                       dropdown_form.Controls.Add(control);\r
-                       dropdown_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));\r
-                       dropdown_form.Width = grid_textbox.Width;\r
-\r
-                       dropdown_form_showing = true;\r
-                       dropdown_form.Show();\r
-                       System.Windows.Forms.MSG msg = new MSG();\r
-                       while (XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0) && dropdown_form_showing) \r
-                       {\r
-                               XplatUI.TranslateMessage(ref msg);\r
-                               XplatUI.DispatchMessage(ref msg);\r
-                       }\r
-               }\r
-\r
-               public System.Windows.Forms.DialogResult ShowDialog(Form dialog)\r
-               {\r
-                       return dialog.ShowDialog(this);\r
-               }\r
-\r
-               #endregion\r
-\r
-               #region DropDownForm Class\r
-               #endregion DropDownForm Class\r
-\r
-               #region Internal Classes\r
-               internal class ITypeDescriptorContextImpl : System.ComponentModel.ITypeDescriptorContext\r
-               {\r
-                       private PropertyGrid property_grid;\r
-                       public ITypeDescriptorContextImpl(PropertyGrid propertyGrid)\r
-                       {\r
-                               property_grid = propertyGrid;\r
-                       }\r
-                       #region ITypeDescriptorContext Members\r
-\r
-                       public void OnComponentChanged()\r
-                       {\r
-                               // TODO:  Add SystemComp.OnComponentChanged implementation\r
-                       }\r
-\r
-                       public IContainer Container\r
-                       {\r
-                               get\r
-                               {\r
-                                       return property_grid as IContainer;\r
-                               }\r
-                       }\r
-\r
-                       public bool OnComponentChanging()\r
-                       {\r
-                               // TODO:  Add SystemComp.OnComponentChanging implementation\r
-                               return false;\r
-                       }\r
-\r
-                       public object Instance\r
-                       {\r
-                               get\r
-                               {\r
-                                       return property_grid.SelectedGridItem.Value;\r
-                               }\r
-                       }\r
-\r
-                       public PropertyDescriptor PropertyDescriptor\r
-                       {\r
-                               get\r
-                               {\r
-                                       return property_grid.SelectedGridItem.PropertyDescriptor;\r
-                               }\r
-                       }\r
-\r
-                       #endregion\r
-\r
-                       #region IServiceProvider Members\r
-\r
-                       public object GetService(Type serviceType)\r
-                       {\r
-                               // TODO:  Add SystemComp.GetService implementation\r
-                               return null;\r
-                       }\r
-\r
-                       #endregion\r
-\r
-               }\r
-\r
-\r
+
+               #region Event Handling
+               private void RedrawEvent (object sender, System.EventArgs e) {
+                       Refresh();
+               }
+
+               private void TextBoxValidating (object sender, CancelEventArgs e) {
+                       if (this.property_grid.SelectedGridItem != null) {
+                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
+                               if (desc != null) {
+                                       try {
+                                               if (desc.Converter != null) {
+                                                       SetPropertyValue(desc.Converter.ConvertFromString(grid_textbox.Text));
+                                               }
+                                               else {
+                                                       System.Console.WriteLine("No converter for type {0}",desc.PropertyType);
+                                               }
+                                       }
+                                       catch (Exception) {
+                                               Console.WriteLine("Error converting string");
+                                       }
+                               }
+                       }
+               }
+
+               #endregion
+
+               private void ToggleValue() {
+                       if (property_grid.SelectedGridItem.GridItemType == GridItemType.Property) {
+                               if (property_grid.SelectedGridItem.PropertyDescriptor != null) {
+                                       if (property_grid.SelectedGridItem.PropertyDescriptor.PropertyType == typeof(bool))
+                                               SetPropertyValue(!(bool)property_grid.SelectedGridItem.Value);
+                                       else if (property_grid.SelectedGridItem.PropertyDescriptor.Converter.GetStandardValuesSupported()){
+                                               System.ComponentModel.TypeConverter.StandardValuesCollection coll = 
+                                                       (System.ComponentModel.TypeConverter.StandardValuesCollection)property_grid.SelectedGridItem.PropertyDescriptor.Converter.GetStandardValues();
+                                               for (int i = 0; i < coll.Count; i++) {
+                                                       if (property_grid.SelectedGridItem.Value.Equals(coll[i])){
+                                                               if (i < coll.Count-1)
+                                                                       SetPropertyValue(coll[i+1]);
+                                                               else
+                                                                       SetPropertyValue(coll[0]);
+                                                               break;
+                                                       }
+
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               private void dropdown_form_Deactivate (object sender, EventArgs e) {
+                       dropdown_form_showing = false;
+                       dropdown_form.Hide();
+               }
+
+               private void listBox_MouseUp(object sender, MouseEventArgs e) {
+                       if (this.property_grid.SelectedGridItem != null) {
+                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
+                               if (desc != null) {
+                                       SetPropertyValue(((PropertyGridListBox)sender).SelectedItem);
+                               }
+                       }
+                       dropdown_form_showing = false;
+                       dropdown_form.Hide();
+                       Refresh();
+               }
+
+               private void SetPropertyValue(object newVal) {
+                       if (this.property_grid.SelectedGridItem != null) {
+                               PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
+                               if (desc != null) {
+                                       desc.SetValue(property_grid.SelectedObject, newVal);
+                               }
+                       }
+               }
+
+               private void DropDownButtonClicked (object sender, EventArgs e) {
+                       if (property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) == null) {
+                               dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);
+                               PropertyGridListBox listBox = new PropertyGridListBox();
+                               listBox.BorderStyle = BorderStyle.FixedSingle;
+                               listBox.Dock = DockStyle.Fill;
+                               int selected_index = 0;
+                               int i = 0;
+                               foreach (object obj in property_grid.SelectedGridItem.PropertyDescriptor.Converter.GetStandardValues()) {
+                                       listBox.Items.Add(obj);
+                                       if (property_grid.SelectedGridItem.Value.Equals(obj))
+                                               selected_index = i;
+                                       i++;
+                               }
+                               listBox.SelectedIndex = selected_index;
+                               listBox.MouseUp+=new MouseEventHandler(listBox_MouseUp);
+                               dropdown_form.Controls.Clear();
+                               dropdown_form.Controls.Add(listBox);
+                               dropdown_form.Location = PointToScreen(new Point(SplitterLocation,grid_textbox.Location.Y+row_height));
+                               dropdown_form.Width = this.Width - this.SplitterLocation;
+                               dropdown_form.Show();
+                       }
+                       else { // use editor
+                               UITypeEditor editor = (UITypeEditor)property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
+                               System.ComponentModel.Design.ServiceContainer service_container = new System.ComponentModel.Design.ServiceContainer();
+                               service_container.AddService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService), this);
+                               SetPropertyValue(editor.EditValue(new ITypeDescriptorContextImpl(this.property_grid), service_container,property_grid.SelectedGridItem.Value));
+                       }
+               }
+               
+               private void DialogButtonClicked(object sender, EventArgs e) {
+                       dialog_form.Show();
+               }
+
+               private void HandleValueChanged(object sender, EventArgs e) {
+                       if (vbar.Value <= 0) {
+                               vbar.Value = 0;
+                       }
+                       if (vbar.Value > vbar.Maximum-ClientRectangle.Height/row_height) {
+                               vbar.Value = vbar.Maximum-ClientRectangle.Height/row_height+1;
+                       }
+                       
+                       int scroll_amount = (skipped_grid_items-vbar.Value)*row_height;
+                       XplatUI.ScrollWindow(Handle, 0, scroll_amount, false);
+                       skipped_grid_items = vbar.Value;
+                       if (scroll_amount > 0)
+                               Invalidate(new Rectangle(0,0,Width,scroll_amount+1));
+                       else
+                               Invalidate(new Rectangle(0,Height+scroll_amount-1,Width,1-scroll_amount));
+               }
+
+               private void grid_textbox_ToggleValue(object sender, EventArgs e) {
+                       ToggleValue();
+                       Invalidate();
+               }
+
+               private void HandleSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e) {                 
+                       // Region not working correctly
+                       //Region clip = new Region();
+                       //if (property_grid.SelectedGridItem != null)
+                       //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, row_height));
+                       //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, row_height));
+
+                       if (e.NewSelection.GridItemType == GridItemType.Property)
+                               grid_textbox.Visible = true;
+                       else
+                               grid_textbox.Visible = false;
+
+                       if (e.NewSelection.PropertyDescriptor != null) {
+                               grid_textbox.Text = e.NewSelection.PropertyDescriptor.Converter.ConvertToString(e.NewSelection.Value);
+                               if (e.NewSelection.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
+                                       grid_textbox.Font = bold_font;
+                               else
+                                       grid_textbox.Font = this.Font;
+                       }
+
+                       Invalidate(/*clip*/this.ClientRectangle);
+                       Update();
+               }
+
+               private void HandlePropertyValueChanged(object s, PropertyValueChangedEventArgs e) {
+                       if (e.ChangedItem.PropertyDescriptor != null) {
+                               grid_textbox.Text = e.ChangedItem.PropertyDescriptor.Converter.ConvertToString(e.ChangedItem.Value);
+                               if (e.ChangedItem.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
+                                       grid_textbox.Font = bold_font;
+                               else
+                                       grid_textbox.Font = this.Font;
+                       }
+               }
+               
+
+               
+               #endregion
+
+               #region IWindowsFormsEditorService Members
+
+               public void CloseDropDown() {
+                       dropdown_form_showing = false;
+                       dropdown_form.Hide();
+               }
+
+               public void DropDownControl(Control control) {
+                       Object  queue_id;
+
+                       dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);
+                       dropdown_form.Size = control.Size;
+                       control.Dock = DockStyle.Fill;
+                       dropdown_form.Controls.Clear();
+                       dropdown_form.Controls.Add(control);
+                       dropdown_form.Location = PointToScreen(new Point(SplitterLocation,grid_textbox.Location.Y+row_height));
+                       dropdown_form.Width = Width - SplitterLocation;
+
+                       dropdown_form_showing = true;
+                       dropdown_form.Show();
+                       System.Windows.Forms.MSG msg = new MSG();
+                       queue_id = XplatUI.StartLoop(Thread.CurrentThread);
+                       while (XplatUI.GetMessage(queue_id, ref msg, IntPtr.Zero, 0, 0) && dropdown_form_showing) {
+                               XplatUI.TranslateMessage(ref msg);
+                               XplatUI.DispatchMessage(ref msg);
+                       }
+               }
+
+               public System.Windows.Forms.DialogResult ShowDialog(Form dialog) {
+                       return dialog.ShowDialog(this);
+               }
+
+               #endregion
+
+               #region Internal Classes
+               internal class ITypeDescriptorContextImpl : System.ComponentModel.ITypeDescriptorContext {
+                       private PropertyGrid property_grid;
+                       public ITypeDescriptorContextImpl(PropertyGrid propertyGrid) {
+                               property_grid = propertyGrid;
+                       }
+                       #region ITypeDescriptorContext Members
+
+                       public void OnComponentChanged() {
+                               // TODO:  Add SystemComp.OnComponentChanged implementation
+                       }
+
+                       public IContainer Container {
+                               get {
+                                       return property_grid as IContainer;
+                               }
+                       }
+
+                       public bool OnComponentChanging() {
+                               // TODO:  Add SystemComp.OnComponentChanging implementation
+                               return false;
+                       }
+
+                       public object Instance {
+                               get {
+                                       return property_grid.SelectedGridItem.Value;
+                               }
+                       }
+
+                       public PropertyDescriptor PropertyDescriptor {
+                               get {
+                                       return property_grid.SelectedGridItem.PropertyDescriptor;
+                               }
+                       }
+
+                       #endregion
+
+                       #region IServiceProvider Members
+
+                       public object GetService(Type serviceType) {
+                               // TODO:  Add SystemComp.GetService implementation
+                               return null;
+                       }
+
+                       #endregion
+
+               }
+
+
                
                /*
                        class ComboListBox
                */
-               internal class PropertyGridDropDown : Form 
-               {
-                       protected override CreateParams CreateParams
-                       {
-                               get \r
-                               {
+               internal class PropertyGridDropDown : Form {
+                       protected override CreateParams CreateParams {
+                               get {
                                        CreateParams cp = base.CreateParams;                            
                                        cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
-                                       cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);                                
+                                       cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_TOPMOST);                            
                                        return cp;
                                }
                        }
 
-               }\r
-               #endregion\r
-\r
-       }\r
+               }
+
+               internal class PropertyGridListBox : ListBox {
+                       private bool mouse_down;
+                       public PropertyGridListBox() {
+                               SetStyle(ControlStyles.DoubleBuffer, true);
+                               mouse_down =false;
+                       }
+
+                       protected override void OnMouseDown(MouseEventArgs e) {
+                               mouse_down = true;
+                               base.OnMouseDown (e);
+                       }
+
+                       protected override void OnMouseUp(MouseEventArgs e) {
+                               mouse_down = false;
+                               base.OnMouseUp (e);
+                       }
+
+                       protected override void OnMouseMove(MouseEventArgs e) {
+                               if (!mouse_down)
+                                       return;
+                               for (int i = 0; i < Items.Count; i++) {
+                                       if (GetItemRectangle(i).Contains(e.X, e.Y)) {
+                                               SelectedIndex = i;
+                                               break;
+                                       }
+                               }
+                               base.OnMouseMove (e);
+                       }
+
+
+
+
+
+               }
+
+               #endregion
+       }
 }