Created
October 20, 2011 07:09
-
-
Save noqisofon/1300593 to your computer and use it in GitHub Desktop.
https://live.gnome.org/Vala/WebKitSample を目コピしたもの。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gtk; | |
using WebKit; | |
public class ValaBrowser : Window { | |
private const string TITLE = "Vala Browser"; | |
private const string HOME_URL = "http://acid3.acidtests.org/"; | |
private const string DEFAULT_PROTOCOL = "http"; | |
private Regex protocol_regex_; | |
private Entry url_bar_; | |
private WebView web_view_; | |
private Label status_bar_; | |
private ToolButton back_button_; | |
private ToolButton forward_button_; | |
private ToolButton reload_button_; | |
public ValaBrowser() { | |
this.title = ValaBrowser.TITLE; | |
set_default_size( 800, 600 ); | |
try { | |
this.protocol_regex_ = new Regex( ".*://.*" ); | |
} catch ( RegexError re ) { | |
critical( "%s", re.message ); | |
} | |
create_widgets(); | |
connect_signals(); | |
this.url_bar_.grab_focus(); | |
} | |
private void create_widgets() { | |
var toolbar = new Toolbar(); | |
this.back_button_ = new ToolButton.from_stock( STOCK_GO_BACK ); | |
this.forward_button_ = new ToolButton.from_stock( STOCK_GO_FORWARD ); | |
this.reload_button_ = new ToolButton.from_stock( STOCK_REFRESH ); | |
toolbar.add( this.back_button_ ); | |
toolbar.add( this.forward_button_ ); | |
toolbar.add( this.reload_button_ ); | |
this.url_bar_ = new Entry(); | |
this.web_view_ = new WebView(); | |
var scrolled_window = new ScrolledWindow( null, null ); | |
scrolled_window.set_policy( PolicyType.AUTOMATIC, PolicyType.AUTOMATIC ); | |
scrolled_window.add( this.web_view_ ); | |
this.status_bar_ = new Label( "Welcome!" ); | |
this.status_bar_.xalign = 0; | |
var vbox = new VBox( false, 0 ); | |
vbox.pack_start( toolbar, false, true, 0 ); | |
vbox.pack_start( this.url_bar_, false, true, 0 ); | |
vbox.add( scrolled_window ); | |
vbox.pack_start( this.status_bar_, false, true, 0 ); | |
add( vbox ); | |
} | |
private void connect_signals() { | |
this.destroy.connect( Gtk.main_quit ); | |
this.url_bar_.activate.connect( on_active ); | |
this.web_view_.title_changed.connect( (source, frame, title) => { | |
this.title = "%s - %s".printf( title, ValaBrowser.TITLE ); | |
} ); | |
this.web_view_.load_committed.connect( (source, frame) => { | |
this.url_bar_.text = frame.get_uri(); | |
update_buttons(); | |
} ); | |
this.back_button_.clicked.connect( this.web_view_.go_back ); | |
this.forward_button_.clicked.connect( this.web_view_.go_forward ); | |
this.reload_button_.clicked.connect( this.web_view_.reload ); | |
} | |
private void update_buttons() { | |
this.back_button_.sensitive = this.web_view_.can_go_back(); | |
this.forward_button_.sensitive = this.web_view_.can_go_forward(); | |
} | |
private void on_active() { | |
var url = this.url_bar_.text; | |
if ( !this.protocol_regex_.match( url ) ) { | |
url = "%s://%s".printf( ValaBrowser.DEFAULT_PROTOCOL, url ); | |
} | |
this.web_view_.open( url ); | |
} | |
public void start() { | |
show_all(); | |
this.web_view_.open( ValaBrowser.HOME_URL ); | |
} | |
public static int main(string[] args) { | |
Gtk.init( ref args ); | |
var browser = new ValaBrowser(); | |
browser.start(); | |
Gtk.main(); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment