%_sodload can recover the struct and cell data
[scilab.git] / scilab / modules / io / macros / %_sodload.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
3 // Copyright (C) 2012 - DIGITEO - Vincent COUVERT
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 function %_sodload(%__filename__, varargin)
12
13     function v = getScilabFileVersion(%__filename__)
14         verStr = h5readattr(%__filename__, "/", "SCILAB_scilab_version")
15         [a,b,c,d] = regexp(verStr, "/scilab-.*(\d)\.(\d)\.(\d)/");
16         if size(d, "*") == 3 then
17             v = evstr(d(1)) * 100 + evstr(d(2)) * 10 + evstr(d(3));
18         else
19             error("unable to find file version: %s", %__filename__);
20         end
21     endfunction
22
23     function [varValues] = %__convertVariable__(varValues, varNames)
24         for i = 1:size(varValues)
25             if typeof(varValues(i)) == "ScilabMatrixHandle" then
26                 //convert tlist to handle
27                 varValues(i) = createMatrixHandle(varValues(i));
28             elseif typeof(varValues(i)) == "ScilabMacro" then
29                 //convert tlist to macro
30                 varValues(i) = createMacro(varValues(i), varNames(i));
31             elseif isList(varValues(i)) then
32                 //list container
33                 varValues(i) = parseList(varValues(i));
34             end
35         end
36     endfunction
37
38     function result = isList(var)
39         //15 : list
40         //16 : tlist
41         //17 : mlist
42         if or(type(var) == [15, 16, 17]) then
43             result  = %t;
44         else
45             result = %f;
46         end
47     endfunction
48
49     function varValue = parseList(varValue)
50
51         if or(typeof(varValue)==["cell","st"]) then
52             if typeof(varValue)=="st" then
53                 fieldNames = fieldnames(varValue);
54             else
55                 fieldNames = varValue;
56             end
57
58             for kField = 1:size(fieldNames,"*")
59
60                 if typeof(varValue)=="st" then
61                     fieldValue = varValue(fieldNames(kField));
62                 else
63                     fieldValue = varValue{kField};
64                 end
65
66                 if typeof(fieldValue) == "ScilabMatrixHandle" then
67                     fieldValue = createMatrixHandle(fieldValue);
68                 elseif typeof(fieldValue) == "ScilabMacro" then
69                     //convert tlist to macro
70                     fieldValue = createMacro(fieldValue, "function");
71                 elseif isList(fieldValue) then
72                     fieldValue = parseList(fieldValue);
73                 end
74
75                 if typeof(varValue)=="st" then
76                     varValue(fieldNames(kField)) = fieldValue;
77                 else
78                     varValue{kField} = fieldValue;
79                 end
80
81             end
82
83         elseif typeof(varValue)=="list" then
84             for i = definedfields(varValue)
85                 if typeof(varValue(i)) == "ScilabMatrixHandle" then
86                     varValue(i) = createMatrixHandle(varValue(i));
87                 elseif typeof(varValue(i)) == "ScilabMacro" then
88                     //convert tlist to macro
89                     varValue(i) = createMacro(varValue(i), "function");
90                 elseif isList(varValue(i)) then
91                     varValue(i) = parseList(varValue(i));
92                 else
93                     varValue(i) = varValue(i);
94                 end
95             end
96         else
97             fieldNb = size(getfield(1, varValue), "*");
98             for kField = 2:fieldNb // Do not inspect first field (field names)
99                 fieldValue = getfield(kField, varValue);
100                 if typeof(fieldValue) == "ScilabMatrixHandle" then
101                     fieldValue = createMatrixHandle(fieldValue);
102                 elseif typeof(fieldValue) == "ScilabMacro" then
103                     //convert tlist to macro
104                     fieldValue = createMacro(fieldValue, "function");
105                 elseif isList(fieldValue) then
106                     fieldValue = parseList(fieldValue);
107                 end
108                 setfield(kField, fieldValue, varValue);
109             end
110         end
111     endfunction
112
113     function h = createMatrixHandle(matrixHandle)
114         h = [];
115         if typeof(matrixHandle) <> "ScilabMatrixHandle" then
116             return;
117         end
118
119         for i = prod(matrixHandle.dims):-1:1
120
121             newItem = createSingleHandle(matrixHandle.values(i));
122             if newItem == [] then
123                 continue;
124             end
125
126             h($+1) = newItem;
127             if or(fieldnames(matrixHandle.values(i))=="user_data") then // TODO Remove after graphic branch merge
128                 if isList(matrixHandle.values(i).user_data) then
129                     set(h($), "user_data", parseList(matrixHandle.values(i).user_data));
130                 elseif typeof(matrixHandle.values(i).user_data) == "ScilabMatrixHandle" then
131                     set(h($), "user_data", createMatrixHandle(matrixHandle.values(i).user_data));
132                 end
133             end
134         end
135     endfunction
136
137     function h = createSingleHandle(item)
138         select item.type
139         case "Figure"
140             h = createFigure(item);
141         case "Axes"
142             h = createAxes(item);
143         case "Polyline"
144             h = createPolyline(item);
145         case "Plot3d"
146             h = createPlot3d(item);
147         case "Fac3d"
148             h = createFac3d(item);
149         case "Compound"
150             h = createCompound(item);
151         case "Rectangle"
152             h = createRectangle(item);
153         case "Arc"
154             h = createArc(item);
155         case "Champ"
156             h = createChamp(item);
157         case "Segs"
158             h = createSegs(item);
159         case "Grayplot"
160             h = createGrayplot(item);
161         case "Matplot"
162             h = createMatplot(item);
163         case "Fec"
164             h = createFec(item);
165         case "Legend"
166             h = createLegend(item);
167         case "Text"
168             h = createText(item);
169         case "Axis"
170             h = createAxis(item);
171         case "uimenu"
172             h = createuimenu(item);
173         case "uicontextmenu"
174             h = createuicontextmenu(item);
175         case "uicontrol"
176             h = createuicontrol(item);
177         case "Datatip"
178             h = createDatatip(item);
179         case "Light"
180             h = createLight(item);
181         else
182             error("handle of type " + item.type + " unhandled");
183             h = [];
184         end
185     endfunction
186
187     //
188     // FIGURE
189     //
190     function h = createFigure(figureProperties)
191         fields = fieldnames(figureProperties);
192         fields(1) = [];
193
194         if or(fields=="resize") then
195             if figureProperties.menubar<>"figure" ..
196                 | figureProperties.toolbar<>"figure" ..
197                 | figureProperties.dockable<>"on" ..
198                 | figureProperties.default_axes<>"on" then
199                 // File created by Scilab 5.5.0 or more
200                 h = figure("menubar", figureProperties.menubar, ...
201                 "toolbar", figureProperties.toolbar, ...
202                 "dockable", figureProperties.dockable, ...
203                 "default_axes", figureProperties.default_axes, ...
204                 "visible", "off");
205                 h.background = -2;
206                 fields(fields=="menubar") = [];
207                 fields(fields=="toolbar") = [];
208                 fields(fields=="dockable") = [];
209                 fields(fields=="default_axes") = [];
210                 fields(fields=="visible") = [];
211             else
212                 [lnums, fnames] = where();
213                 ind = grep(fnames, "xload");
214                 xload_mode = (ind ~= []);
215                 if xload_mode then // See bug #3975
216                     h = gcf();
217                 else
218                     h = scf();
219                 end
220                 h.visible = "off";
221             end
222         else
223             if isempty(winsid()) then
224                 h = figure("visible", "off");
225                 h.background = -2;
226             else
227                 h = gcf();
228                 h.visible = "off";
229             end
230         end
231
232         // Following propeties will be set after all other ones
233         isVisible = figureProperties.visible;
234         fields(fields=="visible") = [];
235         resizefcn = figureProperties.resizefcn;
236         fields(fields=="resizefcn") = [];
237         event_handler = figureProperties.event_handler;
238         fields(fields=="event_handler") = [];
239
240         // Ignore figure_id
241         fields(fields=="figure_id") = [];
242
243         h.figure_position=figureProperties.figure_position;
244         fields(fields=="figure_position") = [];
245         // set auto_resize first otherwise viewport modification may not have any effect.
246         h.auto_resize = figureProperties.auto_resize;
247         fields(fields=="auto_resize") = [];
248         h.figure_size = figureProperties.figure_size;
249         fields(fields=="figure_size") = [];
250         // set axes_size last because it's more important than figure_size
251         h.axes_size = figureProperties.axes_size;
252         fields(fields=="axes_size") = [];
253
254         for i = 1:size(fields, "*")
255             if fields(i) == "children" then
256                 c = figureProperties(fields(i));
257                 s = prod(c.dims);
258                 createSingleHandle(c.values(s));
259                 for  i = s-1:-1:1
260                     xsetech(wrect=[0 0 .1 .1])
261                     createSingleHandle(c.values(i));
262                 end
263             else
264                 if fields(i)<>"pixmap" then // See bug #13310
265                     set(h, fields(i), figureProperties(fields(i)));
266                 end
267             end
268         end
269
270         h.resizefcn = resizefcn;
271         h.event_handler = event_handler;
272         h.visible = isVisible;
273     endfunction
274
275     //
276     // LABEL
277     //
278     function h = createLabel(labelProperties, h)
279         fields = fieldnames(labelProperties);
280         fields(1) = [];
281         for i = 1:size(fields, "*")
282             set(h, fields(i), labelProperties(fields(i)));
283         end
284     endfunction
285
286     //
287     // TICKS
288     //
289     function h = createTicks(ticksProperties)
290         h = tlist(["ticks","locations","labels"], [], []);
291         fields = fieldnames(ticksProperties);
292         for i = 1:size(fields, "*")
293             h(fields(i)) = ticksProperties(fields(i));
294         end
295     endfunction
296
297     //
298     // AXES
299     //
300     function h = createAxes(axesProperties)
301         // Hack to determine whether %h_load has been called by the %h_copy macro
302         // in which case a new Axes object is created
303
304         [lnums, fnames] = where();
305         ind = grep(fnames, "%h_copy");
306         if ~isempty(ind) then
307             newaxes();
308         end;
309
310         h = gca();
311         fields = fieldnames(axesProperties);
312         fields(1) = [];
313
314         // Get log_flags to be sure to set them after data_bounds
315         log_flags = axesProperties.log_flags;
316         fields(fields=="log_flags") = [];
317
318         // Get mark_mode to be sure to set it after mark_style
319         mark_mode = axesProperties.mark_mode;
320         fields(fields=="mark_mode") = [];
321
322         // Get auto_ticks to be sure to set it after ticks labels
323         auto_ticks = axesProperties.auto_ticks;
324         fields(fields=="auto_ticks") = [];
325         automargins = %f;
326         if isfield(axesProperties, "auto_margins") then
327             auto_margins = axesProperties.auto_margins;
328             fields(fields=="auto_margins") = [];
329             automargins = %t;
330         end
331
332         for i = 1:size(fields, "*")
333             if or(fields(i) == ["title","x_label","y_label","z_label"]) then
334                 createLabel(axesProperties(fields(i)), h(fields(i)));
335             elseif or(fields(i) == ["x_ticks", "y_ticks", "z_ticks"]) then
336                 set(h, fields(i), createTicks(axesProperties(fields(i))));
337             elseif fields(i) == "children" then
338                 createMatrixHandle(axesProperties(fields(i)));
339             elseif fields(i) == "clip_state" then
340                 if axesProperties.clip_state=="on" then
341                     set(h,"clip_box",axesProperties.clip_box);
342                 end
343                 set(h,"clip_state", axesProperties.clip_state);
344             elseif fields(i) == "clip_box" then
345                 // managed with 'clip_state'
346             elseif fields(i) == "data_bounds" then
347                 set(h, "data_bounds", axesProperties.data_bounds);
348                 set(h, "log_flags", log_flags);
349             elseif fields(i) == "mark_style" then
350                 set(h, "mark_style", axesProperties.mark_style);
351                 set(h, "mark_mode", mark_mode);
352             else
353                 set(h, fields(i), axesProperties(fields(i)));;
354             end
355         end
356
357         set(h, "auto_ticks", auto_ticks);
358         if automargins then
359             set(h, "auto_margins", auto_margins);
360         end
361
362         // Legend management
363         global %LEG
364         if ~isempty(%LEG) then
365             // Get handles from paths
366             links=getlinksfrompath(h, %LEG.paths)
367             if ~isempty(links) then
368                 L = captions(links, %LEG.text)
369                 L.visible         = %LEG.visible
370                 L.font_style      = %LEG.font_style
371                 L.font_size       = %LEG.font_size
372                 L.font_color      = %LEG.font_color
373                 L.fractional_font = %LEG.fractional_font
374                 L.mark_mode       = "off";
375                 L.legend_location = %LEG.legend_location
376                 L.position        = %LEG.position
377                 L.line_mode       = %LEG.line_mode
378                 L.thickness       = %LEG.thickness
379                 L.foreground      = %LEG.foreground
380                 L.fill_mode       = %LEG.fill_mode
381                 L.background      = %LEG.background
382                 if %LEG.clip_state=="on" then
383                     L.clip_box      = %LEG.clip_box
384                 end
385                 L.clip_state      = %LEG.clip_state
386                 L.user_data       = %LEG.user_data
387             else
388                 warning(msprintf(_("%s: Legend does not fit with the current context. Skipped\n"), "load"));
389             end
390         end
391         clearglobal %LEG
392
393     endfunction
394
395     //
396     // POLYLINE
397     //
398     function h = createPolyline(polylineProperties)
399         fields = fieldnames(polylineProperties);
400         fields(1) = [];
401
402         xpoly(polylineProperties.data(:,1), polylineProperties.data(:,2))
403
404         h = gce();
405
406         if polylineProperties.clip_state=="on" then
407             set(h, "clip_box", polylineProperties.clip_box)
408         end
409         set(h, "clip_state", polylineProperties.clip_state);
410         fields(fields=="clip_box") = [];
411         fields(fields=="clip_state") = [];
412
413         if polylineProperties.interp_color_mode=="on" & ~isempty(polylineProperties.interp_color_vector) then
414             set(h, "interp_color_vector", polylineProperties.interp_color_vector);
415             set(h, "interp_color_mode", polylineProperties.interp_color_mode);
416         else
417             if ~isempty(polylineProperties.interp_color_vector) then
418                 h.interp_color_vector = polylineProperties.interp_color_vector;
419             end
420             h.interp_color_mode = polylineProperties.interp_color_mode;
421         end
422         fields(fields=="interp_color_vector") = [];
423         fields(fields=="interp_color_mode") = [];
424
425         // Get mark_mode to be sure to set it after mark_style
426         mark_mode = polylineProperties.mark_mode;
427         fields(fields=="mark_mode") = [];
428
429         global %POLYLINE
430         %POLYLINE = h
431
432         for i = 1:size(fields, "*")
433             if fields(i) == "mark_style" then
434                 set(h, "mark_style", polylineProperties.mark_style);
435                 set(h, "mark_mode", mark_mode);
436             elseif fields(i) == "children" then
437                 createMatrixHandle(polylineProperties(fields(i)));
438             elseif fields(i) == "datatips" then
439                 createMatrixHandle(polylineProperties(fields(i)));
440             else
441                 h(fields(i)) = polylineProperties(fields(i));
442             end
443         end
444
445         clearglobal %POLYLINE
446
447     endfunction
448
449     //
450     // PLOT3D
451     //
452     function h = createPlot3d(plot3dProperties)
453         h = createSurface(plot3dProperties);
454     endfunction
455
456     //
457     // FAC3D
458     //
459     function h = createFac3d(fac3dProperties)
460         h = createSurface(fac3dProperties);
461     endfunction
462
463     //
464     // SURFACE
465     //
466     function h = createSurface(surfaceProperties)
467         fields = fieldnames(surfaceProperties);
468         fields(1) = [];
469         // plot3d modify the axes properties
470         //  - Save it
471         // - Draw plot3d
472         // - Restore it
473         a=gca();
474         rotation_angles = a.rotation_angles;
475         axes_visible = a.axes_visible;
476         box = a.box;
477         margins = a.margins;
478         x_label_visible = a.x_label.visible;
479         y_label_visible = a.y_label.visible;
480         z_label_visible = a.z_label.visible;
481         x_label_text = a.x_label.text;
482         y_label_text = a.y_label.text;
483         z_label_text = a.z_label.text;
484         axes_isoview = a.isoview;
485
486         if (or(surfaceProperties.color_flag==[2 5]) & ~or(fields=="cdata_mapping")) | ..
487             ((surfaceProperties.color_flag>=2) & or(fields=="cdata_mapping")) then
488             plot3d1(surfaceProperties.data.x, surfaceProperties.data.y, list(surfaceProperties.data.z, surfaceProperties.data.color))
489         else
490             plot3d(surfaceProperties.data.x,surfaceProperties.data.y,surfaceProperties.data.z)
491         end
492         fields(fields=="data") = [];
493
494         // Restore this properties after plot3d.
495         a.rotation_angles = rotation_angles;
496         a.axes_visible = axes_visible;
497         a.box = box;
498         a.margins = margins;
499         a.x_label.visible = x_label_visible;
500         a.y_label.visible = y_label_visible;
501         a.z_label.visible = z_label_visible;
502         a.x_label.text = x_label_text;
503         a.y_label.text = y_label_text;
504         a.z_label.text = z_label_text;
505         a.isoview = axes_isoview;
506
507         // Get mark_mode to be sure to set it after mark_style
508         mark_mode = surfaceProperties.mark_mode;
509         fields(fields=="mark_mode") = [];
510
511         h=gce();
512
513         if or(fields=="cdata_mapping") then // Fac3d specific
514             if surfaceProperties.color_flag >= 2 then
515                 set(h, "cdata_mapping", surfaceProperties.cdata_mapping);
516             end
517             fields(fields=="cdata_mapping") = [];
518         end
519
520         if surfaceProperties.clip_state == "on" then
521             set(h,"clip_box", surfaceProperties.clip_box);
522         end
523         set(h,"clip_state",surfaceProperties.clip_state);
524         fields(fields=="clip_box") = [];
525         fields(fields=="clip_state") = [];
526
527         for i = 1:size(fields, "*")
528             if fields(i) == "mark_style" then
529                 set(h, "mark_style", surfaceProperties.mark_style);
530                 set(h, "mark_mode", mark_mode);
531             else
532                 h(fields(i)) = surfaceProperties(fields(i));
533             end
534         end
535     endfunction
536
537     //
538     // COMPOUND
539     //
540     function h = createCompound(compoundProperties)
541         fields = fieldnames(compoundProperties);
542         fields(1) = [];
543
544         h = glue(createMatrixHandle(compoundProperties.children));
545         fields(fields=="children") = [];
546
547         for i = 1:size(fields, "*")
548             set(h, fields(i), compoundProperties(fields(i)));
549         end
550     endfunction
551
552     //
553     // RECTANGLE
554     //
555     function h = createRectangle(rectangleProperties)
556         fields = fieldnames(rectangleProperties);
557         fields(1) = [];
558
559         xrect(0,1,1,1); // create the rectangle with dummy values
560         h = gce();
561
562         if rectangleProperties.clip_state == "on" then
563             set(h,"clip_box", rectangleProperties.clip_box);
564         end
565         set(h,"clip_state",rectangleProperties.clip_state);
566         fields(fields=="clip_box") = [];
567         fields(fields=="clip_state") = [];
568
569         // Get mark_mode to be sure to set it after mark_style
570         mark_mode = rectangleProperties.mark_mode;
571         fields(fields=="mark_mode") = [];
572
573         for i = 1:size(fields, "*")
574             if fields(i) == "mark_style" then
575                 set(h, "mark_style", rectangleProperties.mark_style);
576                 set(h, "mark_mode", mark_mode);
577             else
578                 h(fields(i)) = rectangleProperties(fields(i));
579             end
580         end
581     endfunction
582
583     //
584     // ARC
585     //
586     function h = createArc(arcProperties)
587         fields = fieldnames(arcProperties);
588         fields(1) = [];
589
590         xarc(0,1,1,1,0,360); // create the arc with dummy values
591         h = gce();
592
593         if arcProperties.clip_state == "on" then
594             set(h,"clip_box", arcProperties.clip_box);
595         end
596         set(h,"clip_state",arcProperties.clip_state);
597         fields(fields=="clip_box") = [];
598         fields(fields=="clip_state") = [];
599
600         for i = 1:size(fields, "*")
601             set(h, fields(i), arcProperties(fields(i)));
602         end
603     endfunction
604
605     //
606     // CHAMP
607     //
608     function h = createChamp(champProperties)
609         fields = fieldnames(champProperties);
610         fields(1) = [];
611
612         champ(champProperties.data.x, champProperties.data.y, champProperties.data.fx, champProperties.data.fy);
613         fields(fields=="data") = [];
614
615         h=gce();
616
617         if champProperties.clip_state == "on" then
618             set(h,"clip_box", champProperties.clip_box);
619         end
620         set(h,"clip_state",champProperties.clip_state);
621         fields(fields=="clip_box") = [];
622         fields(fields=="clip_state") = [];
623
624         for i = 1:size(fields, "*")
625             set(h, fields(i), champProperties(fields(i)));
626         end
627     endfunction
628
629     //
630     // SEG
631     //
632     function h = createSegs(segsProperties)
633         fields = fieldnames(segsProperties);
634         fields(1) = [];
635
636         xsegs(segsProperties.data(:,1), segsProperties.data(:,2))
637
638         h=gce()
639
640         if segsProperties.clip_state == "on" then
641             set(h,"clip_box", segsProperties.clip_box);
642         end
643         set(h,"clip_state",segsProperties.clip_state);
644         fields(fields=="clip_box") = [];
645         fields(fields=="clip_state") = [];
646
647         // Get mark_mode to be sure to set it after mark_style
648         mark_mode = segsProperties.mark_mode;
649         fields(fields=="mark_mode") = [];
650
651         for i = 1:size(fields, "*")
652             if fields(i) == "mark_style" then
653                 set(h, "mark_style", segsProperties.mark_style);
654                 set(h, "mark_mode", mark_mode);
655             else
656                 h(fields(i)) = segsProperties(fields(i));
657             end
658         end
659     endfunction
660
661     //
662     // GRAYPLOT
663     //
664     function h = createGrayplot(grayplotProperties)
665         fields = fieldnames(grayplotProperties);
666         fields(1) = [];
667
668         grayplot(grayplotProperties.data.x, grayplotProperties.data.y, grayplotProperties.data.z);
669         fields(fields=="data") = [];
670
671         h = gce();
672
673         if grayplotProperties.clip_state=="on" then
674             set(h, "clip_box", grayplotProperties.clip_box)
675         end
676         set(h, "clip_state", grayplotProperties.clip_state);
677         fields(fields=="clip_box") = [];
678         fields(fields=="clip_state") = [];
679
680         for i = 1:size(fields, "*")
681             set(h, fields(i), grayplotProperties(fields(i)));
682         end
683     endfunction
684
685     //
686     // MATPLOT
687     //
688     function h = createMatplot(matplotProperties)
689         fields = fieldnames(matplotProperties);
690         fields(1) = [];
691
692         Matplot(matplotProperties.data);
693         fields(fields=="data") = [];
694
695         h = gce();
696
697         if matplotProperties.clip_state=="on" then
698             set(h, "clip_box", matplotProperties.clip_box)
699         end
700         set(h, "clip_state", matplotProperties.clip_state);
701         fields(fields=="clip_box") = [];
702         fields(fields=="clip_state") = [];
703
704         for i = 1:size(fields, "*")
705             set(h, fields(i), matplotProperties(fields(i)));
706         end
707     endfunction
708
709     //
710     // FEC
711     //
712     function h = createFec(fecProperties)
713         fields = fieldnames(fecProperties);
714         fields(1) = [];
715
716         fec(fecProperties.data(:,1), fecProperties.data(:,2), fecProperties.triangles, fecProperties.data(:,3));
717         fields(fields=="data") = [];
718         fields(fields=="triangles") = [];
719
720         h = unglue(gce());
721
722         if fecProperties.clip_state=="on" then
723             set(h, "clip_box", fecProperties.clip_box)
724         end
725         set(h, "clip_state", fecProperties.clip_state);
726         fields(fields=="clip_box") = [];
727         fields(fields=="clip_state") = [];
728
729         for i = 1:size(fields, "*")
730             set(h, fields(i), fecProperties(fields(i)));
731         end
732     endfunction
733
734     //
735     // LEGEND
736     //
737     function h = createLegend(legendProperties)
738         global %LEG
739         %LEG = legendProperties;
740         h = [];
741     endfunction
742
743     //
744     // TEXT
745     //
746     function h = createText(textProperties)
747         fields = fieldnames(textProperties);
748         fields(1) = [];
749
750         if textProperties.text_box_mode == "off" then
751             xstring(textProperties.data(1), textProperties.data(2), textProperties.text)
752         else
753             xstringb(textProperties.data(1), textProperties.data(2), textProperties.text, textProperties.text_box(1), textProperties.text_box(2))
754         end
755
756         h = gce();
757
758         if textProperties.clip_state=="on" then
759             set(h, "clip_box", textProperties.clip_box)
760         end
761         set(h, "clip_state", textProperties.clip_state);
762         fields(fields=="clip_box") = [];
763         fields(fields=="clip_state") = [];
764
765         for i = 1:size(fields, "*")
766             set(h, fields(i), textProperties(fields(i)));
767         end
768     endfunction
769
770     //
771     // DATATIP
772     //
773     function h = createDatatip(datatipProperties)
774
775         fields = fieldnames(datatipProperties);
776         fields(1) = [];
777
778         tip_data = datatipProperties("data");
779         h = datatipCreate(%POLYLINE, tip_data);
780
781         for i = 1:size(fields, "*")
782             if fields(i) == "data" then
783                 continue;
784             end
785
786             set(h, fields(i), datatipProperties(fields(i)));
787         end
788     endfunction
789
790     //
791     // AXIS
792     //
793     function h = createAxis(axisProperties)
794         fields = fieldnames(axisProperties);
795         fields(1) = [];
796
797         if axisProperties.tics_direction == "bottom" then
798             axisdir="d";
799         elseif axisProperties.tics_direction == "top" then
800             axisdir="u";
801         elseif axisProperties.tics_direction == "left" then
802             axisdir="l";
803         elseif axisProperties.tics_direction == "right" then
804             axisdir="r";
805         elseif size(axisProperties.xtics_coord, "*") > 1 then
806             axisdir="u";
807         else
808             axisdir="l";
809         end
810         fields(fields=="tics_direction") = [];
811
812         drawaxis(x=axisProperties.xtics_coord,y=axisProperties.ytics_coord,dir=axisdir);
813         fields(fields=="xtics_coord") = [];
814         fields(fields=="ytics_coord") = [];
815
816         h=gce()
817
818         if axisProperties.clip_state=="on" then
819             set(h, "clip_box", axisProperties.clip_box)
820         end
821         set(h, "clip_state", axisProperties.clip_state);
822         fields(fields=="clip_box") = [];
823         fields(fields=="clip_state") = [];
824
825         for i = 1:size(fields, "*")
826             set(h, fields(i), axisProperties(fields(i)));
827         end
828     endfunction
829
830     //
831     // uimenu
832     //
833     function h = createuimenu(uimenuProperties)
834         fields = fieldnames(uimenuProperties);
835         fields(1) = [];
836
837         h = uimenu();
838
839         for i = 1:size(fields, "*")
840             if fields(i) == "children" then
841                 children = createMatrixHandle(uimenuProperties(fields(i)));
842                 for k=1:size(children, "*")
843                     set(children(k), "parent", h);
844                 end
845             else
846                 set(h, fields(i), uimenuProperties(fields(i)));
847             end
848         end
849     endfunction
850
851     //
852     // UICONTEXTMENU
853     //
854     function h = createuicontextmenu(uicontextmenuProperties)
855         fields = fieldnames(uicontextmenuProperties);
856         fields(1) = [];
857
858         h = uicontextmenu();
859
860         for i = 1:size(fields, "*")
861             if fields(i) == "children" then
862                 children = createMatrixHandle(uicontextmenuProperties(fields(i)));
863                 for k=1:size(children, "*")
864                     set(children(k), "parent", h);
865                 end
866             else
867                 set(h, fields(i), uicontextmenuProperties(fields(i)));
868             end
869         end
870     endfunction
871
872     //
873     // uicontrol
874     //
875     function h = createuicontrol(uicontrolProperties)
876         fields = fieldnames(uicontrolProperties);
877         fields(1) = [];
878
879         if or(fields=="scrollable") then
880             // Properties added in Scilab 5.5.0
881             //  - scrollable must be set at creation (for frames)
882             //  - contraints & margins must be set before parent
883             h = uicontrol("style", uicontrolProperties.style, ...
884             "scrollable", uicontrolProperties.scrollable, ...
885             "constraints", uicontrolProperties.constraints, ...
886             "margins", uicontrolProperties.margins);
887             fields(fields=="scrollable") = [];
888             fields(fields=="constraints") = [];
889             fields(fields=="margins") = [];
890             h.layout_options = uicontrolProperties.layout_options;
891             fields(fields=="layout_options") = [];
892             h.layout = uicontrolProperties.layout;
893             fields(fields=="layout") = [];
894         else
895             h = uicontrol("style", uicontrolProperties.style);
896         end
897         fields(fields=="style") = [];
898
899         for i = 1:size(fields, "*")
900             if fields(i) == "children" then
901                 children = createMatrixHandle(uicontrolProperties(fields(i)));
902                 for k=1:size(children, "*")
903                     set(children(k), "parent", h);
904                 end
905             else
906                 set(h, fields(i), uicontrolProperties(fields(i)));
907             end
908         end
909     endfunction
910
911     //
912     // LIGHT
913     //
914     function h = createLight(lightProperties)
915         fields = fieldnames(lightProperties);
916         fields(1) = [];
917
918         h = light();
919         fields(fields=="children") = [];
920
921         for i = 1:size(fields, "*")
922             set(h, fields(i), lightProperties(fields(i)));
923         end
924     endfunction
925
926     // Utility function for legends, copy/paste from %h_load
927     function links=getlinksfrompath(ax,paths)
928         //  ax is a  handle on an axes entity
929         //  paths a list or row vector which gives the set of paths relative to
930         //  the axes
931         links=[];
932         ok=%t;
933         for p=paths
934             e=ax;
935             p(1)=p(1)-1// the caption does not exists yet
936             for kp=1:size(p,"*"),
937                 if or(e.type==["Axes","Compound"])&p(kp)<=size(e.children,"*") then
938                     e=e.children(p(kp)),
939                 else
940                     ok=%f
941                     break
942                 end
943             end
944             if ~ok then
945                 break
946             end
947             links=[links,e]
948         end
949         if ~ok then
950             links=[];
951         end
952     endfunction
953
954     function macro = createMacro(macroStr, macroName)
955
956         macroSt = macroStr(3);
957         if macroStr(2) == %t then
958             flag = "c";
959         else
960             flag = "n";
961         end
962         header = strsubst(macroSt(1), "function ", "");
963         body = macroSt(2:$-1);
964         if body == [] then
965             body = "";
966         end
967         deff(header, body, flag);
968         execstr("macro = " + macroName);
969     endfunction
970
971     [%__lhs__, %__rhs__] = argn();
972     %__resumeList__ = list();
973     %__resumeVarlist__ = [];
974     if %__rhs__ < 1 then
975         error(999, msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"), "load", 1));
976     end
977
978     if %__rhs__ >= 1 then
979         if typeof(%__filename__) <> "string" | size(%__filename__, "*") <> 1 then
980             error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", 1));
981         end
982     end
983
984     if isfile(%__filename__) & is_hdf5_file(%__filename__) then
985         %__loadFunction__ = import_from_hdf5;
986         //fileVersion = getScilabFileVersion(%__filename__); // Not needed for the moment
987     else
988         %__loadFunction__ = load;
989     end
990
991     //multiple output variables to prevent listinfile prints
992     [%__variableList__, %__varB__, %__varC__, %__varD__] = listvarinfile(%__filename__);
993     //
994     if size(varargin) <> 0 then
995         for i = 1:size(varargin)
996             %__variableName__ = varargin(i);
997             if typeof(%__variableName__) <> "string" | size(%__variableName__, "*") <> 1 then
998                 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", i));
999             end
1000
1001             if or(%__variableList__ == %__variableName__) then
1002                 %__loadFunction__(%__filename__, %__variableName__);
1003                 %__resumeList__($+1) = evstr(%__variableName__);
1004                 %__resumeVarlist__($+1) = %__variableName__;
1005                 clear(%__variableName__);
1006             else
1007                 error(999, msprintf(gettext("%s: variable ''%s'' does not exist in ''%s''.\n"), "load", %__variableName__, %__filename__));
1008             end
1009         end
1010     else
1011         for i = 1:size(%__variableList__, "*")
1012             %__variableName__ = %__variableList__(i);
1013             %__loadFunction__(%__filename__, %__variableName__);
1014             %__resumeList__($+1) = evstr(%__variableName__);
1015             %__resumeVarlist__($+1) = %__variableName__;
1016             clear(%__variableName__);
1017         end
1018     end
1019
1020     if isfile(%__filename__) & is_hdf5_file(%__filename__) then
1021         %__resumeList__ = %__convertVariable__(%__resumeList__, %__resumeVarlist__);
1022     end
1023
1024     execstr("[" + strcat(%__resumeVarlist__, ",") + "] = resume(%__resumeList__(:))");
1025 endfunction