2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
20 #include "ScilabView.hxx"
21 #include "CallGraphicController.hxx"
25 #include "CurrentObject.h"
26 #include "createGraphicObject.h"
27 #include "setGraphicObjectProperty.h"
28 #include "getGraphicObjectProperty.h"
29 #include "graphicObjectProperties.h"
30 #include "getScilabJavaVM.h"
31 #include "deleteGraphicObject.h"
36 * C Wrapping functions
39 void ScilabNativeView__createObject(int iUID)
41 ScilabView::createObject(iUID);
44 void ScilabNativeView__deleteObject(int iUID)
46 ScilabView::deleteObject(iUID);
49 void ScilabNativeView__updateObject(int iUID, int iProperty)
51 ScilabView::updateObject(iUID, iProperty);
54 void ScilabNativeView__setCurrentFigure(int iUID)
56 ScilabView::setCurrentFigure(iUID);
59 void ScilabNativeView__setCurrentSubWin(int iUID)
61 ScilabView::setCurrentSubWin(iUID);
64 void ScilabNativeView__setCurrentObject(int iUID)
66 ScilabView::setCurrentObject(iUID);
69 int ScilabNativeView__getValidDefaultFigureId()
71 return ScilabView::getValidDefaultFigureId();
78 int ScilabView::getValidDefaultFigureId()
80 if (m_figureList.empty())
87 for (__figureList_iterator it = m_figureList.begin(); it != m_figureList.end(); ++it)
99 bool ScilabView::isEmptyFigureList()
101 return m_figureList.empty();
104 int ScilabView::getFigureFromIndex(int figNum)
106 __figureList_iterator it;
108 for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
110 if (it->second == figNum)
118 bool ScilabView::existsFigureId(int id)
120 __figureList_iterator it;
122 for (it = m_figureList.begin(); it != m_figureList.end(); ++it)
124 if (it->second == id)
132 void ScilabView::getFiguresId(int ids[])
134 //__figureList_iterator it;
135 //int i = (int)(m_figureList.size() - 1);
137 //for (it = m_figureList.begin(); it != m_figureList.end(); ++it, --i)
139 // //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
140 // ids[i] = it->second;
143 __figureList_reverse_iterator it;
144 int i = (int)(m_figureList.size() - 1);
146 for (it = m_figureList.rbegin(); it != m_figureList.rend(); ++it, --i)
148 //std::cerr << "[ScilabView] DEBUG " << it->first << " <-> " << it->second << std::endl;
153 int ScilabView::getNbFigure(void)
155 return (int)m_figureList.size();
158 void ScilabView::createObject(int iUID)
160 //std::cerr << "[ScilabView] ++ createObject UID=" << iUID << std::endl;
162 int *piType = &iType;
164 getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void **)&piType);
165 if (iType != -1 && iType == __GO_FIGURE__)
167 m_figureList[iUID] = -1;
168 setCurrentFigure(iUID);
171 // Register object handle.
172 getObjectHandle(iUID);
174 PathItem* item = new PathItem();
177 m_pathList[iUID] = item;
179 m_userdata[iUID]; //create an empty vector<int>
181 //get existing information from current object
182 updateObject(iUID, __GO_PARENT__);
183 updateObject(iUID, __GO_CHILDREN__);
184 updateObject(iUID, __GO_TAG__);
187 void ScilabView::deleteObject(int iUID)
189 //std::cerr << "[ScilabView] -- deleteObject UID=" << iUID << std::endl;
191 int *piType = &iType;
195 ** If deleting a figure, remove from figure list.
197 m_figureList.erase(iUID);
200 ** If deleting current figure find another current one,
201 ** if there is no more figure : NULL
203 if (m_currentFigure == iUID) // Deleting current figure
206 int* piAxesUID = &iAxesUID;
208 if (getNbFigure() != 0)
210 m_currentFigure = m_figureList.begin()->first;
211 getGraphicObjectProperty(m_currentFigure, __GO_SELECTED_CHILD__, jni_int, (void**)&piAxesUID);
212 setCurrentSubWin(iAxesUID);
222 ** If deleting current entity, set parent as new current.
224 if (m_currentObject == iUID) // Deleting current object
226 iParentUID = getParentObject(iUID);
227 setCurrentObject(iParentUID);
230 // Remove the corresponding handle.
231 __handleList_iterator it = m_handleList.find(iUID);
232 if (it != m_handleList.end())
234 m_uidList.erase(it->second);
235 m_handleList.erase(it);
238 deleteDataObject(iUID);
240 m_pathList.erase(iUID);
241 m_userdata.erase(iUID);
244 void ScilabView::updateObject(int iUID, int iProperty)
246 //std::cerr << "[ScilabView] == updateObject UID=" << iUID << " PROPERTY=" << pstProperty << std::endl;
249 ** Take care of update if the value update is ID and object type is a Figure I manage.
255 if (m_figureList.find(iUID) != m_figureList.end())
258 int *piNewId = &iNewId;
260 getGraphicObjectProperty(iUID, __GO_ID__, jni_int, (void **)&piNewId);
262 m_figureList[iUID] = iNewId;
263 //std::cerr << "### [ScilabView] updateMap UID=" << iUID << " id=" << iNewId << std::endl;
267 case __GO_CHILDREN__ :
269 int childrenCount = 0;
270 int* pChildrenCount = &childrenCount;
271 getGraphicObjectProperty(iUID, __GO_CHILDREN_COUNT__, jni_int, (void**)&pChildrenCount);
273 __pathList_iterator it = m_pathList.find(iUID);
274 if (it != m_pathList.end())
276 //update existing item
278 PathItem* item = (*it).second;
280 item->children.clear();
281 if (childrenCount != 0)
283 int* children = NULL;
284 getGraphicObjectProperty(iUID, __GO_CHILDREN__, jni_int_vector, (void**)&children);
285 item->children.assign(children, children + childrenCount);
293 int* piParent = &iParent;
294 getGraphicObjectProperty(iUID, __GO_PARENT__, jni_int, (void**)&piParent);
296 __pathList_iterator it = m_pathList.find(iUID);
298 if (it != m_pathList.end())
300 //update existing item
302 PathItem* item = (*it).second;
303 item->parent = iParent;
310 int* piType = &iType;
311 getGraphicObjectProperty(iUID, __GO_TYPE__, jni_int, (void**)&piType);
314 getGraphicObjectProperty(iUID, __GO_TAG__, jni_string, (void**)&tag);
316 if (tag[0] != 0 && iType == __GO_FIGURE__)
320 //add figure in list of path starter
321 m_pathFigList[tag] = iUID;
324 __pathList_iterator it = m_pathList.find(iUID);
326 if (it != m_pathList.end())
328 //update existing item
330 PathItem* item = (*it).second;
342 ** Register ScilabView to Controller.
343 ** Must be done after Graphics models are created.
345 void ScilabView::registerToController(void)
347 org_scilab_modules_graphic_objects::CallGraphicController::registerScilabView(getScilabJavaVM());
348 m_figureList.get_allocator().allocate(4096);
349 m_handleList.get_allocator().allocate(4096);
350 m_uidList.get_allocator().allocate(4096);
354 ** Reove ScilabView from Controller.
356 void ScilabView::unregisterToController(void)
358 org_scilab_modules_graphic_objects::CallGraphicController::unregisterScilabView(getScilabJavaVM());
362 ** Set Current Figure UID
364 void ScilabView::setCurrentFigure(int UID)
366 m_currentFigure = UID;
370 ** Get Current Figure UID
372 int ScilabView::getCurrentFigure()
374 //std::cerr << "[ScilaView] currentFigure = " << (m_currentFigure == 0 ? "NULL !!" : m_currentFigure) << std::endl;
375 return m_currentFigure;
379 ** Set Current Object UID
381 void ScilabView::setCurrentObject(int UID)
383 m_currentObject = UID;
387 ** Get Current Figure UID
389 int ScilabView::getCurrentObject()
391 //std::cerr << "[ScilaView] currentObject = " << m_currentObject << std::endl;
392 return m_currentObject;
396 ** Set Current SubWin UID
398 void ScilabView::setCurrentSubWin(int UID)
400 m_currentSubWin = UID;
404 ** Get Current Figure UID
406 int ScilabView::getCurrentSubWin()
408 //std::cerr << "[ScilaView] currentSubWin = " << m_currentSubWin << std::endl;
409 return m_currentSubWin;
413 ** Scilab only can store long as handle
415 long ScilabView::getObjectHandle(int UID)
420 * std::cerr << "UID = " << UID << std::endl;
424 * std::cerr << "UID is null :-S" << std::endl;
426 * __handleList_iterator it2;
427 * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
428 * for (it2 = m_handleList.begin() ; it2 != m_handleList.end() ; ++it2)
430 * std::cerr << "UID " << it2->first << " <-> handle " << it2->second << std::endl;
432 * std::cerr << "[DEBUG] +++ handleMap +++" << std::endl;
434 __handleList_iterator it = m_handleList.find(UID);
436 if (it != m_handleList.end())
441 // increase maximum value
442 // register new handle and return it.
444 m_handleList[UID] = m_topHandleValue;
445 m_uidList[m_topHandleValue] = UID;
447 return m_topHandleValue;
450 int ScilabView::getObjectFromHandle(long handle)
452 __uidList_iterator it = m_uidList.find(handle);
453 if (it == m_uidList.end())
461 int ScilabView::getFigureModel(void)
463 //std::cerr << "[ScilabView] getFigureModel = " << (m_figureModel == 0 ? "!! NULL !!" : m_figureModel) << std::endl;
464 return m_figureModel;
467 void ScilabView::setFigureModel(int UID)
472 int ScilabView::getAxesModel(void)
474 //std::cerr << "[ScilabView] getAxesModel = " << (m_axesModel == 0 ? "!! NULL !!" : m_axesModel) << std::endl;
478 void ScilabView::setAxesModel(int UID)
483 PathItem* ScilabView::getItem(int uid)
485 __pathList_iterator it = m_pathList.find(uid);
486 if (it != m_pathList.end())
494 PathItem* ScilabView::getItem(std::string _pstTag)
496 std::list<int> ignored;
497 return getItem(_pstTag, ignored);
500 PathItem* ScilabView::getItem(std::string _pstTag, std::list<int>& _ignoredList)
502 __pathList_iterator it = m_pathList.begin();
503 for (; it != m_pathList.end(); it++)
505 PathItem * item = it->second;
506 if (item->tag == _pstTag)
508 if (std::find(_ignoredList.begin(), _ignoredList.end(), item->uid) == _ignoredList.end())
517 PathItem* ScilabView::getFigureItem(std::string _pstTag)
520 __pathFigList_iterator it = m_pathFigList.find(_pstTag);
521 if (it != m_pathFigList.end())
523 return getItem(it->second);
529 int ScilabView::search_path(char* _pstPath)
531 //copy string to protect it against strtok
532 char* pstPath = strdup(_pstPath);
533 std::list<int> ignoredList;
534 PathItem* path = NULL;
535 char* pstSubPath = strtok(pstPath, "/");
537 while (pstSubPath != NULL)
539 if (pstSubPath[0] == 0)
545 if (pstSubPath[0] != '*')
547 //search in direct children
550 path = ScilabView::getFigureItem(pstSubPath);
553 path = ScilabView::getItem(pstSubPath, ignoredList);
560 //if figure is in ignore list, reeturn not found
561 if (std::find(ignoredList.begin(), ignoredList.end(), path->uid) != ignoredList.end())
568 PathItem* newPath = search_children(path, pstSubPath, bDeep, ignoredList);
571 //flag handle to ignore and restart parsing
572 ignoredList.push_back(path->uid);
573 pstPath = strdup(_pstPath);
574 pstSubPath = strtok(pstPath, "/");
588 //search in all path children
592 pstSubPath = strtok(NULL, "/");
603 PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, bool _bDeep, std::list<int>& _ignoredList)
605 PathItem::__child_iterator it = _path->children.begin();
606 for (; it != _path->children.end() ; it++)
608 PathItem* child = ScilabView::getItem(*it);
609 if (child->tag == _subPath)
611 bool ignored = false;
612 //check if this handle is not in ignoredList
613 if (std::find(_ignoredList.begin(), _ignoredList.end(), child->uid) == _ignoredList.end())
620 PathItem *item = search_children(child, _subPath, _bDeep, _ignoredList);
631 std::string ScilabView::get_path(int uid)
633 PathItem* item = getItem(uid);
636 //impossible to create a useful path from object without tag
640 std::string path = item->tag;
642 while (item->parent != 0)
644 item = getItem(item->parent);
649 //we have already */ just continue
654 //add */ instead of /
660 path = item->tag + "/" + path;
666 //path must start by mane
673 void ScilabView::setUserdata(int _id, int* _data, int _datasize)
675 m_userdata[_id] = std::vector<int>(_data, _data + _datasize);
678 int ScilabView::getUserdataSize(int _id)
680 return (int)m_userdata[_id].size();
683 int* ScilabView::getUserdata(int _id)
685 std::vector<int> &vect = m_userdata[_id];
686 int size = (int)vect.size();
693 //empty userdata must be == NULL
698 ** Allocate static class variable.
700 ScilabView::__figureList ScilabView::m_figureList = *new __figureList();
701 ScilabView::__handleList ScilabView::m_handleList = *new __handleList();
702 ScilabView::__uidList ScilabView::m_uidList = *new __uidList();
703 long ScilabView::m_topHandleValue = 0;
704 int ScilabView::m_currentFigure;
705 int ScilabView::m_currentObject;
706 int ScilabView::m_currentSubWin;
707 int ScilabView::m_figureModel;
708 int ScilabView::m_axesModel;
709 ScilabView::__pathList ScilabView::m_pathList = *new __pathList();
710 ScilabView::__pathFigList ScilabView::m_pathFigList = *new __pathFigList();
711 ScilabView::__userdata ScilabView::m_userdata = *new __userdata();