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