Events.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #pragma once
  7. #ifndef GWEN_EVENTS_H
  8. #define GWEN_EVENTS_H
  9. #include <list>
  10. #include "Gwen/Exports.h"
  11. #include "Gwen/Structures.h"
  12. // TODO: REMOVE THIS - IT SUCKS. Template the function instead.
  13. #define GWEN_MCALL( fnc ) this, (Gwen::Event::Handler::Function)&fnc
  14. namespace Gwen
  15. {
  16. namespace Controls
  17. {
  18. class Base;
  19. }
  20. namespace Event
  21. {
  22. class Caller;
  23. // A class must be derived from this
  24. class GWEN_EXPORT Handler
  25. {
  26. public:
  27. Handler();
  28. virtual ~Handler();
  29. void RegisterCaller( Caller* );
  30. void UnRegisterCaller( Caller* );
  31. protected:
  32. void CleanLinks();
  33. std::list<Caller*> m_Callers;
  34. public:
  35. typedef void (Handler::*Function)( Gwen::Controls::Base* pFromPanel );
  36. typedef void (Handler::*FunctionStr)( const Gwen::String& string );
  37. };
  38. //
  39. //
  40. //
  41. class GWEN_EXPORT Caller
  42. {
  43. public:
  44. Caller();
  45. ~Caller();
  46. void Call( Controls::Base* pThis );
  47. template <typename T>
  48. void Add( Event::Handler* ob, T f )
  49. {
  50. AddInternal( ob, static_cast<Handler::Function>(f) );
  51. }
  52. void RemoveHandler( Event::Handler* pObject );
  53. protected:
  54. void CleanLinks();
  55. void AddInternal( Event::Handler* pObject, Handler::Function pFunction );
  56. struct handler
  57. {
  58. Handler::Function fnFunction;
  59. Event::Handler* pObject;
  60. };
  61. std::list<handler> m_Handlers;
  62. };
  63. }
  64. }
  65. #endif