| 1 | package GFwin32; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.10'; | ||
| 6 | |||
| 7 | use Win32::GUI; | ||
| 8 | use Imager; | ||
| 9 | |||
| 10 | use Exporter 'import'; ##somefunctions are always passed back to GUIDeFATE.pm | ||
| 11 | our @EXPORT = qw<addWidget addVar setScale MainLoop $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 12 | our $frame; # The frame which is the parent of all the widgets | ||
| 13 | # ever widget is referenced by an id and is accessible by $frame -> {id} | ||
| 14 | |||
| 15 | our $winX=30; # These are the window dimensions and are modified by GUIDeFATE | ||
| 16 | our $winY=30; | ||
| 17 | our $winWidth; | ||
| 18 | our $winHeight; | ||
| 19 | our $winTitle; | ||
| 20 | our $winScale=6.5; # This allows the window to be scaled | ||
| 21 | |||
| 22 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 23 | # It may be logical to group them as one array conatining eveything and this | ||
| 24 | # may be the way to go when ready to push out v1.0 | ||
| 25 | my @widgets=(); | ||
| 26 | my %iVars=(); #vars for interface operation (e.g. | ||
| 27 | my %oVars=(); #vars for interface creation (e.g. list of options) | ||
| 28 | my %styles; # styles is a future mod that allows widgets to be styled | ||
| 29 | |||
| 30 | my $lastMenuLabel; | ||
| 31 | |||
| 32 | sub new | ||
| 33 | { | ||
| 34 | my $class=shift; | ||
| 35 | my $self={}; | ||
| 36 | bless ($self,$class); | ||
| 37 | $self->{Window}= new Win32::GUI::Window( | ||
| 38 | -name => "GF", | ||
| 39 | -title => $winTitle, | ||
| 40 | -pos => [ $winX,$winY ], | ||
| 41 | -size => [ $winWidth+10, $winHeight+50 ], | ||
| 42 | ); | ||
| 43 | $self->{Window}->Show(); | ||
| 44 | $self->{font} = Win32::GUI::Font->new( | ||
| 45 | -name => "Arial", | ||
| 46 | -size => 16, | ||
| 47 | ); | ||
| 48 | &setupContent($self, $self->{Window} ); | ||
| 49 | |||
| 50 | $self->{Window}->Show(); | ||
| 51 | return $self; | ||
| 52 | }; | ||
| 53 | |||
| 54 | sub MainLoop{ | ||
| 55 | Win32::GUI::Dialog(); | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub GF_Terminate { return -1; } | ||
| 59 | |||
| 60 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 61 | sub setupContent{ | ||
| 62 | my ($self, $canvas)=@_; | ||
| 63 | $self ->{"menubar"}=undef; | ||
| 64 | my $currentMenu; | ||
| 65 | foreach my $widget (@widgets){ | ||
| 66 | my @params=@$widget; | ||
| 67 | my $wtype=shift @params; | ||
| 68 | if ($wtype eq "btn") {aBt($self, $canvas, @params);} | ||
| 69 | elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);} | ||
| 70 | elsif ($wtype eq "stattext") {aST($self, $canvas, @params);} | ||
| 71 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 72 | elsif ($wtype eq "combo") {aCB($self, $canvas, @params);} | ||
| 73 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 74 | elsif ($wtype eq "mb") | ||
| 75 | { | ||
| 76 | if (! $self->{"menubar"}){ | ||
| 77 | $self ->{"menubar"} = Win32::GUI::Menu->new();; | ||
| 78 | $canvas->SetMenu($self ->{"menubar"}); | ||
| 79 | } | ||
| 80 | $currentMenu=aMB($self,$canvas,$currentMenu,@params) | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | # these functions convert the parameters of the widget into actual widgets | ||
| 85 | sub aBt{ # creates buttons | ||
| 86 | my ($self,$frame, $id, $label, $location, $size, $action)=@_; | ||
| 87 | $self->{"btn$id"}=$frame->AddButton( | ||
| 88 | -name => "btn$id", | ||
| 89 | -text => $label, | ||
| 90 | -pos => $location, | ||
| 91 | -size => $size, | ||
| 92 | -onClick => $action, | ||
| 93 | ); | ||
| 94 | # button id are "btn".$id, action is generally also a function called "btn".$id | ||
| 95 | # referenced by $frame->{"btn".$id} | ||
| 96 | } | ||
| 97 | sub aTC{ # single line text entry | ||
| 98 | my ($self,$frame, $id, $text, $location, $size, $action)=@_; | ||
| 99 | $self->{"textctrl$id"} = $frame->AddTextfield( | ||
| 100 | -text => $text, | ||
| 101 | -pos => $location, | ||
| 102 | -size => $size, | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | sub aST{ #static texts | ||
| 106 | my ($self,$frame, $id, $text, $location)=@_; | ||
| 107 | $self->{"stattext$id"} = $frame->AddLabel( | ||
| 108 | -text => $text, | ||
| 109 | -font => $self->{font}, | ||
| 110 | -pos => $location, | ||
| 111 | -wrap => 0, | ||
| 112 | -truncate => 0, | ||
| 113 | -foreground => [255, 0, 0], | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | sub aCB{ | ||
| 117 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 118 | $self->{"combo$id"}=$canvas->AddCombobox( | ||
| 119 | -name => "combo_box1", | ||
| 120 | -size => $size, | ||
| 121 | -pos => $location, | ||
| 122 | -dropdownlist=> 0, | ||
| 123 | -onChange =>$action, | ||
| 124 | ); | ||
| 125 | my @strings2 = split(",",$oVars{$label}); | ||
| 126 | $iVars{"combo$id"}=$strings2[0]; | ||
| 127 | foreach (@strings2){ $self->{"combo$id"}->InsertItem($_);} | ||
| 128 | |||
| 129 | } | ||
| 130 | sub aMB{ #parses the menu items into a menu. menus may need to be a child of main window | ||
| 131 | my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 132 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 133 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 134 | |||
| 135 | |||
| 136 | if ($type eq "menuhead"){ #the label of the menu | ||
| 137 | $currentMenu="menu".$id; | ||
| 138 | $self ->{$currentMenu} = $self ->{"menubar"}->AddMenuButton( -text, $label); | ||
| 139 | } | ||
| 140 | elsif ($type eq "radio"){ #menu items which are radio buttons in tk there is no function called | ||
| 141 | |||
| 142 | } | ||
| 143 | elsif ($type eq "check"){ #menu items which are check boxes in tk there is no function called | ||
| 144 | |||
| 145 | } | ||
| 146 | elsif ($type eq "separator"){ #separators | ||
| 147 | |||
| 148 | } | ||
| 149 | else{ | ||
| 150 | if($currentMenu!~m/$label/){ | ||
| 151 | $self ->{"menu$id"} = $self ->{$currentMenu}->AddMenuItem( | ||
| 152 | -text => $label, | ||
| 153 | -id =>$id, | ||
| 154 | -name => "menu$id", | ||
| 155 | -onClick => $action, | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | } | ||
| 160 | $canvas->SetMenu($self ->{"menubar"}); | ||
| 161 | return $currentMenu; | ||
| 162 | } | ||
| 163 | sub aSP{ | ||
| 164 | my ($self,$frame, $id, $panelType, $content, $location, $size)=@_; ##image Id must endup $id+1 | ||
| 165 | if ($panelType eq "I"){ # Image panels start with I | ||
| 166 | $content=~s/^\s+|\s+$//g; | ||
| 167 | $self->{"Image$id"} = $frame->AddLabel( | ||
| 168 | -text => "GFPic", | ||
| 169 | -style => '14', | ||
| 170 | -visible => '1', | ||
| 171 | -background => [255,255,255], | ||
| 172 | -foreground => [0,0,0], | ||
| 173 | -pos => $location, | ||
| 174 | ); | ||
| 175 | # $self->{"Image$id"}->SetImage( new Win32::GUI::Bitmap("GFtmp.bmp") ); | ||
| 176 | if (-e $content){ | ||
| 177 | my $image = Imager->new; | ||
| 178 | $image->read(file=>"$content"); | ||
| 179 | my $newimg = $image->scale(xpixels=>${$size}[0], ypixels=>${$size}[1],type=>'nonprop'); | ||
| 180 | $newimg->write(file=>"GFtmp.bmp"); | ||
| 181 | $self->{"Image$id"}->SetImage( new Win32::GUI::Bitmap("GFtmp.bmp") ); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | if ($panelType eq "T"){ # text entry panels start with T | ||
| 186 | $content=~s/^\s+|\s+$//g; | ||
| 187 | $id++; | ||
| 188 | $self->{"TextCtrl$id"} = $frame->AddTextfield( | ||
| 189 | -text => $content, | ||
| 190 | -multiline => 1, | ||
| 191 | -autohscroll => 1, | ||
| 192 | -autovscroll => 1, | ||
| 193 | -pos => $location, | ||
| 194 | -size => $size, | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 201 | sub addWidget{ | ||
| 202 | push (@widgets,shift ); | ||
| 203 | } | ||
| 204 | sub addStyle{ | ||
| 205 | my ($name,$style)=@_; | ||
| 206 | $styles{$name}=$style; | ||
| 207 | } | ||
| 208 | sub addVar{ | ||
| 209 | my ($varName,$value)=@_; | ||
| 210 | $oVars{$varName}=$value; | ||
| 211 | } | ||
| 212 | |||
| 213 | # Functions for internal use | ||
| 214 | sub getSize{ | ||
| 215 | my ($self,$id)=@_; | ||
| 216 | my $found=getItem($self,$id); | ||
| 217 | return ( $found!=-1) ? $widgets[$found][5]:0; | ||
| 218 | |||
| 219 | } | ||
| 220 | sub getLocation{ | ||
| 221 | my ($self,$id)=@_; | ||
| 222 | my $found=getItem($self,$id); | ||
| 223 | return ( $found!=-1) ? $widgets[$found][3]:0; | ||
| 224 | |||
| 225 | } | ||
| 226 | sub getItem{ | ||
| 227 | my ($self,$id)=@_; | ||
| 228 | $id=~s/[^\d]//g; | ||
| 229 | my $i=0; my $found=-1; | ||
| 230 | while ($i<@widgets){ | ||
| 231 | if ($widgets[$i][1]==$id) { | ||
| 232 | $found=$i; | ||
| 233 | } | ||
| 234 | $i++; | ||
| 235 | } | ||
| 236 | return $found; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub setScale{ | ||
| 240 | $winScale=shift; | ||
| 241 | }; | ||
| 242 | |||
| 243 | sub getFrame{ | ||
| 244 | my $self=shift; | ||
| 245 | return $self; | ||
| 246 | }; | ||
| 247 | |||
| 248 | # The functions for GUI Interactions | ||
| 249 | # Static Text functions | ||
| 250 | sub setLabel{ | ||
| 251 | my ($self,$id,$text)=@_; | ||
| 252 | $self->{$id}->Text(""); | ||
| 253 | # label persists...but not contains empty string? how to delete? | ||
| 254 | my $location=getLocation($self,$id,\@widgets); | ||
| 255 | $self->{$id} = $self->{Window}->AddLabel( | ||
| 256 | -text => $text, | ||
| 257 | -font => $self->{font}, | ||
| 258 | -pos => $location, | ||
| 259 | ) | ||
| 260 | } | ||
| 261 | |||
| 262 | #Image functions | ||
| 263 | sub setImage{ | ||
| 264 | my ($self,$id,$file)=@_; | ||
| 265 | my $size=getSize($self,$id,\@widgets); | ||
| 266 | my $location=getLocation($self,$id,\@widgets); | ||
| 267 | my $image = Imager->new; | ||
| 268 | $image->read(file=>"$file"); | ||
| 269 | my $newimg = $image->scale(xpixels=>${$size}[0], ypixels=>${$size}[1],type=>'nonprop'); | ||
| 270 | $newimg->write(file=>"GFtmp.bmp"); | ||
| 271 | $self->{"$id"}->SetImage( new Win32::GUI::Bitmap("GFtmp.bmp") );; | ||
| 272 | }; | ||
| 273 | |||
| 274 | #Text input functions | ||
| 275 | sub getValue{ | ||
| 276 | my ($self,$id)=@_; | ||
| 277 | return $self->{$id}->Text(); | ||
| 278 | # function to get value of an input box | ||
| 279 | } | ||
| 280 | sub setValue{ | ||
| 281 | my ($self,$id,$text)=@_; | ||
| 282 | $self->{$id}->Text( $text ); | ||
| 283 | } | ||
| 284 | sub appendValue{ | ||
| 285 | my ($self,$id,$text)=@_; | ||
| 286 | $self->{$id}->Append( $text ); | ||
| 287 | } | ||
| 288 | |||
| 289 | #Message box, Fileselector and Dialog Boxes | ||
| 290 | sub showFileSelectorDialog{ | ||
| 291 | |||
| 292 | my ($self, $message,$load) = @_; | ||
| 293 | my $filename; | ||
| 294 | if ($load){ | ||
| 295 | $filename = Win32::GUI::GetOpenFileName ( | ||
| 296 | -owner => $self->{Window}, | ||
| 297 | -title =>$message, | ||
| 298 | -directory => ".", | ||
| 299 | ) ; | ||
| 300 | warn "Opened $filename\n"; | ||
| 301 | } | ||
| 302 | else{ | ||
| 303 | $filename = Win32::GUI::GetSaveFileName ( | ||
| 304 | -title=>$message, | ||
| 305 | ); | ||
| 306 | warn "Saved $filename\n"; | ||
| 307 | } | ||
| 308 | return $filename; | ||
| 309 | }; | ||
| 310 | sub showDialog{ | ||
| 311 | my ($self, $title, $message,$response,$icon) = @_; | ||
| 312 | #http://search.cpan.org/~robertmay/Win32-GUI-1.06/docs/GUI/UserGuide/FAQ.pod#What_are_the_icon,_button_and_modality_values_for_MessageBox? | ||
| 313 | my %responses=( YNC=>3, | ||
| 314 | YN =>4, | ||
| 315 | OK =>0, | ||
| 316 | OKC=>1 ); | ||
| 317 | |||
| 318 | my %icons= ( "!"=>16, | ||
| 319 | "?"=>32, | ||
| 320 | "E"=>48, | ||
| 321 | "H"=>48, | ||
| 322 | "I"=>64); | ||
| 323 | $response=$response?$responses{$response}:0; | ||
| 324 | $icon=$icon?$icons{$icon}:64; | ||
| 325 | my $answer= $self-> Win32::GUI::MessageBox( | ||
| 326 | $message, | ||
| 327 | $title, | ||
| 328 | $icon+$response, | ||
| 329 | ); | ||
| 330 | return (($answer == 1)||($answer == 6)) | ||
| 331 | }; | ||
| 332 | |||
| 333 | # Quit | ||
| 334 | sub quit{ | ||
| 335 | my ($self) = @_; | ||
| 336 | $self ->GF_Terminate; | ||
| 337 | } | ||
| 338 | 1; |