2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataGridColumn.cs
1 //
2 // System.Web.UI.WebControls.DataGridColumn.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Web;
35 using System.Web.UI;
36 using System.ComponentModel;
37
38 namespace System.Web.UI.WebControls
39 {
40         [TypeConverter(typeof(ExpandableObjectConverter))]
41         public abstract class DataGridColumn : IStateManager
42         {
43                 private StateBag viewState;
44                 private bool     marked;
45                 private TableItemStyle footerStyle;
46                 private TableItemStyle headerStyle;
47                 private TableItemStyle itemStyle;
48
49                 private DataGrid owner;
50                 private bool     designMode;
51
52                 public DataGridColumn()
53                 {
54                         viewState = new StateBag();
55                 }
56
57                 internal TableItemStyle FooterStyleInternal
58                 {
59                         get
60                         {
61                                 return footerStyle;
62                         }
63                 }
64
65                 internal TableItemStyle HeaderStyleInternal
66                 {
67                         get
68                         {
69                                 return headerStyle;
70                         }
71                 }
72
73                 internal TableItemStyle ItemStyleInternal
74                 {
75                         get
76                         {
77                                 return itemStyle;
78                         }
79                 }
80
81                 [DefaultValue (null), WebCategory ("Misc")]
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
83                 [PersistenceMode (PersistenceMode.InnerProperty)]
84                 [WebSysDescription ("The style applied to the footer of this column.")]
85                 public virtual TableItemStyle FooterStyle
86                 {
87                         get
88                         {
89                                 if(footerStyle == null)
90                                 {
91                                         footerStyle = new TableItemStyle();
92                                         if(IsTrackingViewState)
93                                         {
94                                                 footerStyle.TrackViewState();
95                                         }
96                                 }
97                                 return footerStyle;
98                         }
99                 }
100
101                 [DefaultValue (null), WebCategory ("Misc")]
102                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
103                 [PersistenceMode (PersistenceMode.InnerProperty)]
104                 [WebSysDescription ("The style applied to the header of this column.")]
105                 public virtual TableItemStyle HeaderStyle
106                 {
107                         get
108                         {
109                                 if(headerStyle == null)
110                                 {
111                                         headerStyle= new TableItemStyle();
112                                         if(IsTrackingViewState)
113                                         {
114                                                 headerStyle.TrackViewState();
115                                         }
116                                 }
117                                 return headerStyle;
118                         }
119                 }
120
121                 [DefaultValue (null), WebCategory ("Misc")]
122                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
123                 [PersistenceMode (PersistenceMode.InnerProperty)]
124                 [WebSysDescription ("The style applied to the rows of this column.")]
125                 public virtual TableItemStyle ItemStyle
126                 {
127                         get
128                         {
129                                 if(itemStyle == null)
130                                 {
131                                         itemStyle = new TableItemStyle();
132                                         if(IsTrackingViewState)
133                                         {
134                                                 itemStyle.TrackViewState();
135                                         }
136                                 }
137                                 return itemStyle;
138                         }
139                 }
140
141                 [DefaultValue (""), WebCategory ("Misc")]
142                 [WebSysDescription ("The text within the footer of this column.")]
143                 public virtual string FooterText
144                 {
145                         get
146                         {
147                                 object o = ViewState["FooterText"];
148                                 if(o != null)
149                                 {
150                                         return (string)o;
151                                 }
152                                 return String.Empty;
153                         }
154                         set
155                         {
156                                 ViewState["FooterText"] = value;
157                                 OnColumnChanged();
158                         }
159                 }
160
161                 [DefaultValue (""), WebCategory ("Misc")]
162                 [WebSysDescription ("The URL to an image that is displayed in the header of this column.")]
163                 public virtual string HeaderImageUrl
164                 {
165                         get
166                         {
167                                 object o = ViewState["HeaderImageUrl"];
168                                 if(o != null)
169                                 {
170                                         return (string)o;
171                                 }
172                                 return String.Empty;
173                         }
174                         set
175                         {
176                                 ViewState["HeaderImageUrl"] = value;
177                                 OnColumnChanged();
178                         }
179                 }
180
181                 [DefaultValue (""), WebCategory ("Misc")]
182                 [WebSysDescription ("The text within the header of this column.")]
183                 public virtual string HeaderText
184                 {
185                         get
186                         {
187                                 object o = ViewState["HeaderText"];
188                                 if(o != null)
189                                 {
190                                         return (string)o;
191                                 }
192                                 return String.Empty;
193                         }
194                         set
195                         {
196                                 ViewState["HeaderText"] = value;
197                                 OnColumnChanged();
198                         }
199                 }
200
201                 [DefaultValue (""), WebCategory ("Misc")]
202                 [WebSysDescription ("An expression that determines how the colum should be sorted.")]
203                 public virtual string SortExpression
204                 {
205                         get
206                         {
207                                 object o = ViewState["SortExpression"];
208                                 if(o != null)
209                                 {
210                                         return (string)o;
211                                 }
212                                 return String.Empty;
213                         }
214                         set
215                         {
216                                 ViewState["SortExpression"] = value;
217                                 OnColumnChanged();
218                         }
219                 }
220
221                 [DefaultValue (true), WebCategory ("Misc")]
222                 [WebSysDescription ("The visibility of this column.")]
223                 public bool Visible
224                 {
225                         get
226                         {
227                                 object o = ViewState["Visible"];
228                                 if(o != null)
229                                 {
230                                         return (bool)o;
231                                 }
232                                 return true;
233                         }
234                         set
235                         {
236                                 ViewState["Visible"] = value;
237                                 OnColumnChanged();
238                         }
239                 }
240
241                 public virtual void Initialize()
242                 {
243                         if(owner != null && owner.Site != null)
244                         {
245                                 designMode = owner.Site.DesignMode;
246                         }
247                 }
248
249                 public virtual void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
250                 {
251                         switch(itemType)
252                         {
253                                 case ListItemType.Header : InitializeCellHeader(cell, columnIndex);
254                                                            break;
255                                 case ListItemType.Footer : InitializeCellFooter(cell, columnIndex);
256                                                            break;
257                                 default                  : if (ItemStyleInternal != null)
258                                                                 cell.ApplyStyle (ItemStyleInternal);
259                                                            return;
260                         }
261                 }
262
263                 private void InitializeCellHeader(TableCell cell, int columnIndex)
264                 {
265                         WebControl ctrl = null;
266                         bool       sort = true;
267                         string     sortExpr = "";
268                         ImageButton headButton;
269                         Image       headImage;
270                         LinkButtonInternal link;
271
272                         if(owner != null)
273                         {
274                                 sort = owner.AllowSorting;
275                         }
276                         if(sort)
277                         {
278                                 sortExpr = SortExpression;
279                                 if(sortExpr.Length == 0)
280                                 {
281                                         sort = false;
282                                 }
283                         }
284                         if(HeaderImageUrl.Length > 0)
285                         {
286                                 if(sort)
287                                 {
288                                         headButton = new ImageButton();
289                                         headButton.ImageUrl = HeaderImageUrl;
290                                         headButton.CommandName = "Sort";
291                                         headButton.CommandArgument = sortExpr;
292                                         headButton.CausesValidation = false;
293                                         ctrl = headButton;
294                                 } else
295                                 {
296                                         headImage = new Image();
297                                         headImage.ImageUrl = HeaderImageUrl;
298                                         ctrl = headImage;
299                                 }
300                         } else
301                         {
302                                 if(sort)
303                                 {
304                                         link = new LinkButtonInternal();
305                                         link.Text = HeaderText;
306                                         link.CommandName = "Sort";
307                                         link.CommandArgument = sortExpr;
308                                         link.CausesValidation = false;
309                                         ctrl = link;
310                                 } else
311                                 {
312                                         if(HeaderText.Length > 0)
313                                         {
314                                                 cell.Text = HeaderText;
315                                         } else
316                                         {
317                                                 cell.Text = "&nbsp;";
318                                         }
319                                 }
320                         }
321                         if(ctrl != null)
322                         {
323                                 cell.Controls.Add(ctrl);
324                         }
325                         if (HeaderStyleInternal != null)
326                                 cell.ApplyStyle (HeaderStyleInternal);
327                 }
328
329                 private void InitializeCellFooter(TableCell cell, int columnIndex)
330                 {
331                         cell.Text = (FooterText.Length > 0 ? FooterText : "&nbsp;");
332                         if (FooterStyleInternal != null)
333                                 cell.ApplyStyle (FooterStyleInternal);
334                 }
335
336                 public override string ToString()
337                 {
338                         return String.Empty;
339                 }
340
341                 protected bool DesignMode
342                 {
343                         get
344                         {
345                                 return designMode;
346                         }
347                 }
348
349                 protected DataGrid Owner
350                 {
351                         get
352                         {
353                                 return owner;
354                         }
355                 }
356
357                 protected StateBag ViewState
358                 {
359                         get
360                         {
361                                 return viewState;
362                         }
363                 }
364
365                 /// <summary>
366                 /// Undocumented
367                 /// </summary>
368                 protected virtual void OnColumnChanged()
369                 {
370                         if(owner != null)
371                         {
372                                 owner.OnColumnsChanged();
373                         }
374                 }
375
376                 internal void SetOwner (DataGrid datagrid)
377                 {
378                         owner = datagrid;
379                 }
380                 
381                 protected virtual object SaveViewState()
382                 {
383                         object[] states = new object[4];
384                         states[0] = ViewState.SaveViewState();
385                         states[1] = (footerStyle == null ? null : footerStyle.SaveViewState());
386                         states[2] = (headerStyle == null ? null : headerStyle.SaveViewState());
387                         states[3] = (itemStyle == null ? null : itemStyle.SaveViewState());
388                         
389                         for (int i = 0; i < states.Length; i++) {
390                                 if (states [i] != null)
391                                         return states;
392                         }
393                         return null;
394                 }
395
396                 protected virtual void LoadViewState(object savedState)
397                 {
398                         if (savedState == null)
399                                 return;
400
401                         object[] states = (object[]) savedState;
402                         ViewState.LoadViewState (states [0]);
403                         FooterStyle.LoadViewState (states [1]);
404                         HeaderStyle.LoadViewState (states [2]);
405                         ItemStyle.LoadViewState (states [3]);
406                 }
407
408                 protected virtual void TrackViewState()
409                 {
410                         marked = true;
411                         ViewState.TrackViewState();
412                         if(footerStyle != null)
413                         {
414                                 footerStyle.TrackViewState();
415                         }
416                         if(headerStyle != null)
417                         {
418                                 headerStyle.TrackViewState();
419                         }
420                         if(itemStyle != null)
421                         {
422                                 itemStyle.TrackViewState();
423                         }
424                 }
425
426                 protected bool IsTrackingViewState
427                 {
428                         get
429                         {
430                                 return marked;
431                         }
432                 }
433
434                 void IStateManager.LoadViewState(object savedState)
435                 {
436                         LoadViewState(savedState);
437                 }
438
439                 object IStateManager.SaveViewState()
440                 {
441                         return SaveViewState();
442                 }
443
444                 void IStateManager.TrackViewState()
445                 {
446                         TrackViewState();
447                 }
448
449                 bool IStateManager.IsTrackingViewState
450                 {
451                         get
452                         {
453                                 return IsTrackingViewState;
454                         }
455                 }
456         }
457 }