2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2008 - INRIA - Vincent COUVERT (Java implementation)
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
20 #include "sci_malloc.h"
21 #include "api_scilab.h"
22 #include "localization.h"
23 #include "CallMessageBox.h"
24 #include "getPropertyAssignedValue.h"
26 #include "freeArrayOfString.h"
29 /*--------------------------------------------------------------------------*/
30 int sci_xchoicesi(char *fname, void* pvApiCtx)
34 int* piAddrdefaultValuesAdr = NULL;
35 int* piAddrlabelsAdr = NULL;
36 int* piAddrlineLabelsAdr = NULL;
37 double* emptyMatrixAdr = NULL;
39 int nbRow = 0, nbCol = 0;
40 int nbRowDefaultValues = 0, nbColDefaultValues = 0;
41 int nbRowLineLabels = 0, nbColLineLabels = 0;
45 char **labelsAdr = NULL;
46 char **lineLabelsAdr = NULL;
47 double *defaultValues = NULL;
48 int *defaultValuesInt = NULL;
50 int userValueSize = 0;
51 int *userValue = NULL;
52 double *userValueDouble = NULL;
56 CheckInputArgument(pvApiCtx, 3, 3);
57 CheckOutputArgument(pvApiCtx, 0, 1);
59 /* READ THE DEFAULT VALUES */
60 if (checkInputArgumentType(pvApiCtx, 1, sci_matrix))
62 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrdefaultValuesAdr);
65 printError(&sciErr, 0);
69 // Retrieve a matrix of double at position 1.
70 sciErr = getMatrixOfDouble(pvApiCtx, piAddrdefaultValuesAdr, &nbRowDefaultValues, &nbColDefaultValues, &defaultValues);
73 printError(&sciErr, 0);
74 Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
78 defaultValuesInt = (int *)MALLOC(nbRowDefaultValues * nbColDefaultValues * sizeof(int));
79 for (K = 0; K < nbRowDefaultValues * nbColDefaultValues; K++)
81 defaultValuesInt[K] = (int)defaultValues[K];
86 Scierror(999, _("%s: Wrong type for input argument #%d: Real or complex vector expected.\n"), fname, 1);
90 /* READ THE MESSAGE */
91 if ((checkInputArgumentType(pvApiCtx, 2, sci_strings)))
93 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrlabelsAdr);
96 printError(&sciErr, 0);
97 FREE(defaultValuesInt);
101 // Retrieve a matrix of string at position 2.
102 if (getAllocatedMatrixOfString(pvApiCtx, piAddrlabelsAdr, &nbRow, &nbCol, &labelsAdr))
104 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
105 FREE(defaultValuesInt);
112 Scierror(999, _("%s: Wrong type for input argument #%d: Vector of strings expected.\n"), fname, 2);
113 FREE(defaultValuesInt);
117 /* Create the Java Object */
118 messageBoxID = createMessageBox();
120 /* Title is a default title */
121 setMessageBoxTitle(messageBoxID, _("Scilab Choices Request"));
124 setMessageBoxMultiLineMessage(messageBoxID, labelsAdr, nbCol * nbRow);
125 freeAllocatedMatrixOfString(nbRow, nbCol, labelsAdr);
127 /* READ THE LABELS */
128 if (checkInputArgumentType(pvApiCtx, 3, sci_strings))
130 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrlineLabelsAdr);
133 printError(&sciErr, 0);
134 FREE(defaultValuesInt);
138 // Retrieve a matrix of string at position 3.
139 if (getAllocatedMatrixOfString(pvApiCtx, piAddrlineLabelsAdr, &nbRowLineLabels, &nbColLineLabels, &lineLabelsAdr))
141 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3);
142 FREE(defaultValuesInt);
146 if (nbRow != 1 && nbCol != 1)
148 Scierror(999, _("%s: Wrong size for input argument #%d: Vector of strings expected.\n"), fname, 3);
149 freeAllocatedMatrixOfString(nbRowLineLabels, nbColLineLabels, lineLabelsAdr);
150 FREE(defaultValuesInt);
153 setMessageBoxLineLabels(messageBoxID, lineLabelsAdr, nbColLineLabels * nbRowLineLabels);
154 freeAllocatedMatrixOfString(nbRowLineLabels, nbColLineLabels, lineLabelsAdr);
158 Scierror(999, _("%s: Wrong type for input argument #%d: Vector of strings expected.\n"), fname, 3);
159 FREE(defaultValuesInt);
163 /* Default selected buttons */
164 setMessageBoxDefaultSelectedButtons(messageBoxID, defaultValuesInt, nbRowDefaultValues * nbColDefaultValues);
165 FREE(defaultValuesInt);
167 /* Display it and wait for a user input */
168 messageBoxDisplayAndWait(messageBoxID);
170 /* Read the user answer */
171 userValueSize = getMessageBoxValueSize(messageBoxID);
172 if (userValueSize == 0)
177 sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &emptyMatrixAdr);
180 printError(&sciErr, 0);
181 Scierror(999, _("%s: Memory allocation error.\n"), fname);
187 userValue = (int*)getMessageBoxUserSelectedButtons(messageBoxID);
189 userValueDouble = (double *)MALLOC(nbRowDefaultValues * nbColDefaultValues * sizeof(double));
190 for (K = 0; K < nbRowDefaultValues * nbColDefaultValues; K++)
192 userValueDouble[K] = userValue[K];
195 sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRowDefaultValues, nbColDefaultValues, userValueDouble);
198 printError(&sciErr, 0);
199 Scierror(999, _("%s: Memory allocation error.\n"), fname);
206 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
207 ReturnArguments(pvApiCtx);
210 /*--------------------------------------------------------------------------*/