* Bug 16167 fixed: clf('reset') could remove native menus
[scilab.git] / scilab / modules / graphics / macros / clf.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 // Copyright (C) 2017 - 2019 - Samuel GOUGEON
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 function clf(varargin)
14
15     nbArg = size(varargin);
16     protected = ["atomsFigure" "scilab_demo_fig" "m2sci_gui"];
17
18     if nbArg==0 then
19         h = gcf()
20         if or(h.tag==protected)
21             h = []
22             fid = winsid()
23             for id = fid($:-1:1)
24                 f = scf(id);
25                 if and(f.tag~=protected)
26                     h = f
27                     break
28                 end
29             end
30             if h==[]
31                 h = scf()
32             end
33         end
34         job = "clear"
35
36     elseif nbArg==1 then
37         if type(varargin(1))==1 then     // win num given
38             num = varargin(1)
39             h = [];
40             for k = 1:size(num,"*")
41                 h = [h ; findobj("figure_id",num(k))];
42             end
43             job = "clear"
44         elseif type(varargin(1))==9 then // handle given
45             h = varargin(1);
46             job = "clear"
47         elseif type(varargin(1))==10 then // job given
48             h = gcf()
49             job = varargin(1)
50         end
51
52     elseif nbArg==2 then
53         if type(varargin(1))==1 then     // win num given
54             num = varargin(1)
55             h = [];
56             for k = 1:size(num,"*")
57                 h = [h ; findobj("figure_id",num(k))];
58             end
59         elseif type(varargin(1))==9 then // handle given
60             h = varargin(1);
61         end
62         job = varargin(2);
63
64     else
65         msg = _("%s: Wrong number of input argument(s): %d to %d expected.")
66         error(msprintf(msg, "clf", 0, 2))
67     end
68
69     if and(job<>["clear","reset"]) then
70         msg = _("%s: Wrong value for input argument #%d: ''clear'' or ''reset'' expected.")
71         error(msprintf(msg, "clf", nbArg))
72     end
73
74     nbHandles = size(h,"*");
75     if nbHandles == 0 then
76         return
77     end
78
79     // check that all the handles are figures or uicontrol frames
80     for k = 1:nbHandles
81         curFig = h(k);
82         if curFig.type <> "Figure" & (curFig.type <> "uicontrol" | curFig.style <> "frame")
83             msg = _("%s: Wrong type for input argument #%d: A vector of ''Figure'' or ''Frame'' handle expected.")
84             error(msprintf(msg, "clf", 1))
85         end
86     end
87
88     // Creation of a template figure, to get native menus to preserve or reset
89     if or(h.type=="Figure") & get(get(0),"showhiddenhandles")=="on"
90         tmpfig = figure("visible","off")
91         menus = tmpfig.children(tmpfig.children.type=="uimenu")
92     else
93         menus = []
94     end
95
96     // delete childrens
97     for k = 1:nbHandles
98         curFig = h(k)
99
100         if or(curFig.tag==protected) then
101             continue
102         end
103         //
104         if curFig.type == "uicontrol" then
105             haveAxes = %F;
106             for kChild = 1:size(curFig.children, "*")
107                 if curFig.children(kChild).type=="Axes" then
108                     haveAxes = %T;
109                     break
110                 end
111             end
112             delete(curFig.children);
113             if haveAxes then
114                 newaxes(curFig);
115             end
116         else
117             // Forces drawlater
118             immediateMode = curFig.immediate_drawing;
119             curFig.immediate_drawing = "off";
120             curFig.info_message = "";            // Clears the infobar message
121             curFig.event_handler_enable = "off"; // Disables the event handler
122             // Deletes children, but manages native menus
123             clf_clear_children(curFig, job, menus);
124             // Restores the drawlater entry status:
125             curFig.immediate_drawing = immediateMode;
126         end
127     end
128     if isdef("tmpfig","l") then
129         close(tmpfig)
130     end
131
132     // reset figures to default values if needed
133     if (job == "reset") then
134         defaultFig = gdf();
135         // This literal list should be replaced with a getproperties(gdf())
136         // when such a function will be available:
137         allprops = [
138         "children"
139         "figure_position"
140         "figure_size"
141         "axes_size"
142         "auto_resize"
143         "viewport"
144         "figure_name"
145         "figure_id"
146         "info_message"
147         "color_map"
148         "pixel_drawing_mode"
149         "anti_aliasing"
150         "immediate_drawing"
151         "background"
152         "visible"
153         "rotation_style"
154         "event_handler"
155         "event_handler_enable"
156         "user_data"
157         "resizefcn"
158         "closerequestfcn"
159         "resize"
160         "toolbar"
161         "toolbar_visible"
162         "menubar"
163         "menubar_visible"
164         "infobar_visible"
165         "dockable"
166         "layout"
167         "layout_options"
168         "default_axes"
169         "icon"
170         "tag"
171         ];
172         excluded0 = ["children" "figure_id" "dockable" "menubar" "toolbar" "immediate_drawing"]
173         excluded0 = [excluded0 "layout" "layout_options"] // http://bugzilla.scilab.org/14955
174         for k = 1: nbHandles
175             curFig = h(k);
176             if curFig.type == "uicontrol" | or(curFig.tag == protected) then
177                 continue
178             end
179
180             // Forces drawlater mode
181             curFig.immediate_drawing = "off";
182
183             // properties to set
184             excluded = excluded0
185             if isDocked(curFig)            // http://bugzilla.scilab.org/11476
186                 excluded = [excluded "figure_position" "figure_size" "axes_size"]
187             end
188             defaultProps = setdiff(allprops, excluded);
189
190             // Settings
191             for i = 1:size(defaultProps,"*")
192                 defaultValue = get(defaultFig, defaultProps(i));
193                 if (defaultProps(i) <> "figure_position" | defaultValue <> [-1,-1]) then
194                     // don't reset figure pos is defaultValue is [-1,-1]
195                     set(curFig, defaultProps(i), defaultValue);
196                 end
197             end
198             set(curFig, "immediate_drawing", defaultFig.immediate_drawing)
199         end
200     end
201 endfunction
202
203 // --------------------------------------------------------------------------
204
205 function clf_clear_children(h, job, menus)
206     // internal only called for Figures (not for frames)
207     c = curFig.children
208     if  menus==[] then
209         delete(c)  // native menus are not seen and not deleted
210     else
211         // Native menus must be preserved
212         labels = menus.label
213         tmp = c.type <> "uimenu"
214         delete(c(tmp)) // But a default axes might have been recreated
215         c = curFig.children
216         for i = length(c):-1:1
217             if c(i).type <> "uimenu"    // Possible default axes
218                 continue
219             end
220             tmp = c(i).label
221             j = find(c(i).label==menus.label)
222             if j==[]
223                 delete(c(i))    // Not native menu
224             else
225                 // We keep the menu. We possibly reset it
226                 if job=="reset"
227                     // the template menu is inserted as children(1):
228                     // hm = copy(menus(j), h)   // yields an error
229                     // swap_handles(hm,c(i))
230                     // delete(curFig.children(1))
231                     // => How to restore default native menus?
232                 end
233             end
234         end
235     end
236 endfunction