2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
10 require("favicon.js");
13 function tab_bar (window) {
14 window.tab_bar = this;
15 var element = create_XUL(window, "arrowscrollbox");
16 element.setAttribute("id", "tab-bar");
17 element.setAttribute("orient", "horizontal");
18 var after = window.buffers.container;
20 this.element = element;
21 after.parentNode.insertBefore(element, after);
23 add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
24 add_hook.call(window, "create_buffer_early_hook", tab_bar_add_buffer);
25 add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
26 add_hook.call(window, "create_buffer_hook", tab_bar_update_buffer_title);
27 add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
28 add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
29 add_hook.call(window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
31 window.buffers.for_each(tab_bar_add_buffer);
32 this.update_multiple_attribute();
33 if (window.buffers.current != null)
34 tab_bar_select_buffer(window.buffers.current);
36 tab_bar.prototype.destroy = function () {
37 remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
38 remove_hook.call(this.window, "create_buffer_early_hook", tab_bar_add_buffer);
39 remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
40 remove_hook.call(this.window, "create_buffer_hook", tab_bar_update_buffer_title);
41 remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
42 remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
43 remove_hook.call(this.window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
44 this.window.buffers.for_each(function (b) { delete b.tab; });
45 this.selected_buffer = null;
46 this.element.parentNode.removeChild(this.element);
47 delete this.window.tab_bar;
49 tab_bar.prototype.update_multiple_attribute = function () {
50 if (this.window.buffers.count > 1)
51 this.element.setAttribute("multiple", "true");
53 this.element.setAttribute("multiple", "false");
56 function tab_bar_add_buffer (b) {
57 var t = b.window.tab_bar;
58 t.update_multiple_attribute();
59 var tab = create_XUL(b.window, "hbox");
60 tab.setAttribute("class", "tab");
61 tab.addEventListener("click", function () {
63 b.window.buffers.current = b;
64 }, false /* not capturing */);
65 tab.setAttribute("selected", "false");
66 var image = create_XUL(b.window, "image");
67 image.setAttribute("class", "tab-icon");
69 image.setAttribute("src", b.icon);
70 var label = create_XUL(b.window, "label");
71 label.setAttribute("class", "tab-label");
72 label.setAttribute("crop", "end");
73 var button = create_XUL(b.window, "toolbarbutton");
74 button.setAttribute("class", "tab-close-button");
75 button.addEventListener("click", function (event) {
77 event.stopPropagation();
78 }, false /* not capturing */);
79 tab.appendChild(image);
80 tab.appendChild(label);
81 tab.appendChild(button);
82 tab.tab_label = label;
83 tab.tab_image = image;
84 t.element.appendChild(tab);
86 tab_bar_update_buffer_title(b);
89 function tab_bar_kill_buffer (b) {
90 var t = b.window.tab_bar;
91 t.update_multiple_attribute();
92 if (t.selected_buffer == b)
93 t.selected_buffer = null;
94 b.tab.parentNode.removeChild(b.tab);
98 function tab_bar_select_buffer (b) {
99 var t = b.window.tab_bar;
100 if (t.selected_buffer != null)
101 t.selected_buffer.tab.setAttribute("selected", "false");
102 t.selected_buffer = b;
103 b.tab.setAttribute("selected", "true");
104 t.element.ensureElementIsVisible(b.tab);
107 function tab_bar_update_buffer_title (b) {
109 if (title == null || title.length == 0)
110 title = b.description;
111 b.tab.tab_label.setAttribute("value", title);
114 function tab_bar_update_buffer_icon (b) {
116 b.tab.tab_image.setAttribute("src", b.icon);
118 b.tab.tab_image.removeAttribute("src");
121 function tab_bar_install (window) {
123 throw new Error("tab bar already initialized for window");
127 function tab_bar_uninstall (window) {
129 throw new Error("tab bar not initialized for window");
130 window.tab_bar.destroy();
133 define_global_mode("tab_bar_mode",
134 function () { // enable
135 add_hook("window_initialize_hook", tab_bar_install);
136 for_each_window(tab_bar_install);
138 function () { // disable
139 remove_hook("window_initialize_hook", tab_bar_install);
140 for_each_window(tab_bar_uninstall);