| 1 | package GFtk; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.09'; | ||
| 6 | |||
| 7 | use parent qw(Tk::MainWindow); | ||
| 8 | |||
| 9 | use Tk::JPEG; | ||
| 10 | use Tk::BrowseEntry; | ||
| 11 | use Image::Magick; | ||
| 12 | use MIME::Base64; | ||
| 13 | |||
| 14 | use Exporter 'import'; | ||
| 15 | our @EXPORT = qw<addWidget addVar setScale $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 16 | our $frame; | ||
| 17 | |||
| 18 | our $winX=30; | ||
| 19 | our $winY=30; | ||
| 20 | our $winWidth; | ||
| 21 | our $winHeight; | ||
| 22 | our $winTitle; | ||
| 23 | our $winScale=6.5; | ||
| 24 | |||
| 25 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 26 | my @widgets=(); | ||
| 27 | my %iVars=(); #vars for interface operation (e.g. | ||
| 28 | my %oVars=(); #vars for interface creation (e.g. list of options) | ||
| 29 | my %styles; | ||
| 30 | |||
| 31 | my $lastMenuLabel; #bug workaround in menu generator may be needed for submenus | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my $class = shift; | ||
| 36 | my $self = $class->SUPER::new(@_); # call the superclass' constructor | ||
| 37 | $self -> title($winTitle); | ||
| 38 | $self -> geometry($winWidth."x".$winHeight); | ||
| 39 | $frame = $self->Canvas( | ||
| 40 | -bg => 'lavender', | ||
| 41 | -relief => 'sunken', | ||
| 42 | -width => $winWidth, | ||
| 43 | -height => $winHeight)->pack(-expand => 1, -fill => 'both'); | ||
| 44 | |||
| 45 | $frame ->fontCreate('medium', | ||
| 46 | -family=>'arial', | ||
| 47 | -weight=>'normal', | ||
| 48 | -size=>int(-18*18/14)); | ||
| 49 | setupContent($self,$frame); #then add content | ||
| 50 | return $self; | ||
| 51 | }; | ||
| 52 | |||
| 53 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 54 | sub setupContent{ | ||
| 55 | my ($self, $canvas)=@_; | ||
| 56 | $self ->{"menubar"}=undef; | ||
| 57 | my $currentMenu; | ||
| 58 | foreach my $widget (@widgets){ | ||
| 59 | my @params=@$widget; | ||
| 60 | my $wtype=shift @params; | ||
| 61 | if ($wtype eq "btn") {aBt($self, $canvas, @params);} | ||
| 62 | elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);} | ||
| 63 | elsif ($wtype eq "stattext") {aST($self, $canvas, @params);} | ||
| 64 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 65 | elsif ($wtype eq "combo") {aCB($self, $canvas, @params);} | ||
| 66 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 67 | elsif ($wtype eq "mb") | ||
| 68 | { | ||
| 69 | if (! $self->{"menubar"}){ | ||
| 70 | $self->configure(-menu => $self ->{"menubar"} = $self->Menu); | ||
| 71 | } | ||
| 72 | $currentMenu=aMB($self,$canvas,$currentMenu,@params) | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub aBt{ | ||
| 77 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 78 | $canvas->{"btn$id"}=$canvas->Button(-text => $label, | ||
| 79 | -width => ${$size}[0]/6.68-4, | ||
| 80 | -height => ${$size}[1]/16, | ||
| 81 | -pady => 1, | ||
| 82 | -command => $action); | ||
| 83 | $canvas->createWindow(${$location}[0] ,${$location}[1], | ||
| 84 | -anchor => "nw", | ||
| 85 | -window => $canvas->{"btn$id"}); | ||
| 86 | } | ||
| 87 | sub aTC{ | ||
| 88 | my ($self,$canvas, $id, $text, $location, $size, $action)=@_; | ||
| 89 | $canvas->{"textctrl$id"}=$canvas->Entry( | ||
| 90 | -bg => 'white', | ||
| 91 | -width => (${$size}[0]+32)/8); | ||
| 92 | $canvas->createWindow(${$location}[0] ,${$location}[1], | ||
| 93 | -anchor => "nw", | ||
| 94 | -window => $canvas->{"textctrl$id"} ); | ||
| 95 | } | ||
| 96 | sub aST{ | ||
| 97 | my ($self,$canvas, $id, $text, $location)=@_; | ||
| 98 | $canvas->{"stattext$id"}=$canvas->createText(${$location}[0] ,${$location}[1], | ||
| 99 | -anchor => "nw", | ||
| 100 | -text => $text, | ||
| 101 | -font =>'medium' | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | sub aCB{ #adapted from http://www.perlmonks.org/?node_id=799673 | ||
| 105 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 106 | if (defined $oVars{$label}){ | ||
| 107 | my @strings2 = split(",",$oVars{$label}); | ||
| 108 | $iVars{"combo$id"}=$strings2[0]; | ||
| 109 | $canvas->{"combo$id"}=$canvas->BrowseEntry( | ||
| 110 | -variable => \($iVars{"combo$id"}), | ||
| 111 | -listheight => scalar @strings2, | ||
| 112 | -listwidth => (${$size}[0]-20)/2, | ||
| 113 | -browsecmd => $action); | ||
| 114 | foreach (@strings2){ $canvas->{"combo$id"}->insert("end",$_);} | ||
| 115 | $canvas->{"cont$id"}=$canvas->{"combo$id"}->Subwidget('slistbox')->Subwidget('scrolled');#?? | ||
| 116 | |||
| 117 | $canvas->createWindow(${$location}[0] ,${$location}[1], | ||
| 118 | -width => (${$size}[0]-20)*1.5, | ||
| 119 | -height => ${$size}[1], | ||
| 120 | -anchor => "nw", | ||
| 121 | -window =>$canvas->{"combo$id"}); | ||
| 122 | } | ||
| 123 | |||
| 124 | else {print "Combo options not defined for 'combo$id' with label $label\n"} | ||
| 125 | |||
| 126 | } | ||
| 127 | sub aMB{ | ||
| 128 | my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 129 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 130 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 131 | |||
| 132 | |||
| 133 | if ($type eq "menuhead"){ | ||
| 134 | $currentMenu="menu".$id; | ||
| 135 | $self ->{$currentMenu} = $self ->{"menubar"}->cascade(-label => "~$label") | ||
| 136 | } | ||
| 137 | elsif ($type eq "radio"){ | ||
| 138 | $self ->{$currentMenu}->radiobutton(-label => $label); | ||
| 139 | } | ||
| 140 | elsif ($type eq "check"){ | ||
| 141 | $self ->{$currentMenu}->checkbutton(-label => $label); | ||
| 142 | } | ||
| 143 | elsif ($type eq "separator"){ | ||
| 144 | $self ->{$currentMenu}->separator; | ||
| 145 | } | ||
| 146 | else{ | ||
| 147 | if($currentMenu!~m/$label/){ | ||
| 148 | $self ->{$currentMenu}->command(-label => $label, -command =>$action); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | # logging menu generator print "$currentMenu---$id----$label---$type\n"; | ||
| 152 | return $currentMenu; | ||
| 153 | } | ||
| 154 | sub aSP{ | ||
| 155 | my ($self,$canvas, $id, $panelType, $content, $location, $size)=@_; | ||
| 156 | |||
| 157 | if ($panelType eq "I"){ # Image panels start with I | ||
| 158 | if (! -e $content){ return; } | ||
| 159 | no warnings; # sorry about that...suppresses a "Useless string used in void context" | ||
| 160 | my $image = Image::Magick->new; | ||
| 161 | my $r = $image->Read("$content"); | ||
| 162 | if ($image){ | ||
| 163 | my $bmp; # used to hold the bitmap. | ||
| 164 | my $geom=${$size}[0]."x".${$size}[1]."!"; | ||
| 165 | $image->Scale(geometry => $geom); | ||
| 166 | $bmp = ( $image->ImageToBlob(magick=>'jpg') )[0]; | ||
| 167 | $canvas->{"image".($id+1)}=$canvas->createImage(${$location}[0],${$location}[1], | ||
| 168 | -anchor=>"nw", | ||
| 169 | -image => $canvas->Photo(#"img$id", | ||
| 170 | -format=>'jpeg', | ||
| 171 | -data=>encode_base64($bmp) )); | ||
| 172 | } | ||
| 173 | else {"print failed to load image $content \n";} | ||
| 174 | } | ||
| 175 | if ($panelType eq "T"){ | ||
| 176 | $id++; | ||
| 177 | $canvas->{"TextCtrl$id"}=$canvas->Text( | ||
| 178 | -bg => 'white', | ||
| 179 | -width => (${$size}[0])/7, | ||
| 180 | -height => (${$size}[1]+12)/15); | ||
| 181 | $canvas->{"TextCtrl$id"}->insert('end',$content); | ||
| 182 | $canvas->createWindow(${$location}[0] ,${$location}[1], | ||
| 183 | -anchor => "nw", | ||
| 184 | -window => $canvas->{"TextCtrl$id"}); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 191 | sub addWidget{ | ||
| 192 | push (@widgets,shift ); | ||
| 193 | } | ||
| 194 | sub addStyle{ | ||
| 195 | my ($name,$style)=@_; | ||
| 196 | $styles{$name}=$style; | ||
| 197 | } | ||
| 198 | sub addVar{ | ||
| 199 | my ($varName,$value)=@_; | ||
| 200 | $oVars{$varName}=$value; | ||
| 201 | } | ||
| 202 | |||
| 203 | # Functions for internal use | ||
| 204 | sub getSize{ | ||
| 205 | my ($self,$id)=@_; | ||
| 206 | my $found=getItem($self,$id); | ||
| 207 | return ( $found!=-1) ? $widgets[$found][5]:0; | ||
| 208 | |||
| 209 | } | ||
| 210 | sub getLocation{ | ||
| 211 | my ($self,$id)=@_; | ||
| 212 | my $found=getItem($self,$id); | ||
| 213 | return ( $found!=-1) ? $widgets[$found][4]:0; | ||
| 214 | |||
| 215 | } | ||
| 216 | sub getItem{ | ||
| 217 | my ($self,$id)=@_; | ||
| 218 | $id=~s/[^\d]//g; | ||
| 219 | my $i=0; my $found=-1; | ||
| 220 | while ($i<@widgets){ | ||
| 221 | if ($widgets[$i][1]==$id) { | ||
| 222 | $found=$i; | ||
| 223 | } | ||
| 224 | $i++; | ||
| 225 | } | ||
| 226 | return $found; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub setScale{ | ||
| 230 | $winScale=shift; | ||
| 231 | }; | ||
| 232 | |||
| 233 | sub getFrame{ | ||
| 234 | my $self=shift; | ||
| 235 | return $self; | ||
| 236 | }; | ||
| 237 | |||
| 238 | # The functions for GUI Interactions | ||
| 239 | #Static Text functions | ||
| 240 | sub setLabel{ | ||
| 241 | my ($self,$id,$text)=@_; | ||
| 242 | my $location=$widgets[getItem($self,$id)][3]; | ||
| 243 | $frame->delete($frame->{"$id"}); | ||
| 244 | $frame->{"$id"}=$frame->createText(${$location}[0] ,${$location}[1], | ||
| 245 | -anchor => "nw", | ||
| 246 | -text => $text, | ||
| 247 | -font =>'medium' | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | |||
| 251 | #Image functions | ||
| 252 | sub setImage{ | ||
| 253 | my ($self,$id,$file)=@_; | ||
| 254 | my $location=getLocation($self,$id,\@widgets); | ||
| 255 | my $size=getSize($self,$id,\@widgets); | ||
| 256 | if ($size){ | ||
| 257 | my $image = Image::Magick->new; | ||
| 258 | my $r = $image->Read("$file"); | ||
| 259 | if ($image){ | ||
| 260 | my $bmp; # used to hold the bitmap. | ||
| 261 | my $geom=${$size}[0]."x".${$size}[1]."!"; | ||
| 262 | $image->Scale(geometry => $geom); | ||
| 263 | $bmp = ( $image->ImageToBlob(magick=>'jpg') )[0]; | ||
| 264 | $frame->{"$id"}=$frame->createImage(${$location}[0],${$location}[1], | ||
| 265 | -anchor=>"nw", | ||
| 266 | -image => $frame->Photo(#"img$id", | ||
| 267 | -format=>'jpeg', | ||
| 268 | -data=>encode_base64($bmp) )); | ||
| 269 | } | ||
| 270 | else {"print failed to load image $file \n";} | ||
| 271 | } | ||
| 272 | else {print "Panel not found"} | ||
| 273 | |||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | #Text input functions | ||
| 278 | sub getValue{ | ||
| 279 | my ($self,$id)=@_; | ||
| 280 | if ($id =~/TextCtrl/){return $frame->{$id}->get('1.0','end-1c'); } | ||
| 281 | else { | ||
| 282 | if (exists $iVars{$id}){ | ||
| 283 | return $iVars{$id} | ||
| 284 | } | ||
| 285 | else{ return $frame->{$id}->get(); } | ||
| 286 | } | ||
| 287 | |||
| 288 | } | ||
| 289 | sub setValue{ | ||
| 290 | my ($self,$id,$text)=@_; | ||
| 291 | $frame->{"$id"}->delete('0.0','end'); | ||
| 292 | $frame->{"$id"}->insert("end",$text); | ||
| 293 | } | ||
| 294 | sub appendValue{ | ||
| 295 | my ($self,$id,$text)=@_; | ||
| 296 | $frame->{$id}->insert('end',$text); | ||
| 297 | } | ||
| 298 | |||
| 299 | #Message box, Fileselector and Dialog Boxes | ||
| 300 | sub showFileSelectorDialog{ | ||
| 301 | |||
| 302 | my ($self, $message,$load) = @_; | ||
| 303 | my $filename; | ||
| 304 | if ($load){ | ||
| 305 | $filename = $self->getOpenFile( -title => $message, | ||
| 306 | -defaultextension => '.txt', -initialdir => '.' ); | ||
| 307 | warn "Opened $filename\n"; | ||
| 308 | } | ||
| 309 | else{ | ||
| 310 | $filename = $self->getSaveFile( -title => $message, | ||
| 311 | -defaultextension => '.txt', -initialdir => '.' ); | ||
| 312 | warn "Saved $filename\n"; | ||
| 313 | |||
| 314 | } | ||
| 315 | return $filename; | ||
| 316 | |||
| 317 | }; | ||
| 318 | sub showDialog{ | ||
| 319 | my ($self, $title, $message,$response,$icon) = @_; | ||
| 320 | my %responses=( YNC=>'YesNoCancel', | ||
| 321 | YN =>'YesNo', | ||
| 322 | OK => 'Ok', | ||
| 323 | OKC=>'OkCancel' ); | ||
| 324 | |||
| 325 | my %icons= ( "!"=>"warning", | ||
| 326 | "?"=>"question", | ||
| 327 | "E"=>"error", | ||
| 328 | "H"=>"warning", | ||
| 329 | "I"=>"info" ); | ||
| 330 | $response=$response?$responses{$response}:"ok"; | ||
| 331 | $icon=$icon?$icons{$icon}:"info"; | ||
| 332 | my $answer= $self->messageBox( | ||
| 333 | -icon => $icon, -message => $message, -title => $title, -type => $response); | ||
| 334 | return (($answer eq "Ok")||($answer eq "Yes")) | ||
| 335 | }; | ||
| 336 | |||
| 337 | # Quit | ||
| 338 | sub quit{ | ||
| 339 | my ($self) = @_; | ||
| 340 | $self ->destroy; | ||
| 341 | } | ||
| 342 | 1; |