summaryrefslogtreecommitdiff
path: root/public/dme_controls/BaseAttributePanel.h
blob: 624ce2ae6ed4bb320a483be436bb470abb83dca5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: base class for all element attribute panels
//    An attribute panel is a one line widget that can be used by a list
//    or tree control.
//
// $NoKeywords: $
//
//=============================================================================//

#ifndef BASEATTRIBUTEPANEL_H
#define BASEATTRIBUTEPANEL_H
#ifdef _WIN32
#pragma once
#endif

#include "datamodel/dmattribute.h"
#include "vgui_controls/Panel.h"
#include "datamodel/dmehandle.h"


//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CDmElement;
class IDmNotify;
class IElementPropertiesChoices;
struct AttributeWidgetInfo_t;
class CDmeEditorAttributeInfo;
class CDmeEditorTypeDictionary;

namespace vgui
{
	class Label;
}

using namespace vgui;


//-----------------------------------------------------------------------------
// CBaseAttributePanel
//-----------------------------------------------------------------------------
class CBaseAttributePanel : public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CBaseAttributePanel, vgui::Panel );

public:
	CBaseAttributePanel( vgui::Panel *pParent, const AttributeWidgetInfo_t &info );

	virtual void	PostConstructor();
	virtual void	PerformLayout();
	virtual void	SetFont( HFont font );
	virtual void	ApplySchemeSettings( IScheme *pScheme );
	virtual void	OnCreateDragData( KeyValues *msg );

	void			SetDirty( bool dirty );
	bool			GetDirty() const;
	bool			IsAutoApply() const;

	// Sets/gets the attribute value
	template< class T >
	void SetAttributeValue( const T& value );
	void SetAttributeValue( const char *pValue );

	template< class T >
	const T& GetAttributeValue( );

	// Helper to get/set the attribute value for elements
	CDmElement *GetAttributeValueElement();
	void SetAttributeValueElement( CDmElement *pElement );

	void SetAttributeValueFromString( const char *pString );
	const char *GetAttributeValueAsString( char *pBuf, int nLength );

	// Returns the attribute type to edit
	DmAttributeType_t	GetAttributeType() const;

	// Returns the editor info
	CDmeEditorTypeDictionary *GetEditorTypeDictionary();
	CDmeEditorAttributeInfo *GetEditorInfo();

	// Call this when the data changed
	IDmNotify			*GetNotify();

protected:
	enum
	{
		HIDETYPE	= 0x01,
		HIDEVALUE	= 0x02,
		READONLY	= 0x04,
		DIRTY		= 0x08,
		AUTOAPPLY	= 0x10,
	};

	// Inherited classes must implement this
	virtual	Panel		*GetDataPanel() = 0;
	virtual void		Apply() = 0;
	virtual void		Refresh() = 0;

	// Methods to get/set column size
	int					GetSizeForColumn( Panel *panel );
	void				SetColumnSize( Panel *panel, int width );

	// Returns the element being edited by the panel
	CDmElement			*GetPanelElement();
	const CDmElement	*GetPanelElement() const;

	// Returns the attribute name
	const char*			GetAttributeName() const;

	// Does the element have the attribute we're attempting to reference?
	bool				HasAttribute() const;

	// Returns the attribute array count
	int					GetAttributeArrayCount() const;

	// Are we editing an entry in an attribute array?
	bool				IsArrayEntry() const;

	// Is a particular flag set?
	bool				HasFlag( int flagMask ) const;

private:
	struct colinfo_t
	{
		Panel		*panel;
		int			width;
	};

	// Set a flag
	void				SetFlag( int flagMask, bool bOn );

	// Used to sort the column list
 	static bool			ColInfoLessFunc( const colinfo_t& lhs, const colinfo_t& rhs );

	// Initializes flags from the attribute editor info
	void				InitializeFlags( const AttributeWidgetInfo_t &info );

	// Called when the OK / Apply button is pressed.  Changed data should be written into document.
	MESSAGE_FUNC( OnApplyChanges, "ApplyChanges" );
	MESSAGE_FUNC( OnRefresh, "Refresh" );

protected:
	Label *m_pType;

private:
	CDmeHandle< CDmElement >				m_hObject;
	CDmeHandle< CDmeEditorAttributeInfo >	m_hEditorInfo;
	CDmeHandle< CDmeEditorTypeDictionary >	m_hEditorTypeDict;

	char m_szAttributeName[ 256 ];
	int m_nArrayIndex;
	DmAttributeType_t m_AttributeType;
	IDmNotify *m_pNotify;
	int m_nFlags;
	CUtlRBTree< colinfo_t, int > m_ColumnSize;
	HFont m_hFont;
};


//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline bool CBaseAttributePanel::HasFlag( int flagMask ) const
{
	return ( m_nFlags & flagMask ) ? true : false;
}

inline void CBaseAttributePanel::SetFlag( int flagMask, bool bOn )
{
	if ( bOn )
	{
		m_nFlags |= flagMask;
	}
	else
	{
		m_nFlags &= ~flagMask;
	}
}

inline bool CBaseAttributePanel::GetDirty() const
{
	return HasFlag( DIRTY );
}

inline bool CBaseAttributePanel::IsAutoApply() const
{
	return HasFlag( AUTOAPPLY );
}

inline DmAttributeType_t CBaseAttributePanel::GetAttributeType() const
{
	return m_AttributeType;
}

inline IDmNotify *CBaseAttributePanel::GetNotify()
{
	return m_pNotify;
}

inline const char* CBaseAttributePanel::GetAttributeName() const
{
	return m_szAttributeName;
}

inline bool CBaseAttributePanel::IsArrayEntry() const
{
	return ( m_nArrayIndex >= 0 );
}


template< class T > inline const T& GetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
{
	const CDmrArray<T> array( pElement, pAttribute );
	return array[ nArrayIndex ];
}

template<> inline const DmElementHandle_t& GetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
{
	const CDmrElementArray<> array( pElement, pAttribute );
	return array.GetHandle( nArrayIndex );
}


template< class T > inline void SetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const T& value )
{
	CDmrArray<T> array( pElement, pAttribute );
	array.Set( nArrayIndex, value );
}

template<> inline void SetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const DmElementHandle_t& value )
{
	CDmrElementArray<> array( pElement, pAttribute );
	array.SetHandle( nArrayIndex, value );
}


//-----------------------------------------------------------------------------
// Sets/gets the attribute value
//-----------------------------------------------------------------------------
template< class T >
void CBaseAttributePanel::SetAttributeValue( const T& value )
{
	if ( !IsArrayEntry() )
	{
		GetPanelElement()->SetValue( m_szAttributeName, value );
	}
	else
	{
		SetArrayAttributeValue<T>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, value );
	}
}

inline void CBaseAttributePanel::SetAttributeValue( const char *pValue )
{
	if ( !IsArrayEntry() )
	{
		GetPanelElement()->SetValue( m_szAttributeName, pValue );
	}
	else
	{
		SetArrayAttributeValue<CUtlString>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, pValue );
	}
}

template< class T >
const T& CBaseAttributePanel::GetAttributeValue( )
{
	if ( !IsArrayEntry() )
		return GetPanelElement()->GetValue<T>( m_szAttributeName );
	return GetArrayAttributeValue<T>( GetPanelElement(), m_szAttributeName, m_nArrayIndex );
}


#endif // BASEATTRIBUTEPANEL_H