GFhtml.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 5 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 274 CC 38 SCAR 49.1
CC
38
Cov
0%
setupContent
420.0
aMB
72.0
MainLoop
12.0
CRAP 135.1
1 package GFhtml;
2 use strict;
3 use warnings;
4
5 our $VERSION = '0.09';
6  
7 use Exporter 'import';
8 our @EXPORT = qw<addWidget addVar setScale MainLoop $frame $winScale $winWidth $winHeight $winTitle>;
9 our $frame;
10
11 our $winX=30;
12 our $winY=30;
13 our $winWidth;
14 our $winHeight;
15 our $winTitle="title";
16 our $winScale=6.5;
17
18 # these arrays will contain the widgets each as an arrayref of the parameters
19 my @widgets=();
20 my %iVars=(); #vars for interface operation (e.g.
21 my %oVars=(); #vars for interface creation (e.g. list of options)
22 my %styles;
23
24 my $lastMenuLabel; #bug workaround in menu generator may be needed for submenus
25
26 sub new
27 {
28 my $class = shift;
29 my $self={};
30 bless( $self, $class );
31 $self->{header}=header();
32 $self->{header}.="<title>$winTitle $0 </title>\n";
33 $self->{content}="<div id=window style=\"position:relative;width:$winWidth"."px;height:$winHeight"."px\">\n";
34 setupContent($self,$self->{content});
35
36 $self->{html}=$self->{header};
37 $self->{html}.="<head>\n<style>\n".css()."</style>\n<script>\n".js()."</script>\n".
38 "<script type=\"text/javascript\" src=\"$0.js\"></script>\n".
39 "</head>\n<body>\n";
40 $self->{html}.=$self->{content};
41 if ($self->{menubar}){ $self->{html}.= $self->{menubar} . "\n<br>\n";}
42 $self->{html}.= "</div>\n</body>\n</html>";
43
44 my $filename = $0.".html";
45 open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
46 print $fh $self->{html};
47 close $fh;
48 return $self;
49 };
50
51 sub MainLoop{ #activate UI
52 my $self=shift;
53 my $htmlFile=$0.".html";
54 if ($^O =~/linux/){ system("xdg-open ./".$htmlFile."\n"); }
55 elsif ($^O =~/Win/){ system("start .\\".$htmlFile."\n"); }
56 else{ system("open ./".$htmlFile."\n"); }
57 }
58  
59 # setupContent sets up the initial content before Mainloop can be run.
60 sub setupContent{
61 my ($self, $canvas)=@_; # pass both object as well as the frame element
62 $self ->{"menubar"}=undef; # menu not yet defined
63 my $currentMenu; # undefined menu
64 foreach my $widget (@widgets){ # read each widget data and call gnerator
65 my @params=@$widget;
66 my $wtype=shift @params;
67 if ($wtype eq "btn") {aBt($self, $canvas, @params);}
68 elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);}
69 elsif ($wtype eq "stattext") {aST($self, $canvas, @params);}
70 elsif ($wtype eq "sp") {aSP($self, $canvas, @params);}
71 elsif ($wtype eq "combo") {aCB($self, $canvas, @params);}
72 elsif ($wtype eq "sp") {aSP($self, $canvas, @params);}
73 elsif ($wtype eq "mb")
74 {
75 if (! $self->{"menubar"}){
76 $self ->{"menubar"} = "<div style=\"width:$winWidth"."px;height:16px;\">\n<ul class=menubar>";
77 }
78 $currentMenu=aMB($self,$canvas,$currentMenu,@params)
79 }
80 }
81 if ($self->{"menubar"}) { $self->{"menubar"}.="</div>\n</li>\n</ul></div>"; }
82
83 sub aBt{
84 my ($self,$canvas, $id, $label, $location, $size, $action)=@_;# Button generator
85 $self->{content}.="<input id=btn$id type=button style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;".
86 "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"$label\" onclick=\'act(\"btn$id\",\"$label\")\'>\n";
87 }
88 sub aTC{
89 my ($self,$canvas, $id, $text, $location, $size, $action)=@_;# Single line input generator
90 $self->{content}.="<input id=textctrl$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;".
91 "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"$text\" onchange=\'act(\"textctrl$id\",this.value)\'>\n";
92 }
93 sub aST{
94 my ($self,$canvas, $id, $text, $location)=@_; #Static text element generator
95 $self->{content}.="<div id=stattext$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px\">".$text."</div>\n";
96 }
97 sub aCB{
98 my ($self,$canvas, $id, $label, $location, $size, $action)=@_; #gnerator for comoboxes
99 if (defined $oVars{$label}){
100 my @strings2 = split(",",$oVars{$label}); # extract the defined options
101 $self->{content}.="<select id=combo$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;".
102 "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"".$strings2[0]."\" onchange=\'act(\"textctrl$id\",this.value)\'>\n";
103 foreach (@strings2){
104 $self->{content}.=" <option>$_</option>\n";
105 }
106 $self->{content}.="</select>\n";
107 }
108 else {print "Combo options not defined for 'combo$id' with label $label\n"};
109 }
110
111 sub aMB{
112 my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_;
113 if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround
114 else {$lastMenuLabel=$label}; # in menu generator
115 if ($type eq "menuhead"){
116 if (defined $currentMenu){$canvas.="</div></li>";}
117 $currentMenu="menu".$id;
118 $self ->{"menubar"}.="<li class=\"menuhead dropdown\">\n<a id=menu$id class=\"dropbtn\">$label</a>\n<div class=\"dropdown-content\">\n";
119 }
120 elsif ($type eq "radio"){
121
122 }
123 elsif ($type eq "check"){
124
125 }
126 elsif ($type eq "separator"){
127
128 }
129 else{
130 if($currentMenu!~m/$label/){
131 $self ->{"menubar"}.="<a href=# id=menu$id class=menuitem onclick=\'act(\"menu$id\",\"$label\")\'>$label</a>\n"
132 }
133 }
134 return $currentMenu;
135 }
136 sub aSP{
137 my ($self,$canvas, $id, $panelType, $content, $location, $size)=@_;
138
139 if ($panelType eq "I"){ # Image panels start with I
140 $self->{content}.="<img id=Image$id src=\"$content\" style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;".
141 "width:".${$size}[0]."px;height:".${$size}[1]."px;\">\n";
142 }
143 if ($panelType eq "T"){
144 $self->{content}.="<textarea id=TextCtrl$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;".
145 "width:".${$size}[0]."px;height:".${$size}[1]."px;\">$content</textArea>\n";
146 }
147 }
148 }
149  
150
151 #functions for GUIDeFATE to load the widgets into the backend
152 sub addWidget{
153 push (@widgets,shift );
154 }
155 sub addStyle{
156 my ($name,$style)=@_;
157 $styles{$name}=$style;
158 }
159 sub addVar{
160 my ($varName,$value)=@_;
161 $oVars{$varName}=$value;
162 }
163  
164 # Functions for internal use
165 sub getSize{
166 my ($self,$id)=@_;
167 my $found=getItem($self,$id);
168 return ( $found!=-1) ? $widgets[$found][5]:0;
169
170 }
171 sub getLocation{
172 my ($self,$id)=@_;
173 my $found=getItem($self,$id);
174 return ( $found!=-1) ? $widgets[$found][4]:0;
175
176 }
177 sub getItem{
178 my ($self,$id)=@_;
179 $id=~s/[^\d]//g;
180 my $i=0; my $found=-1;
181 while ($i<@widgets){
182 if ($widgets[$i][1]==$id) {
183 $found=$i;
184 }
185 $i++;
186 }
187 return $found;
188 }
189  
190 sub setScale{
191 $winScale=shift;
192 };
193  
194 sub getFrame{
195
196 };
197  
198 # The functions for GUI Interactions
199 #Static Text functions
200 sub setLabel{
201 my ($self,$id,$text)=@_;
202
203 }
204  
205 #Image functions
206 sub setImage{
207 my ($self,$id,$file)=@_;
208  
209 }
210  
211 #Text input functions
212 sub getValue{
213 my ($self,$id)=@_;
214 if ($id =~/TextCtrl/){
215 }
216 else {
217 if (exists $iVars{$id}){
218 return $iVars{$id}
219 }
220 }
221
222 }
223 sub setValue{
224 my ($self,$id,$text)=@_;
225
226 }
227 sub appendValue{
228 my ($self,$id,$text)=@_;
229
230 }
231  
232 #Message box, Fileselector and Dialog Boxes
233 sub showFileSelectorDialog{
234
235 my ($self, $message,$load,$filter) = @_;
236 my $filename;
237  
238 return $filename;
239 };
240 sub showDialog{
241 my ($self, $title, $message,$response,$icon) = @_;
242  
243 };
244
245 # Quit
246 sub quit{
247  
248 }
249
250
251 sub css{
252 return <<ENDCSS
253 ul.menubar {
254 list-style-type: none;
255 margin: 0;
256 padding: 0;
257 overflow: hidden;
258 background-color: #333;
259 }
260  
261 .menuhead {
262 float: left;
263 }
264  
265 .menuhead a, .dropbtn {
266 display: inline-block;
267 color: white;
268 text-align: center;
269 padding: 1px 3px;
270 text-decoration: none;
271 }
272  
273 li.menuhead a:hover, .dropdown:hover .dropbtn {
274 background-color: red;
275 }
276  
277 .menuhead .dropdown {
278 display: inline-block;
279 }
280  
281 .dropdown-content {
282 display: none;
283 position: absolute;
284 background-color: #f9f9f9;
285 min-width: 160px;
286 box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
287 z-index: 1;
288 }
289  
290 .dropdown-content a {
291 color: black;
292 padding: 1px 3x;
293 text-decoration: none;
294 display: block;
295 text-align: left;
296 }
297  
298 .dropdown-content a:hover {background-color: #f1f1f1}
299  
300 .dropdown:hover .dropdown-content {
301 display: block;
302 }
303  
304 ENDCSS
305  
306 }
307
308 sub header{
309 return <<END
310 <html>
311 END
312  
313 }
314  
315 sub js{
316 return <<END
317 function act(command,label){
318 if (typeof window[command] === "function") {
319 window[strOfFunction](label);
320 }
321 else{
322 alert("Function "+ command + "\\nParameter "+label +"\\nNot yet defined")
323 }
324 }
325
326 END
327  
328 }
329  
330 1;
331