bug 11022: -append can be use in save and export_to_hdf5 functions at any position...
[scilab.git] / scilab / modules / io / macros / %_save.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
3 // 
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at    
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 //called by save function, transform handle in tlist and save result
11 function [] = %_save(filename, varargin)
12
13     varList = list();
14     for i = 1:size(varargin)
15
16         if varargin(i) == "-append" then
17             continue;
18         end
19
20         temp = evstr(varargin(i));
21
22         if isList(temp) then
23             //list container
24             value = inspectList(temp);
25             //update 
26             execstr(varargin(i) + " = value");
27         elseif typeof(temp) == "handle" then
28             //convert handle to tlist
29             value = extractMatrixHandle(temp);
30             //update 
31             execstr(varargin(i) + " = value");
32         end
33     end
34
35     result = export_to_hdf5(filename, varargin(:));
36
37 endfunction
38
39 function result = isList(var)
40
41     //15 : list
42     //16 : tlist
43     //17 : mlist
44     if or(type(var) == [15, 16, 17]) then
45         result  = %t;
46     else
47         result = %f;
48     end
49 endfunction
50
51 function result = inspectList(l)
52 if typeof(l)=="list" then
53     result = list();
54     for i = definedfields(l)
55         if typeof(l(i)) == "handle" then
56             result(i) = extractMatrixHandle(l(i));
57         elseif isList(l(i)) then
58             result(i) = inspectList(l(i));
59         else
60             result(i) = l(i);
61         end
62     end
63 else
64     fieldNb = size(getfield(1, l), "*");
65     for kField = 2:fieldNb // Do not inspect first field (field names)
66         fieldValue = getfield(kField, l);
67         if typeof(fieldValue) == "handle" then
68             fieldValue = extractMatrixHandle(fieldValue);
69         elseif isList(fieldValue) then
70             fieldValue = inspectList(fieldValue);
71         end
72         setfield(kField, fieldValue, l);
73     end
74     result = l;
75 end
76 endfunction
77
78 function matrixHandle = extractMatrixHandle(h)
79
80     if typeof(h) <> "handle" then
81         matrixHandle = [];
82         return;
83     end
84
85     matrixHandle = tlist(["ScilabMatrixHandle", "dims", "values"]);
86     matrixHandle.dims = size(h);
87     matrixHandle.values = list();
88     for i = 1:size(h, "*")
89         matrixHandle.values($+1) = extractSingleHandle(h(i));
90         if or(fieldnames(matrixHandle.values($))=="user_data") then // TODO Remove after graphic branch merge
91             if isList(matrixHandle.values($).user_data) then
92                 matrixHandle.values($).user_data = inspectList(matrixHandle.values($).user_data)
93             elseif typeof(matrixHandle.values($).user_data) == "handle" then
94                 matrixHandle.values($).user_data = extractMatrixHandle(matrixHandle.values($).user_data)
95             end
96         end
97     end
98 endfunction
99
100
101 function item = extractSingleHandle(h)
102
103     select h.type
104     case "Figure"
105         item = extractFigure(h);
106     case "Axes"
107         item = extractAxes(h);
108     case "Polyline"
109         item = extractPolyline(h);
110     case "Plot3d"
111         item = extractPlot3d(h);
112     case "Fac3d"
113         item = extractFac3d(h);
114     case "Compound"
115         item = extractCompound(h);
116     case "Rectangle"
117         item = extractRectangle(h);
118     case "Arc"
119         item = extractArc(h);
120     case "Champ"
121         item = extractChamp(h);
122     case "Segs"
123         item = extractSegs(h);
124     case "Grayplot"
125         item = extractGrayplot(h);
126     case "Matplot"
127         item = extractMatplot(h);
128     case "Fec"
129         item = extractFec(h);
130     case "Legend"
131         item = extractLegend(h);
132     case "Text"
133         item = extractText(h);
134     case "Axis"
135         item = extractAxis(h);
136     case "uimenu"
137         item = extractuimenu(h);
138     case "uicontextmenu"
139         item = extractuicontextmenu(h);
140     case "uicontrol"
141         item = extractuicontrol(h);
142     else
143         error("handle of type " + h.type + " unhandled");
144         item = [];
145     end
146 endfunction
147
148 //
149 // FIGURE
150 //
151 function returnedFigure = extractFigure(h)
152     returnedFigure = tlist([
153             "ScilabSingleHandle", ...
154             "type", ...
155             "visible", ...
156             "figure_position", ...
157             "figure_size", ...
158             "axes_size", ...
159             "viewport", ...
160             "info_message", ...
161             "tag", ...
162             "auto_resize", ...
163             "figure_name", ...
164             "figure_id", ...
165             "color_map", ...
166             "pixmap", ...
167             "pixel_drawing_mode", ...
168             "anti_aliasing", ...
169             "immediate_drawing", ...
170             "background", ...
171             "rotation_style", ...
172             "event_handler", ...
173             "event_handler_enable", ...
174             "children", ...
175             "user_data"]);
176             
177     fields = fieldnames(returnedFigure);
178
179     for i = 1:size(fields, "*")
180         if fields(i) == "children" then
181             returnedFigure(fields(i)) = extractMatrixHandle(h(fields(i)));
182         else
183             returnedFigure(fields(i)) = h(fields(i));
184         end
185     end
186 endfunction
187
188 //
189 // LABEL
190 //
191 function returnedLabel = extractLabel(h)
192     returnedLabel = tlist([
193                 "ScilabSingleHandle", ...
194                 "type", ...
195                 "visible", ...
196                 "text", ...
197                 "font_foreground", ...
198                 "foreground", ...
199                 "background", ...
200                 "fill_mode", ...
201                 "font_style", ...
202                 "font_size", ...
203                 "fractional_font", ...
204                 "font_angle", ...
205                 "auto_rotation", ...
206                 "position", ...
207                 "auto_position"]);
208
209     fields = fieldnames(returnedLabel);
210
211     for i = 1:size(fields, "*")
212         returnedLabel(fields(i)) = h(fields(i));
213     end
214 endfunction
215
216 //
217 // TICKS
218 //
219 function returnedTicks = extractTicks(ticks)
220     returnedTicks = tlist([
221                 "Ticks", ...
222                 "locations", ...
223                 "labels"]);
224
225     fields = fieldnames(returnedTicks);
226
227     for i = 1:size(fields, "*")
228         returnedTicks(fields(i)) = ticks(fields(i));
229     end
230 endfunction
231
232 //
233 // AXES
234 //
235 function returnedAxes = extractAxes(h)
236     returnedAxes = tlist([
237                 "ScilabSingleHandle", ...
238                 "type", ...
239                 "visible", ...
240                 "axes_visible", ...
241                 "axes_reverse", ...
242                 "grid", ...
243                 "grid_position", ...
244                 "x_location", ...
245                 "y_location", ...
246                 "view", ...
247                 "title", ...
248                 "x_label", ...
249                 "y_label", ...
250                 "z_label", ...
251                 "auto_ticks", ...
252                 "x_ticks", ...
253                 "y_ticks", ...
254                 "z_ticks", ...
255                 "box", ...
256                 "filled", ...
257                 "sub_tics", ...
258                 "font_style", ...
259                 "font_size", ...
260                 "font_color", ...
261                 "fractional_font", ...
262                 "isoview", ...
263                 "cube_scaling", ...
264                 "rotation_angles", ...
265                 "log_flags", ...
266                 "tight_limits", ...
267                 "data_bounds", ...
268                 "zoom_box", ...
269                 "margins", ...
270                 "axes_bounds", ...
271                 "auto_clear", ...
272                 "auto_scale", ...
273                 "hidden_axis_color", ...
274                 "arc_drawing_method", ...
275                 "hiddencolor", ...
276                 "line_mode", ...
277                 "line_style", ...
278                 "thickness", ...
279                 "mark_mode", ...
280                 "mark_style", ...
281                 "mark_size", ...
282                 "mark_size_unit", ...
283                 "mark_foreground", ...
284                 "mark_background", ...
285                 "foreground", ...
286                 "background", ...
287                 "clip_state", ...
288                 "clip_box", ...
289                 "children", ...
290                 "user_data"]);
291
292     fields = fieldnames(returnedAxes);
293
294     for i = 1:size(fields, "*")
295         if or(fields(i) == ["title","x_label","y_label","z_label"]) then
296             returnedAxes(fields(i)) = extractLabel(h(fields(i)));
297         elseif or(fields(i) == ["x_ticks", "y_ticks", "z_ticks"]) then
298             returnedAxes(fields(i)) = extractTicks(h(fields(i)));
299         elseif fields(i) == "children" then
300             returnedAxes(fields(i)) = extractMatrixHandle(h(fields(i)));
301         else
302             returnedAxes(fields(i)) = h(fields(i));
303         end
304     end
305 endfunction
306
307 //
308 // POLYLINE
309 //
310 function returnedPolyline = extractPolyline(h)
311     returnedPolyline = tlist([
312             "ScilabSingleHandle", ...
313             "type", ...
314             "visible", ...
315             "data", ...
316             "closed", ...
317             "line_mode", ...
318             "fill_mode", ...
319             "line_style", ...
320             "thickness", ...
321             "arrow_size_factor", ...
322             "polyline_style", ...
323             "interp_color_vector", ...
324             "interp_color_mode", ...
325             "mark_mode", ...
326             "mark_style", ...
327             "mark_size", ...
328             "mark_size_unit", ...
329             "foreground", ...
330             "background", ...
331             "mark_foreground", ...
332             "mark_background", ...
333             "x_shift", ...
334             "y_shift", ...
335             "z_shift", ...
336             "bar_width", ...
337             "clip_state", ...
338             "clip_box", ...
339             "user_data"]);
340
341     fields = fieldnames(returnedPolyline);
342
343     for i = 1:size(fields, "*")
344         returnedPolyline(fields(i)) = h(fields(i));
345     end
346 endfunction
347
348 //
349 // PLOT3D
350 //
351 function returnedPlot3d = extractPlot3d(h)
352     returnedPlot3d = extractSurface(h);
353 endfunction
354
355 //
356 // FAC3D
357 //
358 function returnedFac3d = extractFac3d(h)
359     returnedFac3d = extractSurface(h);
360 endfunction
361
362 //
363 // SURFACE
364 //
365 function returnedSurface = extractSurface(h)
366     returnedSurface = tlist([
367             "ScilabSingleHandle", ...
368             "type", ...
369             "visible", ...
370             "surface_mode", ...
371             "foreground", ...
372             "thickness", ...
373             "mark_mode", ...
374             "mark_style", ...
375             "mark_size", ...
376             "mark_size_unit", ...
377             "mark_foreground", ...
378             "mark_background", ...
379             "color_mode", ...
380             "color_flag", ...
381             "data", ...
382             "color_flag", ...
383             "hiddencolor", ...
384             "clip_state", ...
385             "clip_box", ...
386             "user_data", ...
387             "cdata_mapping"]);
388
389     fields = fieldnames(returnedSurface);
390
391     for i = 1:size(fields, "*")
392         if fields(i)=="cdata_mapping" then
393             if h.type=="Fac3d" then
394                 returnedSurface(fields(i)) = h(fields(i));
395             end
396         else
397             returnedSurface(fields(i)) = h(fields(i));
398         end
399     end
400 endfunction
401
402 //
403 // COMPOUND
404 //
405 function returnedCompound = extractCompound(h)
406     returnedCompound = tlist([
407             "ScilabSingleHandle", ...
408             "type", ...
409             "visible", ...
410             "children", ...
411             "user_data"]);
412
413     fields = fieldnames(returnedCompound);
414
415     for i = 1:size(fields, "*")
416         if fields(i) == "children" then
417             returnedCompound(fields(i)) = extractMatrixHandle(h(fields(i)));
418         else
419             returnedCompound(fields(i)) = h(fields(i));
420         end
421     end
422 endfunction
423
424 //
425 // RECTANGLE
426 //
427 function returnedRectangle = extractRectangle(h)
428     returnedRectangle = tlist([
429             "ScilabSingleHandle", ...
430             "type", ...
431             "visible", ...
432             "thickness", ...
433             "mark_mode", ...
434             "mark_style", ...
435             "mark_size", ...
436             "mark_size_unit", ...
437             "mark_foreground", ...
438             "mark_background", ...
439             "line_mode", ...
440             "line_style", ...
441             "fill_mode", ...
442             "foreground", ...
443             "background", ...
444             "data", ...
445             "clip_state", ...
446             "clip_box", ...
447             "user_data"]);
448
449     fields = fieldnames(returnedRectangle);
450
451     for i = 1:size(fields, "*")
452         returnedRectangle(fields(i)) = h(fields(i));
453     end
454 endfunction
455
456 //
457 // ARC
458 //
459 function returnedArc = extractArc(h)
460     returnedArc = tlist([
461             "ScilabSingleHandle", ...
462             "type", ...
463             "visible", ...
464             "thickness", ...
465             "line_style", ...
466             "line_mode", ...
467             "fill_mode", ...
468             "foreground", ...
469             "background", ...
470             "data", ...
471             "arc_drawing_method", ...
472             "clip_state", ...
473             "clip_box", ...
474             "user_data"]);
475
476     fields = fieldnames(returnedArc);
477
478     for i = 1:size(fields, "*")
479         returnedArc(fields(i)) = h(fields(i));
480     end
481 endfunction
482
483 //
484 // CHAMP
485 //
486 function returnedChamp = extractChamp(h)
487      returnedChamp = tlist([
488             "ScilabSingleHandle", ...
489             "type", ...
490             "visible", ...
491             "data", ...
492             "line_style", ...
493             "thickness", ...
494             "colored", ...
495             "arrow_size", ...
496             "clip_state", ...
497             "clip_box", ...
498             "user_data"]);
499
500     fields = fieldnames(returnedChamp);
501
502     for i = 1:size(fields, "*")
503         returnedChamp(fields(i)) = h(fields(i));
504     end
505 endfunction
506
507 //
508 // SEG
509 //
510 function returnedSeg = extractSegs(h)
511     returnedSeg = tlist([
512             "ScilabSingleHandle", ...
513             "type", ...
514             "visible", ...
515             "data", ...
516             "line_mode", ...
517             "line_style", ...
518             "thickness", ...
519             "segs_color", ...
520             "mark_mode", ...
521             "mark_style", ...
522             "mark_size", ...
523             "mark_size_unit", ...
524             "mark_foreground", ...
525             "mark_background", ...
526             "clip_state", ...
527             "clip_box", ...
528             "user_data"]);
529
530     fields = fieldnames(returnedSeg);
531
532     for i = 1:size(fields, "*")
533         returnedSeg(fields(i)) = h(fields(i));
534     end
535 endfunction
536
537 //
538 // GRAYPLOT
539 //
540 function returnedGrayplot = extractGrayplot(h)
541     returnedGrayplot = tlist([
542             "ScilabSingleHandle", ...
543             "type", ...
544             "visible", ...
545             "data", ...
546             "data_mapping", ...
547             "clip_state", ...
548             "clip_box", ...
549             "user_data"]);
550
551     fields = fieldnames(returnedGrayplot);
552
553     for i = 1:size(fields, "*")
554         returnedGrayplot(fields(i)) = h(fields(i));
555     end
556 endfunction
557
558 //
559 // MATPLOT
560 //
561 function returnedMatplot = extractMatplot(h)
562     returnedMatplot = tlist([
563             "ScilabSingleHandle", ...
564             "type", ...
565             "visible", ...
566             "data", ...
567             "clip_state", ...
568             "clip_box", ...
569             "user_data"]);
570
571     fields = fieldnames(returnedMatplot);
572
573     for i = 1:size(fields, "*")
574         returnedMatplot(fields(i)) = h(fields(i));
575     end
576 endfunction
577
578 //
579 // FEC
580 //
581 function returnedFec = extractFec(h)
582     returnedFec = tlist([
583             "ScilabSingleHandle", ...
584             "type", ...
585             "visible", ...
586             "data", ...
587             "triangles", ...
588             "z_bounds", ...
589             "color_range", ...
590             "outside_colors", ...
591             "line_mode", ...
592             "foreground", ...
593             "clip_state", ...
594             "clip_box", ...
595             "user_data"]);
596
597     fields = fieldnames(returnedFec);
598
599     for i = 1:size(fields, "*")
600         returnedFec(fields(i)) = h(fields(i));
601     end
602 endfunction
603
604 //
605 // LEGEND
606 //
607 function returnedLegend = extractLegend(h)
608     returnedLegend = tlist([
609             "ScilabSingleHandle", ...
610             "type", ...
611             "visible", ...
612             "text", ...
613             "font_style", ...
614             "font_size", ...
615             "font_color", ...
616             "fractional_font", ...
617             "links", ...
618             "legend_location", ...
619             "position", ...
620             "line_mode", ...
621             "thickness", ...
622             "foreground", ...
623             "fill_mode", ...
624             "background", ...
625             "clip_state", ...
626             "clip_box", ...
627             "user_data", ..
628             "paths"]);
629
630     fields = fieldnames(returnedLegend);
631
632     for i = 1:size(fields, "*")
633         if fields(i) == "links" then
634             returnedLegend(fields(i)) = extractMatrixHandle(h(fields(i)));
635         elseif fields(i) == "paths" then
636             p = list();
637             for kl=1:size(h.links,'*');
638                 p($+1) = get_entity_path(h.links(kl));
639             end
640             returnedLegend(fields(i)) = p;
641         else
642             returnedLegend(fields(i)) = h(fields(i));
643         end
644     end
645 endfunction
646
647 //
648 // TEXT
649 //
650 function returnedText = extractText(h)
651     returnedText = tlist([
652             "ScilabSingleHandle", ...
653             "type", ...
654             "visible", ...
655             "text", ...
656             "data", ...
657             "text_box", ...
658             "text_box_mode", ...
659             "foreground", ...
660             "font_style", ...
661             "font_size", ...
662             "font_angle", ...
663             "box", ...
664             "line_mode", ...
665             "fill_mode", ...
666             "font_foreground", ...
667             "background", ...
668             "alignment", ...
669             "fractional_font", ...
670             "clip_state", ...
671             "clip_box", ...
672             "user_data"]);
673
674     fields = fieldnames(returnedText);
675
676     for i = 1:size(fields, "*")
677         returnedText(fields(i)) = h(fields(i));
678     end
679 endfunction
680
681 //
682 // AXIS
683 //
684 function returnedAxis = extractAxis(h)
685     returnedAxis = tlist([
686             "ScilabSingleHandle", ...
687             "type", ...
688             "visible", ...
689             "tics_direction", ...
690             "xtics_coord", ...
691             "ytics_coord", ...
692             "tics_color", ...
693             "tics_segment", ...
694             "tics_style", ...
695             "sub_tics", ...
696             "tics_labels", ...
697             "labels_font_size", ...
698             "labels_font_color", ...
699             "fractional_font", ...
700             "clip_state", ...
701             "clip_box", ...
702             "user_data"]);
703
704     fields = fieldnames(returnedAxis);
705
706     for i = 1:size(fields, "*")
707         returnedAxis(fields(i)) = h(fields(i));
708     end
709 endfunction
710
711 //
712 // uimenu
713 //
714 function returneduimenu = extractuimenu(h)
715     returneduimenu = tlist([
716             "ScilabSingleHandle", ...
717             "type", ...
718             "enable", ...
719             "foregroundcolor", ...
720             "label", ...
721             "visible", ...
722             "callback", ...
723             "callback_type", ...
724             "tag", ...
725             "checked", ...
726             "children", ..
727             "user_data"]);
728
729     fields = fieldnames(returneduimenu);
730
731     for i = 1:size(fields, "*")
732         if fields(i) == "children" then
733             returneduimenu(fields(i)) = extractMatrixHandle(h(fields(i))($:-1:1));
734         else
735             returneduimenu(fields(i)) = h(fields(i));
736         end
737     end
738 endfunction
739
740 //
741 // UICONTEXTMENU
742 //
743 function returneduicontextmenu = extractuicontextmenu(h)
744     returneduicontextmenu = tlist([
745             "ScilabSingleHandle", ...
746             "type", ...
747             "children"]);
748
749     fields = fieldnames(returneduicontextmenu);
750
751     for i = 1:size(fields, "*")
752         if fields(i) == "children" then
753             returneduicontextmenu(fields(i)) = extractMatrixHandle(h(fields(i))($:-1:1));
754         else
755             returneduicontextmenu(fields(i)) = h(fields(i));
756         end
757     end
758 endfunction
759
760 //
761 // uicontrol
762 //
763 function returneduicontrol = extractuicontrol(h)
764     returneduicontrol = tlist([
765             "ScilabSingleHandle", ...
766             "type", ...
767             "style", ...
768             "backgroundcolor", ...
769             "enable", ...
770             "fontangle", ...
771             "fontname", ...
772             "fontsize", ...
773             "fontunits", ...
774             "fontweight", ...
775             "foregroundcolor", ...
776             "horizontalalignment", ...
777             "listboxtop", ...
778             "max", ...
779             "min", ...
780             "position", ...
781             "relief", ...
782             "sliderstep", ...
783             "string", ...
784             "units", ...
785             "value", ...
786             "verticalalignment", ...
787             "visible", ...
788             "callback", ...
789             "callback_type", ...
790             "user_data", ...
791             "tag", ...
792             "children"]);
793
794     fields = fieldnames(returneduicontrol);
795
796     for i = 1:size(fields, "*")
797         if fields(i) == "children" then
798             returneduicontrol(fields(i)) = extractMatrixHandle(h(fields(i))($:-1:1));
799         else
800             returneduicontrol(fields(i)) = h(fields(i));
801         end
802     end
803 endfunction
804
805 // Utility function for legends, copy/paste from %h_load
806 function p=get_entity_path(e)
807 // given a handle e on an entity this function returns its path relative
808 // to its parent axes.
809 // the path is a vector of child index.
810 p=[];
811 parent=e.parent;
812
813 while  %t
814     pos=find(parent.children==e,1)
815     if pos==[] then
816         error(msprintf(_("%s : Invalid entity %s\n"),"save","Legend"))
817     end
818     p=[pos p]
819     if parent.type=='Axes' then
820         break
821     end
822     e=parent
823     parent=e.parent;
824 end
825 endfunction