2007-09-28 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GridEntry.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jonathan Chambers (jonathan.chambers@ansys.com)
24 //
25
26 // NOT COMPLETE
27
28 using System;
29 using System.Windows.Forms;
30 using System.ComponentModel;
31 using System.Drawing;
32
33 namespace System.Windows.Forms.PropertyGridInternal
34 {
35         internal class GridEntry : GridItem, ITypeDescriptorContext
36         {
37                 #region Local Variables
38                 private bool expanded = true;
39                 private GridItemCollection grid_items;
40                 private GridItem parent;
41                 private PropertyDescriptor property_descriptor;
42                 private object[] selected_objects;
43                 private int top;
44                 private Rectangle plus_minus_bounds;
45                 private Rectangle bounds;
46                 private PropertyGridView property_grid_view;
47                 #endregion      // Local Variables
48
49                 #region  Contructors
50                 protected GridEntry (PropertyGridView view)
51                 {
52                         property_grid_view = view;
53                         plus_minus_bounds = new Rectangle(0,0,0,0);
54                         bounds = new Rectangle(0,0,0,0);
55                         top = -1;
56                         grid_items = new GridItemCollection();
57                 }
58
59                 public GridEntry(PropertyGridView view, object[] objs, PropertyDescriptor prop_desc) : this (view) {
60                         selected_objects = objs;
61                         property_descriptor = prop_desc;
62                 }
63                 #endregion      // Constructors
64
65                 #region Public Instance Properties
66                 public override bool Expandable
67                 {
68                         get {
69                                 return grid_items.Count > 0;
70                         }
71                 }
72
73                 public override bool Expanded
74                 {
75                         get {
76                                 return expanded;
77                         }
78
79                         set {
80                                 if (expanded == value)
81                                         return;
82
83                                 expanded = value;
84                                 property_grid_view.RedrawBelowItemOnExpansion (this);
85                         }
86                 }
87
88                 public override GridItemCollection GridItems
89                 {
90                         get {
91                                 return grid_items;
92                         }
93                 }
94
95                 public override GridItemType GridItemType
96                 {
97                         get {
98                                 return GridItemType.Property;
99                         }
100                 }
101
102                 public override string Label
103                 {
104                         get {
105                                 return property_descriptor.Name;
106                         }
107                 }
108
109                 public override GridItem Parent
110                 {
111                         get {
112                                 return parent;
113                         }
114                 }
115
116                 public override PropertyDescriptor PropertyDescriptor
117                 {
118                         get {
119                                 return property_descriptor;
120                         }
121                 }
122
123                 public bool CanResetValue ()
124                 {
125                         if (Value == null) /* not sure if this is always right.. */
126                                 return false;
127
128                         return PropertyDescriptor.CanResetValue(selected_objects[0]);
129                 }
130
131                 public override object Value
132                 {
133                         get {
134                                 /* we should probably put this logic
135                                  * someplace else, maybe when we're
136                                  * initially populating the
137                                  * PropertyGrid? */
138                                 if (selected_objects == null || selected_objects.Length == 0 || property_descriptor == null)
139                                         return null;
140
141                                 object v = property_descriptor.GetValue(selected_objects[0]);
142                                 for (int i = 1; i < selected_objects.Length; i ++) {
143                                         if (!Object.Equals (v, property_descriptor.GetValue(selected_objects[i])))
144                                                 return null;
145                                 }
146
147                                 return v;
148                         }
149                 }
150                 #endregion      // Public Instance Properties
151
152                 #region Public Instance Methods
153                 [MonoTODO]
154                 public override bool Select ()
155                 {
156                         property_grid_view.property_grid.SelectedGridItem = this;
157                         return true;
158                 }
159                 #endregion      // Public Instance Methods
160
161                 #region ITypeDescriptorContext Members
162
163                 void ITypeDescriptorContext.OnComponentChanged() {
164                         // TODO:  Add SystemComp.OnComponentChanged implementation
165                 }
166
167                 [MonoTODO ("this is broken, as PropertyGridView doesn't implement IContainer")]
168                 IContainer ITypeDescriptorContext.Container {
169                         get {
170                                 return property_grid_view as IContainer;
171                         }
172                 }
173
174                 bool ITypeDescriptorContext.OnComponentChanging() {
175                         // TODO:  Add SystemComp.OnComponentChanging implementation
176                         return false;
177                 }
178
179                 object ITypeDescriptorContext.Instance {
180                         get {
181                                 return Value;
182                         }
183                 }
184
185                 PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
186                         get {
187                                 return PropertyDescriptor;
188                         }
189                 }
190
191                 #endregion
192
193                 #region IServiceProvider Members
194
195                 object IServiceProvider.GetService(Type serviceType) {
196                         IComponent selectedComponent = property_grid_view.property_grid.SelectedObject as IComponent;
197                         if (selectedComponent != null && selectedComponent.Site != null)
198                                 return selectedComponent.Site.GetService (serviceType);
199                         return null;
200                 }
201
202                 #endregion
203
204                 internal object[] SelectedObjects {
205                         get {
206                                 return selected_objects;
207                         }
208                         set {
209                                 selected_objects = value;
210                         }
211                 }
212
213                 internal override int Top {
214                         get {
215                                 return top;
216                         }
217                         set {
218                                 if (top == value)
219                                         return;
220
221                                 top = value;
222                                 if (property_grid_view.property_grid.SelectedGridItem == this)
223                                         property_grid_view.grid_textbox_Show (this);
224                         }
225                 }
226
227                 internal override Rectangle PlusMinusBounds {
228                         get{
229                                 return plus_minus_bounds;
230                         }
231                         set{
232                                 plus_minus_bounds = value;
233                         }
234                 }
235
236                 internal override Rectangle Bounds
237                 {
238                         get
239                         {
240                                 return bounds;
241                         }
242                         set
243                         {
244                                 if (bounds == value)
245                                         return;
246
247                                 bounds = value;
248                                 if (property_grid_view.property_grid.SelectedGridItem == this)
249                                         property_grid_view.grid_textbox_Show (this);
250                         }
251                 }
252
253                 internal void SetParent (GridItem parent)
254                 {
255                         this.parent = parent;
256                 }
257         }
258 }