1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2004-2006 - INRIA - Fabrice Leray
4  * Copyright (C) 2006 - INRIA - Allan Cornet
5  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6  * Copyright (C) 2009 - DIGITEO - Pierre Lando
7  * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
8  *
9  * Copyright (C) 2012 - 2016 - Scilab Enterprises
10  *
11  * This file is hereby licensed under the terms of the GNU GPL v2.0,
12  * pursuant to article 5.3.4 of the CeCILL v.2.1.
13  * This file was originally licensed under the terms of the CeCILL v2.1,
14  * and continues to be available under such terms.
15  * For more information, see the COPYING file which you should have received
16  * along with this program.
17  *
18  */
19 
20 /*------------------------------------------------------------------------*/
21 /* file: set_callback_property.c                                          */
22 /* desc : function to modify in Scilab the callback field of              */
23 /*        a handle                                                        */
24 /*------------------------------------------------------------------------*/
25 #include <string.h>
26 #include "setHandleProperty.h"
27 #include "getPropertyAssignedValue.h"
28 #include "Scierror.h"
29 #include "localization.h"
30 #include "SetPropertyStatus.h"
31 #include "graphicObjectProperties.h"
32 #include "setGraphicObjectProperty.h"
33 #include "api_scilab.h"
34 #include "sci_malloc.h"
35 /*------------------------------------------------------------------------*/
set_callback_property(void * _pvCtx,int iObjUID,void * _pvData,int valueType,int nbRow,int nbCol)36 int set_callback_property(void* _pvCtx, int iObjUID, void* _pvData, int valueType, int nbRow, int nbCol)
37 {
38     // Callback must be only one character string
39 
40     BOOL status = FALSE;
41     char * cbString = NULL;
42     int cbType = 0;
43 
44     int strNbRow = 0, strNbCol = 0;
45     int iRows = 0, iCols = 0;
46     double* pdblData = NULL;
47 
48     if (valueType == sci_strings)
49     {
50         if (nbCol != 1)
51         {
52             Scierror(999, _("Wrong size for '%s' property: string expected.\n"), "Callback");
53             return SET_PROPERTY_ERROR;
54         }
55         cbString = (char*)_pvData;
56     }
57     else if (valueType == sci_list)
58     {
59         int iLen = 0;
60         if (nbRow * nbCol != 2)
61         {
62             Scierror(999, _("Wrong size for '%s' property: a 2-item list expected.\n"), "Callback");
63             return SET_PROPERTY_ERROR;
64         }
65 
66         getMatrixOfDoubleInList(_pvCtx, (int*)_pvData, 1, &iRows, &iCols, &pdblData);
67         if (iRows * iCols != 1)
68         {
69             Scierror(999, _("Wrong size for '%s' property: A real expected.\n"), "callback_type");
70             return SET_PROPERTY_ERROR;
71         }
72         else
73         {
74             cbType = (int)pdblData[0];
75         }
76 
77 
78         getMatrixOfStringInList(_pvCtx, (int*)_pvData, 2, &iRows, &iCols, NULL, NULL);
79         if (iRows * iCols != 1)
80         {
81             Scierror(999, _("Wrong size for '%s' property: string expected.\n"), "Callback");
82             return SET_PROPERTY_ERROR;
83         }
84 
85         getMatrixOfStringInList(_pvCtx, (int*)_pvData, 2, &iRows, &iCols, &iLen, NULL);
86         cbString = (char*)MALLOC(sizeof(char) * (iLen + 1));
87         getMatrixOfStringInList(_pvCtx, (int*)_pvData, 2, &iRows, &iCols, &iLen, &cbString);
88     }
89     else
90     {
91 
92         Scierror(999, _("Wrong type for '%s' property: string or 2-item list expected.\n"), "Callback");
93         return SET_PROPERTY_ERROR;
94     }
95 
96     if (strcmp(cbString, "") == 0)
97     {
98         cbType = -1; /* Disabled */
99     }
100 
101     status = setGraphicObjectProperty(iObjUID, __GO_CALLBACK__, cbString, jni_string, 1);
102 
103     if (status != TRUE)
104     {
105         Scierror(999, _("'%s' property does not exist for this handle.\n"), "Callback");
106         return SET_PROPERTY_ERROR;
107     }
108 
109     if (setGraphicObjectProperty(iObjUID, __GO_CALLBACKTYPE__, &cbType, jni_int, 1) == FALSE)
110     {
111         Scierror(999, _("'%s' property does not exist for this handle.\n"), "callback_type");
112         return SET_PROPERTY_ERROR;
113     }
114 
115     return SET_PROPERTY_SUCCEED;
116 }
117 /*------------------------------------------------------------------------*/
118