Skip to content
This repository
tag: v20
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (77 sloc) 3.411 kb
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
# gtk-webbrowser.pl - based on the Gtk C# example
# see Gnome Libraries, Gtk, HTML class at http://docs.go-mono.com/
# needs libgtkhtml3.16-cil on Debian or Ubuntu

# Names that occur multiple times. Names used only once appear inline.
constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GTKHTML = "gtkhtml-sharp, Version=3.16.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $SYSTEM = "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
constant Application = CLR::("Gtk.Application,$GTK");

# ---------------------------- Main program ----------------------------
Application.Init;
my $currentUrl; my @historyUrl; my $name = "Niecza web browser";
my $label = CLR::("Gtk.Label,$GTK").new("Address:");
my $entry = CLR::("Gtk.Entry,$GTK").new;
$entry.add_Activated(&EntryActivated);
my $buttonGo = CLR::("Gtk.Button,$GTK").new("Go!");
$buttonGo.add_Clicked(&EntryActivated); # share $entry's handler
my $buttonBack = CLR::("Gtk.Button,$GTK").new("Back");
$buttonBack.add_Clicked(&BackClicked);
my $hbox = CLR::("Gtk.HBox,$GTK").new(False, 1);
$hbox.PackStart($label, False, False, 1);
$hbox.PackStart($entry, True, True, 1);
$hbox.PackStart($buttonGo, False, False, 1);
$hbox.PackStart($buttonBack, False, False, 1);
my $html = CLR::("Gtk.HTML,$GTKHTML").new;
$html.add_LinkClicked(&LinkClicked);
my $sw = CLR::("Gtk.ScrolledWindow,$GTK").new;
$sw.VscrollbarPolicy = CLR::("Gtk.PolicyType,$GTK").Always;
$sw.HscrollbarPolicy = CLR::("Gtk.PolicyType,$GTK").Always;
$sw.Add($html);
my $vbox = CLR::("Gtk.VBox,$GTK").new(False, 1);
$vbox.PackStart($hbox, False, False, 1);
$vbox.PackStart($sw, True, True, 1);
my $win = CLR::("Gtk.Window,$GTK").new($name);
$win.SetDefaultSize(800, 600);
$win.add_DeleteEvent(sub ($o,$a) { Application.Quit(); }); #OK not used
$win.Add($vbox);
$win.ShowAll;
Application.Run;

# --------------------------- Event handlers ---------------------------
sub EntryActivated($obj, $args) #OK not used
{
    if $currentUrl { push @historyUrl, $currentUrl; }
    $currentUrl = $entry.Text.subst(/^\s+/,"").subst(/\s+$/,""); # trim
    LoadHtml($currentUrl);
}

sub LinkClicked($obj, $args) #OK not used
{
    my $newUrl = $args.Url ~~ /^ 'http://' / # decide absolute or relative '
        ?? $args.Url
        !! $currentUrl ~ $args.Url;
    try {
        LoadHtml($newUrl); # apparently throws numerous exceptions
        CATCH { } # hide the evidence of the failure
        push @historyUrl, $currentUrl;
        $currentUrl = $newUrl;
    }
}

sub BackClicked($obj, $args) #OK not used
{
    if @historyUrl { LoadHtml($currentUrl = pop @historyUrl); }
}

# ------------------------- Helper subroutines -------------------------
sub LoadHtml($Url is copy)
{
    unless $Url ~~ /^ [http|https] '://' / { $Url = 'http://' ~ $Url } # '
    my $web_request = CLR::("System.Net.WebRequest,$SYSTEM").Create($Url);
    my $web_response = $web_request.GetResponse;
    my $stream = $web_response.GetResponseStream;
    my $buffer = CLR::("System.Byte[]").new(8192);
    my $html_stream = $html.Begin;
    while (my $count = $stream.Read($buffer, 0, 8192)) != 0 {
        $html_stream.Write($buffer, $count);
    }
    $html.End($html_stream, CLR::("Gtk.HTMLStreamStatus,$GTKHTML").Ok);
    $entry.Text = $Url;
    $win.Title = $html.Title ~ " - $name";
}
Something went wrong with that request. Please try again.