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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2008 Novell, Inc.
23 // Andreia Gaita (avidigal@novell.com)
27 using System
.Collections
;
28 using Mono
.WebBrowser
;
29 using Mono
.WebBrowser
.DOM
;
31 namespace Mono
.Mozilla
.DOM
33 internal class WindowCollection
: DOMObject
, IWindowCollection
35 protected nsIDOMWindowCollection unmanagedWindows
;
36 protected IWindow
[] windows
;
37 protected int windowCount
;
39 public WindowCollection (WebBrowser control
, nsIDOMWindowCollection windowCol
) : base (control
)
41 if (control
.platform
!= control
.enginePlatform
)
42 unmanagedWindows
= nsDOMWindowCollection
.GetProxy (control
, windowCol
);
44 unmanagedWindows
= windowCol
;
47 public WindowCollection (WebBrowser control
) : base (control
)
49 windows
= new Window
[0];
52 #region IDisposable Members
53 protected override void Dispose (bool disposing
)
60 base.Dispose(disposing
);
65 protected void Clear ()
67 if (windows
!= null) {
68 for (int i
= 0; i
< windowCount
; i
++) {
80 unmanagedWindows
.getLength (out count
);
81 Window
[] tmpwindows
= new Window
[count
];
82 for (int i
= 0; i
< count
;i
++) {
84 unmanagedWindows
.item ((uint)i
, out window
);
85 tmpwindows
[windowCount
++] = new Window (control
, (nsIDOMWindow
)window
);
87 windows
= new Window
[windowCount
];
88 Array
.Copy (tmpwindows
, windows
, windowCount
);
92 #region IEnumerable members
93 public IEnumerator
GetEnumerator ()
95 return new WindowEnumerator (this);
99 #region ICollection members
100 public void CopyTo (Array dest
, int index
)
102 if (windows
!= null) {
103 Array
.Copy (windows
, 0, dest
, index
, windowCount
);
109 if (unmanagedWindows
!= null && windows
== null)
115 object ICollection
.SyncRoot
{
119 bool ICollection
.IsSynchronized
{
120 get { return false; }
125 #region IList members
126 public bool IsReadOnly
131 bool IList
.IsFixedSize
136 void IList
.RemoveAt (int index
)
141 public void RemoveAt (int index
)
143 if (index
> windowCount
|| index
< 0)
145 Array
.Copy (windows
, index
+ 1, windows
, index
, (windowCount
- index
) - 1);
147 windows
[windowCount
] = null;
150 public void Remove (IWindow window
)
152 this.RemoveAt (IndexOf (window
));
155 void IList
.Remove (object window
)
157 Remove (window
as IWindow
);
160 public void Insert (int index
, IWindow
value)
162 if (index
> windowCount
)
164 IWindow
[] tmp
= new Window
[windowCount
+1];
166 Array
.Copy (windows
, 0, tmp
, 0, index
);
168 if (index
< windowCount
)
169 Array
.Copy (windows
, index
, tmp
, index
+ 1, (windowCount
- index
));
174 void IList
.Insert (int index
, object value)
176 this.Insert (index
, value as IWindow
);
179 public int IndexOf (IWindow window
)
181 return Array
.IndexOf (windows
, window
);
184 int IList
.IndexOf (object window
)
186 return IndexOf (window
as IWindow
);
190 public bool Contains (IWindow window
)
192 return this.IndexOf (window
) != -1;
195 bool IList
.Contains (object window
)
197 return Contains (window
as IWindow
);
205 public int Add (IWindow window
)
207 this.Insert (windowCount
+ 1, window
as IWindow
);
208 return windowCount
- 1;
211 int IList
.Add (object window
)
213 return Add (window
as IWindow
);
216 object IList
.this [int index
] {
221 this [index
] = value as IWindow
;
225 public IWindow
this [int index
] {
227 if (index
< 0 || index
>= windowCount
)
228 throw new ArgumentOutOfRangeException ("index");
229 return windows
[index
];
232 if (index
< 0 || index
>= windowCount
)
233 throw new ArgumentOutOfRangeException ("index");
234 windows
[index
] = value as IWindow
;
240 public override int GetHashCode () {
241 if (this.unmanagedWindows
!= null)
242 return this.unmanagedWindows
.GetHashCode ();
243 return base.GetHashCode ();
246 internal class WindowEnumerator
: IEnumerator
{
248 private WindowCollection collection
;
249 private int index
= -1;
251 public WindowEnumerator (WindowCollection collection
)
253 this.collection
= collection
;
256 public object Current
{
260 return collection
[index
];
264 public bool MoveNext ()
266 if (index
+ 1 >= collection
.Count
)