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