* Managed.Windows.Forms/System.Windows.Forms/HtmlDocument.cs,
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / DOM / Element.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26 using System;
27 using System.Runtime.InteropServices;
28 using Mono.WebBrowser;
29 using Mono.WebBrowser.DOM;
30
31 namespace Mono.Mozilla.DOM
32 {
33         internal class Element : Node, IElement
34         {
35                 internal nsIDOMElement node {
36                         get { return base.node as nsIDOMElement;}
37                         set { base.node = value as nsIDOMNode; }
38                 }
39                 
40                 public Element(WebBrowser control, nsIDOMElement domElement) : base (control, domElement as nsIDOMNode)
41                 {
42                         this.node = domElement;
43                 }
44
45                 #region IDisposable Members
46                 protected override  void Dispose (bool disposing)
47                 {
48                         if (!disposed) {
49                                 if (disposing) {
50                                         this.node = null;
51                                 }
52                         }
53                         base.Dispose(disposing);
54                 }               
55                 #endregion
56
57                 #region Properties
58                 public virtual IElement AppendChild (IElement child) {
59                         nsIDOMNode newChild;
60                         Element elem = (Element) child;
61                         this.node.appendChild (elem.node, out newChild);
62                         return new Element (control, newChild as nsIDOMElement);
63                 }
64
65                 public virtual string InnerText
66                 {
67                         get
68                         {
69                                 nsIDOMDocumentRange docRange = ((Document) control.Document).XPComObject as nsIDOMDocumentRange;
70                                 nsIDOMRange range;
71                                 docRange.createRange (out range);
72                                 range.selectNodeContents (this.node);
73                                 range.toString (storage);
74                                 return Base.StringGet (storage);
75                         }
76                         set
77                         {
78                                 Base.StringSet (storage, value);
79                                 this.node.setNodeValue (storage);
80                         }
81                 }
82                 
83                 public virtual string OuterText
84                 {
85                         get
86                         {
87                                 nsIDOMDocumentRange docRange = ((Document) control.Document).XPComObject as nsIDOMDocumentRange;
88                                 nsIDOMRange range;
89                                 docRange.createRange (out range);
90                                 nsIDOMNode parent;
91                                 node.getParentNode (out parent);
92                                 range.selectNodeContents (parent);
93                                 range.toString (storage);
94                                 return Base.StringGet (storage);
95                         }
96                         set
97                         {
98                                 Base.StringSet (storage, value);
99                                 nsIDOMNode parent;
100                                 node.getParentNode (out parent);
101                                 parent.setNodeValue (storage);
102                         }
103                 }               
104
105                 public virtual string InnerHTML {
106                         get { return String.Empty; }
107                         set { }
108                 }
109                 
110                 public virtual string OuterHTML {
111                         get { return String.Empty; }
112                         set {}
113                 }               
114
115                 public IElementCollection All {
116                         get
117                         {
118                                 if (!resources.Contains ("All")) {
119                                 
120                                         HTMLElementCollection col = new HTMLElementCollection (control);
121                                         Recurse (col, this.node); 
122                                         resources.Add ("All", col);
123                                 }
124                                 return resources["All"] as IElementCollection;
125                         }
126                 }
127                 
128                 private void Recurse (HTMLElementCollection col, nsIDOMNode parent) {                   
129                         nsIDOMNodeList children;
130                         parent.getChildNodes (out children);
131                         uint count;
132                         children.getLength (out count);
133
134                         for (int i = 0; i < count;i++) {
135                                 nsIDOMNode node;
136                                 children.item ((uint)i, out node);
137                                 ushort type;
138                                 node.getNodeType (out type);
139                                 if (type == (ushort)NodeType.Element) {
140                                         col.Add (new HTMLElement (control, (nsIDOMHTMLElement)node));
141                                         Recurse (col, node);
142                                 }
143                         }
144                 }
145
146
147                 public IElementCollection Children {
148                         get
149                         {
150                                 if (!resources.Contains ("Children")) {
151                                         nsIDOMNodeList children;
152                                         this.node.getChildNodes (out children);
153                                         resources.Add ("Children", new HTMLElementCollection (control, children));
154                                 }
155                                 return resources["Children"] as IElementCollection;
156                         }
157                 }
158
159
160                 public virtual int TabIndex {
161                         get { return -1; }
162                         set { }
163                 }
164                 
165                 public virtual string TagName {
166                         get {
167                                 node.getTagName (storage);
168                                 return Base.StringGet (storage);
169                         }
170                 }
171
172                 public virtual bool Disabled {
173                         get { return false; }
174                         set { }
175                                 
176                 }
177
178                 public virtual int ClientWidth { 
179                         get {
180                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
181                                 int ret = 0;
182                                 e.getClientWidth (out ret);
183                                 return ret;
184                         }
185                 }
186                 public virtual int ClientHeight {
187                         get {
188                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
189                                 int ret = 0;
190                                 e.getClientHeight (out ret);
191                                 return ret;
192                         }
193                 }
194                 public virtual int ScrollHeight {
195                         get {
196                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
197                                 int ret = 0;
198                                 e.getScrollHeight(out ret);
199                                 return ret;
200                         }
201                 }
202                 public virtual int ScrollWidth  {
203                         get {
204                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
205                                 int ret = 0;
206                                 e.getScrollWidth (out ret);
207                                 return ret;
208                         }
209                 }
210                 public virtual int ScrollLeft {
211                         get {
212                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
213                                 int ret = 0;
214                                 e.getScrollLeft (out ret);
215                                 return ret;
216                         }
217                         set {
218                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
219                                 e.setScrollLeft (value);
220                         }
221                 }
222                 public virtual int ScrollTop {
223                         get {
224                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
225                                 int ret = 0;
226                                 e.getScrollTop (out ret);
227                                 return ret;
228                         }
229                         set {
230                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
231                                 e.setScrollTop (value);
232                         }
233                 }
234                 public virtual int OffsetHeight {
235                         get {
236                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
237                                 int ret = 0;
238                                 e.getOffsetHeight (out ret);
239                                 return ret;
240                         }
241                 }
242                 public virtual int OffsetWidth {
243                         get {
244                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
245                                 int ret = 0;
246                                 e.getOffsetWidth (out ret);
247                                 return ret;
248                         }
249                 }
250                 public virtual int OffsetLeft {
251                         get {
252                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
253                                 int ret = 0;
254                                 e.getOffsetLeft (out ret);
255                                 return ret;
256                         }
257                 }
258                 public virtual int OffsetTop {
259                         get {
260                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
261                                 int ret = 0;
262                                 e.getOffsetTop (out ret);
263                                 return ret;
264                         }
265                 }
266
267                 public virtual IElement OffsetParent {
268                         get {
269                                 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
270                                 nsIDOMElement ret;
271                                 e.getOffsetParent (out ret);
272                                 if ((ret as nsIDOMHTMLElement) != null)
273                                         return new HTMLElement (this.control, ret as nsIDOMHTMLElement);
274                                 return new Element (this.control, ret);
275
276                         }
277                 }
278
279                 #endregion
280                 
281                 #region Methods
282
283                 public void Blur () {
284                         nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
285                         if (elm != null) {
286                                 elm.blur ();
287                         }
288                 }
289
290                 public void Focus () {
291                         nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
292                         if (elm != null) {
293                                 elm.focus ();
294                         }
295                 }
296
297                 public IElementCollection GetElementsByTagName (string name)
298                 {
299                         if (!resources.Contains ("GetElementsByTagName" + name)) {
300                                 nsIDOMNodeList nodes;
301                                 this.node.getElementsByTagName (storage, out nodes);
302                                 resources.Add ("GetElementsByTagName" + name, new HTMLElementCollection(control, nodes));
303                         }
304                         return resources["GetElementsByTagName" + name] as IElementCollection;
305                 }
306                 
307                 public override int GetHashCode () {
308                         return this.hashcode;
309                 }
310
311                 public virtual bool HasAttribute (string name)
312                 {
313                         bool ret;
314                         Base.StringSet (storage, name);
315                         node.hasAttribute (storage, out ret);
316                         return ret;
317                 }
318
319                 public virtual string GetAttribute (string name) {
320                         UniString ret = new UniString (String.Empty);
321                         Base.StringSet (storage, name);
322                         node.getAttribute (storage, ret.Handle);
323                         return ret.ToString ();
324                 }
325
326                 public void ScrollIntoView (bool alignWithTop) 
327                 {
328                         nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
329                         if (elm != null) {
330                                 elm.scrollIntoView (alignWithTop);
331                         }
332                 }
333                 
334                 public virtual void SetAttribute (string name, string value) {
335                         UniString strVal = new UniString (value);
336                         Base.StringSet (storage, name);
337                         node.setAttribute (storage, strVal.Handle);
338                 }               
339                 
340                 #endregion
341                 
342                 internal int Top {
343                         get {
344                                 int ret;
345                                 ((nsIDOMNSHTMLElement)this.node).getOffsetTop (out ret);
346                                 return ret;
347                         }
348                 }
349
350                 internal int Left {
351                         get {
352                                 int ret;
353                                 ((nsIDOMNSHTMLElement)this.node).getOffsetLeft (out ret);
354                                 return ret;
355                         }
356                 }
357         
358                 internal int Width {
359                         get {
360                                 int ret;
361                                 ((nsIDOMNSHTMLElement)this.node).getOffsetWidth (out ret);
362                                 return ret;
363                         }
364                 }
365
366                 internal int Height {
367                         get {
368                                 int ret;
369                                 ((nsIDOMNSHTMLElement)this.node).getOffsetHeight (out ret);
370                                 return ret;
371                         }
372                 }               
373         }
374 }