views:

253

answers:

2

Hello all,

I'm working on a program for my C++ programming class, using wxWidgets. I'm having a huge problem in that my event handlers (I assume) are not getting called, because when I click on the button to trigger the event, nothing happens. My question is: Can you help me find the problem and explain why they would not be getting called?

The event handlers OnAbout and OnQuit are working, just not OnCompute or OnClear. I'm really frustrated as I can't figure this out. Thanks a bunch in advance!

#include "wx/wx.h"
#include "time.h"
#include <string>
using std::string;

// create object of Time class
Time first;

class App: public wxApp
{
virtual bool OnInit();
};

class MainPanel : public wxPanel
{
 public:
   // Constructor for panel class
   // Constructs my panel class 
   // Params - wxWindow pointer
   // no return type
   // pre-conditions: none
   // post-conditions: none
   MainPanel(wxWindow* parent);
   // OnCompute is the event handler for the Compute button
   // params - none
   // preconditions - none
   // postconditions - tasks will have been carried otu successfully
   // returns void
   void OnCompute(wxCommandEvent& WXUNUSED(event));
   // OnClear is the event handler for the Clear button
   // params - none
   // preconditions - none
   // postconditions - all text areas will be cleared of data
   // returns void
   void OnClear(wxCommandEvent& WXUNUSED(event));
   // Destructor for panel class
   // params none
   // preconditions - none
   // postconditions - none
   // no return type
   ~MainPanel( );
  private:
wxStaticText *startLabel;
wxStaticText *endLabel;
wxStaticText *pCLabel;
wxStaticText *newEndLabel;
wxTextCtrl *start;
wxTextCtrl *end;
wxTextCtrl *pC;
wxTextCtrl *newEnd;
wxButton *compute;
wxButton *clear;

     DECLARE_EVENT_TABLE()
    };


class MainFrame: public wxFrame
{
private:
wxPanel *mainPanel;
public:
MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);

~MainFrame();

  DECLARE_EVENT_TABLE()
  };

enum
{
ID_Quit = 1,
ID_About,
BUTTON_COMPUTE = 100,
BUTTON_CLEAR = 200
};

IMPLEMENT_APP(App)

BEGIN_EVENT_TABLE(MainFrame, wxFrame)
    EVT_MENU(ID_Quit, MainFrame::OnQuit)
    EVT_MENU(ID_About, MainFrame::OnAbout)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(MainPanel, wxPanel)
EVT_MENU(BUTTON_COMPUTE, MainPanel::OnCompute)
EVT_MENU(BUTTON_CLEAR, MainPanel::OnClear)
END_EVENT_TABLE()

bool App::OnInit()
{
MainFrame *frame = new MainFrame( _("Good Guys Delivery Time Calculator"), wxPoint(50,       50),
                              wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
} 

MainPanel::MainPanel(wxWindow* parent) : wxPanel(parent) 
{
startLabel = new wxStaticText(this, -1, "Start Time:", wxPoint(75, 35));
start = new wxTextCtrl(this, -1, "", wxPoint(135, 35), wxSize(40, 21));

endLabel = new wxStaticText(this, -1, "End Time:", wxPoint(200, 35));
end = new wxTextCtrl(this, -1, "", wxPoint(260, 35), wxSize(40, 21));

pCLabel = new wxStaticText(this, -1, "Percent Change:", wxPoint(170, 85));
pC = new wxTextCtrl(this, -1, "", wxPoint(260, 85), wxSize(40, 21));

newEndLabel = new wxStaticText(this, -1, "New End Time:", wxPoint(180, 130));
newEnd = new wxTextCtrl(this, -1, "", wxPoint(260, 130), wxSize(40, 21));

compute = new wxButton(this, BUTTON_COMPUTE, "Compute", wxPoint(135, 185),  wxSize(75, 35));
clear = new wxButton(this, BUTTON_CLEAR, "Clear", wxPoint(230, 185), wxSize(75, 35));
}

MainPanel::~MainPanel() {}

MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
mainPanel = new MainPanel(this);
wxMenu *menuFile = new wxMenu;

menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );

wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );

SetMenuBar( menuBar );

CreateStatusBar();
SetStatusText( _("Hi") );
}

MainFrame::~MainFrame() {}

void MainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}

void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("Alex Olson\nProject 11"),
              _("About"),
              wxOK | wxICON_INFORMATION, this);
}

void MainPanel::OnCompute(wxCommandEvent& WXUNUSED(event))
{
int startT;
int endT;
int newEndT;
double tD;

wxString startTString = start->GetValue();
wxString endTString = end->GetValue();

startT = wxAtoi(startTString);
endT = wxAtoi(endTString);

pC->GetValue().ToDouble(&tD);

first.SetStartTime(startT);
first.SetEndTime(endT);
first.SetTimeDiff(tD);

try {
    first.ValidateData();
    newEndT = first.ComputeEndTime();
    *newEnd << newEndT;
}
catch (BaseException& e) {
    wxMessageBox(_(e.GetMessage()), 
                 _("Something Went Wrong!"),
                 wxOK | wxICON_INFORMATION, this);
}
}

void MainPanel::OnClear(wxCommandEvent& WXUNUSED(event))
{
start->Clear();
end->Clear();
pC->Clear();
newEnd->Clear();
}
A: 

I think I see it. In the event table for MainPanel instead of EVT_MENU use EVT_BUTTON.

Matt Blaine
+2  A: 

EVT_MENU(BUTTON_COMPUTE, MainPanel::OnCompute) EVT_MENU(BUTTON_CLEAR, MainPanel::OnClear)

In the above statements use EVT_BUTTON instead of EVT_MENU.

sankoz
Thank you SO MUCH!!!
Alex