2 * (C) Copyright 2007 John J. Foerch
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5 * Use, modification, and distribution are subject to the terms specified in the
11 var define_window_local_hook = simple_local_hook_definer();
12 var define_window_local_coroutine_hook = simple_local_coroutine_hook_definer();
15 define_hook("make_window_hook");
17 var window_watcher = Cc["@mozilla.org/embedcomp/window-watcher;1"]
18 .getService(Ci.nsIWindowWatcher);
20 function generate_new_window_tag (tag) {
22 var exact_match = false;
23 var en = window_watcher.getWindowEnumerator();
28 re = new RegExp ("^" + tag + "<(\\d+)>$");
30 re = new RegExp ("^(\\d+)$");
31 while (en.hasMoreElements()) {
32 var w = en.getNext().QueryInterface(Ci.nsIDOMWindow);
34 if (tag && w.tag == tag) {
38 var re_result = re.exec(w.tag);
40 existing.push(re_result[1]);
43 if (tag && ! exact_match)
46 existing.sort(function (a, b) { return a - b; });
49 for (var i = 0; i < existing.length; i++) {
50 if (existing[i] < n) continue;
51 if (existing[i] == n) { n++; continue; }
55 return tag + "<" + n + ">";
60 function make_chrome_window (chrome_uri, args) {
61 return window_watcher.openWindow(null, chrome_uri, "_blank",
62 "chrome,dialog=no,all,resizable", args);
65 var conkeror_chrome_uri = "chrome://conkeror-gui/content/conkeror.xul";
68 * This is called by Conkeror to explicitly create a conkeror.xul window.
69 * However, windows are sometimes created internally be Mozilla, for example due
70 * to the logic in content-buffer.js:browser_dom_window.openURI. In that case,
71 * make_window is not called and Conkeror sees the window for the first time in
72 * window_initialize, which needs to take care of all of the initialization that
73 * would normally happen in make_window.
75 function make_window (initial_buffer_creator, tag) {
76 var args = { tag: tag,
77 initial_buffer_creator: initial_buffer_creator };
78 var result = make_chrome_window(conkeror_chrome_uri, null);
80 make_window_hook.run(result);
81 window_install_close_intercept(result);
85 function window_install_close_intercept (window) {
86 var close = window.close;
87 window.close = function () {
88 function attempt_close () {
89 var res = yield window_before_close_hook.run(window);
91 window_close_hook.run(window);
95 co_call(attempt_close());
99 function get_window_by_tag (tag) {
100 var en = window_watcher.getWindowEnumerator();
101 while (en.hasMoreElements()) {
102 var w = en.getNext().QueryInterface(Ci.nsIDOMWindow);
103 if ('tag' in w && w.tag == tag)
109 /* FIXME: decide if this should include not-fully-initialized windows */
110 function for_each_window (func) {
111 var en = window_watcher.getWindowEnumerator();
112 while (en.hasMoreElements()) {
113 var w = en.getNext().QueryInterface(Ci.nsIDOMWindow);
119 function get_recent_conkeror_window () {
120 var wm = Cc['@mozilla.org/appshell/window-mediator;1']
121 .getService(Ci.nsIWindowMediator);
122 var window = wm.getMostRecentWindow("navigator:browser");
123 if (window && ("conkeror" in window))
125 var en = window_watcher.getWindowEnumerator();
126 while (en.hasMoreElements()) {
127 window = en.getNext().QueryInterface(Ci.nsIDOMWindow);
128 if ('conkeror' in window)
134 define_window_local_hook("window_initialize_early_hook");
135 define_window_local_hook("window_initialize_hook");
136 define_window_local_hook("window_initialize_late_hook");
138 var window_extra_argument_list = [];
140 define_variable("window_extra_argument_max_delay", 100);
143 * Called by window_initialize. If the window was created using make_window (as
144 * indicated by window.args having been set), nothing needs to be done.
145 * Otherwise, we need to perform the setup that would otherwise occur in
148 function window_setup_args (window) {
149 if (window.args != null)
152 window_install_close_intercept(window);
155 var cur_time = Date.now();
158 for (i = 0; i < window_extra_argument_list.length; ++i) {
159 var a = window_extra_argument_list[i];
160 if (a.time > cur_time - window_extra_argument_max_delay) {
167 window_extra_argument_list = window_extra_argument_list.slice(i);
172 window.args = result;
175 function window_set_extra_arguments (args) {
176 args.time = Date.now();
177 window_extra_argument_list.push(args);
180 function window_get_this_browser () {
181 return this.buffers.current.browser;
184 function window_initialize (window) {
185 window.conkeror = conkeror;
187 // Used by get_window_from_frame to get an unwrapped window reference
188 window.escape_wrapper = function (x) { x(window); };
190 window_setup_args(window);
194 if ('tag' in window.args)
195 tag = window.args.tag;
196 window.tag = generate_new_window_tag(tag);
198 // Add a getBrowser() and content to help certain extensions designed
199 // for Firefox work with conkeror
200 window.getBrowser = window_get_this_browser;
201 window.__defineGetter__('content',
203 return this.buffers.current.browser.contentWindow;
206 // Tell Mozilla the window_initialize_late_hook to run after a timeout
208 // Do this here, before doing the rest of the window
209 // initialization, so that window_initialize_late_hook will be run
210 // prior to any other "late" hooks set up by other modules in a
211 // function registered in window_initialize{_early,}_hook.
212 window.setTimeout(function () {
213 window_initialize_late_hook.run(window);
214 delete window.window_initialize_late_hook; // used only once
215 delete window.args; // get rid of args
218 window_initialize_early_hook.run(window);
219 delete window.window_initialize_early_hook; // used only once
221 window_initialize_hook.run(window);
222 delete window.window_initialize_hook; // used only once
224 window.addEventListener("close",
226 event.preventDefault();
227 event.stopPropagation();
233 define_window_local_coroutine_hook("window_before_close_hook",
234 RUN_HOOK_UNTIL_FAILURE);
235 define_window_local_hook("window_close_hook", RUN_HOOK);
241 function define_global_window_mode (name, hook_name) {
243 function install (window) {
245 throw new Error(name + " already initialized for window");
246 window[name] = new conkeror[name](window);
248 function uninstall (window) {
250 throw new Error(name + " not initialized for window");
251 window[name].uninstall();
254 define_global_mode(name + "_mode",
255 function () { // enable
256 add_hook(hook_name, install);
257 for_each_window(install);
259 function () { // disable
260 remove_hook(hook_name, install);
261 for_each_window(uninstall);
263 forward_keywords(arguments));
265 ignore_function_for_get_caller_source_code_reference("define_global_window_mode");
274 * Default tile formatter. The page url is ignored. If there is a
275 * page_title, returns: "Page title - Conkeror". Otherwise, it
276 * returns just: "Conkeror".
278 function default_title_formatter (window) {
279 var page_title = window.buffers.current.title;
281 if (page_title && page_title.length > 0)
282 return page_title + " - Conkeror";
287 var title_format_fn = null;
289 function set_window_title (window) {
290 window.document.title = title_format_fn(window);
293 function init_window_title () {
294 title_format_fn = default_title_formatter;
296 add_hook("window_initialize_late_hook", set_window_title);
297 add_hook("current_content_buffer_location_change_hook",
299 set_window_title(buffer.window);
301 add_hook("select_buffer_hook",
303 set_window_title(buffer.window);
305 add_hook("current_buffer_title_change_hook",
307 set_window_title(buffer.window);
314 function call_builtin_command (window, command, clear_mark) {
315 var m = window.minibuffer;
316 if (m.active && m._input_mode_enabled) {
317 m._restore_normal_state();
318 var e = m.input_element;
319 var c = e.controllers.getControllerForCommand(command);
321 if (c && c.isCommandEnabled(command))
322 c.doCommand(command);
327 m.current_state.mark_active = false;
329 function attempt_command (element) {
331 if (element.controllers
332 && (c = element.controllers.getControllerForCommand(command)) != null
333 && c.isCommandEnabled(command))
336 c.doCommand(command);
341 window.buffers.current.mark_active = false;
346 var element = window.buffers.current.focused_element;
347 if (element && attempt_command(element, command))
349 var win = window.buffers.current.focused_frame;
351 if (attempt_command(win, command))
353 if (!win.parent || win == win.parent)
362 * window_set_full_screen sets or toggles the fullScreen and hideChrome
363 * properties of the given window. When fullscreen is a boolean, it sets
364 * it to that value. When it is null or not given, it toggles the current
367 function window_set_full_screen (window, fullscreen) {
368 if (fullscreen === true || fullscreen === false) {
369 window.fullScreen = fullscreen;
370 window.hideChrome = fullscreen;
372 window.fullScreen = ! window.fullScreen;
373 window.hideChrome = window.fullScreen;