2006-07-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / PagerSettings.cs
1 //\r
2 // System.Web.UI.WebControls.PagerSettings.cs\r
3 //\r
4 // Authors:\r
5 //      Lluis Sanchez Gual (lluis@novell.com)\r
6 //\r
7 // (C) 2005 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.ComponentModel;\r
34 using System.Security.Permissions;\r
35 \r
36 namespace System.Web.UI.WebControls\r
37 {\r
38         [TypeConverterAttribute (typeof(ExpandableObjectConverter))]\r
39         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]\r
40         public sealed class PagerSettings: IStateManager\r
41         {\r
42                 StateBag ViewState = new StateBag ();\r
43                 Control ctrl;\r
44                 \r
45                 public PagerSettings ()\r
46                 {\r
47                 }\r
48                 \r
49                 internal PagerSettings (Control ctrl)\r
50                 {\r
51                         this.ctrl = ctrl;\r
52                 }\r
53 \r
54                 [WebCategoryAttribute ("Appearance")]\r
55                 [NotifyParentPropertyAttribute (true)]\r
56                 [UrlPropertyAttribute]\r
57                 [DefaultValueAttribute ("")]\r
58                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
59                 public string FirstPageImageUrl {\r
60                         get {\r
61                                 object ob = ViewState ["FirstPageImageUrl"];\r
62                                 if (ob != null) return (string) ob;\r
63                                 return string.Empty;\r
64                         }\r
65                         set {\r
66                                 ViewState ["FirstPageImageUrl"] = value;\r
67                                 RaisePropertyChanged ();\r
68                         }\r
69                 }\r
70         \r
71                 [WebCategoryAttribute ("Appearance")]\r
72                 [DefaultValueAttribute ("&lt;&lt;")]\r
73                 [NotifyParentPropertyAttribute (true)]\r
74                 public string FirstPageText {\r
75                         get {\r
76                                 object ob = ViewState ["FirstPageText"];\r
77                                 if (ob != null) return (string) ob;\r
78                                 return "&lt;&lt;";\r
79                         }\r
80                         set {\r
81                                 ViewState ["FirstPageText"] = value;\r
82                                 RaisePropertyChanged ();\r
83                         }\r
84                 }\r
85         \r
86                 [WebCategoryAttribute ("Appearance")]\r
87                 [NotifyParentPropertyAttribute (true)]\r
88                 [UrlPropertyAttribute]\r
89                 [DefaultValueAttribute ("")]\r
90                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
91                 public string LastPageImageUrl {\r
92                         get {\r
93                                 object ob = ViewState ["LastPageImageUrl"];\r
94                                 if (ob != null) return (string) ob;\r
95                                 return string.Empty;\r
96                         }\r
97                         set {\r
98                                 ViewState ["LastPageImageUrl"] = value;\r
99                                 RaisePropertyChanged ();\r
100                         }\r
101                 }\r
102         \r
103                 [NotifyParentPropertyAttribute (true)]\r
104                 [WebCategoryAttribute ("Appearance")]\r
105                 [DefaultValueAttribute ("&gt;&gt;")]\r
106                 public string LastPageText {\r
107                         get {\r
108                                 object ob = ViewState ["LastPageText"];\r
109                                 if (ob != null) return (string) ob;\r
110                                 return "&gt;&gt;";\r
111                         }\r
112                         set {\r
113                                 ViewState ["LastPageText"] = value;\r
114                                 RaisePropertyChanged ();\r
115                         }\r
116                 }\r
117         \r
118                 [NotifyParentPropertyAttribute (true)]\r
119                 [WebCategoryAttribute ("Appearance")]\r
120                 [DefaultValueAttribute (PagerButtons.Numeric)]\r
121                 public PagerButtons Mode {\r
122                         get {\r
123                                 object ob = ViewState ["Mode"];\r
124                                 if (ob != null) return (PagerButtons) ob;\r
125                                 return PagerButtons.Numeric;\r
126                         }\r
127                         set {\r
128                                 ViewState ["Mode"] = value;\r
129                                 RaisePropertyChanged ();\r
130                         }\r
131                 }\r
132         \r
133                 [WebCategoryAttribute ("Appearance")]\r
134                 [NotifyParentPropertyAttribute (true)]\r
135                 [UrlPropertyAttribute]\r
136                 [DefaultValueAttribute ("")]\r
137                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
138                 public string NextPageImageUrl {\r
139                         get {\r
140                                 object ob = ViewState ["NextPageImageUrl"];\r
141                                 if (ob != null) return (string) ob;\r
142                                 return string.Empty;\r
143                         }\r
144                         set {\r
145                                 ViewState ["NextPageImageUrl"] = value;\r
146                                 RaisePropertyChanged ();\r
147                         }\r
148                 }\r
149 \r
150                 [WebCategoryAttribute ("Appearance")]\r
151                 [NotifyParentPropertyAttribute (true)]\r
152                 [DefaultValueAttribute ("&gt;")]\r
153                 public string NextPageText {\r
154                         get {\r
155                                 object ob = ViewState ["NextPageText"];\r
156                                 if (ob != null) return (string) ob;\r
157                                 return "&gt;";\r
158                         }\r
159                         set {\r
160                                 ViewState ["NextPageText"] = value;\r
161                                 RaisePropertyChanged ();\r
162                         }\r
163                 }\r
164         \r
165                 [WebCategoryAttribute ("Behavior")]\r
166                 [NotifyParentPropertyAttribute (true)]\r
167                 [DefaultValueAttribute (10)]\r
168                 public int PageButtonCount {\r
169                         get {\r
170                                 object ob = ViewState ["PageButtonCount"];\r
171                                 if (ob != null) return (int) ob;\r
172                                 return 10;\r
173                         }\r
174                         set {\r
175                                 ViewState ["PageButtonCount"] = value;\r
176                                 RaisePropertyChanged ();\r
177                         }\r
178                 }\r
179         \r
180                 [WebCategoryAttribute ("Layout")]\r
181                 [DefaultValueAttribute (PagerPosition.Bottom)]\r
182                 [NotifyParentPropertyAttribute (true)]\r
183                 public PagerPosition Position {\r
184                         get {\r
185                                 object ob = ViewState ["Position"];\r
186                                 if (ob != null) return (PagerPosition) ob;\r
187                                 return PagerPosition.Bottom;\r
188                         }\r
189                         set {\r
190                                 ViewState ["Position"] = value;\r
191                         }\r
192                 }\r
193         \r
194                 [WebCategoryAttribute ("Appearance")]\r
195                 [NotifyParentPropertyAttribute (true)]\r
196                 [UrlPropertyAttribute]\r
197                 [DefaultValueAttribute ("")]\r
198                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
199                 public string PreviousPageImageUrl {\r
200                         get {\r
201                                 object ob = ViewState ["PreviousPageImageUrl"];\r
202                                 if (ob != null) return (string) ob;\r
203                                 return string.Empty;\r
204                         }\r
205                         set {\r
206                                 ViewState ["PreviousPageImageUrl"] = value;\r
207                                 RaisePropertyChanged ();\r
208                         }\r
209                 }\r
210         \r
211                 [WebCategoryAttribute ("Appearance")]\r
212                 [DefaultValueAttribute ("&lt;")]\r
213                 [NotifyParentPropertyAttribute (true)]\r
214                 public string PreviousPageText {\r
215                         get {\r
216                                 object ob = ViewState ["PreviousPageText"];\r
217                                 if (ob != null) return (string) ob;\r
218                                 return "&lt;";\r
219                         }\r
220                         set {\r
221                                 ViewState ["PreviousPageText"] = value;\r
222                                 RaisePropertyChanged ();\r
223                         }\r
224                 }\r
225         \r
226                 [WebCategoryAttribute ("Appearance")]\r
227                 [DefaultValueAttribute (true)]\r
228                 [NotifyParentPropertyAttribute (true)]\r
229                 public bool Visible {\r
230                         get {\r
231                                 object ob = ViewState ["Visible"];\r
232                                 if (ob != null) return (bool) ob;\r
233                                 return true;\r
234                         }\r
235                         set {\r
236                                 ViewState ["Visible"] = value;\r
237                         }\r
238                 }\r
239 \r
240                 public event EventHandler PropertyChanged;\r
241 \r
242                 void RaisePropertyChanged ()\r
243                 {\r
244                         if (PropertyChanged != null)\r
245                                 PropertyChanged (this, EventArgs.Empty);\r
246                 }\r
247 \r
248                 public override string ToString ()\r
249                 {\r
250                         return string.Empty;\r
251                 }\r
252 \r
253                 void IStateManager.LoadViewState (object savedState)\r
254                 {\r
255                         ViewState.LoadViewState (savedState);\r
256                 }\r
257                 \r
258                 object IStateManager.SaveViewState ()\r
259                 {\r
260                         return ViewState.SaveViewState();\r
261                 }\r
262                 \r
263                 void IStateManager.TrackViewState ()\r
264                 {\r
265                         ViewState.TrackViewState();\r
266                 }\r
267                 \r
268                 bool IStateManager.IsTrackingViewState\r
269                 {\r
270                         get { return ViewState.IsTrackingViewState; }\r
271                 }\r
272                 \r
273                 internal Table CreatePagerControl (int currentPage, int pageCount)\r
274                 {\r
275                         Table table = new Table ();\r
276                         TableRow row = new TableRow ();\r
277                         table.Rows.Add (row);\r
278 \r
279                         if (Mode == PagerButtons.NextPrevious || Mode == PagerButtons.NextPreviousFirstLast)\r
280                         {\r
281                                 if (currentPage > 0) {\r
282                                         if (Mode == PagerButtons.NextPreviousFirstLast)\r
283                                                 row.Cells.Add (CreateCell (FirstPageText, FirstPageImageUrl, "Page", "First"));\r
284                                         row.Cells.Add (CreateCell (PreviousPageText, PreviousPageImageUrl, "Page", "Prev"));\r
285                                 }\r
286                                 if (currentPage < pageCount - 1) {\r
287                                         row.Cells.Add (CreateCell (NextPageText, NextPageImageUrl, "Page", "Next"));\r
288                                         if (Mode == PagerButtons.NextPreviousFirstLast)\r
289                                                 row.Cells.Add (CreateCell (LastPageText, LastPageImageUrl, "Page", "Last"));\r
290                                 }\r
291                         }\r
292                         else if (Mode == PagerButtons.Numeric || Mode == PagerButtons.NumericFirstLast)\r
293                         {\r
294                                 int pbc = PageButtonCount;
295                                 int cp = currentPage + 1;
296                                 int pbp = pbc <= cp ? cp / pbc : 0;
297                                 int first = cp < pbc ? 0 : (cp + (pbp % pbc) - (pbc + pbp)) + 1;
298                                 int last = first + pbc;\r
299                                 if (last >= pageCount) last = pageCount;\r
300                                 \r
301                                 if (first > 0) {\r
302                                         if (Mode == PagerButtons.NumericFirstLast)\r
303                                                 row.Cells.Add (CreateCell (FirstPageText, FirstPageImageUrl, "Page", "First"));\r
304                                         row.Cells.Add (CreateCell (PreviousPageText, PreviousPageImageUrl, "Page", "Prev"));\r
305                                 }\r
306                                 \r
307                                 for (int n = first; n < last; n++)\r
308                                         row.Cells.Add (CreateCell ((n+1).ToString(), string.Empty, (n != currentPage) ? "Page" : "", (n+1).ToString()));\r
309                                 \r
310                                 if (last < pageCount - 1) {\r
311                                         row.Cells.Add (CreateCell (NextPageText, NextPageImageUrl, "Page", "Next"));\r
312                                         if (Mode == PagerButtons.NumericFirstLast)\r
313                                                 row.Cells.Add (CreateCell (LastPageText, LastPageImageUrl, "Page", "Last"));\r
314                                 }\r
315                         }\r
316                         return table;\r
317                 }\r
318                 \r
319                 TableCell CreateCell (string text, string image, string command, string argument)\r
320                 {\r
321                         TableCell cell = new TableCell ();\r
322                         cell.Controls.Add (new DataControlButton (ctrl, text, image, command, argument, true));\r
323                         return cell;\r
324                 }\r
325         }\r
326 }\r
327 \r
328 #endif