1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
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 /*------------------------------------------------------------------------*/
17 /* file: sci_swap_handles.c */
18 /* desc : interface for swap_handles routine */
19 /*------------------------------------------------------------------------*/
20 #include <string.h>
21 #include "gw_graphics.h"
22 #include "api_scilab.h"
23 #include "HandleManagement.h"
24 #include "localization.h"
25 #include "Scierror.h"
26
27 #include "setGraphicObjectProperty.h"
28 #include "getGraphicObjectProperty.h"
29 #include "graphicObjectProperties.h"
30
31 /*--------------------------------------------------------------------------*/
sci_swap_handles(char * fname,void * pvApiCtx)32 int sci_swap_handles(char * fname, void *pvApiCtx)
33 {
34 SciErr sciErr;
35
36 int* piAddrfirstHdlStkIndex = NULL;
37 long long* firstHdlStkIndex = NULL;
38 int* piAddrsecondHdlStkIndex = NULL;
39 long long* secondHdlStkIndex = NULL;
40
41 int firstHdlCol = 0;
42 int firstHdlRow = 0;
43 int secondHdlCol = 0;
44 int secondHdlRow = 0;
45 int iHandle_1 = 0;
46 int iHandle_2 = 0;
47 int iType_1 = 0;
48 int iType_2 = 0;
49 int * piType_1 = &iType_1;
50 int * piType_2 = &iType_2;
51 int iParent_1 = 0;
52 int* piParent_1 = &iParent_1;
53 int iParent_2 = 0;
54 int* piParent_2 = &iParent_2;
55 int iParentType_1 = 0;
56 int iParentType_2 = 0;
57 int * piParentType_1 = &iParentType_1;
58 int * piParentType_2 = &iParentType_2;
59 int iChildrenCount = 0;
60 int *piChildrenCount = &iChildrenCount;
61 int* piChildrenUID = NULL;
62 int i = 0;
63 long h = 0;
64
65 CheckInputArgument(pvApiCtx, 2, 2);
66 CheckOutputArgument(pvApiCtx, 0, 1);
67
68 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrfirstHdlStkIndex);
69 if (sciErr.iErr)
70 {
71 printError(&sciErr, 0);
72 return 1;
73 }
74
75 // Retrieve a matrix of handle at position 1.
76 sciErr = getMatrixOfHandle(pvApiCtx, piAddrfirstHdlStkIndex, &firstHdlRow, &firstHdlCol, &firstHdlStkIndex);
77 if (sciErr.iErr)
78 {
79 printError(&sciErr, 0);
80 Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, 1);
81 return 1;
82 }
83
84 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrsecondHdlStkIndex);
85 if (sciErr.iErr)
86 {
87 printError(&sciErr, 0);
88 return 1;
89 }
90
91 // Retrieve a matrix of handle at position 2.
92 sciErr = getMatrixOfHandle(pvApiCtx, piAddrsecondHdlStkIndex, &secondHdlRow, &secondHdlCol, &secondHdlStkIndex);
93 if (sciErr.iErr)
94 {
95 printError(&sciErr, 0);
96 Scierror(202, _("%s: Wrong type for input argument #%d: Handle matrix expected.\n"), fname, 2);
97 return 1;
98 }
99
100
101 if (firstHdlRow * firstHdlCol != 1 || secondHdlRow * secondHdlCol != 1)
102 {
103 Scierror(999, _("%s: Routine can only swap two single handles.\n"), fname);
104 return 0;
105 }
106
107 /* get the two handles and swap them */
108 h = (long) * (firstHdlStkIndex);
109 iHandle_1 = getObjectFromHandle(h);
110
111 h = (long) * (secondHdlStkIndex);
112 iHandle_2 = getObjectFromHandle(h);
113
114 getGraphicObjectProperty(iHandle_1, __GO_TYPE__, jni_int, (void **)&piType_1);
115 getGraphicObjectProperty(iHandle_2, __GO_TYPE__, jni_int, (void **)&piType_2);
116
117 iParent_1 = getParentObject(iHandle_1);
118 iParent_2 = getParentObject(iHandle_2);
119
120 getGraphicObjectProperty(iParent_1, __GO_TYPE__, jni_int, (void **)&piParentType_1);
121 getGraphicObjectProperty(iParent_2, __GO_TYPE__, jni_int, (void **)&piParentType_2);
122
123 // Check if objects do not have the same parent
124 if (iParent_1 == iParent_2)
125 {
126 getGraphicObjectProperty(iParent_1, __GO_CHILDREN_COUNT__, jni_int, (void **)&piChildrenCount);
127 getGraphicObjectProperty(iParent_1, __GO_CHILDREN__, jni_int_vector, (void **)&piChildrenUID);
128
129 for (i = 0 ; i < iChildrenCount ; ++i)
130 {
131 if (piChildrenUID[i] == iHandle_1)
132 {
133 piChildrenUID[i] = iHandle_2;
134 }
135 else if (piChildrenUID[i] == iHandle_2)
136 {
137 piChildrenUID[i] = iHandle_1;
138 }
139 }
140
141 setGraphicObjectProperty(iParent_1, __GO_CHILDREN__, piChildrenUID, jni_int_vector, iChildrenCount);
142 }
143 else if (iType_1 == iType_2 || iParentType_1 == iParentType_2)
144 {
145 setGraphicObjectRelationship(iParent_1, iHandle_2);
146 setGraphicObjectRelationship(iParent_2, iHandle_1);
147 }
148 else
149 {
150 Scierror(999, _("%s: Handles do not have the same parent type neither the same type.\n"), fname);
151 return 0;
152 }
153 AssignOutputVariable(pvApiCtx, 1) = 0;
154 ReturnArguments(pvApiCtx);
155 return 0;
156 }
157 /*--------------------------------------------------------------------------*/
158