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