1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - ENPC - Jean-Philipe Chancelier
4  * Copyright (C) 2006 - INRIA - Fabrice Leray
5  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6  * Copyright (C) 2011 - DIGITEO - Bruno JOFRET
7  *
8  * Copyright (C) 2012 - 2016 - Scilab Enterprises
9  *
10  * This file is hereby licensed under the terms of the GNU GPL v2.0,
11  * pursuant to article 5.3.4 of the CeCILL v.2.1.
12  * This file was originally licensed under the terms of the CeCILL v2.1,
13  * and continues to be available under such terms.
14  * For more information, see the COPYING file which you should have received
15  * along with this program.
16  *
17  */
18 
19 /*------------------------------------------------------------------------*/
20 /* file: sci_xrects.c                                                     */
21 /* desc : interface for xrects routine                                    */
22 /*------------------------------------------------------------------------*/
23 
24 #include "gw_graphics.h"
25 #include "api_scilab.h"
26 #include "BuildObjects.h"
27 #include "sciCall.h"
28 #include "DrawObjects.h"
29 #include "GetProperty.h"
30 #include "localization.h"
31 #include "Scierror.h"
32 #include "HandleManagement.h"
33 
34 #include "createGraphicObject.h"
35 #include "setGraphicObjectProperty.h"
36 #include "getGraphicObjectProperty.h"
37 #include "graphicObjectProperties.h"
38 #include "CurrentObject.h"
39 /*--------------------------------------------------------------------------*/
sci_xrects(char * fname,void * pvApiCtx)40 int sci_xrects(char *fname, void *pvApiCtx)
41 {
42     SciErr sciErr;
43 
44     int* piAddrl1 = NULL;
45     double* l1 = NULL;
46     int* piAddr2 = NULL;
47     int* l2 = NULL;
48 
49     int m1 = 0, n1 = 0, m2 = 0, n2 = 0;
50     long  hdl = 0;
51     int i = 0;
52     int iSubwinUID = 0;
53 
54     int foreground = 0;
55     int *piForeground = &foreground;
56     int iCompoundUID = 0;
57 
58     CheckInputArgument(pvApiCtx, 1, 2);
59 
60     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
61     if (sciErr.iErr)
62     {
63         printError(&sciErr, 0);
64         return 1;
65     }
66 
67     // Retrieve a matrix of double at position 1.
68     sciErr = getMatrixOfDouble(pvApiCtx, piAddrl1, &m1, &n1, &l1);
69     if (sciErr.iErr)
70     {
71         printError(&sciErr, 0);
72         Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
73         return 1;
74     }
75 
76     if (m1 != 4)
77     {
78         Scierror(999, _("%s: Wrong size for input argument #%d: %s expected.\n"), fname, 1, "(4,n)");
79         return 0;
80     }
81 
82 
83     if (nbInputArgument(pvApiCtx) == 2)
84     {
85         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
86         if (sciErr.iErr)
87         {
88             printError(&sciErr, 0);
89             return 1;
90         }
91 
92         // Retrieve a matrix of double at position 2.
93         sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, piAddr2, &m2, &n2, &l2);
94         if (sciErr.iErr)
95         {
96             printError(&sciErr, 0);
97             Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2);
98             return 1;
99         }
100 
101         //CheckVector
102         if (m2 != 1 && n2 != 1)
103         {
104             Scierror(999, _("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 2);
105             return 1;
106         }
107 
108         if (m2 * n2 != n1)
109         {
110             Scierror(999, _("%s: Incompatible length for input arguments #%d and #%d.\n"), fname, 1, 2);
111             return 0;
112         }
113     }
114     else
115     {
116         m2 = 1;
117         n2 = n1;
118 
119         sciErr = allocMatrixOfDoubleAsInteger(pvApiCtx, 2, m2, n2, &l2);
120         if (sciErr.iErr)
121         {
122             printError(&sciErr, 0);
123             Scierror(999, _("%s: Memory allocation error.\n"), fname);
124             return 1;
125         }
126 
127         for (i = 0; i < n2; ++i)
128         {
129             l2[i] = 0;
130         }
131     }
132 
133     iSubwinUID = getOrCreateDefaultSubwin();
134 
135     // Create compound.
136     iCompoundUID = createGraphicObject(__GO_COMPOUND__);
137     /* Sets the parent-child relationship for the Compound */
138     setGraphicObjectRelationship(iSubwinUID, iCompoundUID);
139 
140     /** Get Subwin line color */
141     getGraphicObjectProperty(iSubwinUID, __GO_LINE_COLOR__, jni_int, (void**)&piForeground);
142 
143     for (i = 0; i < n1; ++i)
144     {
145         /*       j = (i==0) ? 0 : 1; */
146         if (l2[i] == 0)
147         {
148             /** fil(i) = 0 rectangle i is drawn using the current line style (or color).**/
149             /* color setting is done now */
150 
151             Objrect((l1 + (4 * i)), (l1 + (4 * i) + 1), (l1 + (4 * i) + 2), (l1 + (4 * i) + 3),
152                     &foreground, NULL, FALSE, TRUE, &hdl);
153         }
154         else
155         {
156             if (l2[i] < 0)
157             {
158                 /** fil(i) < 0 rectangle i is drawn using the line style (or color) **/
159                 int tmp = - (*(int*)(l2 + i));
160                 Objrect((l1 + (4 * i)), (l1 + (4 * i) + 1), (l1 + (4 * i) + 2), (l1 + (4 * i) + 3),
161                         &tmp, NULL, FALSE, TRUE, &hdl);
162             }
163             else
164             {
165                 /** fil(i) > 0   rectangle i is filled using the pattern (or color) **/
166                 Objrect((l1 + (4 * i)), (l1 + (4 * i) + 1), (l1 + (4 * i) + 2), (l1 + (4 * i) + 3),
167                         NULL, l2 + i, TRUE, FALSE, &hdl);
168             }
169         }
170         // Add newly created object to Compound
171         setGraphicObjectRelationship(iCompoundUID, getObjectFromHandle(hdl));
172     }
173 
174     /** make Compound current object **/
175     setCurrentObject(iCompoundUID);
176     AssignOutputVariable(pvApiCtx, 1) = 0;
177     ReturnArguments(pvApiCtx);
178 
179     return 0;
180 }
181 /*--------------------------------------------------------------------------*/
182