2010-04-26 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewBand.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 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26
27 #if NET_2_0
28
29 using System.ComponentModel;
30
31 namespace System.Windows.Forms {
32
33         public class DataGridViewBand : DataGridViewElement, ICloneable, IDisposable {
34
35                 private ContextMenuStrip contextMenuStrip = null;
36                 private DataGridViewCellStyle defaultCellStyle;
37                 private Type defaultHeaderCellType;
38                 private bool displayed;
39                 private bool frozen = false;
40                 private int index = -1;
41                 private bool readOnly = false;
42                 private DataGridViewTriState resizable = DataGridViewTriState.NotSet;
43                 private bool selected = false;
44                 private object tag = null;
45                 private bool visible = true;
46                 private DataGridViewHeaderCell headerCellCore;
47                 private bool isRow;
48                 private DataGridViewCellStyle inheritedStyle = null;
49
50                 internal DataGridViewBand ()
51                 {
52                         defaultHeaderCellType = typeof (DataGridViewHeaderCell);
53                         isRow = this is DataGridViewRow;
54                 }
55
56                 ~DataGridViewBand ()
57                 {
58                         Dispose();
59                 }
60
61                 [DefaultValue (null)]
62                 public virtual ContextMenuStrip ContextMenuStrip {
63                         get { return contextMenuStrip; }
64                         set { contextMenuStrip = value; }
65                 }
66
67                 [Browsable (false)]
68                 public virtual DataGridViewCellStyle DefaultCellStyle {
69                         get {
70                                 if (defaultCellStyle == null) {
71                                         defaultCellStyle = new DataGridViewCellStyle();
72                                 }
73                                 return defaultCellStyle;
74                         }
75                         set { defaultCellStyle = value; }
76                 }
77
78                 [Browsable (false)]
79                 public Type DefaultHeaderCellType {
80                         get { return defaultHeaderCellType; }
81                         set {
82                                 if (!value.IsSubclassOf(typeof(DataGridViewHeaderCell))) {
83                                         throw new ArgumentException("Type is not DataGridViewHeaderCell or a derived type.");
84                                 }
85                                 defaultHeaderCellType = value;
86                         }
87                 }
88
89                 [Browsable (false)]
90                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91                 public virtual bool Displayed {
92                         get { return displayed; }
93                 }
94
95                 [DefaultValue (false)]
96                 public virtual bool Frozen {
97                         get { return frozen; }
98                         set {
99                                 if (frozen != value) {
100                                         frozen = value;
101                                         if (frozen)
102                                                 SetState (State | DataGridViewElementStates.Frozen);
103                                         else
104                                                 SetState (State & ~DataGridViewElementStates.Frozen);
105                                 }
106                         }
107                 }
108
109                 [Browsable (false)]
110                 public bool HasDefaultCellStyle {
111                         get { return (defaultCellStyle != null); }
112                 }
113
114                 [Browsable (false)]
115                 public int Index {
116                         get { return index; }
117                 }
118
119                 [Browsable (false)]
120                 public virtual DataGridViewCellStyle InheritedStyle {
121                         get { return inheritedStyle; }
122                 }
123
124                 [DefaultValue (false)]
125                 public virtual bool ReadOnly {
126                         get { return readOnly; }
127                         set {
128                                 if (readOnly != value) {
129                                         readOnly = value;
130                                         if (readOnly)
131                                                 SetState (State | DataGridViewElementStates.ReadOnly);
132                                         else
133                                                 SetState (State & ~DataGridViewElementStates.ReadOnly);
134                                 }
135                         }
136                 }
137
138                 [Browsable (true)]
139                 public virtual DataGridViewTriState Resizable {
140                         get { 
141                                 if (resizable == DataGridViewTriState.NotSet && DataGridView != null) {
142                                         return DataGridView.AllowUserToResizeColumns ? DataGridViewTriState.True : DataGridViewTriState.False;
143                                 }
144                                 return resizable; }
145                         set {
146                                 if (value != resizable) {
147                                         resizable = value;
148                                         if (resizable == DataGridViewTriState.True)
149                                                 SetState (State | DataGridViewElementStates.Resizable);
150                                         else
151                                                 SetState (State & ~DataGridViewElementStates.Resizable);
152                                 }
153                         }
154                 }
155
156                 [Browsable (false)]
157                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158                 public virtual bool Selected {
159                         get { return selected; }
160                         set {
161                                 if (DataGridView == null) {
162                                         throw new InvalidOperationException("Cant select a row non associated with a DataGridView.");
163                                 }
164                                 if (isRow) {
165                                         DataGridView.SetSelectedRowCoreInternal (Index, value);
166                                 } else {
167                                         DataGridView.SetSelectedColumnCoreInternal (Index, value);
168                                 }
169                         }
170                 }
171                 
172                 internal bool SelectedInternal {
173                         get {
174                                 return selected;
175                         }
176                         set {
177                                 if (selected != value) {
178                                         selected = value;
179
180                                         if (selected)
181                                                 SetState (State | DataGridViewElementStates.Selected);
182                                         else
183                                                 SetState (State & ~DataGridViewElementStates.Selected);
184                                 }
185                         }
186                 }
187
188                 internal bool DisplayedInternal {
189                         get { return displayed; }
190                         set {
191                                 if (value != displayed) {
192                                         displayed = value;
193                                         if (displayed)
194                                                 SetState (State | DataGridViewElementStates.Displayed);
195                                         else
196                                                 SetState (State & ~DataGridViewElementStates.Displayed);
197                                 }
198                         }
199                 }
200                 
201                 [Browsable (false)]
202                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
203                 public object Tag {
204                         get { return tag; }
205                         set { tag = value; }
206                 }
207
208                 [DefaultValue (true)]
209                 public virtual bool Visible {
210                         get { return visible; }
211                         set {
212                                 if (visible != value) {
213                                         visible = value;
214                                         if (visible)
215                                                 SetState (State | DataGridViewElementStates.Visible);
216                                         else
217                                                 SetState (State & ~DataGridViewElementStates.Visible);
218                                 }
219                         }
220                 }
221
222                 public virtual object Clone ()
223                 {
224                         DataGridViewBand result = new DataGridViewBand();
225                         //////////////////////////////
226                         return result;
227                 }
228
229                 //public sealed void Dispose () {
230                 public void Dispose ()
231                 {
232                 }
233
234                 public override string ToString ()
235                 {
236                         return this.GetType().Name + ": " + index.ToString() + ".";
237                 }
238
239                 [Browsable (false)]
240                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
241                 protected DataGridViewHeaderCell HeaderCellCore {
242                         get { return headerCellCore; }
243                         set { headerCellCore = value; }
244                 }
245
246                 protected bool IsRow {
247                         get { return isRow; }
248                 }
249
250                 protected virtual void Dispose (bool disposing)
251                 {
252                 }
253
254                 protected override void OnDataGridViewChanged ()
255                 {
256                 }
257
258                 internal virtual void SetIndex (int index)
259                 {
260                         this.index = index;
261                 }
262
263         }
264
265 }
266
267 #endif