baeb9e7d254beed1e79a32c927308ac640a7c81f
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / MultiView.cs
1 //\r
2 // System.Web.UI.WebControls.MultiView.cs\r
3 //\r
4 // Authors:\r
5 //      Lluis Sanchez Gual (lluis@novell.com)\r
6 //\r
7 // (C) 2004 Novell, Inc (http://www.novell.com)\r
8 //\r
9 // Permission is hereby granted, free of charge, to any person obtaining\r
10 // a copy of this software and associated documentation files (the\r
11 // "Software"), to deal in the Software without restriction, including\r
12 // without limitation the rights to use, copy, modify, merge, publish,\r
13 // distribute, sublicense, and/or sell copies of the Software, and to\r
14 // permit persons to whom the Software is furnished to do so, subject to\r
15 // the following conditions:\r
16 // \r
17 // The above copyright notice and this permission notice shall be\r
18 // included in all copies or substantial portions of the Software.\r
19 // \r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
27 //\r
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
29 //\r
30 \r
31 #if NET_2_0\r
32 \r
33 using System;\r
34 using System.Globalization;\r
35 using System.Web;\r
36 using System.Web.UI;\r
37 using System.ComponentModel;\r
38 \r
39 namespace System.Web.UI.WebControls\r
40 {\r
41 //      [ControlBuilder (typeof(MultiViewControlBuilder)]\r
42         [Designer ("System.Web.UI.Design.WebControls.MultiViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]\r
43         [ToolboxData ("<{0}:MultiView runat=\"server\"></{0}:MultiView>")]\r
44         [ParseChildren (false, ChildControlType = typeof(View))]\r
45         [DefaultEvent ("ActiveViewChanged")]\r
46         public class MultiView: Control\r
47         {\r
48                 public static readonly string NextViewCommandName = "NextView";\r
49                 public static readonly string PreviousViewCommandName = "PrevView";\r
50                 public static readonly string SwitchViewByIDCommandName = "SwitchViewByID";\r
51                 public static readonly string SwitchViewByIndexCommandName = "SwitchViewByIndex";\r
52                 \r
53                 private static readonly object ActiveViewChangedEvent = new object();\r
54                 \r
55                 int viewIndex = -1;\r
56                 int initialIndex = -1;\r
57                 bool initied;\r
58                 \r
59                 public event EventHandler ActiveViewChanged {\r
60                         add { Events.AddHandler (ActiveViewChangedEvent, value); }\r
61                         remove { Events.RemoveHandler (ActiveViewChangedEvent, value); }\r
62                 }\r
63                 \r
64                 protected override void AddParsedSubObject (object ob)\r
65                 {\r
66                         if (ob is View)\r
67                                 Controls.Add (ob as View);\r
68                 }\r
69                 \r
70                 protected override ControlCollection CreateControlCollection ()\r
71                 {\r
72                         return new ViewCollection (this);\r
73                 }\r
74                 \r
75                 public View GetActiveView ()\r
76                 {\r
77                         if (viewIndex < 0 || viewIndex >= Controls.Count)\r
78                                 throw new HttpException ("The ActiveViewIndex is not set to a valid View control");\r
79                         return Controls [viewIndex] as View;\r
80                 }\r
81                 \r
82                 public void SetActiveView (View view)\r
83                 {\r
84                         int i = Controls.IndexOf (view);\r
85                         if (i == -1)\r
86                                 throw new HttpException ("The provided view is not contained in the MultiView control.");\r
87                                 \r
88                         ActiveViewIndex = i;\r
89                 }\r
90                 \r
91                 [DefaultValue (-1)]\r
92                 public virtual int ActiveViewIndex {\r
93                         get { return viewIndex; }\r
94                         set {\r
95                                 if (!initied) {\r
96                                         initialIndex = value;\r
97                                         return;\r
98                                 }\r
99                                 \r
100                                 if (value < -1 || value >= Controls.Count)\r
101                                         throw new ArgumentOutOfRangeException ();\r
102 \r
103                                 if (viewIndex != -1)\r
104                                         ((View)Controls [viewIndex]).NotifyActivation (false);\r
105 \r
106                                 viewIndex = value;\r
107 \r
108                                 if (viewIndex != -1)\r
109                                         ((View)Controls [viewIndex]).NotifyActivation (true);\r
110 \r
111                                 UpdateViewVisibility ();\r
112                         }\r
113                 }\r
114 \r
115                 [Browsable (true)]\r
116                 [MonoTODO]\r
117                 public virtual new bool EnableTheming\r
118                 {\r
119                         get {\r
120                                 throw new NotImplementedException ();\r
121                         }\r
122                         set {\r
123                                 throw new NotImplementedException ();\r
124                         }\r
125                 }\r
126                 \r
127                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]\r
128                 [Browsable (false)]\r
129                 public virtual ViewCollection Views {\r
130                         get { return Controls as ViewCollection; }\r
131                 }\r
132                 \r
133                 protected override bool OnBubbleEvent (object source, EventArgs e)\r
134                 {\r
135                         CommandEventArgs ca = e as CommandEventArgs;\r
136                         if (ca != null) {\r
137                                 switch (ca.CommandName) {\r
138                                         case "NextView":\r
139                                                 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)\r
140                                                         ActiveViewIndex = viewIndex + 1;\r
141                                                 break;\r
142                                                 \r
143                                         case "PrevView": \r
144                                                 if (viewIndex > 0)\r
145                                                         ActiveViewIndex = viewIndex - 1;\r
146                                                 break;\r
147                                                 \r
148                                         case "SwitchViewByID":\r
149                                                 foreach (View v in Controls)\r
150                                                         if (v.ID == ca.CommandArgument) {\r
151                                                                 SetActiveView (v);\r
152                                                                 break;\r
153                                                         }\r
154                                                 break;\r
155                                                 \r
156                                         case "SwitchViewByIndex":\r
157                                                 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));\r
158                                                 ActiveViewIndex = i;\r
159                                                 break;\r
160                                 }\r
161                         }\r
162                         return false;\r
163                 }\r
164                 \r
165                 protected internal override void OnInit (EventArgs e)\r
166                 {\r
167                         initied = true;\r
168                         Page.RegisterRequiresControlState (this);\r
169                         if (initialIndex != -1) {\r
170                                 ActiveViewIndex = initialIndex;\r
171                                 initialIndex = -1;\r
172                         }\r
173                         base.OnInit (e);\r
174                 }\r
175                 \r
176                 void UpdateViewVisibility ()\r
177                 {\r
178                         for (int n=0; n<Views.Count; n++)\r
179                                 Views [n].Visible = (n == viewIndex);\r
180                 }\r
181                 \r
182                 protected internal override void RemovedControl (Control ctl)\r
183                 {\r
184                         if (viewIndex >= Controls.Count) {\r
185                                 viewIndex = Controls.Count - 1;\r
186                                 UpdateViewVisibility ();\r
187                         }\r
188 \r
189                         base.RemovedControl (ctl);\r
190                 }\r
191                 \r
192                 protected internal override void LoadControlState (object state)\r
193                 {\r
194                         if (state != null) {\r
195                                 viewIndex = (int)state;\r
196                                 UpdateViewVisibility ();\r
197                         }\r
198                         else viewIndex = -1;\r
199                 }\r
200                 \r
201                 protected internal override object SaveControlState ()\r
202                 {\r
203                         if (viewIndex != -1) return viewIndex;\r
204                         else return null;\r
205                 }\r
206                 \r
207                 protected virtual void OnActiveViewChanged (EventArgs e)\r
208                 {\r
209                         if (Events != null) {\r
210                                 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];\r
211                                 if (eh != null) eh (this, e);\r
212                         }\r
213                 }\r
214                 \r
215                 protected internal override void Render (HtmlTextWriter writer)\r
216                 {\r
217                         if (!initied) viewIndex = initialIndex;\r
218                         if (viewIndex != -1)\r
219                                 GetActiveView ().Render (writer);\r
220                 }\r
221         }\r
222 }\r
223 \r
224 #endif\r