* bug 8630 fixed - Scopes crashed Scilab on simulation when they had the same
[scilab.git] / scilab / modules / scicos_blocks / src / c / scoWindowScope.c
1 /*  Scicos
2 *
3 *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * See the file ./license.txt
20 */
21 /*--------------------------------------------------------------------------*/ 
22 /**
23 \file scoWindowScope.c
24 \author Benoit Bayol
25 \version 1.0
26 \date September 2006 - January 2007
27 \brief Source Code of all functions wich interact with the window like creation of graphical object or refreshing the window
28 */
29 /*--------------------------------------------------------------------------*/ 
30 #include <stdio.h>
31 #include <string.h>
32 #include "scoBase.h"
33 #include "scoWindowScope.h"
34 #include "scoMemoryScope.h"
35 #include "scicos.h"
36 #include "scoMisc.h"
37 #include "scoGetProperty.h"
38 #include "scoSetProperty.h"
39 #include "SetProperty.h"
40 #include "BuildObjects.h"
41 #include "scicos_malloc.h"
42 #include "scicos_free.h"
43 #include "sciprint.h"
44 #include "WindowList.h"
45 #include "DrawingBridge.h"
46 #include "CurrentObjectsManagement.h"
47 #include "ObjectSelection.h"
48 #include "HandleManagement.h" /* sciGetHandle */
49 #include "scicos_block4.h"
50 #include "localization.h"
51 /*--------------------------------------------------------------------------*/ 
52 extern int C2F(dcopy)();
53 /*--------------------------------------------------------------------------*/ 
54 static void scoSetWindowIDInUserData(ScopeMemory * pScopeMemory,int block_number)
55 {
56         /* 15/07/2009, S. Steer: user data set to a Scilab data structure instead of only the block_number value  */
57         double *ptr_d=NULL;
58         int **user_data_ptr=NULL,*size_ptr=NULL;
59
60         scoGraphicalObject pTemp = scoGetPointerScopeWindow(pScopeMemory);
61         sciGetPointerToUserData (pTemp,&user_data_ptr, &size_ptr);
62         FREE(*user_data_ptr);
63         if ((*user_data_ptr=(int *)MALLOC(6*sizeof(int))) == NULL) {
64                 scoScopeError(pScopeMemory,1);
65                 return;
66         }
67         /* one double to scilab stack */
68         *(*user_data_ptr)   = 1;
69         *(*user_data_ptr+1) = 1;
70         *(*user_data_ptr+2) = 1;
71         *(*user_data_ptr+3) = 0;
72         ptr_d  = (double *) (*user_data_ptr+4);
73         *ptr_d = (double) block_number;
74         *size_ptr = 6;
75 }
76 /*--------------------------------------------------------------------------*/ 
77 scoInteger scoGetUserData(scoGraphicalObject pTemp)
78 {
79         /* 15/07/2009, S. Steer: to take into account the scoSetWindowIDInUserData change */
80         double *ptr_d=NULL;
81         int **user_data_ptr=NULL,*size_ptr=NULL;
82         sciGetPointerToUserData (pTemp,&user_data_ptr, &size_ptr);
83         ptr_d = (double *) (*user_data_ptr+4);
84         return (scoInteger) *ptr_d;
85 }
86 /*--------------------------------------------------------------------------*/ 
87 void scoInitOfWindow(ScopeMemory * pScopeMemory, int dimension, int win_id, int * win_pos, int * win_dim, double * xmin, double * xmax, double * ymin, double * ymax, double * zmin, double * zmax)
88 {
89         int i = 0;
90         int block_number = get_block_number();
91         int user_data = -1;
92         scoGraphicalObject pTemp;
93         scoGraphicalObject pTemp2;
94         //if win-id is -1 we give an auto number based on the block_number on the diagram
95         if (win_id == -1)
96         {
97                 win_id = 20000 + block_number; 
98         }
99         //if we restart the simulation this command delete all previous children of the window
100         /* WARNING HERE */
101         /* warning C4047: '=' : 'scoGraphicalObject' differs in levels of indirection from 'BOOL' */
102         if ((sciIsExistingFigure(win_id)))
103         {
104                 pTemp = getFigureFromIndex(win_id);
105                 if (pFIGURE_FEATURE(pTemp)->user_data != NULL)
106                 {
107                         user_data = scoGetUserData(pTemp);
108                 }
109
110                 sciClearFigure(pTemp); //** if already present is cleared 
111
112         }
113         /*if ((user_data == -1 ) || (user_data == win_id))*/
114         if ((user_data == -1 ) || (user_data == block_number))
115         {
116                 scoSetWindowID(pScopeMemory,win_id);
117
118                 scoSetScopeActivation(pScopeMemory,1); //Activate It ! Let's Rock !
119                 //Dont forget this kind of command
120                 sciSetUsedWindow(scoGetWindowID(pScopeMemory));
121                 pTemp = sciGetCurrentFigure();
122                 scoSetHandleScopeWindow(pScopeMemory,sciGetHandle(pTemp));
123                 scoSetWindowIDInUserData(pScopeMemory,block_number);
124                 for (i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
125                 {
126                         if (i == 0)
127                         {
128                                 //We are getting the Handle of the current axes but in the same time we are constructing it (see below)
129                                 //Here pTemp is the pointer on the ScopeWindow
130                                 // J-B please check this 
131                                 scoSetHandleAxes(pScopeMemory,i,sciGetHandle(sciGetFirstTypedSelectedSon( pTemp, SCI_SUBWIN )) );
132                         }
133                         else
134                         {
135                                 //Here pTemp is the pointer on the ScopeWindow
136                                 scoSetHandleAxes(pScopeMemory,i,sciGetHandle(ConstructSubWin(pTemp)));
137                         }
138                         //Here pTemp2 is the pointer on the current Axes
139                         pTemp2 = scoGetPointerAxes(pScopeMemory,i);
140                         sciInitFontSize(pTemp2, 1); //** axes font size (numbers)
141
142                         //** sciSetIsBoxed(pTemp2,TRUE); //** obsolete in Scilab 5
143                         sciSetBoxType(pTemp2,BT_ON);
144
145                         //Here we don't want "smart" limits
146                         pSUBWIN_FEATURE(pTemp2)->tight_limits = TRUE;
147                         //Here PTemp2 is the pointer on the current Subwint
148                         //WRect is for position of Axes in the window
149                         pSUBWIN_FEATURE(pTemp2)->WRect[0] = 0;
150                         pSUBWIN_FEATURE(pTemp2)->WRect[1] = (double)i/scoGetNumberOfSubwin(pScopeMemory);
151                         pSUBWIN_FEATURE(pTemp2)->WRect[2] = 1;
152                         pSUBWIN_FEATURE(pTemp2)->WRect[3] = (double)1/scoGetNumberOfSubwin(pScopeMemory);
153                         pSUBWIN_FEATURE(pTemp2)->axes.filled = FALSE;
154                         switch(dimension)
155                         {
156                         case 3:
157                                 pSUBWIN_FEATURE(pTemp2)->is3d = TRUE;
158                                 pSUBWIN_FEATURE(pTemp2)->axes.axes_visible[2] = TRUE;
159                                 //SRECT is here to give number of x,y, or z legends
160                                 pSUBWIN_FEATURE(pTemp2)->SRect[4] = zmin[i];
161                                 pSUBWIN_FEATURE(pTemp2)->SRect[5] = zmax[i];
162                         case 2:
163                                 pSUBWIN_FEATURE(pTemp2)->axes.axes_visible[1] = TRUE;
164                                 pSUBWIN_FEATURE(pTemp2)->SRect[2] = ymin[i];
165                                 pSUBWIN_FEATURE(pTemp2)->SRect[3] = ymax[i];
166                         case 1:
167                                 pSUBWIN_FEATURE(pTemp2)->axes.axes_visible[0] = TRUE;
168                                 pSUBWIN_FEATURE(pTemp2)->SRect[0] = xmin[i];
169                                 pSUBWIN_FEATURE(pTemp2)->SRect[1] = xmax[i];
170                                 break;
171                         default:
172                                 Coserror(_("Incorrect value for dimension argument."));
173                                 break;
174                         }
175                         forceRedraw(pTemp2);
176
177                 }
178                 if(win_pos != NULL)
179                 {
180                         if (win_pos[0] >= 0) {
181                                 sciInitScreenPosition(pTemp, win_pos[0], win_pos[1]);
182                         }
183                 }
184                 if(win_dim != NULL)
185                 {
186                         if (win_dim[0] >= 0) {
187                                 sciSetDimension(pTemp, win_dim[0], win_dim[1]);
188                         }
189                 }
190
191                 sciSetUsedWindow(scoGetWindowID(pScopeMemory));
192                 sciDrawObj(pTemp);
193         }
194         else
195         {
196                 Coserror(_("This scope has the same window id than another."));
197         }
198 }
199 /*--------------------------------------------------------------------------*/ 
200 void scoRefreshDataBoundsX(ScopeMemory * pScopeMemory, double t)
201 {
202         scoGraphicalObject pLongDraw = NULL;
203         scoGraphicalObject pShortDraw;
204         scoGraphicalObject pAxes;
205
206         int NbrPts = 0, current_period_counter = 0;
207         int i = 0,j = 0;
208         int c__1 =   1 ;
209         /* To know if one or more subwindows need to be redrawn */
210         BOOL needRedraw = FALSE;
211         double period = 0.;
212         /*Go on the window*/
213         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
214         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
215         {
216                 /*if we have to redraw the axis for x-axis*/
217                 if (scoGetNewDraw(pScopeMemory,i) < 0)
218                 {
219                         needRedraw = TRUE;
220                         pAxes = scoGetPointerAxes(pScopeMemory,i);
221                         period = scoGetPeriod(pScopeMemory,i);
222
223                         /*Calculating the current period counter and update the value of x bounds*/
224                         current_period_counter = (int)(t/period);
225                         pSUBWIN_FEATURE(pAxes)->SRect[0] = period*(current_period_counter);
226                         pSUBWIN_FEATURE(pAxes)->SRect[1] = period*(current_period_counter+1);
227                         /*forceRedraw(pAxes);*/
228                         /*Don't forget to save the new value - because we have activated the refresh we are going one step over*/
229                         scoSetPeriodCounter(pScopeMemory,i,current_period_counter);
230
231                         /*Instruction to reinitialize the ShortDraw and to copy the last points of the ShortDraw in the LongDraw to be sure to have continuity*/
232                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
233                         switch(sciGetEntityType(pShortDraw))
234                         {
235                                 /*Getting Number of Points in the ShortDraw
236                                 Then for each polyline in the axis :
237                                 -Reinitialize LongDraw
238                                 -Copy last points of the ShortDraw into the LongDraw
239                                 -Initialize LongDraw
240                                 -Reinitialize the ShortDraw
241                                 -copy last points of the last short into the new for continuity
242                                 -initialize shortdraw
243                                 */
244                         case SCI_POLYLINE:
245                                 {
246                                         NbrPts = pPOLYLINE_FEATURE(pShortDraw)->n1;
247                                         for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
248                                         {
249                                                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
250                                                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
251                                                 //We don't have to draw it because for instance there will be only one point and if it is a line it is not good
252                                                 /* was commented in scilab5 */
253                                                 //pPOLYLINE_FEATURE(pShortDraw)->visible = FALSE;
254
255                                                 pPOLYLINE_FEATURE(pLongDraw)->n1 = 0;
256                                                 /* We copy ShortDraw data in LongDraw  to ensure the continuity */
257                                                 C2F(dcopy)(&NbrPts,pPOLYLINE_FEATURE(pShortDraw)->pvx,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvx,&c__1);
258                                                 C2F(dcopy)(&NbrPts,pPOLYLINE_FEATURE(pShortDraw)->pvy,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvy,&c__1);
259                                                 pPOLYLINE_FEATURE(pLongDraw)->n1 = NbrPts;
260
261                                                 /* Reset the ShortDraw keeping the last point as new satrting point */
262                                                 pPOLYLINE_FEATURE(pShortDraw)->pvx[0] = pPOLYLINE_FEATURE(pLongDraw)->pvx[NbrPts-1];
263                                                 pPOLYLINE_FEATURE(pShortDraw)->pvy[0] = pPOLYLINE_FEATURE(pLongDraw)->pvy[NbrPts-1];
264                                                 pPOLYLINE_FEATURE(pShortDraw)->n1 = 1;
265                                         }
266                                         break;
267                                 }
268                         case SCI_SEGS:
269                                 {
270                                         NbrPts = pSEGS_FEATURE(pShortDraw)->Nbr1;
271                                         for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
272                                         {
273                                                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
274                                                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
275                                                 //pSEGS_FEATURE(pShortDraw)->visible = FALSE;
276
277                                                 pSEGS_FEATURE(pLongDraw)->Nbr1 = 0;
278                                                 pSEGS_FEATURE(pLongDraw)->Nbr2 = 0;
279                                                 C2F(dcopy)(&NbrPts,pSEGS_FEATURE(pShortDraw)->vx ,&c__1,pSEGS_FEATURE(pLongDraw)->vx,&c__1);
280                                                 C2F(dcopy)(&NbrPts,pSEGS_FEATURE(pShortDraw)->vy ,&c__1,pSEGS_FEATURE(pLongDraw)->vy,&c__1);
281                                                 pSEGS_FEATURE(pLongDraw)->Nbr1 = NbrPts;
282                                                 pSEGS_FEATURE(pLongDraw)->Nbr2 = NbrPts;
283                                         }
284                                         break;
285                                 }
286                         default:
287                                 Coserror(_("Cannot use %s with this type of object."), "scoRefreshDataBoundsX");
288                                 break;
289                         }
290                         /* Dont forget to reinit it */
291                         scoSetNewDraw(pScopeMemory,i,0);
292
293                         /* we have modified some thing it is that we need to redraw the subwin */
294                         forceRedraw(pShortDraw);
295                         forceRedraw(pLongDraw);
296                         forceRedraw(pAxes);
297                 }
298         }
299
300         if (needRedraw)
301         {
302
303                 sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
304
305
306         }
307
308
309         //Now that we have redrawn the window we can reactivate the shortdraw because there will be more than one point in the futur
310         //THIS PART CAN BE RECODED - NOT EFFICIENT
311         // as  previous pSEGS_FEATURE(pShortDraw)->visible = FALSE; have been commented out, the following code is not usefull
312         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
313         {
314                 for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
315                 {
316                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
317                         switch(sciGetEntityType(pShortDraw))
318                         {
319                         case SCI_POLYLINE:
320                                 {
321                                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
322                                         pPOLYLINE_FEATURE(pShortDraw)->visible = TRUE;
323                                         break;
324                                 }
325                         case SCI_SEGS:
326                                 {
327                                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
328                                         pSEGS_FEATURE(pShortDraw)->visible = TRUE;
329                                         break;
330                                 }
331                         default:
332                                 {
333                                         break;
334                                 }
335                         }
336                 }
337         }
338 }
339 /*--------------------------------------------------------------------------*/ 
340 //** ------------------------------------------------------------------------------------------------
341 //**
342 //** ------------ The physical redraw of the scope is hidden here ----------------------------------- 
343 //**
344 //**
345 void scoDrawScopeAmplitudeTimeStyle(ScopeMemory * pScopeMemory, double t)
346 {
347         int c__1 = 1;
348         int i = 0,j = 0;
349         int NbrPtsShort = 0,NbrPtsLong = 0,inc = 0;
350         int NbrPtsToCopy = 0;
351         int current_period_counter = 0;
352         int NumCurvInSubWin = 0;
353         int NumSubWin = 0 ;
354         int NumCurvTotal = 0;
355         int ShortDrawTableIndex = 0;  
356         scoGraphicalObject pLongDraw = NULL;
357         scoGraphicalObject pShortDraw = NULL;
358         scoGraphicalObject *pShortDrawTable = NULL;
359
360         double d_current_real_time = 0. ; //** the current real time as  double (52 bit) data structure
361         double last_update_time = 0. , delta_time = 0.;
362         int force_update[256]; //** ... for some headroom :)
363         //** ------ Real Time section -------------------------------------------------------------  
364         //** current real time as double [second] 
365         d_current_real_time = scoGetRealTime();  
366         last_update_time = pScopeMemory->d_last_scope_update_time; //** recover the last update time 
367         delta_time = d_current_real_time - last_update_time ; 
368         //refresh_time = 0.040 ; //** 40ms -> 25 fps min  
369         //** ------ End of Real Time section ------------------------------------------------------
370
371         ShortDrawTableIndex = 0; 
372         NumCurvTotal = 0;  
373         NumSubWin = scoGetNumberOfSubwin(pScopeMemory);
374         for(i = 0 ; i < NumSubWin ; i++) //** for all the subwindows in the scope 
375         {
376                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
377                 switch (sciGetEntityType(pShortDraw))
378                 {
379
380                 case SCI_POLYLINE:
381                         NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1; //** this is incremented by one at each iteration 
382                         break;
383
384                 case SCI_SEGS:
385                         NbrPtsShort = pSEGS_FEATURE(pShortDraw)->Nbr1;
386                         break;
387
388                 default:
389                         Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
390                         break;
391                 }
392
393                 //** combined condition for forced redraw
394                 //S.Steer time condition commented out because it slow down the display 
395                 //an option should be to increase the ShortDraw size if it is full before refresh_time is reached
396                 force_update[i]=/*((delta_time>refresh_time) && (NbrPtsShort>=2)) ||*/ 
397                         (NbrPtsShort>=scoGetShortDrawSize(pScopeMemory,i));
398
399
400
401                 NumCurvTotal += scoGetNumberOfCurvesBySubwin(pScopeMemory,i);
402
403         } 
404
405         pShortDrawTable = scicos_malloc( NumCurvTotal * sizeof(scoGraphicalObject) );
406         //** for all the sub window in the scope 
407         for (i = 0 ; i < NumSubWin ; i++) 
408         {
409                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
410                 switch (sciGetEntityType(pShortDraw))
411                 {
412
413                 case SCI_POLYLINE:
414                         NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1; //** this is incremented by one at each iteration 
415                         break;
416
417                 case SCI_SEGS:
418                         NbrPtsShort = pSEGS_FEATURE(pShortDraw)->Nbr1;
419                         break;
420
421                 default:
422                         Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
423                         break;
424                 }
425
426                 //**-------------------------------------------------------------------------------------------------------    
427                 //**  
428                 if (force_update[i] == 1) 
429                 {   
430                         /*Block for Realloc*/
431                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,0);
432                         switch (sciGetEntityType(pLongDraw))
433                         {
434                         case SCI_POLYLINE:
435                                 NbrPtsLong = pPOLYLINE_FEATURE(pLongDraw)->n1;
436                                 break;
437                         case SCI_SEGS:
438                                 NbrPtsLong = pSEGS_FEATURE(pLongDraw)->Nbr1;
439                                 break;
440                         default:
441                                 Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
442                                 break;
443                         }
444
445                         if ((NbrPtsLong + scoGetShortDrawSize(pScopeMemory,i)) >= scoGetLongDrawSize(pScopeMemory,i))
446                         {
447                                 for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
448                                 {
449                                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
450                                         scoReallocLongDraw(pLongDraw, NbrPtsLong, scoGetShortDrawSize(pScopeMemory,i),10000);
451                                 }
452                                 //Dont forget this one - If in the futur LongDrawSize is a table we can put it in the scoReallocLongDraw() function
453                                 scoSetLongDrawSize(pScopeMemory,i,NbrPtsLong + scoGetShortDrawSize(pScopeMemory,i)+10000);
454                         }
455                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,0);
456                         /*End od block for Realloc*/
457
458                         /*Block for Draw*/
459                         NumCurvInSubWin = scoGetNumberOfCurvesBySubwin(pScopeMemory,i); //** get number of curves in subwin 
460                         for (j = 0 ; j < NumCurvInSubWin ; j++)
461                         {
462                                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
463                                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
464
465                                 if (pShortDraw != NULL) //** protection for NULL pointer 
466                                 {
467                                         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
468                                         sciSetSelectedSubWin(scoGetPointerAxes(pScopeMemory,i));
469                                         sciSetVisibility(pShortDraw, TRUE);
470                                         pShortDrawTable[ShortDrawTableIndex] = pShortDraw ;
471                                         ShortDrawTableIndex++;
472                                 }
473                                 else
474                                 {
475                                         scoScopeError(pScopeMemory,0);
476                                 }
477                         } //** end of the for() loop
478                         /*End of Block for Draw*/
479
480                 }
481         } //** end of the for loop for all the subwindow in the scope 
482
483         /* Draw all short draw(s) in all the _concerned_ subwindows in a row :) */
484         sciDrawSetOfObj(pShortDrawTable, ShortDrawTableIndex );
485         scicos_free(pShortDrawTable);
486
487         for (i = 0 ; i < NumSubWin ; i++) //** for all the subwin in the scope 
488         {
489                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
490                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,0);
491                 switch (sciGetEntityType(pShortDraw))
492                 {
493                 case SCI_POLYLINE:
494                         NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1; //** this is incremented by one at each iteration 
495                         break;
496                 case SCI_SEGS:
497                         NbrPtsShort = pSEGS_FEATURE(pShortDraw)->Nbr1;
498                         break;
499                 default:
500                         Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
501                         break;
502                 }
503                 if (force_update[i] == 1) //** scope need a redraw 
504                 {
505                         switch (sciGetEntityType(pLongDraw))
506                         {
507                         case SCI_POLYLINE:
508                                 NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1-1;
509                                 NbrPtsLong = pPOLYLINE_FEATURE(pLongDraw)->n1;
510                                 break;
511                         case SCI_SEGS:
512                                 NbrPtsShort = pSEGS_FEATURE(pShortDraw)->Nbr1;
513                                 NbrPtsLong = pSEGS_FEATURE(pLongDraw)->Nbr1;
514                                 break;
515                         default:
516                                 Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
517                                 break;
518                         }
519
520                         for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
521                         {
522                                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
523                                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
524                                 /*Block for Memory*/
525                                 /*
526                                 * Alan's patch, 04/10/2007 : add NbrPtsToCopy to copy good numbers of pts for the first buffer in LongDraw + 
527                                 * Tricks to handle the trace empty case
528                                 */
529                                 inc = NbrPtsLong!=0; 
530                                 NbrPtsToCopy = NbrPtsShort + (NbrPtsLong==0);
531
532                                 switch (sciGetEntityType(pShortDraw))
533                                 {
534                                 case SCI_POLYLINE:
535                                         NbrPtsToCopy = NbrPtsShort + (NbrPtsLong==0);
536                                         // We have draw but now we have to copy values in the memory of the shortdraw
537                                         C2F(dcopy)(&NbrPtsToCopy,pPOLYLINE_FEATURE(pShortDraw)->pvx+inc,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvx+NbrPtsLong,&c__1);
538                                         C2F(dcopy)(&NbrPtsToCopy,pPOLYLINE_FEATURE(pShortDraw)->pvy+inc,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvy+NbrPtsLong,&c__1);
539                                         pPOLYLINE_FEATURE(pLongDraw)->n1 = NbrPtsLong+NbrPtsToCopy;
540                                         break;
541                                 case SCI_SEGS:
542                                         C2F(dcopy)(&NbrPtsShort,pSEGS_FEATURE(pShortDraw)->vx,&c__1,pSEGS_FEATURE(pLongDraw)->vx+NbrPtsLong,&c__1);
543                                         C2F(dcopy)(&NbrPtsShort,pSEGS_FEATURE(pShortDraw)->vy,&c__1,pSEGS_FEATURE(pLongDraw)->vy+NbrPtsLong,&c__1);
544                                         pSEGS_FEATURE(pLongDraw)->Nbr1 = NbrPtsLong+NbrPtsShort;
545                                         pSEGS_FEATURE(pLongDraw)->Nbr2 = NbrPtsLong+NbrPtsShort;
546                                         break;
547                                 default:
548                                         Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
549                                         break;
550                                 }
551                                 /*End of Block for Memory*/
552                         }
553
554                         /* Block for Detecting redrawing of x-axis values and others activities*/
555                         current_period_counter = (int)(t/scoGetPeriod(pScopeMemory,i));
556                         // Maybe for the axes we are at the end of it - it is here that we detect and notify it
557                         if (current_period_counter != scoGetPeriodCounter(pScopeMemory,i))
558                         {
559                                 scoSetNewDraw(pScopeMemory,i,-1);
560                                 scoSetPeriodCounter(pScopeMemory,i,current_period_counter);
561                         }
562                         else
563                         {
564                                 for (j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
565                                 {
566                                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
567                                         switch (sciGetEntityType(pShortDraw))
568                                         {
569                                         case SCI_POLYLINE:
570                                                 /* Last point is next first point*/
571                                                 pPOLYLINE_FEATURE(pShortDraw)->pvx[0] = pPOLYLINE_FEATURE(pShortDraw)->pvx[NbrPtsShort];
572                                                 pPOLYLINE_FEATURE(pShortDraw)->pvy[0] = pPOLYLINE_FEATURE(pShortDraw)->pvy[NbrPtsShort];
573                                                 pPOLYLINE_FEATURE(pShortDraw)->n1 = 1;
574                                                 break;
575                                         case SCI_SEGS:
576                                                 /* Do Nothing */
577                                                 break;
578                                         default:
579                                                 Coserror(_("Cannot use %s with this type of object."), "scoDrawScopeAmplitudeTimeStyle");
580                                                 break;
581                                         }
582                                 }
583                         } //** close of if() 
584
585                         pScopeMemory->d_last_scope_update_time = scoGetRealTime(); //** update the time stamping AFTER the draw 
586                         //** this "trick" assure at least 25ms of pure simulation (if the buffer is big enough)
587
588                 } //** the scope has been updated  
589
590         } //** for all the subwin
591
592 }
593
594 //**-------------------------------------------------------------------------------------------------------------
595 scoGraphicalObject scoCreatePolyline(scoGraphicalObject pAxes, scoInteger polyline_size, int color)
596 {
597         scoGraphicalObject pPolyline;
598         int i;
599         double * vx, * vy, *vz;
600
601         vx = (double*)scicos_malloc(polyline_size*sizeof(double));
602         vy = (double*)scicos_malloc(polyline_size*sizeof(double));
603
604         /* Alan, 29/09/07 : initial value set to zero for buffer
605         * to disable random display at the beginning of the simulation
606         */
607         for (i=0;i<polyline_size;i++) {
608                 vx[i] = 0.0;
609                 vy[i] = 0.0;
610         }
611
612         if (pSUBWIN_FEATURE(pAxes)->axes.axes_visible[2] == TRUE) //3D
613         {
614                 vz = (double*)scicos_malloc(polyline_size*sizeof(double));
615                 /* Alan, 29/09/07 : initial values set to zero for buffer
616                 * to disable random display at the beginning of the simulation
617                 */
618                 for (i=0;i<polyline_size;i++) {
619                         vz[i] = 0.0;
620                 }
621                 pPolyline=ConstructPolyline(pAxes,vx,vy,vz,0,polyline_size,1, NULL,NULL,NULL,NULL,NULL,FALSE,FALSE,TRUE,FALSE);
622                 scicos_free(vz);
623         }
624         else //2D
625         {
626                 pPolyline=ConstructPolyline(pAxes,vx,vy,NULL,0,polyline_size,1, NULL,NULL,NULL,NULL,NULL,FALSE,FALSE,TRUE,FALSE);
627         }
628
629         scicos_free(vx);
630         scicos_free(vy);
631
632         pPOLYLINE_FEATURE(pPolyline)->n1 = 0;
633
634         if (color > 0)
635         {
636                 sciSetForeground(pPolyline, color);
637                 sciSetIsLine(pPolyline, 1);
638                 sciSetLineStyle(pPolyline, 1);
639                 sciSetMarkStyle(pPolyline, 0);
640                 sciSetIsMark(pPolyline, 0);
641         }
642         else if (color <= 0)
643         {
644                 sciSetMarkForeground(pPolyline, -1);
645                 sciSetIsLine(pPolyline, 0);
646                 sciSetLineStyle(pPolyline, 0);
647                 sciSetIsMark(pPolyline, 1);
648                 sciSetMarkSizeUnit(pPolyline, 1);
649                 if (color!=0) {
650                         sciSetMarkStyle(pPolyline, -color);
651                         sciSetMarkSize(pPolyline, 4);
652                 }
653                 else {
654                         sciSetMarkStyle(pPolyline, 11);
655                         sciSetMarkSize(pPolyline, 0);
656                         sciSetMarkBackground(pPolyline, -1);
657                 }
658         }
659         sciSetIsClipping(pPolyline, 0);
660
661
662
663         return pPolyline;
664 }
665 /*--------------------------------------------------------------------------*/ 
666 void scoAddPolylineForShortDraw(ScopeMemory * pScopeMemory, int i, int j, int color)
667 {
668         scoGraphicalObject pShortDraw;
669         pShortDraw = scoCreatePolyline(scoGetPointerAxes(pScopeMemory,i),scoGetShortDrawSize(pScopeMemory,i),color);
670         scoSetHandleFromPointerShortDraw(pScopeMemory,i,j,pShortDraw);
671 }
672 /*--------------------------------------------------------------------------*/ 
673 void scoAddPolylineForLongDraw(ScopeMemory * pScopeMemory, int i, int j, int color)
674 {
675
676         scoGraphicalObject pLongDraw;
677         pLongDraw = scoCreatePolyline(scoGetPointerAxes(pScopeMemory,i),scoGetLongDrawSize(pScopeMemory,i),color);
678         scoSetHandleFromPointerLongDraw(pScopeMemory,i,j,pLongDraw);
679
680 }
681 /*--------------------------------------------------------------------------*/ 
682 void scoAddCoupleOfPolylines(ScopeMemory * pScopeMemory, int * colors)
683 {
684         int i,j;
685         int inc = -1;
686         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
687         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
688         {
689                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
690                 {
691                         if(colors != NULL)
692                         {
693                                 scoAddPolylineForShortDraw(pScopeMemory,i,j,colors[j+1+inc]);
694                                 scoAddPolylineForLongDraw(pScopeMemory,i,j,colors[j+1+inc]);
695                         }
696                         else
697                         {
698                                 scoAddPolylineForShortDraw(pScopeMemory,i,j,-1);
699                                 scoAddPolylineForLongDraw(pScopeMemory,i,j,-1);
700                         }
701                 }
702                 inc = j+inc; //not +1 because of we have exited the loop
703         }
704 }
705 /*--------------------------------------------------------------------------*/ 
706 void scoDelCoupleOfPolylines(ScopeMemory * pScopeMemory)
707 {
708         int i,j;
709         int c__1 = 1;
710         scoGraphicalObject pLongDraw;
711         scoGraphicalObject pShortDraw;
712         int NbrPtsLong, NbrPtsShort;
713         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
714         if(scoGetPointerScopeWindow(pScopeMemory) != NULL)
715         {
716                 for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
717                 {
718                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,0);
719                         NbrPtsLong = pPOLYLINE_FEATURE(pLongDraw)->n1;
720                         if(NbrPtsLong + scoGetShortDrawSize(pScopeMemory,i) > scoGetLongDrawSize(pScopeMemory,i))
721                         {
722                                 //We realloc because maybe if we add the shortdraw the size would be bigger than the longdraw size
723                                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
724                                 {
725                                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
726                                         scoReallocLongDraw(pLongDraw, NbrPtsLong, scoGetShortDrawSize(pScopeMemory,i), 1000);
727                                 }
728                                 scoSetLongDrawSize(pScopeMemory,i,NbrPtsLong + scoGetShortDrawSize(pScopeMemory,i) + 1000);
729                         }
730
731                         pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
732                         NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1;
733                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,0);
734                         NbrPtsLong = pPOLYLINE_FEATURE(pLongDraw)->n1;
735
736                         for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
737                         {
738                                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
739                                 pLongDraw = scoGetPointerLongDraw(pScopeMemory,i,j);
740                                 //Here we copy values in memory
741                                 C2F(dcopy)(&NbrPtsShort,pPOLYLINE_FEATURE(pShortDraw)->pvx,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvx+NbrPtsLong,&c__1);
742                                 C2F(dcopy)(&NbrPtsShort,pPOLYLINE_FEATURE(pShortDraw)->pvy,&c__1,pPOLYLINE_FEATURE(pLongDraw)->pvy+NbrPtsLong,&c__1);
743
744                                 pPOLYLINE_FEATURE(pLongDraw)->n1 = NbrPtsLong + NbrPtsShort;
745                                 pPOLYLINE_FEATURE(pShortDraw)->n1 = 0;
746                                 //Destruction of the polyline - no presence in the menu editor anymore
747                                 DestroyPolyline(pShortDraw);
748                                 //** TEST
749                                 forceRedraw(pLongDraw);
750                         }
751                 }
752                 //** sciSetUsedWindow(scoGetWindowID(pScopeMemory));
753
754
755                 sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
756                 //**redrawHierarchy(scoGetPointerScopeWindow(pScopeMemory));
757         }
758 }
759 /*--------------------------------------------------------------------------*/ 
760 void scoAddCoupleOfSegments(ScopeMemory * pScopeMemory, int * color)
761 {
762         int i,j;
763         int longdraw_size;
764         double vx1[2], vy1[2];
765         double * vx2, * vy2;
766         int style[1];
767         scoGraphicalObject pShortDraw;
768         scoGraphicalObject pLongDraw;
769         scoGraphicalObject pAxes;
770
771         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
772         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
773         {
774                 longdraw_size = scoGetLongDrawSize(pScopeMemory,i);
775                 //ConstructSegs dont support for the moment the NULL parameter
776                 vx1[0] = 0.0;
777                 vx1[1] = 0.0;
778                 vy1[0] = 0.0;
779                 vy1[1] = 0.0;
780
781                 vx2 = (double*)scicos_malloc(longdraw_size*sizeof(double));
782                 vy2 = (double*)scicos_malloc(longdraw_size*sizeof(double));
783
784                 for(j = 0 ; j < longdraw_size ; j++)
785                 {
786                         //same than before
787                         vx2[j] = 0.0;
788                         vy2[j] = 0.0;
789                 }
790
791
792
793
794                 pAxes = scoGetPointerAxes(pScopeMemory,i);
795
796                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
797                 {
798
799                         /* Compute color of segments */
800                         if (color == NULL)
801                         {
802                                 /* default color */
803                                 style[0] = 0;
804                         }
805                         else
806                         {
807                                 /* User specified color */
808                                 style[0] = color[j];
809                         }
810
811                         pLongDraw = ConstructSegs(pAxes, 0, vx2, vy2, NULL, longdraw_size, longdraw_size, 0, NULL, NULL,0,style, 0, TRUE, 0);
812                         pSEGS_FEATURE(pLongDraw)->Nbr1 = 0;
813                         pSEGS_FEATURE(pLongDraw)->Nbr2 = 0;
814                         sciSetIsLine(pLongDraw, 1);
815                         sciSetLineStyle(pLongDraw, 1);
816                         sciSetMarkStyle(pLongDraw, 0);
817                         sciSetIsMark(pLongDraw, 0);
818                         sciSetIsClipping(pLongDraw, 0);
819                         scoSetHandleFromPointerLongDraw(pScopeMemory,i,j,pLongDraw);
820
821                         pShortDraw = ConstructSegs(pAxes, 0, vx1, vy1, NULL, 2, 2, 0, NULL, NULL,0,style, 0, TRUE, 0);
822                         pSEGS_FEATURE(pShortDraw)->Nbr1 = 0;
823                         pSEGS_FEATURE(pShortDraw)->Nbr2 = 0;
824
825                         sciSetIsLine(pShortDraw, 1);
826                         sciSetLineStyle(pShortDraw, 1);
827                         sciSetMarkStyle(pShortDraw, 0);
828                         sciSetIsMark(pShortDraw, 0);
829                         sciSetIsClipping(pShortDraw, 0);
830                         scoSetHandleFromPointerShortDraw(pScopeMemory,i,j,pShortDraw);
831                 }
832
833                 scicos_free(vx2);
834                 scicos_free(vy2);
835         }
836 }
837 /*--------------------------------------------------------------------------*/ 
838 void scoDelCoupleOfSegments(ScopeMemory * pScopeMemory)
839 {
840         /* destroy all short draw */
841         int j;
842         int i;
843         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
844         {
845                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
846                 {
847                         sciDelGraphicObj(scoGetPointerShortDraw(pScopeMemory, i, j));
848                 }
849         }
850         sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
851 }
852 /*--------------------------------------------------------------------------*/ 
853 scoGraphicalObject scoCreateSphere(scoGraphicalObject pAxes, double radius, int color)
854 {
855         scoGraphicalObject pSphere;
856         pSphere=ConstructArc(pAxes, 0, 0, radius, radius, 0, 23040, &color, &color, TRUE, FALSE);
857         return pSphere;
858 }
859 /*--------------------------------------------------------------------------*/ 
860 void scoAddSphereForShortDraw(ScopeMemory * pScopeMemory, int i, int j, double radius, int color)
861 {
862         scoGraphicalObject pShortDraw;
863         pShortDraw = scoCreateSphere(scoGetPointerAxes(pScopeMemory,i),radius,color);
864         scoSetHandleFromPointerShortDraw(pScopeMemory,i,j,pShortDraw);
865 }
866 /*--------------------------------------------------------------------------*/ 
867 void scoAddSphereForLongDraw(ScopeMemory * pScopeMemory, int i, int j, double radius, int color)
868 {
869         scoGraphicalObject pLongDraw;
870         pLongDraw = scoCreateSphere(scoGetPointerAxes(pScopeMemory,i),radius,color);
871         scoSetHandleFromPointerLongDraw(pScopeMemory,i,j,pLongDraw);
872 }
873 /*--------------------------------------------------------------------------*/ 
874 void scoAddCoupleOfSpheres(ScopeMemory * pScopeMemory, double * radius, int * colors)
875 {
876         int i = 0,j = 0;
877         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
878         for (i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
879         { 
880                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
881                 {
882                         if(colors != NULL)
883                         {
884                                 scoAddSphereForShortDraw(pScopeMemory,i,j,radius[i+j], colors[i+j]);
885                                 scoAddSphereForLongDraw(pScopeMemory,i,j,radius[i+j], colors[i+j]);
886                         }
887                         else
888                         {
889                                 scoAddSphereForShortDraw(pScopeMemory,i,j,1,1);
890                                 scoAddSphereForLongDraw(pScopeMemory,i,j,1,1);
891                         }
892                 }
893         }
894 }
895 /*--------------------------------------------------------------------------*/ 
896 scoGraphicalObject scoCreateRectangle(scoGraphicalObject pAxes, double x, double y, double width, double height)
897 {
898         scoGraphicalObject pRectangle;
899         pRectangle = ConstructRectangle(pAxes,x,y,height,width,NULL,NULL,0,1);
900         return pRectangle;
901 }
902 /*--------------------------------------------------------------------------*/ 
903 void scoAddRectangleForLongDraw(ScopeMemory * pScopeMemory, int i, int j, double x, double y, double width, double height)
904 {
905         scoGraphicalObject pLongDraw;
906         pLongDraw = scoCreateRectangle(scoGetPointerAxes(pScopeMemory,i),x,y,width,height);
907         scoSetHandleFromPointerLongDraw(pScopeMemory,i,j,pLongDraw);
908 }
909 /*--------------------------------------------------------------------------*/ 
910 void scoAddTitlesScope(ScopeMemory * pScopeMemory, char * label, char * x, char * y, char * z)
911 {
912
913         int i = 0;
914         int nxname = 0;
915         char ** title=NULL;
916         char * x_title=NULL;
917         char * y_title;
918
919         title = (char**)scicos_malloc(scoGetNumberOfSubwin(pScopeMemory)*sizeof(char*));
920         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
921         {
922                 title[i] = (char*)scicos_malloc(20*sizeof(char)); //Why 20 ? BECAUSE :) should be less than 255
923                 sprintf(title[i],"Graphic %d",i+1);
924         }
925
926
927         x_title = x;
928         y_title = y;
929
930         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
931         for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
932         {
933                 sciSetText(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_title,&title[i],1,1); /* 1,1 is nbrow, nbcol */
934                 sciSetText(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_x_label,&x_title,1,1); /* 1,1 is nbrow, nbcol */
935                 sciSetText(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_y_label,&y_title,1,1);/* 1,1 is nbrow, nbcol */
936
937
938                 sciSetFontSize(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_x_label, 1);
939                 sciSetFontSize(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_y_label, 1);
940                 sciSetFontSize(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_title, 1);
941                 forceRedraw(scoGetPointerAxes(pScopeMemory,i));
942         }
943
944         if(z!= NULL)
945         {
946                 char * z_title;
947                 z_title = z;
948
949                 for(i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
950                 {
951                         sciSetFontSize(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_z_label, 1);
952                         sciSetText(pSUBWIN_FEATURE(scoGetPointerAxes(pScopeMemory,i))->mon_z_label,&z_title,1,1); /* 1,1 is nbrow, nbcol */
953                 }
954         }
955         for(i = 0; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
956         {
957                 scicos_free(title[i]);
958         }
959         scicos_free(title);
960
961         /* Code for naming the window*/
962         if (label != NULL)
963         {
964                 nxname = (int)strlen(label);
965                 if (nxname > 39) 
966                 {
967                         nxname = 39;
968                 }
969         }
970         *(label + nxname) = *"\000";
971         if((nxname == 1 && *(unsigned char *)label == ' ') || (nxname ==0))
972         {
973                 /*do nothing*/
974         }
975         else
976         {
977                 sciSetName(scoGetPointerScopeWindow(pScopeMemory), label);
978         }
979         /*End of code for naming window */
980         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
981         sciDrawObj(scoGetPointerScopeWindow(pScopeMemory));
982         //**redrawHierarchy(scoGetPointerScopeWindow(pScopeMemory));
983 }
984 /*--------------------------------------------------------------------------*/ 
985 void scoDrawScopeXYStyle(ScopeMemory * pScopeMemory)
986 {
987         /* 15/07/2009, S. Steer: efficiency improved by grouping the objects to be drawn  */
988
989         scoGraphicalObject Pinceau; //Pencil
990         scoGraphicalObject Trait; //Line
991         int NbrPtsShort, NbrPtsLong;
992         int c__1 = 1;
993         int i = 0,j = 0;
994         int Ncurv = scoGetNumberOfCurvesBySubwin(pScopeMemory,0);
995         scoGraphicalObject *DrawTable = NULL;
996         int DrawTableIndex=0;
997         //Coded only for one subwin can be easily extended to many with a factorj , j < scoGetNumberOfSubwin()
998
999         /* Display the ShortDraw polylines which are filled */
1000         /* The loop on curv has been duplicated for efficiency reason: 
1001         to be able to draw all the polyline in one call*/
1002
1003         DrawTable = scicos_malloc( Ncurv * sizeof(scoGraphicalObject) );
1004         for(i = 0 ; i < Ncurv ;i++)
1005         {
1006                 Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
1007                 NbrPtsShort = pPOLYLINE_FEATURE(Pinceau)->n1;
1008                 if(NbrPtsShort >= scoGetShortDrawSize(pScopeMemory,0)){ 
1009                         DrawTable[ DrawTableIndex++]=Pinceau;
1010                 }
1011         }
1012         if (DrawTableIndex>0) {
1013                 sciSetUsedWindow(scoGetWindowID(pScopeMemory));
1014                 sciDrawSetOfObj(DrawTable,DrawTableIndex);
1015         }
1016         scicos_free(DrawTable);
1017
1018         /* Update the polylines */
1019         for(i = 0 ; i < Ncurv ;i++)
1020         {
1021                 Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
1022                 Trait = scoGetPointerLongDraw(pScopeMemory,0,i);
1023
1024                 NbrPtsShort = pPOLYLINE_FEATURE(Pinceau)->n1;
1025                 if(NbrPtsShort >= scoGetShortDrawSize(pScopeMemory,0))
1026                 {
1027                         NbrPtsLong = pPOLYLINE_FEATURE(Trait)->n1;
1028                         /*Extend  LongDraw polyline allocation if necessary */
1029                         if(NbrPtsLong + NbrPtsShort >= scoGetLongDrawSize(pScopeMemory,0))
1030                         {
1031                                 for(j = 0 ; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; j++)
1032                                 {
1033                                         scoReallocLongDraw(scoGetPointerLongDraw(pScopeMemory,0,j), NbrPtsLong, NbrPtsShort, 5000);
1034                                 }
1035                                 scoSetLongDrawSize(pScopeMemory, 0, NbrPtsLong + NbrPtsShort + 5000);
1036                         }
1037                         NbrPtsLong = pPOLYLINE_FEATURE(Trait)->n1;
1038
1039                         /*Append the ShortDraw polyline points at the end of the LongDraw one */
1040                         C2F(dcopy)(&NbrPtsShort,pPOLYLINE_FEATURE(Pinceau)->pvx,&c__1,pPOLYLINE_FEATURE(Trait)->pvx+NbrPtsLong,&c__1);
1041                         C2F(dcopy)(&NbrPtsShort,pPOLYLINE_FEATURE(Pinceau)->pvy,&c__1,pPOLYLINE_FEATURE(Trait)->pvy+NbrPtsLong,&c__1);
1042                         if(pPOLYLINE_FEATURE(Pinceau)->pvz != NULL)
1043                         {
1044                                 C2F(dcopy)(&NbrPtsShort,pPOLYLINE_FEATURE(Pinceau)->pvz,&c__1,pPOLYLINE_FEATURE(Trait)->pvz+NbrPtsLong,&c__1);
1045                         }      
1046                         pPOLYLINE_FEATURE(Trait)->n1 = NbrPtsLong + NbrPtsShort;
1047                         /*reset the ShortDraw polyline */
1048                         pPOLYLINE_FEATURE(Pinceau)->pvx[0] = pPOLYLINE_FEATURE(Pinceau)->pvx[NbrPtsShort-1];
1049                         pPOLYLINE_FEATURE(Pinceau)->pvy[0] = pPOLYLINE_FEATURE(Pinceau)->pvy[NbrPtsShort-1];
1050                         if(pPOLYLINE_FEATURE(Pinceau)->pvz != NULL)
1051                         {
1052                                 pPOLYLINE_FEATURE(Pinceau)->pvz[0] = pPOLYLINE_FEATURE(Pinceau)->pvz[NbrPtsShort-1];
1053                         }
1054                         pPOLYLINE_FEATURE(Pinceau)->n1 = 1;
1055                 }
1056         }
1057 }
1058 /*--------------------------------------------------------------------------*/ 
1059 void scoDrawScopeAnimXYStyle(ScopeMemory * pScopeMemory, double * u1, double * u2, double * u3)
1060 /* 15/07/2009, S. Steer: efficiency improved by grouping the objects to be drawn  */
1061 {
1062         int i = 0,j = 0;
1063         scoGraphicalObject Gomme; //Rubber
1064         scoGraphicalObject Pinceau; //Pencil
1065         scoGraphicalObject Trait; //Line
1066         int nbr_curves = 0;
1067         scoGraphicalObject *DrawTable = NULL;
1068
1069         /*If only one element to draw*/
1070         if(scoGetLongDrawSize(pScopeMemory,0) == 0)
1071         {
1072                 nbr_curves = scoGetNumberOfCurvesBySubwin(pScopeMemory,0);
1073                 DrawTable = scicos_malloc( nbr_curves * sizeof(scoGraphicalObject) );
1074                 for(i = 0 ; i < nbr_curves ; i++)
1075                 {
1076                         Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
1077                         pPOLYLINE_FEATURE(Pinceau)->pvx[0] = u1[i];
1078                         pPOLYLINE_FEATURE(Pinceau)->pvy[0] = u2[i];
1079                         if(u3 != NULL)
1080                         {
1081                                 pPOLYLINE_FEATURE(Pinceau)->pvz[0] = u3[i];
1082                         }
1083                 }
1084                 /* Draw all the objects in a single call for efficiency */
1085                 sciSetUsedWindow(scoGetWindowID(pScopeMemory));
1086                 sciDrawSetOfObj(DrawTable,nbr_curves);
1087                 scicos_free(DrawTable);
1088         }
1089         else /*It is a  polyline scope*/
1090         {
1091                 nbr_curves = scoGetNumberOfCurvesBySubwin(pScopeMemory,0)/2;
1092                 DrawTable = scicos_malloc((2 * nbr_curves) * sizeof(scoGraphicalObject) );
1093                 if(scoGetShortDrawSize(pScopeMemory,0) == 1)/*It is a mark style scope*/
1094                 {
1095                         for(i = 0 ; i < nbr_curves ; i++)
1096                         {
1097                                 Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
1098                                 Gomme = scoGetPointerShortDraw(pScopeMemory,0,i+nbr_curves);
1099                                 Trait = scoGetPointerLongDraw(pScopeMemory,0,i);
1100
1101                                 pPOLYLINE_FEATURE(Gomme)->pvx[0] = pPOLYLINE_FEATURE(Trait)->pvx[scoGetLongDrawSize(pScopeMemory,0)-1];
1102                                 pPOLYLINE_FEATURE(Gomme)->pvy[0] = pPOLYLINE_FEATURE(Trait)->pvy[scoGetLongDrawSize(pScopeMemory,0)-1];
1103
1104                                 for (j = scoGetLongDrawSize(pScopeMemory,0)-1 ; j > 0 ; j--)
1105                                 {
1106                                         pPOLYLINE_FEATURE(Trait)->pvx[j] = pPOLYLINE_FEATURE(Trait)->pvx[j-1];
1107                                         pPOLYLINE_FEATURE(Trait)->pvy[j] = pPOLYLINE_FEATURE(Trait)->pvy[j-1];
1108                                 }
1109
1110                                 pPOLYLINE_FEATURE(Trait)->pvx[0] = pPOLYLINE_FEATURE(Pinceau)->pvx[0];
1111                                 pPOLYLINE_FEATURE(Trait)->pvy[0] = pPOLYLINE_FEATURE(Pinceau)->pvy[0];
1112
1113                                 pPOLYLINE_FEATURE(Pinceau)->pvx[0] = u1[i];
1114                                 pPOLYLINE_FEATURE(Pinceau)->pvy[0] = u2[i];
1115                                 if(u3 != NULL) //3D scope
1116                                 {
1117                                         pPOLYLINE_FEATURE(Gomme)->pvz[0] = pPOLYLINE_FEATURE(Trait)->pvz[scoGetLongDrawSize(pScopeMemory,0)-1];
1118                                         for (j = scoGetLongDrawSize(pScopeMemory,0)-1 ; j > 0 ; j--)
1119                                         {
1120                                                 pPOLYLINE_FEATURE(Trait)->pvz[j] = pPOLYLINE_FEATURE(Trait)->pvz[j-1];
1121                                         }
1122                                         pPOLYLINE_FEATURE(Trait)->pvz[0] = pPOLYLINE_FEATURE(Pinceau)->pvz[0];
1123                                         pPOLYLINE_FEATURE(Pinceau)->pvz[0] = u3[i];  
1124                                 }
1125
1126                                 /* memorize objects to be drawn */
1127                                 DrawTable[2*i]   = Pinceau;
1128                                 DrawTable[2*i+1] = Gomme;
1129                         }
1130
1131                         /* Draw all the objects in one call for efficiency*/
1132                         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
1133                         sciDrawSetOfObj(DrawTable,2*nbr_curves);
1134                         scicos_free(DrawTable);
1135                 }
1136                 else /*It is a line style polyline scope*/
1137                 {
1138                         nbr_curves = scoGetNumberOfCurvesBySubwin(pScopeMemory,0)/2;
1139                         DrawTable = scicos_malloc((2 * nbr_curves) * sizeof(scoGraphicalObject) );
1140
1141                         for(i = 0 ; i <  nbr_curves ; i++)
1142                         {
1143                                 Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
1144                                 Gomme = scoGetPointerShortDraw(pScopeMemory,0,i+nbr_curves);
1145                                 Trait = scoGetPointerLongDraw(pScopeMemory,0,i);
1146
1147                                 pPOLYLINE_FEATURE(Gomme)->pvx[0] = pPOLYLINE_FEATURE(Trait)->pvx[scoGetLongDrawSize(pScopeMemory,0)-1];
1148                                 pPOLYLINE_FEATURE(Gomme)->pvy[0] = pPOLYLINE_FEATURE(Trait)->pvy[scoGetLongDrawSize(pScopeMemory,0)-1];
1149
1150                                 pPOLYLINE_FEATURE(Gomme)->pvx[1] = pPOLYLINE_FEATURE(Trait)->pvx[scoGetLongDrawSize(pScopeMemory,0)-2];
1151                                 pPOLYLINE_FEATURE(Gomme)->pvy[1] = pPOLYLINE_FEATURE(Trait)->pvy[scoGetLongDrawSize(pScopeMemory,0)-2];
1152
1153                                 for (j = scoGetLongDrawSize(pScopeMemory,0)-1 ; j > 0 ; j--)
1154                                 {
1155                                         pPOLYLINE_FEATURE(Trait)->pvx[j] = pPOLYLINE_FEATURE(Trait)->pvx[j-1];
1156                                         pPOLYLINE_FEATURE(Trait)->pvy[j] = pPOLYLINE_FEATURE(Trait)->pvy[j-1];
1157                                 }
1158
1159                                 pPOLYLINE_FEATURE(Trait)->pvx[0] = pPOLYLINE_FEATURE(Pinceau)->pvx[0];
1160                                 pPOLYLINE_FEATURE(Trait)->pvy[0] = pPOLYLINE_FEATURE(Pinceau)->pvy[0];
1161
1162                                 pPOLYLINE_FEATURE(Pinceau)->pvx[1] = pPOLYLINE_FEATURE(Pinceau)->pvx[0];
1163                                 pPOLYLINE_FEATURE(Pinceau)->pvy[1] = pPOLYLINE_FEATURE(Pinceau)->pvy[0];
1164
1165                                 pPOLYLINE_FEATURE(Pinceau)->pvx[0] = u1[i];
1166                                 pPOLYLINE_FEATURE(Pinceau)->pvy[0] = u2[i];
1167                                 if(u3 != NULL)//3D Scope Mode
1168                                 {
1169                                         pPOLYLINE_FEATURE(Gomme)->pvz[0] = pPOLYLINE_FEATURE(Trait)->pvz[scoGetLongDrawSize(pScopeMemory,0)-1];
1170                                         pPOLYLINE_FEATURE(Gomme)->pvz[1] = pPOLYLINE_FEATURE(Trait)->pvz[scoGetLongDrawSize(pScopeMemory,0)-2];
1171                                         for (j = scoGetLongDrawSize(pScopeMemory,0)-1 ; j > 0 ; j--)
1172                                         {
1173                                                 pPOLYLINE_FEATURE(Trait)->pvz[j] = pPOLYLINE_FEATURE(Trait)->pvz[j-1];
1174                                         }
1175                                         pPOLYLINE_FEATURE(Trait)->pvz[0] = pPOLYLINE_FEATURE(Pinceau)->pvz[0];
1176                                         pPOLYLINE_FEATURE(Pinceau)->pvz[1] = pPOLYLINE_FEATURE(Pinceau)->pvz[0];
1177                                         pPOLYLINE_FEATURE(Pinceau)->pvz[0] = u3[i];
1178                                 }
1179                                 /* memorize objects to be drawn */
1180                                 DrawTable[2*i]   = Pinceau;
1181                                 DrawTable[2*i+1] = Gomme;
1182
1183                         }
1184
1185                         /* Draw all the objects in one call for efficiency*/
1186                         sciSetUsedWindow(scoGetWindowID(pScopeMemory));
1187                         sciDrawSetOfObj(DrawTable,2*nbr_curves);
1188                         scicos_free(DrawTable);
1189                 }
1190
1191         }
1192 }
1193 /*--------------------------------------------------------------------------*/ 
1194 scoGraphicalObject scoCreateGrayplot(scoGraphicalObject pAxes, int size_x, int size_y)
1195 {
1196         scoGraphicalObject pGrayplot;
1197         double * pvecx = NULL, * pvecy = NULL, * pvecz = NULL;
1198         int i = 0;
1199
1200         pvecx = (double*)scicos_malloc(size_x*sizeof(double));
1201         for(i = 0; i < size_x ; i++)
1202                 pvecx[i] = i;
1203
1204         pvecy = (double*)scicos_malloc(size_y*sizeof(double));
1205         for(i = 0; i < size_y ; i++)
1206                 pvecy[i] = i;
1207
1208         pvecz = (double*)scicos_malloc(size_x*size_y*sizeof(double));
1209         for(i = 0; i < size_x*size_y ; i++)
1210                 pvecz[i] = 0;
1211
1212         pGrayplot = ConstructGrayplot(pAxes, pvecx, pvecy, pvecz, size_x, size_y, 0);
1213
1214         scicos_free(pvecx);
1215         scicos_free(pvecy);
1216         scicos_free(pvecz);
1217
1218         return pGrayplot;
1219 }
1220 /*--------------------------------------------------------------------------*/ 
1221 void scoAddGrayplotForShortDraw(ScopeMemory * pScopeMemory, int i, int j, int size_x, int size_y)
1222 {
1223         scoGraphicalObject pShortDraw;
1224         pShortDraw = scoCreateGrayplot(scoGetPointerAxes(pScopeMemory,i),size_x,size_y);
1225         scoSetHandleFromPointerShortDraw(pScopeMemory,i,j,pShortDraw);
1226 }
1227 /*--------------------------------------------------------------------------*/ 
1228 scoGraphicalObject scoCreatePlot3d(scoGraphicalObject pAxes, int size_x, int size_y)
1229 {
1230         scoGraphicalObject pPlot3d;
1231         double * pvecx = NULL, * pvecy = NULL, *pvecz = NULL;
1232         int i = 0;
1233         int flag[3];
1234         double ebox[6];
1235         int isfac = 0;
1236         int m1 = 0,n1 = 0,m2 = 0,n2 = 0,m3 = 0,n3 = 0,m3n = 0,n3n = 0;
1237         int colorflag = 0 ;
1238
1239         pvecx = (double*)scicos_malloc(size_x*sizeof(double));
1240         for(i = 0; i < size_x ; i++)
1241                 pvecx[i] = i;
1242
1243         pvecy = (double*)scicos_malloc(size_y*sizeof(double));
1244         for(i = 0; i < size_y ; i++)
1245                 pvecy[i] = i;
1246
1247         pvecz = (double*)scicos_malloc(size_x*size_y*sizeof(double));
1248         for(i = 0; i < size_x*size_y ; i++)
1249                 pvecz[i] = 0;
1250
1251         //I have found all these values thanks to gdb and debugging a demo of scilab with a grayplot. I don't know exactly what they are and I don't care :p
1252         flag[0] = 2;
1253         flag[1] = 8;
1254         flag[2] = 4;
1255
1256         ebox[0] = 0;
1257         ebox[1] = 1;
1258         ebox[2] = 0;
1259         ebox[3] = 1;
1260         ebox[4] = 0;
1261         ebox[5] = 1;
1262
1263         isfac = 0;
1264
1265         colorflag = 1;
1266
1267         m1 = 1;
1268         n1 = size_x;
1269         m2 = 1;
1270         n2 = size_y;
1271         m3 =  size_x;
1272         n3 = size_y;
1273         m3n = 0;
1274         n3n = 0;
1275
1276         pPlot3d = ConstructSurface (pAxes, SCI_PLOT3D, pvecx, pvecy, pvecz, NULL, 0, size_x, size_y, flag, ebox, colorflag, &isfac, &m1, &n1, &m2, &n2, &m3, &n3, &m3n, &n3n);
1277
1278         scicos_free(pvecx);
1279         scicos_free(pvecy);
1280         scicos_free(pvecz);
1281
1282         return pPlot3d;
1283 }
1284 /*--------------------------------------------------------------------------*/ 
1285 void scoAddPlot3dForShortDraw(ScopeMemory * pScopeMemory, int i, int j, int size_x, int size_y)
1286 {
1287         scoGraphicalObject pShortDraw;
1288         pShortDraw = scoCreatePlot3d(scoGetPointerAxes(pScopeMemory,i),size_x,size_y);
1289         scoSetHandleFromPointerShortDraw(pScopeMemory,i,j,pShortDraw);
1290 }
1291 /*--------------------------------------------------------------------------*/