2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - INRIA - Jean-Baptiste Silvy
4 * Copyright (C) 2012 - DIGITEO - Vincent COUVERT
5 * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /*------------------------------------------------------------------------*/
16 /* file: sci_rubberbox.c */
17 /* desc : interface for rubberbox routine */
18 /*------------------------------------------------------------------------*/
19 #include "gw_graphics.h"
21 #include "localization.h"
22 #include "getPropertyAssignedValue.h"
24 #include "JavaInteraction.h"
25 #include "HandleManagement.h"
26 #include "BuildObjects.h"
27 #include "CurrentSubwin.h"
28 #include "getGraphicObjectProperty.h"
29 #include "graphicObjectProperties.h"
31 /*--------------------------------------------------------------------------*/
32 static int getInitialRectangle(double initRect[4]);
33 static int getEditionMode(int rhsPos);
34 static int returnRectAndButton(const double *_piJavaValues, int _iSelectedRectSize);
35 /*--------------------------------------------------------------------------*/
36 static int getInitialRectangle(double initRect[4])
40 int rectStackPointer = 0;
45 /* Initial rect is always in first position */
46 GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &rectNbRow, &rectNbCol, &rectStackPointer);
48 /* We have found a matrix of double within the parameters check its dims */
49 nbDims = rectNbCol * rectNbRow;
51 if (nbDims != 2 && nbDims != 4)
57 /* pointer on the stack */
58 rect = getDoubleMatrixFromStack(rectStackPointer);
61 for (i = 0; i < 4; i++)
66 /* Set specified values */
67 for (i = 0; i < nbDims; i++)
69 initRect[i] = rect[i];
73 /*--------------------------------------------------------------------------*/
74 static int getEditionMode(int rhsPos)
80 GetRhsVar(rhsPos, MATRIX_OF_BOOLEAN_DATATYPE, &nbRow, &nbCol, &stackPointer);
82 if (nbRow * nbCol != 1)
87 isEditionModeOn = *istk(stackPointer);
98 /*--------------------------------------------------------------------------*/
99 static int returnRectAndButton(const double *_piJavaValues, int _iSelectedRectSize)
103 int rectStackPointer = 0;
107 // button < 0 means user cancelled selection so return [] or [], []
108 if (_piJavaValues[0] < 0)
110 CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &rectStackPointer);
114 CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &rectStackPointer);
121 /* return rectangle */
122 CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &one, &_iSelectedRectSize, &rectStackPointer);
123 for ( i = 0; i < _iSelectedRectSize / 2; i++)
125 j = i + _iSelectedRectSize / 2;
126 stk(rectStackPointer)[i] = _piJavaValues[i + 1];
127 stk(rectStackPointer)[j] = _piJavaValues[i + 4];
134 int buttonStackPointer = 0;
135 CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &one, &one, &buttonStackPointer);
136 *stk(buttonStackPointer) = _piJavaValues[0];
143 /*--------------------------------------------------------------------------*/
144 int sci_rubberbox(char * fname, unsigned long fname_len)
147 /* [final_rect, btn] = rubberbox([initial_rect],[edition_mode]) */
150 double initialRect[4] = {0.0, 0.0, 0.0, 0.0};
151 int initialRectSize = 0;
153 double *piJavaValues = NULL;
155 char *pSubwinUID = getOrCreateDefaultSubwin();
157 int* piView = &iView;
159 BOOL bClickMode = TRUE;
165 getGraphicObjectProperty(pSubwinUID, __GO_VIEW__, jni_int, &piView);
166 getGraphicObjectProperty(pSubwinUID, __GO_PARENT__, jni_string, &pFigureUID);
176 // Check we are running 2D view rubberbox,
177 // Otherwise initial_rect and edition_mode are not usable.
178 if (iView == 1) // 3D
180 Scierror(999, _("%s: Can not run rubberbox on a 3D view with initial_rect or edition_mode option. See help for more information.\n"), fname);
183 /* rubberbox(initial_rect) or rubberbox(edition_mode) */
184 if (GetType(1) == sci_matrix)
186 /* rubberbox(initial_rect) */
187 if (getInitialRectangle(initialRect) == 1)
194 Scierror(999, _("%s: Wrong size for input argument #%d: Vector of size %d or %d expected.\n"), fname, 1, 2, 4);
198 else if (GetType(1) == sci_boolean)
200 /* rubberbox(editionMode) */
201 int editionModeStatus = getEditionMode(1);
203 if (editionModeStatus == 1)
208 else if (editionModeStatus == 0)
215 Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected\n."), fname, 1);
221 /* Wrong parameter specified, neither edition mode nor intial rect */
222 Scierror(999, _("%s: Wrong type for input argument #%d: Real row vector or a boolean expected.\n"), fname, 1);
228 /* rubberbox(initial_rect, edition_mode) */
230 /* Default values, intial rect and edition mode to false */
231 double initialRect[4] = {0.0, 0.0, 0.0, 0.0};
232 int editionModeStatus;
234 // Check we are running 2D view rubberbox,
235 // Otherwise initial_rect and edition_mode are not usable.
236 if (iView == 1) // 3D
238 Scierror(999, _("%s: Can not run rubberbox on a 3D view with initial_rect or edition_mode option. See help for more information.\n"), fname);
242 if (GetType(1) != sci_matrix)
244 Scierror(999, _("%s: Wrong type for input argument #%d: Real row vector expected.\n"), fname, 1);
248 if (GetType(2) != sci_boolean)
250 Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2);
254 /* Getting initial rect */
255 if (getInitialRectangle(initialRect) != 1)
257 Scierror(999, _("%s: Wrong size for input argument #%d: Vector of size %d or %d expected.\n"), fname, 1, 2, 4);
261 /* Getting edition mode */
262 editionModeStatus = getEditionMode(2);
264 if (editionModeStatus == 1)
266 /* rubberbox(initial_rect, %t) */
268 ** Force click mode !!! Don't know to drag
269 ** if first point is set using init rect
273 else if (editionModeStatus == 0)
275 /* rubberbox(initial_rect, %f) */
280 Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected\n."), fname, 2);
287 if (bClickMode == TRUE)
289 piJavaValues = javaClickRubberBox(pFigureUID, initialRect, initialRectSize);
293 piJavaValues = javaDragRubberBox(pFigureUID);
295 /* Put values into the stack and return */
298 return returnRectAndButton(piJavaValues, 6);
302 return returnRectAndButton(piJavaValues, 4);
306 /*--------------------------------------------------------------------------*/