]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/modalhook.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/modalhook.h
3 // Purpose: Public interface of wxModalDialogHook class.
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 Allows to intercept all modal dialog calls.
12 This class can be used to hook into normal modal dialog handling for some
13 special needs. One of the most common use cases is for testing: as
14 automatic tests can't continue if a modal dialog is shown while they run,
15 this class can be used to avoid showing the modal dialogs during unattended
16 execution. wxModalDialogHook can also be used for disabling some background
17 operation while a modal dialog is shown.
19 To install a modal dialog hook, you need to derive your own class from this
20 one and implement its pure virtual Enter() method. Then simply create an
21 object of your class and call Register() on it to start receiving calls to
22 your overridden Enter() (and possibly Exit()) methods:
24 class MyModalDialogHook : public wxModalDialogHook
27 virtual int Enter(wxDialog* dialog)
29 // Just for demonstration purposes, intercept all uses of
30 // wxFileDialog. Notice that this doesn't provide any real
31 // sandboxing, of course, the program can still read and write
32 // files by not using wxFileDialog to ask the user for their
34 if ( wxDynamicCast(dialog, wxFileDialog) )
36 wxLogError("Access to file system disallowed.");
38 // Skip showing the file dialog entirely.
42 m_lastEnter = wxDateTime::Now();
44 // Allow the dialog to be shown as usual.
48 virtual void Exit(wxDialog* dialog)
50 // Again, just for demonstration purposes, show how long did
51 // the user take to dismiss the dialog. Notice that we
52 // shouldn't use wxLogMessage() here as this would result in
53 // another modal dialog call and hence infinite recursion. In
54 // general, the hooks should be as unintrusive as possible.
55 wxLogDebug("%s dialog took %s to be dismissed",
56 dialog->GetClassInfo()->GetClassName(),
57 (wxDateTime::Now() - m_lastEnter).Format());
61 class MyApp : public wxApp
72 MyModalDialogHook m_myHook;
78 class wxModalDialogHook
82 Default and trivial constructor.
84 The constructor doesn't do anything, call Register() to make this hook
90 Destructor unregisters the hook if it's currently active.
92 virtual ~wxModalDialogHook();
95 Register this hook as being active.
97 After registering the hook, its Enter() and Exit() methods will be
98 called whenever a modal dialog is shown.
100 Notice that the order of registration matters: the last hook registered
101 is called first, and if its Enter() returns a value different from
102 ::wxID_NONE, the subsequent hooks are skipped.
104 It is an error to register the same hook twice.
109 Unregister this hook.
111 Notice that is done automatically from the destructor, so usually
112 calling this method explicitly is unnecessary.
114 The hook must be currently registered.
120 Called by wxWidgets before showing any modal dialogs.
122 Override this to be notified whenever a modal dialog is about to be
125 If the return value of this method is ::wxID_NONE, the dialog is shown
126 as usual and Exit() will be called when it is dismissed. If the return
127 value is anything else, the dialog is not shown at all and its
128 wxDialog::ShowModal() simply returns with the given result. In this
129 case, Exit() won't be called neither.
131 @param dialog The dialog about to be shown, never @NULL.
132 @return wxID_NONE to continue with showing the dialog or anything else
133 to skip showing the dialog and just return this value from its
136 virtual int Enter(wxDialog
* dialog
) = 0;
139 Called by wxWidgets after dismissing the modal dialog.
141 Notice that it won't be called if Enter() hadn't been called because
142 another modal hook, registered after this one, intercepted the dialog
143 or if our Enter() was called but returned a value different from
146 @param dialog The dialog that was shown and dismissed, never @NULL.
148 virtual void Exit(wxDialog
* dialog
);