|
These dialogs ask you to type in information. They include wxNumberEntryDialog, wxTextEntryDialog, wxPasswordEntryDialog, and wxFindReplaceDialog. wxNumberEntryDialogwxNumberEntryDialog 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 WindowswxNumberEntryDialog ExampleFigure 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 wxPasswordEntryDialogwxTextEnTRyDialog 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 WindowsPass a parent window, message, caption, default value, and style to the constructor. The style can be a bit-list of wxOK, wxCANCEL, and wxCENTRE (or wxCENTER), and you can also pass wxTextCtrl styles such as wxTE_CENTRE (or wxTE_CENTER). You can set the default string separately with SetValue, and GetValue returns the text entered by the user. wxTextEntryDialog ExampleFigure 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")); wxFindReplaceDialogwxFindReplaceDialog 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 WindowsOn other platforms, such as GTK+ and Mac OS X, wxWidgets uses the generic version of the dialog, as shown in Figure 8-26. Figure 8-26. wxFindReplaceDialog under GTK+Handling Events from the DialogwxFindReplaceDialog 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.
wxFindDialogEvent FunctionswxFindDialogEvent 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 DialogTo 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.
wxFindReplaceData holds the data for wxFindReplaceDialog. It is used to initialize the dialog with the default values and will keep the last values from the dialog when it is closed. It is also updated each time a wxFindDialogEvent is generated, so instead of using the wxFindDialogEvent methods, you can also directly query this object. Use the dialog's GeTData function to return a pointer to the data you passed to the dialog constructor. wxFindReplaceData FunctionsThese 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 ExampleThe 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; } |
|