2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA - Allan CORNET
4 * Copyright (C) 2007 - INRIA - Vincent COUVERT
5 * Copyright (C) 2010 - DIGITEO - Yann COLLETTE
6 * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
8 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at
12 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
17 /*--------------------------------------------------------------------------*/
18 #include "CreateUIControl.h"
19 #include "HandleManagement.h"
20 #include "MALLOC.h" /* MALLOC */
21 #include "localization.h"
24 #include "SetPropertyStatus.h"
25 #include "SetHashTable.h"
27 #include "FigureList.h" /* getFigureFromIndex */
28 #include "Widget.h" /* requestWidgetFocus */
29 #include "freeArrayOfString.h"
30 #include "setGraphicObjectProperty.h"
31 #include "getGraphicObjectProperty.h"
32 #include "graphicObjectProperties.h"
33 #include "CurrentFigure.h"
34 #include "BuildObjects.h"
35 #include "api_scilab.h"
36 /*--------------------------------------------------------------------------*/
37 #define NBPROPERTIES 28
38 #define MAXPROPERTYNAMELENGTH 20
39 /*--------------------------------------------------------------------------*/
40 int sci_uicontrol(char *fname, unsigned long fname_len)
42 int nbRow = 0, nbCol = 0, stkAdr = 0, k = 0;
43 char **stkAdrForStrings = NULL;
45 int setStatus = SET_PROPERTY_SUCCEED;
49 int inputIndex = 0, beginIndex = 0;
51 char *propertyName = NULL;
52 char *styleProperty = NULL;
54 char *pParentUID = NULL;
55 char *pUicontrol = NULL;
57 unsigned long GraphicHandle = 0;
59 int found = 0; /* Does the property exists ? */
61 /* @TODO remove this crappy initialization */
62 /* DO NOT CHANGE ORDER !! */
63 char propertiesNames[NBPROPERTIES][MAXPROPERTYNAMELENGTH] =
65 "style", "parent", "backgroundcolor", "foregroundcolor", "string", "units", "fontweight", "min", "max", "tag", "position", "relief",
66 "horizontalalignment", "verticalalignment", "sliderstep", "fontname", "callback", "fontangle", "fontunits", "fontsize", "listboxtop", "user_data", "value", "userdata", "visible", "enable",
67 "callback_type", "tooltipstring"
69 int *propertiesValuesIndices = NULL;
71 char *propertyPart = NULL;
74 int *piParentType = &iParentType;
75 int iParentStyle = -1;
76 int *piParentStyle = &iParentStyle;
77 char const* pstCurrentFigure = NULL;
83 /* Create a pushbutton in current figure */
85 /* Create a new pushbutton */
86 GraphicHandle = getHandle(CreateUIControl(NULL));
88 /* Set current figure as parent */
89 pstCurrentFigure = getCurrentFigure();
90 if (pstCurrentFigure == NULL)
92 pstCurrentFigure = createNewFigureWithAxes();
94 setGraphicObjectRelationship(pstCurrentFigure, (char*)getObjectFromHandle(GraphicHandle));
95 pUicontrol = (char*)getObjectFromHandle(GraphicHandle);
99 /* Create a pushbutton in figure given as parameter */
100 /* Or give focus to the uicontrol given as parameter */
102 if (VarType(1) != sci_handles)
105 C2F(overload) (&lw, "uicontrol", 9);
108 else /* Get parent ID */
110 GetRhsVar(1, GRAPHICAL_HANDLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
112 if (nbRow * nbCol != 1)
114 Scierror(999, _("%s: Wrong size for input argument #%d: A graphic handle expected.\n"), fname, 1);
117 pParentUID = (char*)getObjectFromHandle((long) * hstk(stkAdr));
118 if (pParentUID != NULL)
120 getGraphicObjectProperty(pParentUID, __GO_TYPE__, jni_int, (void **)&piParentType);
121 if (iParentType == __GO_UICONTROL__) /* Focus management */
123 GraphicHandle = (long) * hstk(stkAdr);
124 requestFocus(pParentUID);
126 else if (iParentType == __GO_FIGURE__ || iParentType == __GO_UIMENU__) /* PushButton creation */
128 /* Create a new pushbutton */
129 GraphicHandle = getHandle(CreateUIControl(NULL));
131 /* First parameter is the parent */
132 setGraphicObjectRelationship(pParentUID, getObjectFromHandle(GraphicHandle));
133 setStatus = callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), stkAdr, sci_handles, nbRow, nbCol, (char *)propertiesNames[1]);
134 if (setStatus == SET_PROPERTY_ERROR)
136 Scierror(999, _("%s: Could not set property '%s'.\n"), fname, propertyName);
142 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s', '%s' or '%s' handle expected.\n"), fname, 1, "Uicontrol",
149 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s', '%s' or '%s' handle expected.\n"), fname, 1, "Uicontrol", "Figure",
153 pUicontrol = (char*)getObjectFromHandle(GraphicHandle);
158 if ((VarType(1) != sci_handles) && (VarType(1) != sci_strings))
161 C2F(overload) (&lw, "uicontrol", 9);
165 /* Allocate memory to store the position of properties in uicontrol call */
166 if ((propertiesValuesIndices = (int *)MALLOC(sizeof(int) * NBPROPERTIES)) == NULL)
168 Scierror(999, _("%s: No more memory.\n"), fname);
172 /* Init all positions to NOT_FOUND */
173 for (inputIndex = 0; inputIndex < NBPROPERTIES; inputIndex++)
175 propertiesValuesIndices[inputIndex] = NOT_FOUND; /* Property initialized as not found */
179 * Odd number of input arguments
180 * First input is the parent ID
181 * All event inputs are property names
182 * All odd (except first) inputs are property values
186 if (VarType(1) != sci_handles)
188 if (VarType(1) == sci_matrix)
190 GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
191 if (nbRow * nbCol == 1)
193 pParentUID = (char*)getFigureFromIndex((int)(*stk(stkAdr)));
195 if (pParentUID == NULL)
197 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
201 getGraphicObjectProperty(pParentUID, __GO_TYPE__, jni_int, (void **)&piParentType);
202 if (iParentType != __GO_FIGURE__)
204 getGraphicObjectProperty(pParentUID, __GO_STYLE__, jni_int, (void **)&piParentStyle);
205 if (iParentType != __GO_UICONTROL__ || iParentStyle != __GO_UI_FRAME__)
207 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
212 /* First parameter is the parent */
213 propertiesValuesIndices[1] = 1;
217 Scierror(999, _("%s: Wrong size for input argument #%d: A graphic handle expected.\n"), fname, 1);
223 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
228 else /* Get parent ID */
230 GetRhsVar(1, GRAPHICAL_HANDLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
232 if (nbRow * nbCol != 1)
234 Scierror(999, _("%s: Wrong size for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
238 pParentUID = (char*)getObjectFromHandle((long) * hstk(stkAdr));
239 if (pParentUID == NULL)
241 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
245 getGraphicObjectProperty(pParentUID, __GO_TYPE__, jni_int, (void **)&piParentType);
246 if (iParentType != __GO_FIGURE__)
248 getGraphicObjectProperty(pParentUID, __GO_STYLE__, jni_int, (void **)&piParentStyle);
249 if (iParentType != __GO_UICONTROL__ || iParentStyle != __GO_UI_FRAME__)
251 Scierror(999, _("%s: Wrong type for input argument #%d: A '%s' or a '%s' handle expected.\n"), fname, 1, "Figure",
256 /* First parameter is the parent */
257 propertiesValuesIndices[1] = 1;
260 // First input parameter which is a property name
264 * Even number of input arguments
265 * All odd inputs are property names
266 * All even inputs are property values
270 // First input parameter which is a property name
274 /* Get all properties positions */
275 for (inputIndex = beginIndex; inputIndex < Rhs; inputIndex = inputIndex + 2)
277 /* Read property name */
278 if (VarType(inputIndex) != sci_strings)
280 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, inputIndex);
285 GetRhsVar(inputIndex, STRING_DATATYPE, &nbRow, &nbCol, &stkAdr);
286 propertyName = cstk(stkAdr);
289 /* We only want to compare propertyName along its length */
290 /* 'posi' must be matched to 'position' */
291 propertyPart = (char *)MALLOC(sizeof(char) * (MAXPROPERTYNAMELENGTH + 1));
292 if (propertyPart == NULL)
294 Scierror(999, _("%s: No more memory.\n"), fname);
299 for (k = 0; k < NBPROPERTIES; k++)
301 if (strlen(propertyName) <= MAXPROPERTYNAMELENGTH)
303 strncpy(propertyPart, propertiesNames[k], strlen(propertyName));
304 propertyPart[strlen(propertyName)] = '\0';
306 if (stricmp(propertyName, propertyPart) == 0)
308 propertiesValuesIndices[k] = inputIndex + 1; /* Position of value for property */
319 Scierror(999, _("%s: Unknown property: %s for '%s' handles.\n"), fname, propertyName, "Uicontrol");
325 if (propertiesValuesIndices[0] != NOT_FOUND) /* Style found */
327 if (VarType(propertiesValuesIndices[0]) == sci_strings)
329 GetRhsVar(propertiesValuesIndices[0], STRING_DATATYPE, &nbRow, &nbCol, &stkAdr);
330 styleProperty = cstk(stkAdr);
334 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, propertiesValuesIndices[0]);
339 /* Create a new uicontrol */
340 pUicontrol = CreateUIControl(styleProperty);
341 if (pUicontrol == NULL) /* Error in creation */
343 Scierror(999, _("%s: Could not create 'Uicontrol' handle.\n"), fname);
346 GraphicHandle = getHandle(pUicontrol);
348 /* If no parent given then the current figure is the parent */
349 if (propertiesValuesIndices[1] == NOT_FOUND)
351 char *graphicObjectUID = (char*)getObjectFromHandle(GraphicHandle);
354 pstCurrentFigure = (char*)getCurrentFigure();
356 if (pstCurrentFigure == NULL)
358 pstCurrentFigure = createNewFigureWithAxes();
360 setGraphicObjectRelationship(pstCurrentFigure, graphicObjectUID);
363 /* Read and set all properties */
364 for (inputIndex = 1; inputIndex < NBPROPERTIES; inputIndex++) /* Style has already been set */
366 if (propertiesValuesIndices[inputIndex] != NOT_FOUND)
368 if (inputIndex == 21 || inputIndex == 23) /* User data settings */
370 stkAdr = propertiesValuesIndices[inputIndex]; /* Special management */
374 callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), stkAdr, VarType(propertiesValuesIndices[inputIndex]), nbRow, nbCol,
375 (char *)propertiesNames[inputIndex]);
377 else /* All other properties */
379 /* Read property value */
380 switch (VarType(propertiesValuesIndices[inputIndex]))
383 GetRhsVar(propertiesValuesIndices[inputIndex], MATRIX_OF_DOUBLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
385 callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), stkAdr, sci_matrix, nbRow, nbCol,
386 (char *)propertiesNames[inputIndex]);
389 /* Index for String & TooltipString properties: Can be more than one character string */
390 if ((inputIndex == 4) || (inputIndex == 27))
392 GetRhsVar(propertiesValuesIndices[inputIndex], MATRIX_OF_STRING_DATATYPE, &nbRow, &nbCol, &stkAdrForStrings);
394 callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), (size_t) stkAdrForStrings, sci_strings, nbRow, nbCol,
395 (char *)propertiesNames[inputIndex]);
396 freeArrayOfString(stkAdrForStrings, nbRow * nbCol);
400 GetRhsVar(propertiesValuesIndices[inputIndex], STRING_DATATYPE, &nbRow, &nbCol, &stkAdr);
402 callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), stkAdr, sci_strings, nbRow, nbCol,
403 (char *)propertiesNames[inputIndex]);
407 GetRhsVar(propertiesValuesIndices[inputIndex], GRAPHICAL_HANDLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
409 callSetProperty(pvApiCtx, (char*)getObjectFromHandle(GraphicHandle), stkAdr, sci_handles, nbRow, nbCol,
410 (char *)propertiesNames[inputIndex]);
413 setStatus = SET_PROPERTY_ERROR;
417 if (setStatus == SET_PROPERTY_ERROR)
419 Scierror(999, _("%s: Could not set property '%s'.\n"), fname, (char *)propertiesNames[inputIndex]);
426 if ((Rhs < 2) || (propertiesValuesIndices[24] == NOT_FOUND)) /* Visible property not set */
428 /* Force the uicontrol to be visible because is invisible by default in the model (See bug #10346) */
430 setGraphicObjectProperty(pUicontrol, __GO_VISIBLE__, &b, jni_bool, 1);
433 if ((Rhs < 2) || (propertiesValuesIndices[14] == NOT_FOUND)) /* SliderStep property not set */
435 /* Set SliderStep property to [1/100*(Max-Min) 1/10*(Max-Min)] */
437 double* pdblMaxValue = &maxValue;
439 double* pdblMinValue = &minValue;
442 getGraphicObjectProperty(pUicontrol, __GO_UI_MIN__, jni_double, (void**) &pdblMinValue);
443 getGraphicObjectProperty(pUicontrol, __GO_UI_MAX__, jni_double, (void**) &pdblMaxValue);
445 pdblStep[0] = 0.01 * (maxValue - minValue);
446 pdblStep[1] = 0.1 * (maxValue - minValue);
448 setGraphicObjectProperty(pUicontrol, __GO_UI_SLIDERSTEP__, pdblStep, jni_double_vector, 2);
451 FREE(propertiesValuesIndices);
453 /* Create return variable */
456 CreateVar(Rhs + 1, GRAPHICAL_HANDLE_DATATYPE, &nbRow, &nbCol, &stkAdr);
457 *hstk(stkAdr) = GraphicHandle;
466 /*--------------------------------------------------------------------------*/