2003-09-04 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls / Command.cs
1 /**
2  * Project   : Mono
3  * Namespace : System.Web.UI.MobileControls
4  * Class     : Command
5  * Author    : Gaurav Vaish
6  *
7  * Copyright : 2003 with Gaurav Vaish, and with
8  *             Ximian Inc
9  */
10
11 using System.Collections.Specialized;
12 using System.Web.UI;
13 using System.Web.UI.WebControls;
14
15 namespace System.Web.UI.MobileControls
16 {
17         public class Command : TextControl, IPostBackEventHandler,
18                                IPostBackDataHandler
19         {
20                 private static readonly object ClickEvent       = new object();
21                 private static readonly object ItemCommandEvent = new object();
22
23                 public Command()
24                 {
25                 }
26
27                 public event EventHandler Click
28                 {
29                         add
30                         {
31                                 Events.AddHandler(ClickEvent, value);
32                         }
33                         remove
34                         {
35                                 Events.RemoveHandler(ClickEvent, value);
36                         }
37                 }
38
39                 public event ObjectListCommandEventHandler ItemCommand
40                 {
41                         add
42                         {
43                                 Events.AddHandler(ItemCommandEvent, value);
44                         }
45                         remove
46                         {
47                                 Events.RemoveHandler(ItemCommandEvent, value);
48                         }
49                 }
50
51                 bool IPostBackDataHandler.LoadPostData(string key,
52                                                       NameValueCollection data)
53                 {
54                         bool dataChanged;
55                         bool stateChanged = Adapter.LoadPostData(key, data, null, out dataChanged);
56                         if(stateChanged)
57                         {
58                                 if(dataChanged)
59                                         Page.RegisterRequiresRaiseEvent(this);
60                         } else
61                         {
62                                 if(data[key] != null)
63                                         Page.RegisterRequiresRaiseEvent(this);
64                         }
65                         return false;
66                 }
67
68                 void IPostBackDataHandler.RaisePostDataChangedEvent()
69                 {
70                 }
71
72                 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
73                 {
74                         if(CausesValidation)
75                                 MobilePage.Validate();
76                         Form.CurrentPage = 1;
77                         OnClick(EventArgs.Empty);
78                         OnItemCommand(new CommandEventArgs(CommandName, CommandArgument));
79                 }
80
81                 public bool CausesValidation
82                 {
83                         get
84                         {
85                                 object o = ViewState["CausesValidation"];
86                                 if(o != null)
87                                         return (bool)o;
88                                 return true;
89                         }
90                         set
91                         {
92                                 ViewState["CausesValidation"] = value;
93                         }
94                 }
95
96                 public string CommandArgument
97                 {
98                         get
99                         {
100                                 object o = ViewState["CommandArgument"];
101                                 if(o != null)
102                                         return (string)o;
103                                 return String.Empty;
104                         }
105                         set
106                         {
107                                 ViewState["CommandArgument"] = value;
108                         }
109                 }
110
111                 public string CommandName
112                 {
113                         get
114                         {
115                                 object o = ViewState["CommandName"];
116                                 if(o != null)
117                                         return (string)o;
118                                 return String.Empty;
119                         }
120                         set
121                         {
122                                 ViewState["CommandName"] = value;
123                         }
124                 }
125
126                 public CommandFormat Format
127                 {
128                         get
129                         {
130                                 object o = ViewState["Format"];
131                                 if(o != null)
132                                         return (CommandFormat)o;
133                                 return CommandFormat.Button;
134                         }
135                         set
136                         {
137                                 //if(!System.Enum.IsDefined(typeof(CommandFormat), value)
138                                 //      throw new ArgumentException("Illegal value");
139                                 ViewState["Format"] = value;
140                         }
141                 }
142
143                 public string ImageUrl
144                 {
145                         get
146                         {
147                                 object o = ViewState["ImageUrl"];
148                                 if(o != null)
149                                         return (string)o;
150                                 return String.Empty;
151                         }
152                         set
153                         {
154                                 ViewState["ImageUrl"] = value;
155                         }
156                 }
157
158                 public string SoftKeyLabel
159                 {
160                         get
161                         {
162                                 object o = ViewState["SoftKeyLabel"];
163                                 if(o != null)
164                                         return (string)o;
165                                 return String.Empty;
166                         }
167                         set
168                         {
169                                 ViewState["SoftKeyLabel"] = value;
170                         }
171                 }
172
173                 protected virtual void OnClick(EventArgs e)
174                 {
175                         EventHandler eh = (EventHandler)(Events[ClickEvent]);
176                         if(eh != null)
177                                 eh(this, e);
178                 }
179
180                 protected virtual void OnItemCommand(CommandEventArgs e)
181                 {
182                         CommandEventHandler ceh = (CommandEventHandler)(Events[ItemCommandEvent]);
183                         if(ceh != null)
184                                 ceh(this, e);
185                 }
186
187                 protected virtual bool IsFormSubmitControl()
188                 {
189                         return true;
190                 }
191         }
192 }