new module to enable editing and deleting of bookmarks
[conkeror/arlinius.git] / contrib / modules / mode-line-buttons.js
blob4b267a023abfd4a8561ab79098e84de8b38c411a
1 /**
2  * (C) Copyright 2009 David Kettler
3  * (C) Copyright 2010-2011 Peter Lunicks
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("mode-line.js");
11 function button_widget (window) {
12     this.class_name = "button-widget";
13     text_widget.call(this, window);
15 button_widget.prototype = {
16     constructor: button_widget,
17     __proto__: text_widget.prototype,
19     make_element: function (window) {
20         var command = this.command;
21         var element = create_XUL(window, "image");
23         element.addEventListener("click", function (event) {
24             var I = new interactive_context(window.buffers.current);
25             co_call(call_interactively(I, command));
26         }, false);
28         element.addEventListener("mouseover", function (event) {
29             var msg = "Button: " + command;
30             var keymaps = get_current_keymaps(window);
31             var list = keymap_lookup_command(keymaps, command);
32             if (list.length)
33                 msg += " (which is on key " + list.join(", ") + ")";
34             window.minibuffer.show(msg);
35         }, false);
37         element.addEventListener("mouseout", function (event) {
38             window.minibuffer.show("");
39         }, false);
41         element.setAttribute("id", "button-widget-" + command);
42         element.setAttribute("class", this.class_name);
43         for (var a in this.attributes) {
44             element.setAttribute(a, this.attributes[a]);
45         }
47         return element;
48     }
51 function make_button_widget (command, attributes, update) {
52     if (typeof attributes == "string")
53         // Simple case
54         attributes = { src: "moz-icon://stock/gtk-" + attributes };
56     function new_widget (window) {
57         button_widget.call(this, window);
58     }
59     new_widget.prototype = {
60         constructor: new_widget,
61         __proto__: button_widget.prototype,
62         command: command,
63         attributes: attributes,
64         update: update
65     };
66     new_widget.prototype.add_hook = function (hook_name, handler) {
67         var obj = this;
68         if (handler == null)
69             handler = function () { obj.update(); };
70         add_hook.call(this.window, hook_name, handler);
71         this.window_hooks.push([hook_name, handler]);
72     };
73     new_widget.mode_line_adder = function (window) {
74         var widget = new new_widget(window);
75         if (new_widget.prototype.update) {
76             widget.add_hook("content_buffer_started_loading_hook");
77             widget.add_hook("content_buffer_finished_loading_hook");
78             widget.add_hook("kill_buffer_hook");
79             widget.add_hook("select_buffer_hook");
80         }
81         window.mode_line.add_widget(widget, widget.make_element(window));
82     };
84     return new_widget;
87 function mode_line_add_buttons (buttons, prepend) {
88     for (var i = 0, n = buttons.length; i < n; i++) {
89         var j = prepend ? n - i - 1 : i;
90         var w = make_button_widget(buttons[j][0], buttons[j][1], buttons[j][2]);
91         add_hook("mode_line_hook", mode_line_adder(w), prepend);
92     }
95 standard_mode_line_buttons = [
96     ["find-url", "open"],
97     ["find-url-new-buffer", "new"],
98     ["back", "go-back"],
99     ["forward", "go-forward"],
100     ["reload", "refresh"],
101     ["kill-current-buffer", "close"],
102     ["buffer-previous", "go-up"],
103     ["buffer-next", "go-down"],
104     ["help-page", "help"],
107 provide("mode-line-buttons");