The Full Implementation Of The TextFrame Class

From WxWiki

Jump to: navigation, search
Declaration
#ifndef _TEXTFRAME_H
#define _TEXTFRAME_H
 
class TextFrame : public wxFrame
{
public:
 
	/** Constructor. Creates a new TextFrame */
	TextFrame(const wxChar *title, int xpos, int ypos, int width, int height);
 
	/** Processes menu File|Open */
	void OnMenuFileOpen(wxCommandEvent &event);
 
	/** Processes menu File|Save */
	void OnMenuFileSave(wxCommandEvent &event);
 
	/** Processes menu File|Quit */
	void OnMenuFileQuit(wxCommandEvent &event);
 
	/** Processes menu About|Info */
	void OnMenuHelpAbout(wxCommandEvent &event);
 
protected:
 
	DECLARE_EVENT_TABLE()
 
private:
 
	wxTextCtrl *m_pTextCtrl;
	wxMenuBar *m_pMenuBar;
	wxMenu *m_pFileMenu;
	wxMenu *m_pHelpMenu;
};
 
#endif _TEXTFRAME_H 
Implementation
// For compilers that supports precompilation , includes "wx/wx.h"
#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))
{
	m_pTextCtrl = new wxTextCtrl(this, -1, _T("Type some text..."),
		wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
 
	CreateStatusBar();
 
	m_pMenuBar = new wxMenuBar();
	// File Menu
	m_pFileMenu = new wxMenu();
	m_pFileMenu->Append(wxID_OPEN, _T("&Open"));
	m_pFileMenu->Append(wxID_SAVE, _T("&Save"));
	m_pFileMenu->AppendSeparator();
	m_pFileMenu->Append(wxID_EXIT, _T("&Quit"));
	m_pMenuBar->Append(m_pFileMenu, _T("&File"));
	// About menu
	m_pHelpMenu = new wxMenu();
	m_pHelpMenu->Append(wxID_ABOUT, _T("&About"));
	m_pMenuBar->Append(m_pHelpMenu, _T("&Help"));
 
	SetMenuBar(m_pMenuBar);
}
 
// If you're doing an application by inheriting from wxApp
// be sure to change wxFrame to wxApp (or whatever component
// you've inherited your class from).
BEGIN_EVENT_TABLE(TextFrame, wxFrame)
	EVT_MENU(wxID_OPEN, TextFrame::OnMenuFileOpen)
	EVT_MENU(wxID_SAVE, TextFrame::OnMenuFileSave)
	EVT_MENU(wxID_EXIT, TextFrame::OnMenuFileQuit)
	EVT_MENU(wxID_ABOUT, TextFrame::OnMenuHelpAbout)
END_EVENT_TABLE()
 
void TextFrame::OnMenuFileOpen(wxCommandEvent &event)
{
	wxFileDialog *OpenDialog= new wxFileDialog(this, _T("Choose a file"), _(""), _(""), _("*.*"), wxOPEN);
	if ( OpenDialog->ShowModal() == wxID_OK )
	{
		m_pTextCtrl->LoadFile(OpenDialog->GetPath()) ?
			SetStatusText(_T("Loaded")) :
			SetStatusText(_T("Load Failed")) ;
	}
	OpenDialog->Close(); // Or OpenDialog->Destroy() ?
}
 
void TextFrame::OnMenuFileSave(wxCommandEvent &event)
{
	wxFileDialog *SaveDialog= new wxFileDialog(this, _T("Choose a file"), _(""), _(""), _("*.*"), wxSAVE);
	if ( SaveDialog->ShowModal() == wxID_OK )
	{
		m_pTextCtrl->SaveFile(SaveDialog->GetPath()) ?
			SetStatusText(_T("Saved")) :
			SetStatusText(_T("Save Failed"));
	}
	SaveDialog->Close();
}
 
void TextFrame::OnMenuFileQuit(wxCommandEvent &event)
{
	Close(false);
}
 
void TextFrame::OnMenuHelpAbout(wxCommandEvent &event)
{
	wxLogMessage(_T("The Simple Text Editor"));
}
Personal tools