Clean-up (debug features)
[scilab.git] / scilab / modules / graphics / macros / getfont.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA
3 // This file must be used under the terms of the CeCILL.
4 // This source file is licensed as described in the file COPYING, which
5 // you should have received as part of this distribution.  The terms
6 // are also available at
7 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
8
9
10 function [fontId,fontSize]=getfont(S,v1)
11
12     //returns the clicked font id and font size.
13     fontId=[];fontSize=[];
14
15     warnobsolete("ged", "5.5.0");
16
17     if exists("S","local")==0 then S="a";end
18     if type(S)<>10 then
19         error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"), "getfont", 1));
20     end
21     S=part(S(1),1)
22
23     win=max(winsid()+1);
24     f=scf(win);
25
26     //fnts=xlfont();nf=size(find(fnts<>''),'*')-1
27     fontnames=["Courrier";"Symbol";"Times";"Times Italic";
28     "Times Bold";"Times Bold Italic";
29     "Helvetica";"Helvetica Italic";
30     "Helvetica Bold";"Helvetica Bold Italic"];
31
32     nf=size(fontnames,"*")
33
34     font_sz=[0.5 1:6];
35     nz=size(font_sz,"*"); //number of font sizes
36     //Disabling standard menus
37
38     addmenu(win,"Ok",list(2,"menu_ok"));
39     addmenu(win,"Cancel",list(2,"menu_cancel"));
40     delmenu(win,"&?")
41     delmenu(win,_("&Edit"))
42     delmenu(win,_("&Tools"))
43     delmenu(win,_("&File"))
44     toolbar(win,"off")
45     x=2;
46     red=addcolor([1 0 0]);
47     W1=6 //first column width
48     H1=2 // first row height
49     H=3.5;W=2.5;
50
51     Hdl=drawchart();
52
53     done=%f;
54
55     deff("menu_ok(k,gwin)","done=%t;k=[-fontId,fontSize];")
56     deff("menu_cancel(k,gwin)","done=%t;k=[];")
57
58     if exists("font","local") then
59         fontId=max(0,min(nf,font(1)))
60         fontSize=max(0,min(5,font(2)))
61         selected=fontId+nf*fontSize+1;
62         t=Hdl(selected); t.foreground=red;
63     else
64         selected=0
65     end
66
67     while %t
68
69         [c_i,cx,cy,cw,str]=xclick();
70
71         if c_i==-2 then
72             if strindex(str,"menu_ok")==1 then xdel(win);break,end
73             if strindex(str,"menu_cancel")==1 then xdel(win);break,end
74
75         elseif c_i==-1000 then
76             fontId=[];
77             fontSize=[];
78             break,
79
80         elseif c_i>=0 & c_i<=12 then
81             if selected<>0 then
82                 t=Hdl(selected)
83                 t.foreground=-1;
84             end
85
86             fontId=max(0,min(nf,floor((cy)/H)));
87             fontSize=max(0,min(nz,floor((cx-W1)/W)));
88             selected=fontId+nf*fontSize+1;
89             t=Hdl(selected);t.foreground=red;
90
91             xinfo("You have chosen fontId = "+string(fontId)+" , fontSize =  "+string(fontSize))
92
93         elseif c_i>=32 & c_i<1032 then
94             S=ascii(c_i)
95             drawlater();Hdl.text=S;drawnow()
96         elseif c_i>=1032
97             S=ascii(c_i-1000)
98             drawlater();Hdl.text=S;;drawnow()
99         end
100     end
101
102     if strindex(str,"menu_cancel")==1 then
103         fontId=[];
104         fontSize=[];
105     end
106
107     if argn(1)<2 then
108         fontId=[fontId,fontSize];
109     end
110
111 endfunction
112
113 function Hdl=drawchart()
114     f=gcf();
115     a=gca();
116     drawlater()
117     a.margins([1:2 4])=0.1
118     a.data_bounds=[0 0;W1+nz*W H1+(nf)*H]
119     a.title.text=["Click to select font Id  and font size";
120     "or press a key to select a character"];
121
122     // Drawing first column
123     for k=0:nf-1
124         xstringb(0,H*k,fontnames(k+1)+" ("+string(k)+")",W1,H)
125         xrect(0, H*(k+1), W1, H);
126     end
127
128     // Drawing first row
129     for l=0:nz-1
130         xstringb(W1+l*W,(nf)*H ,string(font_sz(l+1)),W,H1)
131         xrect(W1+l*W, H1+(nf)*H, W, H1);
132     end
133
134     // drawing top left corner
135     xrect(0,H1+(nf)*H,W1,H1);
136     xstringb(0, (nf)*H,"Id (index)\Size",W1,H1)
137
138     Hdl=[]
139     for k=-(0:nf-1)
140         Hdll=[]
141         for x=1:nz
142             xstringb(W1+(x-1)*W,-H*k,S,W,H)
143             t=gce();t.font_size=font_sz(x);t.font_style=-k
144             Hdll=[Hdll t]
145         end
146         Hdl=[Hdl;Hdll]
147     end
148
149     glue(Hdl)
150     drawnow()
151
152 endfunction