1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - ENPC - Jean-Philipe Chancelier
4  * Copyright (C) 2006 - INRIA - Fabrice Leray
5  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6  * Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
7  *
8  * Copyright (C) 2012 - 2016 - Scilab Enterprises
9  *
10  * This file is hereby licensed under the terms of the GNU GPL v2.0,
11  * pursuant to article 5.3.4 of the CeCILL v.2.1.
12  * This file was originally licensed under the terms of the CeCILL v2.1,
13  * and continues to be available under such terms.
14  * For more information, see the COPYING file which you should have received
15  * along with this program.
16  *
17  */
18 
19 /*------------------------------------------------------------------------*/
20 /* file: sci_zoom_rect.c                                                  */
21 /* desc : interface for zoom_rect routine                                 */
22 /*------------------------------------------------------------------------*/
23 
24 #include "gw_graphics.h"
25 #include "api_scilab.h"
26 #include "getPropertyAssignedValue.h"
27 #include "axesScale.h"
28 #include "localization.h"
29 #include "Scierror.h"
30 #include "GetProperty.h"
31 #include "SetPropertyStatus.h"
32 #include "HandleManagement.h"
33 #include "CurrentFigure.h"
34 #include "CurrentSubwin.h"
35 #include "BuildObjects.h"
36 
37 #include "JavaInteraction.h"
38 
39 #include "graphicObjectProperties.h"
40 #include "getGraphicObjectProperty.h"
41 #include "createGraphicObject.h"
42 
43 /*--------------------------------------------------------------------------*/
44 static int getZoomedObject(void* pvApiCtx, const char * fname);
45 static BOOL getZoomRect(void* pvApiCtx, const char * fname, int attribPos, double rect[4]);
46 /*--------------------------------------------------------------------------*/
47 /**
48  * Get the [xmin, ymin, xmax, ymax] vector specified as input argument
49  * @param fname name of the calling function for error messages
50  * @param attribPos position of the argument within the rhs (1 or 2)
51  * @param[out] rect retrieved rectangle
52  * @return TRUE if the rect could be retrieved, false otherwise
53  */
getZoomRect(void * pvApiCtx,const char * fname,int attribPos,double rect[4])54 static BOOL getZoomRect(void* pvApiCtx, const char * fname, int attribPos, double rect[4])
55 {
56     SciErr sciErr;
57     int nbRow = 0;
58     int nbCol = 0;
59     int* piAddrstackPointer = NULL;
60     double* stackPointer = NULL;
61     int i = 0;
62     double* rectVect = NULL;
63     sciErr = getVarAddressFromPosition(pvApiCtx, attribPos, &piAddrstackPointer);
64     if (sciErr.iErr)
65     {
66         printError(&sciErr, 0);
67         return 1;
68     }
69 
70     // Retrieve a matrix of double at position attribPos.
71     sciErr = getMatrixOfDouble(pvApiCtx, piAddrstackPointer, &nbRow, &nbCol, &stackPointer);
72     if (sciErr.iErr)
73     {
74         printError(&sciErr, 0);
75         Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, attribPos);
76         return 1;
77     }
78 
79 
80     if (nbRow * nbCol != 4)
81     {
82         if (attribPos == 1)
83         {
84             Scierror(999, _("%s: Wrong size for input argument #%d: Vector of size %d expected.\n"), fname, 1, 4);
85         }
86         else
87         {
88             Scierror(999, _("%s: Wrong size for input argument #%d: Vector of size %d expected.\n"), fname, 2, 4);
89         }
90         return FALSE;
91     }
92 
93     rectVect = (stackPointer);
94     for (i = 0; i < 4; i++)
95     {
96         rect[i] = rectVect[i];
97     }
98 
99     return TRUE;
100 
101 }
102 /*--------------------------------------------------------------------------*/
103 /**
104  * Return the handle passed as input argument if one exists
105  * @param fname of the function for errors
106  * @return NULL if the input argument is not correct,
107  *              the object to zoom otherwise
108  */
getZoomedObject(void * pvApiCtx,const char * fname)109 static int getZoomedObject(void* pvApiCtx, const char * fname)
110 {
111     SciErr sciErr;
112     int nbRow  = 0;
113     int nbCol = 0;
114     int* piAddrstackPointer = NULL;
115     long long* stackPointer = NULL;
116     int iObjUID = 0;
117     int iType = -1;
118     int *piType = &iType;
119     /* if a handle is specified it must be the first input argument */
120     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrstackPointer);
121     if (sciErr.iErr)
122     {
123         printError(&sciErr, 0);
124         return 0;
125     }
126 
127     // Retrieve a matrix of handle at position 1.
128     sciErr = getMatrixOfHandle(pvApiCtx, piAddrstackPointer, &nbRow, &nbCol, &stackPointer);
129     if (sciErr.iErr)
130     {
131         printError(&sciErr, 0);
132         Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, 1);
133         return 0;
134     }
135 
136 
137     /* check that there is only a single Figre or subwin */
138     if (nbRow * nbCol != 1)
139     {
140         Scierror(999, _("%s: Wrong size for input argument #%d: Single handle expected.\n"), fname, 1);
141         return 0;
142     }
143 
144     iObjUID = getObjectFromHandle((long int) * stackPointer);
145 
146     if (iObjUID == 0)
147     {
148         Scierror(999, _("%s: Wrong type for input argument #%d: Figure or Axes handle expected.\n"), fname, 1);
149         return 0;
150     }
151 
152     getGraphicObjectProperty(iObjUID, __GO_TYPE__, jni_int, (void **)&piType);
153 
154     if (iType != __GO_FIGURE__ && iType != __GO_AXES__)
155     {
156         Scierror(999, _("%s: Wrong type for input argument #%d: Figure or Axes handle expected.\n"), fname, 1);
157         return 0;
158     }
159 
160     return iObjUID;
161 
162 
163 }
164 /*--------------------------------------------------------------------------*/
sci_zoom_rect(char * fname,void * pvApiCtx)165 int sci_zoom_rect(char *fname, void *pvApiCtx)
166 {
167     int iFigureUID = 0;
168     int* piChildrenUID = NULL;
169     int iChildrenCount = 0;
170     int* childrencount = &iChildrenCount;
171     int iHidden = 0;
172     int *piHidden = &iHidden;
173     int i = 0;
174 
175     CheckInputArgument(pvApiCtx, 0, 2);
176     CheckOutputArgument(pvApiCtx, 0, 1);
177     if (nbInputArgument(pvApiCtx) == 0)
178     {
179         /* zoom_rect() */
180         iFigureUID = getCurrentFigure();
181         if (iFigureUID == 0)
182         {
183             iFigureUID = createNewFigureWithAxes();
184         }
185         startInteractiveZoom(iFigureUID);
186     }
187     else if (nbInputArgument(pvApiCtx) == 1)
188     {
189         /* zoom_rect([xmin,ymin,xmax,ymax]) or zoom_rect(handle) */
190         /* with handle a figure or subwindow */
191         if (checkInputArgumentType(pvApiCtx, 1, sci_handles))
192         {
193             int iZoomedObject = getZoomedObject(pvApiCtx, fname);
194             if (iZoomedObject == 0)
195             {
196                 return -1;
197             }
198             startInteractiveZoom(iZoomedObject);
199         }
200         else if (checkInputArgumentType(pvApiCtx, 1, sci_matrix))
201         {
202             double rect[4];
203             if (getZoomRect(pvApiCtx, fname, 1, rect))
204             {
205                 /* rectangle found */
206                 //int status = sciZoom2D(getCurrentSubWin(), rect);
207                 int status = 0;
208                 iFigureUID = getCurrentFigure();
209 
210                 getGraphicObjectProperty(iFigureUID, __GO_CHILDREN_COUNT__, jni_int, (void **)&childrencount);
211 
212                 getGraphicObjectProperty(iFigureUID, __GO_CHILDREN__, jni_int_vector, (void **)&piChildrenUID);
213 
214                 if (childrencount && piChildrenUID)
215                 {
216                     for (i = 0; i < childrencount[0]; ++i)
217                     {
218                         getGraphicObjectProperty(piChildrenUID[i], __GO_HIDDEN__, jni_bool, (void **)&piHidden);
219                         if (iHidden == 0)
220                         {
221                             status = sciZoom2D(piChildrenUID[i], rect);
222                         }
223                     }
224                 }
225                 else
226                 {
227                     status = SET_PROPERTY_ERROR;
228                 }
229                 if (status == SET_PROPERTY_ERROR)
230                 {
231                     /* error on rectangle bounds */
232                     Scierror(999, _("%s: Wrong value for input argument #%d: Specified bounds are not correct.\n"), fname, 1);
233                     return -1;
234                 }
235             }
236             else
237             {
238                 /* error on rectagle definition */
239                 return -1;
240             }
241         }
242         else
243         {
244             Scierror(999, _("%s: Wrong type for input argument #%d: Handle or vector of double expected.\n"), fname, 1);
245             return 0;
246         }
247     }
248     else if (nbInputArgument(pvApiCtx) == 2)
249     {
250         /* zoom_rect(handle, [xmin,ymin,xmax,ymax]) */
251 
252         double rect[4];
253         int iZoomedObject = 0;
254 
255         if ((!checkInputArgumentType(pvApiCtx, 1, sci_handles)) || (!checkInputArgumentType(pvApiCtx, 2, sci_matrix)))
256         {
257             Scierror(999, _("%s: Wrong type for input arguments: Handle or vector of double expected.\n"), fname);
258             return -1;
259         }
260 
261         iZoomedObject = getZoomedObject(pvApiCtx, fname);
262         if (iZoomedObject == 0 || !getZoomRect(pvApiCtx, fname, 2, rect))
263         {
264             return -1;
265         }
266 
267         if (sciZoomRect(iZoomedObject, rect) == SET_PROPERTY_ERROR)
268         {
269             /* error on rectangle bounds */
270             Scierror(999, _("%s: Error on input argument #%d: Specified bounds are not correct.\n"), fname, 1);
271             return -1;
272         }
273     }
274 
275     AssignOutputVariable(pvApiCtx, 1) = 0;
276     ReturnArguments(pvApiCtx);
277     return 0;
278 }
279 /*--------------------------------------------------------------------------*/
280 
281