2007-07-24 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / MultiView.cs
1 //
2 // System.Web.UI.WebControls.MultiView.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004 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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.UI;
37 using System.ComponentModel;
38
39 namespace System.Web.UI.WebControls
40 {
41 //      [ControlBuilder (typeof(MultiViewControlBuilder)]
42         [Designer ("System.Web.UI.Design.WebControls.MultiViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43         [ToolboxData ("<{0}:MultiView runat=\"server\"></{0}:MultiView>")]
44 #if NET_2_0
45         [ParseChildren (typeof(View))]
46 #else
47         [ParseChildren (false)]
48 #endif
49         [DefaultEvent ("ActiveViewChanged")]
50         public class MultiView: Control
51         {
52                 public static readonly string NextViewCommandName = "NextView";
53                 public static readonly string PreviousViewCommandName = "PrevView";
54                 public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";
55                 public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";
56                 
57                 private static readonly object ActiveViewChangedEvent = new object();
58                 
59                 int viewIndex = -1;
60                 int initialIndex = -1;
61                 
62                 public event EventHandler ActiveViewChanged {
63                         add { Events.AddHandler (ActiveViewChangedEvent, value); }
64                         remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }
65                 }
66                 
67                 protected override void AddParsedSubObject (object ob)
68                 {
69                         if (ob is View)
70                                 Controls.Add (ob as View);
71                         // LAMESPEC: msdn talks that only View contorls are allowed, for others controls HttpException should be thrown
72                         // but actually, aspx praser adds LiteralControl controls.
73                         //else
74                         //      throw new HttpException ("MultiView cannot have children of type 'Control'.  It can only have children of type View.");
75                 }
76                 
77                 protected override ControlCollection CreateControlCollection ()
78                 {
79                         return new ViewCollection (this);
80                 }
81                 
82                 public View GetActiveView ()
83                 {
84                         if (viewIndex < 0 || viewIndex >= Controls.Count)
85                                 throw new HttpException ("The ActiveViewIndex is not set to a valid View control");
86                         return Controls [viewIndex] as View;
87                 }
88                 
89                 public void SetActiveView (View view)
90                 {
91                         int i = Controls.IndexOf (view);
92                         if (i == -1)
93                                 throw new HttpException ("The provided view is not contained in the MultiView control.");
94                                 
95                         ActiveViewIndex = i;
96                 }
97                 
98                 [DefaultValue (-1)]
99                 public virtual int ActiveViewIndex {
100                         get
101                         {
102                                 if (Controls.Count == 0)
103                                         return initialIndex;
104
105                                 return viewIndex;
106                         }
107                         set 
108                         {
109                                 if (Controls.Count == 0) {
110                                         initialIndex = value;
111                                         return;
112                                 }
113                                 
114                                 if (value < -1 || value >= Controls.Count)
115                                         throw new ArgumentOutOfRangeException ();
116
117                                 if (viewIndex != -1)
118                                         ((View)Controls [viewIndex]).NotifyActivation (false);
119
120                                 viewIndex = value;
121
122                                 if (viewIndex != -1)
123                                         ((View)Controls [viewIndex]).NotifyActivation (true);
124
125                                 UpdateViewVisibility ();
126
127                                 OnActiveViewChanged (EventArgs.Empty);
128                         }
129                 }
130
131                 [Browsable (true)]
132                 public virtual new bool EnableTheming
133                 {
134                         get { return base.EnableTheming; }
135                         set { base.EnableTheming = value; }
136                 }
137                 
138                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
139                 [Browsable (false)]
140                 public virtual ViewCollection Views {
141                         get { return Controls as ViewCollection; }
142                 }
143                 
144                 protected override bool OnBubbleEvent (object source, EventArgs e)
145                 {
146                         CommandEventArgs ca = e as CommandEventArgs;
147                         if (ca != null) {
148                                 switch (ca.CommandName) {
149                                         case "NextView":
150                                                 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)
151                                                         ActiveViewIndex = viewIndex + 1;
152                                                 break;
153                                                 
154                                         case "PrevView": 
155                                                 if (viewIndex > 0)
156                                                         ActiveViewIndex = viewIndex - 1;
157                                                 break;
158                                                 
159                                         case "SwitchViewByID":
160                                                 foreach (View v in Controls)
161                                                         if (v.ID == (string)ca.CommandArgument) {
162                                                                 SetActiveView (v);
163                                                                 break;
164                                                         }
165                                                 break;
166                                                 
167                                         case "SwitchViewByIndex":
168                                                 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));
169                                                 ActiveViewIndex = i;
170                                                 break;
171                                 }
172                         }
173                         return false;
174                 }
175                 
176                 protected internal override void OnInit (EventArgs e)
177                 {
178                         Page.RegisterRequiresControlState (this);
179                         if (initialIndex != -1) {
180                                 ActiveViewIndex = initialIndex;
181                                 initialIndex = -1;
182                         }
183                         base.OnInit (e);
184                 }
185                 
186                 void UpdateViewVisibility ()
187                 {
188                         for (int n=0; n<Views.Count; n++)
189                                 Views [n].VisibleInternal = (n == viewIndex);
190                 }
191                 
192                 protected internal override void RemovedControl (Control ctl)
193                 {
194                         if (viewIndex >= Controls.Count) {
195                                 viewIndex = Controls.Count - 1;
196                                 UpdateViewVisibility ();
197                         }
198
199                         base.RemovedControl (ctl);
200                 }
201                 
202                 protected internal override void LoadControlState (object state)
203                 {
204                         if (state != null) {
205                                 viewIndex = (int)state;
206                                 UpdateViewVisibility ();
207                         }
208                         else viewIndex = -1;
209                 }
210                 
211                 protected internal override object SaveControlState ()
212                 {
213                         if (viewIndex != -1) return viewIndex;
214                         else return null;
215                 }
216                 
217                 protected virtual void OnActiveViewChanged (EventArgs e)
218                 {
219                         if (Events != null) {
220                                 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];
221                                 if (eh != null) eh (this, e);
222                         }
223                 }
224                 
225                 protected internal override void Render (HtmlTextWriter writer)
226                 {
227                         if ((Controls.Count == 0) && (initialIndex != -1)) 
228                                 viewIndex = initialIndex;
229                         if (viewIndex != -1)
230                                 GetActiveView ().Render (writer);
231                 }
232         }
233 }
234
235 #endif