download-manager.js: fix bug resulting in un-killable special buffers
[conkeror.git] / modules / mode-line.js
blob19171597dafe82ed433d8f57522acb397aba825a
1 /**
2  * (C) Copyright 2005 Shawn Betts
3  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("mode.js");
11 define_window_local_hook("mode_line_hook");
13 function generic_element_widget_container(window, container)
15     this.window = window;
16     this.container = container;
18 define_keywords("$flex", "$align", "$class", "$crop");
19 generic_element_widget_container.prototype = {
20     add_text_widget : function (widget) {
21         keywords(arguments);
22         var flex = arguments.$flex;
23         var class_name = arguments.$class;
24         var align = arguments.$align;
25         var crop = arguments.$crop;
26         var element = create_XUL(this.window, "label");
27         if (flex != null)
28             element.setAttribute("flex", flex);
29         if (align != null)
30             element.setAttribute("align", align);
31         if (class_name == null)
32             class_name = widget.name;
33         element.setAttribute("class", class_name);
34         if (crop == null)
35             crop = widget.crop;
36         element.setAttribute("crop", crop);
37         element.conkeror_widget = new generic_widget_element(element, widget);
38         this.container.appendChild(element);
39         return element.conkeror_widget;
40     },
41     destroy : function() {
42         var children = this.container.childNodes;
43         for (var i = 0; i < children.length; ++i)
44             children.item(i).conkeror_widget.destroy();
45     }
48 function mode_line(window)
50     var element = create_XUL(window, "hbox");
51     element.setAttribute("class", "mode-line");
52     /* FIXME: this will need to changed to be buffer-local */
53     var insert_before = window.document.getElementById("minibuffer");
54     insert_before.parentNode.insertBefore(element, insert_before);
55     window.mode_line = this;
56     generic_element_widget_container.call(this, window, element);
57     mode_line_hook.run(window, this);
59 mode_line.prototype = {
60     __proto__: generic_element_widget_container.prototype,
62     uninstall: function () {
63         this.container.parentNode.removeChild(this.window.mode_line.container);
64         this.__proto__.__proto__.destroy.call(this);
65     }
69 function generic_widget_element(element, widget)
71     this.element = element;
72     this.widget = widget;
73     widget.attach(this);
76 generic_widget_element.prototype = {
77     get text () {
78         return this.element.getAttribute("value");
79     },
81     set text (v) {
82         this.element.setAttribute("value", v);
83     },
85     destroy : function () {
86         this.widget.destroy();
87     },
89     remove : function () {
90         this.element.parentNode.removeChild(this.element);
91         this.destroy();
92     }
96 function text_widget(window)
98     this.window_hooks = [];
99     this.window = window;
101 text_widget.prototype = {
102     add_hook : function (hook_name, handler)
103     {
104         var obj = this;
105         if (handler == null) handler = function () { obj.update(); }
106         add_hook.call(this.window, hook_name, handler);
107         this.window_hooks.push([hook_name, handler]);
108     },
110     view : null,
112     attach : function (view) {
113         this.view = view;
114         this.update();
115     },
117     update : function () {
118     },
120     destroy : function ()
121     {
122         for each (let i in this.window_hooks) {
123             remove_hook.call(this.window, i[0], i[1]);
124         }
125     },
127     remove : function ()
128     {
129         this.view.remove();
130     }
133 define_global_window_mode("mode_line", "window_initialize_early_hook");
135 function current_buffer_name_widget(window) {
136     this.name = "current-buffer-name-widget";
137     text_widget.call(this, window);
138     this.crop = "end";
139     this.add_hook("current_content_buffer_location_change_hook");
140     this.add_hook("select_buffer_hook");
142 current_buffer_name_widget.prototype.__proto__ = text_widget.prototype;
143 current_buffer_name_widget.prototype.update = function () {
144     this.view.text = this.window.buffers.current.description;
147 function current_buffer_scroll_position_widget(window) {
148     this.name = "current-buffer-scroll-position-widget";
149     text_widget.call(this, window);
150     this.add_hook("current_buffer_scroll_hook");
151     this.add_hook("select_buffer_hook");
152     this.add_hook("current_content_buffer_location_change_hook");
153     this.add_hook("current_content_buffer_focus_change_hook");
155 current_buffer_scroll_position_widget.prototype.__proto__ = text_widget.prototype;
156 current_buffer_scroll_position_widget.prototype.update = function () {
157     var b = this.window.buffers.current;
158     var scrollX, scrollY, scrollMaxX, scrollMaxY;
159     if (b instanceof content_buffer)
160     {
161         var w = b.focused_frame;
162         scrollX = w.scrollX;
163         scrollY = w.scrollY;
164         scrollMaxX = w.scrollMaxX;
165         scrollMaxY = w.scrollMaxY;
166     } else
167     {
168         scrollX = b.scrollX;
169         scrollY = b.scrollY;
170         scrollMaxX = b.scrollMaxX;
171         scrollMaxY = b.scrollMaxY;
172     }
173     var x = scrollMaxX == 0 ? 100 : Math.round(scrollX / scrollMaxX * 100);
174     var y = scrollMaxY == 0 ? 100 : Math.round(scrollY / scrollMaxY * 100);
175     this.view.text = "(" + x + ", " + y + ")";
178 function clock_widget(window)
180     this.name = "clock-widget";
181     text_widget.call(this, window);
182     var obj = this;
183     this.timer_ID = window.setInterval(function () { obj.update(); }, 60000);
185 clock_widget.prototype.__proto__ = text_widget.prototype;
186 clock_widget.prototype.update = function () {
187     var time = new Date();
188     var hours = time.getHours();
189     var mins = time.getMinutes();
190     this.view.text = (hours<10 ? "0" + hours:hours) + ":" + (mins<10 ?"0" +mins:mins);
192 clock_widget.prototype.destroy = function () {
193     this.window.clearTimeout(this.timer_ID);
196 function mode_line_adder(widget_constructor) {
197     return function (window) { window.mode_line.add_text_widget(new widget_constructor(window)); }
200 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget));
201 add_hook("mode_line_hook", mode_line_adder(clock_widget));
202 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget));
204 mode_line_mode(true);