a01b02a918568b21ecdcea51e35f215152fe7890
[scilab.git] / scilab / modules / graphics / src / c / Actions.c
1 /*------------------------------------------------------------------------
2  *    Graphic library
3  *    Copyright (C) 1998-2001 Enpc/Jean-Philippe Chancelier
4  *    jpc@cermics.enpc.fr 
5  --------------------------------------------------------------------------*/
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "math_graphics.h"
11 #include "Graphics.h" 
12 #include "GetProperty.h"
13 #include "SetProperty.h"
14 #include "DrawObjects.h"
15 #include "Xcall1.h"
16 #include "WindowList.h"
17 #include "MALLOC.h"
18
19 extern int xinitxend_flag;
20
21 /********************************************************
22  * the functions in this file are called from 
23  * callback ( see jpc_SGraph.c ) for the XWindow version 
24  * Nov 1998 : we must be sure that during the evaluation of 
25  *            scig_xxx an other function scig_yyy won't be 
26  *            run. This is possible since during the execution of 
27  *            one scig_xxx function a sciprint can be performed 
28  *            and it will lead to an event check which can 
29  *            produce a call to an other scig_yyy function 
30  *            flag scig_buzy  is used to check for that 
31  *            
32  ********************************************************/
33 extern int sciSwitchWindow  __PARAMS((int *winnum));/* NG */
34 extern void sciGetIdFigure __PARAMS((int *vect, int *id, int *iflag));/* NG */
35 extern int C2F(syncexec)(char *str, int *ns, int *ierr, int *seq, long int str_len) ;
36 #if !defined(_MSC_VER)
37 extern int WithBackingStore();
38 #endif
39 static int scig_buzy = 0;
40
41 void reset_scig_handler(void);
42 int scig_handler_none(int win_num);
43
44 /********************************************************
45  * A handler which can be dynamically set to custiomize 
46  * action of scig_xxx functions 
47  ********************************************************/
48
49 int scig_handler_none(int win_num) {
50         return win_num;
51 };
52
53 Scig_handler scig_handler = scig_handler_none;
54
55 Scig_handler set_scig_handler(Scig_handler f)
56 {
57   Scig_handler old = scig_handler;
58   scig_handler = f;
59   return old;
60 }
61
62 void reset_scig_handler(void)
63 {
64   scig_handler = scig_handler_none;
65 }
66
67 /********************************************************
68  * Basic Replay : redraw recorded graphics 
69  ********************************************************/
70
71 void scig_replay(integer win_num)
72 {
73   /* Modification Allan CORNET Mai 2004 */
74   integer verb=0,cur,pix,na,backing;
75   char name[4];
76   if ( scig_buzy  == 1 ) return ;
77   scig_buzy =1;
78   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
79   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
80   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
81   C2F(dr)("xget","pixmap",&verb,&pix,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
82
83 #if defined(_MSC_VER)
84   backing = 0;
85 #else
86   backing = WithBackingStore();
87 #endif
88   if (backing) 
89     {
90       C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);    
91     }
92   else 
93     {
94       if (pix == 0) 
95         {
96           if ( (GetDriver()) != 'R') C2F(SetDriver)("Rec",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
97           C2F(dr)("xclear","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
98           sciRedrawFigure();
99           C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
100         }
101       else
102         {
103           C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);    
104         }
105     }
106
107   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
108   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
109   scig_buzy = 0;
110 }
111
112
113 /********************************************************
114  * Basic Replay : expose graphics i.e 
115  * if we have a pixmap we can perform a wshow 
116  * else we perform a sgig_replay 
117  ********************************************************/
118
119 void scig_expose(integer win_num)
120 {
121   integer verb=0,cur,pix,na,backing;
122   char name[4];
123   if ( scig_buzy  == 1 ) return ;
124   scig_buzy =1;
125   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
126   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
127   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
128   C2F(dr)("xget","pixmap",&verb,&pix,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
129
130 #if defined(_MSC_VER)
131   backing = 0;
132 #else
133   backing = WithBackingStore();
134 #endif
135   if (backing) 
136     {
137       /* only used whith X11 were pixmap mode can be used for backing store 
138        * we are here in a case where the pixmap is used for backing store 
139        */
140       C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);    
141     }
142   else 
143     {
144       if (pix == 0) 
145         {
146           if ( (GetDriver()) != 'R') 
147             C2F(SetDriver)("Rec",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
148           C2F(dr)("xclear","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
149           sciRedrawFigure();
150           C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
151         }
152       else
153         {
154           C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);    
155         }
156     }
157   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
158   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
159   scig_buzy = 0;
160 }
161
162 /********************************************************
163  * Redraw graphic window win_num  after resizing 
164  ********************************************************/
165
166 void scig_resize(integer win_num)
167 {
168   integer verb=0,cur,na,pix,backing;
169   char name[4];
170   if ( scig_buzy  == 1 ) return ;
171   scig_buzy =1;
172   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
173   if ( (GetDriver()) !='R') 
174     C2F(SetDriver)("Rec",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
175   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
176   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
177   C2F(dr)("xget","pixmap",&verb,&pix,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
178   CPixmapResize1();
179   C2F(dr)("xclear","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);    
180 #if defined(_MSC_VER)
181   backing = 0;
182 #else
183   backing = WithBackingStore();
184 #endif
185   sciRedrawFigure();
186   if (backing && pix!=1 ) C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
187
188   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
189   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
190   scig_buzy = 0;
191 }
192
193 /********************************************************
194  * Just resize a pixmap (win95 only)
195  ********************************************************/
196
197 void scig_resize_pixmap(integer win_num)
198 {
199   integer verb=0,cur,na;
200   char name[4];
201   if ( scig_buzy  == 1 ) return ;
202   scig_buzy =1;
203   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
204   C2F(SetDriver)("Int",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
205   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
206   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
207   CPixmapResize1();
208   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
209   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
210   scig_buzy = 0;
211 }
212
213
214 /********************************************************
215  * clear window 
216  ********************************************************/
217
218 void  scig_erase(integer win_num)
219 {
220   integer verb=0,cur,na;
221   char name[4];
222   if ( scig_buzy  == 1 ) return ;
223   scig_buzy =1;
224   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
225   if ( (GetDriver()) !='R') 
226     C2F(SetDriver)("Rec",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
227   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
228   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
229   sciXbasc() ;
230   C2F(dr)("xclear","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
231   C2F(dr)("xstart","v",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
232   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
233   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
234   scig_buzy = 0;
235 }
236
237
238
239 /*******************************************************
240  * 2D Zoom 
241  ******************************************************/
242
243 int scig_2dzoom(integer win_num)
244 {
245   char name[4];
246   int ret;
247   integer verb=0,cur,na;
248
249   if ( scig_buzy  == 1 ) return 0; ;
250   scig_buzy =1;
251   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
252
253   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
254   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
255   ret=zoom();
256   if (cur != win_num)
257           C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
258   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
259
260   scig_buzy = 0;
261   return ret;
262 }
263
264
265 /*******************************************************
266  * Unzoom function 
267  ******************************************************/
268
269 void   scig_unzoom(integer win_num)
270 {
271   integer verb=0,cur,na;
272   char name[4];
273   if ( scig_buzy  == 1 ) return ;
274   scig_buzy =1;
275   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
276   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
277   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
278   unzoom();
279   if (cur != win_num)
280         C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
281   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
282   scig_buzy = 0;
283 }
284
285 /*******************************************************
286  * 3d rotation function 
287  ******************************************************/
288
289 int scig_3drot(integer win_num)
290 {
291   integer verb=0,cur,na,ret;
292   char name[4];
293   if ( scig_buzy  == 1 ) return 0;
294   scig_buzy =1;
295   GetDriver1(name,PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0);
296
297   C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);  
298   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
299   ret=I3dRotation();
300   if (cur != win_num)
301         C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
302   C2F(dr)("xsetdr",name, PI0, PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
303   scig_buzy = 0;
304   return ret;
305 }
306
307 /********************************************************
308  * graphic Window selection 
309  ********************************************************/
310
311 void scig_sel(integer win_num)
312 {
313   char c ;
314   int v=1;
315   if ((c=GetDriver())=='R' || c == 'X' || c == 'W')
316     {
317       C2F(dr)("xset","window",&win_num,&v,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
318       sciSwitchWindow(&win_num) ;
319     }
320 }
321
322 /********************************************************
323  * graphic Window raise 
324  ********************************************************/
325
326 void scig_raise(integer win_num)
327 {
328  
329   int cur,n,na,verb=0,iflag=0;
330
331   sciGetIdFigure (PI0,&n,&iflag);
332   if ( n > 0 )
333   {
334     C2F(dr)("xget","window",&verb,&cur,&na,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
335      if (win_num != cur) 
336      {
337        C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
338        sciSwitchWindow(&win_num);
339        C2F(dr)("xselect","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
340        C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
341      }
342      else
343      {
344        C2F(dr)("xselect","v",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
345      }
346   }
347   else
348   { 
349     C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
350     sciSwitchWindow(&win_num);
351   }
352 }
353
354
355 /********************************************************
356  * Reload a saved graphic
357  ********************************************************/
358
359 void scig_loadsg(int win_num, char *filename)
360 {
361   integer verb = 0 ;
362   int cur = 0 ;
363   int narg = 0 ;
364   int ierr = 0 ;
365   int seq = 1 ;
366   char * macroCall = NULL ;
367   /* the sting is "xload('(1)')" where (1) is filemame */
368   /* Consequently we have 9 fixed character and two variable strings. */
369   int macroCallLength = 9 + strlen(filename) ;
370
371   if ( scig_buzy  == 1 ) { return ; }
372   scig_buzy  = 1 ;
373
374   C2F(dr)("xget","window",&verb,&cur,&narg,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
375   C2F(dr)("xset","window",&win_num,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
376   
377   macroCall = MALLOC( (macroCallLength+1) * sizeof(char) ) ; /* +1 for the \0 terminating character */
378   sprintf(macroCall,"xload('%s')",filename);
379
380   C2F(syncexec)(macroCall,&macroCallLength,&ierr,&seq, macroCallLength );
381
382   FREE( macroCall ) ;
383   if(ierr != 0) { sciprint("Wrong plot file : %s\r\n",filename) ; }
384
385   C2F(dr)("xset","window",&cur,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);
386   scig_buzy = 0;
387 }
388
389 void scig_savesg( int win_num, char * filename )
390 {
391   integer ierr ;
392   integer seq = 1 ;
393   char * macroCall = NULL ;
394
395   /* the sting is "xsave('(1)',(2))" where (1) is filemame and (2) the string */
396   /* corresponding of win_num. Consequently we have 10 fixed character and two variable strings. */
397   int macroCallLength = 10 + strlen(filename) + GET_NB_DIGITS(win_num) ;
398
399   macroCall = MALLOC( (macroCallLength+1) * sizeof(char) ) ; /* +1 for the \0 terminating character */
400
401   sprintf( macroCall, "xsave('%s',%d)", filename, win_num ) ; /* call xsave macro */
402   C2F(syncexec)(macroCall,&macroCallLength,&ierr,&seq, macroCallLength );
403   FREE( macroCall ) ;
404 }