2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
23 #include "internal.hxx"
24 #include "ScilabView.hxx"
25 #include "CallGraphicController.hxx"
26 #include "returnProperty.h"
30 #include "CurrentObject.h"
31 #include "createGraphicObject.h"
32 #include "setGraphicObjectProperty.h"
33 #include "getGraphicObjectProperty.h"
34 #include "graphicObjectProperties.h"
35 #include "getScilabJavaVM.h"
36 #include "deleteGraphicObject.h"
37 #include "sci_malloc.h"
41 * C Wrapping functions
44 void ScilabNativeView__createObject(int iUID)
46 ScilabView::createObject(iUID);
49 void ScilabNativeView__deleteObject(int iUID)
51 ScilabView::deleteObject(iUID);
54 void ScilabNativeView__updateObject(int iUID, int iProperty)
56 ScilabView::updateObject(iUID, iProperty);
59 void ScilabNativeView__setCurrentFigure(int iUID)
61 ScilabView::setCurrentFigure(iUID);
64 void ScilabNativeView__setCurrentSubWin(int iUID)
66 ScilabView::setCurrentSubWin(iUID);
69 void ScilabNativeView__setCurrentObject(int iUID)
71 ScilabView::setCurrentObject(iUID);
74 int ScilabNativeView__getValidDefaultFigureId()
76 return ScilabView::getValidDefaultFigureId();
79 int ScilabNativeView__getFigureFromIndex(int figNum)
81 return ScilabView::getFigureFromIndex(figNum);
88 int ScilabView::getValidDefaultFigureId()
90 if (m_figureList.empty())
97 for (__figureList_iterator it = m_figureList.begin(); it != m_figureList.end(); ++it)
109 bool ScilabView::isEmptyFigureList()
111 return m_figureList.empty();
114 int ScilabView::getFigureFromIndex(int figNum)
116 __figureList_iterator it;
118 for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
120 if (it->second == figNum)
128 bool ScilabView::existsFigureId(int id)
130 __figureList_iterator it;
132 for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
134 if (it->second == id)
142 void ScilabView::getFiguresId(int ids[])
144 //__figureList_iterator it;
145 //int i = (int)(m_figureList.size() - 1);
147 //for (it = m_figureList.begin(); it != m_figureList.end(); ++it, --i)
149 // //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
150 // ids[i] = it->second;
153 __figureList_reverse_iterator it;
154 int i = (int)(m_figureList.size() - 1);
156 for (it = m_figureList.rbegin(); it != m_figureList.rend(); ++it, --i)
158 //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
163 int ScilabView::getNbFigure(void)
165 return (int)m_figureList.size();
168 void ScilabView::createObject(int iUID)
170 //std::cerr << "[ScilabView] ++ createObject UID=" << iUID << std::endl;
172 int *piType = &iType;
174 getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void **)&piType);
175 if (iType != -1 && iType == __GO_FIGURE__)
177 m_figureList[iUID] = -1;
178 setCurrentFigure(iUID);
181 // Register object handle.
182 getObjectHandle(iUID);
184 PathItem* item = new PathItem();
187 m_pathList[iUID] = item;
189 m_userdata[iUID]; //create an empty vector<int>
191 //get existing information from current object
192 updateObject(iUID, __GO_PARENT__);
193 updateObject(iUID, __GO_CHILDREN__);
194 updateObject(iUID, __GO_TAG__);
197 void ScilabView::deleteObject(int iUID)
199 //std::cerr << "[ScilabView] -- deleteObject UID=" << iUID << std::endl;
201 int *piType = &iType;
204 /* get USEr_DATA property and free it*/
205 int iUserDataSize = 0;
206 int *piUserDataSize = &iUserDataSize;
207 int *piUserData = NULL;
208 getGraphicObjectProperty(iUID, __GO_USER_DATA_SIZE__, jni_int, (void **)&piUserDataSize);
209 getGraphicObjectProperty(iUID, __GO_USER_DATA__, jni_int_vector, (void **)&piUserData);
211 if (piUserData && piUserDataSize)
213 types::InternalType* pUD = (types::InternalType*)sciReturnUserData(piUserData, iUserDataSize);
218 ** If deleting a figure, remove from figure list.
220 m_figureList.erase(iUID);
223 ** If deleting current figure find another current one,
224 ** if there is no more figure : NULL
226 if (m_currentFigure == iUID) // Deleting current figure
229 int* piAxesUID = &iAxesUID;
231 if (getNbFigure() != 0)
233 m_currentFigure = m_figureList.begin()->first;
234 getGraphicObjectProperty(m_currentFigure, __GO_SELECTED_CHILD__, jni_int, (void**)&piAxesUID);
235 setCurrentSubWin(iAxesUID);
245 ** If deleting current entity, set parent as new current.
247 if (m_currentObject == iUID) // Deleting current object
249 iParentUID = getParentObject(iUID);
250 setCurrentObject(iParentUID);
253 // Remove the corresponding handle.
254 __handleList_iterator it = m_handleList.find(iUID);
255 if (it != m_handleList.end())
257 m_uidList.erase(it->second);
258 m_handleList.erase(it);
261 deleteDataObject(iUID);
263 /*clear userdata object*/
264 m_userdata.erase(iUID);
267 __pathList_iterator itPath = m_pathList.find(iUID);
268 if (itPath != m_pathList.end())
270 delete itPath->second; //destroy PathItem object
271 m_pathList.erase(itPath); //remove entry
275 void ScilabView::updateObject(int iUID, int iProperty)
277 //std::cerr << "[ScilabView] == updateObject UID=" << iUID << " PROPERTY=" << pstProperty << std::endl;
280 ** Take care of update if the value update is ID and object type is a Figure I manage.
286 if (m_figureList.find(iUID) != m_figureList.end())
289 int *piNewId = &iNewId;
291 getGraphicObjectProperty(iUID, __GO_ID__, jni_int, (void **)&piNewId);
293 m_figureList[iUID] = iNewId;
294 //std::cerr << "### [ScilabView] updateMap UID=" << iUID << " id=" << iNewId << std::endl;
298 case __GO_CHILDREN__ :
300 int childrenCount = 0;
301 int* pChildrenCount = &childrenCount;
302 getGraphicObjectProperty(iUID, __GO_CHILDREN_COUNT__, jni_int, (void**)&pChildrenCount);
304 __pathList_iterator it = m_pathList.find(iUID);
305 if (it != m_pathList.end())
307 //update existing item
309 PathItem* item = (*it).second;
311 item->children.clear();
312 if (childrenCount != 0)
314 int* children = NULL;
315 getGraphicObjectProperty(iUID, __GO_CHILDREN__, jni_int_vector, (void**)&children);
316 item->children.assign(children, children + childrenCount);
317 releaseGraphicObjectProperty(__GO_CHILDREN__, children, jni_int_vector, childrenCount);
325 int* piParent = &iParent;
326 getGraphicObjectProperty(iUID, __GO_PARENT__, jni_int, (void**)&piParent);
328 __pathList_iterator it = m_pathList.find(iUID);
330 if (it != m_pathList.end())
332 //update existing item
334 PathItem* item = (*it).second;
335 item->parent = iParent;
342 int* piType = &iType;
343 getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void**)&piType);
346 getGraphicObjectProperty(iUID, __GO_TAG__, jni_string, (void**)&tag);
348 if (tag[0] != 0 && iType == __GO_FIGURE__)
352 //add figure in list of path starter
353 m_pathFigList[tag] = iUID;
356 __pathList_iterator it = m_pathList.find(iUID);
358 if (it != m_pathList.end())
360 //update existing item
362 PathItem* item = (*it).second;
374 ** Register ScilabView to Controller.
375 ** Must be done after Graphics models are created.
377 void ScilabView::registerToController(void)
379 org_scilab_modules_graphic_objects::CallGraphicController::registerScilabView(getScilabJavaVM());
383 ** Reove ScilabView from Controller.
385 void ScilabView::unregisterToController(void)
387 org_scilab_modules_graphic_objects::CallGraphicController::unregisterScilabView(getScilabJavaVM());
391 ** Set Current Figure UID
393 void ScilabView::setCurrentFigure(int UID)
395 m_currentFigure = UID;
399 ** Get Current Figure UID
401 int ScilabView::getCurrentFigure()
403 //std::cerr << "[ScilaView] currentFigure = " << (m_currentFigure == 0 ? "NULL !!" : m_currentFigure) << std::endl;
404 return m_currentFigure;
408 ** Set Current Object UID
410 void ScilabView::setCurrentObject(int UID)
412 m_currentObject = UID;
416 ** Get Current Figure UID
418 int ScilabView::getCurrentObject()
420 //std::cerr << "[ScilaView] currentObject = " << m_currentObject << std::endl;
421 return m_currentObject;
425 ** Set Current SubWin UID
427 void ScilabView::setCurrentSubWin(int UID)
429 m_currentSubWin = UID;
433 ** Get Current Figure UID
435 int ScilabView::getCurrentSubWin()
437 //std::cerr << "[ScilaView] currentSubWin = " << m_currentSubWin << std::endl;
438 return m_currentSubWin;
442 ** Scilab only can store long as handle
444 long ScilabView::getObjectHandle(int UID)
449 * std::cerr << "UID = " << UID << std::endl;
453 * std::cerr << "UID is null :-S" << std::endl;
455 * __handleList_iterator it2;
456 * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
457 * for (it2 = m_handleList.begin() ; it2 != m_handleList.end() ; ++it2)
459 * std::cerr << "UID " << it2->first << " <-> handle " << it2->second << std::endl;
461 * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
463 __handleList_iterator it = m_handleList.find(UID);
465 if (it != m_handleList.end())
470 // increase maximum value
471 // register new handle and return it.
473 m_handleList[UID] = m_topHandleValue;
474 m_uidList[m_topHandleValue] = UID;
476 return m_topHandleValue;
479 int ScilabView::getObjectFromHandle(long handle)
481 __uidList_iterator it = m_uidList.find(handle);
482 if (it == m_uidList.end())
490 int ScilabView::getFigureModel(void)
492 //std::cerr << "[ScilabView] getFigureModel = " << (m_figureModel == 0 ? "!! NULL !!" : m_figureModel) << std::endl;
493 return m_figureModel;
496 void ScilabView::setFigureModel(int UID)
501 int ScilabView::getAxesModel(void)
503 //std::cerr << "[ScilabView] getAxesModel = " << (m_axesModel == 0 ? "!! NULL !!" : m_axesModel) << std::endl;
507 void ScilabView::setAxesModel(int UID)
512 PathItem* ScilabView::getItem(int uid)
514 __pathList_iterator it = m_pathList.find(uid);
515 if (it != m_pathList.end())
523 PathItem* ScilabView::getItem(std::string _pstTag)
525 std::list<int> ignored;
526 return getItem(_pstTag, ignored);
529 PathItem* ScilabView::getItem(std::string _pstTag, std::list<int>& _ignoredList)
531 __pathList_iterator it = m_pathList.begin();
532 for (; it != m_pathList.end(); it++)
534 PathItem * item = it->second;
535 if (item->tag == _pstTag)
537 if (std::find(_ignoredList.begin(), _ignoredList.end(), item->uid) == _ignoredList.end())
546 PathItem* ScilabView::getFigureItem(std::string _pstTag)
549 __pathFigList_iterator it = m_pathFigList.find(_pstTag);
550 if (it != m_pathFigList.end())
552 return getItem(it->second);
558 int ScilabView::search_path(char* _pstPath)
560 //copy string to protect it against strtok
561 char* pstPath = strdup(_pstPath);
562 std::list<int> ignoredList;
563 PathItem* path = NULL;
564 char* pstSubPath = strtok(pstPath, "/");
566 while (pstSubPath != NULL)
568 if (pstSubPath[0] == 0)
574 if (pstSubPath[0] != '*')
576 //search in direct children
579 path = ScilabView::getFigureItem(pstSubPath);
582 path = ScilabView::getItem(pstSubPath, ignoredList);
589 //if figure is in ignore list, reeturn not found
590 if (std::find(ignoredList.begin(), ignoredList.end(), path->uid) != ignoredList.end())
597 PathItem* newPath = search_children(path, pstSubPath, bDeep, ignoredList);
600 //flag handle to ignore and restart parsing
601 ignoredList.push_back(path->uid);
602 pstPath = strdup(_pstPath);
603 pstSubPath = strtok(pstPath, "/");
617 //search in all path children
621 pstSubPath = strtok(NULL, "/");
633 PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, bool _bDeep, std::list<int>& _ignoredList)
635 PathItem::__child_iterator it = _path->children.begin();
636 for (; it != _path->children.end() ; it++)
638 PathItem* child = ScilabView::getItem(*it);
639 if (child->tag == _subPath)
641 bool ignored = false;
642 //check if this handle is not in ignoredList
643 if (std::find(_ignoredList.begin(), _ignoredList.end(), child->uid) == _ignoredList.end())
650 PathItem *item = search_children(child, _subPath, _bDeep, _ignoredList);
661 std::string ScilabView::get_path(int uid)
663 PathItem* item = getItem(uid);
666 //impossible to create a useful path from object without tag
670 std::string path = item->tag;
672 while (item->parent != 0)
674 item = getItem(item->parent);
679 //we have already */ just continue
684 //add */ instead of /
690 path = item->tag + "/" + path;
696 //path must start by mane
703 void ScilabView::setUserdata(int _id, int* _data, int _datasize)
705 m_userdata[_id] = std::vector<int>(_data, _data + _datasize);
708 int ScilabView::getUserdataSize(int _id)
710 return (int)m_userdata[_id].size();
713 int* ScilabView::getUserdata(int _id)
715 std::vector<int> &vect = m_userdata[_id];
716 int size = (int)vect.size();
723 //empty userdata must be == NULL
728 ** Allocate static class variable.
730 ScilabView::__figureList ScilabView::m_figureList;
731 ScilabView::__handleList ScilabView::m_handleList;
732 ScilabView::__uidList ScilabView::m_uidList;
733 long ScilabView::m_topHandleValue = 0;
734 int ScilabView::m_currentFigure;
735 int ScilabView::m_currentObject;
736 int ScilabView::m_currentSubWin;
737 int ScilabView::m_figureModel;
738 int ScilabView::m_axesModel;
739 ScilabView::__pathList ScilabView::m_pathList;
740 ScilabView::__pathFigList ScilabView::m_pathFigList;
741 ScilabView::__userdata ScilabView::m_userdata;