| 1 | package GFwxFrame; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.09'; | ||
| 7 | |||
| 8 | use Exporter 'import'; | ||
| 9 | our @EXPORT = qw<addWidget addVar setScale>; | ||
| 10 | |||
| 11 | use Wx qw(wxMODERN wxTE_PASSWORD wxTE_PROCESS_ENTER wxDEFAULT wxNORMAL | ||
| 12 | wxFONTENCODING_DEFAULT wxTE_MULTILINE wxHSCROLL wxDefaultPosition wxFD_SAVE | ||
| 13 | wxYES wxFD_OPEN wxFD_FILE_MUST_EXIST wxFD_CHANGE_DIR wxID_CANCEL | ||
| 14 | wxYES_NO wxCANCEL wxOK wxCENTRE wxICON_EXCLAMATION wxICON_HAND | ||
| 15 | wxICON_ERROR wxICON_QUESTION wxICON_INFORMATION wxCB_DROPDOWN); | ||
| 16 | |||
| 17 | use Wx::Event qw( EVT_BUTTON EVT_TEXT_ENTER EVT_UPDATE_UI EVT_MENU EVT_COMBOBOX); | ||
| 18 | use Wx::Perl::Imagick; #for image panels | ||
| 19 | |||
| 20 | use base qw(Wx::Frame); # Inherit from Wx::Frame | ||
| 21 | |||
| 22 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 23 | my @widgets=(); | ||
| 24 | my %iVars=(); #vars for interface operation (e.g. | ||
| 25 | my %oVars=(); #vars for interface | ||
| 26 | my %styles; | ||
| 27 | |||
| 28 | my $lastMenuLabel; #bug workaround in menu generator | ||
| 29 | |||
| 30 | our $winScale=6.5; | ||
| 31 | my $font = Wx::Font->new( 3*$winScale, | ||
| 32 | wxDEFAULT, | ||
| 33 | wxNORMAL, | ||
| 34 | wxNORMAL, | ||
| 35 | 0, | ||
| 36 | "", | ||
| 37 | wxFONTENCODING_DEFAULT); | ||
| 38 | |||
| 39 | sub new | ||
| 40 | { | ||
| 41 | my $class = shift; | ||
| 42 | my $self = $class->SUPER::new(@_); # call the superclass' constructor | ||
| 43 | |||
| 44 | # Then define a Panel to put the content on | ||
| 45 | my $panel = Wx::Panel->new( $self,-1); # parent, id | ||
| 46 | setupContent($self,$panel); #then add content | ||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 51 | |||
| 52 | sub setupContent{ | ||
| 53 | my ($self,$canvas)=@_; | ||
| 54 | $self ->{"menubar"}=undef; | ||
| 55 | my $currentMenu; | ||
| 56 | foreach my $widget (@widgets){ | ||
| 57 | my @params=@$widget; | ||
| 58 | my $wtype=shift @params; | ||
| 59 | if ($wtype eq "btn") {aBt($self, $canvas, @params);} | ||
| 60 | elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);} | ||
| 61 | elsif ($wtype eq "stattext") {aST($self, $canvas, @params);} | ||
| 62 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 63 | elsif ($wtype eq "combo") {aCB($self, $canvas, @params);} | ||
| 64 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 65 | elsif ($wtype eq "mb") | ||
| 66 | { | ||
| 67 | if (! $self->{"menubar"}){ | ||
| 68 | $self ->{"menubar"} = Wx::MenuBar->new(); | ||
| 69 | $self->SetMenuBar($self ->{"menubar"}); | ||
| 70 | } | ||
| 71 | $currentMenu=aMB($self,$canvas,$currentMenu,@params) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub aCB{ | ||
| 76 | my ($self,$panel, $id, $label, $location, $size, $action)=@_; | ||
| 77 | if (defined $oVars{$label}){ | ||
| 78 | my @strings2 = split(",",$oVars{$label}); | ||
| 79 | $self->{"combo".$id}= Wx::ComboBox->new($panel, $id, $strings2[0],$location, Wx::Size->new($$size[0], $$size[1]),\@strings2,wxCB_DROPDOWN); | ||
| 80 | EVT_COMBOBOX( $self, $id, $action); | ||
| 81 | } | ||
| 82 | else {print "Combo options not defined for 'combo$id' with label $label\n"} | ||
| 83 | } | ||
| 84 | |||
| 85 | sub aMB{ | ||
| 86 | my ($self,$panel,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 87 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 88 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 89 | |||
| 90 | |||
| 91 | if ($type eq "menuhead"){ | ||
| 92 | $currentMenu="menu".$id; | ||
| 93 | $self ->{$currentMenu} = Wx::Menu->new(); | ||
| 94 | $self ->{"menubar"}->Append($self ->{$currentMenu}, $label); | ||
| 95 | } | ||
| 96 | elsif ($type eq "radio"){ | ||
| 97 | $self ->{$currentMenu}->AppendRadioItem($id, $label); | ||
| 98 | EVT_MENU( $self, $id, $action ) | ||
| 99 | } | ||
| 100 | elsif ($type eq "check"){ | ||
| 101 | $self ->{$currentMenu}->AppendCheckItem($id, $label); | ||
| 102 | EVT_MENU( $self, $id, $action ) | ||
| 103 | } | ||
| 104 | elsif ($type eq "separator"){ | ||
| 105 | $self ->{$currentMenu}->AppendSeparator(); | ||
| 106 | } | ||
| 107 | else{ | ||
| 108 | if($currentMenu!~m/$label/){ | ||
| 109 | $self ->{$currentMenu}->Append($id, $label); | ||
| 110 | EVT_MENU( $self, $id, $action ) | ||
| 111 | } | ||
| 112 | } | ||
| 113 | # logging menu generator print "$currentMenu---$id----$label---$type\n"; | ||
| 114 | return $currentMenu; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub aBt{ | ||
| 118 | my ($self,$panel, $id, $label, $location, $size, $action)=@_; | ||
| 119 | $self->{"btn".$id} = Wx::Button->new( $panel, # parent | ||
| 120 | $id, # ButtonID | ||
| 121 | $label, # label | ||
| 122 | $location, # position | ||
| 123 | $size # size | ||
| 124 | ); | ||
| 125 | EVT_BUTTON( $self, $id, $action ); #object to bind to, buton id, and subroutine to call | ||
| 126 | } | ||
| 127 | |||
| 128 | sub aTC{ | ||
| 129 | my ($self,$panel, $id, $text, $location, $size, $action)=@_; | ||
| 130 | $self->{"textctrl".$id} = Wx::TextCtrl->new( | ||
| 131 | $panel, | ||
| 132 | $id, | ||
| 133 | $text, | ||
| 134 | $location, | ||
| 135 | $size, | ||
| 136 | wxTE_PROCESS_ENTER | ||
| 137 | ); | ||
| 138 | EVT_TEXT_ENTER( $self, $id, $action); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub aST{ | ||
| 142 | my ($self,$panel, $id, $text, $location)=@_; | ||
| 143 | $self->{"stattext".$id} = Wx::StaticText->new( $panel, # parent | ||
| 144 | $id, # id | ||
| 145 | $text, # label | ||
| 146 | $location # position | ||
| 147 | ); | ||
| 148 | $self->{"stattext".$id}->SetFont($font); | ||
| 149 | } | ||
| 150 | sub aSP{ | ||
| 151 | my ($self,$panel, $id, $panelType, $content, $location, $size)=@_; | ||
| 152 | $self->{"subpanel".$id}= Wx::Panel->new( $panel,# parent | ||
| 153 | $id,# id | ||
| 154 | $location, | ||
| 155 | $size | ||
| 156 | ); | ||
| 157 | |||
| 158 | if ($panelType eq "I"){ # Image panels start with I | ||
| 159 | if (! -e $content){ return; } | ||
| 160 | no warnings; # sorry about that...suppresses a "Useless string used in void context" | ||
| 161 | my $image = Wx::Perl::Imagick->new($content); | ||
| 162 | if ($image){ | ||
| 163 | my $bmp; # used to hold the bitmap. | ||
| 164 | my $geom=${$size}[0]."x".${$size}[1]."!"; | ||
| 165 | $image->Resize(geometry => $geom); | ||
| 166 | $bmp = $image->ConvertToBitmap(); | ||
| 167 | if( $bmp->Ok() ) { | ||
| 168 | $self->{"Image".($id+1)}= Wx::StaticBitmap->new($self->{"subpanel".$id}, $id+1, $bmp); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | else {"print failed to load image $content \n";} | ||
| 172 | } | ||
| 173 | if ($panelType eq "T"){ # handle | ||
| 174 | |||
| 175 | $self->{"TextCtrl".($id+1)} = Wx::TextCtrl->new( | ||
| 176 | $self->{"subpanel".$id}, | ||
| 177 | $id+1, | ||
| 178 | $content, | ||
| 179 | wxDefaultPosition, | ||
| 180 | $size, | ||
| 181 | wxTE_MULTILINE|wxHSCROLL | ||
| 182 | ); | ||
| 183 | $self->{"TextCtrl".($id+1)}->SetFont(Wx::Font->new(10, wxMODERN, wxNORMAL, wxNORMAL )); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 189 | sub addWidget{ | ||
| 190 | push (@widgets,shift ); | ||
| 191 | } | ||
| 192 | sub addStyle{ | ||
| 193 | my ($name,$style)=@_; | ||
| 194 | $styles{$name}=$style; | ||
| 195 | } | ||
| 196 | sub addVar{ | ||
| 197 | my ($varName,$value)=@_; | ||
| 198 | $oVars{$varName}=$value; | ||
| 199 | } | ||
| 200 | |||
| 201 | # Functions for internal use | ||
| 202 | sub getSize{ | ||
| 203 | my ($self,$id,$arrayRef)=@_; | ||
| 204 | my $found=getItem($self,$id,$arrayRef); | ||
| 205 | return ( $found!=-1) ? $$arrayRef[$found][5]:0; | ||
| 206 | |||
| 207 | } | ||
| 208 | sub getLocation{ | ||
| 209 | my ($self,$id,$arrayRef)=@_; | ||
| 210 | my $found=getItem($self,$id,$arrayRef); | ||
| 211 | return ( $found!=-1) ? $$arrayRef[$found][4]:0; | ||
| 212 | |||
| 213 | } | ||
| 214 | sub getItem{ | ||
| 215 | my ($self,$id,$arrayRef)=@_; | ||
| 216 | my $i=0; my $found=-1; | ||
| 217 | while ($i<@$arrayRef){ | ||
| 218 | if ($$arrayRef[$i][1]==$id) { | ||
| 219 | $found=$i; | ||
| 220 | } | ||
| 221 | $i++; | ||
| 222 | } | ||
| 223 | return $found; | ||
| 224 | } | ||
| 225 | sub setScale{ | ||
| 226 | $winScale=shift; | ||
| 227 | $font = Wx::Font->new( 3*$winScale, | ||
| 228 | wxDEFAULT, | ||
| 229 | wxNORMAL, | ||
| 230 | wxNORMAL, | ||
| 231 | 0, | ||
| 232 | "", | ||
| 233 | wxFONTENCODING_DEFAULT); | ||
| 234 | } | ||
| 235 | sub reSize{ | ||
| 236 | my ($self,$id,$newSize)=@_; | ||
| 237 | } | ||
| 238 | # The functions for GUI Interactions | ||
| 239 | #Static Text functions | ||
| 240 | sub setLabel{ | ||
| 241 | my ($self,$id,$text)=@_; | ||
| 242 | $self->{$id}->SetLabel($text); | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | #Image functions | ||
| 247 | sub setImage{ | ||
| 248 | my ($self,$id,$file)=@_; | ||
| 249 | $id=~s/[^\d]//g; | ||
| 250 | my $size=getSize($self,$id,\@widgets); | ||
| 251 | if ($size){ | ||
| 252 | my $image = Wx::Perl::Imagick->new($file); | ||
| 253 | if ($image){ | ||
| 254 | my $bmp; # used to hold the bitmap. | ||
| 255 | my $geom=${$size}[0]."x".${$size}[1]."!"; | ||
| 256 | $image->Resize(geometry => $geom); | ||
| 257 | $bmp = $image->ConvertToBitmap(); | ||
| 258 | if( $bmp->Ok() ) { | ||
| 259 | $self->{"Image".($id+1)}= Wx::StaticBitmap->new($self->{"subpanel".$id}, $id+1, $bmp); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | else {"print failed to load image $file \n";} | ||
| 263 | } | ||
| 264 | else {print "Panel not found"} | ||
| 265 | |||
| 266 | |||
| 267 | } | ||
| 268 | |||
| 269 | #Text input functions | ||
| 270 | sub getValue{ | ||
| 271 | my ($self,$id)=@_; | ||
| 272 | if (!$self->{$id}) {print "can not get Value for widget ". $id.": Widget not found\n";} | ||
| 273 | else { $self->{$id}->GetValue();} | ||
| 274 | } | ||
| 275 | sub setValue{ | ||
| 276 | my ($self,$id,$text)=@_; | ||
| 277 | if (!$self->{$id}) {print "can not set Value for widget ". $id.": Widget not found\n";} | ||
| 278 | else { $self->{$id}->SetValue($text);} | ||
| 279 | } | ||
| 280 | sub appendValue{ | ||
| 281 | my ($self,$id,$text)=@_; | ||
| 282 | if (!$self->{$id}) {print "can not Append Value to widget ". $id.": Widget not found\n";} | ||
| 283 | else { $self->{$id}->AppendText($text);} | ||
| 284 | } | ||
| 285 | |||
| 286 | #Message box, Fileselector and Dialog Boxes | ||
| 287 | sub showFileSelectorDialog{ | ||
| 288 | |||
| 289 | my ($self, $message,$load) = @_; | ||
| 290 | my $loadOptions=wxFD_OPEN|wxFD_FILE_MUST_EXIST|wxFD_CHANGE_DIR; | ||
| 291 | my $saveOptions=wxFD_SAVE|wxFD_CHANGE_DIR; | ||
| 292 | my $fd = Wx::FileDialog->new( $self, $message, ".", q{}, | ||
| 293 | "All files|*|Data (*.dat)|*.dat", | ||
| 294 | $load?$loadOptions:$saveOptions, | ||
| 295 | wxDefaultPosition); | ||
| 296 | if ($fd->ShowModal == wxID_CANCEL) { | ||
| 297 | print "Data import cancelled\n"; | ||
| 298 | return; | ||
| 299 | }; | ||
| 300 | return $fd->GetPath; | ||
| 301 | |||
| 302 | }; | ||
| 303 | |||
| 304 | sub showDialog{ | ||
| 305 | my ($self, $title, $message,$response,$icon) = @_; | ||
| 306 | my %responses=( YNC=>wxYES_NO|wxCANCEL|wxCENTRE, | ||
| 307 | YN =>wxYES_NO|wxCENTRE, | ||
| 308 | OK =>wxOK|wxCENTRE, | ||
| 309 | OKC=>wxOK|wxCANCEL|wxCENTRE ); | ||
| 310 | |||
| 311 | my %icons= ( "!"=>wxICON_EXCLAMATION, | ||
| 312 | "?"=>wxICON_QUESTION, | ||
| 313 | "E"=>wxICON_ERROR, | ||
| 314 | "H"=>wxICON_HAND, | ||
| 315 | "I"=>wxICON_INFORMATION ); | ||
| 316 | $response=$response?$responses{$response}:wxOK|wxCENTRE; | ||
| 317 | $icon=$icon?$icons{$icon}:wxICON_INFORMATION; | ||
| 318 | my $answer= Wx::MessageBox( $message, | ||
| 319 | $title, | ||
| 320 | $response|$icon, | ||
| 321 | $self); | ||
| 322 | return (($answer==wxOK)||($answer==wxYES)) | ||
| 323 | |||
| 324 | }; | ||
| 325 | |||
| 326 | # quit | ||
| 327 | sub quit{ | ||
| 328 | my ($self) = @_; | ||
| 329 | $self ->Close(1); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | package GFwx; | ||
| 334 | |||
| 335 | use strict; | ||
| 336 | use warnings; | ||
| 337 | |||
| 338 | our $VERSION = '0.09'; | ||
| 339 | |||
| 340 | use parent qw(Wx::App); # Inherit from Wx::App | ||
| 341 | use Exporter 'import'; | ||
| 342 | import GFwxFrame qw<addWidget addVar setScale>; | ||
| 343 | |||
| 344 | our @EXPORT = qw<addWidget addVar setScale $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 345 | our $frame; | ||
| 346 | |||
| 347 | our $winX=30; | ||
| 348 | our $winY=30; | ||
| 349 | our $winWidth; | ||
| 350 | our $winHeight; | ||
| 351 | our $winTitle; | ||
| 352 | our $winScale=6.5; | ||
| 353 | |||
| 354 | sub OnInit | ||
| 355 | { | ||
| 356 | my $self=shift; | ||
| 357 | $frame = GFwxFrame->new( undef, # Parent window | ||
| 358 | -1, # Window id | ||
| 359 | $winTitle, # Title | ||
| 360 | [$winX,$winY], # position [X, Y] | ||
| 361 | [$winWidth,$winHeight] # size [$winWidth, $winHeight] | ||
| 362 | ); | ||
| 363 | $self->SetTopWindow($frame); # Define the toplevel window | ||
| 364 | $frame->Show(1); # Show the frame | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | sub getFrame{ | ||
| 369 | return $frame; | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | 1; | ||
| 374 | |||
| 375 |