Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
11 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef PANORAMA_ILOCALIZE_H
  6. #define PANORAMA_ILOCALIZE_H
  7. #include "language.h"
  8. #include "tier0/platform.h"
  9. #include "tier1/interface.h"
  10. #include "tier1/utlflags.h"
  11. #include "tier1/utlsymbol.h"
  12. #include "tier1/utlstring.h"
  13. #include "tier1/utlmap.h"
  14. #include "tier1/fileio.h"
  15. #include "tier1/utlpriorityqueue.h"
  16. #include "steam/steamtypes.h"
  17. #if defined( SOURCE2_PANORAMA )
  18. #include "currencyamount.h"
  19. #else
  20. #include "stime.h"
  21. #include "constants.h"
  22. #include "globals.h"
  23. #include "amount.h"
  24. #include "rtime.h"
  25. #endif
  26. #include "reliabletimer.h"
  27. #include "language.h"
  28. namespace panorama
  29. {
  30. //-----------------------------------------------------------------------------
  31. // Purpose: type of variable data we are supporting
  32. //-----------------------------------------------------------------------------
  33. enum EPanelKeyType
  34. {
  35. k_ePanelVartype_None,
  36. k_ePanelVartype_String,
  37. k_ePanelVartype_Time,
  38. k_ePanelVartype_Money,
  39. k_ePanelVartype_Number,
  40. k_ePanelVartype_Generic
  41. };
  42. // help function syntax for the generic key handler
  43. typedef const char *( *PFNLocalizeDialogVariableHandler )( const CUtlString &sStringValue, int nIntValue, const IUIPanel *pPanel, const char *pszKey, void *pUserData );
  44. enum EPanelKeyTimeModifiers
  45. {
  46. k_ePanelKeyTimeModifiers_ShortDate = 1 << 0,
  47. k_ePanelKeyTimeModifiers_LongDate = 1 << 1,
  48. k_ePanelKeyTimeModifiers_ShortTime = 1 << 2,
  49. k_ePanelKeyTimeModifiers_LongTime = 1 << 3,
  50. k_ePanelKeyTimeModifiers_DateTime = 1 << 4,
  51. k_ePanelKeyTimeModifiers_Relative = 1 << 5,
  52. k_ePanelKeyTimeModifiers_Duration = 1 << 6,
  53. };
  54. enum EStringTruncationStyle
  55. {
  56. k_eStringTruncationStyle_None,
  57. k_eStringTruncationStyle_Rear, // prevent any chars being added above max length
  58. k_eStringTruncationStyle_Front, // remove
  59. };
  60. enum EStringTransformStyle
  61. {
  62. k_eStringTransformStyle_None,
  63. k_eStringTransformStyle_Uppercase,
  64. k_eStringTransformStyle_Lowercase,
  65. };
  66. //-----------------------------------------------------------------------------
  67. // Purpose: callback interface to help measuring strings
  68. //-----------------------------------------------------------------------------
  69. class ILocalizationStringSizeResolver
  70. {
  71. public:
  72. virtual ~ILocalizationStringSizeResolver() {}
  73. virtual int ResolveStringLengthInPixels( const char *pchString ) = 0;
  74. };
  75. const uint32 k_nLocalizeMaxChars = (uint32)~0;
  76. class CPanelKeyValue;
  77. class CLocalization;
  78. class CPanel2D;
  79. //-----------------------------------------------------------------------------
  80. // Purpose: interface to final string data to display to users
  81. //-----------------------------------------------------------------------------
  82. class ILocalizationString
  83. {
  84. public:
  85. virtual ~ILocalizationString() {}
  86. // Get the length of the string in characters
  87. virtual int Length() const = 0;
  88. virtual bool IsEmpty() const = 0;
  89. virtual const char *String() const = 0;
  90. virtual const char *StringNoTransform() const = 0;
  91. virtual operator const char *() const = 0;
  92. // add this string on the end
  93. virtual bool AppendText( const char *pchText ) = 0;
  94. // done with this string, delete it
  95. virtual void Release() const = 0;
  96. virtual EStringTransformStyle GetTransformStyle() const = 0;
  97. virtual const IUIPanel *GetOwningPanel() const = 0;
  98. virtual uint32 GetMaxChars() const = 0;
  99. virtual EStringTruncationStyle GetTruncationStyle() const = 0;
  100. #ifdef DBGFLAG_VALIDATE
  101. virtual void Validate( CValidator &validator, const tchar *pchName ) = 0;
  102. #endif
  103. protected:
  104. // internal loc engine helpers, you won't call these
  105. friend class CLocalization;
  106. virtual void Recalculate( const CUtlString *pString ) = 0;
  107. virtual bool BContainsDialogVariable( const CPanelKeyValue &key ) = 0;
  108. };
  109. class CLocStringSafePointer
  110. {
  111. public:
  112. CLocStringSafePointer()
  113. {
  114. m_pString = NULL;
  115. }
  116. CLocStringSafePointer( const ILocalizationString *pLocString )
  117. {
  118. m_pString = pLocString;
  119. }
  120. CLocStringSafePointer &operator =( const ILocalizationString *pString )
  121. {
  122. if ( m_pString == pString )
  123. return *this;
  124. Clear();
  125. m_pString = const_cast<ILocalizationString *>( pString );
  126. return *this;
  127. }
  128. const ILocalizationString *operator ->( ) const
  129. {
  130. return Get();
  131. }
  132. const ILocalizationString *operator *( ) const
  133. {
  134. return Get();
  135. }
  136. operator const ILocalizationString*( ) const
  137. {
  138. return Get();
  139. }
  140. ~CLocStringSafePointer()
  141. {
  142. Clear();
  143. }
  144. void Clear()
  145. {
  146. if ( m_pString )
  147. {
  148. m_pString->Release();
  149. m_pString = NULL;
  150. }
  151. }
  152. bool IsValid() { return m_pString != nullptr; }
  153. const ILocalizationString *Get() const
  154. {
  155. return m_pString;
  156. }
  157. private:
  158. const ILocalizationString *m_pString;
  159. };
  160. //-----------------------------------------------------------------------------
  161. // Purpose: interface to localize strings
  162. //-----------------------------------------------------------------------------
  163. class IUILocalization
  164. {
  165. public:
  166. // change the language used by the loc system
  167. virtual bool SetLanguage( const char *pchLanguage ) = 0;
  168. #if defined( SOURCE2_PANORAMA )
  169. // add a loc file to the system, in the form of <prefix>_<language>.txt , i.e dota_french.txt, the files will be loaded from the panorama/localization folder of your mod
  170. virtual bool BLoadLocalizationFile( const char *pchFilePrefix ) = 0;
  171. #else
  172. virtual ELanguage CurrentLanguage() = 0;
  173. #endif
  174. virtual void InstallCustomDialogVariableHandler( const char *pchCustomHandlerName, PFNLocalizeDialogVariableHandler pfnLocalizeFunc, void *pUserData = NULL ) = 0;
  175. virtual void RemoveCustomDialogVariableHandler( const char *pchCustomHandlerName ) = 0;
  176. // find the string corresponding to this localization token, or if we don't find it then just return back the string wrapped in a loc object
  177. virtual const ILocalizationString *PchFindToken( const IUIPanel *pPanel, const char *pchToken, const uint32 ccMax , EStringTruncationStyle eTrunkStyle, EStringTransformStyle eTransformStyle, bool bAllowDialogVariable = false ) = 0;
  178. // give me a localize string wrapper around this string, don't try and apply token localizing on it though, but do optionally allow it to have dialog variables that we parse in it
  179. // be careful allowing dialog variable parsing, you want to sanitize any user input before allowing it
  180. virtual const ILocalizationString *PchSetString( const IUIPanel *pPanel, const char *pchText, const uint32 ccMax, EStringTruncationStyle eTrunkStyle, EStringTransformStyle eTransformStyle, bool bAllowDialogVariable, bool bStringAlreadyFullyParsed ) = 0;
  181. virtual const ILocalizationString *ChangeTransformStyleAndRelease( const ILocalizationString *pLocalizationString, EStringTransformStyle eTranformStyle ) = 0;
  182. // copy an existing loc string without altering the ref count on the current
  183. virtual ILocalizationString *CloneString( const IUIPanel *pPanel, const ILocalizationString *pLocToken ) = 0;
  184. // return the raw, un-parsed, value for this loc token, returns NULL if we didn't have this token in a loc file from disk
  185. virtual const char *PchFindRawString( const char *pchToken ) = 0;
  186. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, const char *pchValue ) = 0;
  187. #if defined( SOURCE2_PANORAMA )
  188. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, time_t timeVal ) = 0;
  189. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CCurrencyAmount amount ) = 0;
  190. #else
  191. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CRTime timeVal ) = 0;
  192. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CAmount amount ) = 0;
  193. #endif
  194. virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, int nVal ) = 0;
  195. // copy all the dialog vars to a new panel
  196. virtual void CloneDialogVariables( const IUIPanel *pPanelFrom, IUIPanel *pPanelTo ) = 0;
  197. // force a re-evaluation of a specific dialog variable
  198. virtual void DirtyDialogVariable( const IUIPanel *pPanel, const char *pchKey ) = 0;
  199. // given this loc string find the longest string in any language that we could display here and update to use it
  200. virtual void SetLongestStringForToken( const ILocalizationString *pLocalizationString, ILocalizationStringSizeResolver *pResolver ) = 0;
  201. };
  202. //-----------------------------------------------------------------------------
  203. // Purpose: Wrapper around ILocalizationString that will take care of making
  204. // a copy if it ends up needing to be mutable.
  205. //-----------------------------------------------------------------------------
  206. class CMutableLocalizationString
  207. {
  208. public:
  209. CMutableLocalizationString()
  210. : m_bMutable( false )
  211. , m_pString( NULL )
  212. {
  213. }
  214. CMutableLocalizationString( ILocalizationString *pString )
  215. : m_bMutable( true )
  216. , m_pString( pString )
  217. {
  218. }
  219. CMutableLocalizationString( const ILocalizationString *pString )
  220. : m_bMutable( false )
  221. , m_pString( const_cast< ILocalizationString * >( pString ) )
  222. {
  223. }
  224. CMutableLocalizationString( const CMutableLocalizationString &other )
  225. {
  226. m_bMutable = true;
  227. if ( other.m_pString )
  228. {
  229. m_pString = UILocalize()->CloneString( other.m_pString->GetOwningPanel(), other.m_pString );
  230. }
  231. else
  232. {
  233. m_pString = NULL;
  234. }
  235. }
  236. ~CMutableLocalizationString()
  237. {
  238. Clear();
  239. }
  240. const ILocalizationString *Get() const
  241. {
  242. return m_pString;
  243. }
  244. ILocalizationString *GetMutable()
  245. {
  246. if ( m_bMutable || !m_pString )
  247. return m_pString;
  248. ILocalizationString *pOldString = m_pString;
  249. m_pString = UILocalize()->CloneString( pOldString->GetOwningPanel(), pOldString );
  250. m_bMutable = true;
  251. pOldString->Release();
  252. return m_pString;
  253. }
  254. void Clear()
  255. {
  256. if ( m_pString )
  257. {
  258. m_pString->Release();
  259. m_pString = NULL;
  260. }
  261. }
  262. const ILocalizationString *Extract()
  263. {
  264. const ILocalizationString *pString = m_pString;
  265. m_pString = NULL;
  266. return pString;
  267. }
  268. ILocalizationString *ExtractMutable()
  269. {
  270. ILocalizationString *pString = GetMutable();
  271. m_pString = NULL;
  272. return pString;
  273. }
  274. // The -> operator is only overloaded to return a const string. If you need a mutable version, you must call GetMutable directly.
  275. const ILocalizationString *operator ->() const
  276. {
  277. return Get();
  278. }
  279. explicit operator bool() const
  280. {
  281. return Get() != NULL;
  282. }
  283. bool operator ==( const CMutableLocalizationString &other ) const
  284. {
  285. return m_pString == other.m_pString;
  286. }
  287. bool operator !=( const CMutableLocalizationString &other ) const
  288. {
  289. return !( *this == other );
  290. }
  291. bool operator ==( const ILocalizationString *pString ) const
  292. {
  293. return m_pString == pString;
  294. }
  295. bool operator !=( const ILocalizationString *pString ) const
  296. {
  297. return !( *this == pString );
  298. }
  299. CMutableLocalizationString &operator =( const CMutableLocalizationString &other )
  300. {
  301. if ( other.m_pString == m_pString )
  302. return *this;
  303. Clear();
  304. m_bMutable = true;
  305. if ( other.m_pString )
  306. {
  307. m_pString = UILocalize()->CloneString( other.m_pString->GetOwningPanel(), other.m_pString );
  308. }
  309. else
  310. {
  311. m_pString = NULL;
  312. }
  313. return *this;
  314. }
  315. CMutableLocalizationString &operator =( const ILocalizationString *pString )
  316. {
  317. if ( m_pString == pString )
  318. return *this;
  319. Clear();
  320. m_bMutable = false;
  321. m_pString = const_cast< ILocalizationString * >( pString );
  322. return *this;
  323. }
  324. private:
  325. bool m_bMutable;
  326. ILocalizationString *m_pString;;
  327. };
  328. } // namespace panorama
  329. #endif // PANORAMA_ILOCALIZE_H