Fix crash on Scicos exiting when simulation was stoped and some scope closed
[scilab.git] / scilab / modules / scicos_blocks / src / c / cmatview.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    \file cmatview.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CMATVIEW is a scope that connects a matrix to a grayplot. The values of the matrix are the values at the nodes
27   \see CMATVIEW.sci in macros/scicos_blocks/Sinks/
28 */
29 #include "CurrentObjectsManagement.h"
30 #include "DrawingBridge.h"
31 #include "scoMemoryScope.h"
32 #include "scoWindowScope.h"
33 #include "scoMisc.h"
34 #include "scoGetProperty.h"
35 #include "scoSetProperty.h"
36 #include "scicos_block4.h"
37 #include <math.h>
38 #include <stdlib.h>
39
40 /** \fn cmatview_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
41     \brief Function to draw or redraw the window
42 */
43 void cmatview_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
44 {
45   int i; //As usual
46   int * ipar; //Integer Parameters
47   int win_pos[2]; //Position of the Window
48   int win_dim[2]; //Dimension of the Window
49   int dimension = 2;
50   double * rpar; //Reals parameters
51   double  ymin, ymax; //Ymin and Ymax are vectors here
52   double  xmin, xmax;
53   int number_of_curves_by_subwin;
54   int number_of_subwin;
55   double * mat;
56   int size_mat;
57
58   rpar = GetRparPtrs(block);
59   ipar = GetIparPtrs(block);
60   number_of_subwin = 1;
61   win_pos[0] = -1;
62   win_pos[1] = -1;
63   win_dim[0] = -1;
64   win_dim[1] = -1;
65
66   size_mat = ipar[2];
67   mat = (double*)scicos_malloc(size_mat*sizeof(double));
68   for(i = 0 ; i < size_mat ; i++)
69     {
70       mat[i] = rpar[i+2];
71     }
72   xmax = GetInPortSize(block,1,1);
73   xmin = 0;
74   ymax = GetInPortSize(block,1,2);
75   ymin = 0;
76
77   number_of_curves_by_subwin = 1;
78
79   /*Allocating memory*/
80
81   if(firstdraw == 1)
82     {
83       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
84     }
85
86   /*Creating the Scope*/
87   scoInitOfWindow(*pScopeMemory, dimension, -1, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
88   if(scoGetScopeActivation(*pScopeMemory) == 1)
89     {
90       sciSetColormap(scoGetPointerScopeWindow(*pScopeMemory), mat , size_mat/3, 3);
91       scoAddTitlesScope(*pScopeMemory,"x","y",NULL);
92       scoAddGrayplotForShortDraw(*pScopeMemory,0,0,GetInPortSize(block,1,1),GetInPortSize(block,1,2));
93     }
94   scicos_free(mat);
95
96 }
97
98 /** \fn void cmatview(scicos_block * block, int flag)
99     \brief the computational function
100     \param block A pointer to a scicos_block
101     \param flag An int which indicates the state of the block (init, update, ending)
102 */
103 void cmatview(scicos_block * block, int flag)
104 {
105   /* Declarations */
106   ScopeMemory * pScopeMemory;
107   scoGraphicalObject pShortDraw;
108   double * u1;
109   double alpha,beta;
110   int i,j;
111   double * rpar;
112   int dim_i, dim_j;
113   /* Initializations and Allocations*/
114   //Allocations are done here because there are dependent of some values presents below
115  
116   /* State Machine Control */
117   switch(flag)
118     {
119     case Initialization:
120       {
121         cmatview_draw(block,&pScopeMemory,1);
122         break;
123       }
124     case StateUpdate:
125       {
126         /*Retreiving Scope in the block->work*/
127         scoRetrieveScopeMemory(block->work,&pScopeMemory);
128         if(scoGetScopeActivation(pScopeMemory) == 1)
129           {
130             /* If window has been destroyed we recreate it */
131             if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
132               {
133                 cmatview_draw(block,&pScopeMemory,0);
134               }
135             
136             pShortDraw = scoGetPointerShortDraw(pScopeMemory,0,0);
137             rpar = GetRparPtrs(block);
138             alpha = rpar[0];
139             beta = rpar[1];
140             u1 = GetInPortPtrs(block,1);
141             
142             dim_i = GetInPortRows(block,1);
143             dim_j = GetInPortCols(block,1);
144             
145             for(i = 0 ; i < dim_i ; i++)
146               {
147                 for(j = 0; j < dim_j ; j++)
148                   {
149                     pGRAYPLOT_FEATURE(pShortDraw)->pvecz[i*dim_j+j] = floor(alpha*u1[j+i*dim_j]+beta);
150                   }
151               }
152             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
153             if(sciGetPixmapMode(scoGetPointerScopeWindow(pScopeMemory)))
154               {
155                           /* TODO : not implemented */
156                           /*C2F(dr)("xset","wshow",PI0,PI0,PI0,PI0,PI0,PI0,PD0,PD0,PD0,PD0,0L,0L);*/
157               }
158             sciDrawObj(scoGetPointerShortDraw(pScopeMemory,0,0));
159           }
160         break;
161       }//End of stateupdate
162     case Ending:
163       {
164                                 scoRetrieveScopeMemory(block->work, &pScopeMemory);
165                                 if(scoGetScopeActivation(pScopeMemory) == 1)
166                                 {
167                                         /* sciSetUsedWindow(scoGetWindowID(pScopeMemory)); */
168                                         /*pShortDraw = sciGetCurrentFigure(); */
169                                         /*pFIGURE_FEATURE(pShortDraw)->user_data = NULL; */
170                                         /*pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0; */
171                                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
172                                         scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
173                                         if (figure != NULL)
174                                         {
175                                                 /*pShortDraw = scoGetPointerScopeWindow(pScopeMemory);*/
176                                                 clearUserData(figure);
177                                         }
178                                 }
179                                 scoFreeScopeMemory(block->work, &pScopeMemory);
180                                 break;
181       }
182     }
183 }