I am trying to follow this tutorial. http://www.remy.org.uk/tech.php?tech=1407951209
the test compiles and runs apart from not getting the correct result. for some reason the test gui is not getting focus.
I tried to step through the code it appends the text on the current window i have focus which is the text editor.
What am i missing from the example?
#pragma once
#include <wx/wx.h>
#include <wx/uiaction.h>
#include "gtest/gtest.h"
class TestFrame : public wxFrame
{
wxTextCtrl *textIn;
wxButton *button;
wxTextCtrl *textOut;
FRIEND_TEST(GuiTest, CopyTest);
public:
TestFrame() : wxFrame(NULL, wxID_ANY, "wxUnitTest", wxPoint(50, 50), wxSize(450, 340))
{
textIn = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
button = new wxButton(this, wxID_ANY, wxT(" => "), wxDefaultPosition, wxDefaultSize, 0);
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &TestFrame::OnButton, this);
textOut = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
wxBoxSizer *boxSizer = new wxBoxSizer(wxHORIZONTAL); boxSizer->Add(textIn, 1, wxALL, 5);
boxSizer->Add(button, 0, wxALL, 5); boxSizer->Add(textOut, 1, wxALL, 5);
this->SetSizer(boxSizer); this->Layout(); this->Centre(wxBOTH); }
void OnButton(wxCommandEvent &WXUNUSED(event)) { this->textOut->SetValue(this->textIn->GetValue()); }
};
class TestApp : public wxApp
{
public: TestFrame *frame; virtual bool OnInit()
{
frame = new TestFrame();
frame->Show(true);
return true;
}
};
class GuiTest : public testing::Test
{
protected:
TestApp *app;
virtual void SetUp()
{
char appname[] = "wxUnitTest.exe";
int argc = 1;
char *argv[1] = { appname };
app = new TestApp();
wxApp::SetInstance(app);
wxEntryStart(argc, argv);
app->OnInit();
}
virtual void TearDown()
{
//wxTheApp->OnRun();
app->OnExit();
wxEntryCleanup();
}
};
TEST_F(GuiTest, CopyTest)
{
wxUIActionSimulator acts;
app->frame->textIn->SetFocus();
wxYield();
acts.Text("Text");
wxYield();
app->frame->button->SetFocus();
wxYield();
acts.Char(WXK_RETURN);
wxYield();
ASSERT_EQ("Text", app->frame->textOut->GetValue());
}
// Main.cpp
#include "stdafx.h"
#include "Main.h"
#include "tst_guitest.h"
#include "wx/app.h"
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Related
I followed the wxCrafter Hello World tutorial: https://wiki.codelite.org/pmwiki.php/Main/WxCrafterHelloWorld
Building process shows no errors, however no dialog shows up when I click "Build->Run" in CodeLite 13, for some reason nothing happens :/
main.cpp:
#include <wx/app.h>
#include <wx/event.h>
#include "MainDialog.h"
#include <wx/image.h>
// Define the MainApp
class MainApp : public wxApp
{
public:
MainApp() {}
virtual ~MainApp() {}
virtual bool OnInit() {
// Add the common image handlers
wxImage::AddHandler( new wxPNGHandler );
wxImage::AddHandler( new wxJPEGHandler );
MainDialog mainDialog(NULL);
mainDialog.ShowModal();
return false;
}
};
DECLARE_APP(MainApp)
IMPLEMENT_APP(MainApp)
MainDialog.h:
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include "wxcrafter.h"
class MainDialog : public MainDialogBaseClass
{
public:
MainDialog(wxWindow* parent);
virtual ~MainDialog();
protected:
virtual void OnButtonokButtonClicked(wxCommandEvent& event);
};
#endif // MAINDIALOG_H
MainDialog.cpp:
#include "MainDialog.h"
#include <wx/msgdlg.h>
MainDialog::MainDialog(wxWindow* parent)
: MainDialogBaseClass(parent)
{
}
MainDialog::~MainDialog()
{
}
void MainDialog::OnButtonokButtonClicked(wxCommandEvent& event)
{
::wxMessageBox(_("Hello World"));
}
wxcrafter.h:
//////////////////////////////////////////////////////////////////////
// This file was auto-generated by codelite's wxCrafter Plugin
// wxCrafter project file: wxcrafter.wxcp
// Do not modify this file by hand!
//////////////////////////////////////////////////////////////////////
#ifndef _WORKSPACE_MEDCEN_PLUS_WXCRAFTER_BASE_CLASSES_H
#define _WORKSPACE_MEDCEN_PLUS_WXCRAFTER_BASE_CLASSES_H
#include <wx/artprov.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/iconbndl.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/xrc/xh_bmp.h>
#include <wx/xrc/xmlres.h>
#if wxVERSION_NUMBER >= 2900
#include <wx/persist.h>
#include <wx/persist/bookctrl.h>
#include <wx/persist/toplevel.h>
#include <wx/persist/treebook.h>
#endif
#ifdef WXC_FROM_DIP
#undef WXC_FROM_DIP
#endif
#if wxVERSION_NUMBER >= 3100
#define WXC_FROM_DIP(x) wxWindow::FromDIP(x, NULL)
#else
#define WXC_FROM_DIP(x) x
#endif
class MainDialogBaseClass : public wxDialog
{
protected:
wxStaticText* m_staticText21;
wxTextCtrl* m_textCtrl23;
wxStaticLine* m_staticLine15;
wxButton* m_buttonOK;
wxButton* m_buttonCancel;
protected:
virtual void OnButtonokButtonClicked(wxCommandEvent& event)
{
event.Skip();
}
public:
wxStaticText* GetStaticText21()
{
return m_staticText21;
}
wxTextCtrl* GetTextCtrl23()
{
return m_textCtrl23;
}
wxStaticLine* GetStaticLine15()
{
return m_staticLine15;
}
wxButton* GetButtonOK()
{
return m_buttonOK;
}
wxButton* GetButtonCancel()
{
return m_buttonCancel;
}
MainDialogBaseClass(wxWindow* parent,
wxWindowID id = wxID_ANY,
const wxString& title = _("My Dialog"),
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxSize(500, 300),
long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
virtual ~MainDialogBaseClass();
};
#endif
wxcrafter.cpp:
//////////////////////////////////////////////////////////////////////
// This file was auto-generated by codelite's wxCrafter Plugin
// wxCrafter project file: wxcrafter.wxcp
// Do not modify this file by hand!
//////////////////////////////////////////////////////////////////////
#include "wxcrafter.h"
// Declare the bitmap loading function
extern void wxC9ED9InitBitmapResources();
static bool bBitmapLoaded = false;
MainDialogBaseClass::MainDialogBaseClass(wxWindow* parent,
wxWindowID id,
const wxString& title,
const wxPoint& pos,
const wxSize& size,
long style)
: wxDialog(parent, id, title, pos, size, style)
{
if(!bBitmapLoaded) {
// We need to initialise the default bitmap handler
wxXmlResource::Get()->AddHandler(new wxBitmapXmlHandler);
wxC9ED9InitBitmapResources();
bBitmapLoaded = true;
}
this->SetFocus();
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
this->SetSizer(mainSizer);
wxBoxSizer* boxSizer19 = new wxBoxSizer(wxHORIZONTAL);
mainSizer->Add(boxSizer19, 1, wxALL | wxEXPAND, WXC_FROM_DIP(5));
m_staticText21 = new wxStaticText(
this, wxID_ANY, _("Static Text Label"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
boxSizer19->Add(m_staticText21, 0, wxALL, WXC_FROM_DIP(5));
m_textCtrl23 = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
#if wxVERSION_NUMBER >= 3000
m_textCtrl23->SetHint(wxT(""));
#endif
boxSizer19->Add(m_textCtrl23, 0, wxALL, WXC_FROM_DIP(5));
mainSizer->Add(0, 0, 1, wxALL | wxEXPAND, WXC_FROM_DIP(5));
m_staticLine15 =
new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), wxLI_HORIZONTAL);
mainSizer->Add(m_staticLine15, 0, wxALL | wxEXPAND, WXC_FROM_DIP(5));
wxBoxSizer* boxSizer12 = new wxBoxSizer(wxHORIZONTAL);
mainSizer->Add(boxSizer12, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, WXC_FROM_DIP(5));
m_buttonOK = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
m_buttonOK->SetDefault();
boxSizer12->Add(m_buttonOK, 0, wxALL, WXC_FROM_DIP(5));
m_buttonCancel =
new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
boxSizer12->Add(m_buttonCancel, 0, wxALL, WXC_FROM_DIP(5));
SetName(wxT("MainDialogBaseClass"));
SetSize(wxDLG_UNIT(this, wxSize(500, 300)));
if(GetSizer()) {
GetSizer()->Fit(this);
}
if(GetParent()) {
CentreOnParent(wxBOTH);
} else {
CentreOnScreen(wxBOTH);
}
#if wxVERSION_NUMBER >= 2900
if(!wxPersistenceManager::Get().Find(this)) {
wxPersistenceManager::Get().RegisterAndRestore(this);
} else {
wxPersistenceManager::Get().Restore(this);
}
#endif
// Connect events
m_buttonOK->Connect(
wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainDialogBaseClass::OnButtonokButtonClicked), NULL, this);
}
MainDialogBaseClass::~MainDialogBaseClass()
{
m_buttonOK->Disconnect(
wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainDialogBaseClass::OnButtonokButtonClicked), NULL, this);
}
Something is wrong with either the C::L installation or the wxCrafter plugin.
If you did follow the referenced tutorial, it should create a wxButton object as a member of the dialog and bind the event handler to it.
Can you compile and run the minimal sample from the wxWidgets install?
I have a simple frame which is containing some buttons. My aim is that, after clicking the GetMousePosition button, getting the first mouse click position. I try to capture mouse click, even if I click outside of the running application.
This is a desktop application running on Windows. I tried some mouse events that wxwidgets provides, but I couldn't handle the next click event. I tried to find a solution with following code, but if there is some different solution, I can throw that code in the trash.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(BUTTON_GetPos, MyFrame::OnButtonClick)
EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent)
EVT_MOUSE_CAPTURE_LOST(MyFrame::OnMouseCaptureLost)
END_EVENT_TABLE()
//some more code
void MyFrame::OnButtonClick(wxCommandEvent & WXUNUSED(event))
{
//Start Capturing for next mouse left-click
if (!HasCapture())
CaptureMouse();
}
void MyFrame::OnMouseEvent(wxMouseEvent &event)
{
if (event.LeftDown()) {
//GetMousePosition
if (HasCapture())
ReleaseMouse();
}
}
void MyFrame::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
{
}
I expect to get the mouse position, in the first left click after the button is pressed.
The code you posted looks like it should work. If there's a problem, it might be in the code you omitted. Anyway, here's a small example application showing the behavior you want. The underlying logic of this example is the same as the code you posted, except this example uses dynamic binding instead of event tables.
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
class MyFrame : public wxFrame
{
public:
MyFrame(wxWindow* parent);
protected:
void OnButtonClick(wxCommandEvent& event);
void OnMouseCapLost(wxMouseCaptureLostEvent& event);
void OnLeftDown(wxMouseEvent&);
void CleanUp();
private:
wxTextCtrl* m_textCtrl;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
MyFrame::MyFrame(wxWindow* parent)
:wxFrame(parent, wxID_ANY, "Demo", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
{
wxPanel* panel = new wxPanel(this, wxID_ANY );
wxButton* button = new wxButton(panel, wxID_ANY, "Click Me");
m_textCtrl = new wxTextCtrl(panel, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_DONTWRAP|wxTE_MULTILINE|wxTE_READONLY);
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
bSizer->Add(button, 0, wxALL, 5);
bSizer->Add(m_textCtrl, 1, wxALL|wxEXPAND, 5);
panel->SetSizer(bSizer);
Layout();
button->Bind(wxEVT_BUTTON,&MyFrame::OnButtonClick,this);
}
void MyFrame::OnButtonClick(wxCommandEvent& event)
{
if ( !HasCapture() )
{
CaptureMouse();
m_textCtrl->AppendText("Mouse captured.\n");
Bind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
Bind(wxEVT_MOUSE_CAPTURE_LOST, &MyFrame::OnMouseCapLost, this);
}
}
void MyFrame::CleanUp()
{
if ( HasCapture() )
ReleaseMouse();
Unbind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
Unbind(wxEVT_MOUSE_CAPTURE_LOST, &MyFrame::OnMouseCapLost, this);
}
void MyFrame::OnMouseCapLost(wxMouseCaptureLostEvent& event)
{
m_textCtrl->AppendText("Mouse cap lost.\n");
CleanUp();
}
void MyFrame::OnLeftDown(wxMouseEvent& event)
{
m_textCtrl->AppendText("Click recorded.\n");
CleanUp();
}
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
I hope this helps.
edit: Here's a version using event tables as well:
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#define BUTTON_ID 101
class MyFrame : public wxFrame
{
public:
MyFrame(wxWindow* parent);
protected:
void OnButtonClick(wxCommandEvent& event);
void OnMouseCapLost(wxMouseCaptureLostEvent& event);
void OnMouseEvent(wxMouseEvent&);
void CleanUp();
private:
wxTextCtrl* m_textCtrl;
wxDECLARE_EVENT_TABLE();
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(BUTTON_ID, MyFrame::OnButtonClick)
EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent)
EVT_MOUSE_CAPTURE_LOST(MyFrame::OnMouseCapLost)
wxEND_EVENT_TABLE()
MyFrame::MyFrame(wxWindow* parent)
:wxFrame(parent, wxID_ANY, "Demo", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
{
wxPanel* panel = new wxPanel(this, wxID_ANY );
wxButton* button = new wxButton(panel, BUTTON_ID, "Click Me");
m_textCtrl = new wxTextCtrl(panel, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_DONTWRAP|wxTE_MULTILINE|wxTE_READONLY);
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
bSizer->Add(button, 0, wxALL, 5);
bSizer->Add(m_textCtrl, 1, wxALL|wxEXPAND, 5);
panel->SetSizer(bSizer);
Layout();
}
void MyFrame::OnButtonClick(wxCommandEvent& event)
{
if ( !HasCapture() )
{
CaptureMouse();
m_textCtrl->AppendText("Mouse captured.\n");
}
}
void MyFrame::CleanUp()
{
if ( HasCapture() )
ReleaseMouse();
}
void MyFrame::OnMouseCapLost(wxMouseCaptureLostEvent& event)
{
m_textCtrl->AppendText("Mouse cap lost.\n");
CleanUp();
}
void MyFrame::OnMouseEvent(wxMouseEvent& event)
{
if( HasCapture() && event.LeftIsDown() )
{
m_textCtrl->AppendText("Click recorded.\n");
CleanUp();
}
}
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
I need unpack an archive which contains unicode characters for filenames in archives.
My code equivalent this:
#include "wx/wx.h"
#include <wx/archive.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/fs_arc.h>
#include "wx/fs_zip.h"
#include <memory>
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title) :
wxFrame( NULL, -1, title, wxDefaultPosition, wxSize(350,180 ),
wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxMAXIMIZE_BOX))
{
wxBoxSizer* Sizer = new wxBoxSizer( wxVERTICAL );
TextBox = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
Sizer->Add( TextBox, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5 );
auto Button = new wxButton( this, wxID_ANY, wxT("Show entries"), wxDefaultPosition, wxDefaultSize, 0 );
Button->Bind(wxEVT_BUTTON, [this](wxCommandEvent&)
{
auto filename = "test.zip";
auto factory = wxArchiveClassFactory::Find(filename, wxSTREAM_FILEEXT);
if(!factory)
return;
std::unique_ptr<wxArchiveInputStream> inarc(factory->NewStream(new wxFFileInputStream(filename)));
std::unique_ptr<wxArchiveEntry> entry(factory->NewEntry());
while (entry.reset(inarc->GetNextEntry()), entry.get() != NULL)
TextBox->AppendText("Entry : "+entry->GetName()+"\n");
});
Sizer->Add( Button, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
this->SetSizer( Sizer );
this->Layout();
}
private:
wxTextCtrl* TextBox;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE
{
MyFrame *frame = new MyFrame(wxT("Test"));
frame->Show(true);
return true;
};
};
wxIMPLEMENT_APP(MyApp);
Is it possible to get the correct names for these files using wxWidgets?
Edit: I on Windows7 use wxWidgets 3.1.0, compiler - cl( 17.00.50727.1) VS11.
It seems you suffer this bug: http://trac.wxwidgets.org/ticket/17244 which has been fixed after wx3.1 release.
You need to download the current head code (available with git).
I pretend to "grab" and process the events: EVT_TEXT from wxTextCtrl and EVT_BUTTON from wxID_APPLY.
I try handling the events with Event Tables and I can't? Why?
The source code:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Dialog : public wxDialog
{
public:
Dialog(wxWindow *parent, const wxString &title);
private:
void OnNameChange(wxCommandEvent &event);
void OnApply(wxCommandEvent &event);
wxDECLARE_EVENT_TABLE();
wxTextCtrl *m_name;
};
enum
{
ID_NAME = 1
};
Dialog::Dialog(wxWindow *parent, const wxString &title) :
wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
SetSizer(vbox);
wxFlexGridSizer *flexGrid = new wxFlexGridSizer(2, 2, 5, 5);
wxStaticText *label = new wxStaticText(this, wxID_ANY, "&Name:");
flexGrid->Add(label);
m_name = new wxTextCtrl(this, ID_NAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
flexGrid->Add(m_name, 1, wxEXPAND);
label = new wxStaticText(this, wxID_ANY, "&Description:");
flexGrid->Add(label);
m_description = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
flexGrid->Add(m_description, 1, wxEXPAND);
flexGrid->AddGrowableRow(1, 1);
flexGrid->AddGrowableCol(1, 1);
vbox->Add(flexGrid, 1, wxALL | wxEXPAND, 15);
vbox->Add(CreateSeparatedButtonSizer(wxAPPLY | wxCANCEL), 0, wxEXPAND | wxALL, 5);
}
void Dialog::OnNameChange(wxCommandEvent &event)
{
m_description->AppendText("Hello\n");
}
void Dialog::OnApply(wxCommandEvent &event)
{
m_description->AppendText("Apply\n");
}
wxBEGIN_EVENT_TABLE(Dialog, wxDialog)
EVT_TEXT(ID_NAME, Dialog::OnNameChange)
EVT_BUTTON(wxID_APPLY, Dialog::OnApply)
wxEND_EVENT_TABLE()
It's only possible to handle the events dynamically?
Bind(wxEVT_TEXT, &Dialog::OnNameChange, this, m_name->GetId());
The code as shown absolutely should work. It's not self-contained, so I can't test it, but there must be something else not shown here preventing it from working. Please try to make a SSCCE if you still can't isolate the problem yourself.
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();
}
EVT_MENU(BUTTON_COMPUTE,
MainPanel::OnCompute)
EVT_MENU(BUTTON_CLEAR,
MainPanel::OnClear)
In the above statements use EVT_BUTTON instead of EVT_MENU.
I think I see it. In the event table for MainPanel instead of EVT_MENU use EVT_BUTTON.