Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (77 sloc) 3.411 kB
5239f11 @mberends Add a lightweight (text only) Gtk.HTML based web browser example
mberends authored
1 # gtk-webbrowser.pl - based on the Gtk C# example
2 # see Gnome Libraries, Gtk, HTML class at http://docs.go-mono.com/
3 # needs libgtkhtml3.16-cil on Debian or Ubuntu
4
5 # Names that occur multiple times. Names used only once appear inline.
6 constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
7 constant $GTKHTML = "gtkhtml-sharp, Version=3.16.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
8 constant $SYSTEM = "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
9 constant Application = CLR::("Gtk.Application,$GTK");
10
11 # ---------------------------- Main program ----------------------------
12 Application.Init;
13 my $currentUrl; my @historyUrl; my $name = "Niecza web browser";
14 my $label = CLR::("Gtk.Label,$GTK").new("Address:");
15 my $entry = CLR::("Gtk.Entry,$GTK").new;
16 $entry.add_Activated(&EntryActivated);
17 my $buttonGo = CLR::("Gtk.Button,$GTK").new("Go!");
18 $buttonGo.add_Clicked(&EntryActivated); # share $entry's handler
19 my $buttonBack = CLR::("Gtk.Button,$GTK").new("Back");
20 $buttonBack.add_Clicked(&BackClicked);
21 my $hbox = CLR::("Gtk.HBox,$GTK").new(False, 1);
22 $hbox.PackStart($label, False, False, 1);
23 $hbox.PackStart($entry, True, True, 1);
24 $hbox.PackStart($buttonGo, False, False, 1);
25 $hbox.PackStart($buttonBack, False, False, 1);
26 my $html = CLR::("Gtk.HTML,$GTKHTML").new;
27 $html.add_LinkClicked(&LinkClicked);
28 my $sw = CLR::("Gtk.ScrolledWindow,$GTK").new;
29 $sw.VscrollbarPolicy = CLR::("Gtk.PolicyType,$GTK").Always;
30 $sw.HscrollbarPolicy = CLR::("Gtk.PolicyType,$GTK").Always;
31 $sw.Add($html);
32 my $vbox = CLR::("Gtk.VBox,$GTK").new(False, 1);
33 $vbox.PackStart($hbox, False, False, 1);
34 $vbox.PackStart($sw, True, True, 1);
35 my $win = CLR::("Gtk.Window,$GTK").new($name);
36 $win.SetDefaultSize(800, 600);
37 $win.add_DeleteEvent(sub ($o,$a) { Application.Quit(); }); #OK not used
38 $win.Add($vbox);
39 $win.ShowAll;
40 Application.Run;
41
42 # --------------------------- Event handlers ---------------------------
43 sub EntryActivated($obj, $args) #OK not used
44 {
45 if $currentUrl { push @historyUrl, $currentUrl; }
46 $currentUrl = $entry.Text.subst(/^\s+/,"").subst(/\s+$/,""); # trim
47 LoadHtml($currentUrl);
48 }
49
50 sub LinkClicked($obj, $args) #OK not used
51 {
52 my $newUrl = $args.Url ~~ /^ 'http://' / # decide absolute or relative '
53 ?? $args.Url
54 !! $currentUrl ~ $args.Url;
55 try {
56 LoadHtml($newUrl); # apparently throws numerous exceptions
57 CATCH { } # hide the evidence of the failure
58 push @historyUrl, $currentUrl;
59 $currentUrl = $newUrl;
60 }
61 }
62
63 sub BackClicked($obj, $args) #OK not used
64 {
65 if @historyUrl { LoadHtml($currentUrl = pop @historyUrl); }
66 }
67
68 # ------------------------- Helper subroutines -------------------------
69 sub LoadHtml($Url is copy)
70 {
71 unless $Url ~~ /^ [http|https] '://' / { $Url = 'http://' ~ $Url } # '
72 my $web_request = CLR::("System.Net.WebRequest,$SYSTEM").Create($Url);
73 my $web_response = $web_request.GetResponse;
74 my $stream = $web_response.GetResponseStream;
75 my $buffer = CLR::("System.Byte[]").new(8192);
76 my $html_stream = $html.Begin;
77 while (my $count = $stream.Read($buffer, 0, 8192)) != 0 {
78 $html_stream.Write($buffer, $count);
79 }
80 $html.End($html_stream, CLR::("Gtk.HTMLStreamStatus,$GTKHTML").Ok);
81 $entry.Text = $Url;
82 $win.Title = $html.Title ~ " - $name";
83 }
Something went wrong with that request. Please try again.