merge from trunk revisions 58933, 58935, 58936
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PropertyGridView.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Jonathan Chambers       (jonathan.chambers@ansys.com)
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.Collections;
31 using System.Drawing;
32 using System.Drawing.Design;
33 using System.ComponentModel;
34 using System.Windows.Forms.Design;
35
36 namespace System.Windows.Forms.PropertyGridInternal 
37 {
38         internal class PropertyGridView : System.Windows.Forms.ScrollableControl, IWindowsFormsEditorService
39         {
40
41                 #region Private Members
42                 private const int V_INDENT = 16;
43                 private const int ROW_HEIGHT = 16;
44                 private const int RESIZE_WIDTH = 3;
45                 private const int BUTTON_WIDTH = 25;
46                 private PropertyGridTextBox grid_textbox;
47                 private PropertyGrid property_grid;
48                 private bool resizing_grid;
49                 private int splitter_location;
50                 private int open_grid_item_count = -1;
51                 private int skipped_grid_items;
52                 private PropertyGridDropDown dropdown_form;
53                 private bool dropdown_form_showing;
54                 private Form dialog_form;
55                 private VScrollBar vbar;
56                 private StringFormat stringFormat;
57                 #endregion
58
59                 #region Contructors
60                 public PropertyGridView (PropertyGrid propertyGrid) 
61                 {
62                         property_grid = propertyGrid;
63
64                         property_grid.SelectedGridItemChanged+=new SelectedGridItemChangedEventHandler(HandleSelectedGridItemChanged);
65                         property_grid.PropertyValueChanged+=new PropertyValueChangedEventHandler(HandlePropertyValueChanged);
66
67                         stringFormat = new StringFormat();
68                         stringFormat.FormatFlags = StringFormatFlags.NoWrap;
69                         stringFormat.Trimming = StringTrimming.None;
70
71                         this.BackColor = Color.Beige;
72                         grid_textbox = new PropertyGridTextBox();
73                         grid_textbox.DropDownButtonClicked +=new EventHandler(DropDownButtonClicked);
74                         grid_textbox.DialogButtonClicked +=new EventHandler(DialogButtonClicked);
75
76                         
77                         dropdown_form = new PropertyGridDropDown();
78                         dropdown_form.FormBorderStyle = FormBorderStyle.None;
79                         dropdown_form.ShowInTaskbar = false;
80                         
81                         dialog_form = new Form();
82                         //dialog_form.FormBorderStyle = FormBorderStyle.None;
83
84                         
85
86                         grid_textbox.Visible = false;
87                         grid_textbox.Font = this.Font;//new Font(this.Font,FontStyle.Bold);
88                         grid_textbox.BackColor = this.BackColor;
89                         // Not working at all, used to??
90                         grid_textbox.Validating += new CancelEventHandler(TextBoxValidating);
91                         this.Controls.Add(grid_textbox);
92
93                         vbar = new VScrollBar();
94                         vbar.Visible = false;
95                         vbar.Scroll+=new ScrollEventHandler(HandleScroll);
96                         vbar.Dock = DockStyle.Right;
97                         this.Controls.Add(vbar);
98
99                         splitter_location = 65;
100                         resizing_grid = false;
101
102                         ForeColorChanged+=new EventHandler(RedrawEvent);
103                         BackColorChanged+=new System.EventHandler(RedrawEvent);
104                         FontChanged+=new EventHandler(RedrawEvent);
105                         SizeChanged+=new EventHandler(RedrawEvent);
106                         
107                         SetStyle(ControlStyles.DoubleBuffer, true);
108                         SetStyle(ControlStyles.UserPaint, true);
109                         SetStyle(ControlStyles.AllPaintingInWmPaint, true);
110                         SetStyle(ControlStyles.ResizeRedraw, false);
111                         SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
112                 }
113
114                 #endregion
115
116                 #region Protected Instance Methods
117                 
118                 protected override void OnPaint(PaintEventArgs e)
119                 {
120                         if (property_grid.SelectedGridItem != null && property_grid.SelectedGridItem.GridItemType == GridItemType.Property) 
121                         {
122                                 grid_textbox.Visible = true;
123                                 if (!grid_textbox.Focused)
124                                         grid_textbox.Focus();
125                         }
126                         else 
127                         {
128                                 grid_textbox.Visible = false;
129                         }
130
131                         // Decide if we need a scrollbar
132                         open_grid_item_count = 0;
133
134                         // draw grid outline
135                         //DrawBackground(e);
136                         
137                         e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
138
139                         // draw grid items
140                         // can we use the transform
141                         //pevent.Graphics.TranslateTransform(0, -vbar.Value*ROW_HEIGHT);
142                         int yLoc = -vbar.Value*ROW_HEIGHT;
143                         DrawGridItems(property_grid.grid_items, e, 1, ref yLoc);
144
145                         DrawGrid(e, yLoc);
146                         e.Graphics.DrawRectangle(SystemPens.ControlDark, 0,0,Width-1,Height-1 );
147                         
148                         
149
150
151                         UpdateScrollBar();
152                         
153                         base.OnPaint(e);
154                 }
155
156                 protected override void OnMouseMove (MouseEventArgs e) 
157                 {
158
159                         if (resizing_grid) 
160                         {
161                                 splitter_location = Math.Max(e.X,2*V_INDENT);
162                                 Refresh();
163                         }
164                         if (e.X > splitter_location - RESIZE_WIDTH && e.X < splitter_location + RESIZE_WIDTH) 
165                                 this.Cursor = Cursors.SizeWE;
166                         else
167                                 this.Cursor = Cursors.Default;
168                         base.OnMouseMove (e);
169                 }
170
171                 private GridItem GetSelectedGridItem (GridItemCollection grid_items, int y, ref int current) 
172                 {
173                         foreach (GridItem child_grid_item in grid_items) 
174                         {
175                                 if (y > current && y < current + ROW_HEIGHT) 
176                                 {
177                                         return child_grid_item;
178                                 }
179                                 current += ROW_HEIGHT;
180                                 if (child_grid_item.Expanded)
181                                 {
182                                         GridItem foundItem = GetSelectedGridItem(child_grid_item.GridItems, y, ref current);
183                                         if (foundItem != null)
184                                                 return foundItem;
185                                 }
186                         }
187                         return null;
188                 }
189
190                 protected override void OnMouseDown (MouseEventArgs e) 
191                 {
192                         if (e.X > splitter_location - RESIZE_WIDTH && e.X < splitter_location + RESIZE_WIDTH) 
193                         {
194                                 resizing_grid = true;
195                         }
196                         else 
197                         {
198                                 int offset = -vbar.Value*ROW_HEIGHT;
199                                 GridItem foundItem = GetSelectedGridItem(property_grid.grid_items, e.Y, ref offset);
200                                 
201                                 if (foundItem != null)
202                                 {
203                                         if (foundItem.Expandable) 
204                                         {
205                                                 if (e.X >=3 && e.X <= 11 && (e.Y % ROW_HEIGHT >= 3 && e.Y % ROW_HEIGHT <= 11))
206                                                 {
207                                                         foundItem.Expanded = !foundItem.Expanded;
208                                                         Invalidate();
209                                                 }
210                                         }
211                                         this.property_grid.SelectedGridItem = foundItem;
212                                 }
213                                 
214                                 base.OnMouseDown (e);
215                         }
216                 }
217
218                 protected override void OnMouseUp (MouseEventArgs e) 
219                 {
220                         resizing_grid = false;
221                         base.OnMouseUp (e);
222                 }
223
224                 protected override void OnKeyDown (KeyEventArgs e) 
225                 {
226                         base.OnKeyDown (e);
227                 }
228
229                 #endregion
230
231                 #region Private Helper Methods
232
233                 private void UpdateScrollBar()
234                 {
235                         int visible_rows = this.ClientRectangle.Height/ROW_HEIGHT;
236                         if (open_grid_item_count > visible_rows)
237                         {
238                                 vbar.Visible = true;
239                                 vbar.SmallChange = 1;
240                                 vbar.Minimum = 0;
241                                 vbar.Maximum = open_grid_item_count-1;
242                                 vbar.LargeChange = visible_rows;
243                         }
244                         else
245                         {
246                                 vbar.Visible = false;
247                         }
248
249                 }
250
251                 private GridItem GetGridItemAt (int y) 
252                 {
253                         return null;
254                 }
255
256                 #region Drawing Code
257
258                 private void DrawGrid(PaintEventArgs pevent, int yLoc)
259                 {
260                         Pen pen = ThemeEngine.Current.ResPool.GetPen(property_grid.LineColor);
261                         // vertical divider line
262                         pevent.Graphics.DrawLine(pen, splitter_location, 0, splitter_location, yLoc);
263                         
264                         while (yLoc >= 0)
265                         {
266                                 // horizontal lines
267                                 pevent.Graphics.DrawLine(pen, 0, yLoc, ClientRectangle.Width, yLoc);
268                                 yLoc -= ROW_HEIGHT;
269                         }
270                 }
271
272                 private void DrawGridItems(GridItemCollection grid_items, PaintEventArgs pevent, int depth, ref int yLoc)
273                 {
274                         foreach (GridItem grid_item in grid_items) 
275                         {
276                                 DrawGridItem (grid_item, pevent, depth, ref yLoc);
277                                 if (grid_item.Expanded)
278                                         DrawGridItems(grid_item.GridItems, pevent, (grid_item.GridItemType == GridItemType.Category) ? depth : depth+1, ref yLoc);
279                         }
280                 }
281
282                 private void DrawGridItemLabel(GridItem grid_item, PaintEventArgs pevent, Rectangle rect)
283                 {
284                         int x = rect.X+1;
285                         if (grid_item.Parent != null && grid_item.Parent.GridItemType != GridItemType.Category)
286                                 x += V_INDENT;
287
288                         Font font = this.Font;
289                         Brush brush = SystemBrushes.WindowText;
290                         if (grid_item.GridItemType == GridItemType.Category)
291                         {
292                                 font = new Font(font, FontStyle.Bold);
293                                 brush = SystemBrushes.ControlDark;
294                         }
295
296                         if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category)
297                         {
298                                 pevent.Graphics.FillRectangle (SystemBrushes.Highlight, rect);
299                                 // Label
300                                 brush = SystemBrushes.HighlightText;
301                         }
302
303                         
304                         pevent.Graphics.DrawString(grid_item.Label,font,brush,new Rectangle(x, rect.Y + 2,x-rect.X+rect.Width-2,rect.Height-2),stringFormat);
305                 }
306
307                 private void DrawGridItemValue(GridItem grid_item, PaintEventArgs pevent, Rectangle rect)
308                 {
309                         // Value
310                         if (grid_item.PropertyDescriptor != null)
311                         {
312
313                                 bool paintsValue = false;
314                                 UITypeEditor editor = null;
315                                 object temp = grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
316                                 editor = (UITypeEditor)temp;//grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
317                                 if (editor != null) 
318                                 {
319                                         paintsValue = editor.GetPaintValueSupported();
320                                 }
321
322                                 if (grid_item == property_grid.SelectedGridItem)
323                                 {
324                                         grid_textbox.ReadOnly = false;
325                                         grid_textbox.DropDownButtonVisible = false;
326                                         grid_textbox.DialogButtonVisible = false;
327                                         if (editor != null) 
328                                         {
329                                                 UITypeEditorEditStyle style = editor.GetEditStyle();
330                                         
331                                                 switch (style)
332                                                 {
333                                                         case UITypeEditorEditStyle.DropDown:
334                                                                 grid_textbox.DropDownButtonVisible = true;
335                                                                 break;
336                                                         case UITypeEditorEditStyle.Modal:
337                                                                 grid_textbox.DialogButtonVisible = true;
338                                                                 break;
339                                                 }
340                                         }
341                                         else 
342                                         {
343                                                 try
344                                                 {
345                                                         if (grid_item.PropertyDescriptor.Converter != null)
346                                                         {
347                                                                 if (grid_item.PropertyDescriptor.Converter.GetStandardValuesSupported()) 
348                                                                 {
349                                                         
350                                                                         grid_textbox.DropDownButtonVisible = true;
351                                                                         grid_textbox.ReadOnly = true;
352                                                                 }
353                                                         }
354                                                         else
355                                                         {
356                                                                 System.Console.WriteLine("Converter not available for type {0}",grid_item.PropertyDescriptor.PropertyType);
357                                                         }
358                                                 
359                                                 }
360                                                 catch (Exception ex)
361                                                 {
362                                                 }
363                                         }
364                                 }
365
366                                 
367
368                                 int xLoc = splitter_location+1;
369                                 if (paintsValue)
370                                 {
371                                         pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(Color.Black), splitter_location+2,rect.Y+2, 20, ROW_HEIGHT-4);
372                                         try
373                                         {
374                                                 editor.PaintValue(grid_item.Value, pevent.Graphics, new Rectangle(splitter_location+3,rect.Y+3, 19, ROW_HEIGHT-5));
375                                         }
376                                         catch (Exception ex)
377                                         {
378                                                 System.Console.WriteLine(ex.Message);
379                                                 System.Console.WriteLine("Paint Value failed for type {0}",grid_item.PropertyDescriptor.PropertyType);
380                                                 // design time stuff is not playing nice
381                                         }
382                                         xLoc += 27;
383                                 }
384
385                                 Font font = this.Font;
386                                 try 
387                                 {
388                                         if (grid_item.PropertyDescriptor.Converter != null)
389                                         {
390                                                 string value = grid_item.PropertyDescriptor.Converter.ConvertToString(grid_item.Value);
391                                                 if (grid_item.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
392                                                         font = new Font(font, FontStyle.Bold);
393                                 
394                                                 pevent.Graphics.DrawString(value,font,SystemBrushes.WindowText,new RectangleF(xLoc,rect.Y+2, ClientRectangle.Width-(xLoc), ROW_HEIGHT),stringFormat);
395                                         }
396                                         else
397                                         {
398                                                 System.Console.WriteLine("No converter for type {0}",grid_item.PropertyDescriptor.PropertyType);
399                                         }
400
401                                 }
402                                 catch (Exception e)
403                                 {
404                                 }
405                                 if (grid_item == property_grid.SelectedGridItem && grid_item.GridItemType != GridItemType.Category)
406                                 {
407                                         grid_textbox.SetBounds(xLoc, rect.Top, ClientRectangle.Width-xLoc - (vbar.Visible ? vbar.Width: 0),ROW_HEIGHT);
408                                 }
409                         }
410                 }
411
412                 private void DrawGridItem (GridItem grid_item, PaintEventArgs pevent, int depth, ref int yLoc) 
413                 {
414                         if (yLoc > -ROW_HEIGHT && yLoc < ClientRectangle.Height)
415                         {
416                         
417                                 // left column
418                                 pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), 0,yLoc,V_INDENT, ROW_HEIGHT);
419
420                                 if (grid_item.Expandable) 
421                                 {
422                                         grid_item.PlusMinusBounds = DrawPlusMinus(pevent, 3, yLoc+3, grid_item.Expanded, grid_item.GridItemType == GridItemType.Category);
423                                 }
424                         
425                                 if (grid_item.GridItemType == GridItemType.Category)
426                                 {
427                                         pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (property_grid.LineColor), depth*V_INDENT,yLoc,ClientRectangle.Width-(depth*V_INDENT), ROW_HEIGHT);
428                                 }
429
430                                 DrawGridItemLabel(grid_item, pevent, new Rectangle(depth*V_INDENT,yLoc, splitter_location-depth*V_INDENT, ROW_HEIGHT));
431                                 DrawGridItemValue(grid_item, pevent, new Rectangle(splitter_location+2,yLoc, ClientRectangle.Width-splitter_location-2, ROW_HEIGHT));
432                         
433                                 
434                                 
435                         }
436                         grid_item.Top = yLoc;
437                         yLoc += ROW_HEIGHT;
438                         open_grid_item_count++;
439                 }
440
441                 private Rectangle DrawPlusMinus (PaintEventArgs pevent, int x, int y, bool expanded, bool category)
442                 {
443                         Rectangle bounds = new Rectangle(x, y, 8, 8);
444                         if (!category) pevent.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush(Color.White), bounds);
445                         pevent.Graphics.DrawRectangle (SystemPens.ControlDark, bounds);
446                         pevent.Graphics.DrawLine (SystemPens.ControlDark, x+2, y+4, x + 6, y+4);
447                         if (!expanded)
448                                 pevent.Graphics.DrawLine (SystemPens.ControlDark, x+4, y+2, x+4, y+6);
449
450                         return bounds;
451                 }
452
453                 #endregion
454
455                 #region Event Handling
456                 private void RedrawEvent (object sender, System.EventArgs e) 
457                 {
458                         Refresh();
459                 }
460
461                 private void TextBoxValidating (object sender, CancelEventArgs e) 
462                 {
463                         if (this.property_grid.SelectedGridItem != null) 
464                         {
465                                 PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
466                                 if (desc != null) 
467                                 {
468                                         try 
469                                         {
470                                                 if (desc.Converter != null)
471                                                 {
472                                                         SetPropertyValue(desc.Converter.ConvertFromString(grid_textbox.Text));
473                                                 }
474                                                 else
475                                                 {
476                                                         System.Console.WriteLine("No converter for type {0}",desc.PropertyType);
477                                                 }
478                                         }
479                                         catch (Exception ex)
480                                         {
481                                                 Console.WriteLine("Error converting string");
482                                         }
483                                 }
484                         }
485                 }
486
487                 #endregion
488
489                 
490                 #endregion
491
492                 private void dropdown_form_Deactivate (object sender, EventArgs e) 
493                 {
494                         dropdown_form_showing = false;
495                         dropdown_form.Hide();
496                         //dropdown_form = new Form();
497                 }
498
499                 private void listBox_SelectedIndexChanged (object sender, EventArgs e) 
500                 {
501                         if (this.property_grid.SelectedGridItem != null) 
502                         {
503                                 PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
504                                 if (desc != null) 
505                                 {
506                                         SetPropertyValue(((ListBox)sender).SelectedItem);
507                                 }
508                         }
509                         dropdown_form.Hide();
510                         //dropdown_form = new Form();
511                         Refresh();
512                 }
513
514                 private void SetPropertyValue(object newVal)
515                 {
516                         if (this.property_grid.SelectedGridItem != null) 
517                         {
518                                 PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
519                                 if (desc != null) 
520                                 {
521                                         desc.SetValue(property_grid.SelectedObject, newVal);
522                                 }
523                         }
524                 }
525
526                 private void DropDownButtonClicked (object sender, EventArgs e) 
527                 {
528                         if (property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) == null)
529                         {
530                                 //dropdown_form.FormBorderStyle = FormBorderStyle.None;
531                                 dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);
532                                 ListBox listBox = new ListBox();
533                                 listBox.Dock = DockStyle.Fill;
534                                 listBox.SelectedIndexChanged +=new EventHandler(listBox_SelectedIndexChanged);
535                                 foreach (object obj in property_grid.SelectedGridItem.PropertyDescriptor.Converter.GetStandardValues())
536                                         listBox.Items.Add(obj);
537                                 dropdown_form.Controls.Clear();
538                                 dropdown_form.Controls.Add(listBox);
539                                 dropdown_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));
540                                 dropdown_form.Width = grid_textbox.Width;
541                                 dropdown_form.Show();
542                         }
543                         else // use editor
544                         {
545                                 UITypeEditor editor = (UITypeEditor)property_grid.SelectedGridItem.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
546                                 System.ComponentModel.Design.ServiceContainer service_container = new System.ComponentModel.Design.ServiceContainer();
547                                 service_container.AddService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService), this);
548                                 SetPropertyValue(editor.EditValue(new ITypeDescriptorContextImpl(this.property_grid), service_container,property_grid.SelectedGridItem.Value));
549                         }
550                 }
551                 
552                 private void DialogButtonClicked(object sender, EventArgs e) 
553                 {
554                         //dialog_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));
555                         //dropdown_form.Width = grid_textbox.Width;
556                         dialog_form.Show();
557                 }
558
559                 private void HandleScroll(object sender, ScrollEventArgs e)
560                 {
561                         if (e.NewValue <= 0)
562                         {
563                                 e.NewValue = 0;
564                                 if (e.NewValue == vbar.Value)return;
565                         }
566                         if (e.NewValue > vbar.Maximum-ClientRectangle.Height/ROW_HEIGHT)
567                         {
568                                 e.NewValue = vbar.Maximum-ClientRectangle.Height/ROW_HEIGHT+1;
569                                 if (e.NewValue == vbar.Value)return;
570                         }
571
572                         switch (e.Type)
573                         {
574                                 case ScrollEventType.SmallDecrement:
575                                         XplatUI.ScrollWindow(Handle, 0, ROW_HEIGHT, false);
576                                         grid_textbox.Top += ROW_HEIGHT;
577                                         Invalidate(ClientRectangle);
578                                         //Invalidate(new Rectangle(0,0,ClientRectangle.Width,ROW_HEIGHT));
579                                         break;
580                                 case ScrollEventType.SmallIncrement:
581                                         XplatUI.ScrollWindow(Handle, 0, -ROW_HEIGHT, false);
582                                         grid_textbox.Top -= ROW_HEIGHT;
583                                         Invalidate(ClientRectangle);
584                                         //Invalidate(new Rectangle(0,ClientRectangle.Bottom-ROW_HEIGHT,ClientRectangle.Width,ROW_HEIGHT));
585                                         break;
586                                 case ScrollEventType.LargeDecrement:
587                                         XplatUI.ScrollWindow(Handle, 0, ROW_HEIGHT, false);
588                                         Invalidate(ClientRectangle);
589                                         break;
590                                 case ScrollEventType.LargeIncrement:
591                                         XplatUI.ScrollWindow(Handle, 0, -ROW_HEIGHT, false);
592                                         Invalidate(ClientRectangle);
593                                         break;
594                                 case ScrollEventType.ThumbTrack:
595                                         XplatUI.ScrollWindow(Handle, 0, -(vbar.Value-e.NewValue), false);
596                                         Invalidate(ClientRectangle);
597                                         break;
598                                 case ScrollEventType.ThumbPosition:
599                                         Invalidate(ClientRectangle);
600                                         break;
601                         }
602                 }
603
604
605                 private void HandleSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
606                 {                       
607                         // Region not working correctly
608                         //Region clip = new Region();
609                         //if (property_grid.SelectedGridItem != null)
610                         //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, ROW_HEIGHT));
611                         //      clip.Union(new Rectangle(0,property_grid.SelectedGridItem.Top, ClientRectangle.Width, ROW_HEIGHT));
612
613                         if (e.NewSelection.PropertyDescriptor != null)
614                         {
615                                 grid_textbox.Text = e.NewSelection.PropertyDescriptor.Converter.ConvertToString(e.NewSelection.Value);
616                                 if (e.NewSelection.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
617                                         grid_textbox.Font = new Font(this.Font, FontStyle.Bold);
618                                 else
619                                         grid_textbox.Font = this.Font;
620                         }
621
622                         Invalidate(/*clip*/this.ClientRectangle);
623                         Update();
624                 }
625
626                 private void HandlePropertyValueChanged(object s, PropertyValueChangedEventArgs e)
627                 {
628                         if (e.ChangedItem.PropertyDescriptor != null)
629                         {
630                                 grid_textbox.Text = e.ChangedItem.PropertyDescriptor.Converter.ConvertToString(e.ChangedItem.Value);
631                                 if (e.ChangedItem.PropertyDescriptor.CanResetValue(property_grid.SelectedObject))
632                                         grid_textbox.Font = new Font(this.Font, FontStyle.Bold);
633                                 else
634                                         grid_textbox.Font = this.Font;
635                         }
636                 }
637                 
638                 #region IWindowsFormsEditorService Members
639
640                 public void CloseDropDown()
641                 {
642                         dropdown_form_showing = false;
643                         dropdown_form.Hide();
644                 }
645
646                 public void DropDownControl(Control control)
647                 {
648                         //dropdown_form.FormBorderStyle = FormBorderStyle.None;
649                         dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);
650                         dropdown_form.Size = control.Size;
651                         control.Dock = DockStyle.Fill;
652                         dropdown_form.Controls.Clear();
653                         dropdown_form.Controls.Add(control);
654                         dropdown_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));
655                         dropdown_form.Width = grid_textbox.Width;
656
657                         dropdown_form_showing = true;
658                         dropdown_form.Show();
659                         System.Windows.Forms.MSG msg = new MSG();
660                         while (XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0) && dropdown_form_showing) 
661                         {
662                                 XplatUI.TranslateMessage(ref msg);
663                                 XplatUI.DispatchMessage(ref msg);
664                         }
665                 }
666
667                 public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
668                 {
669                         return dialog.ShowDialog(this);
670                 }
671
672                 #endregion
673
674                 #region DropDownForm Class
675                 #endregion DropDownForm Class
676
677                 #region Internal Classes
678                 internal class ITypeDescriptorContextImpl : System.ComponentModel.ITypeDescriptorContext
679                 {
680                         private PropertyGrid property_grid;
681                         public ITypeDescriptorContextImpl(PropertyGrid propertyGrid)
682                         {
683                                 property_grid = propertyGrid;
684                         }
685                         #region ITypeDescriptorContext Members
686
687                         public void OnComponentChanged()
688                         {
689                                 // TODO:  Add SystemComp.OnComponentChanged implementation
690                         }
691
692                         public IContainer Container
693                         {
694                                 get
695                                 {
696                                         return property_grid as IContainer;
697                                 }
698                         }
699
700                         public bool OnComponentChanging()
701                         {
702                                 // TODO:  Add SystemComp.OnComponentChanging implementation
703                                 return false;
704                         }
705
706                         public object Instance
707                         {
708                                 get
709                                 {
710                                         return property_grid.SelectedGridItem.Value;
711                                 }
712                         }
713
714                         public PropertyDescriptor PropertyDescriptor
715                         {
716                                 get
717                                 {
718                                         return property_grid.SelectedGridItem.PropertyDescriptor;
719                                 }
720                         }
721
722                         #endregion
723
724                         #region IServiceProvider Members
725
726                         public object GetService(Type serviceType)
727                         {
728                                 // TODO:  Add SystemComp.GetService implementation
729                                 return null;
730                         }
731
732                         #endregion
733
734                 }
735
736
737                 
738                 /*
739                         class ComboListBox
740                 */
741                 internal class PropertyGridDropDown : Form 
742                 {
743                         protected override CreateParams CreateParams
744                         {
745                                 get 
746                                 {
747                                         CreateParams cp = base.CreateParams;                            
748                                         cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
749                                         cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);                                
750                                         return cp;
751                                 }
752                         }
753
754                 }
755                 #endregion
756
757         }
758 }