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