1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#!/usr/bin/env seed
var home_page = "http://www.google.com";
Gtk = imports.gi.Gtk;
WebKit = imports.gi.WebKit;
Gtk.init(null, null);
TabbedBrowser = new GType({
parent: Gtk.Notebook.type,
name: "TabbedBrowser",
init: function (){
// Public
this.close_tab = function (tab){
browser.remove_page(browser.page_num(tab));
tab.destroy();
if(!browser.get_n_pages()){
browser.new_tab(home_page);
}
};
this.new_tab = function (url){
var new_tab = new BrowserTab();
new_tab.get_web_view().browse(url);
var tab_label = new Gtk.Label({label:"Untitled"});
var tab_button = new Gtk.Button({relief: Gtk.ReliefStyle.NONE});
tab_button.set_image(new Gtk.Image({stock: "gtk-close",
icon_size: Gtk.IconSize.MENU}));
tab_button.signal.clicked.connect(this.close_tab, this);
var tab_title = new Gtk.HBox();
tab_title.pack_start(tab_label);
tab_title.pack_start(tab_button);
tab_title.show_all();
new_tab.set_tab_label(tab_label);
this.append_page(new_tab, tab_title);
};
this.current_tab = function (){
return this.get_nth_page(this.page);
};
// Implementation
this.new_tab(home_page);
}
});
BrowserTab = new GType({
parent: Gtk.VBox.type,
name: "BrowserTab",
init: function (klass)
{
// Private
var toolbar = new BrowserToolbar();
var web_view = new BrowserView();
var scroll_view = new Gtk.ScrolledWindow();
var tab_label;
// Public
this.get_toolbar = function (){
return toolbar;
};
this.get_web_view = function (){
return web_view;
};
this.set_tab_label = function (new_tab_label){
tab_label = new_tab_label;
};
this.get_tab_label = function (){
return tab_label;
};
// Implementation
web_view.set_tab(this);
scroll_view.smooth_scroll = true;
scroll_view.add(web_view);
scroll_view.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC);
this.pack_start(toolbar);
this.pack_start(scroll_view, true, true);
this.show_all();
}
});
BrowserView = new GType({
parent: WebKit.WebView.type,
name: "BrowserView",
init: function (){
// Private
var tab;
var update_title = function (web_view, web_frame, title){
if(title.length > 25)
title = title.slice(0,25) + "...";
tab.get_tab_label().label = title;
};
var update_url = function (web_view, web_frame){
var toolbar = tab.get_toolbar();
toolbar.set_url(web_frame.get_uri());
toolbar.set_can_go_back(web_view.can_go_back());
toolbar.set_can_go_forward(web_view.can_go_forward());
};
// Public
this.browse = function (url){
if(url.search("://") < 0)
url = "http://" + url;
this.open(url);
};
this.set_tab = function (new_tab){
tab = new_tab;
}
this.get_tab = function (){
return tab;
};
// Implementation
this.set_scroll_adjustments(null, null);
this.signal.title_changed.connect(update_title);
this.signal.load_committed.connect(update_url);
}
});
BrowserToolbar = new GType({
parent: Gtk.HBox.type,
name: "BrowserToolbar",
init: function (){
// Private
var url_bar = new Gtk.Entry();
var back_button = new Gtk.ToolButton({stock_id:"gtk-go-back"});
var forward_button = new Gtk.ToolButton({stock_id:"gtk-go-forward"});
var refresh_button = new Gtk.ToolButton({stock_id:"gtk-refresh"});
var back = function (){
browser.current_tab().get_web_view().go_back();
};
var forward = function (){
browser.current_tab().get_web_view().go_forward();
};
var refresh = function (){
browser.current_tab().get_web_view().reload();
};
var browse = function (url){
browser.current_tab().get_web_view().browse(url.text);
};
// Public
this.set_url = function (url){
url_bar.text = url;
};
this.set_can_go_back = function (can_go_back){
back_button.sensitive = can_go_back;
};
this.set_can_go_forward = function (can_go_forward){
forward_button.sensitive = can_go_forward;
};
// Implementation
back_button.signal.clicked.connect(back);
forward_button.signal.clicked.connect(forward);
refresh_button.signal.clicked.connect(refresh);
url_bar.signal.activate.connect(browse);
this.pack_start(back_button);
this.pack_start(forward_button);
this.pack_start(refresh_button);
this.pack_start(url_bar, true, true);
}
});
window = new Gtk.Window({title: "Browser"});
window.resize(800, 600);
window.signal.hide.connect(Gtk.main_quit);
browser = new TabbedBrowser();
window.add(browser);
window.show_all();
Gtk.main();
|