Advertisement
hackloper775

PerlBrowser 4.1

Jan 4th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.70 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. #
  4. # Instalar Gtk3 y Gtk3::WebKit por CPAN,para funcionar correctamente
  5. # Programa de prueba para Tiempo de Tux <http://www.itimetux.com>
  6. # Por Demianx864
  7. # Eres libre de estudiar modificar y añadir nuevas cosas a el programa,que te diviertas :D
  8. # Simple PerlBrowser 4.1
  9. # Gracias al modulo WebKit <http://webkitgtk.org/>
  10. #
  11.  
  12. use strict;
  13. use feature ':5.14';
  14. use Glib qw/TRUE FALSE/;
  15. use Gtk3 -init;
  16. binmode STDOUT, ":utf8";
  17. use utf8;
  18. use JSON;
  19.  
  20. my $json;
  21.  
  22. &json;
  23.  
  24. my $confP = decode_json($json);
  25.  
  26.     my $iniP = $confP->{'pantalla'}->{'inicio'}->[0];
  27.     my $unscreen = $confP->{'pantalla'}->{'inicio'}->[1];
  28.  
  29.         use Gtk3::WebKit;  
  30.    
  31.     # Usar logo.png
  32.  
  33.         my $ventana = Gtk3::Window->new('toplevel');
  34.     my $icono = 'logo.png';
  35.     if ( -e $icono ) {
  36.             my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file($icono);
  37.             $ventana->set_icon_from_file($icono);
  38.     }
  39.  
  40.     # Parametros de la ventana
  41.  
  42.     my $titulo = "Simple Perl Browser";
  43.         $ventana->set_default_size(800, 600);
  44.     $ventana->set_title($titulo);  
  45.     $ventana->set_border_width(5);
  46.     $ventana->$unscreen();
  47.     $ventana->signal_connect(delete_event => \&salir);
  48.     my $inicio = Gtk3::Window->new('toplevel');
  49.  
  50.     # Opciones
  51.  
  52.     my $table = Gtk3::Grid->new;
  53.  
  54.     my @entries = (
  55.     [ "Opciones", undef, "_Opciones" ],
  56.     ["Acerca de", undef, "_Acerca de", undef, "Acerca de", \&activate_action ],
  57.     ["Inicio", undef, "_Inicio", undef, "Inicio", \&activate_action ],
  58.     ["Salir", undef, "_Salir", undef, "Salir", \&activate_action ]
  59.     );
  60.  
  61.     my @toggle_entries = (                      
  62.     ["Completa",undef, "_Pantalla Completa", undef,    
  63.     "Pantalla completa", \&activate_action, FALSE ],               
  64.     ["HideTitlebar", undef, "_Hide Titlebar", undef,
  65.     "Hide Titlebar",  \&activate_action,FALSE]
  66.     );
  67.  
  68.     my $action_group = Gtk3::ActionGroup->new('AppWindowActions');
  69.     $action_group->add_actions( \@entries, undef );
  70.         $action_group->add_toggle_actions( \@toggle_entries, undef );
  71.  
  72.     my $ui = Gtk3::UIManager->new();
  73.         $ui->insert_action_group( $action_group, 0 );
  74.     my $ui_info = get_ui();
  75.         $ui->add_ui_from_string( $ui_info, length($ui_info) );
  76.         $table->attach( $ui->get_widget('/MenuBar'), 0, 0, 1, 1 );
  77.  
  78.     # Webkit
  79.  
  80.         my $uri = shift @ARGV || $iniP ;
  81.     my $ver = Gtk3::WebKit::WebView->new();
  82.  
  83.     # Cargamos url 
  84.    
  85.     &web();
  86.        
  87.     # Añadimos url a scrolls
  88.  
  89.         my $scrolls = Gtk3::ScrolledWindow->new();
  90.         $scrolls->set_policy ('automatic', 'automatic');    
  91.     $scrolls->add($ver);
  92.    
  93.     # Entrada
  94.  
  95.     my $entry = Gtk3::Entry->new;  
  96.     $entry->signal_connect( "activate" => \&entrada, undef );
  97.     my $entrycio = Gtk3::Entry->new;
  98.     $entrycio->signal_connect( "activate" => \&confinicio, undef );
  99.  
  100.     # Botones
  101.  
  102.     my $brg = Gtk3::ToolButton->new_from_stock('gtk-quit');
  103.     my $go = Gtk3::ToolButton->new_from_stock('gtk-find');
  104.     my $acinicio = Gtk3::ToolButton->new_from_stock('gtk-apply');
  105.     my $fresh = Gtk3::ToolButton->new_from_stock('gtk-refresh');
  106.     my $frenar = Gtk3::ToolButton->new_from_stock('gtk-stop'); 
  107.     my $batras = Gtk3::ToolButton->new_from_stock('gtk-go-back');
  108.         my $belante = Gtk3::ToolButton->new_from_stock('gtk-go-forward');
  109.     my $vermenos = Gtk3::ToolButton->new_from_stock('gtk-zoom-in');
  110.         my $vermas = Gtk3::ToolButton->new_from_stock('gtk-zoom-out');
  111.     my $label = Gtk3::Label->new("");
  112.  
  113.     $go->signal_connect ( "clicked" => \&entrada, undef );
  114.     $fresh->signal_connect ( "clicked" => \&recargar, undef ); 
  115.     $frenar->signal_connect ( "clicked" => \&parar );  
  116.     $batras->signal_connect( "clicked" => \&atras );
  117.     $belante->signal_connect ( "clicked" => \&adelante );
  118.     $vermenos->signal_connect ( "clicked" => \&zomin );
  119.     $vermas->signal_connect ( "clicked" => \&zomout);
  120.     $acinicio->signal_connect ( "clicked" => \&confinicio, undef );
  121.  
  122.     # Frenar y recargar unificados
  123.  
  124.     my $botones = Gtk3::Notebook->new();
  125.     $botones->set_show_tabs(FALSE);
  126.     $botones->set_show_border(FALSE);
  127.     $botones->append_page($fresh);
  128.     $botones->append_page($frenar);
  129.  
  130.     # Unimos todo
  131.    
  132.     # hbox
  133.  
  134.     my $hbox = Gtk3::Box->new("horizontal", 0);
  135.     $hbox->pack_start($entry, TRUE,TRUE , 0);
  136.     $hbox->pack_start($go, FALSE,FALSE , 0);
  137.     $hbox->pack_start($label, FALSE,FALSE , 0);
  138.     $hbox->pack_start($botones, FALSE,FALSE , 0);
  139.     $hbox->pack_start($vermas, FALSE,FALSE , 0);
  140.     $hbox->pack_start($vermenos, FALSE,FALSE , 0); 
  141.     $hbox->pack_start($batras, FALSE, FALSE, 0);
  142.     $hbox->pack_start($belante, FALSE, FALSE, 0);
  143.     $hbox->pack_start($table,FALSE, FALSE, 0);
  144.  
  145.     # vbox
  146.  
  147.     my $vbox = Gtk3::Box->new("vertical", 0);
  148.     $vbox->pack_start($hbox, FALSE, FALSE, 0);
  149.     $vbox->pack_start($scrolls, TRUE, TRUE,  0);
  150.  
  151.     $ventana->add($vbox);
  152.    
  153.     # Lo mostramos
  154.  
  155.     $ventana->show_all();
  156.      
  157.        
  158. Gtk3->main();  
  159.  
  160. ### Funciones :) ###
  161.  
  162. sub salir {
  163. &unfull;
  164. Gtk3->main_quit;
  165. return FALSE;
  166. }
  167.  
  168. sub adelante{
  169.  
  170. $ver->go_forward;
  171.  
  172. &cargar();
  173.  
  174. }
  175.  
  176. sub atras{
  177.  
  178. $ver->go_back;
  179.  
  180. &cargar();
  181.  
  182. }
  183.  
  184. sub entrada{
  185.  
  186. &cargar();
  187.  
  188. my $uri = $entry->get_text;
  189.  
  190. my $sbr = substr($uri,0,7);
  191.  
  192.     if ($sbr eq "http://") {    
  193.  
  194.         $uri = $uri;
  195.     }
  196.  
  197.     elsif($uri =~ /[.]/) {
  198.        
  199.          $uri = "http://$uri";
  200.     }
  201.     else {  
  202.  
  203.         $uri = "http://www.google.com/search?q=$uri";
  204.     }
  205.    
  206.    
  207.     $ver->load_uri($uri);
  208.    
  209.     my $url_sitio = $ver->get_uri();
  210.  
  211.     $botones->set_current_page(1);
  212. }
  213.  
  214. sub zomin { $ver->zoom_in }
  215.  
  216. sub zomout { $ver->zoom_out }
  217.  
  218. sub progreso{
  219. &cargar();
  220. my $ctitulo;
  221. my $carga = $ver->get('progress');
  222. my $url_sitio = $ver->get_uri();
  223. $entry->set_text("$url_sitio");
  224. for ( $ctitulo < 100 ) {
  225.    my $ctitul = sprintf ("%2d%%", 100 * $carga);
  226.    $ctitulo = chop($ctitul);
  227.    $label->set_markup("$ctitul%");  
  228.    if ($ctitul == 100) {
  229.    $botones->set_current_page(0);
  230.         }
  231.     }
  232. }
  233.  
  234. sub titulo {
  235. my $titul2 = $ver->get_title();
  236. if ($titul2){
  237. $ventana->set_title( $titul2 );}
  238. }
  239.  
  240. sub recargar {
  241. &cargar();
  242. $ver->reload;
  243. }
  244.  
  245. sub parar {
  246. $ver->stop_loading;
  247. $botones->set_current_page(0);
  248. }
  249.  
  250. sub cargar { $botones->set_current_page(1); }
  251.  
  252. sub acerca_de {
  253.  
  254. my $texto =<<EOF;
  255.  
  256. SimplePerlBrowser
  257.  
  258. Navegador Web escrito en Perl
  259.  
  260. Por: Tiempo de Tux
  261.  
  262. Version: 4.1
  263.  
  264. EOF
  265.  
  266. my $dialog = Gtk3::MessageDialog->new(
  267. $ventana,'destroy-with-parent', 'info', 'close', $texto);
  268. $dialog->signal_connect( response => sub { $dialog->destroy } );
  269. $dialog->show();
  270. }
  271.  
  272. sub web {
  273. $ver->load_uri( $uri );
  274. $ver->signal_connect( 'notify::progress' => \&progreso, undef );
  275. $ver->signal_connect( 'notify::title' => \&titulo, undef );
  276. }
  277.  
  278. sub activate_action {
  279. my $action = shift;
  280. my $name = $action->get_name;
  281. if ( $name eq 'Completa' ) {
  282.     &pantallacompleta;
  283. }elsif ( $name eq 'HideTitlebar'){
  284.     my $value = $ventana->get_hide_titlebar_when_maximized();
  285. if ( $value == 0 ) { $ventana->set_hide_titlebar_when_maximized (TRUE);}
  286. else{ $ventana->set_hide_titlebar_when_maximized (FALSE); }
  287. }
  288. elsif ( $name eq 'Acerca de'){ &acerca_de(); }
  289. elsif ( $name eq 'Inicio'){ &inicioconf(); }
  290. elsif ( $name eq 'Salir'){ &salir(); }
  291. }
  292.  
  293. sub inicioconf {
  294.  
  295.     $inicio->set_default_size(270, 100);
  296.     $inicio->set_title("Pagina de inicio");
  297.         $inicio->set_resizable(FALSE);
  298.     $inicio->set_border_width(5);
  299.         $inicio->set_position("center");  
  300.     $inicio->signal_connect (delete_event => sub { Gtk3->main_quit });
  301.     my $labl = Gtk3::Label->new("http://");
  302.     my $vcio = Gtk3::Box->new("horizontal", 0);
  303.     $vcio->pack_start($labl, FALSE, FALSE,   0);
  304.     $vcio->pack_start($entrycio, FALSE, FALSE, 0);
  305.     $vcio->pack_start($acinicio, FALSE, FALSE, 0);
  306.     $inicio->add($vcio);
  307.    
  308.     # Lo mostramos
  309.  
  310.     $inicio->show_all();  
  311. }
  312.  
  313. sub get_ui {
  314.     return "<ui>
  315.     <menubar name='MenuBar'>
  316.    <menu action='Opciones'>
  317.       <menuitem action='Completa'/>
  318.       <menuitem action='HideTitlebar'/>
  319.       <menuitem action='Inicio'/>
  320.       <menuitem action='Acerca de'/>
  321.       <menuitem action='Salir'/>
  322.    </menu>
  323.  </menubar>
  324. </ui>";
  325. }
  326.  
  327. sub confinicio {
  328.  
  329. my $paginicio = $entrycio->get_text;
  330.  
  331. if ($paginicio ne "" ) {
  332.  
  333. $iniP = $confP->{'pantalla'}->{'inicio'}->[0] = "http://" . $paginicio;
  334. open my $cfn, ">", "conf.json";
  335. print $cfn encode_json($confP);
  336. close $cfn;
  337. }
  338.  
  339. $inicio->destroy;
  340. }
  341.  
  342. sub pantallacompleta {
  343.  
  344. my $fuu = substr($unscreen,0,2);
  345.  
  346. if ($fuu eq "un") {
  347.  
  348. $unscreen = $confP->{'pantalla'}->{'inicio'}->[1] = "fullscreen";
  349. open my $cfn, ">", "conf.json";
  350. print $cfn encode_json($confP);
  351. close $cfn;
  352.  
  353. }
  354. elsif ( $fuu eq "fu") {
  355.  
  356. &unfull;
  357.  
  358. }
  359.  
  360. else {
  361.  
  362. say "No tienes configurado\nconf.json\n";
  363.  
  364. }
  365.  
  366. &json;
  367.  
  368. $ventana->$unscreen();
  369. }
  370.  
  371. sub json {
  372.  
  373. {
  374.   local $/;
  375.   open my $cf, "<", "conf.json";
  376.   $json = <$cf>;
  377.   close $cf;
  378. }
  379.  
  380. }
  381. sub unfull {
  382.  
  383. $unscreen = $confP->{'pantalla'}->{'inicio'}->[1] = "unfullscreen";
  384. open my $cfn, ">", "conf.json";
  385. print $cfn encode_json($confP);
  386. close $cfn;
  387.  
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement