Text Editing tutorial:
Part
1
This tutorial makes a simple
wxWidgets text-editing program, and shows how to insert menus and
dialogs.
Getting Going
- Run wxhatch and choose the
compiler you have (Choose | Select external
Compiler)
- Choose File | New project
and select the Text type. Use the combo box to give the project a
name, eg txt1. By default the projects will be put in the project
directory under wxhatch.
- A skeleton code is produced which
contains a Frame and a multiline TextCtrl
- This can be compiled (Run | Make)
and then executed (Run|Run)
Add menu Items
- The Menuzone is probably showing
(if not use File | View menu or Ctrl-W). Use Add Menu | File
menu
- Double Click the MenuZone replica
of Exit and you should be taken to the method body. Type the
response to the menu, in this case Close(); is appropriate
void MyFrame::OnFileQuit (wxCommandEvent
&
WXUNUSED(event))
{
//Respond to menu here
Close() ;
}
- The TextCtrl alrady has
method to Clear, Load and Save files,
so this is straightforward. In the menuzone : double Click the
New replica and type some suitable code:
textctrl -> Clear();
- To save typing,
highlight the word textctrl , press F2 to add it to
the autotext and give it a good abbreviation e.g. tc To reuse
this autotext, type tc and press ALT+F2
- Double Click the Open
replica and Choose Insert Dialog |
wxOpenFile Dialog. In the code uncomment the line which says
textctrl -> LoadFile( myFilename ); Move the line wxString
myFilename ; to the header file (hint press Ctrl + h), placing it
in the MyFrame declaration.
- Double Click the Save
As replica and Choose Insert Dialog |
wxSaveFile Dialog. In the code uncomment the line which says
OnFileSave (event); and remove the WXUNUSED from the function
header
- Double Click the Save
replica remove the WXUNUSED from the
function header, and type
if ( myFilename.IsEmpty() ) OnFileSave__As (event); else textctrl
->SaveFile (myFilename) ;
Remember to practice the Autotext entry tc !!
- We want to load files
only after saving, when we have
unmodiifed text. Right click the New replica, and choose Go Enable.
Type some code like this :
bool b = ! textctrl -> IsModified() ;
event.Enable(b);
Repeat this for the Open
replica, but this time use code
OnEnableFileNew(event); - We need to prevent the
file from closing when unsaved. Here's
how: Right click the grey bar at the top of the menu zone and
choose Go Frame Close from the popup menu. Add code to deal with
the Saved situation:
if (!textctrl -> IsModified ())
{
event.Skip();
return ;
}
If the textctrl is
modified we need to ask if the user wants to
save it: Use Insert dialog | Message box Question and uncomment the
event.Veto(); and event.Skip(); lines; these determine if the
wxCloseEvent will be carried out
- Compile and test your
application loads files and check the
enable works on both menus
Next
wxHatch
Home 