Entry Dialogs

team bbl


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


Pass 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 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


On 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 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.

Table 8-4. wxFindReplaceDialog Events

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.

Table 8-5. wxFindReplaceData Style

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 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 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; } 

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net