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";
67 function make_window (initial_buffer_creator, tag) {
68 var args = { tag: tag,
69 initial_buffer_creator: initial_buffer_creator };
70 var result = make_chrome_window(conkeror_chrome_uri, null);
72 make_window_hook.run(result);
73 var close = result.close;
74 result.close = function () {
75 function attempt_close () {
76 var res = yield window_before_close_hook.run(result);
78 window_close_hook.run(result);
82 co_call(attempt_close());
87 function get_window_by_tag (tag) {
88 var en = window_watcher.getWindowEnumerator();
89 while (en.hasMoreElements()) {
90 var w = en.getNext().QueryInterface(Ci.nsIDOMWindow);
91 if ('tag' in w && w.tag == tag)
97 /* FIXME: decide if this should include not-fully-initialized windows */
98 function for_each_window (func) {
99 var en = window_watcher.getWindowEnumerator();
100 while (en.hasMoreElements()) {
101 var w = en.getNext().QueryInterface(Ci.nsIDOMWindow);
107 function get_recent_conkeror_window () {
108 var wm = Cc['@mozilla.org/appshell/window-mediator;1']
109 .getService(Ci.nsIWindowMediator);
110 var window = wm.getMostRecentWindow("navigator:browser");
111 if (window && ("conkeror" in window))
113 var en = window_watcher.getWindowEnumerator();
114 while (en.hasMoreElements()) {
115 window = en.getNext().QueryInterface(Ci.nsIDOMWindow);
116 if ('conkeror' in window)
122 define_window_local_hook("window_initialize_early_hook");
123 define_window_local_hook("window_initialize_hook");
124 define_window_local_hook("window_initialize_late_hook");
126 var window_extra_argument_list = [];
128 define_variable("window_extra_argument_max_delay", 100);
130 function window_setup_args (window) {
131 if (window.args != null)
134 var cur_time = Date.now();
137 for (i = 0; i < window_extra_argument_list.length; ++i) {
138 var a = window_extra_argument_list[i];
139 if (a.time > cur_time - window_extra_argument_max_delay) {
146 window_extra_argument_list = window_extra_argument_list.slice(i);
151 window.args = result;
154 function window_set_extra_arguments (args) {
155 args.time = Date.now();
156 window_extra_argument_list.push(args);
159 function window_get_this_browser () {
160 return this.buffers.current.browser;
163 function window_initialize (window) {
164 window.conkeror = conkeror;
166 // Used by get_window_from_frame to get an unwrapped window reference
167 window.escape_wrapper = function (x) { x(window); };
169 window_setup_args(window);
173 if ('tag' in window.args)
174 tag = window.args.tag;
175 window.tag = generate_new_window_tag(tag);
177 // Add a getBrowser() and content to help certain extensions designed
178 // for Firefox work with conkeror
179 window.getBrowser = window_get_this_browser;
180 window.__defineGetter__('content',
182 return this.buffers.current.browser.contentWindow;
185 window_initialize_early_hook.run(window);
186 delete window.window_initialize_early_hook; // used only once
188 window_initialize_hook.run(window);
189 delete window.window_initialize_hook; // used only once
191 window.setTimeout(function () {
192 window_initialize_late_hook.run(window);
193 delete window.window_initialize_late_hook; // used only once
194 delete window.args; // get rid of args
197 window.addEventListener("close",
199 event.preventDefault();
200 event.stopPropagation();
206 define_window_local_coroutine_hook("window_before_close_hook",
207 RUN_HOOK_UNTIL_FAILURE);
208 define_window_local_hook("window_close_hook", RUN_HOOK);
214 function define_global_window_mode (name, hook_name) {
216 function install (window) {
218 throw new Error(name + " already initialized for window");
219 window[name] = new conkeror[name](window);
221 function uninstall (window) {
223 throw new Error(name + " not initialized for window");
224 window[name].uninstall();
227 define_global_mode(name + "_mode",
228 function () { // enable
229 add_hook(hook_name, install);
230 for_each_window(install);
232 function () { // disable
233 remove_hook(hook_name, install);
234 for_each_window(uninstall);
236 forward_keywords(arguments));
238 ignore_function_for_get_caller_source_code_reference("define_global_window_mode");
247 * Default tile formatter. The page url is ignored. If there is a
248 * page_title, returns: "Page title - Conkeror". Otherwise, it
249 * returns just: "Conkeror".
251 function default_title_formatter (window) {
252 var page_title = window.buffers.current.title;
254 if (page_title && page_title.length > 0)
255 return page_title + " - Conkeror";
260 var title_format_fn = null;
262 function set_window_title (window) {
263 window.document.title = title_format_fn(window);
266 function init_window_title () {
267 title_format_fn = default_title_formatter;
269 add_hook("window_initialize_late_hook", set_window_title);
270 add_hook("current_content_buffer_location_change_hook",
272 set_window_title(buffer.window);
274 add_hook("select_buffer_hook",
276 set_window_title(buffer.window);
278 add_hook("current_buffer_title_change_hook",
280 set_window_title(buffer.window);
287 function call_builtin_command (window, command, clear_mark) {
288 var m = window.minibuffer;
289 if (m.active && m._input_mode_enabled) {
290 m._restore_normal_state();
291 var e = m.input_element;
292 var c = e.controllers.getControllerForCommand(command);
294 if (c && c.isCommandEnabled(command))
295 c.doCommand(command);
300 m.current_state.mark_active = false;
302 function attempt_command (element) {
304 if (element.controllers
305 && (c = element.controllers.getControllerForCommand(command)) != null
306 && c.isCommandEnabled(command))
309 c.doCommand(command);
314 window.buffers.current.mark_active = false;
319 var element = window.buffers.current.focused_element;
320 if (element && attempt_command(element, command))
322 var win = window.buffers.current.focused_frame;
324 if (attempt_command(win, command))
326 if (!win.parent || win == win.parent)
335 * window_set_full_screen sets or toggles the fullScreen and hideChrome
336 * properties of the given window. When fullscreen is a boolean, it sets
337 * it to that value. When it is null or not given, it toggles the current
340 function window_set_full_screen (window, fullscreen) {
341 if (fullscreen === true || fullscreen === false) {
342 window.fullScreen = fullscreen;
343 window.hideChrome = fullscreen;
345 window.fullScreen = ! window.fullScreen;
346 window.hideChrome = window.fullScreen;