[bcl] Remove NET_3_5 defines from class libs
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI.WebControls / NextPreviousPagerField.cs
1 //
2 // System.Web.UI.WebControls.NextPreviousPagerField
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2007-2008 Novell, Inc
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33 using System.Web;
34 using System.Web.UI;
35
36 namespace System.Web.UI.WebControls
37 {
38         [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class NextPreviousPagerField : DataPagerField
41         {
42                 int _startRowIndex;
43                 int _maximumRows;
44                 int _totalRowCount;
45                 int _fieldIndex;
46                 
47                 public NextPreviousPagerField ()
48                 {
49                 }
50
51                 protected override void CopyProperties (DataPagerField newField)
52                 {
53                         base.CopyProperties (newField);
54
55                         NextPreviousPagerField field = newField as NextPreviousPagerField;
56                         if (field == null)
57                                 return;
58                         
59                         field.ButtonCssClass = ButtonCssClass;
60                         field.ButtonType = ButtonType;
61                         field.FirstPageImageUrl = FirstPageImageUrl;
62                         field.FirstPageText = FirstPageText;
63                         field.LastPageImageUrl = LastPageImageUrl;
64                         field.LastPageText = LastPageText;
65                         field.NextPageImageUrl = NextPageImageUrl;
66                         field.NextPageText = NextPageText;
67                         field.PreviousPageImageUrl = PreviousPageImageUrl;
68                         field.PreviousPageText = PreviousPageText;
69                         field.ShowFirstPageButton = ShowFirstPageButton;
70                         field.ShowLastPageButton = ShowLastPageButton;
71                         field.ShowNextPageButton = ShowNextPageButton;
72                         field.ShowPreviousPageButton = ShowPreviousPageButton;
73                 }
74
75                 // What's the fieldIndex used for?
76                 public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
77                 {
78                         _startRowIndex = startRowIndex;
79                         _maximumRows = maximumRows;
80                         _totalRowCount = totalRowCount;
81                         _fieldIndex = fieldIndex;
82
83                         bool setPagePropertiesNeeded = false;
84                         bool queryMode = GetQueryModeStartRowIndex (_totalRowCount, _maximumRows, ref _startRowIndex, ref setPagePropertiesNeeded);
85                         bool enablePrevFirst = _startRowIndex >= _maximumRows;
86                         bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
87                         bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
88
89                         if (ShowFirstPageButton)
90                                 CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
91                                               queryMode, enablePrevFirst, addNonBreakingSpace);
92                         
93                         int newPageNum = -1;
94                         if (ShowPreviousPageButton) {
95                                 if (queryMode)
96                                         newPageNum = (_startRowIndex / _maximumRows) - 1;
97                                 
98                                 CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
99                                               queryMode, enablePrevFirst, addNonBreakingSpace);
100                         }
101                         
102                         if (ShowNextPageButton) {
103                                 if (queryMode)
104                                         newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
105                                 
106                                 CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
107                                               queryMode, enableNextLast, addNonBreakingSpace);
108                         }
109                         
110                         if (ShowLastPageButton) {
111                                 if (queryMode) {
112                                         newPageNum = _totalRowCount / _maximumRows;
113                                         if ((_totalRowCount % _maximumRows) == 0)
114                                                 newPageNum--;
115                                 }
116                                 
117                                 CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
118                                               queryMode, enableNextLast, addNonBreakingSpace);
119                         }
120                         
121                         if (setPagePropertiesNeeded)
122                                 DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
123                 }
124
125                 void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum,
126                                    bool queryMode, bool enabled, bool addNonBreakingSpace)
127                 {
128                         WebControl ctl = null;
129                         
130                         if (queryMode) {
131                                 pageNum++;
132                                 HyperLink h = new HyperLink ();
133                                 h.Text = text;
134                                 h.ImageUrl = imageUrl;
135                                 h.Enabled = enabled;
136                                 h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
137                                 h.CssClass = ButtonCssClass;
138                                 ctl = h;
139                         } else {
140                                 if (!enabled && RenderDisabledButtonsAsLabels) {
141                                         Label l = new Label ();
142                                         l.Text = text;
143                                         ctl = l;
144                                 } else {
145                                         switch (ButtonType) {
146                                                 case ButtonType.Button:
147                                                         Button btn = new Button ();
148                                                         btn.CommandName = commandName;
149                                                         btn.Text = text;
150                                                         ctl = btn;
151                                                         break;
152
153                                                 case ButtonType.Link:
154                                                         LinkButton lbtn = new LinkButton ();
155                                                         lbtn.CommandName = commandName;
156                                                         lbtn.Text = text;
157                                                         ctl = lbtn;
158                                                         break;
159
160                                                 case ButtonType.Image:
161                                                         ImageButton ibtn = new ImageButton ();
162                                                         ibtn.CommandName = commandName;
163                                                         ibtn.ImageUrl = imageUrl;
164                                                         ibtn.AlternateText = text;
165                                                         ctl = ibtn;
166                                                         break;
167                                         }
168
169                                         if (ctl != null) {
170                                                 ctl.Enabled = enabled;
171                                                 ctl.CssClass = ButtonCssClass;
172                                         }
173                                 }
174                         }
175
176                         if (ctl != null) {
177                                 container.Controls.Add (ctl);
178                                 if (addNonBreakingSpace)
179                                         container.Controls.Add (new LiteralControl ("&nbsp;"));
180                         }
181                 }
182                 
183                 protected override DataPagerField CreateField ()
184                 {
185                         return new NextPreviousPagerField ();
186                 }
187
188                 public override bool Equals (object o)
189                 {
190                         NextPreviousPagerField field = o as NextPreviousPagerField;
191                         if (field == null)
192                                 return false;
193                         
194                         // Compare using the properties that are copied in CopyProperties
195                         if (field.ButtonCssClass != ButtonCssClass)
196                                 return false;
197
198                         if (field.ButtonType != ButtonType)
199                                 return false;
200
201                         if (field.FirstPageImageUrl != FirstPageImageUrl)
202                                 return false;
203                         
204                         if (field.FirstPageText != FirstPageText)
205                                 return false;
206                         
207                         if (field.LastPageImageUrl != LastPageImageUrl)
208                                 return false;
209                         
210                         if (field.LastPageText != LastPageText)
211                                 return false;
212                         
213                         if (field.NextPageImageUrl != NextPageImageUrl)
214                                 return false;
215                         
216                         if (field.NextPageText != NextPageText)
217                                 return false;
218                         
219                         if (field.PreviousPageImageUrl != PreviousPageImageUrl)
220                                 return false;
221                         
222                         if (field.PreviousPageText != PreviousPageText)
223                                 return false;
224                         
225                         if (field.ShowFirstPageButton != ShowFirstPageButton)
226                                 return false;
227                         
228                         if (field.ShowLastPageButton != ShowLastPageButton)
229                                 return false;
230                         
231                         if (field.ShowNextPageButton != ShowNextPageButton)
232                                 return false;
233                         
234                         if (field.ShowPreviousPageButton != ShowPreviousPageButton)
235                                 return false;
236
237                         return true;
238                 }
239
240                 public override int GetHashCode ()
241                 {
242                         int ret = 0;
243
244                         // Base the calculation on the properties that are copied in CopyProperties
245                         ret |= ButtonCssClass.GetHashCode ();
246                         ret |= ButtonType.GetHashCode ();
247                         ret |= FirstPageImageUrl.GetHashCode ();
248                         ret |= FirstPageText.GetHashCode ();
249                         ret |= LastPageImageUrl.GetHashCode ();
250                         ret |= LastPageText.GetHashCode ();
251                         ret |= NextPageImageUrl.GetHashCode ();
252                         ret |= NextPageText.GetHashCode ();
253                         ret |= PreviousPageImageUrl.GetHashCode ();
254                         ret |= PreviousPageText.GetHashCode ();
255                         ret |= ShowFirstPageButton.GetHashCode ();
256                         ret |= ShowLastPageButton.GetHashCode ();
257                         ret |= ShowNextPageButton.GetHashCode ();
258                         ret |= ShowPreviousPageButton.GetHashCode ();
259
260                         return ret;
261                 }
262
263                 public override void HandleEvent (CommandEventArgs e)
264                 {
265                         string commandName = e.CommandName;
266                         int newStartIndex = -1;
267                         int pageSize = DataPager.PageSize;
268                         
269                         if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
270                                 newStartIndex = 0;
271                         else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
272                                 int lastPageMod = _totalRowCount % pageSize;
273                                 if (lastPageMod == 0)
274                                         newStartIndex = _totalRowCount - pageSize;
275                                 else
276                                         newStartIndex = _totalRowCount - lastPageMod;
277                         } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
278                                 newStartIndex = _startRowIndex + pageSize;
279                                 if (_totalRowCount >= 0 && newStartIndex > _totalRowCount)
280                                         newStartIndex = _totalRowCount - pageSize;
281                         } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
282                                 newStartIndex = _startRowIndex - pageSize;
283                                 if (newStartIndex < 0)
284                                         newStartIndex = 0;
285                         }
286
287                         if (newStartIndex >= 0)
288                                 DataPager.SetPageProperties (newStartIndex, pageSize, true);
289                 }
290
291                 public string ButtonCssClass {
292                         get {
293                                 string s = ViewState ["ButtonCssClass"] as string;
294                                 if (s != null)
295                                         return s;
296
297                                 return String.Empty;
298                         }
299                         
300                         set { ViewState ["ButtonCssClass"] = value; }
301                 }
302
303                 public ButtonType ButtonType {
304                         get {
305                                 object o = ViewState ["ButtonType"];
306                                 if (o != null)
307                                         return (ButtonType) o;
308
309                                 return ButtonType.Link;
310                         }
311                         
312                         set { ViewState ["ButtonType"] = value; }
313                 }
314
315                 public string FirstPageImageUrl {
316                         get {
317                                 string s = ViewState ["FirstPageImageUrl"] as string;
318                                 if (s != null)
319                                         return s;
320
321                                 return String.Empty;
322                         }
323                         
324                         set { ViewState ["FirstPageImageUrl"] = value; }
325                 }
326
327                 public string FirstPageText {
328                         get {
329                                 string s = ViewState ["FirstPageText"] as string;
330                                 if (s != null)
331                                         return s;
332
333                                 return "First";
334                         }
335                         
336                         set { ViewState ["FirstPageText"] = value; }
337                 }
338
339                 public string LastPageImageUrl {
340                         get {
341                                 string s = ViewState ["LastPageImageUrl"] as string;
342                                 if (s != null)
343                                         return s;
344
345                                 return String.Empty;
346                         }
347                         
348                         set { ViewState ["LastPageImageUrl"] = value; }
349                 }
350
351                 public string LastPageText {
352                         get {
353                                 string s = ViewState ["LastPageText"] as string;
354                                 if (s != null)
355                                         return s;
356
357                                 return "Last";
358                         }
359                         
360                         set { ViewState ["LastPageText"] = value; }
361                 }
362
363                 public string NextPageImageUrl {
364                         get {
365                                 string s = ViewState ["NextPageImageUrl"] as string;
366                                 if (s != null)
367                                         return s;
368
369                                 return String.Empty;
370                         }
371                         
372                         set { ViewState ["NextPageImageUrl"] = value; }
373                 }
374
375                 public string NextPageText {
376                         get {
377                                 string s = ViewState ["NextPageText"] as string;
378                                 if (s != null)
379                                         return s;
380
381                                 return "Next";
382                         }
383                         
384                         set { ViewState ["NextPageText"] = value; }
385                 }
386
387                 public string PreviousPageImageUrl {
388                         get {
389                                 string s = ViewState ["PreviousPageImageUrl"] as string;
390                                 if (s != null)
391                                         return s;
392
393                                 return String.Empty;
394                         }
395                         
396                         set { ViewState ["PreviousPageImageUrl"] = value; }
397                 }
398
399                 public string PreviousPageText {
400                         get {
401                                 string s = ViewState ["PreviousPageText"] as string;
402                                 if (s != null)
403                                         return s;
404
405                                 return "Previous";
406                         }
407                         
408                         set { ViewState ["PreviousPageText"] = value; }
409                 }
410
411                 public bool RenderDisabledButtonsAsLabels {
412                         get {
413                                 object o = ViewState ["RenderDisabledButtonsAsLabels"];
414                                 if (o != null)
415                                         return (bool) o;
416
417                                 return false;
418                         }
419                         
420                         set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
421                 }
422
423                 public bool RenderNonBreakingSpacesBetweenControls {
424                         get {
425                                 object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
426                                 if (o != null)
427                                         return (bool) o;
428
429                                 return true;
430                         }
431                         
432                         set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
433                 }
434
435                 public bool ShowFirstPageButton {
436                         get {
437                                 object o = ViewState ["ShowFirstPageButton"];
438                                 if (o != null)
439                                         return (bool) o;
440
441                                 return false;
442                         }
443                         
444                         set { ViewState ["ShowFirstPageButton"] = value; }
445                 }
446
447                 public bool ShowLastPageButton {
448                         get {
449                                 object o = ViewState ["ShowLastPageButton"];
450                                 if (o != null)
451                                         return (bool) o;
452
453                                 return false;
454                         }
455                         
456                         set { ViewState ["ShowLastPageButton"] = value; }
457                 }
458
459                 public bool ShowNextPageButton {
460                         get {
461                                 object o = ViewState ["ShowNextPageButton"];
462                                 if (o != null)
463                                         return (bool) o;
464
465                                 return true;
466                         }
467                         
468                         set { ViewState ["ShowNextPageButton"] = value; }
469                 }
470
471                 public bool ShowPreviousPageButton {
472                         get {
473                                 object o = ViewState ["ShowPreviousPageButton"];
474                                 if (o != null)
475                                         return (bool) o;
476
477                                 return true;
478                         }
479                         
480                         set { ViewState ["ShowPreviousPageButton"] = value; }
481                 }
482         }
483 }