2 * (C) Copyright 2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008 Nicholas A. Zigarovich
4 * (C) Copyright 2008 John J. Foerch
5 * (C) Copyright 2011 Peter Lunicks
7 * Use, modification, and distribution are subject to the terms specified in the
10 * This is a tab bar which is based on tab-bar.js but makes customization of the
11 * tabs much easier. It provides sensible default styles but lets the user
12 * simply override these defaults by normal CSS.
19 define_variable("tab_bar_button_select", 0,
20 "The mouse button that selects tabs." +
21 "0 = left, 1 = middle, 2 = right, null = disabled.");
23 define_variable("tab_bar_button_close", 2,
24 "The mouse button that closes tabs." +
25 "0 = left, 1 = middle, 2 = right, null = disabled.");
27 define_variable("tab_bar_show_icon", false,
28 "Whether or not to show buffer icons in tabs.");
30 define_variable("tab_bar_show_index", true,
31 "Whether or not to show the tab index in each tab.");
34 * Constructs a tab bar for the given window.
36 function tab_bar (window) {
37 window.tab_bar = this;
38 var scrollbox = create_XUL(window, "arrowscrollbox");
39 scrollbox.setAttribute("id", "tab2-bar");
40 scrollbox.setAttribute("orient", "horizontal");
41 var after = window.buffers.container;
43 this.element = scrollbox;
44 after.parentNode.insertBefore(scrollbox, after);
46 add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
47 add_hook.call(window, "create_buffer_early_hook", tab_bar_add_buffer);
48 add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
49 add_hook.call(window, "move_buffer_hook", tab_bar_move_buffer);
50 add_hook.call(window, "create_buffer_hook", tab_bar_update_buffer_title);
51 add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
52 add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
53 add_hook.call(window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
55 window.buffers.for_each(function (b) tab_bar_add_buffer(b, true));
56 this.update_multiple_attribute();
57 if (window.buffers.current != null)
58 tab_bar_select_buffer(window.buffers.current);
63 * Destroys the tab bar.
65 tab_bar.prototype.destroy = function () {
66 remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
67 remove_hook.call(this.window, "create_buffer_early_hook", tab_bar_add_buffer);
68 remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
69 remove_hook.call(this.window, "move_buffer_hook", tab_bar_move_buffer);
70 remove_hook.call(this.window, "create_buffer_hook", tab_bar_update_buffer_title);
71 remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
72 remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
73 remove_hook.call(this.window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
74 this.window.buffers.for_each(function (b) { delete b.tab; });
75 this.selected_buffer = null;
76 this.element.parentNode.removeChild(this.element);
77 delete this.window.tab_bar;
82 * Updates the "index" node and "ordinal" attribute of all tabs.
84 tab_bar.prototype.update_ordinals = function () {
85 var buffers = this.window.buffers;
86 for (var i = 0, n = this.element.childNodes.length; i < n; i++) {
87 var ordinal = buffers.index_of(this.element.childNodes[i].buffer) + 1;
88 this.element.childNodes[i].setAttribute("ordinal", ordinal);
89 this.element.childNodes[i].index.setAttribute("value", ordinal);
95 * Updates the "multiple" attribute of the tab bar.
97 tab_bar.prototype.update_multiple_attribute = function () {
98 if (this.window.buffers.count > 1)
99 this.element.setAttribute("multiple", "true");
101 this.element.setAttribute("multiple", "false");
106 * Adds a tab for the given buffer. When second argument 'noupdate' is
107 * true, a new tab in the middle of the buffer list will not cause the
108 * ordinals of other tabs to be updated. This is used during
109 * initialization of the tab bar.
111 function tab_bar_add_buffer (buffer, noupdate) {
114 var tabbar = buffer.window.tab_bar;
115 tabbar.update_multiple_attribute();
117 var ordinal = buffer.window.buffers.index_of(buffer) + 1;
118 if (ordinal < buffer.window.buffers.buffer_list.length && ! noupdate)
119 tabbar.update_ordinals();
121 // Create a tab and add it to the tab bar
122 var tab = create_XUL(buffer.window, "hbox");
124 tab.setAttribute("class", "tab2");
125 tab.addEventListener("click", function (event) {
126 if (event.button == tab_bar_button_select) {
127 if (!tab.buffer.dead)
128 tab.buffer.window.buffers.current = tab.buffer;
130 }, false /* not capturing */);
131 tab.setAttribute("selected", "false");
133 // Create the label to hold the buffer icon
134 var image = create_XUL(buffer.window, "image");
135 image.setAttribute("class", "tab2-icon");
136 if (buffer.icon != null)
137 image.setAttribute("src", buffer.icon);
139 // Create the label to hold the tab number
140 var index = create_XUL(buffer.window, "label");
141 index.setAttribute("class", "tab2-index");
142 index.setAttribute("value", ordinal);
144 // Create the label to hold the tab title
145 var label = create_XUL(buffer.window, "label");
146 label.setAttribute("class", "tab2-label");
147 label.setAttribute("crop", "end");
149 // No close button, just use the designated mouse button.
150 tab.addEventListener("click", function (event) {
151 if (event.button == tab_bar_button_close) {
152 kill_buffer(tab.buffer);
153 event.stopPropagation();
155 }, false /* not capturing */);
157 // Add all the stuff to the new tab
161 if (tab_bar_show_index)
162 tab.appendChild(index);
163 if (tab_bar_show_icon)
164 tab.appendChild(image);
165 tab.appendChild(label);
166 tabbar.element.appendChild(tab);
168 tab_bar_update_buffer_title(buffer);
170 // Note, XULRunner 1.9.x puts the tab in the wrong location if we set
171 // the ordinal before adding the tab to the tab-bar.
172 tab.setAttribute("ordinal", ordinal);
177 * Removes the tab for the given buffer.
179 function tab_bar_kill_buffer (b) {
180 var t = b.window.tab_bar;
181 t.update_multiple_attribute();
182 if (t.selected_buffer == b)
183 t.selected_buffer = null;
184 b.tab.parentNode.removeChild(b.tab);
185 t = b.window.tab_bar;
188 // Renumber the tabs.
194 * Updates all tab indices and ensure that the current tab is still visible.
196 function tab_bar_move_buffer (b) {
197 var t = b.window.tab_bar;
199 t.element.ensureElementIsVisible(b.window.buffers.current.tab);
204 * Updates the tab of the given buffer to indicate it is the currently open one.
206 function tab_bar_select_buffer (b) {
207 var t = b.window.tab_bar;
208 if (t.selected_buffer != null)
209 t.selected_buffer.tab.setAttribute("selected", "false");
210 t.selected_buffer = b;
211 b.tab.setAttribute("selected", "true");
212 t.element.ensureElementIsVisible(b.tab);
217 * Updates the tab title for the given buffer.
219 function tab_bar_update_buffer_title (b) {
221 if (title == null || title.length == 0)
222 title = b.description;
223 b.tab.label.setAttribute("value", title);
228 * Updates the tab icon for the given buffer.
230 function tab_bar_update_buffer_icon (b) {
232 b.tab.image.setAttribute("src", b.icon);
234 b.tab.image.removeAttribute("src");
239 * Inserts the tab bar in the given window.
241 function tab_bar_install (window) {
243 throw new Error("tab bar already initialized for window");
249 * Removes the tab bar from the given window.
250 * If the tab bar is not installed, throws an error.
252 function tab_bar_uninstall (window) {
254 throw new Error("tab bar not initialized for window");
255 window.tab_bar.destroy();
259 define_global_mode("tab_bar_mode",
260 function () { // enable
261 add_hook("window_initialize_hook", tab_bar_install);
262 for_each_window(tab_bar_install);
264 function () { // disable
265 remove_hook("window_initialize_hook", tab_bar_install);
266 for_each_window(tab_bar_uninstall);