
#include "wxProtoApp.h"

class wxProtoExampleApp : public wxProtoApp
{
    public:
        wxProtoExampleApp();
        ~wxProtoExampleApp();
        
        // wxApp base class overrides
        bool OnInit();
        int OnExit();
                
    private:
        bool OnTxTimeout(ProtoTimer& theTimer);
        void OnClose(wxCloseEvent& event);
        
        enum
        {
            ID_START = wxID_HIGHEST+1,
            ID_STOP   
        };
    
        ProtoTimer   tx_timer;
        unsigned int counter;
        wxFrame*     frame;      // main window
        wxTextCtrl*  tx_dest_edit;
        wxTextCtrl*  tx_rate_edit;
        wxTextCtrl*  rx_port_edit;
        DECLARE_EVENT_TABLE()
                
};  // end class wxProtoExampleApp
        
BEGIN_EVENT_TABLE(wxProtoExampleApp, wxProtoApp)
    EVT_CLOSE(wxProtoExampleApp::OnClose)        
END_EVENT_TABLE()
    
DECLARE_APP(wxProtoExampleApp)
IMPLEMENT_APP(wxProtoExampleApp)
        
wxProtoExampleApp::wxProtoExampleApp()
 : counter(0), frame(NULL)
{
    tx_timer.SetListener(this, &wxProtoExampleApp::OnTxTimeout);
}

wxProtoExampleApp::~wxProtoExampleApp()
{
    
}

void wxProtoExampleApp::OnClose(wxCloseEvent& /*event*/)
{ 
    TRACE("wxProtoExampleApp::OnClose() ...\n");
}

bool wxProtoExampleApp::OnInit()
{
    tx_timer.SetInterval(0.001);
    tx_timer.SetRepeat(-1);
    ActivateTimer(tx_timer);
    
    if (!(frame = new wxFrame()))
    {
        DMSG(0, "wxProtoExampleApp::OnInit() new wxFrame error: %s\n",
                GetErrorString());
        return false;   
    }
    if (!frame->Create((wxFrame*)NULL, -1, _T("wxProtoExample")))
    {
        OnExit();
        return false;
    }
    
    wxBoxSizer* masterSizer = new wxBoxSizer(wxVERTICAL);
    
    
    // Row 1 - tx dest
    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(new wxStaticText(frame, -1, _T("TxDest:")), 0, 
               wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
    if (!(tx_dest_edit = new wxTextCtrl(frame, -1, _T("127.0.0.1/5000"))))
    {
        DMSG(0, "wxProtoExampleApp::OnInit() Error creating tx_dest_edit\n");
        OnExit();
        return false;
    }
    sizer->Add(tx_dest_edit, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 10);
    masterSizer->Add(sizer, 0, wxEXPAND | wxALIGN_CENTER);
    
    // Row 2 - tx rate
    sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(new wxStaticText(frame, -1, _T("TxRate:")), 0, 
               wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
    if (!(tx_rate_edit = new wxTextCtrl(frame, -1, _T("1.0"))))
    {
        DMSG(0, "wxProtoExampleApp::OnInit() Error creating tx_dest_edit\n");
        OnExit();
        return false;
    }
    sizer->Add(tx_rate_edit, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 10);
    sizer->Add(new wxStaticText(frame, -1, _T("pkt/sec")), 0, 
               wxALIGN_CENTER_VERTICAL);
    masterSizer->Add(sizer, 0, wxEXPAND | wxALIGN_CENTER);
    
    // Row 3 - rx port
    sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(new wxStaticText(frame, -1, _T("TxRate:")), 0, 
               wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
    if (!(rx_port_edit = new wxTextCtrl(frame, -1, _T("5000"))))
    {
        DMSG(0, "wxProtoExampleApp::OnInit() Error creating tx_dest_edit\n");
        OnExit();
        return false;
    }
    sizer->Add(rx_port_edit, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 10);
    masterSizer->Add(sizer, 0, wxEXPAND | wxALIGN_CENTER);
    
    
    // Row 4 - start/stop buttons
    sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(new wxButton(frame, ID_START, _T("Start")), 0, 
               wxALIGN_CENTER | wxALL, 10);
    sizer->Add(new wxButton(frame, ID_STOP, _T("Stop")), 0,  
               wxALIGN_CENTER | wxALL, 10);
    masterSizer->Add(sizer, 0, wxALIGN_CENTER | wxALL, 10);
    
    frame->SetSizer(masterSizer);
    frame->Show(true);
    return true;
}

int wxProtoExampleApp::OnExit()
{
    TRACE("wxProtoExampleApp::OnExit() ...\n");
    if (frame)
    {
        
        //frame->Destroy();
        frame = NULL;
        tx_dest_edit = NULL;
        tx_rate_edit = NULL; 
    }
    return 0;
}  // end wxProtoExampleApp::OnExit()

bool wxProtoExampleApp::OnTxTimeout(ProtoTimer& /*theTimer*/)
{
    counter++;
    //TRACE("wxPrototoExampleApp::OnTxTimeout(%u) ...\n", counter);
    return true;
}
