Entry Dialogs
These dialogs ask you to type in information. They include wxNumberEntryDialog, wxTextEntryDialog, wxPasswordEntryDialog, and wxFindReplaceDialog.
wxNumberEntryDialog
wxNumberEntryDialog prompts the user for an integer within a given range. The dialog shows a spin control so that the number can be entered directly or by clicking on the up and down arrows. This dialog is implemented by wxWidgets, so it has the same functionality on all platforms.Create a wxNumberEntryDialog passing a parent window, message text, prompt text (that will precede the spin control), caption, default value, minimum value, maximum value, and position. Then call ShowDialog and, if wxID_OK is returned, retrieve the number using GetValue.Figure 8-23 shows what the dialog looks like under Windows.
Figure 8-23. wxNumberEntryDialog under Windows

wxNumberEntryDialog Example
Figure 8-23 was created using the following code:
#include "wx/numdlg.h"
wxNumberEntryDialog dialog(parent,
wxT("This is some text, actually a lot of text\nEven two rows of text"),
wxT("Enter a number:"), wxT("Numeric input test"), 50, 0, 100);
if (dialog.ShowModal() == wxID_OK)
{
long value = dialog.GetValue();
}
wxTextEntryDialog and wxPasswordEntryDialog
wxTextEnTRyDialog and wxPasswordEntryDialog present the user with a single-line text control and a message. They function identically except that the letters typed into a wxPasswordEntryDialog are masked so that they cannot be read. Figure 8-24 shows a wxTextEntryDialog under Windows.
Figure 8-24. wxTextEnTRyDialog under Windows

wxTextEntryDialog Example
Figure 8-24 was created using this code:
#include "wx/textdlg.h"
wxTextEntryDialog dialog(this,
wxT("This is a small sample\n")
wxT("A long, long string to test out the text entrybox"),
wxT("Please enter a string"),
wxT("Default value"),
wxOK | wxCANCEL);
if (dialog.ShowModal() == wxID_OK)
wxMessageBox(dialog.GetValue(), wxT("Got string"));
wxFindReplaceDialog
wxFindReplaceDialog is a modeless dialog that allows the user to search for some text and replace it with something else, if desired. The actual searching must be done in a derived class or a parent window, responding to events generated by the dialog's buttons. Unlike most standard dialogs, this one must have a parent window. This dialog cannot be used modally; it is always, by design and implementation, modeless.The Windows Find and Replace dialog is shown in Figure 8-25.
Figure 8-25. wxFindReplaceDialog under Windows

Figure 8-26. wxFindReplaceDialog under GTK+

Handling Events from the Dialog
wxFindReplaceDialog sends command events when the user clicks on controls in the dialog. Event handlers take a wxFindDialogEvent argument, and the event table macros take the dialog identifier and handler function, as listed in Table 8-4.
EVT_FIND(id, func) | Handles Find button clicks. |
EVT_FIND_NEXT(id, func) | Handles Next button clicks. |
EVT_FIND_REPLACE(id, func) | Handles Replace button clicks. |
EVT_FIND_REPLACE_ALL(id, func) | Handles Replace All button clicks. |
EVT_FIND_CLOSE(id, func) | Handles a close event, generated when the user closes the dialog via Cancel or other means. |
wxFindDialogEvent Functions
wxFindDialogEvent has the following functions.GetFlags returns flags for the current selections on the dialog. The value is a bit-list of wxFR_DOWN, wxFR_WHOLEWORD, and wxFR_MATCHCASE.GetFindString returns the string the user entered as the text to find.GetreplaceString returns the string the user entered as the text to use as the replacement.Getdialog returns a pointer to the wxFindReplaceDialog that generated the event.
Passing Data to the Dialog
To create a wxFindReplaceDialog, pass a window parent, a pointer to a wxFindReplaceData object, a dialog caption, and a style, which is a bit-list of values shown in Table 8-5.
wxFR_REPLACEDIALOG | Specifies a find and replace dialog; otherwise, it will be a find dialog. |
wxFR_NOUPDOWN | Specifies that the search direction should not be adjustable. |
wxFR_NOMATCHCASE | Specifies that case-sensitive searching is not allowable. |
wxFR_NOWHOLEWORD | Specifies that whole-word searching is not allowable. |
wxFindReplaceData Functions
These are the functions for setting and accessing data in wxFindReplaceData. Note that the setters may only be called before showing the dialog, and calling them has no effect later.GetFindString and SetFindString are accessors for the search string, provided by the application or entered by the user.GetFlags and SetFlags are accessors for the flags specifying the state of the find dialog (refer to Table 8-5).GetreplaceString and SetReplaceString are accessors for the replace string, provided by the application or entered by the user.
Find and Replace Example
The following shows an example fragment of wxFindReplaceDialog usage, employing hypothetical DoFind and DoReplace functions to do the actual search and replace for the application. These functions would maintain application-dependent variables in the dialog class, storing the last position that was searched, so that each time the functions are called, the next match can be found. The functions will also change the document view and highlight the match.
#include "wx/fdrepdlg.h"
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_REPLACE, MyFrame::ShowReplaceDialog)
EVT_FIND(wxID_ANY, MyFrame::OnFind)
EVT_FIND_NEXT(wxID_ANY, MyFrame::OnFind)
EVT_FIND_REPLACE(wxID_ANY, MyFrame::OnReplace)
EVT_FIND_REPLACE_ALL(wxID_ANY, MyFrame::OnReplaceAll)
EVT_FIND_CLOSE(wxID_ANY, MyFrame::OnFindClose)
END_EVENT_TABLE()
void MyFrame::ShowReplaceDialog( wxCommandEvent& event )
{
if ( m_dlgReplace )
{
delete m_dlgReplace;
m_dlgReplace = NULL;
}
else
{
m_dlgReplace = new wxFindReplaceDialog
(
this,
&m_findData,
wxT("Find and replace dialog"),
wxFR_REPLACEDIALOG
);
m_dlgReplace->Show(true);
}
}
void MyFrame::OnFind(wxFindDialogEvent& event)
{
if (!DoFind(event.GetFindString(), event.GetFlags()))
{
wxMessageBox(wxT("No more matches."));
}
}
void MyFrame::OnReplace(wxFindDialogEvent& event)
{
if (!DoReplace(event.GetFindString(), event.GetReplaceString(),
event.GetFlags(), REPLACE_THIS))
{
wxMessageBox(wxT("No more matches."));
}
}
void MyFrame::OnReplaceAll(wxFindDialogEvent& event)
{
if (DoReplace(event.GetFindString(), event.GetReplaceString(),
event.GetFlags(), REPLACE_ALL))
{
wxMessageBox(wxT("Replacements made."));
}
else
{
wxMessageBox(wxT("No replacements made."));
}
}
void MyFrame::OnFindClose(wxFindDialogEvent& event)
{
m_dlgReplace->Destroy();
m_dlgReplace = NULL;
}
