Text Editing tutorial:
Part
2
This tutorial develops the simple
wxWidgets text-editing program, and in this stage we add an Edit Menu
Add a standard Edit
menu
- In the Menuzone, click Add menu |
Edit menu
- The wxTextCtrl alrady
has methods to Cut, Copy and Paste text,
and CanCut(), CanCopy and CanPaste() so it should be
starightforward to develop this code following the examples set for
the File menu. If you get stuck, type wxTextCtrl, select it and
press Ctrl+F1; this should give you context sensitve help
- You may like to drag
the Replicas into the toolbar and you will
automatically get bitmap icons for them
Compile and test your
application loads files, does Cut, Copy and
Paste and check the enable works on the menus and toolbars
Add to the Edit Menu items to change Font and Search
- Font: Use the MenuZone
to add a separator to the bottom of the
Edit Menu and then add an Item saying &Font
- Double click this and
use Insert Dialog | wxFont Dialog. In the
comments at the bottom, you need to use textctrl->SetFont
(myFont); [if you are cunning, you can use txtctrl->GetFont ()
to get the current font]
- Search: Use the
MenuZone to add a new item to the end ot the
Edit menu, saying &Search.
- Double click the Search
replica and insert a wxTextEntryDialog
into the body; changing the text in the wxTextEntryDialog
constructor to say something appropriate
- At the line myText
=dialog.GetValue() ; we need to respond to
the input of search text, as follows
textctrl -> Find (myText) ;
However, the wxTextCtrl
does not have methods to Find text within
the window, so we will add them as follows:
- In the menuzone,
Right-click in the middle of the white windowa
nd in the MyTxtCtrl Menu choose Go/Add NonGUI method. This will
allow us to insert declarations and a body framework for the find
method, so type into the dialog box
int Find (const wxString & sSearch)
Into the body that this
creates, type
wxString sText =GetValue() ;
sText.Replace(wxT( "\n" ), wxT( " \n" ) ) ;
int iStart = sText.Find (sSearch);
int iEnd = iStart + sSearch.Length();
SetSelection (iStart, iEnd);
return iStart ;
Note that, under windows, the GetValue() method removes \r from
each line break, so we need to add a space to the end of each line
- hence the Replace line.
- Compile and run
the
project.
- The Find code will find
the first occurence of the search, so
to do a search from current position you need to work on the code
yourself a bit
- In the Dialog Tutorial, we will
build a dialog suitable for doing Search/Replace
Next
Back
wxHatch
Home 