Merge pull request #5714 from alexischr/update_bockbuild
[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-2010 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                 protected DataGridColumn ()
52                 {
53                         viewstate = new StateBag ();
54                 }
55                 
56                 [DefaultValue (null)]
57                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
58                 [PersistenceMode (PersistenceMode.InnerProperty)]
59                 [WebSysDescription ("")]
60                 [WebCategory ("Misc")]
61                 public virtual TableItemStyle FooterStyle {
62                         get {
63                                 if (footer_style == null) {
64                                         footer_style = new TableItemStyle ();
65
66                                         if (tracking_viewstate)
67                                                 footer_style.TrackViewState ();
68                                 }
69
70                                 return (footer_style);
71                         }
72                 }
73                 
74                 [DefaultValue ("")]
75                 [WebSysDescription ("")]
76                 [WebCategory ("Misc")]
77                 public virtual string FooterText {
78                         get { return (viewstate.GetString ("FooterText", String.Empty)); }
79                         set { viewstate["FooterText"] = value; }
80                 }
81
82                 [DefaultValue ("")]
83                 [WebSysDescription ("")]
84                 [WebCategory ("Misc")]
85                 [UrlProperty]
86                 public virtual string HeaderImageUrl {
87                         get { return (viewstate.GetString ("HeaderImageUrl", String.Empty)); }
88                         set { viewstate["HeaderImageUrl"] = value; }
89                 }
90                 
91                 [DefaultValue (null)]
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
93                 [PersistenceMode (PersistenceMode.InnerProperty)]
94                 [WebSysDescription ("")]
95                 [WebCategory ("Misc")]
96                 public virtual TableItemStyle HeaderStyle {
97                         get {
98                                 if (header_style == null) {
99                                         header_style = new TableItemStyle ();
100
101                                         if (tracking_viewstate)
102                                                 header_style.TrackViewState ();
103                                 }
104
105                                 return (header_style);
106                         }
107                 }
108
109                 [DefaultValue ("")]
110                 [WebSysDescription ("")]
111                 [WebCategory ("Misc")]
112                 public virtual string HeaderText {
113                         get { return (viewstate.GetString ("HeaderText", String.Empty)); }
114                         set { viewstate["HeaderText"] = value; }
115                 }
116                 
117                 [DefaultValue (null)]
118                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
119                 [PersistenceMode (PersistenceMode.InnerProperty)]
120                 [WebSysDescription ("")]
121                 [WebCategory ("Misc")]
122                 public virtual TableItemStyle ItemStyle {
123                         get {
124                                 if (item_style == null) {
125                                         item_style = new TableItemStyle ();
126
127                                         if (tracking_viewstate)
128                                                 item_style.TrackViewState ();
129                                 }
130
131                                 return (item_style);
132                         }
133                 }
134                 
135                 [DefaultValue ("")]
136                 [WebSysDescription ("")]
137                 [WebCategory ("Misc")]
138                 public virtual string SortExpression {
139                         get { return (viewstate.GetString ("SortExpression", String.Empty)); }
140                         set { viewstate["SortExpression"] = value; }
141                 }
142
143                 [DefaultValue (true)]
144                 [WebSysDescription ("")]
145                 [WebCategory ("Misc")]
146                 public bool Visible  {
147                         get { return (viewstate.GetBool ("Visible", true)); }
148                         set { viewstate["Visible"] = value; }
149                 }
150
151                 public virtual void Initialize ()
152                 {
153                         if (owner != null && owner.Site != null)
154                                 design = owner.Site.DesignMode;
155                 }
156
157                 internal class ForeColorLinkButton : LinkButton
158                 {
159                         Color GetForeColor (WebControl control)
160                         {
161                                 if (control == null)
162                                         return Color.Empty;
163
164                                 // don't go beyond the container table.
165                                 if (control is Table)
166                                         return control.ControlStyle.ForeColor;
167                                 
168                                 Color color = control.ControlStyle.ForeColor;
169                                 if (color != Color.Empty)
170                                         return color;
171
172                                 return GetForeColor ((WebControl) control.Parent);
173                         }
174
175                         protected internal override void Render (HtmlTextWriter writer)
176                         {
177                                 Color color = GetForeColor (this);
178                                 if (color != Color.Empty)
179                                         ForeColor = color;
180                                 base.Render (writer);
181                         }
182                 }
183
184                 public virtual void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
185                 {
186                         switch (itemType) {
187                                 case ListItemType.Header: 
188                                 {
189                                         /* If sorting is enabled, add a
190                                          * LinkButton or an ImageButton
191                                          * (depending on HeaderImageUrl).
192                                          *
193                                          * If sorting is disabled, the
194                                          * HeaderText or an Image is displayed
195                                          * (depending on HeaderImageUrl).
196                                          *
197                                          * If neither HeaderText nor
198                                          * HeaderImageUrl is set, use &nbsp;
199                                          */
200                                         bool sort = false;
201                                         string sort_ex = SortExpression;
202                                 
203                                         if (owner != null && sort_ex.Length > 0)
204                                                 sort = owner.AllowSorting;
205                                 
206                                         string image_url = HeaderImageUrl;
207                                         if (image_url.Length > 0) {
208                                                 if (sort) {
209                                                         ImageButton butt = new ImageButton ();
210
211                                                         /* Don't need to
212                                                          * resolve this, Image
213                                                          * does that when it
214                                                          * renders
215                                                          */
216                                                         butt.ImageUrl = image_url;
217                                                         butt.CommandName = "Sort";
218                                                         butt.CommandArgument = sort_ex;
219
220                                                         cell.Controls.Add (butt);
221                                                 } else {
222                                                         Image image = new Image ();
223
224                                                         image.ImageUrl = image_url;
225
226                                                         cell.Controls.Add (image);
227                                                 }
228                                         } else {
229                                                 if (sort) {
230                                                         // This one always gets the forecolor of the header_style
231                                                         // from one of the parents, but we can't look it up at this
232                                                         // point, as it can change afterwards.
233                                                         LinkButton link = new ForeColorLinkButton ();
234
235                                                         link.Text = HeaderText;
236                                                         link.CommandName = "Sort";
237                                                         link.CommandArgument = sort_ex;
238
239                                                         cell.Controls.Add (link);
240                                                 } else {
241                                                         string text = HeaderText;
242                                                         if (text.Length > 0)
243                                                                 cell.Text = text;
244                                                         else
245                                                                 cell.Text = "&nbsp;";
246                                                 }
247                                         }
248                                 }
249                                 break;
250
251                                 case ListItemType.Footer:
252                                 {
253                                         /* Display FooterText or &nbsp; */
254                                         string text = FooterText;
255
256                                         if (text.Length > 0)
257                                                 cell.Text = text;
258                                         else
259                                                 cell.Text = "&nbsp;";
260                                 }
261                                 break;
262
263                                 default:
264                                         break;
265                         }
266                 }
267
268                 public override string ToString ()
269                 {
270                         return (String.Empty);
271                 }
272
273                 protected bool DesignMode {
274                         get {return (design); }
275                 }
276                 
277                 protected DataGrid Owner {
278                         get { return (owner); }
279                 }
280
281                 internal TableItemStyle GetStyle (ListItemType type)
282                 {
283                         if (type == ListItemType.Header)
284                                 return header_style;
285
286                         if (type == ListItemType.Footer)
287                                 return footer_style;
288
289                         return item_style;
290                 }
291
292                 internal void Set_Owner (DataGrid value) 
293                 {
294                         owner = value;
295                 }
296                 
297                 protected StateBag ViewState {
298                         get { return (viewstate); }
299                 }
300
301                 /* There are no events defined for DataGridColumn, so no
302                  * idea what this method is supposed to do
303                  */
304                 protected virtual void OnColumnChanged ()
305                 {
306                 }
307                 
308                 void IStateManager.LoadViewState (object savedState)
309                 {
310                         LoadViewState (savedState);
311                 }
312
313                 object IStateManager.SaveViewState ()
314                 {
315                         return (SaveViewState ());
316                 }
317
318                 void IStateManager.TrackViewState ()
319                 {
320                         TrackViewState ();
321                 }
322
323                 bool IStateManager.IsTrackingViewState {
324                         get { return (IsTrackingViewState); }
325                 }
326
327                 protected virtual void LoadViewState (object savedState)
328                 {
329                         object[] pieces = savedState as object[];
330
331                         if (pieces == null)
332                                 return;
333
334                         if (pieces[0] != null)
335                                 viewstate.LoadViewState (pieces[0]);
336                         
337                         if (pieces[1] != null)
338                                 FooterStyle.LoadViewState (pieces[1]);
339                         
340                         if (pieces[2] != null)
341                                 HeaderStyle.LoadViewState (pieces[2]);
342                         
343                         if (pieces[3] != null)
344                                 ItemStyle.LoadViewState (pieces[3]);
345                 }
346
347                 protected virtual object SaveViewState ()
348                 {
349                         object[] res = new object[4];
350
351                         res[0] = viewstate.SaveViewState ();
352
353                         if (footer_style != null)
354                                 res[1] = footer_style.SaveViewState ();
355                         
356                         if (header_style != null)
357                                 res[2] = header_style.SaveViewState ();
358                         
359                         if (item_style != null)
360                                 res[3] = item_style.SaveViewState ();
361
362                         return (res);
363                 }
364
365                 protected virtual void TrackViewState ()
366                 {
367                         tracking_viewstate = true;
368                         
369                         viewstate.TrackViewState ();
370                         if (footer_style != null)
371                                 footer_style.TrackViewState ();
372                         
373                         if (header_style != null)
374                                 header_style.TrackViewState ();
375                         
376                         if (item_style != null)
377                                 item_style.TrackViewState ();
378                 }
379
380                 protected bool IsTrackingViewState {
381                         get { return (tracking_viewstate); }
382                 }
383         }
384 }