2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls.Adapters / ControlAdapter.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /**
23  * Project   : Mono
24  * Namespace : System.Web.UI.MobileControls.Adapters
25  * Class     : ControlAdapter
26  * Author    : Gaurav Vaish
27  *
28  * Copyright : 2003 with Gaurav Vaish, and with
29  *             Ximian Inc
30  */
31
32 using System;
33 using System.Collections.Specialized;
34 using System.Web.Mobile;
35 using System.Web.UI;
36 using System.Web.UI.MobileControls;
37
38 namespace System.Web.UI.MobileControls.Adapters
39 {
40         public abstract class ControlAdapter : IControlAdapter
41         {
42                 private MobileControl control = null;
43
44                 protected static readonly int BackLabel = 0x00;
45                 protected static readonly int GoLabel   = 0x01;
46                 protected static readonly int OKLabel   = 0x02;
47                 protected static readonly int MoreLabel = 0x03;
48                 protected static readonly int OptionsLabel = 0x04;
49                 protected static readonly int NextLabel = 0x05;
50                 protected static readonly int PreviousLabel = 0x06;
51                 protected static readonly int LinkLabel = 0x07;
52                 protected static readonly int CallLabel = 0x08;
53
54                 private static string[] labelIDs = {
55                         "ControlAdapter_Back",
56                         "ControlAdapter_Go",
57                         "ControlAdapter_OK",
58                         "ControlAdapter_More",
59                         "ControlAdapter_Options",
60                         "ControlAdapter_Next",
61                         "ControlAdapter_Previous",
62                         "ControlAdapter_Link",
63                         "ControlAdapter_Call"
64                 };
65
66                 private string[] defaultLabels = null;
67
68                 public ControlAdapter()
69                 {
70                 }
71
72                 public MobileCapabilities Device
73                 {
74                         get
75                         {
76                                 if(Page != null)
77                                 {
78                                         return (MobileCapabilities) Page.Request.Browser;
79                                 }
80                                 return null;
81                         }
82                 }
83
84                 public System.Web.UI.MobileControls.Style Style
85                 {
86                         get
87                         {
88                                 return Control.Style;
89                         }
90                 }
91
92                 public virtual MobileControl Control
93                 {
94                         get
95                         {
96                                 return this.control;
97                         }
98                         set
99                         {
100                                 this.control = value;
101                         }
102                 }
103
104                 public int ItemWeight
105                 {
106                         get
107                         {
108                                 return ControlPager.UseDefaultWeight;
109                         }
110                 }
111
112                 public MobilePage Page
113                 {
114                         get
115                         {
116                                 if(Control != null)
117                                         return Control.MobilePage;
118                                 return null;
119                         }
120                 }
121
122                 public int VisibleWeight
123                 {
124                         get
125                         {
126                                 return ControlPager.UseDefaultWeight;
127                         }
128                 }
129
130                 public void CreateTemplatedUI(bool doDataBind)
131                 {
132                         Control.CreateDefaultTemplatedUI(doDataBind);
133                 }
134
135                 public virtual bool HandlePostBackEvent(string eventArguments)
136                 {
137                         return false;
138                 }
139
140                 public virtual void LoadAdapterState(object state)
141                 {
142                 }
143
144                 public virtual bool LoadPostData(string postKey,
145                                NameValueCollection postCollection,
146                                object privateControlData, out bool dataChanged)
147                 {
148                         dataChanged = false;
149                         return false;
150                 }
151
152                 public virtual void OnInit(EventArgs e)
153                 {
154                         return;
155                 }
156
157                 public virtual void OnLoad(EventArgs e)
158                 {
159                         return;
160                 }
161
162                 public virtual void OnPreRender(EventArgs e)
163                 {
164                         return;
165                 }
166
167                 public virtual void OnUnload(EventArgs e)
168                 {
169                         return;
170                 }
171
172                 public virtual void Render(HtmlTextWriter writer)
173                 {
174                         RenderChildren(writer);
175                 }
176
177                 public virtual object SaveAdapterState()
178                 {
179                         return null;
180                 }
181
182                 protected void RenderChildren(HtmlTextWriter writer)
183                 {
184                         if(Control != null && Control.HasControls())
185                         {
186                                 foreach(Control cCtrl in Control.Controls)
187                                 {
188                                         cCtrl.RenderControl(writer);
189                                 }
190                                 if(Control.Controls.GetEnumerator() is IDisposable)
191                                 {
192                                         ((IDisposable)Control.Controls.GetEnumerator()).Dispose();
193                                 }
194                         }
195                 }
196
197                 protected int CalculateOptimumPageWeight(int defaultPageWeight)
198                 {
199                         int defWt = defaultPageWeight;
200                         if(Device != null)
201                         {
202                                 string httpDefWt = Device[Constants.OptimumPageWeightParameter];
203                                 if(httpDefWt != null)
204                                 {
205                                         try
206                                         {
207                                                 defWt = Convert.ToInt32(httpDefWt);
208                                         } catch { }
209                                 }
210                         }
211                         return -1;
212                 }
213
214                 protected string GetDefaultLabel(int labelID)
215                 {
216                         if(labelID < 0 || labelID >= labelIDs.Length)
217                         {
218                                 // FIXME
219                                 throw new ArgumentException("ControlAdapter" +
220                                                             "_InvalidDefaultLabel");
221                         }
222                         string retVal = null;
223                         if(Page != null)
224                         {
225                                 ControlAdapter ca = (ControlAdapter)Page.Adapter;
226                                 if(defaultLabels == null)
227                                 {
228                                         defaultLabels = new string[labelIDs.Length];
229                                         Array.Copy(labelIDs, defaultLabels, labelIDs.Length);
230                                 }
231                                 retVal = defaultLabels[labelID];
232                         }
233                         if(retVal == null)
234                         {
235                                 retVal = labelIDs[labelID];
236                         }
237                         return retVal;
238                 }
239         }
240 }