2005-04-12 Dick Porter <dick@ximian.com>
[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 //
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, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "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                 }\r
112                 \r
113                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]\r
114                 [Browsable (false)]\r
115                 public virtual ViewCollection Views {\r
116                         get { return Controls as ViewCollection; }\r
117                 }\r
118                 \r
119                 protected override bool OnBubbleEvent (object source, EventArgs e)\r
120                 {\r
121                         CommandEventArgs ca = e as CommandEventArgs;\r
122                         if (ca != null) {\r
123                                 switch (ca.CommandName) {\r
124                                         case "NextView":\r
125                                                 if (viewIndex < Controls.Count - 1 && Controls.Count > 0)\r
126                                                         ActiveViewIndex = viewIndex + 1;\r
127                                                 break;\r
128                                                 \r
129                                         case "PrevView": \r
130                                                 if (viewIndex > 0)\r
131                                                         ActiveViewIndex = viewIndex - 1;\r
132                                                 break;\r
133                                                 \r
134                                         case "SwitchViewByID":\r
135                                                 foreach (View v in Controls)\r
136                                                         if (v.ID == ca.CommandArgument) {\r
137                                                                 SetActiveView (v);\r
138                                                                 break;\r
139                                                         }\r
140                                                 break;\r
141                                                 \r
142                                         case "SwitchViewByIndex":\r
143                                                 int i = (int) Convert.ChangeType (ca.CommandArgument, typeof(int));\r
144                                                 ActiveViewIndex = i;\r
145                                                 break;\r
146                                 }\r
147                         }\r
148                         return false;\r
149                 }\r
150                 \r
151                 protected override void OnInit (EventArgs e)\r
152                 {\r
153                         initied = true;\r
154                         Page.RegisterRequiresControlState (this);\r
155                         ActiveViewIndex = initialIndex;\r
156                         base.OnInit (e);\r
157                 }\r
158                 \r
159                 protected internal override void RemovedControl (Control ctl)\r
160                 {\r
161                         if (viewIndex >= Controls.Count)\r
162                                 viewIndex = Controls.Count - 1;\r
163 \r
164                         base.RemovedControl (ctl);\r
165                 }\r
166                 \r
167                 protected internal override void LoadControlState (object state)\r
168                 {\r
169                         if (state != null) viewIndex = (int)state;\r
170                         else viewIndex = -1;\r
171                 }\r
172                 \r
173                 protected internal override object SaveControlState ()\r
174                 {\r
175                         if (viewIndex != -1) return viewIndex;\r
176                         else return null;\r
177                 }\r
178                 \r
179                 protected virtual void OnActiveViewChanged (EventArgs e)\r
180                 {\r
181                         if (Events != null) {\r
182                                 EventHandler eh = (EventHandler) Events [ActiveViewChangedEvent];\r
183                                 if (eh != null) eh (this, e);\r
184                         }\r
185                 }\r
186                 \r
187                 protected override void Render (HtmlTextWriter writer)\r
188                 {\r
189                         if (viewIndex != -1)\r
190                                 GetActiveView ().Render (writer);\r
191                 }\r
192         }\r
193 }\r
194 \r
195 #endif\r