#!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::WebKit; use Glib qw(TRUE FALSE); # by zentara.... a free Perl adaptation of WebKit's GtkLauncher my $win = Gtk2::Window->new; $win->set_default_size(800, 600); $win->signal_connect(destroy => sub { Gtk2->main_quit }); #globals my ($toolbarbox,$viewbox,$statusbox,$entry,$view,$statusbar, $title, $load_progress, $status_context_id); $title = 'Perl_Browser'; $win->set_title($title); my $vbox = Gtk2::VBox->new( FALSE, 2 ); $vbox->set_border_width(2); $win->add($vbox); $vbox->set_border_width(2); foreach my $box ( $toolbarbox, $viewbox, $statusbox){ $box = Gtk2::VBox->new( FALSE, 0 ); $box->set_border_width(0); $vbox->pack_start($box,1,1,1); } create_toolbar($toolbarbox); create_browser($viewbox); create_status($statusbox); $win->show_all; $view->grab_focus; #if you want to open a default file or uri #$view->open( $ARGV[0] || 'file://z.html' ); #$view->open( $ARGV[0] || 'http://perlmonks.org' ); Gtk2->main; sub create_toolbar{ my $box = $_[0]; my $toolbar = Gtk2::Toolbar->new; $toolbar->set_icon_size ('large-toolbar'); $toolbar->set_show_arrow (FALSE); $toolbar->set_orientation ('GTK_ORIENTATION_HORIZONTAL'); #make the toolbar buttons, etc my $button = Gtk2::ToolButton->new_from_stock('gtk-go-back'); $button->signal_connect( "clicked" => \&go_back, undef ); $toolbar->insert($button, -1); $toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 ); $button = Gtk2::ToolButton->new_from_stock('gtk-go-forward'); $button->signal_connect( "clicked" => \&go_forward, undef ); $toolbar->insert($button, -1); $toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 ); # make entry for URI's my $entbox = Gtk2::ToolItem->new(); $entbox->set_expand(1); $entry = Gtk2::Entry->new(); $entbox->add($entry); $entry->signal_connect( "activate" => \&activate_uri_entry, undef ); $toolbar->insert($entbox, -1); $button = Gtk2::ToolButton->new_from_stock('gtk-ok'); $button->signal_connect( "clicked" => \&activate_uri_entry, undef ); $toolbar->insert($button, -1); $button = Gtk2::ToolButton->new(undef,'Screenshot'); $button->signal_connect( "clicked" => \&screenshot, undef ); $toolbar->insert($button, -1); $box->pack_start($toolbar,FALSE,FALSE,0); } sub create_browser{ my $box = $_[0]; my $sw = Gtk2::ScrolledWindow->new; $sw->set_policy ('automatic', 'automatic'); $sw->set_size_request (500, 500); # hack to set initial empty browser size $view = Gtk2::WebKit::WebView->new; $view->signal_connect( 'notify::title' => \¬ify_title, undef ); $view->signal_connect( 'notify::load-status' => \¬ify_load_status, undef ); $view->signal_connect( 'notify::progress' => \¬ify_progress, undef ); $view->signal_connect( 'hovering-over-link' => \&hovering_over_link, undef ); $sw->add($view); $box->pack_start($sw,1,1,0); } sub create_status{ my $box = $_[0]; $statusbar = Gtk2::Statusbar->new; $status_context_id = $statusbar->get_context_id('Link Hover'); $box->pack_start($statusbar,FALSE,FALSE,0); } sub go_back{ $view->go_back } sub go_forward{ $view->go_forward } sub activate_uri_entry{ my $uri = $entry->get_text; $uri ||= 'http://google.com'; $view->load_uri($uri); } sub notify_title{ my $vtitle = $view->get_title(); if ($vtitle){ $win->set_title( $vtitle ) }; } sub notify_load_status{ if( $view->get('load_status') eq 'WEBKIT_LOAD_COMMITTED'){ my $frame = $view->get_main_frame(); my $uri = $view->frame_get_uri($frame); if( $uri ){ $entry->set_text($uri) } } } sub notify_progress{ $load_progress = $view->get('progress'); my $title = sprintf ("Progress: %2d%%", 100 * $load_progress); $win->set_title($title); } sub hovering_over_link{ my ($hash, undef, $link) = @_; if(defined $link){ #print "$status_context_id\t$link \n"; $statusbar->pop($status_context_id); $statusbar->push($status_context_id, $link); } } sub update_title{ $win->set_title($load_progress) } sub screenshot{ # we are going to save the visible browser view window # this DOES NOT save the entire html, only what is visible my ($width, $height) = $view->window->get_size; # create blank pixbuf to hold the image my $gdkpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb', 0, 8, $width, $height); $gdkpixbuf->get_from_drawable ($view->window, undef, 0, 0, 0, 0, $width, $height); #only jpeg and png is supported !!!! it's 'jpeg', not 'jpg' $gdkpixbuf->save ("$0.jpg", 'jpeg', quality => 100); return FALSE; }