Adding A Control
Step 1 - The TextFrame Definition
Now that the frame window is created, you need to add a control for processing text. wxTextCtrl is the class you need. The frame class contains this control.
This code shows the new definition of the TextFrame class. The only thing that's changed is the addition of a member of the type wxTextCtrl. This member is initialized in the constructor.
#ifndef _TEXTFRAME_H
#define _TEXTFRAME_H
class TextFrame : public wxFrame
{
public:
/**
* Constructor. Creates a new TextFrame
*/
TextFrame(const wxString& title, int xpos, int ypos, int width, int height);
/**
* Destructor
*/
~TextFrame();
private:
wxTextCtrl *m_pTextCtrl;
};
#endif // _TEXTFRAME_H
Step 2 - The TextFrame Implementation
The following code shows the new constructor of the TextFrame class. The parent of the wxTextCtrl member is the TextFrame, so we pass it the 'this' pointer. The text 'Type some text...' will be shown as the default text. Notice that the wxTextCtrl's constructor looks similar to the wxFrame's constructor. Each class derived from wxWindow follows the same constructor pattern.
Again, there's no code for deleting the pointer of wxTextCtrl. It's not necessary (actually, it's not allowed), because TextFrame is the parent that will automatically delete all its children when it is destroyed.
wxTE_MULTILINE is a specific window-style for a text control. This style indicates that wxTextCtrl allows multiple lines.
// For compilers that supports precompilation , include "wx/wx.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include "TextFrame.h"
TextFrame::TextFrame(const wxString& 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, wxT("Type some text..."),
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
// More will be added here later
Layout();
}
TextFrame::~TextFrame()
{
}
When you build this project, you'll have an application that shows a window where you can enter some text. Try cut, paste, and you'll see that this small piece of code does a lot for you.
The call to Layout() is there because windows doesn't always send a size-event at the right moment for wxWidgets to catch in an OnSize handler (where it calls Layout() itself).
As per previous example to compile in linux:
g++ TextEditorApp.cpp TextFrame.cpp `wx-config --libs` `wx-config --cxxflags` -o TextEditorApp
next: Adding A Menubar