* bug 8630 fixed - Scopes crashed Scilab on simulation when they had the same
[scilab.git] / scilab / modules / scicos_blocks / src / c / cscopxy.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 cscopxy.c
24    \author Benoit Bayol
25    \version 1.0
26    \date September 2006 - January 2007
27    \brief CSCOPXY is a scope in 2D which draw its input as a XY scope, there is no animation, everything is keep in memory instead of CANIMXY
28    \see CSCOPXY.sci in macros/scicos_blocks/Sinks/
29 */
30 /*--------------------------------------------------------------------------*/ 
31 #include "CurrentObjectsManagement.h"
32 #include "scoMemoryScope.h"
33 #include "scoWindowScope.h"
34 #include "scoMisc.h"
35 #include "scoGetProperty.h"
36 #include "scoSetProperty.h"
37 #include "scicos_block4.h"
38 #include "DrawingBridge.h"
39 #include "SetJavaProperty.h"
40 #include "MALLOC.h"
41 #include "dynlib_scicos_blocks.h"
42 /*--------------------------------------------------------------------------*/ 
43 /** \fn cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
44     \brief Function to draw or redraw the window
45 */
46 SCICOS_BLOCKS_IMPEXP void cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
47 {
48   int * ipar = NULL; //Integer Parameters
49   int color_flag = 0; //Flag on Color
50   int color[2];
51   int line_size = 0;
52   int animed = 0;
53   int win = 0; //Windows ID : To give a name to the window
54   int buffer_size = 0; //Buffer Size
55   int win_pos[2]; //Position of the Window
56   int win_dim[2]; //Dimension of the Window
57   int nipar = 0;
58   double * rpar = NULL; //Reals parameters
59   double xmin = 0., xmax = 0., ymin = 0., ymax = 0.; //Ymin and Ymax are vectors here
60   int number_of_subwin = 0;
61   int number_of_curves_by_subwin = 0;
62   int dimension = 2;
63   int i = 0;
64   char *label = NULL;
65   scoGraphicalObject ShortDraw;
66   scoGraphicalObject LongDraw;
67
68   ipar = GetIparPtrs(block);
69   nipar = GetNipar(block);
70   rpar = GetRparPtrs(block);
71   win = ipar[0];
72   color_flag = ipar[1];
73   buffer_size = ipar[2];
74   color[0] = ipar[3];
75   color[1] = ipar[3];
76   line_size = ipar[4];
77   animed = ipar[5];
78   win_pos[0] = ipar[6];
79   win_pos[1] = ipar[7];
80   win_dim[0] = ipar[8];
81   win_dim[1] = ipar[9];
82   xmin = rpar[0];
83   xmax = rpar[1];
84   ymin = rpar[2];
85   ymax = rpar[3];
86   label = GetLabelPtrs(block);
87
88   number_of_subwin = 1;
89   number_of_curves_by_subwin = ipar[10]; //it is a trick to recognize the type of scope, not sure it is a good way because normally a curve is the combination of a short and a longdraw
90   if(firstdraw == 1)
91     {
92       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
93       scoSetShortDrawSize(*pScopeMemory,0,buffer_size);
94       scoSetLongDrawSize(*pScopeMemory,0,5000);
95     }
96
97   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
98   if(scoGetScopeActivation(*pScopeMemory) == 1)
99     {
100       for( i = 0 ; i < number_of_curves_by_subwin ; i++)
101         {
102           scoAddPolylineForShortDraw(*pScopeMemory,0,i,color[0]);
103           scoAddPolylineForLongDraw(*pScopeMemory,0,i,color[0]);
104           ShortDraw = scoGetPointerShortDraw(*pScopeMemory,0,i);
105           LongDraw = scoGetPointerLongDraw(*pScopeMemory,0,i);
106           /* Set ShortDraw properties */
107           sciSetLineWidth(ShortDraw, line_size);
108           sciSetMarkSize(ShortDraw, line_size);
109           /* Set LongDraw properties */
110           sciSetLineWidth(LongDraw, line_size);
111           sciSetMarkSize(LongDraw, line_size);
112         }
113       scoAddTitlesScope(*pScopeMemory,label,"x","y",NULL);
114     }
115
116     /* use only single buffering to be sure to draw on the screen */
117     if (scoGetPointerScopeWindow(*pScopeMemory) != NULL) {
118         sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
119     }
120 }
121 /*--------------------------------------------------------------------------*/ 
122 /** \fn void cscopxy(scicos_block * block, int flag)
123     \brief the computational function
124     \param block A pointer to a scicos_block
125     \param flag An int which indicates the state of the block (init, update, ending)
126 */
127 SCICOS_BLOCKS_IMPEXP void cscopxy(scicos_block * block, int flag)
128 {
129   /* Declarations*/
130   ScopeMemory * pScopeMemory = NULL;
131   double *u1 = NULL,*u2 = NULL;
132   scoGraphicalObject Pinceau; 
133   int NbrPtsShort = 0;
134   int i = 0;
135
136   /* State Machine Control */
137   switch(flag)
138     {
139     case Initialization:
140       {
141         cscopxy_draw(block,&pScopeMemory,1);
142         break; //Break of the switch condition don t forget it
143       } //End of Initialization
144
145     case StateUpdate:
146       {
147         scoRetrieveScopeMemory(block->work,&pScopeMemory);
148         if(scoGetScopeActivation(pScopeMemory) == 1)
149           {
150             
151         /* Charging Elements */
152         if (scoGetPointerScopeWindow(pScopeMemory) == NULL) // If the window has been destroyed we recreate it
153           {
154             cscopxy_draw(block,&pScopeMemory,0);
155           }
156
157         u1 = GetRealInPortPtrs(block,1);
158         u2 = GetRealInPortPtrs(block,2);
159         
160         for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0); i++)
161           {
162             Pinceau = scoGetPointerShortDraw(pScopeMemory,0,i);
163             NbrPtsShort = pPOLYLINE_FEATURE(Pinceau)->n1;
164             pPOLYLINE_FEATURE(Pinceau)->pvx[NbrPtsShort] = u1[i];
165             pPOLYLINE_FEATURE(Pinceau)->pvy[NbrPtsShort] = u2[i];
166             pPOLYLINE_FEATURE(Pinceau)->n1++;
167           }
168         
169         scoDrawScopeXYStyle(pScopeMemory);
170           }
171         break; //Break of the switch don t forget it !
172       }//End of stateupdate
173       
174       //This case is activated when the simulation is done or when we close scicos
175     case Ending:
176       {
177         scoRetrieveScopeMemory(block->work, &pScopeMemory);
178         if(scoGetScopeActivation(pScopeMemory) == 1)
179           {
180             /*sciSetUsedWindow(scoGetWindowID(pScopeMemory));*/
181             
182                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
183                         scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
184                         if (figure != NULL)
185                         {
186                                 for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0); i++)
187                                 {
188                                         Pinceau = scoGetPointerLongDraw(pScopeMemory,0,i);
189                                         forceRedraw(Pinceau);
190                                 }
191                         
192                                 //Here Pinceau = Window
193                                 /*Pinceau = sciGetCurrentFigure();*/
194                                 /*pFIGURE_FEATURE(Pinceau)->user_data = NULL;
195                                 pFIGURE_FEATURE(Pinceau)->size_of_user_data = 0;*/
196                                 clearUserData(figure);
197                                 
198                                 /* restore double buffering */
199                                 if (figure) {
200                     sciSetJavaUseSingleBuffer(figure, FALSE);
201                 }
202                         }
203         }
204         scoFreeScopeMemory(block->work, &pScopeMemory);
205         break; //Break of the switch
206       }
207       //free the memory which is allocated at each turn by some variables
208     }
209 }
210 /*--------------------------------------------------------------------------*/