load: avoid creation of double axes in figure
[scilab.git] / scilab / modules / gui / sci_gateway / cpp / sci_ClipBoard.cpp
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2005 - INRIA - Allan CORNET
4 * Copyright (C) 2008 - INRIA - Vincent COUVERT
5 * Copyright (C) 2010 - DIGITEO - Vincent COUVERT
6 *
7  * Copyright (C) 2012 - 2016 - Scilab Enterprises
8  *
9  * This file is hereby licensed under the terms of the GNU GPL v2.0,
10  * pursuant to article 5.3.4 of the CeCILL v.2.1.
11  * This file was originally licensed under the terms of the CeCILL v2.1,
12  * and continues to be available under such terms.
13  * For more information, see the COPYING file which you should have received
14  * along with this program.
15 *
16 */
17
18 extern "C"
19 {
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "gw_gui.h"
24 #include "api_scilab.h"
25 #include "Scierror.h"
26 #include "sci_malloc.h"
27 #include "sciprint.h"
28 #include "configvariable_interface.h"
29 #include "localization.h"
30 #include "freeArrayOfString.h"
31 #include "CallClipboard.h"
32 #include "os_string.h"
33 #include "FigureList.h"
34 }
35
36 /*--------------------------------------------------------------------------*/
37 int sci_ClipBoard(char *fname, void* pvApiCtx)
38 {
39     SciErr sciErr;
40
41     int* piAddr1   = NULL;
42     int* piAddrl1   = NULL;
43     int* piAddrStr  = NULL;
44     double* pdbll1  = NULL;
45     int* pil1       = NULL;
46
47     static int n1 = 0, m1 = 0;
48     char* param1 = NULL;
49     char* param2 = NULL;
50
51     nbInputArgument(pvApiCtx) = Max(0, nbInputArgument(pvApiCtx));
52     CheckInputArgument(pvApiCtx, 0, 2);
53     CheckOutputArgument(pvApiCtx, 0, 1);
54
55     if ( getScilabMode() != SCILAB_NWNI )
56     {
57         /*--------------------*/
58         /* clipboard("paste") */
59         /*--------------------*/
60         if (nbInputArgument(pvApiCtx) == 1)
61         {
62             if (checkInputArgumentType(pvApiCtx, 1, sci_strings))
63             {
64                 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
65                 if (sciErr.iErr)
66                 {
67                     printError(&sciErr, 0);
68                     return 1;
69                 }
70
71                 // Retrieve a matrix of double at position 1.
72                 if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param1))
73                 {
74                     Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 1);
75                     return 1;
76                 }
77
78                 if ( ( strcmp(param1, "paste") == 0 ) || ( strcmp(param1, "pastespecial") == 0 ) )
79                 {
80                     /* Use the Java clipboard (CallScilabBridge.java returns "" if clipboard could not be read) */
81                     char *output = getClipboardContents();
82
83
84                     m1 = (int)strlen(output);
85                     n1 = 1;
86
87                     if (createSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, output))
88                     {
89                         freeAllocatedSingleString(param1);
90                         Scierror(999, _("%s: Memory allocation error.\n"), fname);
91                         return 1;
92                     }
93
94                     delete[] output;
95                     AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
96
97                     freeAllocatedSingleString(param1);
98                     ReturnArguments(pvApiCtx);
99                     return TRUE;
100                 }
101                 else
102                 {
103                     freeAllocatedSingleString(param1);
104
105                     Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "paste", "pastespecial");
106                     return FALSE;
107                 }
108             }
109             else
110             {
111                 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
112                 return FALSE;
113             }
114         }
115         else if (nbInputArgument(pvApiCtx) == 2)
116         {
117             if (checkInputArgumentType(pvApiCtx, 1, sci_strings))
118             {
119                 /* Get the first argument: should be "copy" or "do" */
120                 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
121                 if (sciErr.iErr)
122                 {
123                     printError(&sciErr, 0);
124                     return 1;
125                 }
126
127                 // Retrieve a matrix of double at position 1.
128                 if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param1))
129                 {
130                     Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 1);
131                     return 1;
132                 }
133
134                 if (( strcmp(param1, "do") != 0 ) && ( strcmp(param1, "copy") != 0 ))
135                 {
136                     freeAllocatedSingleString(param1);
137                     Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do", "copy");
138                     return FALSE;
139                 }
140
141                 if (checkInputArgumentType(pvApiCtx, 2, sci_strings))
142                 {
143
144                     /*-------------------------------------------*/
145                     /* clipboard("do", {"paste","copy","empty"}) */
146                     /*-------------------------------------------*/
147
148                     /* @TODO : should be remplaced by an enum */
149                     if ( strcmp(param1, "do") == 0 )
150                     {
151                         freeAllocatedSingleString(param1);
152
153                         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl1);
154                         if (sciErr.iErr)
155                         {
156                             printError(&sciErr, 0);
157                             return 1;
158                         }
159
160                         // Retrieve a matrix of double at position 2.
161                         if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param2))
162                         {
163                             Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
164                             return 1;
165                         }
166
167                         if ( strcmp(param2, "paste") == 0 )
168                         {
169                             /* Call Java to do the job */
170                             pasteClipboardIntoConsole();
171                         }
172                         else if ( strcmp(param2, "copy") == 0 )
173                         {
174                             /* Call Java to do the job */
175                             copyConsoleSelection();
176                         }
177                         else if ( strcmp(param2, "empty") == 0 )
178                         {
179                             /* Call Java to do the job */
180                             emptyClipboard();
181                         }
182                         else
183                         {
184                             freeAllocatedSingleString(param2);
185                             Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s' or '%s' expected.\n"), fname, 2, "copy", "paste", "empty");
186                             return FALSE;
187                         }
188
189                         m1 = 0;
190                         n1 = 0;
191
192                         freeAllocatedSingleString(param2);
193
194                         sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
195                         if (sciErr.iErr)
196                         {
197                             printError(&sciErr, 0);
198                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
199                             return 1;
200                         }
201
202                         AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
203                         ReturnArguments(pvApiCtx);
204                         return TRUE;
205                     }
206
207                     /*-------------------------*/
208                     /* clipboard("copy", data) */
209                     /*-------------------------*/
210
211                     else if ( strcmp(param1, "copy") == 0 )
212                     {
213                         char *TextToPutInClipboard = NULL;
214                         char **Str = NULL;
215                         int m2 = 0, n2 = 0;
216
217                         freeAllocatedSingleString(param1);
218
219                         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrStr);
220                         if (sciErr.iErr)
221                         {
222                             printError(&sciErr, 0);
223                             return 1;
224                         }
225
226                         // Retrieve a matrix of string at position 2.
227                         if (getAllocatedMatrixOfString(pvApiCtx, piAddrStr, &m2, &n2, &Str))
228                         {
229                             Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
230                             return 1;
231                         }
232
233                         if (m2 * n2 == 1) /* Single line copy */
234                         {
235                             TextToPutInClipboard = Str[0];
236                             /* Call Java to do the job */
237                             setClipboardContents(TextToPutInClipboard);
238                         }
239                         else /* Multi-line copy */
240                         {
241                             int i = 0, j = 0, l2 = 0;
242                             char *TextToSendInClipboard = NULL;
243                             int SizeofTextToSendInClipboard = 0;
244                             char **buffer = (char**)MALLOC( (m2 * n2) * sizeof(char *) );
245                             if (buffer == NULL)
246                             {
247
248                                 freeAllocatedMatrixOfString(m2, n2, Str);
249                                 Scierror(999, _("%s: No more memory.\n"), fname);
250                                 return FALSE;
251                             }
252
253                             for (i = 0; i < m2; i++)
254                             {
255                                 for (j = 0; j < n2; j++)
256                                 {
257                                     SizeofTextToSendInClipboard = SizeofTextToSendInClipboard + (int)strlen(Str[j * m2 + i]) + (int)strlen("\n") + (int)strlen(" ");
258                                     buffer[i * n2 + j] = os_strdup(Str[j * m2 + i]);
259                                 }
260                             }
261
262                             TextToSendInClipboard = (char*)MALLOC( (SizeofTextToSendInClipboard) * sizeof(char) );
263                             if (TextToSendInClipboard == NULL)
264                             {
265                                 freeAllocatedMatrixOfString(m2, n2, Str);
266                                 Scierror(999, _("%s: No more memory.\n"), fname);
267                                 freeArrayOfString(buffer, m2 * n2);
268
269                                 return FALSE;
270                             }
271
272                             strcpy(TextToSendInClipboard, "");
273
274                             for (i = 0; i < m2; i++)
275                             {
276                                 for (j = 0; j < n2; j++)
277                                 {
278                                     strcat(TextToSendInClipboard, buffer[l2++]);
279                                     strcat(TextToSendInClipboard, " ");
280                                 }
281                                 if ( i != (m2 - 1) )
282                                 {
283                                     strcat(TextToSendInClipboard, "\n");
284                                 }
285                             }
286
287                             /* Call Java to do the job */
288                             setClipboardContents(TextToSendInClipboard);
289
290                             FREE(buffer);
291                             buffer = NULL;
292                             freeArrayOfString(buffer, m2 * n2);
293                             FREE(TextToSendInClipboard);
294                             TextToSendInClipboard = NULL;
295                         }
296
297                         freeAllocatedMatrixOfString(m2, n2, Str);
298                         m1 = 0;
299                         n1 = 0;
300
301                         sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
302                         if (sciErr.iErr)
303                         {
304                             printError(&sciErr, 0);
305                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
306                             return 1;
307                         }
308
309                         AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
310                         ReturnArguments(pvApiCtx);
311                         return TRUE;
312                     }
313                 }
314                 else
315                 {
316                     freeAllocatedSingleString(param1);
317                     Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
318                     return FALSE;
319                 }
320             }
321
322             /*----------------------------------*/
323             /* clipboard(fignum, {"EMF","DIB"}) */
324             /*----------------------------------*/
325
326             else if (checkInputArgumentType(pvApiCtx, 1, sci_matrix))
327             {
328
329                 int num_win = -2;
330
331                 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
332                 if (sciErr.iErr)
333                 {
334                     printError(&sciErr, 0);
335                     return 1;
336                 }
337
338                 // Retrieve a matrix of double at position 1.
339                 sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, piAddr1, &m1, &n1, &pil1);
340                 if (sciErr.iErr)
341                 {
342                     printError(&sciErr, 0);
343                     Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
344                     return 1;
345                 }
346
347                 num_win = pil1[0];
348
349                 if (m1 * n1 != 1)
350                 {
351                     Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 1);
352                     return FALSE;
353                 }
354
355                 if (checkInputArgumentType(pvApiCtx, 2, sci_strings))
356                 {
357                     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl1);
358                     if (sciErr.iErr)
359                     {
360                         printError(&sciErr, 0);
361                         return 1;
362                     }
363
364                     // Retrieve a matrix of double at position 2.
365                     if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param2))
366                     {
367                         Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
368                         return 1;
369                     }
370
371                     if ( ( strcmp(param2, "EMF") == 0 ) || ( strcmp(param2, "DIB") == 0 ) )
372                     {
373                         if (num_win >= 0)
374                         {
375                             /* Call Java */
376                             if ( strcmp(param2, "EMF") == 0)
377                             {
378                                 /* @TODO create EMF */
379                                 copyFigureToClipBoard(getFigureFromIndex(num_win));
380                             }
381                             else
382                             {
383                                 /* @TODO create DIB */
384                                 copyFigureToClipBoard(getFigureFromIndex(num_win));
385                             }
386
387                             m1 = 0;
388                             n1 = 0;
389
390                             freeAllocatedSingleString(param2);
391
392                             sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
393                             if (sciErr.iErr)
394                             {
395                                 printError(&sciErr, 0);
396                                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
397                                 return 1;
398                             }
399                             AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
400                             ReturnArguments(pvApiCtx);
401                             return TRUE;
402                         }
403                         else
404                         {
405                             freeAllocatedSingleString(param2);
406                             Scierror(999, _("%s: Wrong value for input argument #%d: Must be >= %d expected.\n"), fname, 1, 0);
407                             return FALSE;
408                         }
409
410                     }
411                     else
412                     {
413                         freeAllocatedSingleString(param2);
414                         Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "EMF", "DIB");
415                         return FALSE;
416                     }
417
418                 }
419                 else
420                 {
421                     Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
422                     return FALSE;
423                 }
424             }
425             else
426             {
427                 Scierror(999, _("%s: Wrong type for input argument #%d: string or real expected.\n"), fname, 1);
428                 return FALSE;
429             }
430         }
431     }
432     else
433     {
434         Scierror(999, _("%s: Function not available in NWNI mode.\n"), fname);
435         return FALSE;
436     }
437
438     return FALSE;
439 }
440 /*--------------------------------------------------------------------------*/