#!/usr/bin/perl # ldict 0.1 by Jonathan Leto # http://leto.net/code/ldict/ # Wed Jul 04 03:55:42 EST 2001 use Gtk; use strict; use Net::Dict; set_locale Gtk; init Gtk; my $sep = "%%"; # seperator for cache file my $Version = "ldict 0.1"; my $cachedir = $ENV{LDICT_CACHE} || "$ENV{HOME}/.ldict"; my $cachefile = "$cachedir/cache"; my $window = new Gtk::Window("toplevel"); my $button = new Gtk::Button("Lookup"); my $table = new Gtk::Table( 5, 5, 0 ); my $label = new Gtk::Label("Word: "); my $entry = new Gtk::Entry( 50 ); my $text = new Gtk::Text( undef, undef ); my $scroll = new Gtk::VScrollbar( $text->vadj ); my $clbutton = new Gtk::Button("Clear"); my $exbutton = new Gtk::Button("Exit"); my $filemenu = new Gtk::MenuItem("File"); my $menu = new Gtk::Menu(); my $about_item = new Gtk::MenuItem("About"); my $menubar = new Gtk::MenuBar(); my $cmap = Gtk::Gdk::Colormap->get_system(); my $color; my $debug = 0; my $word = shift @ARGV; $color->{ 'red' } = 0xFFFF; $color->{ 'green' } = 0; $color->{ 'blue' } = 0; warn( "Couldn't allocate color\n" ) unless ( defined( $cmap->color_alloc( $color ) ) ); $about_item->signal_connect( 'activate', \&file_about ); $window->signal_connect("delete_event", \&close_window ); $window->title($Version); $window->set_default_size( 600, 300 ); $window->border_width( 0 ); $window->add($table); # lookup word when button is clicked or enter pressed $button->signal_connect( "clicked", \&lookup_word ); $entry->signal_connect( "activate", \&lookup_word ); $clbutton->signal_connect("clicked", \&clear_all ); $exbutton->signal_connect("clicked",\&close_window); $entry->set_text($word) if defined $word; $menu->set_title("File"); $menu->append( $about_item ); $menubar->append($filemenu); $filemenu->set_submenu($menu); setup_table(); if( defined $word ){ lookup_word(); } main_loop(); # das boot ####################### subroutines ###########################3 sub main_loop { $about_item->show(); $filemenu->show(); $menubar->show(); $clbutton->show(); $exbutton->show(); $text->show(); $scroll->show(); $button->show(); $entry->show(); $label->show(); $table->show(); $window->show(); main Gtk; exit(0); } sub file_about{ my $window = new Gtk::Window(); my $text = new Gtk::Text(undef,undef); $window->set_title("$Version: About"); $window->set_default_size( 400, 300 ); $window->border_width( 5 ); $text->insert(undef,undef,undef,< Coded with GTK+ 1.2.10, perl 5.6.0, and Gtk 0.7000 on Linux 2.2 All comments welcome. For more information about the DICT protocol and databases, go to http://www.dict.org . ABOUT $window->add($text); $text->show(); $window->show(); } ## commented out for now =head sub file_setup{ my $window = new Gtk::Window(); my $table = new Gtk::Table(5,5,0); my $hostlabel = new Gtk::Label("Host:"); my $hostentry = new Gtk::Entry( 50 ); my $hostbutt = new Gtk::Button("Save"); my $cachelabel = new Gtk::Label("Cache:"); my $cachebutt = new Gtk::Button("Save"); my $cacheentry = new Gtk::Entry( 50 ); my $cdirlabel = new Gtk::Label("Cache Dir:"); my $cdirentry = new Gtk::Entry( 50 ); my $cdirbutt = new Gtk::Button("Save"); $window->set_title("$Version: Setup"); $window->add( $table ); $table->attach_defaults( $hostlabel, 0, 1 , 0 , 1 ); $table->attach_defaults( $cachelabel, 0, 1 , 1 , 2 ); $table->attach_defaults( $cdirlabel, 0, 1 , 2 , 3 ); $table->attach_defaults( $hostentry, 1, 2 , 0 , 1 ); $table->attach_defaults( $cacheentry, 1, 2 , 1 , 2 ); $table->attach_defaults( $cdirentry, 1, 2 , 2 , 3 ); $table->attach_defaults( $hostbutt, 2, 3, 0 , 1 ); $table->attach_defaults( $cachebutt, 2, 3, 1 , 2 ); $table->attach_defaults( $cdirbutt, 2, 3, 2 , 3 ); $table->show(); $hostlabel->show(); $cachelabel->show(); $cdirlabel->show(); $hostbutt->show(); $cachebutt->show(); $cdirbutt->show(); $hostentry->show(); $cacheentry->show(); $cdirentry->show(); $window->show(); } =cut sub setup_table { $table->attach($menubar, 0, 4, 0, 1, ['fill'], [], 0, 0 ); $table->attach_defaults( $label, 0, 1, 1, 2 ); $table->attach_defaults( $entry, 1, 2, 1, 2 ); $table->attach( $button, 2, 3, 1, 2 , [] , [], 0, 0); $table->attach_defaults( $text, 0, 3, 2, 3 ); $table->attach( $scroll, 3, 4, 2, 3, 'fill', [ 'expand', 'shrink', 'fill' ], 0, 0 ); $table->attach( $clbutton, 0, 1, 3, 4, ['fill'], [], 0, 0 ); $table->attach( $exbutton, 2, 4, 3, 4, ['fill'], [], 0, 0 ); $table->set_row_spacings( 0 ); $table->set_col_spacings( 0 ); } sub close_window { Gtk->exit( 0 ); return 0; } sub clear_all { $entry->set_text(""); $text->set_point( 0 ); $text->forward_delete( $text->get_length() ); } sub define { my $word = shift; my (@defs,@tmp,$dict); my $host = $ENV{LDICT_HOST} || 'dict.org'; # first look if it is in the cache if( -e $cachefile ){ print "found cachefile\n" if $debug; undef $/; # slurp all open(FD,"<$cachefile") or die $!; @tmp = split(/$sep/, ); for(my $i=0;$idefine($word) }; } $dict = Net::Dict->new($host); return unless ( defined $dict ); return \@{ $dict->define($word) }; } sub cache_word { my ($word,$db,$def,$num) = @_; my @tmp; if ( -e $cachefile ){ # see if it is in there already open(FD,"<$cachefile") or die $!; @tmp = split(/$sep/, ); for(my $i=0;$i>$cachefile") or die $!; # word num database definition print "caching $word def #$num\n" if $debug; print FD "$word$sep$num$sep$db$sep$def$sep"; close(FD); } } sub lookup_word { my ($dict,$host,$eref,$db,$def); $word = $entry->get_text(); print "got '$word' from entrybox\n" if $debug; $eref = define($word); # clear out old definition $text->set_point( 0 ); $text->forward_delete( $text->get_length() ); unless ( defined @$eref ) { $text->insert(undef,undef,undef,"No definition for '"); $text->insert(undef,$color,undef,$word); $text->insert(undef,undef,undef,"'.\n"); } else { # num of definitions my $num = scalar(@$eref); foreach my $e (@$eref) { ($db,$def) = @$e; cache_word($word,$db,$def,$num); $db = prettify($db); $text->insert(undef,$color,undef, "$db:\n"); $text->insert(undef,undef,undef, "$def\n\n"); $window->set_title("$Version: $word"); $num--; } } } sub prettify { my %database; my $text = shift; %database = ( "web1913" => "Webster's Revised Unabridged Dictionary (1913)", "wn" => "WordNet (r) 1.6", "devils" => "The Devil's Dictionary ((C)1911 Released April 15 1993)", "elements" => "Elements database 20001107", "jargon" => "Jargon File (4.2.3, 23 NOV 2000)", "foldoc" => "The Free On-line Dictionary of Computing (13 Mar 01)", "hitchcock" => "Hitchcock's Bible Names Dictionary (late 1800's)", "world95" => "The CIA World Factbook (1995)", "vera" => "V.E.R.A. -- Virtual Entity of Relevant Acronyms 13 March 2001", "easton" => "Easton's 1897 Bible Dictionary" ); while( my ($k,$v) = each %database ){ if( $text eq $k ){ $text = $v; } } return $text; }