1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2004-2006 - INRIA - Farid Belahcene
3// Copyright (C) 2012 - 2016 - Scilab Enterprises
4// Copyright (C) 2013, 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
13function TitleLabel(varargin)
14    // TITLELABEL function
15    // This internal is used by the functions title, xlabel, ylabel, zlabel
16
17    [lhs,rhs]=argn(0);
18
19    ListArg = varargin;
20    titlelabel = ListArg(1);
21    fname = strsubst(titlelabel,"_","");// for error messages
22    ListArg(1) = null();  // remove this parameter from the list
23    arginPos = 0;                       // to compute argin # for error messages
24
25    //detect and set the current axes now:
26    if type(ListArg(1)) == 9
27        hdle = ListArg(1);
28        if (hdle.type == "Axes")
29            sca(ListArg(1));
30            ListArg(1) = null(); // remove this parameter from the list
31            arginPos = 1;
32        else
33            warning(msprintf(gettext("%s: Wrong type for input argument #%d: Axes handle expected.\n"), fname, 1));
34            return;
35        end
36    end
37
38    monaxe = gca();
39
40    nv = size(ListArg);
41    if  modulo(nv,2) == 1 then
42        // detect and set the string now:
43        if  type(ListArg(1))== 10
44            st = ListArg(1);
45            execstr("monaxe."+ titlelabel +".text"+"=st")
46            ListArg(1) = null();
47            nv=nv-1
48            arginPos = arginPos + 1;
49        else
50            msg = gettext("%s: Wrong size or type for input argument #%d: string expected.\n")
51            error(msprintf(msg, fname, arginPos+1));
52        end
53    end
54
55    T = [];
56
57    for k=1:nv
58        T(k,1) = type(ListArg(k))
59    end
60
61    given_data = 0;
62    P1 = 0;
63
64    for i=1:nv
65        if T(i) == 1
66            given_data = given_data +1;
67        else
68            P1 = i;  // Position of the first PropertyName field
69            break;
70        end
71    end
72
73    // delay the drawing commands
74    // smart drawlater
75    current_figure = gcf();
76    cur_draw_mode = current_figure.immediate_drawing;
77    current_figure.immediate_drawing = "off";
78
79    ///////////////////////////////////
80    //Global Property treatment      //
81    //PropertyName and PropertyValue //
82    ///////////////////////////////////
83
84    // P1 is the position of the first PropertyName field.
85    Property = P1;
86
87    current_titlelabel = get(monaxe,titlelabel)
88    arginPos = arginPos + 1;  // position of the propertyName in the caller argin list
89    while ((Property <> 0) & (Property <= nv-1))
90        setTitleLabelProperty(ListArg(Property),ListArg(Property+1),current_titlelabel,current_figure,cur_draw_mode, fname, arginPos)
91        Property = Property+2;
92        arginPos = arginPos+2;
93    end
94
95    //postponed drawings are done now !
96    // smart drawnow
97    ResetFigureDDM(current_figure, cur_draw_mode);
98endfunction
99
100// ============================================================================
101
102function [PName] = getTitleLabelPropertyNam(PropertyName,current_figure,cur_draw_mode)
103    // Copyright INRIA
104    // GETTITLELABELPROPERTYNAME function
105    // is used by the functions title, xlabel, ylabel, zlabel
106    // this function gets the matlab properties names and repalce them by the
107    // equivalent scilab properties names, using Table and TableEQ
108
109    //conversion to lower format
110    str = convstr(PropertyName);
111
112
113    Table    = [ "tag" "visible" "box" "fontname" "fontangle" "fontsize" ..
114    "rotation" "position" "color" "edgecolor" "backgroundcolor" ];
115
116    TableEQ =  [ "tag" "visible" "fill_mode" "font_style" "font_style" "font_size" ..
117    "font_angle" "position" "font_foreground" "foreground" "background"];
118
119    opt1=[];
120    k=find(part(Table,1:length(str))==str);
121
122    if (isempty(k)) then
123        // Scilab names are allowed too...
124        k=find(part(TableEQ,1:length(str))==str);
125        if ~isempty(k) then
126            PName = TableEQ(k(1))
127            return;
128        end
129    end
130
131    if (k == [])
132        warning("Error in Property specification : bad argument specified");
133        PName=[];
134        ResetFigureDDM(current_figure, cur_draw_mode);
135        return;
136    elseif (size(k,"*") > 1)
137        warning("Ambiguous text property");
138        PName=[];
139        ResetFigureDDM(current_figure, cur_draw_mode);
140        return;
141    end
142
143    opt = Table(k);
144    for i=1:length(str)
145        if part(opt,i) <> part(str,i)
146            break;
147        end
148    end
149
150    opt1 = [opt1 k];
151
152    str = part(str,i:length(str));
153
154    if (size(opt1,"*") > 1)
155        warning("Error in Property specification : too much properties specified");
156        ResetFigureDDM(current_figure, cur_draw_mode);
157        return;
158    end
159
160    PName = TableEQ(opt1);
161endfunction
162
163// ============================================================================
164
165function [fail] = setTitleLabelProperty(PropertyName,PropertyValue,titlelabel,current_figure,cur_draw_mode, fname, arginPos)
166    // SETTITLELABELPROPERTY function
167    // is used by the functions title, xlabel, ylabel, zlabel
168    // This function sets the title (or x_, y_, z_label) properties
169
170    fail=0;
171
172    //conversion to lower format
173    str = convstr(PropertyName);
174
175    //Property = ['foreground' 'clipping'];
176
177    [PName] = getTitleLabelPropertyNam(str, current_figure, cur_draw_mode)
178
179    if (PName==[])
180        ResetFigureDDM(current_figure, cur_draw_mode);
181        return;
182    end
183
184    /////////////////////////
185    if  PName == "font_size"       // <=> fontsize
186        /////////////////////////
187        ok =  size(PropertyValue,"*")==1 & or(type(PropertyValue)==[1 10]);
188        if ok & type(PropertyValue)==10
189            if isnum(PropertyValue)
190                PropertyValue =evstr(PropertyValue)   // literal fontsize, like "3"
191            else
192                ok = %f
193            end
194        end
195        ok = ok & PropertyValue >= 0;
196        if ~ok
197            warning(msprintf(gettext("%s: Argument #%d: Positive scalar number expected.\n"), fname, arginPos+1));
198            ResetFigureDDM(current_figure, cur_draw_mode);
199            return;
200        end
201        titlelabel.font_size = PropertyValue
202        if PropertyValue~=int(PropertyValue)
203            titlelabel.fractional_font = "on"
204        else
205            titlelabel.fractional_font = "off"
206        end
207        return
208
209        /////////////////////////
210    elseif  PName == "font_angle"  // <=> rotation
211        /////////////////////////
212        ok =  size(PropertyValue,"*")==1 & or(type(PropertyValue)==[1 10]);
213        if ok & type(PropertyValue)==10
214            if isnum(PropertyValue)
215                PropertyValue =evstr(PropertyValue)   // literal angle, like "30"
216            else
217                ok = %f;
218            end
219        end
220        if ~ok
221            warning(msprintf(gettext("%s: Argument #%d: Scalar number expected.\n"), fname, arginPos+1));
222            ResetFigureDDM(current_figure, cur_draw_mode);
223            return;
224        end
225        titlelabel.auto_rotation = "off"
226        newPropertyValue = modulo(PropertyValue,360)
227        titlelabel.font_angle = 360 - newPropertyValue
228        return
229
230        /////////////////////////
231    elseif or(PName==["font_foreground" "foreground" "background"])
232        /////////////////////////
233        //  "font_foreground"  <=>  color
234        //  "foreground"       <=>  edgecolor, foregroundcolor
235        //  "background"       <=>  backgroundcolor
236        index = []
237        ok = %f
238        if size(PropertyValue,"*")==1
239            if type(PropertyValue)==10 & isnum(PropertyValue) // literal index like "14"
240                PropertyValue = evstr(PropertyValue)
241            end
242        end
243        colors = iscolor(PropertyValue);
244        ok = size(colors,1)==1 & ~isnan(colors(1))
245        if ok
246            if size(colors,2)==3    // RGB
247                index = addcolor(colors)
248            else
249                index = colors
250            end
251        else
252            if type(PropertyValue)==10 & size(PropertyValue,"*")==1 & ..
253                grep("none", "/^"+PropertyValue+"/", "r") <> []
254                // 'none' selected
255                titlelabel.color_mode = 0; // <=> - colormap(1) and not black at all!!
256                return
257            end
258        end
259        if ~ok
260            msg = gettext("%s: Argument #%d: Wrong color specification.\n");
261            warning(msprintf(msg, fname, arginPos+1));
262            ResetFigureDDM(current_figure, cur_draw_mode);
263            return
264        end
265        if or(PName==["foreground" "background"])
266            titlelabel.fill_mode = "on"
267        end
268        titlelabel(PName) = index;
269
270        /////////////////////////
271    elseif  PName == "text"        // <=> string
272        /////////////////////////
273        if (type(PropertyValue) == 10)
274            titlelabel.text = PropertyValue;
275            return
276        else
277            warning(msprintf(_("%s: Wrong type for input argument #%d: string expected.\n"), fname, arginPos+1));
278            ResetFigureDDM(current_figure, cur_draw_mode);
279            return;
280        end
281
282        /////////////////////////
283    elseif  PName == "position"        // <=> position, à completer: la position par rapport à z n'est pas pris en compte, de plus le vecteur position doit contenir des coordonnées utilisateur (coordonnées selon l'axe)
284        /////////////////////////
285
286        if type(PropertyValue) == 1 & size(PropertyValue,"*")<=3
287            titlelabel.auto_position = "off";
288            titlelabel.position = PropertyValue ;
289            return
290        else
291            warning(msprintf(gettext("%s: Wrong size or type for input argument #%d: A real matrix expected.\n"), fname, arginPos+1));
292            ResetFigureDDM(current_figure, cur_draw_mode);
293            return;
294        end
295
296        /////////////////////////
297    elseif  or(PName == ["visible" "fill_mode"])    // <=> visible | box
298        /////////////////////////
299
300        if type(PropertyValue) == 10
301            if or(PropertyValue == ["off" "of"])
302                titlelabel(PName) = "off";
303                return
304            elseif PropertyValue == "on"
305                titlelabel(PName) = "on";
306                return
307            end
308        end
309        msg = gettext("%s: Argument #%d: ''on'' or ''off'' expected.\n")
310        warning(msprintf(msg, fname, arginPos+1));
311        ResetFigureDDM(current_figure, cur_draw_mode);
312
313
314        /////////////////////////
315    elseif  PName == "tag"     //
316        /////////////////////////
317        if type(PropertyValue) == 10
318            titlelabel.tag = PropertyValue;
319        else
320            msg = gettext("%s: Argument #%d: Text expected.\n")
321            warning(msprintf(msg, fname, arginPos+1));
322            ResetFigureDDM(current_figure, cur_draw_mode);
323        end
324
325        /////////////////////////
326    elseif PName == "font_style"     // <=> fontangle
327        /////////////////////////
328        fonts = ["courrier" "symbol" "times" "timesitalic" "timesbold" ..
329        "timesbolditalic" "helvetica" "helveticaitalic" "helveticabold"..
330        "helveticabolditalic" ]
331        if typeof(PropertyValue) == "string" & isnum(PropertyValue)
332            PropertyValue = evstr(PropertyValue)    // Literal id, like "3"
333        end
334        if typeof(PropertyValue) == "string"
335            PropertyValue = strsubst(convstr(PropertyValue,"l"), " ", "");
336            if or(PropertyValue == fonts)
337                titlelabel.font_style = find(fonts==PropertyValue)-1;
338            else
339                warning(msprintf(_("%s: Wrong value for input argument #%d: At least one ""%s"" expected.\n"), fname, arginPos+1, "font"));
340                ResetFigureDDM(current_figure, cur_draw_mode);
341                return;
342            end
343        elseif typeof(PropertyValue) == "constant" & or(PropertyValue==(0:9))
344            titlelabel.font_style = PropertyValue;
345        else
346            warning(msprintf(_("%s: Wrong type for input argument #%d: A real matrix or a string matrix expected.\n"), fname, arginPos+1));
347            ResetFigureDDM(current_figure, cur_draw_mode);
348            return;
349        end
350    end
351
352endfunction
353
354