summaryrefslogtreecommitdiffstats
path: root/scilab/modules/graphic_objects/src/cpp/ScilabView.cpp
blob: 57a70ecb4ae2abbed83a6467d2ae0dfc3528df92 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include <algorithm>
#include <iostream>
#include <map>
#include <list>
#include <cstring>
#include <limits.h>

#include "internal.hxx"
#include "ScilabView.hxx"
#include "CallGraphicController.hxx"
#include "returnProperty.h"

extern "C"
{
#include "CurrentObject.h"
#include "createGraphicObject.h"
#include "setGraphicObjectProperty.h"
#include "getGraphicObjectProperty.h"
#include "graphicObjectProperties.h"
#include "getScilabJavaVM.h"
#include "deleteGraphicObject.h"
#include "sci_malloc.h"
}

/**
 * C Wrapping functions
 * \{
 */
void ScilabNativeView__createObject(int iUID)
{
    ScilabView::createObject(iUID);
}

void ScilabNativeView__deleteObject(int iUID)
{
    ScilabView::deleteObject(iUID);
}

void ScilabNativeView__updateObject(int iUID, int iProperty)
{
    ScilabView::updateObject(iUID, iProperty);
}

void ScilabNativeView__setCurrentFigure(int iUID)
{
    ScilabView::setCurrentFigure(iUID);
}

void ScilabNativeView__setCurrentSubWin(int iUID)
{
    ScilabView::setCurrentSubWin(iUID);
}

void ScilabNativeView__setCurrentObject(int iUID)
{
    ScilabView::setCurrentObject(iUID);
}

int ScilabNativeView__getValidDefaultFigureId()
{
    return ScilabView::getValidDefaultFigureId();
}

int ScilabNativeView__getFigureFromIndex(int figNum)
{
    return ScilabView::getFigureFromIndex(figNum);
}

/**
 * \}
 */

int ScilabView::getValidDefaultFigureId()
{
    if (m_figureList.empty())
    {
        return 0;
    }
    else
    {
        int max = INT_MIN;
        for (__figureList_iterator it = m_figureList.begin(); it != m_figureList.end(); ++it)
        {
            if (it->second > max)
            {
                max = it->second;
            }
        }

        return max + 1;
    }
}

bool ScilabView::isEmptyFigureList()
{
    return m_figureList.empty();
}

int ScilabView::getFigureFromIndex(int figNum)
{
    __figureList_iterator it;

    for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
    {
        if (it->second == figNum)
        {
            return it->first;
        }
    }
    return 0;
}

bool ScilabView::existsFigureId(int id)
{
    __figureList_iterator it;

    for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
    {
        if (it->second == id)
        {
            return true;
        }
    }
    return false;
}

void ScilabView::getFiguresId(int ids[])
{
    //__figureList_iterator it;
    //int i = (int)(m_figureList.size() - 1);

    //for (it = m_figureList.begin(); it != m_figureList.end(); ++it, --i)
    //{
    //    //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
    //    ids[i] = it->second;
    //}

    __figureList_reverse_iterator it;
    int i = (int)(m_figureList.size() - 1);

    for (it = m_figureList.rbegin(); it != m_figureList.rend(); ++it, --i)
    {
        //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
        ids[i] = it->second;
    }
}

int ScilabView::getNbFigure(void)
{
    return (int)m_figureList.size();
}

void ScilabView::createObject(int iUID)
{
    //std::cerr << "[ScilabView] ++ createObject UID=" << iUID << std::endl;
    int iType = -1;
    int *piType = &iType;

    getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void **)&piType);
    if (iType != -1 && iType == __GO_FIGURE__)
    {
        m_figureList[iUID] = -1;
        setCurrentFigure(iUID);
    }

    // Register object handle.
    getObjectHandle(iUID);

    PathItem* item = new PathItem();
    item->uid = iUID;

    m_pathList[iUID] = item;

    m_userdata[iUID]; //create an empty vector<int>

    //get existing information from current object
    updateObject(iUID, __GO_PARENT__);
    updateObject(iUID, __GO_CHILDREN__);
    updateObject(iUID, __GO_TAG__);
}

void ScilabView::deleteObject(int iUID)
{
    //std::cerr << "[ScilabView] -- deleteObject UID=" << iUID << std::endl;
    int iType = -1;
    int *piType = &iType;
    int iParentUID = 0;

    /* get USEr_DATA property and free it*/
    int iUserDataSize = 0;
    int *piUserDataSize = &iUserDataSize;
    int *piUserData = NULL;
    getGraphicObjectProperty(iUID, __GO_USER_DATA_SIZE__, jni_int, (void **)&piUserDataSize);
    getGraphicObjectProperty(iUID, __GO_USER_DATA__, jni_int_vector, (void **)&piUserData);

    if (piUserData && piUserDataSize)
    {
        types::InternalType* pUD = (types::InternalType*)sciReturnUserData(piUserData, iUserDataSize);
        pUD->DecreaseRef();
        pUD->killMe();
    }
    /*
    ** If deleting a figure, remove from figure list.
    */
    m_figureList.erase(iUID);

    /*
    ** If deleting current figure find another current one,
    ** if there is no more figure : NULL
    */
    if (m_currentFigure == iUID) // Deleting current figure
    {
        int iAxesUID = 0;
        int* piAxesUID = &iAxesUID;

        if (getNbFigure() != 0)
        {
            m_currentFigure = m_figureList.begin()->first;
            getGraphicObjectProperty(m_currentFigure, __GO_SELECTED_CHILD__, jni_int,  (void**)&piAxesUID);
            setCurrentSubWin(iAxesUID);
        }
        else
        {
            setCurrentFigure(0);
            setCurrentSubWin(0);
        }
    }

    /*
    ** If deleting current entity, set parent as new current.
    */
    if (m_currentObject == iUID) // Deleting current object
    {
        iParentUID = getParentObject(iUID);
        setCurrentObject(iParentUID);
    }

    // Remove the corresponding handle.
    __handleList_iterator it = m_handleList.find(iUID);
    if (it != m_handleList.end())
    {
        m_uidList.erase(it->second);
        m_handleList.erase(it);
    }

    deleteDataObject(iUID);

    /*clear userdata object*/
    m_userdata.erase(iUID);

    //clear path object
    __pathList_iterator itPath = m_pathList.find(iUID);
    if (itPath != m_pathList.end())
    {
        delete itPath->second; //destroy PathItem object
        m_pathList.erase(itPath); //remove entry
    }
}

void ScilabView::updateObject(int iUID, int iProperty)
{
    //std::cerr << "[ScilabView] == updateObject UID=" << iUID << " PROPERTY=" << pstProperty << std::endl;

    /*
    ** Take care of update if the value update is ID and object type is a Figure I manage.
    */
    switch (iProperty)
    {
        case __GO_ID__ :
        {
            if (m_figureList.find(iUID) != m_figureList.end())
            {
                int iNewId = 0;
                int *piNewId = &iNewId;

                getGraphicObjectProperty(iUID, __GO_ID__, jni_int, (void **)&piNewId);

                m_figureList[iUID] = iNewId;
                //std::cerr << "### [ScilabView] updateMap UID=" << iUID << " id=" << iNewId << std::endl;
            }
            break;
        }
        case __GO_CHILDREN__ :
        {
            int childrenCount = 0;
            int* pChildrenCount = &childrenCount;
            getGraphicObjectProperty(iUID, __GO_CHILDREN_COUNT__, jni_int, (void**)&pChildrenCount);

            __pathList_iterator it = m_pathList.find(iUID);
            if (it != m_pathList.end())
            {
                //update existing item

                PathItem* item = (*it).second;
                //reset children
                item->children.clear();
                if (childrenCount != 0)
                {
                    int* children = NULL;
                    getGraphicObjectProperty(iUID, __GO_CHILDREN__, jni_int_vector, (void**)&children);
                    item->children.assign(children, children + childrenCount);
                    releaseGraphicObjectProperty(__GO_CHILDREN__, children, jni_int_vector, childrenCount);
                }
            }
            break;
        }
        case __GO_PARENT__ :
        {
            int iParent = 0;
            int* piParent = &iParent;
            getGraphicObjectProperty(iUID, __GO_PARENT__, jni_int, (void**)&piParent);

            __pathList_iterator it = m_pathList.find(iUID);

            if (it != m_pathList.end())
            {
                //update existing item

                PathItem* item = (*it).second;
                item->parent = iParent;
            }
            break;
        }
        case __GO_TAG__ :
        {
            int iType = 0;
            int* piType = &iType;
            getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void**)&piType);

            char* tag = NULL;
            getGraphicObjectProperty(iUID, __GO_TAG__, jni_string, (void**)&tag);

            if (tag[0] != 0 && iType == __GO_FIGURE__)
            {
                //not empty string

                //add figure in list of path starter
                m_pathFigList[tag] = iUID;
            }

            __pathList_iterator it = m_pathList.find(iUID);

            if (it != m_pathList.end())
            {
                //update existing item

                PathItem* item = (*it).second;
                item->tag = tag;
                free(tag);
            }
            break;
        }
        default:
            break;
    }
}

/*
** Register ScilabView to Controller.
** Must be done after Graphics models are created.
*/
void ScilabView::registerToController(void)
{
    org_scilab_modules_graphic_objects::CallGraphicController::registerScilabView(getScilabJavaVM());
}

/*
** Reove ScilabView from Controller.
*/
void ScilabView::unregisterToController(void)
{
    org_scilab_modules_graphic_objects::CallGraphicController::unregisterScilabView(getScilabJavaVM());
}

/*
** Set Current Figure UID
*/
void ScilabView::setCurrentFigure(int UID)
{
    m_currentFigure = UID;
}

/*
** Get Current Figure UID
*/
int ScilabView::getCurrentFigure()
{
    //std::cerr << "[ScilaView] currentFigure = " << (m_currentFigure == 0 ? "NULL !!" : m_currentFigure) << std::endl;
    return m_currentFigure;
}

/*
** Set Current Object UID
*/
void ScilabView::setCurrentObject(int UID)
{
    m_currentObject = UID;
}

/*
** Get Current Figure UID
*/
int ScilabView::getCurrentObject()
{
    //std::cerr << "[ScilaView] currentObject = " << m_currentObject << std::endl;
    return m_currentObject;
}

/*
** Set Current SubWin UID
*/
void ScilabView::setCurrentSubWin(int UID)
{
    m_currentSubWin = UID;
}

/*
** Get Current Figure UID
*/
int ScilabView::getCurrentSubWin()
{
    //std::cerr << "[ScilaView] currentSubWin = " << m_currentSubWin << std::endl;
    return m_currentSubWin;
}

/*
** Scilab only can store long as handle
*/
long ScilabView::getObjectHandle(int UID)
{
    /*
     * if (UID != NULL)
     * {
     * std::cerr << "UID = " << UID << std::endl;
     * }
     * else
     * {
     * std::cerr << "UID is null :-S" << std::endl;
     * }
     * __handleList_iterator it2;
     * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
     * for (it2 = m_handleList.begin() ; it2 != m_handleList.end() ; ++it2)
     * {
     * std::cerr << "UID " << it2->first << " <-> handle " << it2->second << std::endl;
     * }
     * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
     */
    __handleList_iterator it = m_handleList.find(UID);

    if (it != m_handleList.end())
    {
        return it->second;
    }

    // increase maximum value
    // register new handle and return it.
    m_topHandleValue++;
    m_handleList[UID] = m_topHandleValue;
    m_uidList[m_topHandleValue] = UID;

    return m_topHandleValue;
}

int ScilabView::getObjectFromHandle(long handle)
{
    __uidList_iterator it = m_uidList.find(handle);
    if (it == m_uidList.end())
    {
        return 0;
    }

    return it->second;
}

int ScilabView::getFigureModel(void)
{
    //std::cerr << "[ScilabView] getFigureModel = " << (m_figureModel == 0 ? "!! NULL !!" : m_figureModel) << std::endl;
    return m_figureModel;
}

void ScilabView::setFigureModel(int UID)
{
    m_figureModel = UID;
}

int ScilabView::getAxesModel(void)
{
    //std::cerr << "[ScilabView] getAxesModel = " << (m_axesModel == 0 ? "!! NULL !!" : m_axesModel) << std::endl;
    return m_axesModel;
}

void ScilabView::setAxesModel(int UID)
{
    m_axesModel = UID;
}

PathItem* ScilabView::getItem(int uid)
{
    __pathList_iterator it = m_pathList.find(uid);
    if (it != m_pathList.end())
    {
        return it->second;
    }

    return NULL;
}

PathItem* ScilabView::getItem(std::string _pstTag)
{
    std::list<int> ignored;
    return getItem(_pstTag, ignored);
}

PathItem* ScilabView::getItem(std::string _pstTag, std::list<int>& _ignoredList)
{
    __pathList_iterator it = m_pathList.begin();
    for (; it != m_pathList.end(); it++)
    {
        PathItem * item = it->second;
        if (item->tag == _pstTag)
        {
            if (std::find(_ignoredList.begin(), _ignoredList.end(), item->uid) == _ignoredList.end())
            {
                return item;
            }
        }
    }
    return NULL;
}

PathItem* ScilabView::getFigureItem(std::string _pstTag)
{

    __pathFigList_iterator it = m_pathFigList.find(_pstTag);
    if (it != m_pathFigList.end())
    {
        return getItem(it->second);
    }

    return NULL;
}

int ScilabView::search_path(const char* _pstPath)
{
    //copy string to protect it against strtok
    char* pstPath = strdup(_pstPath);
    std::list<int> ignoredList;
    PathItem* path = NULL;
    char* pstSubPath = strtok(pstPath, "/");
    bool bDeep = false;
    while (pstSubPath != NULL)
    {
        if (pstSubPath[0] == 0)
        {
            //"" ?
            break;
        }

        if (pstSubPath[0] != '*')
        {
            //search in direct children
            if (path == NULL)
            {
                path = ScilabView::getFigureItem(pstSubPath);
                if (path == NULL)
                {
                    path = ScilabView::getItem(pstSubPath, ignoredList);
                    if (path == NULL)
                    {
                        break;
                    }
                }

                //if figure is in ignore list, reeturn not found
                if (std::find(ignoredList.begin(), ignoredList.end(), path->uid) != ignoredList.end())
                {
                    return 0;
                }
            }
            else
            {
                PathItem* newPath = search_children(path, pstSubPath, bDeep, ignoredList);
                if (newPath == NULL)
                {
                    //flag handle to ignore and restart parsing
                    ignoredList.push_back(path->uid);
                    pstPath = strdup(_pstPath);
                    pstSubPath = strtok(pstPath, "/");
                    path = NULL;
                    continue;
                }
                else
                {
                    path = newPath;
                }

                bDeep = false;
            }
        }
        else
        {
            //search in all path children
            bDeep = true;
        }

        pstSubPath = strtok(NULL, "/");
    }

    if (path == NULL)
    {
        return 0;
    }

    return path->uid;
}

PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, bool _bDeep, std::list<int>& _ignoredList)
{
    PathItem::__child_iterator it = _path->children.begin();
    for (; it != _path->children.end() ; it++)
    {
        PathItem* child = ScilabView::getItem(*it);
        if (child->tag == _subPath)
        {
            bool ignored = false;
            //check if this handle is not in ignoredList
            if (std::find(_ignoredList.begin(), _ignoredList.end(), child->uid) == _ignoredList.end())
            {
                return child;
            }
        }
        else if (_bDeep)
        {
            PathItem *item = search_children(child, _subPath, _bDeep, _ignoredList);
            if (item)
            {
                return item;
            }
        }
    }

    return NULL;
}

std::string ScilabView::get_path(int uid)
{
    PathItem* item = getItem(uid);
    if (item->tag == "")
    {
        //impossible to create a useful path from object without tag
        return "";
    }

    std::string path = item->tag;

    while (item->parent != 0)
    {
        item = getItem(item->parent);
        if (item->tag == "")
        {
            if (path[0] == '*')
            {
                //we have already */ just continue
                continue;
            }
            else
            {
                //add */ instead of /
                path = "*/" + path;
            }
        }
        else
        {
            path = item->tag + "/" + path;
        }
    }

    if (path[0] == '*')
    {
        //path must start by mane
        return "";
    }

    return path;
}

void ScilabView::setUserdata(int _id, int* _data, int _datasize)
{
    m_userdata[_id] = std::vector<int>(_data, _data + _datasize);
}

int ScilabView::getUserdataSize(int _id)
{
    return (int)m_userdata[_id].size();
}

int* ScilabView::getUserdata(int _id)
{
    std::vector<int> &vect = m_userdata[_id];
    int size = (int)vect.size();

    if (size != 0)
    {
        return &(vect[0]);
    }

    //empty userdata must be == NULL
    return NULL;
}

/*
** Allocate static class variable.
*/
ScilabView::__figureList ScilabView::m_figureList;
ScilabView::__handleList ScilabView::m_handleList;
ScilabView::__uidList ScilabView::m_uidList;
long ScilabView::m_topHandleValue = 0;
int ScilabView::m_currentFigure;
int ScilabView::m_currentObject;
int ScilabView::m_currentSubWin;
int ScilabView::m_figureModel;
int ScilabView::m_axesModel;
ScilabView::__pathList ScilabView::m_pathList;
ScilabView::__pathFigList ScilabView::m_pathFigList;
ScilabView::__userdata ScilabView::m_userdata;