[Dev-C++] wxwindows: Problem with inheritance
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Glen V. <gve...@pa...> - 2003-06-03 13:03:41
|
First of all: I'm quite a newbie at OO programming in C++ ;)
Now for the problem:
1. Since yesterday I'm following the wxTutorial.pdf (from the wxwindows
site) and am trying out the stuff I read there.
I copied the example on how to only let numeric chars get through to the
textctrl (deriving wxTextCtrl to NumTextCtrl) and it worked.
Later on I saw the example on using the function "bool
wxTextCtrl::IsModified()" (for learning to "use wxCloseEvent::Veto()").
It works when using the class wxTextCtrl, but not with class NumTextCtrl
where it alway returns a false. I thought that functions supported by
the parent automatically are available to the child (using
NumTextCtrl::OnChar() at least verifies this, so the problem prolly is
somewhere else)?
Overriding the function to NumTextCtrl::IsModified(){ return true; } works.
2. I also had a problem including NumTextCtrl.h. It needed to be
included in TextFrame.h and not in TextFrame.cpp (where all the other
wx-stuff is included)
Is there a logic behind this or did I somehow broke something?
=================================================================
NumTextCtrl.h:
#ifndef _NUMTEXTCTRL_H
#define _NUMTEXTCTRL_H
class NumTextCtrl: public wxTextCtrl{
public:
//variables:
//Functions:
NumTextCtrl(wxWindow *parent);
void OnChar(wxKeyEvent &event);
//bool IsModified();
protected:
DECLARE_EVENT_TABLE()
};
#endif
================================================================
NumTextCtrl.cpp:
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "NumTextCtrl.h"
NumTextCtrl::NumTextCtrl(wxWindow *parent): wxTextCtrl(parent,-1){}
void NumTextCtrl::OnChar(wxKeyEvent &event){
if (isdigit(event.GetKeyCode())){
event.Skip();
}
else{
wxBell();
}
}
/*
bool IsModified(){
return true; //This works
}
*/
BEGIN_EVENT_TABLE(NumTextCtrl, wxTextCtrl)
EVT_CHAR(NumTextCtrl::OnChar)
END_EVENT_TABLE()
=========================================================================
TextFrame.h (main frame of the app)
#include "NumTextCtrl.h"
#ifndef _TEXTFRAME_H
#define _TEXTRAME_H
class TextFrame: public wxFrame{
public:
//constructor
TextFrame(const wxChar *title, int xpos, int ypos, int
width, int height);
//Destructor
~TextFrame();
void OnMenuFileOpen(wxCommandEvent &event);
void OnMenuFileSave(wxCommandEvent &event);
void OnMenuFileQuit(wxCloseEvent &event);
void OnMenuInfoAbout(wxCommandEvent &event);
protected:
DECLARE_EVENT_TABLE()
private:
NumTextCtrl *m_pNumTextCtrl;
wxMenuBar *m_pMenuBar;
wxMenu *m_pFileMenu;
wxMenu *m_pInfoMenu;
enum{
MENU_FILE_OPEN,
MENU_FILE_SAVE,
MENU_FILE_QUIT,
MENU_INFO_ABOUT
};
};
#endif
===========================================================================
TextFrame.cpp:
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "TextFrame.h"
TextFrame::TextFrame(const wxChar *title, int xpos, int ypos, int width,
int height)
: wxFrame((wxFrame *) NULL, -1, title, wxPoint(xpos, ypos),
wxSize(width, height)){
//Constructor immediately redirects parameters to parents'
constructor
m_pNumTextCtrl = new NumTextCtrl(this);
m_pMenuBar = new wxMenuBar();
m_pFileMenu = new wxMenu();
m_pFileMenu->Append(MENU_FILE_OPEN, "&Open", "Opent een file");
m_pFileMenu->Append(MENU_FILE_SAVE, "&Save", "Bewaart huidige file");
m_pFileMenu->Append(wxID_CLOSE, "&Quit", "Geefetop");
m_pMenuBar->Append(m_pFileMenu, "&File");
m_pInfoMenu = new wxMenu();
m_pInfoMenu->Append(MENU_INFO_ABOUT, "&Help", "Toont help-menu");
m_pMenuBar->Append(m_pInfoMenu, "&About");
this->SetMenuBar(m_pMenuBar); //"this->" is optional, but as a
newb I prefer
this->CreateStatusBar(3); //clear distinction between methods
and functions
this->SetStatusText("Wippieeee", 1);
}
TextFrame::~TextFrame(){}
BEGIN_EVENT_TABLE(TextFrame, wxFrame)
EVT_MENU(MENU_FILE_OPEN, TextFrame::OnMenuFileOpen)
EVT_MENU(MENU_FILE_SAVE, TextFrame::OnMenuFileSave)
EVT_MENU(wxID_CLOSE, TextFrame::OnMenuFileQuit)
EVT_MENU(MENU_INFO_ABOUT, TextFrame::OnMenuInfoAbout)
END_EVENT_TABLE()
void TextFrame::OnMenuFileOpen(wxCommandEvent &event){
wxLogMessage("File Open Menu Selected");
}
void TextFrame::OnMenuFileSave(wxCommandEvent &event){
wxLogMessage("File Save Menu Selected");
}
void TextFrame::OnMenuFileQuit(wxCloseEvent &event){
bool destroy = true;
if(event.CanVeto()){
if(m_pNumTextCtrl->IsModified()){
wxMessageDialog *dlg = new wxMessageDialog(this,
"Text is changed!\nAre you sure to
exit?",
"Text Changed!!!", wxYES_NO |
wxNO_DEFAULT);
int result = dlg->ShowModal();
if (result == wxID_NO){
event.Veto();
destroy = false;
}
}
}
if(destroy){
Destroy();
}
}
void TextFrame::OnMenuInfoAbout(wxCommandEvent &event){
wxLogMessage("Help About Menu Selected");
}
|