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