Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / client / src / interface_v3 / interface_observer.h
blob4a3884c9ce64c76a2ed3721778da56d2e56fe313
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef NL_INTERFACE_OBSERVER_H
23 #define NL_INTERFACE_OBSERVER_H
25 #include "nel/misc/types_nl.h"
26 #include "interface_manager.h"
27 #include "nel/misc/xml_auto_ptr.h"
30 /**
31 * interface to define a simple observer
32 * \author Nicolas Brigand
33 * \author Nevrax France
34 * \date 2002
36 class IInterfaceObserver : public NLMISC::ICDBNode::IPropertyObserver
38 public:
40 /**
41 * parse the element and initalize it
42 * \param cur : pointer to the node describing this element
43 * \param ctrl : pointer to the parent group
44 * \return true if success
46 virtual bool parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)=0;
48 /**
49 * observer update
51 virtual void update (NLMISC::ICDBNode* leaf)=0;
57 class IInterfaceObserverFactory;
59 /**
60 * manager for observer factory
61 * \author Nicolas Brigand
62 * \author Nevrax France
63 * \date 2002
65 class CInterfaceObserverFactoryManager
67 public:
68 typedef std::map< std::string, IInterfaceObserverFactory* > TFactoryMap;
69 static CInterfaceObserverFactoryManager* getInstance()
71 if (_GlobalInstance == NULL)
72 _GlobalInstance = new CInterfaceObserverFactoryManager;
73 return _GlobalInstance;
76 /// map of action handler factories
77 TFactoryMap FactoryMap;
79 private:
80 CInterfaceObserverFactoryManager(){}
81 static CInterfaceObserverFactoryManager *_GlobalInstance;
86 /**
87 * interface for observer factory
88 * \author Nicolas Brigand
89 * \author Nevrax France
90 * \date 2002
92 class IInterfaceObserverFactory
94 public:
97 /**
98 * build the desired observer from script
99 *\param idHandler : the string identifying the handler
100 *\return a pointer to the factory instance to be used
102 static IInterfaceObserver *create(xmlNodePtr node,CInterfaceGroup* parentGroup)
104 CInterfaceManager* iMngr = CInterfaceManager::getInstance();
105 CXMLAutoPtr idObserver((const char*)xmlGetProp(node,(char unsigned*)"action"));
106 if (!idObserver)
108 nlinfo("action not found in a observer node");
109 return NULL;
111 CXMLAutoPtr ptr((const char*) xmlGetProp (node, (xmlChar*)"data"));
112 if (!ptr)
114 nlinfo ("no data in a observer tag");
115 return NULL;
120 IInterfaceObserver* obs =NULL;
122 char * end = ptr.getDatas() + strlen( ptr.getDatas() );
123 char * dataTok = strtok( ptr.getDatas()," ,");
124 NLMISC::ICDBNode::CTextId textId;
126 while(dataTok)
128 std::string data (dataTok);
129 if (NLGUI::CDBManager::getInstance()->getDbProp(data,false) == NULL) // Full path provided ? No.
131 CInterfaceGroup *parent = parentGroup;
132 while (parent != NULL)
134 std::string sTmp;
135 if (data[0] == ':')
136 sTmp = parent->getId() + data;
137 else
138 sTmp = parent->getId() + ":" + data;
139 if (NLGUI::CDBManager::getInstance()->getDbProp(sTmp,false) != NULL)
141 data = sTmp;
142 break;
144 parent = parent->getParent();
146 if (NLGUI::CDBManager::getInstance()->getDbProp(data,false) == NULL)
148 std::string sTmp = std::string("data (")+std::string(dataTok)+std::string(") in a observer tag do not exist, CREATING!");
149 nlinfo (sTmp.c_str());
150 data = (const char*)dataTok;
151 NLGUI::CDBManager::getInstance()->getDbProp (data, true);
154 if ( !obs )
156 CInterfaceObserverFactoryManager::TFactoryMap::const_iterator it = CInterfaceObserverFactoryManager::getInstance()->FactoryMap.find((const char *) idObserver);
157 if (it== CInterfaceObserverFactoryManager::getInstance()->FactoryMap.end())
159 nlinfo("undefined observer : %s", (const char*)idObserver);
160 return NULL;
162 obs = it->second->createObserver(node,parentGroup);
163 if (!obs)
165 return NULL;
168 textId = NLMISC::ICDBNode::CTextId( data );
169 if ( ! NLGUI::CDBManager::getInstance()->getDB()->addObserver(obs,textId ) )
171 return NULL;
173 dataTok+= strlen(dataTok ) +1;
174 if (dataTok >= end)
175 break;
176 dataTok = strtok( dataTok," ,");
180 return obs;
184 * Create an action handler
185 *\param node : a pointer to the XML node containing the handlers parameters
186 *\return a pointer to the action handler created
188 virtual IInterfaceObserver *createObserver(xmlNodePtr node,CInterfaceGroup* parent)=0;
193 * register a new observer by defining the associated factory and insert the handler name in the global action handler map
194 * \param handler : name of the action handler class
195 * \param name : name of the action handler in the map (same as the "onaction" value in the XML script)
198 #define REGISTER_OBSERVER(observer ,name) \
199 class observer##Factory : public IInterfaceObserverFactory\
201 public:\
202 observer##Factory ()\
204 CInterfaceObserverFactoryManager::getInstance()->FactoryMap.insert(make_pair(std::string(name),this));\
206 protected:\
207 IInterfaceObserver *createObserver(xmlNodePtr node,CInterfaceGroup* parent)\
209 observer * buf = new observer; \
210 buf->parse(node,parent);\
211 return buf;\
213 }; \
214 observer##Factory observer##FactoryInstance ;\
218 #endif // NL_INTERFACE_OBSERVER_H
220 /* End of interface_observer.h */