00001 ///////////////////////////////////////////////////////////////////////////// 00002 // Name: wxlcallb.h 00003 // Purpose: wxLuaEventCallback and wxLuaWinDestroyCallback 00004 // Author: Francis Irving, John Labenski 00005 // Created: 21/01/2002 00006 // Copyright: (c) 2002 Creature Labs. All rights reserved. 00007 // Licence: wxWidgets licence 00008 ///////////////////////////////////////////////////////////////////////////// 00009 00010 #ifndef _WXLCALLB_H_ 00011 #define _WXLCALLB_H_ 00012 00013 #include "wxlua/include/wxldefs.h" 00014 #include "wxlua/include/wxlstate.h" 00015 00016 // ---------------------------------------------------------------------------- 00017 // wxLuaEventCallback - Forward events from wxEvtHandlers to Lua functions. 00018 // 00019 // The wxLuaEventCallback is created with the wxLuaState, the stack index of a 00020 // Lua function to call when a wxEvent is received, the window id ranges, and 00021 // the wxEventType used with wxEvtHandler::Connect() with "this" as the 00022 // callback user data for event. 00023 // 00024 // Do NOT delete wxLuaEventCallbacks since the wxEvtHandler deletes the 00025 // callback user data itself. 00026 // 00027 // The function wxLuaEventCallback::OnAllEvents() generically handles all wxEvents 00028 // by retrieving the wxLuaEventCallback instance from the wxEvent userdata 00029 // to call wxLuaEventCallback::OnEvent() on the correct instance. 00030 // ---------------------------------------------------------------------------- 00031 00032 #define WXLUAEVENTCALLBACK_NOROUTINE 1000000 // use this for the lua_func_stack_idx 00033 // param of the constructor for no Lua routine 00034 00035 class WXDLLIMPEXP_WXLUA wxLuaEventCallback : public wxObject 00036 { 00037 public: 00038 // default constructor, call Connect() to actually connect the event 00039 wxLuaEventCallback(); 00040 00041 virtual ~wxLuaEventCallback(); 00042 00043 // Verifies the inputs and calls evtHandler->Connect() with "this" as 00044 // the callback userdata. 00045 // lua_func_stack_idx is the Lua stack index of a function to call with 00046 // the wxEvent as the single parameter. 00047 // win_id and last_id follow the same notation as wxEvtHandler::Connect(). 00048 // If only one event Id is needed set last_id = wxID_ANY 00049 // Returns an empty string on success and the wxEvtHandler takes ownership of this, 00050 // otherwise an error message is returned and you must delete this since nobody else will. 00051 virtual wxString Connect( const wxLuaState& wxlState, int lua_func_stack_idx, 00052 wxWindowID win_id, wxWindowID last_id, 00053 wxEventType eventType, wxEvtHandler *evtHandler ); 00054 00055 void ClearwxLuaState(); // m_wxlState.UnRef() 00056 00057 wxLuaState GetwxLuaState() const { return m_wxlState; } 00058 wxWindowID GetId() const { return m_id; } 00059 wxWindowID GetLastId() const { return m_last_id; } 00060 wxEventType GetEventType() const { return m_wxlBindEvent ? *m_wxlBindEvent->eventType : wxEVT_NULL; } 00061 wxEvtHandler* GetEvtHandler() const { return m_evtHandler; } 00062 int GetLuaFuncRef() const { return m_luafunc_ref; } 00063 00064 const wxLuaBindEvent* GetwxLuaBindEvent() const { return m_wxlBindEvent; } 00065 00066 // Get a human readable string about this callback. 00067 // "wxEVT_XXX(evt#) -> wxLuaEventCallback(&callback, ids %d %d)|wxEvtHandler(&evthandler) -> wxEvtHandlerClassName" 00068 wxString GetInfo() const; // Get a human readable string 00069 00070 // Central event handler that calls OnEvent() for the actual 00071 // wxLuaEventCallback callback userdata. 00072 // This function is treated like a static function that all handlers of 00073 // this class will call. 00074 void OnAllEvents(wxEvent& event); 00075 00076 // Handle the wxEvent by calling the Lua function to handle the event. 00077 // The Lua function will receive a single parameter, the wxEvent. 00078 virtual void OnEvent(wxEvent *event); 00079 00080 protected: 00081 int m_luafunc_ref; // ref of the Lua routine to call in the wxlua_lreg_refs_key registry table 00082 wxLuaState m_wxlState; // stored to verify that that lua_State is still active 00083 wxEvtHandler* m_evtHandler; 00084 wxWindowID m_id; 00085 wxWindowID m_last_id; 00086 const wxLuaBindEvent* m_wxlBindEvent; // data for this wxEventType 00087 00088 private: 00089 DECLARE_ABSTRACT_CLASS(wxLuaEventCallback) 00090 }; 00091 00092 // ---------------------------------------------------------------------------- 00093 // wxLuaWinDestroyCallback - Handle the wxEVT_DESTROY event from wxWindows. 00094 // 00095 // Clears the metatable for the wxWindow userdata so that after a call to 00096 // win:Destroy() calling a function on win won't crash wxLua, but will generate 00097 // an error message in Lua. 00098 // 00099 // Do NOT delete this, the wxEvtHandler deletes the callback user data itself 00100 // unless it is !Ok() since that means it wasn't attached to the window. 00101 // 00102 // The function OnAllDestroyEvents() generically handles the events and forwards them 00103 // to the wxEvent's wxLuaWinDestroyCallback callback user data function OnDestroy(). 00104 // ---------------------------------------------------------------------------- 00105 00106 class WXDLLIMPEXP_WXLUA wxLuaWinDestroyCallback : public wxObject 00107 { 00108 public: 00109 wxLuaWinDestroyCallback(const wxLuaState& state, wxWindow *win); 00110 00111 virtual ~wxLuaWinDestroyCallback(); 00112 00113 void ClearwxLuaState(); // m_wxlState.UnRef() 00114 00115 wxLuaState GetwxLuaState() const { return m_wxlState; } 00116 wxWindow* GetWindow() const { return m_window; } 00117 00118 // If Ok then this should be attached to a wxEVT_DESTROY callback 00119 // else you should delete this since nobody else will. 00120 bool Ok() const { return m_wxlState.Ok() && (m_window != NULL); } 00121 00122 // Get a human readable string 00123 // "wxWindowClassName(&win, id=%d)|wxLuaDestroyCallback(&callback)" 00124 wxString GetInfo() const; 00125 00126 // Central event handler that calls OnDestroy() for the actual 00127 // wxLuaWinDestroyCallback callback user data. 00128 // This function is treated like a static function that all handlers of 00129 // this class will call. 00130 void OnAllDestroyEvents(wxWindowDestroyEvent& event); 00131 00132 // Handle the event by clearing the metatable for the window. 00133 virtual void OnDestroy(wxWindowDestroyEvent& event); 00134 00135 protected: 00136 wxLuaState m_wxlState; // store it since we're added to a list of its callbacks. 00137 wxWindow* m_window; 00138 00139 private: 00140 DECLARE_ABSTRACT_CLASS(wxLuaWinDestroyCallback) 00141 }; 00142 00143 #endif //_WXLCALLB_H_