2 * (C) Copyright 2005 Shawn Betts
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5 * Use, modification, and distribution are subject to the terms specified in the
11 define_window_local_hook("mode_line_hook");
13 function generic_element_widget_container(window
, container
)
16 this.container
= container
;
18 define_keywords("$flex", "$align", "$class", "$crop");
19 generic_element_widget_container
.prototype = {
20 add_text_widget : function (widget
) {
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");
28 element
.setAttribute("flex", flex
);
30 element
.setAttribute("align", align
);
31 if (class_name
== null)
32 class_name
= widget
.name
;
33 element
.setAttribute("class", class_name
);
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
;
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();
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);
69 function generic_widget_element(element
, widget
)
71 this.element
= element
;
76 generic_widget_element
.prototype = {
78 return this.element
.getAttribute("value");
82 this.element
.setAttribute("value", v
);
85 destroy : function () {
86 this.widget
.destroy();
89 remove : function () {
90 this.element
.parentNode
.removeChild(this.element
);
96 function text_widget(window
)
98 this.window_hooks
= [];
101 text_widget
.prototype = {
102 add_hook : function (hook_name
, handler
)
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
]);
112 attach : function (view
) {
117 update : function () {
120 destroy : function ()
122 for each (let i
in this.window_hooks
) {
123 remove_hook
.call(this.window
, i
[0], i
[1]);
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
);
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
)
161 var w
= b
.focused_frame
;
164 scrollMaxX
= w
.scrollMaxX
;
165 scrollMaxY
= w
.scrollMaxY
;
170 scrollMaxX
= b
.scrollMaxX
;
171 scrollMaxY
= b
.scrollMaxY
;
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
);
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 buffer_count_widget(window
) {
197 this.name
= "buffer-count-widget";
198 text_widget
.call(this, window
);
199 this.add_hook("select_buffer_hook");
200 this.add_hook("create_buffer_hook");
201 this.add_hook("kill_buffer_hook");
203 buffer_count_widget
.prototype.__proto__
= text_widget
.prototype;
204 buffer_count_widget
.prototype.update = function () {
205 this.view
.text
= ("[" + (this.window
.buffers
.selected_index
+1) + "/" +
206 this.window
.buffers
.count
+ "]");
209 function mode_line_adder(widget_constructor
) {
210 if (!('mode_line_adder' in widget_constructor
))
211 widget_constructor
.mode_line_adder = function (window
) {
212 window
.mode_line
.add_text_widget(new widget_constructor(window
));
214 return widget_constructor
.mode_line_adder
;
217 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget
));
218 add_hook("mode_line_hook", mode_line_adder(clock_widget
));
219 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget
));
221 mode_line_mode(true);