Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / gui / view_text_id.cpp
blobbea55a04d91833615f22a240bdbc653e1f64ad76
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 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "stdpch.h"
23 #include "nel/gui/db_manager.h"
24 #include "nel/gui/view_text_id.h"
25 #include "nel/misc/xml_auto_ptr.h"
26 #include "nel/misc/algo.h"
28 using namespace std;
29 using NLMISC::CCDBNodeLeaf;
31 #ifdef DEBUG_NEW
32 #define new DEBUG_NEW
33 #endif
35 NLMISC_REGISTER_OBJECT(CViewBase, CViewTextID, std::string, "text_id");
37 namespace NLGUI
40 CViewTextID::IViewTextProvider* CViewTextID::textProvider = NULL;
42 // ***************************************************************************
43 CViewTextID::CViewTextID(const std::string& id,const std::string &/* idDBPath */, sint FontSize /*=12*/,NLMISC::CRGBA Color /*=NLMISC::CRGBA(255,255,255)*/,bool Shadow /*=false*/)
44 : CViewText (id, std::string(""), FontSize, Color, Shadow)
46 _StringModifier= NULL;
47 _IsDBLink = true;
48 _DBTextId.link(id.c_str());
49 _Initialized = false;
50 _DynamicString = true;
51 _IsTextFormatTaged= false;
54 // ***************************************************************************
55 CViewTextID::~CViewTextID()
59 std::string CViewTextID::getProperty( const std::string &name ) const
61 if( name == "textid" )
63 if( _DBTextId.getNodePtr() != NULL )
64 return _DBTextId.getNodePtr()->getFullName();
65 else
66 return NLMISC::toString( _TextId );
68 else
69 if( name == "dynamic_string" )
71 return NLMISC::toString( _DynamicString );
73 else
74 if( name == "format_taged" )
76 return NLMISC::toString( _IsTextFormatTaged );
78 else
79 return CViewText::getProperty( name );
82 void CViewTextID::setProperty( const std::string &name, const std::string &value )
84 if( name == "textid" )
86 uint32 i;
87 if( NLMISC::fromString( value, i ) )
89 _TextId = i;
90 _IsDBLink = false;
92 else
94 _DBTextId.link( value.c_str() );
95 _IsDBLink = true;
97 return;
99 else
100 if( name == "dynamic_string" )
102 bool b;
103 if( NLMISC::fromString( value, b ) )
104 _DynamicString = b;
105 return;
107 else
108 if( name == "format_taged" )
110 bool b;
111 if( NLMISC::fromString( value, b ) )
112 _IsTextFormatTaged = b;
113 return;
115 else
116 CViewText::setProperty( name, value );
120 xmlNodePtr CViewTextID::serialize( xmlNodePtr parentNode, const char *type ) const
122 xmlNodePtr node = CViewText::serialize( parentNode, type );
123 if( node == NULL )
124 return NULL;
126 xmlSetProp( node, BAD_CAST "type", BAD_CAST "text_id" );
128 if( _DBTextId.getNodePtr() != NULL )
129 xmlSetProp( node, BAD_CAST "textid", BAD_CAST _DBTextId.getNodePtr()->getFullName().c_str() );
130 else
131 xmlSetProp( node, BAD_CAST "textid", BAD_CAST "" );
133 xmlSetProp( node, BAD_CAST "dynamic_string",
134 BAD_CAST NLMISC::toString( _DynamicString ).c_str() );
136 xmlSetProp( node, BAD_CAST "format_taged",
137 BAD_CAST NLMISC::toString( _IsTextFormatTaged ).c_str() );
139 return node;
142 // ***************************************************************************
143 bool CViewTextID::parse(xmlNodePtr cur, CInterfaceGroup * parentGroup)
145 if (!CViewText::parse(cur, parentGroup))
146 return false;
148 if(!parseTextIdOptions(cur))
149 return false;
151 return true;
154 // ***************************************************************************
155 bool CViewTextID::parseTextIdOptions(xmlNodePtr cur)
157 _Initialized = false;
159 CXMLAutoPtr prop((const char*) xmlGetProp( cur, (xmlChar*)"textid" ));
160 _IsDBLink = false;
161 _TextId = 0xFFFFFFFF;
162 if (prop)
164 if ( isdigit(*prop.getDatas()) || *(prop.getDatas())=='-')
166 NLMISC::fromString((const char*)prop, _TextId);
168 else
170 if (_DBTextId.link(prop))
172 _TextId = (uint32)_DBTextId.getSInt64();
173 _IsDBLink = true;
175 else
177 return false;
182 prop= (char*) xmlGetProp( cur, (xmlChar*)"dynamic_string" );
183 _DynamicString = true;
184 if (prop)
185 _DynamicString = convertBool(prop);
187 // format_taged
188 prop= (char*) xmlGetProp( cur, (xmlChar*)"format_taged" );
189 _IsTextFormatTaged= false;
190 if(prop)
191 _IsTextFormatTaged= convertBool(prop);
193 return true;
196 // ***************************************************************************
197 void CViewTextID::checkCoords()
199 if (_IsDBLink)
201 uint32 newId = (uint32)_DBTextId.getSInt64();
202 setTextId (newId);
205 if (!_Initialized)
207 // String result
208 string result;
210 if( textProvider != NULL )
212 // Get the string
213 if( _DynamicString )
214 _Initialized = textProvider->getDynString( _TextId, result );
215 else
216 _Initialized = textProvider->getString( _TextId, result );
219 // Remove all {break}
220 for(;;)
222 string::size_type index = result.find("{break}");
223 if (index == string::npos) break;
224 result = result.substr (0, index) + result.substr(index+7, result.size());
228 // Remove all {ros_exit}
229 while(NLMISC::strFindReplace(result, "{ros_exit}", ""));
231 // Modify the text?
232 if (_StringModifier)
234 _StringModifier->onReceiveTextId(result);
237 // Set the Text
238 if(_IsTextFormatTaged)
239 setTextFormatTaged(result);
240 else
241 setText(result);
243 CViewText::checkCoords();
246 // ***************************************************************************
247 uint32 CViewTextID::getTextId () const
249 return _TextId;
252 // ***************************************************************************
253 void CViewTextID::setTextId (uint32 newId)
255 if (newId != _TextId)
256 _Initialized = false;
257 _TextId = newId;
258 if (_IsDBLink)
259 _DBTextId.setSInt64(_TextId);
262 // ***************************************************************************
263 bool CViewTextID::setDBTextID(const std::string &dbPath)
265 if (_DBTextId.link(dbPath.c_str()))
267 _TextId = (uint32)_DBTextId.getSInt64();
268 _IsDBLink = true;
269 _Initialized = false;
270 #ifdef NL_DEBUG
271 _DBPath =dbPath;
272 #endif
273 return true;
275 else
277 _IsDBLink = false;
278 _Initialized = false;
279 #ifdef NL_DEBUG
280 _DBPath="Bad db path : " + dbPath;
281 #endif
282 return false;
286 // ***************************************************************************
287 void CViewTextID::setDBLeaf(CCDBNodeLeaf *leaf)
289 if (!leaf)
291 _IsDBLink = false;
292 _TextId = 0;
293 return;
295 _IsDBLink = true;
296 _DBTextId.setNodePtr(leaf);
299 // ***************************************************************************
300 string CViewTextID::getTextIdDbLink() const
302 if (!_IsDBLink) return "";
303 if (_DBTextId.getNodePtr() == NULL) return "";
304 return _DBTextId.getNodePtr()->getFullName();
307 // ***************************************************************************
308 void CViewTextID::setTextIdDbLink(const string &link)
310 CCDBNodeLeaf *pNL = NLGUI::CDBManager::getInstance()->getDbProp(link,false);
311 if (pNL == NULL)
313 nlwarning("cant set textidlink for %s",link.c_str());
314 return;
316 setDBLeaf(pNL);