2 * (C) Copyright 2005 Shawn Betts
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4 * (C) Copyright 2010-2011 John J. Foerch
6 * Use, modification, and distribution are subject to the terms specified in the
12 define_window_local_hook("mode_line_hook");
14 define_keywords("$flex", "$align", "$class", "$crop");
15 function generic_element_widget_container (window, container) {
17 this.container = container;
19 generic_element_widget_container.prototype = {
20 constructor: generic_element_widget_container,
21 add_text_widget: function (widget) {
22 keywords(arguments, $flex = widget.flex,
23 $class = widget.class_name, $crop = widget.crop);
24 var flex = arguments.$flex;
25 var class_name = arguments.$class;
26 var align = arguments.$align;
27 var crop = arguments.$crop;
28 var element = create_XUL(this.window, "label");
30 element.setAttribute("flex", flex);
32 element.setAttribute("align", align);
34 element.setAttribute("class", class_name);
36 element.setAttribute("crop", crop);
37 return this.add_widget(widget, element);
39 add_widget: function (widget, element) {
40 element.conkeror_widget = new generic_widget_element(element, widget);
41 this.container.appendChild(element);
42 return element.conkeror_widget;
44 destroy: function () {
45 var children = this.container.childNodes;
46 for (var i = 0, nchildren = children.length; i < nchildren; ++i)
47 children.item(i).conkeror_widget.destroy();
51 function mode_line (window) {
52 var element = create_XUL(window, "hbox");
53 element.setAttribute("class", "mode-line");
54 /* FIXME: this will need to changed to be buffer-local */
55 var insert_before = window.document.getElementById("minibuffer");
56 insert_before.parentNode.insertBefore(element, insert_before);
57 window.mode_line = this;
58 generic_element_widget_container.call(this, window, element);
59 mode_line_hook.run(window, this);
61 mode_line.prototype = {
62 constructor: mode_line,
63 __proto__: generic_element_widget_container.prototype,
64 toString: function () "#<mode_line>",
66 uninstall: function () {
67 this.container.parentNode.removeChild(this.window.mode_line.container);
68 generic_element_widget_container.prototype.destroy.call(this);
73 function generic_widget_element (element, widget) {
74 this.element = element;
78 generic_widget_element.prototype = {
79 constructor: generic_widget_element,
81 return this.element.getAttribute("value");
85 this.element.setAttribute("value", v);
88 destroy: function () {
89 this.widget.destroy();
93 this.element.parentNode.removeChild(this.element);
99 function text_widget (window) {
100 this.window_hooks = [];
101 this.window = window;
103 text_widget.prototype = {
104 constructor: text_widget,
105 add_hook: function (hook_name, handler) {
108 handler = function () { obj.update(); };
109 add_hook.call(this.window, hook_name, handler);
110 this.window_hooks.push([hook_name, handler]);
115 attach: function (view) {
120 update: function () {},
122 destroy: function () {
123 for each (let i in this.window_hooks) {
124 remove_hook.call(this.window, i[0], i[1]);
128 remove: function () {
134 define_global_window_mode("mode_line", "window_initialize_early_hook");
138 * current_buffer_name_widget shows the name of the current buffer.
140 function current_buffer_name_widget (window) {
141 this.class_name = "current-buffer-name-widget";
142 text_widget.call(this, window);
145 this.add_hook("current_content_buffer_location_change_hook");
146 this.add_hook("select_buffer_hook");
148 current_buffer_name_widget.prototype = {
149 constructor: current_buffer_name_widget,
150 __proto__: text_widget.prototype,
151 update: function () {
152 this.view.text = this.window.buffers.current.description;
158 * current_buffer_scroll_position_widget shows the vertical and horizontal
159 * scroll position of the current buffer.
161 function current_buffer_scroll_position_widget (window) {
162 this.class_name = "current-buffer-scroll-position-widget";
163 text_widget.call(this, window);
164 this.add_hook("current_buffer_scroll_hook");
165 this.add_hook("select_buffer_hook");
166 this.add_hook("current_content_buffer_location_change_hook");
167 this.add_hook("current_content_buffer_focus_change_hook");
168 this.add_hook("current_special_buffer_generated_hook");
170 current_buffer_scroll_position_widget.prototype = {
171 constructor: current_buffer_scroll_position_widget,
172 __proto__: text_widget.prototype,
173 update: function () {
174 var b = this.window.buffers.current;
175 var scrollX, scrollY, scrollMaxX, scrollMaxY;
176 var w = b.focused_frame;
179 scrollMaxX = w.scrollMaxX;
180 scrollMaxY = w.scrollMaxY;
181 var x = scrollMaxX == 0 ? 100 : Math.round(scrollX / scrollMaxX * 100);
182 var y = scrollMaxY == 0 ? 100 : Math.round(scrollY / scrollMaxY * 100);
183 this.view.text = "(" + x + ", " + y + ")";
188 define_variable("clock_time_format", "%R",
189 "Format string for the mode-line clock widget.\n"+
190 "It takes the same format as strftime() in C. "+
191 "See http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html "+
194 function clock_widget (window) {
195 this.class_name = "clock-widget";
196 text_widget.call(this, window);
198 this.do_update = function () { obj.update(); };
199 // todo: use one timer for multiple clock widgets
200 this.timer_ID = window.setTimeout(this.do_update, 0);
201 this.timer_timeout = true;
203 clock_widget.prototype.__proto__ = text_widget.prototype;
204 clock_widget.prototype.update = function () {
205 var time = new Date();
206 this.view.text = time.toLocaleFormat(clock_time_format);
207 if (time.getSeconds() > 0 || time.getMilliseconds() > 100) {
208 this.window.clearTimeout(this.timer_ID);
209 var time = time.getSeconds() * 1000 + time.getMilliseconds();
211 this.timer_ID = this.window.setTimeout(this.do_update, time);
212 this.timer_timeout = true;
213 } else if (this.timer_timeout) {
214 this.window.clearTimeout(this.timer_ID);
215 this.timer_ID = this.window.setInterval(this.do_update, 60000);
216 this.timer_timeout = false;
219 clock_widget.prototype.destroy = function () {
220 this.window.clearTimeout(this.timer_ID);
223 function buffer_count_widget (window) {
224 this.class_name = "buffer-count-widget";
225 text_widget.call(this, window);
226 this.add_hook("select_buffer_hook");
227 this.add_hook("create_buffer_hook");
228 this.add_hook("kill_buffer_hook");
229 this.add_hook("move_buffer_hook");
231 buffer_count_widget.prototype.__proto__ = text_widget.prototype;
232 buffer_count_widget.prototype.update = function () {
233 this.view.text = ("[" + (this.window.buffers.selected_index+1) + "/" +
234 this.window.buffers.count + "]");
237 function loading_count_widget (window) {
238 this.class_name = "loading-count-widget";
239 text_widget.call(this, window);
241 this.add_hook("content_buffer_started_loading_hook");
242 this.add_hook("content_buffer_finished_loading_hook");
243 this.add_hook("kill_buffer_hook");
245 loading_count_widget.prototype.__proto__ = text_widget.prototype;
246 loading_count_widget.prototype.update = function () {
248 for_each_buffer(function (b) { if (b.loading) count++; });
250 this.view.text = "(" + count + " loading)";
257 * buffer_icon_widget shows the icon for the current buffer, if any, in
260 function buffer_icon_widget (window) {
261 this.class_name = "buffer-icon-widget";
262 text_widget.call(this, window);
263 this.add_hook("current_buffer_icon_change_hook");
264 this.add_hook("select_buffer_hook");
266 buffer_icon_widget.prototype.__proto__ = text_widget.prototype;
267 buffer_icon_widget.prototype.update = function () {
268 var buffer = this.window.buffers.current;
270 this.view.element.setAttribute("src", buffer.icon);
272 this.view.element.removeAttribute("src");
274 buffer_icon_widget.mode_line_adder = function (window) {
275 var element = create_XUL(window, "image");
276 element.setAttribute("class", "buffer-icon-widget");
277 element.setAttribute("width", "16");
278 element.setAttribute("height", "16");
279 window.mode_line.add_widget(new buffer_icon_widget(window), element);
284 * downloads_status_widget shows the number of active downloads.
286 function downloads_status_widget (window) {
287 text_widget.call(this, window);
289 this.updater = function () { obj.update(); };
290 add_hook("download_progress_change_hook", this.updater);
291 add_hook("download_state_change_hook", this.updater);
293 downloads_status_widget.prototype = {
294 constructor: downloads_status_widget,
295 __proto__: text_widget.prototype,
296 class_name: "downloads-status-widget",
297 update: function (info) {
298 this.view.text = download_manager_service.activeDownloadCount;
300 destroy: function () {
301 remove_hook("download_progress_change_hook", this.updater);
302 remove_hook("download_state_change_hook", this.updater);
310 function zoom_widget (window) {
311 text_widget.call(this, window);
312 this.add_hook("select_buffer_hook");
313 this.add_hook("current_buffer_zoom_hook");
314 this.add_hook("current_content_buffer_location_change_hook");
315 this.add_hook("current_content_buffer_finished_loading_hook");
317 zoom_widget.prototype = {
318 constructor: zoom_widget,
319 __proto__: text_widget.prototype,
320 class_name: "zoom-widget",
321 update: function () {
322 var buffer = this.window.buffers.current;
323 var t = Math.round(buffer.markup_document_viewer.textZoom * 100);
324 var f = Math.round(buffer.markup_document_viewer.fullZoom * 100);
325 var str = t + "%/" + f + "%";
327 var doc = buffer.document.QueryInterface(Ci.nsIImageDocument);
328 if (doc.imageIsResized) {
329 if (t == 100 && f == 100)
332 str += "/zoom-to-fit";
335 this.view.text = str;
340 function mode_line_adder (widget_constructor) {
341 if (!('mode_line_adder' in widget_constructor))
342 widget_constructor.mode_line_adder = function (window) {
343 window.mode_line.add_text_widget(new widget_constructor(window));
345 return widget_constructor.mode_line_adder;
348 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget));
349 add_hook("mode_line_hook", mode_line_adder(clock_widget));
350 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget));
352 mode_line_mode(true);
354 provide("mode-line");