1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Vincent COUVERT (java version)
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #include "gw_gui.h"
17 #include "api_scilab.h"
18 #include "localization.h"
19 #include "CallMessageBox.h"
20 #include "Scierror.h"
21 #include "getPropertyAssignedValue.h"
22 #include "os_string.h"
23 #include "messageboxoptions.h"
24 
25 void freeVariable(int _iRows, int _iCols, char** _pstData, char** _pstData1, int _iRows2, int _iCols2, char** _pstData2, char** _pstData3);
26 
27 /*--------------------------------------------------------------------------*/
sci_messagebox(char * fname,void * pvApiCtx)28 int sci_messagebox(char *fname, void* pvApiCtx)
29 {
30     SciErr sciErr;
31 
32     int* piAddrmessageAdr       = NULL;
33     int* piAddrtitleAdr         = NULL;
34     int* piAddriconAdr          = NULL;
35     int* piAddrbuttonsTextAdr   = NULL;
36     int* piAddrmodalOptionAdr   = NULL;
37     double* buttonNumberAdr     = NULL;
38 
39     int messageBoxID = 0;
40 
41     /* Used to read input arguments */
42     int nbRow = 0, nbCol = 0;
43     int nbRowButtons = 0, nbColButtons = 0;
44     int nbRowMessage = 0, nbColMessage = 0;
45 
46     char **buttonsTextAdr   = 0;
47     char **messageAdr       = 0;
48     char **titleAdr         = 0;
49     char **modalOptionAdr   = 0;
50     char **iconAdr          = 0;
51 
52     /* Used to write output argument */
53     int buttonNumber = 0;
54 
55     CheckInputArgument(pvApiCtx, 1, 5);
56     CheckOutputArgument(pvApiCtx, 0, 1);
57 
58     /* Message to be displayed */
59     if ((checkInputArgumentType(pvApiCtx, 1, sci_strings)))
60     {
61         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrmessageAdr);
62         if (sciErr.iErr)
63         {
64             printError(&sciErr, 0);
65             return 1;
66         }
67 
68         // Retrieve a matrix of string at position 1.
69         if (getAllocatedMatrixOfString(pvApiCtx, piAddrmessageAdr, &nbRowMessage, &nbColMessage, &messageAdr))
70         {
71             Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 1);
72             return 1;
73         }
74     }
75     else
76     {
77         Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
78         return FALSE;
79     }
80 
81     /* Title to be displayed */
82     if (nbInputArgument(pvApiCtx) >= 2)
83     {
84         if (checkInputArgumentType(pvApiCtx, 2, sci_strings))
85         {
86             sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrtitleAdr);
87             if (sciErr.iErr)
88             {
89                 printError(&sciErr, 0);
90                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
91                 return 1;
92             }
93 
94             // Retrieve a matrix of string at position 2.
95             if (getAllocatedMatrixOfString(pvApiCtx, piAddrtitleAdr, &nbRow, &nbCol, &titleAdr))
96             {
97                 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
98                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
99                 return 1;
100             }
101 
102             if (nbRow * nbCol != 1)
103             {
104                 Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 2);
105                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
106                 freeAllocatedMatrixOfString(nbRow, nbCol, titleAdr);
107                 return FALSE;
108             }
109             /* The title argument can be used to give the modal option */
110             if (isModalOption(titleAdr[0]))
111             {
112                 modalOptionAdr = titleAdr;
113                 titleAdr = NULL;
114             }
115         }
116         else
117         {
118             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
119             freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
120             return FALSE;
121         }
122     }
123 
124     /* Icon to be displayed */
125     if (nbInputArgument(pvApiCtx) >= 3)
126     {
127         if ((checkInputArgumentType(pvApiCtx, 3, sci_strings)))
128         {
129             sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddriconAdr);
130             if (sciErr.iErr)
131             {
132                 printError(&sciErr, 0);
133                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
134                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
135                 return 1;
136             }
137 
138             // Retrieve a matrix of string at position 3.
139             if (getAllocatedMatrixOfString(pvApiCtx, piAddriconAdr, &nbRow, &nbCol, &iconAdr))
140             {
141                 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3);
142                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
143                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
144                 return 1;
145             }
146 
147             if (nbRow * nbCol == 1)
148             {
149                 /* The icon argument can be used to give the modal option or the buttons names */
150                 if (isModalOption(iconAdr[0]))
151                 {
152                     modalOptionAdr = (char **)iconAdr;
153                     iconAdr = NULL;
154                 }
155                 else if (!isIconName(iconAdr[0]))
156                 {
157                     buttonsTextAdr = (char **)iconAdr;
158                     nbRowButtons = nbRow;
159                     nbColButtons = nbCol;
160                     iconAdr = NULL;
161                     nbRow = 1;
162                     nbCol = 1;
163                 }
164             }
165             else  /* More than one string --> buttons names */
166             {
167                 buttonsTextAdr = (char **)iconAdr;
168                 nbRowButtons = nbRow;
169                 nbColButtons = nbCol;
170                 iconAdr = NULL;
171                 nbRow = 1;
172                 nbCol = 1;
173             }
174         }
175         else
176         {
177             Scierror(999, _("%s: Wrong type for input argument #%d: string or string vector expected.\n"), fname, 3);
178             freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
179             freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
180             return FALSE;
181         }
182     }
183 
184     /* Buttons names */
185     if (nbInputArgument(pvApiCtx) >= 4)
186     {
187         if ((checkInputArgumentType(pvApiCtx, 4, sci_strings)))
188         {
189             sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddrbuttonsTextAdr);
190             if (sciErr.iErr)
191             {
192                 printError(&sciErr, 0);
193                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
194                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
195                 return 1;
196             }
197 
198             // Retrieve a matrix of string at position 4.
199             if (getAllocatedMatrixOfString(pvApiCtx, piAddrbuttonsTextAdr, &nbRowButtons, &nbColButtons, &buttonsTextAdr))
200             {
201                 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 4);
202                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
203                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
204                 return 1;
205             }
206 
207             if (nbRowButtons * nbColButtons == 1)
208             {
209                 /* The buttons names argument can be used to give the modal option */
210                 if (isModalOption(buttonsTextAdr[0]))
211                 {
212                     modalOptionAdr = buttonsTextAdr;
213                     buttonsTextAdr = NULL;
214                 }
215             }
216         }
217         else
218         {
219             Scierror(999, _("%s: Wrong type for input argument #%d: string or string vector expected.\n"), fname, 3);
220             freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
221             freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
222             return FALSE;
223         }
224     }
225 
226     /* Modal option */
227     if (nbInputArgument(pvApiCtx) == 5)
228     {
229         if ((checkInputArgumentType(pvApiCtx, 5, sci_strings)))
230         {
231             sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddrmodalOptionAdr);
232             if (sciErr.iErr)
233             {
234                 printError(&sciErr, 0);
235                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
236                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
237                 return 1;
238             }
239 
240             // Retrieve a matrix of string at position 5.
241             if (getAllocatedMatrixOfString(pvApiCtx, piAddrmodalOptionAdr, &nbRow, &nbCol, &modalOptionAdr))
242             {
243                 Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 5);
244                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
245                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
246                 return 1;
247             }
248 
249             if (nbRow * nbCol != 1)
250             {
251                 Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 5);
252                 freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
253                 freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
254                 return FALSE;
255             }
256         }
257         else
258         {
259             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 5);
260             freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
261             freeVariable(nbRow, nbCol, titleAdr, iconAdr, nbRowButtons, nbColButtons, buttonsTextAdr, modalOptionAdr);
262             return FALSE;
263         }
264     }
265     /* Create the Java Object */
266     messageBoxID = createMessageBox();
267 
268     /* Message */
269     setMessageBoxMultiLineMessage(messageBoxID, messageAdr, nbColMessage * nbRowMessage);
270     freeAllocatedMatrixOfString(nbRowMessage, nbColMessage, messageAdr);
271 
272     /* Title */
273     if (titleAdr != NULL)
274     {
275         setMessageBoxTitle(messageBoxID, titleAdr[0]);
276         freeAllocatedMatrixOfString(nbRow, nbCol, titleAdr);
277     }
278     else
279     {
280         setMessageBoxTitle(messageBoxID, _("Scilab Message"));
281     }
282 
283     /* Icon */
284     if (iconAdr != NULL)
285     {
286         setMessageBoxIcon(messageBoxID, iconAdr[0]);
287         freeAllocatedMatrixOfString(nbRow, nbCol, iconAdr);
288     }
289 
290     /* Buttons */
291     if (buttonsTextAdr != NULL)
292     {
293         setMessageBoxButtonsLabels(messageBoxID, buttonsTextAdr, nbColButtons * nbRowButtons);
294         freeAllocatedMatrixOfString(nbRowButtons, nbColButtons, buttonsTextAdr);
295     }
296 
297     /* Modal ? */
298     if (modalOptionAdr != NULL)
299     {
300         setMessageBoxModal(messageBoxID, !stricmp(modalOptionAdr[0], "modal"));
301         freeAllocatedMatrixOfString(nbRow, nbCol, modalOptionAdr);
302     }
303     else
304     {
305         setMessageBoxModal(messageBoxID, FALSE);
306     }
307 
308     /* Display it and wait for a user input */
309     messageBoxDisplayAndWait(messageBoxID);
310 
311     /* Return the index of the button selected */
312     if (nbOutputArgument(pvApiCtx) <= 1)
313     {
314         /* Read the user answer */
315         buttonNumber = getMessageBoxSelectedButton(messageBoxID);
316 
317         nbRow = 1;
318         nbCol = 1;
319 
320         sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &buttonNumberAdr);
321         if (sciErr.iErr)
322         {
323             printError(&sciErr, 0);
324             Scierror(999, _("%s: Memory allocation error.\n"), fname);
325             return 1;
326         }
327 
328         buttonNumberAdr[0] = buttonNumber;
329 
330         AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
331     }
332     else
333     {
334         AssignOutputVariable(pvApiCtx, 1) = 0;
335     }
336 
337     ReturnArguments(pvApiCtx);
338     return TRUE;
339 }
340 /*--------------------------------------------------------------------------*/
341 
freeVariable(int _iRows,int _iCols,char ** _pstData,char ** _pstData1,int _iRows2,int _iCols2,char ** _pstData2,char ** _pstData3)342 void freeVariable(int _iRows, int _iCols, char** _pstData, char** _pstData1, int _iRows2, int _iCols2, char** _pstData2, char** _pstData3)
343 {
344     // tilteAdr
345     if (_pstData != NULL)
346     {
347         freeAllocatedMatrixOfString(_iRows, _iCols, _pstData);
348     }
349 
350     // iconAdr
351     if (_pstData1 != NULL)
352     {
353         freeAllocatedMatrixOfString(_iRows, _iCols, _pstData1);
354     }
355 
356     // buttonsTextAdr
357     if (_pstData2 != NULL)
358     {
359         freeAllocatedMatrixOfString(_iRows2, _iCols2, _pstData2);
360     }
361 
362     // modalOptionAdr
363     if (_pstData3 != NULL)
364     {
365         freeAllocatedMatrixOfString(_iRows, _iCols, _pstData3);
366     }
367 }