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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jonathan Chambers (jonathan.chambers@ansys.com)
29 using System.Collections;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
33 using System.Reflection;
34 using System.Windows.Forms.Design;
35 using System.Windows.Forms.PropertyGridInternal;
37 namespace System.Windows.Forms
39 [Designer("System.Windows.Forms.Design.PropertyGridDesigner, " + Consts.AssemblySystem_Design)]
40 public class PropertyGrid : System.Windows.Forms.ContainerControl, ComponentModel.Com2Interop.IComPropertyBrowser
42 #region Private Members
45 private const int GRID_ITEM_HEIGHT = 16;
46 private const int GRID_LEFT_COLUMN_WIDTH = 16;
47 private const int DIVIDER_PADDING = 2;
49 private AttributeCollection browsable_attributes = null;
50 private bool can_show_commands = false;
51 private Color commands_back_color;
52 private Color commands_fore_color;
53 private bool commands_visible;
54 private bool commands_visible_if_available;
55 private Point context_menu_default_location;
56 private bool large_buttons;
57 private Color line_color;
58 private PropertySort property_sort;
59 private PropertyTabCollection property_tabs;
60 private GridItem selected_grid_item;
61 internal GridItemCollection grid_items;
62 private object[] selected_objects;
63 private PropertyTab selected_tab;
65 private ImageList toolbar_imagelist;
66 private ToolBarButton categorized_toolbarbutton;
67 private ToolBarButton alphabetic_toolbarbutton;
68 private ToolBarButton separator_toolbarbutton;
69 private ToolBarButton propertypages_toolbarbutton;
71 internal ToolBar toolbar;
72 internal PropertyGridView property_grid_view;
73 internal Splitter splitter;
74 internal Panel help_panel;
75 internal Label help_title_label;
76 internal Label help_description_label;
77 private ContextMenu context_menu;
78 private MenuItem reset_menuitem;
79 private MenuItem description_menuitem;
82 #endregion // Private Members
87 line_color = SystemColors.ScrollBar;
88 browsable_attributes = new AttributeCollection(new Attribute[] {});
89 commands_visible_if_available = false;
90 property_sort = PropertySort.CategorizedAlphabetical;
92 property_grid_view = new PropertyGridView(this);
93 property_grid_view.Dock = DockStyle.Fill;
95 splitter = new Splitter();
96 splitter.Dock = DockStyle.Bottom;
98 help_panel = new Panel();
99 help_panel.Dock = DockStyle.Bottom;
100 help_panel.Height = 50;
102 help_description_label = new Label();
103 help_description_label.Dock = DockStyle.Fill;
104 help_description_label.Name = "help_description_label";
105 help_description_label.Font = this.Font;
107 help_title_label = new Label();
108 help_title_label.Dock = DockStyle.Top;
109 help_title_label.Name = "help_title_label";
110 help_description_label.Font = new Font(this.Font,FontStyle.Bold);
112 help_panel.Controls.Add(help_description_label);
113 help_panel.Controls.Add(help_title_label);
115 toolbar = new ToolBar();
116 toolbar.Dock = DockStyle.Top;
117 categorized_toolbarbutton = new ToolBarButton();
118 alphabetic_toolbarbutton = new ToolBarButton();
119 separator_toolbarbutton = new ToolBarButton();
120 propertypages_toolbarbutton = new ToolBarButton();
121 context_menu = new ContextMenu();
123 //help_title_label.Dock = DockStyle.Top;
124 help_title_label.Name = "help_title_label";
125 help_title_label.Location = new Point(2,20);
126 help_title_label.Size = new Size(20,20);
128 toolbar_imagelist = new ImageList();
129 toolbar_imagelist.ColorDepth = ColorDepth.Depth32Bit;
130 toolbar_imagelist.ImageSize = new System.Drawing.Size(16, 16);
131 toolbar_imagelist.TransparentColor = System.Drawing.Color.Transparent;
133 toolbar.Appearance = ToolBarAppearance.Flat;
134 toolbar.AutoSize = false;
135 toolbar.Buttons.AddRange(new ToolBarButton[] {
136 categorized_toolbarbutton,
137 alphabetic_toolbarbutton,
138 separator_toolbarbutton,
139 propertypages_toolbarbutton});
141 toolbar.ButtonSize = new System.Drawing.Size(20, 20);
142 toolbar.ImageList = toolbar_imagelist;
143 toolbar.Location = new System.Drawing.Point(0, 0);
144 toolbar.Name = "toolbar";
145 toolbar.ShowToolTips = true;
146 toolbar.Size = new System.Drawing.Size(256, 27);
147 toolbar.TabIndex = 0;
148 toolbar.ButtonClick += new ToolBarButtonClickEventHandler(toolbar_ButtonClick);
150 categorized_toolbarbutton.Style = ToolBarButtonStyle.ToggleButton;
151 categorized_toolbarbutton.ToolTipText = "Categorized";
152 categorized_toolbarbutton.Text = "C";
154 alphabetic_toolbarbutton.Style = ToolBarButtonStyle.ToggleButton;
155 alphabetic_toolbarbutton.ToolTipText = "Alphabetic";
156 alphabetic_toolbarbutton.Text = "A";
158 separator_toolbarbutton.Style = ToolBarButtonStyle.Separator;
160 propertypages_toolbarbutton.Enabled = false;
161 propertypages_toolbarbutton.Style = ToolBarButtonStyle.ToggleButton;
162 propertypages_toolbarbutton.ToolTipText = "Property Pages";
163 propertypages_toolbarbutton.Text = "P";
166 reset_menuitem = context_menu.MenuItems.Add("Reset");
167 reset_menuitem.Click +=new EventHandler(OnResetPropertyClick);
168 context_menu.MenuItems.Add("-");
169 description_menuitem = context_menu.MenuItems.Add("Description");
170 description_menuitem.Click += new EventHandler(OnDescriptionClick);
171 description_menuitem.Checked = this.HelpVisible;
172 this.ContextMenu = context_menu;
173 toolbar.ContextMenu = context_menu;
175 this.Controls.Add(property_grid_view);
176 this.Controls.Add(toolbar);
177 this.Controls.Add(splitter);
178 this.Controls.Add(help_panel);
179 this.Name = "PropertyGrid";
180 this.Size = new System.Drawing.Size(256, 400);
182 selected_objects = new object[1];
183 grid_items = new GridItemCollection();
187 //TextChanged+=new System.EventHandler(RedrawEvent);
188 //ForeColorChanged+=new EventHandler(RedrawEvent);
189 //BackColorChanged+=new System.EventHandler(RedrawEvent);
190 //FontChanged+=new EventHandler(RedrawEvent);
191 //SizeChanged+=new EventHandler(RedrawEvent);
194 SetStyle(ControlStyles.UserPaint, true);
195 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
196 SetStyle(ControlStyles.ResizeRedraw, true);
197 SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
199 #endregion // Constructors
201 #region Public Instance Properties
203 [BrowsableAttribute(false)]
204 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
205 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
206 public AttributeCollection BrowsableAttributes
209 return browsable_attributes;
213 if (browsable_attributes == value) {
217 browsable_attributes = value;
221 public override Color BackColor
224 return base.BackColor;
228 if (base.BackColor == value) {
231 base.BackColor = value;
236 [BrowsableAttribute(false)]
237 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
238 public virtual bool CanShowCommands
241 return can_show_commands;
245 public Color CommandsBackColor
248 return commands_back_color;
252 if (commands_back_color == value) {
255 commands_back_color = value;
259 public Color CommandsForeColor
262 return commands_fore_color;
266 if (commands_fore_color == value) {
269 commands_fore_color = value;
273 [BrowsableAttribute(false)]
274 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
275 public virtual bool CommandsVisible
278 return commands_visible;
282 [DefaultValue(false)]
283 public virtual bool CommandsVisibleIfAvailable
286 return commands_visible_if_available;
290 if (commands_visible_if_available == value) {
293 commands_visible_if_available = value;
297 [BrowsableAttribute(false)]
298 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
299 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
300 public Point ContextMenuDefaultLocation
303 return context_menu_default_location;
307 public Color HelpBackColor
311 return help_panel.BackColor;
316 if (help_panel.BackColor == value) {
320 help_panel.BackColor = value;
324 public Color HelpForeColor
327 return help_panel.ForeColor;
331 if (help_panel.ForeColor == value) {
335 help_panel.ForeColor = value;
341 public virtual bool HelpVisible
344 return help_panel.Visible;
348 if (help_panel.Visible == value) {
352 help_panel.Visible = value;
356 public bool LargeButtons
359 return large_buttons;
363 if (large_buttons == value) {
367 large_buttons = value;
371 public Color LineColor
378 if (line_color == value) {
386 [DefaultValue(PropertySort.CategorizedAlphabetical)]
387 public PropertySort PropertySort
390 return property_sort;
394 if (!Enum.IsDefined (typeof (PropertySort), value)) {
395 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for PropertySort", value));
398 if (property_sort == value) {
402 property_sort = value;
406 [BrowsableAttribute(false)]
407 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
408 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
409 public PropertyTabCollection PropertyTabs
412 return property_tabs;
416 [BrowsableAttribute(false)]
417 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
418 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
419 public GridItem SelectedGridItem
422 return selected_grid_item;
426 if (selected_grid_item == value) {
430 selected_grid_item = value;
431 this.help_title_label.Text = selected_grid_item.PropertyDescriptor.Name;
432 this.help_description_label.Text = selected_grid_item.PropertyDescriptor.Description;
433 property_grid_view.Redraw();
439 [TypeConverter("System.Windows.Forms.PropertyGrid+SelectedObjectConverter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
440 public object SelectedObject
443 return selected_objects[0];
447 selected_objects = new object[] {value};
449 property_grid_view.Redraw();
453 [BrowsableAttribute(false)]
454 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
455 public object[] SelectedObjects
458 return selected_objects;
462 selected_objects = value;
467 [BrowsableAttribute(false)]
468 [EditorBrowsable(EditorBrowsableState.Advanced)]
469 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
470 public PropertyTab SelectedTab
477 public override ISite Site
490 public virtual bool ToolbarVisible
493 return toolbar.Visible;
497 if (toolbar.Visible == value) {
501 toolbar.Visible = value;
505 public Color ViewBackColor
508 return property_grid_view.BackColor;
512 if (property_grid_view.BackColor == value) {
516 property_grid_view.BackColor = value;
520 public Color ViewForeColor
523 return property_grid_view.ForeColor;
527 if (property_grid_view.ForeColor == value) {
531 property_grid_view.ForeColor = value;
535 #endregion // Public Instance Properties
537 #region Protected Instance Properties
539 protected override Size DefaultSize
542 return base.DefaultSize;
549 [EditorBrowsable(EditorBrowsableState.Advanced)]
550 protected virtual Type DefaultTabType
553 throw new NotImplementedException();
557 protected override bool ShowFocusCues
560 return base.ShowFocusCues;
564 #endregion // Protected Instance Properties
566 #region Public Instance Methods
568 public void CollapseAllGridItems()
570 throw new NotImplementedException();
574 public void ExpandAllGridItems()
576 throw new NotImplementedException();
579 public override void Refresh()
586 public void RefreshTabs(PropertyTabScope tabScope)
588 throw new NotImplementedException();
592 public void ResetSelectedProperty()
594 throw new NotImplementedException();
596 #endregion // Public Instance Methods
598 #region Protected Instance Methods
600 protected virtual PropertyTab CreatePropertyTab(Type tabType)
602 throw new NotImplementedException();
605 protected override void OnFontChanged(EventArgs e)
607 base.OnFontChanged (e);
610 protected override void OnGotFocus(EventArgs e)
615 protected override void OnHandleCreated(EventArgs e)
617 base.OnHandleCreated (e);
620 protected override void OnHandleDestroyed(EventArgs e)
622 base.OnHandleDestroyed (e);
626 protected override void OnMouseDown(MouseEventArgs e)
628 base.OnMouseDown (e);
631 protected override void OnMouseMove(MouseEventArgs e)
633 base.OnMouseMove (e);
636 protected override void OnMouseUp(MouseEventArgs e)
641 protected override void OnPaint(PaintEventArgs pevent)
643 base.OnPaint (pevent);
647 protected virtual void OnPropertyTabChanged(PropertyTabChangedEventArgs e)
649 throw new NotImplementedException();
653 protected virtual void OnPropertyValueChanged(PropertyValueChangedEventArgs e)
655 throw new NotImplementedException();
658 protected override void OnResize(EventArgs e)
664 protected virtual void OnSelectedGridItemChanged(SelectedGridItemChangedEventArgs e)
666 throw new NotImplementedException();
670 protected virtual void OnSelectedObjectsChanged(EventArgs e)
672 throw new NotImplementedException();
675 protected override void OnSystemColorsChanged(EventArgs e)
677 base.OnSystemColorsChanged (e);
680 protected override void OnVisibleChanged(EventArgs e)
682 base.OnVisibleChanged (e);
685 protected override bool ProcessDialogKey(Keys keyData)
687 return base.ProcessDialogKey (keyData);
690 protected override void ScaleCore(float dx, float dy)
692 base.ScaleCore (dx, dy);
695 protected override void WndProc(ref Message m)
697 base.WndProc (ref m);
702 public event EventHandler PropertySortChanged;
703 public event PropertyTabChangedEventHandler PropertyTabChanged;
704 public event PropertyValueChangedEventHandler PropertyValueChanged;
705 public event SelectedGridItemChangedEventHandler SelectedGridItemChanged;
706 public event EventHandler SelectedObjectsChanged;
709 #region Com2Interop.IComPropertyBrowser Interface
711 bool ComponentModel.Com2Interop.IComPropertyBrowser.InPropertySet {
713 throw new NotImplementedException();
718 void ComponentModel.Com2Interop.IComPropertyBrowser.DropDownDone() {
719 throw new NotImplementedException();
723 bool ComponentModel.Com2Interop.IComPropertyBrowser.EnsurePendingChangesCommitted() {
724 throw new NotImplementedException();
728 void ComponentModel.Com2Interop.IComPropertyBrowser.HandleF4() {
729 throw new NotImplementedException();
733 void ComponentModel.Com2Interop.IComPropertyBrowser.LoadState(Microsoft.Win32.RegistryKey key) {
734 throw new NotImplementedException();
738 void ComponentModel.Com2Interop.IComPropertyBrowser.SaveState(Microsoft.Win32.RegistryKey key) {
739 throw new NotImplementedException();
743 private event ComponentRenameEventHandler com_component_name_changed;
744 event ComponentRenameEventHandler ComponentModel.Com2Interop.IComPropertyBrowser.ComComponentNameChanged {
745 add { com_component_name_changed += value; }
746 remove { com_component_name_changed -= value; }
748 #endregion // Com2Interop.IComPropertyBrowser Interface
750 #region PropertyTabCollection Class
751 public class PropertyTabCollection : ICollection, IEnumerable
753 #region Private Constructors
754 private PropertyTabCollection() {
757 #endregion // Private Constructors
760 public PropertyTab this[int index]
763 throw new NotImplementedException();
767 #region ICollection Members
769 bool ICollection.IsSynchronized
772 // TODO: Add PropertyTabCollection.IsSynchronized getter implementation
778 void ICollection.CopyTo(Array array, int index)
780 // TODO: Add PropertyTabCollection.CopyTo implementation
784 object ICollection.SyncRoot
787 // TODO: Add PropertyTabCollection.SyncRoot getter implementation
794 #region IEnumerable Members
796 public IEnumerator GetEnumerator()
798 // TODO: Add PropertyTabCollection.GetEnumerator implementation
804 #region ICollection Members
809 // TODO: Add PropertyTabCollection.Count getter implementation
816 #endregion // PropertyTabCollection Class
819 #region Private Helper Methods
821 private void toolbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
823 if (e.Button == alphabetic_toolbarbutton) {
824 this.PropertySort = PropertySort.Alphabetical;
825 } else if (e.Button == categorized_toolbarbutton) {
826 this.PropertySort = PropertySort.Categorized;
830 internal void UpdateToolBarButtons()
832 if (PropertySort == PropertySort.Alphabetical) {
833 categorized_toolbarbutton.Pushed = false;
834 alphabetic_toolbarbutton.Pushed = true;
835 } else if (PropertySort == PropertySort.Categorized) {
836 categorized_toolbarbutton.Pushed = true;
837 alphabetic_toolbarbutton.Pushed = false;
839 categorized_toolbarbutton.Pushed = false;
840 alphabetic_toolbarbutton.Pushed = false;
844 private void OnResetPropertyClick(object sender, EventArgs e)
846 ResetSelectedProperty();
849 private void OnDescriptionClick(object sender, EventArgs e)
851 this.HelpVisible = !this.HelpVisible;
852 description_menuitem.Checked = this.HelpVisible;
856 private void ReflectObjects()
858 grid_items = new GridItemCollection();
859 foreach (object obj in selected_objects) {
861 PopulateGridItemCollection(obj,grid_items);
866 private void PopulateGridItemCollection(object obj, GridItemCollection grid_item_coll)
868 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
869 for (int i = 0; i < properties.Count;i++) {
870 bool not_browseable = properties[i].Attributes.Contains(new Attribute[] {new BrowsableAttribute(false)});
871 if (!not_browseable) {
872 GridEntry grid_entry = new GridEntry(obj, properties[i]);
873 //object test = grid_item_coll["Length"];
874 grid_item_coll.Add(properties[i].Name,grid_entry);
875 //PopulateGridItemCollection(grid_entry.Value,grid_entry.GridItems);
879 #endregion // Private Helper Methods