summaryrefslogtreecommitdiffstats
path: root/scilab/modules/graphics/sci_gateway/c/sci_xclick.c
blob: 18d4ddfde697ebd668f16e66043cdc45ebb8c6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2006 - INRIA - Fabrice Leray
 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
 * Copyright (C) 2007 - INRIA - Vincent Couvert
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

/*------------------------------------------------------------------------*/
/* file: sci_xclick.c                                                     */
/* desc : interface for xclick routine                                    */
/*------------------------------------------------------------------------*/

#include <string.h>
#include "gw_graphics.h"
#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include "CallJxclick.h"
#include "GetProperty.h"
#include "FigureList.h"
#include "axesScale.h"
#include "HandleManagement.h"
#include "BuildObjects.h"
#include "CurrentSubwin.h"

#include "graphicObjectProperties.h"
#include "getGraphicObjectProperty.h"

/*--------------------------------------------------------------------------*/
int sci_xclick(char *fname, void *pvApiCtx)
{
    SciErr sciErr;

    double* rep = NULL;
    int one = 1, three = 3;
    int istr = 0;
    //int iflag = 0;

    int mouseButtonNumber = 0;
    char * menuCallback = NULL;
    int iWindowUID = 0;
    int pixelCoords[2];
    double userCoords2D[2];

    int iFigureId = 0;
    int *piFigureId = &iFigureId;

    CheckInputArgument(pvApiCtx, -1, 1);
    CheckOutputArgument(pvApiCtx, 1, 5);

    //iflag = (nbInputArgument(pvApiCtx) >= 1) ? 1 :0;

    // Select current figure or create it
    getOrCreateDefaultSubwin();

    // Call Java xclick
    CallJxclick();

    // Get return values
    mouseButtonNumber = getJxclickMouseButtonNumber();
    pixelCoords[0] = (int)getJxclickXCoordinate();
    pixelCoords[1] = (int)getJxclickYCoordinate();
    iWindowUID = getJxclickWindowID();
    menuCallback = getJxclickMenuCallback();

    // Convert pixel coordinates to user coordinates
    // Conversion is not done if the user clicked on a menu (pixelCoords[*] == -1)
    if (pixelCoords[0] != -1 && pixelCoords[1] != -1)
    {
        int iClickedSubwinUID = getCurrentSubWin();
        sciGet2dViewCoordFromPixel(iClickedSubwinUID, pixelCoords, userCoords2D);
    }
    else
    {
        userCoords2D[0] = pixelCoords[0];
        userCoords2D[1] = pixelCoords[1];
    }

    if (nbOutputArgument(pvApiCtx) == 1)
    {
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;

        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, one, three, &rep);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        rep[0] = (double)mouseButtonNumber;
        rep[1] = userCoords2D[0];
        rep[2] = userCoords2D[1];
    }
    else
    {
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;

        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, one, one, &rep);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        rep[0] = (double)mouseButtonNumber;
    }

    if (nbOutputArgument(pvApiCtx) >= 2)
    {
        AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;

        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, one, one, &rep);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        rep[0] = userCoords2D[0];
    }

    if (nbOutputArgument(pvApiCtx) >= 3)
    {
        AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3;
        // YOU MUST REMOVE YOUR VARIABLE DECLARATION "int rep".
        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 3, one, one, &rep);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        rep[0] = userCoords2D[1];
    }

    if (nbOutputArgument(pvApiCtx) >= 4)
    {
        AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx) + 4;

        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 4, one, one, &rep);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        getGraphicObjectProperty(iWindowUID, __GO_ID__, jni_int, (void**)&piFigureId);
        rep[0] = (double)iFigureId;
    }

    if (nbOutputArgument(pvApiCtx) >= 5)
    {
        if (createSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 5, menuCallback))
        {
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            deleteJxclickString(menuCallback);
            return 1;
        }

        AssignOutputVariable(pvApiCtx, 5) = nbInputArgument(pvApiCtx) + 5;
    }

    deleteJxclickString(menuCallback);

    ReturnArguments(pvApiCtx);

    return 0;
}
/*--------------------------------------------------------------------------*/