Counter Strike : Global Offensive Source Code
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.

308 lines
8.0 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose: base class for all element attribute panels
  4. // An attribute panel is a one line widget that can be used by a list
  5. // or tree control.
  6. //
  7. // $NoKeywords: $
  8. //
  9. //=============================================================================//
  10. #ifndef BASEATTRIBUTEPANEL_H
  11. #define BASEATTRIBUTEPANEL_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "datamodel/dmattribute.h"
  16. #include "datamodel/dmattributevar.h"
  17. #include "vgui_controls/Panel.h"
  18. #include "datamodel/dmehandle.h"
  19. #include "tier1/fmtstr.h"
  20. #define FirstColumnWidth 30
  21. #define TypeColumnWidth 75
  22. #define ColumnBorderWidth 2
  23. #define PickerWidth 25
  24. #define PickerHeight 13
  25. //-----------------------------------------------------------------------------
  26. // Forward declarations
  27. //-----------------------------------------------------------------------------
  28. class CDmElement;
  29. class IDmNotify;
  30. class IElementPropertiesChoices;
  31. struct AttributeWidgetInfo_t;
  32. class CDmeEditorAttributeInfo;
  33. class CDmeEditorTypeDictionary;
  34. namespace vgui
  35. {
  36. class Label;
  37. }
  38. using namespace vgui;
  39. //-----------------------------------------------------------------------------
  40. // CBaseAttributePanel
  41. //-----------------------------------------------------------------------------
  42. class CBaseAttributePanel : public vgui::Panel
  43. {
  44. DECLARE_CLASS_SIMPLE( CBaseAttributePanel, vgui::Panel );
  45. public:
  46. CBaseAttributePanel( vgui::Panel *pParent, const AttributeWidgetInfo_t &info );
  47. virtual void PostConstructor();
  48. virtual void PerformLayout();
  49. virtual void SetFont( HFont font );
  50. virtual void ApplySchemeSettings( IScheme *pScheme );
  51. virtual void OnCreateDragData( KeyValues *msg );
  52. void SetDirty( bool dirty );
  53. bool GetDirty() const;
  54. bool IsAutoApply() const;
  55. // Sets/gets the attribute value
  56. template< class T >
  57. void SetAttributeValue( const T& value );
  58. void SetAttributeValue( const char *pValue );
  59. template< class T >
  60. const T& GetAttributeValue( );
  61. // Helper to get/set the attribute value for elements
  62. CDmElement *GetAttributeValueElement();
  63. void SetAttributeValueElement( CDmElement *pElement );
  64. void SetAttributeValueFromString( const char *pString );
  65. bool GetAttributeValueAsString( char *pBuf, int nLength );
  66. // Returns the attribute type to edit
  67. DmAttributeType_t GetAttributeType() const;
  68. CDmAttribute *GetAttribute();
  69. // Returns the editor info
  70. CDmeEditorTypeDictionary *GetEditorTypeDictionary();
  71. CDmeEditorAttributeInfo *GetEditorInfo();
  72. // Call this when the data changed
  73. IDmNotify *GetNotify();
  74. protected:
  75. enum
  76. {
  77. HIDETYPE = 0x01,
  78. HIDEVALUE = 0x02,
  79. READONLY = 0x04,
  80. DIRTY = 0x08,
  81. AUTOAPPLY = 0x10,
  82. };
  83. // Inherited classes must implement this
  84. virtual Panel *GetDataPanel() = 0;
  85. virtual void Apply() = 0;
  86. virtual void Refresh() = 0;
  87. // Methods to get/set column size
  88. int GetSizeForColumn( Panel *panel );
  89. void SetColumnSize( Panel *panel, int width );
  90. virtual void GetPickerBounds( int *x, int *y, int *w, int *h );
  91. // Returns the element being edited by the panel
  92. CDmElement *GetPanelElement();
  93. const CDmElement *GetPanelElement() const;
  94. // Returns the attribute name
  95. const char* GetAttributeName() const;
  96. // Does the element have the attribute we're attempting to reference?
  97. bool HasAttribute() const;
  98. // Returns the attribute array count
  99. int GetAttributeArrayCount() const;
  100. // Are we editing an entry in an attribute array?
  101. bool IsArrayEntry() const;
  102. // Is a particular flag set?
  103. bool HasFlag( int flagMask ) const;
  104. private:
  105. struct colinfo_t
  106. {
  107. Panel *panel;
  108. int width;
  109. };
  110. // Set a flag
  111. void SetFlag( int flagMask, bool bOn );
  112. // Used to sort the column list
  113. static bool ColInfoLessFunc( const colinfo_t& lhs, const colinfo_t& rhs );
  114. // Initializes flags from the attribute editor info
  115. void InitializeFlags( const AttributeWidgetInfo_t &info );
  116. // Called when the OK / Apply button is pressed. Changed data should be written into document.
  117. MESSAGE_FUNC( OnApplyChanges, "ApplyChanges" );
  118. MESSAGE_FUNC( OnRefresh, "Refresh" );
  119. protected:
  120. Label *m_pType;
  121. private:
  122. CDmeHandle< CDmElement > m_hObject;
  123. CDmeHandle< CDmeEditorAttributeInfo > m_hEditorInfo;
  124. CDmeHandle< CDmeEditorTypeDictionary > m_hEditorTypeDict;
  125. char m_szAttributeName[ 256 ];
  126. int m_nArrayIndex;
  127. DmAttributeType_t m_AttributeType;
  128. IDmNotify *m_pNotify;
  129. int m_nFlags;
  130. CUtlRBTree< colinfo_t, int > m_ColumnSize;
  131. HFont m_hFont;
  132. };
  133. //-----------------------------------------------------------------------------
  134. // Inline methods
  135. //-----------------------------------------------------------------------------
  136. inline bool CBaseAttributePanel::HasFlag( int flagMask ) const
  137. {
  138. return ( m_nFlags & flagMask ) ? true : false;
  139. }
  140. inline void CBaseAttributePanel::SetFlag( int flagMask, bool bOn )
  141. {
  142. if ( bOn )
  143. {
  144. m_nFlags |= flagMask;
  145. }
  146. else
  147. {
  148. m_nFlags &= ~flagMask;
  149. }
  150. }
  151. inline bool CBaseAttributePanel::GetDirty() const
  152. {
  153. return HasFlag( DIRTY );
  154. }
  155. inline bool CBaseAttributePanel::IsAutoApply() const
  156. {
  157. return HasFlag( AUTOAPPLY );
  158. }
  159. inline DmAttributeType_t CBaseAttributePanel::GetAttributeType() const
  160. {
  161. return m_AttributeType;
  162. }
  163. inline IDmNotify *CBaseAttributePanel::GetNotify()
  164. {
  165. return m_pNotify;
  166. }
  167. inline const char* CBaseAttributePanel::GetAttributeName() const
  168. {
  169. return m_szAttributeName;
  170. }
  171. inline bool CBaseAttributePanel::IsArrayEntry() const
  172. {
  173. return ( m_nArrayIndex >= 0 );
  174. }
  175. template< class T > inline const T& GetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
  176. {
  177. const CDmrArray<T> array( pElement, pAttribute );
  178. return array[ nArrayIndex ];
  179. }
  180. template<> inline const DmElementHandle_t& GetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
  181. {
  182. const CDmrElementArray<> array( pElement, pAttribute );
  183. return array.GetHandle( nArrayIndex );
  184. }
  185. template< class T > inline void SetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const T& value )
  186. {
  187. CDmrArray<T> array( pElement, pAttribute );
  188. array.Set( nArrayIndex, value );
  189. }
  190. template<> inline void SetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const DmElementHandle_t& value )
  191. {
  192. CDmrElementArray<> array( pElement, pAttribute );
  193. array.SetHandle( nArrayIndex, value );
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Sets/gets the attribute value
  197. //-----------------------------------------------------------------------------
  198. template< class T >
  199. void CBaseAttributePanel::SetAttributeValue( const T& value )
  200. {
  201. CUndoScopeGuard sg( CFmtStr( "Set %s", m_szAttributeName ) );
  202. if ( !IsArrayEntry() )
  203. {
  204. GetPanelElement()->SetValue( m_szAttributeName, value );
  205. }
  206. else
  207. {
  208. SetArrayAttributeValue<T>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, value );
  209. }
  210. }
  211. inline void CBaseAttributePanel::SetAttributeValue( const char *pValue )
  212. {
  213. CUndoScopeGuard sg( CFmtStr( "Set %s", m_szAttributeName ) );
  214. if ( !IsArrayEntry() )
  215. {
  216. GetPanelElement()->SetValue( m_szAttributeName, pValue );
  217. }
  218. else
  219. {
  220. CUtlSymbolLarge symbol = g_pDataModel->GetSymbol( pValue );
  221. SetArrayAttributeValue<CUtlSymbolLarge>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, symbol );
  222. }
  223. }
  224. template< class T >
  225. const T& CBaseAttributePanel::GetAttributeValue( )
  226. {
  227. CDmElement *pPanelElement = GetPanelElement();
  228. if ( !pPanelElement )
  229. {
  230. static T temp;
  231. CDmAttributeInfo<T>::SetDefaultValue( temp );
  232. return temp;
  233. }
  234. if ( !IsArrayEntry() )
  235. return pPanelElement->GetValue<T>( m_szAttributeName );
  236. return GetArrayAttributeValue<T>( pPanelElement, m_szAttributeName, m_nArrayIndex );
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Returns the panel element
  240. //-----------------------------------------------------------------------------
  241. inline CDmElement *CBaseAttributePanel::GetPanelElement()
  242. {
  243. return m_hObject;
  244. }
  245. inline const CDmElement *CBaseAttributePanel::GetPanelElement() const
  246. {
  247. return m_hObject;
  248. }
  249. #endif // BASEATTRIBUTEPANEL_H