New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataGridColumn.cs
1 //
2 // System.Web.UI.WebControls.DataGridColumn.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [TypeConverter (typeof (System.ComponentModel.ExpandableObjectConverter))]
40         public abstract class DataGridColumn : IStateManager
41         {
42                 DataGrid owner;
43                 StateBag viewstate;
44                 bool tracking_viewstate;
45                 bool design;
46
47                 TableItemStyle footer_style;
48                 TableItemStyle header_style;
49                 TableItemStyle item_style;
50                 
51 #if NET_2_0
52                 protected
53 #else
54                 public
55 #endif
56                 DataGridColumn ()
57                 {
58                         viewstate = new StateBag ();
59                 }
60                 
61                 [DefaultValue (null)]
62                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
63                 [PersistenceMode (PersistenceMode.InnerProperty)]
64                 [WebSysDescription ("")]
65                 [WebCategory ("Misc")]
66                 public virtual TableItemStyle FooterStyle 
67                 {
68                         get {
69                                 if (footer_style == null) {
70                                         footer_style = new TableItemStyle ();
71
72                                         if (tracking_viewstate) {
73                                                 footer_style.TrackViewState ();
74                                         }
75                                 }
76
77                                 return (footer_style);
78                         }
79                 }
80                 
81                 [DefaultValue ("")]
82                 [WebSysDescription ("")]
83                 [WebCategory ("Misc")]
84                 public virtual string FooterText 
85                 {
86                         get {
87                                 return (viewstate.GetString ("FooterText",
88                                                              String.Empty));
89                         }
90                         set {
91                                 viewstate["FooterText"] = value;
92                         }
93                 }
94
95                 [DefaultValue ("")]
96                 [WebSysDescription ("")]
97                 [WebCategory ("Misc")]
98 #if NET_2_0
99                 [UrlProperty]
100 #endif
101                 public virtual string HeaderImageUrl
102                 {
103                         get {
104                                 return (viewstate.GetString ("HeaderImageUrl",
105                                                              String.Empty));
106                         }
107                         set {
108                                 viewstate["HeaderImageUrl"] = value;
109                         }
110                 }
111                 
112                 [DefaultValue (null)]
113                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
114                 [PersistenceMode (PersistenceMode.InnerProperty)]
115                 [WebSysDescription ("")]
116                 [WebCategory ("Misc")]
117                 public virtual TableItemStyle HeaderStyle 
118                 {
119                         get {
120                                 if (header_style == null) {
121                                         header_style = new TableItemStyle ();
122
123                                         if (tracking_viewstate) {
124                                                 header_style.TrackViewState ();
125                                         }
126                                 }
127
128                                 return (header_style);
129                         }
130                 }
131
132                 [DefaultValue ("")]
133                 [WebSysDescription ("")]
134                 [WebCategory ("Misc")]
135                 public virtual string HeaderText 
136                 {
137                         get {
138                                 return (viewstate.GetString ("HeaderText",
139                                                              String.Empty));
140                         }
141                         set {
142                                 viewstate["HeaderText"] = value;
143                         }
144                 }
145                 
146                 [DefaultValue (null)]
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
148                 [PersistenceMode (PersistenceMode.InnerProperty)]
149                 [WebSysDescription ("")]
150                 [WebCategory ("Misc")]
151                 public virtual TableItemStyle ItemStyle 
152                 {
153                         get {
154                                 if (item_style == null) {
155                                         item_style = new TableItemStyle ();
156
157                                         if (tracking_viewstate) {
158                                                 item_style.TrackViewState ();
159                                         }
160                                 }
161
162                                 return (item_style);
163                         }
164                 }
165                 
166                 [DefaultValue ("")]
167                 [WebSysDescription ("")]
168                 [WebCategory ("Misc")]
169                 public virtual string SortExpression 
170                 {
171                         get {
172                                 return (viewstate.GetString ("SortExpression",
173                                                              String.Empty));
174                         }
175                         set {
176                                 viewstate["SortExpression"] = value;
177                         }
178                 }
179
180                 [DefaultValue (true)]
181                 [WebSysDescription ("")]
182                 [WebCategory ("Misc")]
183                 public bool Visible 
184                 {
185                         get {
186                                 return (viewstate.GetBool ("Visible",
187                                                            true));
188                         }
189                         set {
190                                 viewstate["Visible"] = value;
191                         }
192                 }
193
194                 public virtual void Initialize ()
195                 {
196                         if (owner != null && owner.Site != null) {
197                                 design = owner.Site.DesignMode;
198                         }
199                 }
200
201                 internal class ForeColorLinkButton : LinkButton {
202                         Color GetForeColor (WebControl control)
203                         {
204                                 if (control == null)
205                                         return Color.Empty;
206
207                                 // don't go beyond the container table.
208                                 if (control is Table)
209                                         return control.ControlStyle.ForeColor;
210                                 
211                                 Color color = control.ControlStyle.ForeColor;
212                                 if (color != Color.Empty)
213                                         return color;
214
215                                 return GetForeColor ((WebControl) control.Parent);
216                         }
217
218 #if NET_2_0
219                         protected internal
220 #else           
221                         protected
222 #endif          
223                         override void Render (HtmlTextWriter writer)
224                         {
225                                 Color color = GetForeColor (this);
226                                 if (color != Color.Empty)
227                                         ForeColor = color;
228                                 base.Render (writer);
229                         }
230                 }
231
232                 public virtual void InitializeCell (TableCell cell,
233                                                     int columnIndex,
234                                                     ListItemType itemType)
235                 {
236                         switch (itemType) {
237                         case ListItemType.Header: 
238                         {
239                                 /* If sorting is enabled, add a
240                                  * LinkButton or an ImageButton
241                                  * (depending on HeaderImageUrl).
242                                  *
243                                  * If sorting is disabled, the
244                                  * HeaderText or an Image is displayed
245                                  * (depending on HeaderImageUrl).
246                                  *
247                                  * If neither HeaderText nor
248                                  * HeaderImageUrl is set, use &nbsp;
249                                  */
250                                 bool sort = false;
251                                 string sort_ex = SortExpression;
252                                 
253                                 if (owner != null &&
254                                     sort_ex.Length > 0) {
255                                         sort = owner.AllowSorting;
256                                 }
257                                 
258                                 string image_url = HeaderImageUrl;
259                                 if (image_url.Length > 0) {
260                                         if (sort) {
261                                                 ImageButton butt = new ImageButton ();
262
263                                                 /* Don't need to
264                                                  * resolve this, Image
265                                                  * does that when it
266                                                  * renders
267                                                  */
268                                                 butt.ImageUrl = image_url;
269                                                 butt.CommandName = "Sort";
270                                                 butt.CommandArgument = sort_ex;
271
272                                                 cell.Controls.Add (butt);
273                                         } else {
274                                                 Image image = new Image ();
275
276                                                 image.ImageUrl = image_url;
277
278                                                 cell.Controls.Add (image);
279                                         }
280                                 } else {
281                                         if (sort) {
282                                                 // This one always gets the forecolor of the header_style
283                                                 // from one of the parents, but we can't look it up at this
284                                                 // point, as it can change afterwards.
285                                                 LinkButton link = new ForeColorLinkButton ();
286
287                                                 link.Text = HeaderText;
288                                                 link.CommandName = "Sort";
289                                                 link.CommandArgument = sort_ex;
290
291                                                 cell.Controls.Add (link);
292                                         } else {
293                                                 string text = HeaderText;
294                                                 if (text.Length > 0) {
295                                                         cell.Text = text;
296                                                 } else {
297                                                         cell.Text = "&nbsp;";
298                                                 }
299                                         }
300                                 }
301                         }
302                         break;
303
304                         case ListItemType.Footer:
305                         {
306                                 /* Display FooterText or &nbsp; */
307                                 string text = FooterText;
308
309                                 if (text.Length > 0) {
310                                         cell.Text = text;
311                                 } else {
312                                         cell.Text = "&nbsp;";
313                                 }
314                         }
315                         break;
316
317                         default:
318                                 break;
319                                 
320                         }
321                         
322                 }
323
324                 public override string ToString ()
325                 {
326                         return (String.Empty);
327                 }
328
329                 protected bool DesignMode 
330                 {
331                         get {
332                                 return (design);
333                         }
334                 }
335                 
336                 protected DataGrid Owner 
337                 {
338                         get {
339                                 return (owner);
340                         }
341                 }
342
343
344                 internal TableItemStyle GetStyle (ListItemType type)
345                 {
346                         if (type == ListItemType.Header)
347                                 return header_style;
348
349                         if (type == ListItemType.Footer)
350                                 return footer_style;
351
352                         return item_style;
353                 }
354
355                 internal void Set_Owner (DataGrid value) 
356                 {
357                         owner = value;
358                 }
359                 
360                 
361                 protected StateBag ViewState 
362                 {
363                         get {
364                                 return (viewstate);
365                         }
366                 }
367
368                 /* There are no events defined for DataGridColumn, so no
369                  * idea what this method is supposed to do
370                  */
371                 protected virtual void OnColumnChanged ()
372                 {
373                 }
374                 
375                 void IStateManager.LoadViewState (object savedState)
376                 {
377                         LoadViewState (savedState);
378                 }
379
380                 object IStateManager.SaveViewState ()
381                 {
382                         return (SaveViewState ());
383                 }
384
385                 void IStateManager.TrackViewState ()
386                 {
387                         TrackViewState ();
388                 }
389
390                 bool IStateManager.IsTrackingViewState
391                 {
392                         get {
393                                 return (IsTrackingViewState);
394                         }
395                 }
396
397                 protected virtual void LoadViewState (object savedState)
398                 {
399                         object[] pieces = savedState as object[];
400
401                         if (pieces == null) {
402                                 return;
403                         }
404
405                         if (pieces[0] != null) {
406                                 viewstate.LoadViewState (pieces[0]);
407                         }
408                         if (pieces[1] != null) {
409                                 FooterStyle.LoadViewState (pieces[1]);
410                         }
411                         if (pieces[2] != null) {
412                                 HeaderStyle.LoadViewState (pieces[2]);
413                         }
414                         if (pieces[3] != null) {
415                                 ItemStyle.LoadViewState (pieces[3]);
416                         }
417                 }
418
419                 protected virtual object SaveViewState ()
420                 {
421                         object[] res = new object[4];
422
423                         res[0] = viewstate.SaveViewState ();
424
425                         if (footer_style != null) {
426                                 res[1] = footer_style.SaveViewState ();
427                         }
428                         if (header_style != null) {
429                                 res[2] = header_style.SaveViewState ();
430                         }
431                         if (item_style != null) {
432                                 res[3] = item_style.SaveViewState ();
433                         }
434
435                         return (res);
436                 }
437
438                 protected virtual void TrackViewState ()
439                 {
440                         tracking_viewstate = true;
441                         
442                         viewstate.TrackViewState ();
443                         if (footer_style != null) {
444                                 footer_style.TrackViewState ();
445                         }
446                         if (header_style != null) {
447                                 header_style.TrackViewState ();
448                         }
449                         if (item_style != null) {
450                                 item_style.TrackViewState ();
451                         }
452                 }
453
454                 protected bool IsTrackingViewState 
455                 {
456                         get {
457                                 return (tracking_viewstate);
458                         }
459                 }
460         }
461 }