NsIWindowMediator: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(moved to devmo)
 
Line 1: Line 1:
{{extdev}}
See [http://developer.mozilla.org/en/docs/nsIWindowMediator nsIWindowMediator] at the Mozilla Developer Center.
{{wrongtitle|title=nsIWindowMediator}}


Window mediator is Mozilla component that keeps track of open windows. It's accessed through <code>nsIWindowMediator</code> interface. Two most common uses of <code>nsIWindowMediator</code> are:
[[Category:Redirects]]
# Getting the most recent / any window of given type.
# Enumerating all windows of given type.
 
In the examples below <code>type</code> specifies the type of windows you want to search. You can specify a type of your own window by creating an <code>windowtype</code> attribute on the document element (the top-level one, e.g. <code><window></code> or <code>dialog</code>).
 
Browser windows have <code>navigator:browser</code> window type. To search all windows, regardless of their type, pass an empty string, <code>""</code>.
 
== Getting most recent window ==
The following code is useful when you need any of the windows of given type, or to check if a window of a particular type (e.g. your extension's Options dialog) is already open.
 
<code>getMostRecentWindow</code> returns a <code>ChromeWindow</code> object, or <code>null</code>, if there are no windows of given type open.
<pre>
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                  .getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow(type);
</pre>
 
== Enumerating windows ==
The following code can be used if you need to do something for each open window of a particular type. For example you could use it in "OK" handler of your Options dialog to apply the new settings to each open browser window.
 
<pre>
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                  .getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator(type);
while(enumerator.hasMoreElements()) {
  var win = enumerator.getNext();
  // |win| is [Object ChromeWindow] (just like |window|), do something with it
}
</pre>
 
'''Note:''' in <code>nsIWindowMediator</code>'s reference page the return type of <code>getMostRecentWindow</code> and the type of enumerator's elements is said to be <code>nsIDOMWindow</code>/<code>nsIDOMWindowInternal</code>. In fact, those methods usually (always?) return a <code>ChromeWindow</code> object, implementing both of those interfaces and a few others, when called from JavaScript code. The global <code>window</code> object, you're probably familiar with, is of <code>ChromeWindow</code> type.
 
== References ==
* XUL Planet:
** [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference]
** [http://xulplanet.com/references/objref/ChromeWindow.html ChromeWindow object reference]
* Devmo
** [http://developer.mozilla.org/en/docs/Working_with_windows_in_chrome_code Working with windows in chrome code]
 
[[Category:Example code|nsIWindowMediator]] [[Category:XPCOM example code|nsIWindowMediator]]

Latest revision as of 21:00, 19 September 2006

See nsIWindowMediator at the Mozilla Developer Center.