#include "wxProtoApp.h"

/*BEGIN_DECLARE_EVENT_TYPES()
    DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777)
END_DECLARE_EVENT_TYPES()

DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)*/

const wxEventType wxProtoApp::wxPROTO_DISPATCH_EVENT = wxNewEventType();

// it may also be convenient to define an event table macro for this event type
#define EVT_PROTO_DISPATCH_COMMAND(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxProtoApp::wxPROTO_DISPATCH_EVENT, id, -1, \
        (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
        (wxObject *) NULL \
    ),

BEGIN_EVENT_TABLE(wxProtoApp, wxApp)
    EVT_PROTO_DISPATCH_COMMAND(-1, wxProtoApp::OnDispatchEvent)
END_EVENT_TABLE()
        
wxProtoApp::wxProtoApp()
 : ProtoDispatcher::Controller(dispatcher)
{
    
}

wxProtoApp::~wxProtoApp()
{
    dispatcher.Stop();
}

int wxProtoApp::MainLoop()
{
    // 1) Start ProtoDispatcher thread
    dispatcher.StartThread(this);
    
    // 2) Enter standard event loop
    int result = wxApp::MainLoop();
    
    TRACE("MainLoop exiting ...\n");
    
    // Stop ProtoDispatcher thread
    dispatcher.Stop();
    return result;
}  // end wxProtoApp::MainLoop()


bool wxProtoApp::SignalDispatchReady()
{
    wxCommandEvent dispatchEvent(wxPROTO_DISPATCH_EVENT);
    //AddPendingEvent(dispatchEvent);    
    wxPostEvent(this, dispatchEvent);
    return true;
}  // end wxProtoApp::SignalDispatchReady()


void wxProtoApp::OnDispatchEvent(wxCommandEvent& /*event*/)
{
    ProtoDispatcher::Controller::OnDispatch();
}
