2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Fabrice Leray
4 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
5 * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
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.1-en.txt
15 /*------------------------------------------------------------------------*/
16 /* file: sci_xset.c */
17 /* desc : interface for xset routine */
18 /*------------------------------------------------------------------------*/
20 #include "gw_graphics.h"
21 #include "api_scilab.h"
22 #include "GetProperty.h"
23 #include "SetProperty.h"
24 #include "DrawObjects.h"
25 #include "InitObjects.h"
26 #include "XsetXgetParameters.h"
29 #include "localization.h"
31 #include "HandleManagement.h"
33 #include "BuildObjects.h"
34 #include "graphicObjectProperties.h"
35 #include "setGraphicObjectProperty.h"
36 #include "FigureList.h"
37 #include "CurrentFigure.h"
38 #include "CurrentSubwin.h"
39 #include "AxesModel.h"
40 #include "getGraphicObjectProperty.h"
41 #include "deleteGraphicObject.h"
42 #include "warningmode.h"
45 /*--------------------------------------------------------------------------*/
46 int sci_xset(char *fname, unsigned long fname_len)
57 int m1 = 0, m2 = 0, xm[5], xn[5], x[5] = {0, 0, 0, 0, 0}, i = 0, v = 0;
58 double xx[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
59 char * subwinUID = NULL;
60 BOOL keyFound = FALSE;
62 if (nbInputArgument(pvApiCtx) <= 0)
64 sci_demo(fname, fname_len);
68 CheckInputArgument(pvApiCtx, 1, 6);
69 CheckOutputArgument(pvApiCtx, 0, 1);
71 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
74 printError(&sciErr, 0);
78 // Retrieve a string at position 1.
79 if (getAllocatedSingleString(pvApiCtx, piAddrl1, &l1))
81 Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
85 for (i = 0 ; i < NUMSETFONC ; i++)
87 if (strcmp(l1, KeyTab_[i]) == 0)
96 Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1));
100 // in case of "fpf" and "auto clear", the second argument must be a string
101 if (nbInputArgument(pvApiCtx) == 2 && (!checkInputArgumentType(pvApiCtx, 2, sci_matrix)))
103 /* second argument is not a scalar it must be a string */
104 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl2);
107 printError(&sciErr, 0);
111 // Retrieve a string at position 2.
112 if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
114 Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
118 if (strcmp(l1, "fpf") == 0)
120 strcpy(getFPF(), l2);
122 else if (strcmp(l1, "auto clear") == 0)
124 int bAutoClear = (int) FALSE;
126 if (strcmp(l2, "on") == 0)
128 bAutoClear = (int) TRUE;
131 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_AUTO_CLEAR__, &bAutoClear, jni_bool, 1);
135 Scierror(999, _("%s: Unrecognized input argument '%s'.\n"), "xset(arg,<string>)", l1);
139 AssignOutputVariable(pvApiCtx, 1) = 0;
140 ReturnArguments(pvApiCtx);
144 // in other case than "fpf" and "auto clear", the secod to fifth argument must be a matrix of double
145 for (i = 2 ; i <= nbInputArgument(pvApiCtx) ; i++)
147 sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrlr);
150 printError(&sciErr, 0);
154 // Retrieve a matrix of double at position i.
155 sciErr = getMatrixOfDouble(pvApiCtx, piAddrlr, &xm[i - 2], &xn[i - 2], &lr);
158 printError(&sciErr, 0);
159 Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, i);
163 x[i - 2] = (int)lr[0];
167 if (strcmp((l1), "clipping") == 0)
170 if (nbInputArgument(pvApiCtx) != 5 && nbInputArgument(pvApiCtx) != 2)
172 Scierror(999, _("%s: Wrong number of input arguments: %d or %d expected.\n"), fname, 2, 5);
176 if (nbInputArgument(pvApiCtx) == 2)
182 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrlr);
185 printError(&sciErr, 0);
189 // Retrieve a matrix of double at position 2.
190 sciErr = getMatrixOfDouble(pvApiCtx, piAddrlr, &iRows, &iCols, &lr);
193 printError(&sciErr, 0);
194 Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, 2);
199 if (iRows * iCols != 4)
201 Scierror(999, _("%s: Wrong size for input argument #%d: A %d-element vector expected.\n"), fname, 2, 4);
205 for (i = 0; i < 4 ; i++)
210 subwinUID = (char*)getOrCreateDefaultSubwin();
211 setGraphicObjectProperty(subwinUID, __GO_CLIP_BOX__, xx, jni_double_vector, 4);
212 setGraphicObjectProperty(subwinUID, __GO_CLIP_STATE__, &clipState, jni_int, 1);
214 else if (strcmp((l1), "colormap") == 0)
216 char *pFigureUID = NULL;
217 if (nbInputArgument(pvApiCtx) != 2)
219 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
223 getOrCreateDefaultSubwin();
224 pFigureUID = (char*)getCurrentFigure();
225 setGraphicObjectProperty(pFigureUID, __GO_COLORMAP__, (lr), jni_double_vector, *xm * (*xn));
227 else if (strcmp((l1), "mark size") == 0)
229 int markSize = (int) xx[0];
230 int markSizeUnit = 1; /* force switch to tabulated mode : old syntax / 0 : point, 1 : tabulated */
231 char *subwinUID = (char*)getOrCreateDefaultSubwin();
233 if (nbInputArgument(pvApiCtx) != 2)
235 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
239 setGraphicObjectProperty(subwinUID, __GO_MARK_SIZE_UNIT__, &markSizeUnit, jni_int, 1);
240 setGraphicObjectProperty(subwinUID, __GO_MARK_SIZE__, &markSize, jni_int, 1);
242 else if (strcmp((l1), "mark") == 0)
244 int markStyle = (int) xx[0];
245 int markSize = (int) xx[1];
246 int markSizeUnit = 1; /* force switch to tabulated mode : old syntax / 0 : point, 1 : tabulated */
247 char *subwinUID = NULL;
248 if (nbInputArgument(pvApiCtx) != 3)
250 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 3);
254 subwinUID = (char*)getOrCreateDefaultSubwin();
255 setGraphicObjectProperty(subwinUID, __GO_MARK_SIZE_UNIT__, &markSizeUnit, jni_int, 1); /* force switch to tabulated mode : old syntax */
256 setGraphicObjectProperty(subwinUID, __GO_MARK_STYLE__, &markStyle, jni_int, 1);
257 setGraphicObjectProperty(subwinUID, __GO_MARK_SIZE__, &markSize, jni_int, 1);
259 else if (strcmp((l1), "font size") == 0)
261 double fontSize = xx[0];
262 if (nbInputArgument(pvApiCtx) != 2)
264 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
267 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, &fontSize, jni_double, 1);
269 else if (strcmp((l1), "default") == 0)
271 if (nbInputArgument(pvApiCtx) != 1)
273 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 1);
280 unsigned short defcolors[] =
282 0, 0, 0, /* Black: DEFAULTBLACK */
283 0, 0, 255, /* Blue */
284 0, 255, 0, /* Green */
285 0, 255, 255, /* Cyan */
287 255, 0, 255, /* Magenta */
288 255, 255, 0, /* Yellow */
289 255, 255, 255, /* White: DEFAULTWHITE */
290 0, 0, 144, /* Blue4 */
291 0, 0, 176, /* Blue3 */
292 0, 0, 208, /* Blue2 */
293 135, 206, 255, /* LtBlue */
294 0, 144, 0, /* Green4 */
295 0, 176, 0, /* Green3 */
296 0, 208, 0, /* Green2 */
297 0, 144, 144, /* Cyan4 */
298 0, 176, 176, /* Cyan3 */
299 0, 208, 208, /* Cyan2 */
300 144, 0, 0, /* Red4 */
301 176, 0, 0, /* Red3 */
302 208, 0, 0, /* Red2 */
303 144, 0, 144, /* Magenta4 */
304 176, 0, 176, /* Magenta3 */
305 208, 0, 208, /* Magenta2 */
306 128, 48, 0, /* Brown4 */
307 160, 64, 0, /* Brown3 */
308 192, 96, 0, /* Brown2 */
309 255, 128, 128, /* Pink4 */
310 255, 160, 160, /* Pink3 */
311 255, 192, 192, /* Pink2 */
312 255, 224, 224, /* Pink */
313 255, 215, 0 /* Gold */
316 int piFigurePosition[2] = {200, 200};
317 int piFigureSize[2] = {500, 500};
318 int piAxesSize[2] = {498, 366};
319 int piViewPort[2] = {0, 0};
320 int piEmptyMatrix[4] = {1, 0, 0, 0};
322 // Create new axes and set it in current figure
323 char* pSubWinUID = (char*)getCurrentSubWin();
329 int m = NUMCOLORS_SCI;
333 int defaultBackground = -2;
334 char error_message[70];
336 double* pdblColorMap = (double*)malloc(m * 3 * sizeof(double));
338 // Create figure if it not exist.
339 char* pFigureUID = (char*)getCurrentFigure();
340 if (pFigureUID == NULL)
342 pFigureUID = createNewFigureWithAxes();
343 setCurrentFigure(pFigureUID);
344 AssignOutputVariable(pvApiCtx, 1) = 0;
345 ReturnArguments(pvApiCtx);
350 if (pdblColorMap == NULL)
352 sprintf(error_message, _("%s: No more memory.\n"), "xset");
356 if (pSubWinUID != NULL)
358 int iChildrenCount = 0;
359 int* childrencount = &iChildrenCount;
360 char** childrenUID = NULL;
362 int *piHidden = &iHidden;
364 getGraphicObjectProperty(pFigureUID, __GO_CHILDREN_COUNT__, jni_int, (void **)&childrencount);
365 getGraphicObjectProperty(pFigureUID, __GO_CHILDREN__, jni_string_vector, (void **)&childrenUID);
367 for (i = 0; i < childrencount[0]; ++i)
369 getGraphicObjectProperty(childrenUID[i], __GO_HIDDEN__, jni_bool, (void **)&piHidden);
372 deleteGraphicObject(childrenUID[i]);
377 cloneAxesModel(pFigureUID);
379 // Set default figure properties
380 setGraphicObjectProperty(pFigureUID, __GO_POSITION__, piFigurePosition, jni_int_vector, 2);
381 setGraphicObjectProperty(pFigureUID, __GO_SIZE__, piFigureSize, jni_int_vector, 2);
382 setGraphicObjectProperty(pFigureUID, __GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2);
383 setGraphicObjectProperty(pFigureUID, __GO_AUTORESIZE__, &bTrue, jni_bool, 1);
384 setGraphicObjectProperty(pFigureUID, __GO_VIEWPORT__, piViewPort, jni_int_vector, 2);
385 setGraphicObjectProperty(pFigureUID, __GO_NAME__, _("Figure n°%d"), jni_string, 1);
386 setGraphicObjectProperty(pFigureUID, __GO_INFO_MESSAGE__, "", jni_string, 1);
387 setGraphicObjectProperty(pFigureUID, __GO_PIXMAP__, &bFalse, jni_bool, 1);
388 setGraphicObjectProperty(pFigureUID, __GO_PIXEL_DRAWING_MODE__, &iCopy, jni_int, 1);
389 setGraphicObjectProperty(pFigureUID, __GO_ANTIALIASING__, &iZero, jni_int, 1);
390 setGraphicObjectProperty(pFigureUID, __GO_IMMEDIATE_DRAWING__, &bTrue, jni_bool, 1);
391 setGraphicObjectProperty(pFigureUID, __GO_BACKGROUND__, &defaultBackground, jni_int, 1);
392 setGraphicObjectProperty(pFigureUID, __GO_VISIBLE__, &bTrue, jni_bool, 1);
393 setGraphicObjectProperty(pFigureUID, __GO_ROTATION_TYPE__, &iZero, jni_int, 1);
394 setGraphicObjectProperty(pFigureUID, __GO_EVENTHANDLER__, "", jni_string, 1);
395 setGraphicObjectProperty(pFigureUID, __GO_EVENTHANDLER_ENABLE__, &bFalse, jni_bool, 1);
396 setGraphicObjectProperty(pFigureUID, __GO_USER_DATA__, piEmptyMatrix, jni_int_vector, 4);
397 setGraphicObjectProperty(pFigureUID, __GO_RESIZEFCN__, "", jni_string, 1);
398 setGraphicObjectProperty(pFigureUID, __GO_TAG__, "", jni_string, 1);
400 for (i = 0; i < m; i++)
402 pdblColorMap[i] = (double)(defcolors[3 * i] / 255.0);
403 pdblColorMap[i + m] = (double)(defcolors[3 * i + 1] / 255.0);
404 pdblColorMap[i + 2 * m] = (double)(defcolors[3 * i + 2] / 255.0);
407 setGraphicObjectProperty(pFigureUID, __GO_COLORMAP__, pdblColorMap, jni_double_vector, 3 * m);
408 setGraphicObjectProperty(pFigureUID, __GO_PARENT__, "", jni_string, 1);
411 else if (strcmp((l1), "clipgrf") == 0)
414 /* special treatement for xset("cligrf") */
415 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, &clipState, jni_int, 1);
417 else if (strcmp((l1), "clipoff") == 0)
420 /* special treatement for xset("clipoff") */
421 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, &clipState, jni_int, 1);
423 else if (strcmp((l1), "hidden3d") == 0)
425 /* special treatement for xset("hidden3d") */
426 int hiddenColor = (int) x[0];
427 if (nbInputArgument(pvApiCtx) != 2)
429 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
432 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_HIDDEN_COLOR__, &hiddenColor, jni_int, 1);
434 else if (strcmp((l1), "font") == 0)
436 int fontStyle = (int) xx[0];
437 double fontSize = xx[1];
438 if (nbInputArgument(pvApiCtx) != 3)
440 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 3);
444 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, &fontSize, jni_double, 1);
445 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_STYLE__, &fontStyle, jni_int, 1);
447 else if (strcmp((l1), "window") == 0 || strcmp((l1), "figure") == 0)
449 // Find if window already exists, if not create a new one
451 char *pFigureUID = (char*)getFigureFromIndex(iID);
453 if (nbInputArgument(pvApiCtx) != 2)
455 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
459 if (pFigureUID == NULL)
461 pFigureUID = createNewFigureWithAxes();
462 setGraphicObjectProperty(pFigureUID, __GO_ID__, &iID, jni_int, 1);
463 setCurrentFigure(pFigureUID);
465 setCurrentFigure(pFigureUID);
467 else if ((strcmp((l1), "foreground") == 0) || (strcmp((l1), "color") == 0) || (strcmp((l1), "pattern") == 0))
469 int iColor = (int) x[0];
470 if (nbInputArgument(pvApiCtx) != 2)
472 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
475 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_COLOR__, &iColor, jni_int, 1);
477 else if (strcmp((l1), "background") == 0)
479 int iColor = (int) x[0];
480 if (nbInputArgument(pvApiCtx) != 2)
482 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
485 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_BACKGROUND__, &iColor, jni_int, 1);
487 else if (strcmp((l1), "thickness") == 0)
489 if (nbInputArgument(pvApiCtx) != 2)
491 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
494 sciSetLineWidth((char*)getOrCreateDefaultSubwin(), x[0]);
496 else if (strcmp((l1), "line style") == 0)
498 int lineStyle = (int) x[0];
499 if (nbInputArgument(pvApiCtx) != 2)
501 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
504 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, &lineStyle, jni_int, 1);
506 else if (strcmp((l1), "mark") == 0)
510 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_MARK_MODE__, &markMode, jni_bool, 1);
511 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_MARK_STYLE__, x, jni_int, 1);
513 else if (strcmp((l1), "colormap") == 0)
515 getOrCreateDefaultSubwin();
516 if (nbInputArgument(pvApiCtx) != 2)
518 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
521 setGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP__, (lr), jni_double_vector, xm[0] * xn[0]);
523 else if (strcmp((l1), "dashes") == 0)
525 int lineStyle = (int) x[0];
526 if (nbInputArgument(pvApiCtx) != 2)
528 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
531 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, &lineStyle, jni_int, 1);
533 else if (strcmp((l1), "wresize") == 0)
535 int iAutoResizeMode = x[0];
536 if (nbInputArgument(pvApiCtx) != 2)
538 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
541 setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_AUTORESIZE__, &iAutoResizeMode, jni_bool, 1);
543 else if (strcmp((l1), "wpos") == 0)
545 int figurePosition[2];
546 if (nbInputArgument(pvApiCtx) != 2)
548 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
551 getOrCreateDefaultSubwin();
553 figurePosition[0] = x[0];
554 figurePosition[1] = x[1];
555 setGraphicObjectProperty(getCurrentFigure(), __GO_POSITION__, figurePosition, jni_int_vector, 2);
557 else if (strcmp((l1), "wpdim") == 0 || strcmp((l1), "wdim") == 0)
560 if (nbInputArgument(pvApiCtx) != 2 && nbInputArgument(pvApiCtx) != 3)
562 Scierror(999, _("%s: Wrong number of input arguments: %d or %d expected.\n"), fname, 2, 3);
566 /* Xwindows limits dimensions to 2^16 */
567 if ((x[0] > 65535) || (x[1] > 65535))
569 x[0] = Min(x[0], 65535);
570 x[1] = Min(x[1], 65535);
576 getOrCreateDefaultSubwin();
578 figureSize[0] = x[0];
579 figureSize[1] = x[1];
580 setGraphicObjectProperty(getCurrentFigure(), __GO_SIZE__, figureSize, jni_int_vector, 2);
581 } /*Ajout A.Djalel le 10/11/03 */
582 else if (strcmp((l1), "pixmap") == 0)
584 int iPixmapMode = x[0];
585 if (nbInputArgument(pvApiCtx) != 2)
587 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
590 getOrCreateDefaultSubwin();
591 setGraphicObjectProperty(getCurrentFigure(), __GO_PIXMAP__, &iPixmapMode, jni_bool, 1);
593 else if (strcmp((l1), "viewport") == 0)
595 int viewport[4] = {x[0], x[1], 0, 0};
596 if (nbInputArgument(pvApiCtx) != 3)
598 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 3);
601 getOrCreateDefaultSubwin();
602 setGraphicObjectProperty(getCurrentFigure(), __GO_VIEWPORT__, viewport, jni_int_vector, 2);
604 else if (strcmp((l1), "line mode") == 0)
606 char *pstSubwinUID = (char*)getOrCreateDefaultSubwin();
609 if (nbInputArgument(pvApiCtx) != 2)
611 Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
617 setGraphicObjectProperty(pstSubwinUID, __GO_LINE_MODE__, &iZero, jni_bool, 1);
621 setGraphicObjectProperty(pstSubwinUID, __GO_LINE_MODE__, &iOne, jni_bool, 1);
626 Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1));
630 AssignOutputVariable(pvApiCtx, 1) = 0;
631 ReturnArguments(pvApiCtx);
632 freeAllocatedSingleString(l1);
633 freeAllocatedSingleString(l2);
636 /*--------------------------------------------------------------------------*/