SofaGUI.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include "SofaGUI.h"
00028 #include <string.h>
00029 namespace sofa
00030 {
00031 
00032 namespace gui
00033 {
00034 
00035 const char* SofaGUI::programName = NULL;
00036 std::string SofaGUI::guiName = "";
00037 std::vector<std::string> SofaGUI::guiOptions;
00038 SofaGUI* SofaGUI::currentGUI = NULL;
00039 
00040 //std::list<SofaGUI::GUICreator> SofaGUI::guiCreators;
00041 std::list<SofaGUI::GUICreator>& SofaGUI::guiCreators()
00042 {
00043     static std::list<SofaGUI::GUICreator> creators;
00044     return creators;
00045 }
00046 
00047 void SofaGUI::SetProgramName(const char* argv0)
00048 {
00049     if (argv0)
00050         programName = argv0;
00051 }
00052 
00053 const char* SofaGUI::GetProgramName()
00054 {
00055     return programName;
00056 }
00057 
00058 std::vector<std::string> SofaGUI::ListSupportedGUI()
00059 {
00060     std::vector<std::string> names;
00061     for(std::list<GUICreator>::iterator it = guiCreators().begin(), itend = guiCreators().end(); it != itend; ++it)
00062     {
00063         names.push_back(it->name);
00064     }
00065     return names;
00066 }
00067 
00068 std::string SofaGUI::ListSupportedGUI(char separator)
00069 {
00070     std::string names;
00071     bool first = true;
00072     for(std::list<GUICreator>::iterator it = guiCreators().begin(), itend = guiCreators().end(); it != itend; ++it)
00073     {
00074         if (!first) names += separator; else first = false;
00075         names += it->name;
00076     }
00077     return names;
00078 }
00079 
00080 const char* SofaGUI::GetGUIName()
00081 {
00082     const char* name = guiName.c_str();
00083     if (!name[0] && !guiCreators().empty())
00084     {
00085         std::list<GUICreator>::iterator it = guiCreators().begin();
00086         std::list<GUICreator>::iterator itend = guiCreators().end();
00087         name = it->name;
00088         int prio = it->priority;
00089         while (++it != itend)
00090         {
00091             if (it->priority > prio)
00092             {
00093                 name = it->name;
00094                 prio = it->priority;
00095             }
00096         }
00097     }
00098     return name;
00099 }
00100 
00101 void SofaGUI::SetGUIName(const char* name)
00102 {
00103     guiName = name;
00104 }
00105 
00106 void SofaGUI::AddGUIOption(const char* option)
00107 {
00108     guiOptions.push_back(option);
00109 }
00110 
00111 SofaGUI::GUICreator* SofaGUI::GetGUICreator(const char* name)
00112 {
00113     if (!name) name = GetGUIName();
00114     std::list<GUICreator>::iterator it = guiCreators().begin();
00115     std::list<GUICreator>::iterator itend = guiCreators().end();
00116     while (it != itend && strcmp(name, it->name))
00117         ++it;
00118     if (it == itend)
00119     {
00120         std::cerr << "ERROR(SofaGUI): GUI "<<name<<" not found."<<std::endl;
00121         std::cerr << "Available GUIs:" << ListSupportedGUI(' ') << std::endl;
00122         return NULL;
00123     }
00124     else
00125         return &(*it);
00126 }
00127 
00128 SofaGUI* SofaGUI::CurrentGUI()
00129 {
00130     return currentGUI;
00131 }
00132 
00133 void SofaGUI::Redraw()
00134 {
00135     if (currentGUI) currentGUI->redraw();
00136 }
00137 
00138 sofa::simulation::Node* SofaGUI::CurrentSimulation()
00139 {
00140     if (currentGUI)
00141         return currentGUI->currentSimulation();
00142     else
00143         return NULL;
00144 }
00145 
00146 int SofaGUI::RegisterGUI(const char* name, CreateGUIFn* creator, InitGUIFn* init, int priority)
00147 {
00148     std::list<GUICreator>::iterator it = guiCreators().begin();
00149     std::list<GUICreator>::iterator itend = guiCreators().end();
00150     while (it != itend && strcmp(name, it->name))
00151         ++it;
00152     if (it != itend)
00153     {
00154         std::cerr << "ERROR(SofaGUI): GUI "<<name<<" duplicate registration."<<std::endl;
00155         return 1;
00156     }
00157     GUICreator entry;
00158     entry.name = name;
00159     entry.creator = creator;
00160     entry.init = init;
00161     entry.priority = priority;
00162     guiCreators().push_back(entry);
00163     return 0;
00164 }
00165 
00166 SofaGUI::SofaGUI()
00167 {
00168     if (currentGUI)
00169         std::cerr << "WARNING(SofaGUI): multiple concurrent active gui." << std::endl;
00170     else
00171         currentGUI = this;
00172 }
00173 
00174 SofaGUI::~SofaGUI()
00175 {
00176     if (currentGUI == this)
00177         currentGUI = NULL;
00178 }
00179 
00180 } // namespace gui
00181 
00182 } // namespace sofa

Generated on Sun Nov 23 02:28:39 2008 for Sofa by  doxygen 1.5.7.1