1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) INRIA
3// Copyright (C) 2012 - 2016 - Scilab Enterprises
4//
5// This file is hereby licensed under the terms of the GNU GPL v2.0,
6// pursuant to article 5.3.4 of the CeCILL v.2.1.
7// This file was originally licensed under the terms of the CeCILL v2.1,
8// and continues to be available under such terms.
9// For more information, see the COPYING file which you should have received
10// along with this program.
11
12function ged_insert(k,win)
13
14    [lhs,rhs]=argn(0);
15
16    if rhs<2,
17        error(msprintf(gettext("%s: Wrong number of input argument(s): At least %d expected.\n"), "ged_insert", 2));
18    end
19
20    global active men
21
22    //check if user changes is mind
23    active=active+1;men=k
24    if  active>1 then return,end
25
26    ged_current_figure = gcf().figure_id
27    scf(win)
28
29    scf(win);
30    ged_cur_fig_handle=gcf();
31
32    mess1 = "Press the right mouse button (during a while) to stop the line creation";
33    mess2 = "Press any mouse button to complete the object drawing"
34    entities=["Rectangle","Segment","Polyline","Arrow","Double Arrow", ...
35    "Text","Circle"]
36
37    default_axes = gca(); // get the default axes where we start
38    f=gcf();
39    job="annotation"
40    if job=="annotation" then axes = get_insertion_axes(job,f),end
41
42    [k,xc,yc]=ged_click()
43
44    drawlater();
45    default_axes = gca(); // get the default axes where we start
46    rep(3)=-1
47    select k
48    case -1000 then //window has been closed
49        ged_insert_end(),return
50    case 1 then //Single Line
51        axes = get_insertion_axes(job,f,default_axes,xc,yc)
52        [xc,yc] = xchange(xc,yc,"f2i"); // I pass to pixel
53        sca(axes);  // I change axes and therefore change the scale
54        [xc,yc] = xchange(xc,yc,"i2f"); // in the new scale I recompute the corresponding pixel values
55        xpoly([xc;xc],[yc;yc])
56        drawnow();
57        r=gce();r.foreground=-1;
58        r.clip_state="off";
59        gcf().info_message = mess2;
60        while rep(3)==-1 do
61            rep=xgetmouse(0,[%t %t])
62            r.data=[xc,yc;rep(1),rep(2)]
63            gcf().info_message = mess2;
64        end
65    case 2 then //Polyline (stroken line)
66        axes = get_insertion_axes(job,f,default_axes,xc,yc)
67        [xc,yc] = xchange(xc,yc,"f2i");
68        sca(axes);
69        [xc,yc] = xchange(xc,yc,"i2f");
70        xpoly([xc;xc],[yc;yc])
71        drawnow();
72        r=gce();r.foreground=-1;
73        r.data(:,3)=0.;
74        r.clip_state="off";
75        gcf().info_message = mess1;
76        while %t
77            rep(3)=-1;
78            while rep(3)==-1 do
79                rep=xgetmouse(0)
80                r.data($,:)= [rep(1),rep(2),0]
81                gcf().info_message = mess1;
82            end
83            if or(rep(3)==[2 5 -1000]) then break,end
84            r.data=[r.data;r.data($,:)]
85        end
86    case 3 // Arrow (single arrow)
87        axes = get_insertion_axes(job,f,default_axes,xc,yc)
88        [xc,yc] = xchange(xc,yc,"f2i");
89        sca(axes);
90        [xc,yc] = xchange(xc,yc,"i2f");
91        xpoly([xc;xc],[yc;yc])
92        drawnow();
93        r=gce();r.foreground=-1;
94        r.data(:,3)=0.;
95        r.polyline_style = 4;
96        r.arrow_size_factor=10; // change the factor to have a nice arrow
97        r.clip_state="off";
98        gcf().info_message = mess2;
99        yc = [];
100        while rep(3)==-1 do
101            rep=xgetmouse(0,[%t %t])
102            r.data($,:)= [rep(1),rep(2),0]
103            gcf().info_message = mess2;
104        end
105        //   case 4 // Double Arrow
106        //      axes = get_insertion_axes(job,f,default_axes,xc,yc)
107        //     [xc,yc] = xchange(xc,yc,'f2i');
108        //     sca(axes);
109        //     [xc,yc] = xchange(xc,yc,'i2f');
110        //     xpoly([xc;xc],[yc;yc])
111        //     r1=gce();r.foreground=-1;
112        //     r1.data(:,3)=0.;
113        //     r1.polyline_style = 4;
114        //     r1.arrow_size_factor=5; // change the factor to have a nice arrow
115        //     r1.clip_state='off';
116        //
117        //     // second polyline (for the seconf arrow from end to origin)
118        //     xpoly([xc;xc],[yc;yc])
119        //     //xpoly([r.data(2,1);r.data(1,1)],[r.data(2,2);r.data(1,2)])
120        //     r2=gce();r.foreground=-1;
121        //     r2.data(:,3)=0.;
122        //     r2.polyline_style = 4;
123        //     r2.arrow_size_factor=5; // change the factor to have a nice arrow
124        //     r2.clip_state='off';
125        //
126        //     gcf().info_message = mess2;
127        //     while rep(3)==-1 do
128        //       rep=xgetmouse(0,[%t %t])
129        //       drawlater();
130        //       r1.data($,:)= [rep(1),rep(2),0]
131        //       r2.data(1,:)= [rep(1),rep(2),0]
132        //       drawnow();
133        //       gcf().info_message = mess2;
134        //     end
135
136    case 4 then //Text
137        // open a dialog to enter the text
138        text = x_dialog("Enter the new text here.","") ;
139        if ( text <> [] & text <> "" ) then
140            // do nothing if cancel button has been pressed or no text entered
141            // get the position of the text
142            xstring( xc, yc, text ) ;
143        end
144        drawnow();
145    case 5 then //Rectangle
146        axes = get_insertion_axes(job,f,default_axes,xc,yc)
147        [xc,yc] = xchange(xc,yc,"f2i");
148        sca(axes);
149        [xc,yc] = xchange(xc,yc,"i2f");
150        xrect(xc,yc,0,0)
151        drawnow();
152        r=gce();r.foreground=-1;
153        r.clip_state="off";
154        gcf().info_message = mess2;
155        while rep(3)==-1 do
156            rep=xgetmouse(0,[%t %t])
157            r.data=[min(xc,rep(1)),max(yc,rep(2)),abs(xc-rep(1)),abs(yc-rep(2))]
158            gcf().info_message = mess2;
159        end
160    case 6 then //Circle
161        axes = get_insertion_axes(job,f,default_axes,xc,yc)
162        [xc,yc] = xchange(xc,yc,"f2i");
163        sca(axes);
164        [xc,yc] = xchange(xc,yc,"i2f");
165        xarc(xc,yc,0,0,0,64*360)
166        drawnow();
167        r=gce();r.foreground=-1;
168        r.clip_state="off";
169        gcf().info_message = mess2;
170        while rep(3)==-1 do
171            rep=xgetmouse(0,[%t %t])
172            r.data=[min(xc,rep(1)),max(yc,rep(2)),abs(xc-rep(1)),abs(yc-rep(2)),0,64*360]
173            gcf().info_message = mess2;
174        end
175    end
176    ged_insert_end()
177endfunction
178
179
180
181function axes = get_insertion_axes(job,f,default_axes,xc,yc)
182    if job=="annotation" then //special axes used
183        axes=f.children(1)
184        if axes.user_data=="annotation" then return,end
185        //  create a new specialized axes at left bottom of the window
186        //it should be better if the annotation axes be directly attached to
187        //the figure (to be done)
188        axes=newaxes();
189        axes.user_data="annotation"
190        axes.axes_bounds=[0,1,0.15,0.15];
191        axes.margins=[0 0 0 0];
192        axes.axes_visible = ["off","off","off"]
193        axes.background=f.background;
194    else //get the axes containing the point xc,yc
195        //the point  xc,yc is given in user coordinate relative to the
196        //default_axes axes entity.
197        nb_axes = size(f.children,"*")
198        // for now I consider that all children of a figure are of type Axes
199        axes_size = f.axes_size // given in pixels
200        axes_size = [axes_size axes_size];
201        [x,y]=xchange(xc,yc,"f2i")
202        found=%f
203        for i=1:nb_axes
204            axes = f.children(i);
205            cur_axes_bounds = axes.axes_bounds;
206            rect = cur_axes_bounds.*axes_size; // rectangle in pixels (margins inside)
207            rect(3) = rect(3) + rect(1);
208            rect(4) = rect(4) + rect(2);
209            if (x>rect(1) & x<rect(3) & y>rect(2) & y<rect(4)) then  found=%t,break,end
210        end
211        if ~found then axes=[],end
212    end
213endfunction
214
215function  ged_insert_end()
216    if or(win==winsid()) then //  the window still exists
217        sca(default_axes); // resume the default axes
218        f=gcf()
219        clearglobal active men
220    end
221    if win <> ged_current_figure then
222        scf(ged_current_figure)
223    end
224
225endfunction
226function [men,xc,yc]=ged_click()
227    global  men
228    [btn,xc,yc]=xclick()
229    if btn==-1000 then men=-1000,end
230    for k=1:3,end
231    job=men
232endfunction
233
234