| 1 | package GFgtk; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.10'; | ||
| 6 | |||
| 7 | use Glib ':constants'; # load Glib and import useful constants | ||
| 8 | use Gtk3 '-init'; # load Gtk3 module and initialize it | ||
| 9 | use Exporter 'import'; | ||
| 10 | our @EXPORT = qw<addWidget addVar setScale MainLoop $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 11 | our $frame; | ||
| 12 | |||
| 13 | our $winX=30; | ||
| 14 | our $winY=30; | ||
| 15 | our $winWidth; | ||
| 16 | our $winHeight; | ||
| 17 | our $winTitle="title"; | ||
| 18 | our $winScale=6.5; | ||
| 19 | |||
| 20 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 21 | my @widgets=(); | ||
| 22 | my %iVars=(); #vars for interface operation (e.g. | ||
| 23 | my %oVars=(); #vars for interface creation (e.g. list of options) | ||
| 24 | my %styles; | ||
| 25 | |||
| 26 | my $lastMenuLabel; #bug workaround in menu generator may be needed for submenus | ||
| 27 | |||
| 28 | sub new | ||
| 29 | { | ||
| 30 | my $class = shift; | ||
| 31 | my $self={}; | ||
| 32 | bless( $self, $class ); | ||
| 33 | $self->{window}= Gtk3::Window->new; | ||
| 34 | $self->{window}->signal_connect(destroy => sub { Gtk3::main_quit; }); | ||
| 35 | $self->{window}->resize($winWidth,$winHeight); | ||
| 36 | $self->{window} -> set_title($winTitle); | ||
| 37 | $self->{window}->signal_connect('delete-event' => sub { Gtk3->main_quit }); | ||
| 38 | |||
| 39 | $self->{panel}=Gtk3::Fixed->new(); | ||
| 40 | $self->{window}->add($self->{panel}); | ||
| 41 | |||
| 42 | setupContent($self,$self->{panel}); #then add content | ||
| 43 | $self->{window} ->show_all(); | ||
| 44 | return $self; | ||
| 45 | }; | ||
| 46 | |||
| 47 | sub MainLoop{ | ||
| 48 | Gtk3->main; | ||
| 49 | } | ||
| 50 | |||
| 51 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 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"} = Gtk3::MenuBar->new; | ||
| 69 | $canvas->put($self ->{"menubar"},0,0); | ||
| 70 | } | ||
| 71 | $currentMenu=aMB($self,$canvas,$currentMenu,@params) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub aBt{ | ||
| 76 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 77 | $canvas->{"btn$id"}=Gtk3::Button->new($label); | ||
| 78 | $canvas->{"btn$id"}->set_property("width-request", ${$size}[0]); | ||
| 79 | $canvas->{"btn$id"}->signal_connect( clicked => $action ); | ||
| 80 | $canvas->put($canvas->{"btn$id"},${$location}[0] ,${$location}[1]); | ||
| 81 | } | ||
| 82 | sub aTC{ | ||
| 83 | my ($self,$canvas, $id, $text, $location, $size, $action)=@_; | ||
| 84 | $canvas->{"textctrl$id"}=Gtk3::Entry->new(); | ||
| 85 | $canvas->{"textctrl$id"}->set_text($text); | ||
| 86 | $canvas->{"textctrl$id"}->set_property("width-request", ${$size}[0]); | ||
| 87 | $canvas->put($canvas->{"textctrl$id"},${$location}[0] ,${$location}[1]); | ||
| 88 | } | ||
| 89 | sub aST{ | ||
| 90 | my ($self,$canvas, $id, $text, $location)=@_; | ||
| 91 | $canvas->{"stattext$id"}=Gtk3::Label->new(); | ||
| 92 | $canvas->{"stattext$id"}->set_label($text); | ||
| 93 | $canvas->{"stattext$id"}->set_markup('<span size="xx-large" >'.$text.'</span>'); | ||
| 94 | $canvas->put($canvas->{"stattext$id"},${$location}[0] ,${$location}[1]); | ||
| 95 | } | ||
| 96 | sub aCB{ #adapted from http://www.perlmonks.org/?node_id=799673 | ||
| 97 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 98 | if (defined $oVars{$label}){ | ||
| 99 | my @strings2 = split(",",$oVars{$label}); | ||
| 100 | $iVars{"combo$id"}=$strings2[0]; | ||
| 101 | $canvas->{"combo$id"}=Gtk3::ComboBoxText->new(); | ||
| 102 | foreach (@strings2){ $canvas->{"combo$id"}->append_text($_);} | ||
| 103 | $canvas->{"combo$id"}->set_active (0); | ||
| 104 | $canvas->{"combo$id"}->signal_connect(changed => sub{ | ||
| 105 | $iVars{"combo$id"}=$canvas->{"combo$id"}->get_active_text; | ||
| 106 | &$action}); | ||
| 107 | $canvas->put($canvas->{"combo$id"},${$location}[0] ,${$location}[1]); | ||
| 108 | } | ||
| 109 | else {print "Combo options not defined for 'combo$id' with label $label\n"} | ||
| 110 | |||
| 111 | } | ||
| 112 | sub aMB{ | ||
| 113 | my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 114 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 115 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 116 | if ($type eq "menuhead"){ | ||
| 117 | $currentMenu="menu".$id; | ||
| 118 | $self ->{$currentMenu} = Gtk3::MenuItem->new_with_label($label); | ||
| 119 | $self ->{"sm$currentMenu"}=Gtk3::Menu->new(); | ||
| 120 | $self ->{$currentMenu}->set_submenu( $self ->{"sm$currentMenu"} ); | ||
| 121 | $self ->{"menubar"}->append($self ->{$currentMenu}); | ||
| 122 | } | ||
| 123 | elsif ($type eq "radio"){ | ||
| 124 | $self ->{"sm$currentMenu"}->append(Gtk3::RadioMenuItem->new($label)); | ||
| 125 | } | ||
| 126 | elsif ($type eq "check"){ | ||
| 127 | $self ->{"sm$currentMenu"}->append(Gtk3::CheckMenuItem->new($label)); | ||
| 128 | } | ||
| 129 | elsif ($type eq "separator"){ | ||
| 130 | $self ->{"sm$currentMenu"}->append(Gtk3::SeparatorMenuItem->new()); | ||
| 131 | } | ||
| 132 | else{ | ||
| 133 | if($currentMenu!~m/$label/){ | ||
| 134 | $self ->{"mi$id"}=Gtk3::MenuItem->new($label); | ||
| 135 | $self ->{"sm$currentMenu"}->append($self ->{"mi$id"}); | ||
| 136 | $self ->{"mi$id"}->signal_connect('activate' => $action); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | # logging menu generator print "$currentMenu---$id----$label---$type\n"; | ||
| 140 | return $currentMenu; | ||
| 141 | } | ||
| 142 | sub aSP{ | ||
| 143 | my ($self,$canvas, $id, $panelType, $content, $location, $size)=@_; | ||
| 144 | |||
| 145 | if ($panelType eq "I"){ # Image panels start with I | ||
| 146 | $canvas->{"Image".$id} = Gtk3::Image->new(); | ||
| 147 | $canvas->put($canvas->{"Image".$id},${$location}[0] ,${$location}[1]); | ||
| 148 | if (! -e $content){ return; } | ||
| 149 | my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file($content); | ||
| 150 | my $scaled = $pixbuf->scale_simple(${$size}[0],${$size}[1], 'GDK_INTERP_HYPER'); | ||
| 151 | $canvas->{"Image".$id}->set_from_pixbuf($scaled); | ||
| 152 | } | ||
| 153 | if ($panelType eq "T"){ | ||
| 154 | $canvas->{"sw$id"}= Gtk3::ScrolledWindow->new(); | ||
| 155 | $canvas->{"sw$id"}->set_hexpand(1); | ||
| 156 | $canvas->{"sw$id"}->set_vexpand(1); | ||
| 157 | $canvas->{"sw$id"}->set_size_request (${$size}[0],${$size}[1]); | ||
| 158 | $canvas->{"TextCtrl".($id+1)}=Gtk3::TextView->new; | ||
| 159 | $canvas->{"TextCtrl".($id+1)}->get_buffer()->set_text($content); | ||
| 160 | $canvas->{"TextCtrl".($id+1)}->set_editable (1) ; | ||
| 161 | $canvas->{"sw$id"}->add($canvas->{"TextCtrl".($id+1)}); | ||
| 162 | $canvas->put($canvas->{"sw$id"},${$location}[0] ,${$location}[1]); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 171 | sub addWidget{ | ||
| 172 | push (@widgets,shift ); | ||
| 173 | } | ||
| 174 | sub addStyle{ | ||
| 175 | my ($name,$style)=@_; | ||
| 176 | $styles{$name}=$style; | ||
| 177 | } | ||
| 178 | sub addVar{ | ||
| 179 | my ($varName,$value)=@_; | ||
| 180 | $oVars{$varName}=$value; | ||
| 181 | } | ||
| 182 | |||
| 183 | # Functions for internal use | ||
| 184 | sub getSize{ | ||
| 185 | my ($self,$id)=@_; | ||
| 186 | my $found=getItem($self,$id); | ||
| 187 | return ( $found!=-1) ? $widgets[$found][5]:0; | ||
| 188 | |||
| 189 | } | ||
| 190 | sub getLocation{ | ||
| 191 | my ($self,$id)=@_; | ||
| 192 | my $found=getItem($self,$id); | ||
| 193 | return ( $found!=-1) ? $widgets[$found][4]:0; | ||
| 194 | |||
| 195 | } | ||
| 196 | sub getItem{ | ||
| 197 | my ($self,$id)=@_; | ||
| 198 | $id=~s/[^\d]//g; | ||
| 199 | my $i=0; my $found=-1; | ||
| 200 | while ($i<@widgets){ | ||
| 201 | if ($widgets[$i][1]==$id) { | ||
| 202 | $found=$i; | ||
| 203 | } | ||
| 204 | $i++; | ||
| 205 | } | ||
| 206 | return $found; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub setScale{ | ||
| 210 | $winScale=shift; | ||
| 211 | }; | ||
| 212 | |||
| 213 | sub getFrame{ | ||
| 214 | my $self=shift; | ||
| 215 | return $self; | ||
| 216 | }; | ||
| 217 | |||
| 218 | # The functions for GUI Interactions | ||
| 219 | #Static Text functions | ||
| 220 | sub setLabel{ | ||
| 221 | my ($self,$id,$text)=@_; | ||
| 222 | $self->{panel}->{$id}->set_label($text); | ||
| 223 | } | ||
| 224 | |||
| 225 | #Image functions | ||
| 226 | sub setImage{ | ||
| 227 | my ($self,$id,$file)=@_; | ||
| 228 | my $canvas=$self->{panel}; | ||
| 229 | my $location=getLocation($self,$id); | ||
| 230 | my $size=getSize($self,$id); | ||
| 231 | if ($size){ | ||
| 232 | if (! -e $file){ print "$file not found\n"; return; } | ||
| 233 | my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file($file); | ||
| 234 | my $scaled = $pixbuf->scale_simple(${$size}[0],${$size}[1], 'GDK_INTERP_HYPER'); | ||
| 235 | $canvas->{"$id"} ->clear; | ||
| 236 | $canvas->{"$id"}->set_from_pixbuf($scaled); | ||
| 237 | } | ||
| 238 | else {print "Panel not found"} | ||
| 239 | |||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | #Text input functions | ||
| 244 | sub getValue{ | ||
| 245 | my ($self,$id)=@_; | ||
| 246 | if ($id =~/TextCtrl/){ | ||
| 247 | my $tb=$self->{panel}->{$id}->get_buffer(); | ||
| 248 | return $tb->get_text($tb->get_start_iter, $tb->get_end_iter, 1); | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | if (exists $iVars{$id}){ | ||
| 252 | return $iVars{$id} | ||
| 253 | } | ||
| 254 | else{ return $self->{panel}->{"$id"}->get_text; } | ||
| 255 | } | ||
| 256 | |||
| 257 | } | ||
| 258 | sub setValue{ | ||
| 259 | my ($self,$id,$text)=@_; | ||
| 260 | if ($id=~/^T/){ | ||
| 261 | $self->{panel}->{"$id"}->get_buffer()->set_text($text); | ||
| 262 | } | ||
| 263 | elsif ($id=~/^t/){ | ||
| 264 | $self->{panel}->{"$id"}->set_text($text); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | sub appendValue{ | ||
| 268 | my ($self,$id,$text)=@_; | ||
| 269 | if ($id=~/^T/){ | ||
| 270 | my $textbuffer=$self->{panel}->{$id}->get_buffer(); | ||
| 271 | my $textiter = $textbuffer->get_end_iter; | ||
| 272 | $textbuffer->insert($textiter,$text); | ||
| 273 | $self->{panel}->{$id}->set_buffer($textbuffer); | ||
| 274 | } | ||
| 275 | elsif ($id=~/^t/){ | ||
| 276 | my $newText=$self->{panel}->{"$id"}->get_text . $text; | ||
| 277 | $self->{panel}->{"$id"}->set_text($newText); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | #Message box, Fileselector and Dialog Boxes | ||
| 282 | sub showFileSelectorDialog{ | ||
| 283 | |||
| 284 | my ($self, $message,$load,$filter) = @_; | ||
| 285 | my $filename; | ||
| 286 | my $file_chooser = Gtk3::FileChooserDialog->new ( | ||
| 287 | $message, | ||
| 288 | undef, | ||
| 289 | $load?'open':'save', | ||
| 290 | 'gtk-cancel' => 'cancel', | ||
| 291 | $load?'gtk-open':'gtk-save' => 'ok' | ||
| 292 | ); | ||
| 293 | (defined $filter)&&($file_chooser->add_filter($filter)); | ||
| 294 | if ('ok' eq $file_chooser->run){ $filename = $file_chooser->get_filename; } | ||
| 295 | $file_chooser->destroy; | ||
| 296 | return $filename; | ||
| 297 | |||
| 298 | }; | ||
| 299 | sub showDialog{ | ||
| 300 | my ($self, $title, $message,$response,$icon) = @_; | ||
| 301 | my %responses=( YNC=>'yes-no', | ||
| 302 | YN =>'yes-no', | ||
| 303 | OK => 'ok', | ||
| 304 | OKC=>'ok-cancel' ); | ||
| 305 | |||
| 306 | my %icons= ( "!"=>"warning", | ||
| 307 | "?"=>"question", | ||
| 308 | "E"=>"error", | ||
| 309 | "H"=>"warning", | ||
| 310 | "I"=>"info" ); | ||
| 311 | $response=$response?$responses{$response}:"ok"; | ||
| 312 | $icon=$icon?$icons{$icon}:"info"; | ||
| 313 | my $dialog=Gtk3::MessageDialog->new (undef, | ||
| 314 | [qw( modal destroy-with-parent )], | ||
| 315 | $icon, # message type | ||
| 316 | $response, # which set of buttons? | ||
| 317 | $message); | ||
| 318 | my $answer= $dialog->run; | ||
| 319 | $dialog->destroy; | ||
| 320 | return (($answer eq "ok")||($answer eq "yes")) | ||
| 321 | }; | ||
| 322 | |||
| 323 | # Quit | ||
| 324 | sub quit{ | ||
| 325 | my ($self) = @_; | ||
| 326 | $self->{window}->destroy(); | ||
| 327 | } | ||
| 328 | 1; | ||
| 329 |