Move negative tests from ilasm/tests to ilasm/errors.
[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         [ParseChildren (false, ChildControlType = typeof(View))]
45         [DefaultEvent ("ActiveViewChanged")]
46         public class MultiView: Control
47         {
48                 public static readonly string NextViewCommandName = "NextView";
49                 public static readonly string PreviousViewCommandName = "PrevView";
50                 public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";
51                 public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";
52                 
53                 private static readonly object ActiveViewChangedEvent = new object();
54                 
55                 int viewIndex = -1;
56                 int initialIndex = -1;
57                 bool initied;
58                 
59                 public event EventHandler ActiveViewChanged {
60                         add { Events.AddHandler (ActiveViewChangedEvent, value); }
61                         remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }
62                 }
63                 
64                 protected override void AddParsedSubObject (object ob)
65                 {
66                         if (ob is View)
67                                 Controls.Add (ob as View);
68                 }
69                 
70                 protected override ControlCollection CreateControlCollection ()
71                 {
72                         return new ViewCollection (this);
73                 }
74                 
75                 public View GetActiveView ()
76                 {
77                         if (viewIndex < 0 || viewIndex >= Controls.Count)
78                                 throw new HttpException ("The ActiveViewIndex is not set to a valid View control");
79                         return Controls [viewIndex] as View;
80                 }
81                 
82                 public void SetActiveView (View view)
83                 {
84                         int i = Controls.IndexOf (view);
85                         if (i == -1)
86                                 throw new HttpException ("The provided view is not contained in the MultiView control.");
87                                 
88                         ActiveViewIndex = i;
89                 }
90                 
91                 [DefaultValue (-1)]
92                 public virtual int ActiveViewIndex {
93                         get { return viewIndex; }
94                         set {
95                                 if (!initied) {
96                                         initialIndex = value;
97                                         return;
98                                 }
99                                 
100                                 if (value < -1 || value >= Controls.Count)
101                                         throw new ArgumentOutOfRangeException ();
102
103                                 if (viewIndex != -1)
104                                         ((View)Controls [viewIndex]).NotifyActivation (false);
105
106                                 viewIndex = value;
107
108                                 if (viewIndex != -1)
109                                         ((View)Controls [viewIndex]).NotifyActivation (true);
110
111                                 UpdateViewVisibility ();
112                         }
113                 }
114
115                 [Browsable (true)]
116                 [MonoTODO]
117                 public virtual new bool EnableTheming
118                 {
119                         get {
120                                 throw new NotImplementedException ();
121                         }
122                         set {
123                                 throw new NotImplementedException ();
124                         }
125                 }
126                 
127                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
128                 [Browsable (false)]
129                 public virtual ViewCollection Views {
130                         get { return Controls as ViewCollection; }
131                 }
132                 
133                 protected override bool OnBubbleEvent (object source, EventArgs e)
134                 {
135                         CommandEventArgs ca = e as CommandEventArgs;
136                         if (ca != null) {
137                                 switch (ca.CommandName) {
138                                         case "NextView":
139                                                 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)
140                                                         ActiveViewIndex = viewIndex + 1;
141                                                 break;
142                                                 
143                                         case "PrevView": 
144                                                 if (viewIndex > 0)
145                                                         ActiveViewIndex = viewIndex - 1;
146                                                 break;
147                                                 
148                                         case "SwitchViewByID":
149                                                 foreach (View v in Controls)
150                                                         if (v.ID == (string)ca.CommandArgument) {
151                                                                 SetActiveView (v);
152                                                                 break;
153                                                         }
154                                                 break;
155                                                 
156                                         case "SwitchViewByIndex":
157                                                 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));
158                                                 ActiveViewIndex = i;
159                                                 break;
160                                 }
161                         }
162                         return false;
163                 }
164                 
165                 protected internal override void OnInit (EventArgs e)
166                 {
167                         initied = true;
168                         Page.RegisterRequiresControlState (this);
169                         if (initialIndex != -1) {
170                                 ActiveViewIndex = initialIndex;
171                                 initialIndex = -1;
172                         }
173                         base.OnInit (e);
174                 }
175                 
176                 void UpdateViewVisibility ()
177                 {
178                         for (int n=0; n<Views.Count; n++)
179                                 Views [n].Visible = (n == viewIndex);
180                 }
181                 
182                 protected internal override void RemovedControl (Control ctl)
183                 {
184                         if (viewIndex >= Controls.Count) {
185                                 viewIndex = Controls.Count - 1;
186                                 UpdateViewVisibility ();
187                         }
188
189                         base.RemovedControl (ctl);
190                 }
191                 
192                 protected internal override void LoadControlState (object state)
193                 {
194                         if (state != null) {
195                                 viewIndex = (int)state;
196                                 UpdateViewVisibility ();
197                         }
198                         else viewIndex = -1;
199                 }
200                 
201                 protected internal override object SaveControlState ()
202                 {
203                         if (viewIndex != -1) return viewIndex;
204                         else return null;
205                 }
206                 
207                 protected virtual void OnActiveViewChanged (EventArgs e)
208                 {
209                         if (Events != null) {
210                                 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];
211                                 if (eh != null) eh (this, e);
212                         }
213                 }
214                 
215                 protected internal override void Render (HtmlTextWriter writer)
216                 {
217                         if (!initied) viewIndex = initialIndex;
218                         if (viewIndex != -1)
219                                 GetActiveView ().Render (writer);
220                 }
221         }
222 }
223
224 #endif