Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / gui / dbview_number.cpp
blobc0c728f2b9426aaf19e0153282221bc2dd1cd35a
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-2014 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/dbview_number.h"
24 #include "nel/misc/xml_auto_ptr.h"
25 #include "nel/misc/common.h"
27 using namespace std;
28 using namespace NL3D;
29 using namespace NLMISC;
31 #ifdef DEBUG_NEW
32 #define new DEBUG_NEW
33 #endif
35 NLMISC_REGISTER_OBJECT(CViewBase, CDBViewNumber, std::string, "text_number");
37 // ***************************************************************************
39 namespace NLGUI
42 CDBViewNumber::CDBViewNumber(const TCtorParam &param)
43 :CViewText(param)
45 _Positive = false;
46 _Cache= 0;
47 setText(std::string("0"));
48 _Divisor = 1;
49 _Modulo = 0;
52 std::string CDBViewNumber::getProperty( const std::string &name ) const
54 if( name == "value" )
56 if( _Number.getNodePtr() != NULL )
57 return _Number.getNodePtr()->getFullName();
58 else
59 return "";
61 else
62 if( name == "positive" )
64 return toString( _Positive );
66 else
67 if( name == "format" )
69 return toString( _Format );
71 else
72 if( name == "divisor" )
74 return toString( _Divisor );
76 else
77 if( name == "modulo" )
79 return toString( _Modulo );
81 else
82 if( name == "suffix" )
84 return _Suffix.toString();
86 else
87 if( name == "prefix" )
89 return _Prefix.toString();
91 else
92 return CViewText::getProperty( name );
95 void CDBViewNumber::setProperty( const std::string &name, const std::string &value )
97 if( name == "value" )
99 _Number.link( value.c_str() );
100 return;
102 else
103 if( name == "positive" )
105 bool b;
106 if( fromString( value, b ) )
107 _Positive = b;
108 return;
110 else
111 if( name == "format" )
113 bool b;
114 if( fromString( value, b ) )
115 _Format = b;
116 return;
118 else
119 if( name == "divisor" )
121 sint64 i;
122 if( fromString( value, i ) )
123 _Divisor = i;
124 return;
126 else
127 if( name == "modulo" )
129 sint64 i;
130 if( fromString( value, i ) )
131 _Modulo = i;
132 return;
134 else
135 if( name == "suffix" )
137 _Suffix = value;
138 return;
140 else
141 if( name == "prefix" )
143 _Prefix = value;
144 return;
146 else
147 CViewText::setProperty( name, value );
150 xmlNodePtr CDBViewNumber::serialize( xmlNodePtr parentNode, const char *type ) const
152 xmlNodePtr node = CViewText::serialize( parentNode, type );
153 if( node == NULL )
154 return NULL;
156 xmlSetProp( node, BAD_CAST "type", BAD_CAST "text_number" );
158 if( _Number.getNodePtr() != NULL )
159 xmlSetProp( node, BAD_CAST "value", BAD_CAST _Number.getNodePtr()->getFullName().c_str() );
160 else
161 xmlSetProp( node, BAD_CAST "value", BAD_CAST "" );
163 xmlSetProp( node, BAD_CAST "positive", BAD_CAST toString( _Positive ).c_str() );
164 xmlSetProp( node, BAD_CAST "format", BAD_CAST toString( _Format ).c_str() );
165 xmlSetProp( node, BAD_CAST "divisor", BAD_CAST toString( _Divisor ).c_str() );
166 xmlSetProp( node, BAD_CAST "modulo", BAD_CAST toString( _Modulo ).c_str() );
167 xmlSetProp( node, BAD_CAST "suffix", BAD_CAST _Suffix.toString().c_str() );
168 xmlSetProp( node, BAD_CAST "prefix", BAD_CAST _Prefix.toString().c_str() );
170 return node;
173 // ***************************************************************************
174 bool CDBViewNumber::parse (xmlNodePtr cur, CInterfaceGroup * parentGroup)
176 if(!CViewText::parse(cur, parentGroup))
177 return false;
179 // link to the db
180 CXMLAutoPtr ptr;
181 ptr = xmlGetProp (cur, (xmlChar*)"value");
182 if ( ptr )
184 // Yoyo: verify doesn't entered a direct number :). MUST BE A CORRECT DATABASE ENTRY
185 const char *serverDb= "SERVER:";
186 const char *localDb= "LOCAL:";
187 const char *uiDb= "UI:";
188 if( strncmp((const char*)ptr, serverDb, strlen(serverDb))==0 ||
189 strncmp((const char*)ptr, localDb, strlen(localDb))==0 ||
190 strncmp((const char*)ptr, uiDb, strlen(uiDb))==0 )
192 // OK? => Link.
193 _Number.link ( ptr );
195 else
197 nlinfo ("bad value in %s", _Id.c_str());
198 return false;
201 else
203 nlinfo ("no value in %s", _Id.c_str());
204 return false;
207 ptr = xmlGetProp (cur, (xmlChar*)"positive");
208 if (ptr) _Positive = convertBool(ptr);
209 else _Positive = false;
211 ptr = xmlGetProp (cur, (xmlChar*)"format");
212 if (ptr) _Format = convertBool(ptr);
213 else _Format = false;
215 ptr = xmlGetProp (cur, (xmlChar*)"divisor");
216 if (ptr) fromString((const char*)ptr, _Divisor);
218 ptr = xmlGetProp (cur, (xmlChar*)"modulo");
219 if (ptr) fromString((const char*)ptr, _Modulo);
221 ptr = xmlGetProp (cur, (xmlChar*)"suffix");
222 if (ptr) _Suffix = (const char*)ptr;
224 ptr = xmlGetProp (cur, (xmlChar*)"prefix");
225 if (ptr) _Prefix = (const char*)ptr;
227 // init cache.
228 _Cache= 0;
229 setText(std::string("0"));
231 return true;
234 // ***************************************************************************
235 void CDBViewNumber::checkCoords()
237 // change text
238 sint64 val = getVal();
239 if (_Cache != val)
241 _Cache= val;
242 std::string value = _Format ? NLMISC::formatThousands(toString(val)) : toString(val);
243 if (_Positive) setText(val >= 0 ? ( _Prefix.toString() + value + _Suffix.toString() ) : "?");
244 else setText( _Prefix.toString() + value + _Suffix.toString() );
248 // ***************************************************************************
249 void CDBViewNumber::draw ()
251 // parent call
252 CViewText::draw();
255 void CDBViewNumber::forceLink()
259 sint64 CDBViewNumber::getVal()
261 if( !_Number.hasValue() )
262 return 0;
264 if( _Modulo == 0 )
265 return _Number.getSInt64() / _Divisor;
266 else
267 return ( _Number.getSInt64() / _Divisor ) % _Modulo;