xcos_debug_gui function added for a simple use of the Debug block
[scilab.git] / scilab / modules / xcos / macros / xcos_debug_gui.sci
1 function flag=xcos_debug_gui(flag,block)
2     // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3     // Copyright (C) 2013 - INRIA - Serge Steer <serge.steer@inria.fr>
4     //
5     // This file must be used under the terms of the CeCILL.
6     // This source file is licensed as described in the file COPYING, which
7     // you should have received as part of this distribution.  The terms
8     // are also available at;
9     // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10
11     if scicos_debug()==0  then return;end
12     gui =  findobj("tag","Xcos_debug_gui");
13     init=gui==[]
14
15     if init then
16         gui=xcos_debug_create_gui()
17         before=%t
18         t=0
19         cb=curblock()
20         f=%cpr.sim.funs(cb)
21         path=%cpr.corinv(cb);
22     end
23     ud=gui.user_data;
24     if ~init then
25         //at least one flags must be selected
26         Flags=find(ud.Flags.value==1)
27         if Flags==[] then
28             Flags=2
29             ud.Flags(2).value=1
30         end
31         //at least before or after must be selected
32         When=find(ud.When.value==1)
33         if When==[] then
34             When=1
35             ud.When(1).value=1
36         end
37
38         if and(flag+1<>Flags) then return,end
39         t=scicos_time();
40         time_to_wait=evstr(ud.Time.string);
41         if t< time_to_wait then return,end
42
43         instr=stripblanks(ud.Condition.string);
44         if instr<>"" then
45             if execstr("Cond="+instr,"errcatch")==0 then
46                 if ~Cond then return,end
47             end
48         end
49
50         cb=curblock();// l'index du block dans la structure r├ęsultant de la compilation
51         f=%cpr.sim.funs(cb);//fonction de calcul
52
53         path=%cpr.corinv(cb);
54
55         before=ud.previous==[];
56         if before then
57             ud.previous=path
58             if and(When<>1) then set(gui,"user_data",ud);return,end
59         else
60             ud.previous=[]
61             if and(When<>2) then set(gui,"user_data",ud);return,end
62         end
63     end
64     //break asked
65     handles=ud.handles;
66     if before then //before block
67         handles(4).string=_("Stopped before block:")
68     else //after
69         handles(4).string=_("Stopped after block:")
70     end
71     Actions=[_("State derivative computation")
72     _("Regular outputs update")
73     _("States update on discrete event")
74     _("Output activation dates")
75     _("Initialization")
76     _("Ending")
77     _("ReInitialization")
78     _("Continous states properties update")
79     ""
80     _("Zero crossing surfaces computation")
81     _("Residual computation")];
82
83     handles(1).string=msprintf("%.3g",t);
84     handles(2).string=string(cb);
85     handles(3).string=string(flag)+" ("+Actions(flag+1)+")"
86     handles(5).string=sci2exp(path,0)
87     //Highlight current block
88     blk=scs_m.objs(path(1));
89     uids=blk.model.label;
90     for k=2:size(path,"*")
91         blk=blk.model.rpar.objs(path(k));
92         uids=[uids blk.model.label];
93     end
94     ud.Buttons.enable="on";
95     warnBlockByUID(uids, "Error");
96     while %t //To handle pause actions
97         realtimeinit(0.01);
98         tt=0;
99         global ret;
100         ret=0;
101         while ret==0 then
102             //Wait till user select a button
103             //slow down the waiting loop
104             tt=tt+1;
105             realtime(tt);
106         end
107         for k=size(uids,"*"):-1:1
108             warnBlockByUID(uids(1:k), "")
109         end
110
111         r=ret;clearglobal ret;clear ret
112         select r
113         case 1 then //Next
114             if xcos_debug_check_time(ud.Time)&xcos_debug_check_cond(ud.Condition) then
115                 if before then ud.previous=path;end
116                 ud.Buttons.enable="off"
117                 set(gui,"user_data",ud)
118                 return
119             end
120         case 2 then //End debug
121             scicos_debug(0)
122             delete(gui.parent)
123             return
124         case 3 then //pause
125             Y=list()
126             if block.nout>0 then
127                 outtb=%cpr.state.outtb
128                 sz=matrix(block.outsz,block.nout,-1)
129                 for i=1:block.nout
130                     Y($+1)=%cpr.state.outtb(block.outptr(i)+(1:prod(sz(i,1:2))))
131                 end
132             end
133             U=list()
134             if block.nin>0 then
135                 outtb=%cpr.state.outtb
136                 sz=matrix(block.insz,block.nin,-1)
137                 for i=1:block.nout
138                     U($+1)=%cpr.state.outtb(block.inptr(i)+(1:prod(sz(i,1:2))))
139                 end
140             end
141             ud.Buttons.enable="off"
142             pause
143             ud.Buttons.enable="on"
144             if before then ud.previous=path;end
145             set(gui,"user_data",ud)
146             //    end
147         end
148     end
149 endfunction
150
151 function gui=xcos_debug_create_gui()
152     margin_x     = 5;      // Horizontal margin between each elements
153     margin_y     = 5;      // Vertical margin between each elements
154     button_w     = 80;
155     button_h     = 20;
156     label_h      = 20;
157     label_h1      = 30;
158     label_w1      = 200;
159     label_w2      = 80;
160     frame_w     = max(4*margin_x+label_w1+label_w2,4*margin_x+3*button_w,12*margin_x+10*label_h)
161     frame_h     = 16*margin_y+10*label_h+3*label_h1+button_h
162
163     axes_w       = 2*margin_x+frame_w;
164     axes_h       = 2*margin_y+frame_h;// Frame height
165     defaultfont  = "arial"; // Default Font
166
167     fig_id=max(winsid())+1
168     fig = scf(fig_id)
169     toolbar(fig.figure_id, "off");
170     fig.axes_size       = [axes_w axes_h];
171     // Remove Scilab graphics menus & toolbar
172     delmenu(fig.figure_id, gettext("&File"));
173     delmenu(fig.figure_id, gettext("&Tools"));
174     delmenu(fig.figure_id, gettext("&Edit"));
175     delmenu(fig.figure_id, gettext("&?"));
176     fig.figure_size(2)=axes_h
177     fig.axes_size       = [axes_w axes_h];
178     fig.background      = addcolor([0.8 0.8 0.8]);
179     fig.figure_name     = _("Xcos debug");
180     fig.closerequestfcn = "xcos_debug_gui_close()"
181     ax=fig.children;
182     ax.background= fig.background ;
183
184     gui=uicontrol( ...
185     "parent"              , fig,...
186     "style"               , "frame",...
187     "units"               , "pixels",...
188     "position"            , [margin_x margin_y frame_w frame_h],...
189     "background"          , [1 1 1]*0.8, ...
190     "tag"                 , "Xcos_debug_gui",...
191     "visible"             , "on");
192
193     yo=axes_h-button_h;
194
195
196     y=frame_h-margin_y-label_h1
197     x=margin_x;
198     uicontrol( ...
199     "parent"              , gui,...
200     "style"               , "text",...
201     "string"              , _("Break point conditions"),...
202     "units"               , "pixels",...
203     "position"            , [x y frame_w-2*margin_x label_h1],....
204     "fontname"            , defaultfont,...
205     "fontunits"           , "points",...
206     "fontsize"            , 16,...
207     "fontweight"          , "bold", ...
208     "horizontalalignment" , "center", ...
209     "background"          , [1 1 1]*0.8, ...
210     "visible"             , "on");
211     y=y-label_h-margin_y;
212     uicontrol( ...
213     "parent"              , gui,...
214     "style"               , "text",...
215     "string"              , _("Break on selected flags"),...
216     "units"               , "pixels",...
217     "position"            , [x y frame_w-2*margin_x label_h],....
218     "fontname"            , defaultfont,...
219     "fontunits"           , "points",...
220     "fontsize"            , 12,...
221     "fontweight"          , "bold", ...
222     "horizontalalignment" , "left", ...
223     "background"          , [1 1 1]*0.8, ...
224     "visible"             , "on");
225     yl=y-label_h-margin_y;
226     y=yl-label_h-margin_y;
227     xk=x
228     Flags=[]
229     for k=0:9
230         uicontrol( ...
231         "parent"              , gui,...
232         "style"               , "text",...
233         "string"              , string(k),...
234         "units"               , "pixels",...
235         "position"            , [xk yl label_h label_h],....
236         "fontname"            , defaultfont,...
237         "fontunits"           , "points",...
238         "fontsize"            , 12,...
239         "fontweight"          , "bold", ...
240         "horizontalalignment" , "left", ...
241         "background"          , [1 1 1]*0.8, ...
242         "visible"             , "on");
243         Flags=[Flags,uicontrol( ...
244         "parent"              , gui,...
245         "style"               , "checkbox",...
246         "value"               , 0,...
247         "units"               , "pixels",...
248         "position"            , [xk y label_h label_h],....
249         "fontname"            , defaultfont,...
250         "fontunits"           , "points",...
251         "fontsize"            , 12,...
252         "fontweight"          , "bold", ...
253         "horizontalalignment" , "left", ...
254         "background"          , [1 1 1]*0.8, ...
255         "visible"             , "on")];
256         xk=xk+label_h+margin_x
257     end
258     Flags(2).value=1;
259     yl=y-label_h-margin_y;
260     y=yl-label_h-margin_y;
261     xk=x;
262     When=[]
263     L=[_("Before call"),_("After call")]
264     for k=1:2
265         uicontrol( ...
266         "parent"              , gui,...
267         "style"               , "text",...
268         "string"              , L(k),...
269         "units"               , "pixels",...
270         "position"            , [xk yl label_w2 label_h],....
271         "fontname"            , defaultfont,...
272         "fontunits"           , "points",...
273         "fontsize"            , 12,...
274         "fontweight"          , "bold", ...
275         "horizontalalignment" , "left", ...
276         "background"          , [1 1 1]*0.8, ...
277         "visible"             , "on");
278         When=[When,uicontrol( ...
279         "parent"              , gui,...
280         "style"               , "checkbox",...
281         "value"               , 0,...
282         "units"               , "pixels",...
283         "position"            , [xk y label_h label_h],....
284         "fontname"            , defaultfont,...
285         "fontunits"           , "points",...
286         "fontsize"            , 12,...
287         "fontweight"          , "bold", ...
288         "horizontalalignment" , "left", ...
289         "background"          , [1 1 1]*0.8, ...
290         "visible"             , "on")];
291         xk=xk+label_w2+margin_x
292     end
293     When(1).value=1;
294     uicontrol( ...
295     "parent"              , gui,...
296     "style"               , "text",...
297     "string"              , _("After date"),...
298     "units"               , "pixels",...
299     "position"            , [xk yl label_w2 label_h],....
300     "fontname"            , defaultfont,...
301     "fontunits"           , "points",...
302     "fontsize"            , 12,...
303     "fontweight"          , "bold", ...
304     "horizontalalignment" , "left", ...
305     "background"          , [1 1 1]*0.8, ...
306     "visible"             , "on");
307     Time=uicontrol( ...
308     "parent"              , gui,...
309     "style"               , "edit",...
310     "string"               , "0",...
311     "units"               , "pixels",...
312     "position"            , [xk y label_w2 label_h],....
313     "fontname"            , defaultfont,...
314     "fontunits"           , "points",...
315     "fontsize"            , 12,...
316     "fontweight"          , "bold", ...
317     "horizontalalignment" , "left", ...
318     "background"          , [1 1 1]*0.8, ...
319     "callback"            , "xcos_debug_check_time();" ,...
320     "visible"             , "on");
321     y=y-label_h-2*margin_y;
322     uicontrol( ...
323     "parent"              , gui,...
324     "style"               , "text",...
325     "string"              , _("On the Scilab condition above"),...
326     "units"               , "pixels",...
327     "position"            , [x y frame_w-2*margin_x label_h],....
328     "fontname"            , defaultfont,...
329     "fontunits"           , "points",...
330     "fontsize"            , 12,...
331     "fontweight"          , "bold", ...
332     "horizontalalignment" , "left", ...
333     "background"          , [1 1 1]*0.8, ...
334     "visible"             , "on");
335     y=y-label_h-2*margin_y;
336     Condition=uicontrol( ...
337     "parent"              , gui,...
338     "style"               , "edit",...
339     "string"              , "",...
340     "units"               , "pixels",...
341     "position"            , [x y frame_w-6*margin_x label_h],....
342     "fontname"            , defaultfont,...
343     "fontunits"           , "points",...
344     "fontsize"            , 12,...
345     "fontweight"          , "bold", ...
346     "horizontalalignment" , "left", ...
347     "background"          , [1 1 1]*0.8, ...
348     "callback"            , "xcos_debug_check_cond();" ,...
349     "visible"             , "on");
350     y=y-label_h1-margin_y;
351     uicontrol( ...
352     "parent"              , gui,...
353     "style"               , "text",...
354     "string"              , _("Current simulation state"),...
355     "units"               , "pixels",...
356     "position"            , [x y frame_w-2*margin_x label_h1],....
357     "fontname"            , defaultfont,...
358     "fontunits"           , "points",...
359     "fontsize"            , 16,...
360     "fontweight"          , "bold", ...
361     "horizontalalignment" , "center", ...
362     "background"          , [1 1 1]*0.8, ...
363     "visible"             , "on");
364     y=y-label_h-margin_y;
365     X=uicontrol( ...
366     "parent"              , gui,...
367     "style"               , "text",...
368     "string"              , "",...
369     "units"               , "pixels",...
370     "position"            , [x y label_w1 label_h],....
371     "fontname"            , defaultfont,...
372     "fontunits"           , "points",...
373     "fontsize"            , 12,...
374     "horizontalalignment" , "left", ...
375     "fontweight"          , "bold", ..
376     "background"          , [1 1 1]*0.8, ...
377     "visible"             , "on");
378
379     B=uicontrol( ...
380     "parent"              , gui,...
381     "style"               , "text",...
382     "string"              , "0",...
383     "units"               , "pixels",...
384     "position"            , [x+label_w1+margin_x y label_w2 label_h],....
385     "fontname"            , defaultfont,...
386     "fontunits"           , "points",...
387     "fontsize"            , 12,...
388     "horizontalalignment" , "right", ...
389     "background"          , [1 1 1]*0.8, ...
390     "visible"             , "on");
391     y=y-label_h-margin_y;
392     //%%%%%%
393     uicontrol( ...
394     "parent"              , gui,...
395     "style"               , "text",...
396     "string"              , _("scs_m block path:"),...
397     "units"               , "pixels",...
398     "position"            , [x y label_w1 label_h],....
399     "fontname"            , defaultfont,...
400     "fontunits"           , "points",...
401     "fontsize"            , 12,...
402     "fontweight"          , "bold", ...
403     "horizontalalignment" , "left", ...
404     "background"          , [1 1 1]*0.8, ...
405     "visible"             , "on");
406
407     P=uicontrol( ...
408     "parent"              , gui,...
409     "style"               , "text",...
410     "string"              , "0",...
411     "units"               , "pixels",...
412     "position"            , [x+label_w1+margin_x y label_w2 label_h],....
413     "fontname"            , defaultfont,...
414     "fontunits"           , "points",...
415     "fontsize"            , 12,...
416     "horizontalalignment" , "right", ...
417     "background"          , [1 1 1]*0.8, ...
418     "visible"             , "on");
419     y=y-label_h-margin_y;
420     uicontrol( ...
421     "parent"              , gui,...
422     "style"               , "text",...
423     "string"              , _("Time:"),...
424     "units"               , "pixels",...
425     "position"            , [x y label_w1 label_h],....
426     "fontname"            , defaultfont,...
427     "fontunits"           , "points",...
428     "fontsize"            , 12,...
429     "fontweight"          , "bold", ...
430     "horizontalalignment" , "left", ...
431     "background"          , [1 1 1]*0.8, ...
432     "visible"             , "on");
433
434     T=uicontrol( ...
435     "parent"              , gui,...
436     "style"               , "text",...
437     "string"              , "0",...
438     "units"               , "pixels",...
439     "position"            , [x+label_w1+margin_x y label_w2 label_h],....
440     "fontname"            , defaultfont,...
441     "fontunits"           , "points",...
442     "fontsize"            , 12,...
443     "horizontalalignment" , "right", ...
444     "background"          , [1 1 1]*0.8, ...
445     "visible"             , "on");
446
447     y=y-label_h-margin_y;
448     uicontrol( ...
449     "parent"              , gui,...
450     "style"               , "text",...
451     "string"              , _("Flag:"),...
452     "units"               , "pixels",...
453     "position"            , [x y 4*label_h label_h],....
454     "fontname"            , defaultfont,...
455     "fontunits"           , "points",...
456     "fontsize"            , 12,...
457     "fontweight"          , "bold", ...
458     "horizontalalignment" , "left", ...
459     "background"          , [1 1 1]*0.8, ...
460     "visible"             , "on");
461
462     F=uicontrol( ...
463     "parent"              , gui,...
464     "style"               , "text",...
465     "string"              , "6",...
466     "units"               , "pixels",...
467     "position"            , [x+4*label_h+margin_x y label_w2+label_w1+-4*label_h label_h],....
468     "fontname"            , defaultfont,...
469     "fontunits"           , "points",...
470     "fontsize"            , 12,...
471     "horizontalalignment" , "right", ...
472     "background"          , [1 1 1]*0.8, ...
473     "visible"             , "on");
474
475     //buttons
476     y=y-button_h-margin_y;
477     xb=x;
478     Next = uicontrol( ...
479     "parent"              , gui,...
480     "relief"              , "groove",...
481     "style"               , "pushbutton",...
482     "string"              , _("Next"),...
483     "units"               , "pixels",...
484     "position"            , [xb,y,button_w button_h],..
485     "fontname"            , "arial",...
486     "fontunits"           , "points",...
487     "fontsize"            , 12,...
488     "fontweight"          , "bold", ...
489     "horizontalalignment" , "center", ...
490     "visible"             , "on", ...
491     "callback"            , "global ret;ret=1;");
492     xb=xb+margin_x+button_w
493     Stop = uicontrol( ...
494     "parent"              , gui,...
495     "relief"              , "groove",...
496     "style"               , "pushbutton",...
497     "string"              , _("End debug"),...
498     "units"               , "pixels",...
499     "position"            , [xb,y,button_w button_h],..
500     "fontname"            , "arial",...
501     "fontunits"           , "points",...
502     "fontsize"            , 12,...
503     "fontweight"          , "bold", ...
504     "horizontalalignment" , "center", ...
505     "visible"             , "on", ...
506     "callback"            , "global ret;ret=2;");
507     xb=xb+margin_x+button_w
508     Pause = uicontrol( ...
509     "parent"              , gui,...
510     "relief"              , "groove",...
511     "style"               , "pushbutton",...
512     "string"              , _("Pause"),...
513     "units"               , "pixels",...
514     "position"            , [xb,y,button_w button_h],..
515     "fontname"            , "arial",...
516     "fontunits"           , "points",...
517     "fontsize"            , 12,...
518     "fontweight"          , "bold", ...
519     "horizontalalignment" , "center", ...
520     "visible"             , "on", ...
521     "callback"            , "global ret;ret=3;");
522
523     set(gui,"user_data",struct("handles",[T,B,F,X,P],"previous",[],...
524     "Flags",Flags,"When",When,"Time",Time,...
525     "Condition",Condition,...
526     "Buttons",[Next,Stop,Pause]))
527 endfunction
528 function ok=xcos_debug_check_time(Time)
529     if argn(2)<1 then Time=gcbo;end
530     ud=Time.parent.user_data
531     ok=execstr("t="+Time.string,"errcatch")==0
532     if ~ok|type(t)<>1|size(t,"*")<>1 then
533         Time.ForegroundColor=[1 0 0];
534         ud.Buttons.enable="off"
535         ok=%f
536     else
537         Time.ForegroundColor=[0 0 0];
538         ud.Buttons.enable="on"
539         ok=%t
540     end
541 endfunction
542 function ok=xcos_debug_check_cond(Condition)
543     if argn(2)<1 then Condition=gcbo;end
544     ud=Condition.parent.user_data
545     Cond=stripblanks(Condition.string)
546     if Cond=="" then
547         ok=%t
548         c=%f
549     else
550         ok=execstr("c="+Cond,"errcatch")==0
551     end
552     if ~ok|and(type(c)<>[1 4])|size(c,"*")<>1 then
553         Condition.ForegroundColor=[1 0 0];
554         ud.Buttons.enable="off"
555         ok=%f
556     else
557         Condition.ForegroundColor=[0 0 0];
558         ud.Buttons.enable="on"
559         ok=%t
560     end
561
562 endfunction
563 function xcos_debug_gui_close()
564     messagebox(msprintf(_("Use the ""%s"" button to end the debugging"),_("End debug")),"modal")
565 endfunction