Improved "mode" infrastructure, including support for page modes
[conkeror.git] / modules / mode-line.js
blob98bace9176b9df02bf7849b5625d2b2b5698811f
1 require("mode.js");
3 define_window_local_hook("mode_line_hook");
5 function generic_element_widget_container(window, container)
7     this.window = window;
8     this.container = container;
10 define_keywords("$flex", "$align", "$class", "$crop");
11 generic_element_widget_container.prototype = {
12     add_text_widget : function (widget) {
13         keywords(arguments);
14         var flex = arguments.$flex;
15         var class_name = arguments.$class;
16         var align = arguments.$align;
17         var crop = arguments.$crop;
18         var element = create_XUL(this.window, "label");
19         if (flex != null)
20             element.setAttribute("flex", flex);
21         if (align != null)
22             element.setAttribute("align", align);
23         if (class_name == null)
24             class_name = widget.name;
25         element.setAttribute("class", class_name);
26         if (crop == null)
27             crop = widget.crop;
28         element.setAttribute("crop", crop);
29         element.conkeror_widget = new generic_widget_element(element, widget);
30         this.container.appendChild(element);
31         return element.conkeror_widget;
32     },
33     destroy : function() {
34         var children = this.container.childNodes;
35         for (var i = 0; i < children.length; ++i)
36             children.item(i).conkeror_widget.destroy();
37     }
40 function mode_line(window)
42     var element = create_XUL(window, "hbox");
43     element.setAttribute("class", "mode-line");
44     /* FIXME: this will need to changed to be buffer-local */
45     var insert_before = window.document.getElementById("minibuffer");
46     insert_before.parentNode.insertBefore(element, insert_before);
47     window.mode_line = this;
48     generic_element_widget_container.call(this, window, element);
49     mode_line_hook.run(window, this);
51 mode_line.prototype = {
52     __proto__: generic_element_widget_container.prototype,
54     uninstall: function () {
55         this.container.parentNode.removeChild(this.window.mode_line.container);
56         this.__proto__.__proto__.destroy.call(this);
57     }
61 function generic_widget_element(element, widget)
63     this.element = element;
64     this.widget = widget;
65     widget.attach(this);
68 generic_widget_element.prototype = {
69     get text () {
70         return this.element.getAttribute("value");
71     },
73     set text (v) {
74         this.element.setAttribute("value", v);
75     },
77     destroy : function () {
78         this.widget.destroy();
79     },
81     remove : function () {
82         this.element.parentNode.removeChild(this.element);
83         this.destroy();
84     }
88 function text_widget(window)
90     this.window_hooks = [];
91     this.window = window;
93 text_widget.prototype = {
94     add_hook : function (hook_name, handler)
95     {
96         var obj = this;
97         if (handler == null) handler = function () { obj.update(); }
98         add_hook.call(this.window, hook_name, handler);
99         this.window_hooks.push(handler);
100     },
102     view : null,
104     attach : function (view) {
105         this.view = view;
106         this.update();
107     },
109     update : function () {
110     },
112     destroy : function ()
113     {
114         for (var i = 0; i < this.window_hooks.length; ++i)
115             remove_hook.call(this.window, this.window_hooks[i]);
116     },
118     remove : function ()
119     {
120         this.view.remove();
121     }
124 define_global_window_mode("mode_line", "window_initialize_early_hook");
126 function current_buffer_name_widget(window) {
127     this.name = "current-buffer-name-widget";
128     text_widget.call(this, window);
129     this.crop = "end";
130     this.add_hook("current_content_buffer_location_change_hook");
131     this.add_hook("select_buffer_hook");
133 current_buffer_name_widget.prototype.__proto__ = text_widget.prototype;
134 current_buffer_name_widget.prototype.update = function () {
135     this.view.text = this.window.buffers.current.description;
138 function current_buffer_scroll_position_widget(window) {
139     this.name = "current-buffer-scroll-position-widget";
140     text_widget.call(this, window);
141     this.add_hook("current_buffer_scroll_hook");
142     this.add_hook("select_buffer_hook");
143     this.add_hook("current_content_buffer_location_change_hook");
144     this.add_hook("current_content_buffer_focus_change_hook");
146 current_buffer_scroll_position_widget.prototype.__proto__ = text_widget.prototype;
147 current_buffer_scroll_position_widget.prototype.update = function () {
148     var b = this.window.buffers.current;
149     var scrollX, scrollY, scrollMaxX, scrollMaxY;
150     if (b instanceof content_buffer)
151     {
152         var w = b.focused_frame;
153         scrollX = w.scrollX;
154         scrollY = w.scrollY;
155         scrollMaxX = w.scrollMaxX;
156         scrollMaxY = w.scrollMaxY;
157     } else 
158     {
159         scrollX = b.scrollX;
160         scrollY = b.scrollY;
161         scrollMaxX = b.scrollMaxX;
162         scrollMaxY = b.scrollMaxY;
163     }
164     var x = scrollMaxX == 0 ? 100 : Math.round(scrollX / scrollMaxX * 100);
165     var y = scrollMaxY == 0 ? 100 : Math.round(scrollY / scrollMaxY * 100);
166     this.view.text = "(" + x + ", " + y + ")";
169 function clock_widget(window)
171     this.name = "clock-widget";
172     text_widget.call(this, window);
173     var obj = this;
174     this.timer_ID = window.setInterval(function () { obj.update(); }, 60000);
176 clock_widget.prototype.__proto__ = text_widget.prototype;
177 clock_widget.prototype.update = function () {
178     var time = new Date();
179     var hours = time.getHours();
180     var mins = time.getMinutes();
181     this.view.text = (hours<10 ? "0" + hours:hours) + ":" + (mins<10 ?"0" +mins:mins);
183 clock_widget.prototype.destroy = function () {
184     this.window.clearTimeout(this.timer_ID);
187 function mode_line_adder(widget_constructor) {
188     return function (window) { window.mode_line.add_text_widget(new widget_constructor(window)); }
191 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget));
192 add_hook("mode_line_hook", mode_line_adder(clock_widget));
193 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget));
195 mode_line_mode(true);