Base.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #pragma once
  7. #ifndef GWEN_CONTROLS_BASE_H
  8. #define GWEN_CONTROLS_BASE_H
  9. #include "Gwen/Exports.h"
  10. #include "Gwen/Structures.h"
  11. #include "Gwen/BaseRender.h"
  12. #include "Gwen/Events.h"
  13. #include <list>
  14. #include <map>
  15. #define GWEN_DECLARE_CAST(T)\
  16. virtual class T* DynamicCast##T() { return 0;}\
  17. virtual const class T* DynamicCast##T() const { return 0;}\
  18. #define GWEN_IMPLEMENT_CAST(T)\
  19. virtual class T* DynamicCast##T() { return (T*)this;}\
  20. virtual const class T* DynamicCast##T() const { return (T*)this;}\
  21. namespace Gwen
  22. {
  23. namespace ControlsInternal
  24. {
  25. class ColorDisplay;
  26. class Resizer;
  27. };
  28. namespace Pos
  29. {
  30. enum
  31. {
  32. None = 0,
  33. Left = (1 << 1),
  34. Right = (1 << 2),
  35. Top = (1 << 3),
  36. Bottom = (1 << 4),
  37. CenterV = (1 << 5),
  38. CenterH = (1 << 6),
  39. Fill = (1 << 7),
  40. Center = CenterV | CenterH,
  41. };
  42. }
  43. namespace Skin
  44. {
  45. class Base;
  46. }
  47. namespace Controls
  48. {
  49. class Canvas;
  50. namespace Layout
  51. {
  52. class TableRow;
  53. };
  54. class GWEN_EXPORT Base : public Event::Handler
  55. {
  56. public:
  57. GWEN_DECLARE_CAST(TabButton)
  58. GWEN_DECLARE_CAST(DockedTabControl)
  59. virtual class Layout::TableRow* DynamicCastLayoutTableRow() { return 0;}
  60. virtual const class Layout::TableRow* DynamicCastLayoutTableRow() const { return 0;}
  61. GWEN_DECLARE_CAST(TextBoxNumeric)
  62. GWEN_DECLARE_CAST(HorizontalSlider)
  63. GWEN_DECLARE_CAST(DockBase)
  64. GWEN_DECLARE_CAST(MenuItem)
  65. GWEN_DECLARE_CAST(PropertyRow)
  66. GWEN_DECLARE_CAST(WindowControl)
  67. GWEN_DECLARE_CAST(TreeControl)
  68. GWEN_DECLARE_CAST(TreeNode)
  69. GWEN_DECLARE_CAST(HSVColorPicker)
  70. GWEN_DECLARE_CAST(TabControl)
  71. GWEN_DECLARE_CAST(TabControlInner)
  72. GWEN_DECLARE_CAST(GroupBox)
  73. GWEN_DECLARE_CAST(Properties)
  74. GWEN_DECLARE_CAST(RadioButton)
  75. GWEN_DECLARE_CAST(LabeledRadioButton)
  76. virtual class ::Gwen::ControlsInternal::Resizer* DynamicCastResizer() { return 0;}
  77. virtual const class ::Gwen::ControlsInternal::Resizer* DynamicCastResizer() const { return 0;}
  78. virtual class ::Gwen::ControlsInternal::ColorDisplay* DynamicCastColorDisplay() { return 0;}
  79. virtual const class ::Gwen::ControlsInternal::ColorDisplay* DynamicCastColorDisplay() const { return 0;}
  80. typedef std::list<Base*> List;
  81. typedef std::map<Gwen::UnicodeString, Gwen::Event::Caller*> AccelMap;
  82. Base( Base* pParent );
  83. virtual ~Base();
  84. virtual void DelayedDelete();
  85. virtual void SetParent( Controls::Base* pParent );
  86. virtual Controls::Base* GetParent() const { return m_Parent; }
  87. virtual Controls::Canvas* GetCanvas();
  88. virtual Base::List& GetChildren(){ if ( m_InnerPanel ) return m_InnerPanel->GetChildren(); return Children; }
  89. virtual bool IsChild( Controls::Base* pChild );
  90. virtual int NumChildren();
  91. virtual bool SizeToChildren( bool w = true, bool h = true );
  92. virtual Gwen::Point ChildrenSize();
  93. virtual Controls::Base* FindChildByName( const Gwen::String& name, bool bRecursive = false );
  94. virtual void SetName(Gwen::String name) { m_Name = name; }
  95. virtual const Gwen::String& GetName() { return m_Name; }
  96. virtual void Think(){}
  97. virtual void ExpandAll(){}
  98. virtual void SizeToContents(){}
  99. virtual bool IsActive() { return false;}
  100. virtual void AddChild( Controls::Base* pChild );
  101. virtual void RemoveChild( Controls::Base* pParent );
  102. protected:
  103. virtual void OnChildAdded( Controls::Base* pChild );
  104. virtual void OnChildRemoved( Controls::Base* pChild );
  105. public:
  106. virtual void RemoveAllChildren();
  107. virtual void SendToBack( void );
  108. virtual void BringToFront( void );
  109. virtual void BringNextToControl( Controls::Base* pChild, bool bBehind );
  110. virtual Gwen::Point LocalPosToCanvas( const Gwen::Point& in );
  111. virtual Gwen::Point CanvasPosToLocal( const Gwen::Point& in );
  112. virtual void Dock( int iDock );
  113. virtual int GetDock();
  114. virtual void RestrictToParent( bool restrict ) { m_bRestrictToParent = restrict; }
  115. virtual bool ShouldRestrictToParent() { return m_bRestrictToParent; }
  116. virtual int X() const
  117. {
  118. return m_Bounds.x;
  119. }
  120. virtual int Y() const
  121. {
  122. return m_Bounds.y;
  123. }
  124. virtual int Width() const
  125. {
  126. return m_Bounds.w;
  127. }
  128. virtual int Height() const
  129. {
  130. return m_Bounds.h;
  131. }
  132. virtual int Bottom() const
  133. {
  134. return m_Bounds.y + m_Bounds.h + m_Margin.bottom;
  135. }
  136. virtual int Right() const
  137. {
  138. return m_Bounds.x + m_Bounds.w + m_Margin.right;
  139. }
  140. virtual const Margin& GetMargin() const { return m_Margin; }
  141. virtual const Padding& GetPadding() const { return m_Padding; }
  142. virtual void SetPos( int x, int y );
  143. virtual void SetWidth( int w )
  144. {
  145. SetSize( w, Height());
  146. }
  147. virtual void SetHeight( int h )
  148. {
  149. SetSize( Width(), h);
  150. }
  151. virtual bool SetSize( int w, int h );
  152. virtual bool SetBounds( int x, int y, int w, int h );
  153. virtual bool SetBounds( const Gwen::Rect& bounds );
  154. virtual void SetPadding( const Padding& padding );
  155. virtual void SetMargin( const Margin& margin );
  156. // MoveTo is identical to SetPos except it uses ShouldRestrictToParent()
  157. virtual void MoveTo (int x, int y );
  158. virtual void MoveBy (int x, int y );
  159. virtual const Gwen::Rect& GetBounds() const
  160. {
  161. return m_Bounds;
  162. }
  163. virtual Controls::Base* GetControlAt( int x, int y );
  164. protected:
  165. virtual void OnBoundsChanged( Gwen::Rect oldBounds );
  166. virtual void OnChildBoundsChanged( Gwen::Rect oldChildBounds, Base* pChild );
  167. virtual void OnScaleChanged();
  168. public:
  169. // Innerbounds is the area inside the control that
  170. // doesn't have child controls docked to it.
  171. virtual const Gwen::Rect& GetInnerBounds() const { return m_InnerBounds; }
  172. protected:
  173. Gwen::Rect m_InnerBounds;
  174. public:
  175. virtual const Gwen::Rect& GetRenderBounds() const{ return m_RenderBounds; }
  176. protected:
  177. virtual void UpdateRenderBounds();
  178. public:
  179. virtual void DoRender( Gwen::Skin::Base* skin );
  180. virtual void DoCacheRender( Gwen::Skin::Base* skin, Gwen::Controls::Base* pMaster );
  181. protected:
  182. virtual void Render( Gwen::Skin::Base* skin );
  183. virtual void RenderUnder( Gwen::Skin::Base* /*skin*/ ){};
  184. virtual void RenderOver( Gwen::Skin::Base* /*skin*/ ){};
  185. virtual void RenderFocus( Gwen::Skin::Base* /*skin*/ );
  186. public:
  187. virtual void SetHidden( bool hidden )
  188. {
  189. if ( m_bHidden == hidden )
  190. return;
  191. m_bHidden = hidden;
  192. Invalidate();
  193. }
  194. virtual bool Hidden() const; // Returns true only if this control is hidden
  195. virtual bool Visible() const; // Returns false if this control or its parents are hidden
  196. virtual void Hide(){ SetHidden( true ); }
  197. virtual void Show(){ SetHidden( false ); }
  198. //Skin
  199. virtual void SetSkin( Skin::Base* skin, bool doChildren = false );
  200. virtual Gwen::Skin::Base* GetSkin( void );
  201. // Background drawing
  202. virtual bool ShouldDrawBackground(){ return m_bDrawBackground; }
  203. virtual void SetShouldDrawBackground( bool b ){ m_bDrawBackground =b; }
  204. protected:
  205. virtual void OnSkinChanged( Gwen::Skin::Base* newSkin );
  206. public:
  207. virtual void OnMouseMoved( int x, int y, int deltaX, int deltaY );
  208. virtual bool OnMouseWheeled( int iDelta );
  209. virtual void OnMouseClickLeft( int /*x*/, int /*y*/, bool /*bDown*/ ){};
  210. virtual void OnMouseClickRight( int /*x*/, int /*y*/, bool /*bDown*/ ){}
  211. virtual void OnMouseDoubleClickLeft( int x, int y ){ OnMouseClickLeft( x, y, true ); };
  212. virtual void OnMouseDoubleClickRight( int x, int y ){ OnMouseClickRight( x, y, true ); };
  213. virtual void OnLostKeyboardFocus(){}
  214. virtual void OnKeyboardFocus(){}
  215. virtual void SetMouseInputEnabled( bool b ) { m_bMouseInputEnabled = b; }
  216. virtual bool GetMouseInputEnabled() { return m_bMouseInputEnabled; }
  217. virtual void SetKeyboardInputEnabled( bool b ){ m_bKeyboardInputEnabled = b; }
  218. virtual bool GetKeyboardInputEnabled() const { return m_bKeyboardInputEnabled; }
  219. virtual bool NeedsInputChars(){ return false; }
  220. virtual bool OnChar( Gwen::UnicodeChar /*c*/ ){ return false; }
  221. virtual bool OnKeyPress( int iKey, bool bPress = true );
  222. virtual bool OnKeyRelease( int iKey );
  223. virtual void OnPaste(Controls::Base* /*pFrom*/){}
  224. virtual void OnCopy(Controls::Base* /*pFrom*/){}
  225. virtual void OnCut(Controls::Base* /*pFrom*/){}
  226. virtual void OnSelectAll(Controls::Base* /*pFrom*/){}
  227. virtual bool OnKeyTab( bool bDown );
  228. virtual bool OnKeySpace( bool /*bDown*/ ){ return false; }
  229. virtual bool OnKeyReturn( bool /*bDown*/ ){ return false; }
  230. virtual bool OnKeyBackspace( bool /*bDown*/ ){ return false; }
  231. virtual bool OnKeyDelete( bool /*bDown*/ ){ return false; }
  232. virtual bool OnKeyRight( bool /*bDown*/ ){ return false; }
  233. virtual bool OnKeyLeft( bool /*bDown*/ ){ return false; }
  234. virtual bool OnKeyHome( bool /*bDown*/ ){ return false; }
  235. virtual bool OnKeyEnd( bool /*bDown*/ ){ return false; }
  236. virtual bool OnKeyUp( bool /*bDown*/ ){ return false; }
  237. virtual bool OnKeyDown( bool /*bDown*/ ){ return false; }
  238. virtual bool OnKeyEscape( bool /*bDown*/ ) { return false; }
  239. virtual void OnMouseEnter();
  240. virtual void OnMouseLeave();
  241. virtual bool IsHovered();
  242. virtual bool ShouldDrawHover();
  243. virtual void Touch();
  244. virtual void OnChildTouched( Controls::Base* pChild );
  245. virtual bool IsOnTop();
  246. virtual bool HasFocus();
  247. virtual void Focus();
  248. virtual void Blur();
  249. //Other
  250. virtual void SetDisabled( bool active ) { m_bDisabled = active; }
  251. virtual bool IsDisabled(){ return m_bDisabled; }
  252. virtual void Redraw(){ m_bCacheTextureDirty = true; if ( m_Parent ) m_Parent->Redraw(); }
  253. virtual void SetCacheToTexture() { m_bCacheToTexture = true; }
  254. virtual bool ShouldCacheToTexture() { return m_bCacheToTexture; }
  255. virtual void SetCursor( unsigned char c ){ m_Cursor = c; }
  256. virtual void UpdateCursor();
  257. virtual Gwen::Point GetMinimumSize(){ return Gwen::Point( 1, 1 ); }
  258. virtual Gwen::Point GetMaximumSize(){ return Gwen::Point( 4096, 4096 ); }
  259. virtual void SetToolTip( const String& strText );
  260. virtual void SetToolTip( const UnicodeString& strText );
  261. virtual void SetToolTip( Base* tooltip ) { m_ToolTip = tooltip; if ( m_ToolTip ){ m_ToolTip->SetParent( this ); m_ToolTip->SetHidden( true ); } }
  262. virtual Base* GetToolTip() { return m_ToolTip; }
  263. virtual bool IsMenuComponent();
  264. virtual void CloseMenus();
  265. virtual bool IsTabable() { return m_Tabable; }
  266. virtual void SetTabable( bool isTabable ) { m_Tabable = isTabable; }
  267. //Accelerator functionality
  268. void DefaultAccel( Gwen::Controls::Base* /*pCtrl*/ ) { AcceleratePressed(); }
  269. virtual void AcceleratePressed() {};
  270. virtual bool AccelOnlyFocus() { return false; }
  271. virtual bool HandleAccelerator( Gwen::UnicodeString& accelerator );
  272. template <typename T>
  273. void AddAccelerator( Gwen::UnicodeString accelerator, T func, Gwen::Event::Handler* handler = NULL )
  274. {
  275. if ( handler == NULL )
  276. handler = this;
  277. Gwen::Event::Caller* caller = new Gwen::Event::Caller();
  278. caller->Add( handler, func );
  279. m_Accelerators[ accelerator ] = caller;
  280. }
  281. void AddAccelerator( Gwen::UnicodeString accelerator )
  282. {
  283. AddAccelerator( accelerator, &Base::DefaultAccel, this );
  284. }
  285. AccelMap m_Accelerators;
  286. // Default Events
  287. Gwen::Event::Caller onHoverEnter;
  288. Gwen::Event::Caller onHoverLeave;
  289. // Childrens List
  290. Base::List Children;
  291. protected:
  292. // The logical parent
  293. // It's usually what you expect, the control you've parented it to.
  294. Base* m_Parent;
  295. // If the innerpanel exists our children will automatically
  296. // become children of that instead of us - allowing us to move
  297. // them all around by moving that panel (useful for scrolling etc)
  298. Base* m_InnerPanel;
  299. // This is the panel's actual parent - most likely the logical
  300. // parent's InnerPanel (if it has one). You should rarely need this.
  301. Base* m_ActualParent;
  302. Base* m_ToolTip;
  303. Skin::Base* m_Skin;
  304. Gwen::Rect m_Bounds;
  305. Gwen::Rect m_RenderBounds;
  306. Padding m_Padding;
  307. Margin m_Margin;
  308. Gwen::String m_Name;
  309. bool m_bRestrictToParent;
  310. bool m_bDisabled;
  311. bool m_bHidden;
  312. bool m_bMouseInputEnabled;
  313. bool m_bKeyboardInputEnabled;
  314. bool m_bDrawBackground;
  315. int m_iDock;
  316. unsigned char m_Cursor;
  317. bool m_Tabable;
  318. public:
  319. bool NeedsLayout(){ return m_bNeedsLayout; }
  320. void Invalidate();
  321. void InvalidateParent(){ if ( m_Parent ){ m_Parent->Invalidate(); } }
  322. void InvalidateChildren( bool bRecursive = false );
  323. void Position( int pos, int xpadding = 0, int ypadding = 0 );
  324. protected:
  325. virtual void RecurseLayout( Skin::Base* skin );
  326. virtual void Layout( Skin::Base* skin );
  327. virtual void PostLayout( Skin::Base* /*skin*/ ){};
  328. bool m_bNeedsLayout;
  329. bool m_bCacheTextureDirty;
  330. bool m_bCacheToTexture;
  331. //
  332. // Drag + Drop
  333. public:
  334. // Giver
  335. virtual void DragAndDrop_SetPackage( bool bDraggable, const String& strName = "", void* pUserData = NULL );
  336. virtual bool DragAndDrop_Draggable();
  337. virtual bool DragAndDrop_ShouldStartDrag(){ return true; }
  338. virtual void DragAndDrop_StartDragging( Gwen::DragAndDrop::Package* pPackage, int x, int y );
  339. virtual Gwen::DragAndDrop::Package* DragAndDrop_GetPackage( int x, int y );
  340. virtual void DragAndDrop_EndDragging( bool /*bSuccess*/, int /*x*/, int /*y*/ ){};
  341. protected:
  342. DragAndDrop::Package* m_DragAndDrop_Package;
  343. public:
  344. // Receiver
  345. virtual void DragAndDrop_HoverEnter( Gwen::DragAndDrop::Package* /*pPackage*/, int /*x*/, int /*y*/ ){ }
  346. virtual void DragAndDrop_HoverLeave( Gwen::DragAndDrop::Package* /*pPackage*/ ){ }
  347. virtual void DragAndDrop_Hover( Gwen::DragAndDrop::Package* /*pPackage*/, int /*x*/, int /*y*/ ){};
  348. virtual bool DragAndDrop_HandleDrop( Gwen::DragAndDrop::Package* pPackage, int x, int y );
  349. virtual bool DragAndDrop_CanAcceptPackage( Gwen::DragAndDrop::Package* /*pPackage*/ ){ return false; }
  350. //
  351. // This is to be used by the client implementation
  352. // NOT HOOKS ETC.
  353. //
  354. public:
  355. void* GetUserData(){ return m_pUserData; }
  356. void SetUserData( void* pData ){ m_pUserData = pData; }
  357. private:
  358. void* m_pUserData;
  359. //
  360. // Useful anim shortcuts
  361. //
  362. public:
  363. #ifndef GWEN_NO_ANIMATION
  364. virtual void Anim_WidthIn( float fLength, float fDelay = 0.0f, float fEase = 1.0f );
  365. virtual void Anim_HeightIn( float fLength, float fDelay = 0.0f, float fEase = 1.0f );
  366. virtual void Anim_WidthOut( float fLength, bool bHide = true, float fDelay = 0.0f, float fEase = 1.0f );
  367. virtual void Anim_HeightOut( float fLength, bool bHide = true, float fDelay = 0.0f, float fEase = 1.0f );
  368. #endif
  369. };
  370. }
  371. }
  372. // To be placed in the controls .h definition.
  373. #define GWEN_CONTROL( ThisName, BaseName )\
  374. public:\
  375. typedef BaseName BaseClass;\
  376. typedef ThisName ThisClass;\
  377. GWEN_IMPLEMENT_CAST(ThisName);\
  378. ThisName( Gwen::Controls::Base* pParent )
  379. #define GWEN_CONTROL_INLINE( ThisName, BaseName )\
  380. GWEN_CONTROL( ThisName, BaseName ) : BaseClass( pParent )
  381. #define GWEN_CONTROL_CONSTRUCTOR( ThisName )\
  382. ThisName::ThisName( Gwen::Controls::Base* pParent ) : BaseClass( pParent )
  383. #endif