1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/modalhook.h
3 // Purpose: Allows to hook into showing modal dialogs.
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_MODALHOOK_H_
11 #define _WX_MODALHOOK_H_
13 #include "wx/vector.h"
15 class WXDLLIMPEXP_FWD_CORE wxDialog
;
17 // ----------------------------------------------------------------------------
18 // Class allowing to be notified about any modal dialog calls.
19 // ----------------------------------------------------------------------------
21 // To be notified about entering and exiting modal dialogs and possibly to
22 // replace them with something else (e.g. just return a predefined value for
23 // testing), define an object of this class, override its Enter() and
24 // possibly Exit() methods and call Register() on it.
25 class WXDLLIMPEXP_CORE wxModalDialogHook
28 // Default ctor doesn't do anything, call Register() to activate the hook.
29 wxModalDialogHook() { }
31 // Dtor unregisters the hook if it had been registered.
32 virtual ~wxModalDialogHook() { DoUnregister(); }
34 // Register this hook as being active, i.e. its Enter() and Exit() methods
37 // Notice that the order of registration matters: the last hook registered
38 // is called first, and if its Enter() returns something != wxID_NONE, the
39 // subsequent hooks are skipped.
42 // Unregister this hook. Notice that is done automatically from the dtor.
45 // Called from wxWidgets code before showing any modal dialogs and calls
46 // Enter() for every registered hook.
47 static int CallEnter(wxDialog
* dialog
);
49 // Called from wxWidgets code after dismissing the dialog and calls Exit()
50 // for every registered hook.
51 static void CallExit(wxDialog
* dialog
);
54 // Called by wxWidgets before showing any modal dialogs, override this to
55 // be notified about this and return anything but wxID_NONE to skip showing
56 // the modal dialog entirely and just return the specified result.
57 virtual int Enter(wxDialog
* dialog
) = 0;
59 // Called by wxWidgets after dismissing the modal dialog. Notice that it
60 // won't be called if Enter() hadn't been.
61 virtual void Exit(wxDialog
* WXUNUSED(dialog
)) { }
64 // Unregister the given hook, return true if it was done or false if the
68 // All the hooks in reverse registration order (i.e. in call order).
69 typedef wxVector
<wxModalDialogHook
*> Hooks
;
70 static Hooks ms_hooks
;
72 wxDECLARE_NO_COPY_CLASS(wxModalDialogHook
);
75 // Helper object used by WX_MODAL_DIALOG_HOOK below to ensure that CallExit()
76 // is called on scope exit.
77 class wxModalDialogHookExitGuard
80 wxEXPLICIT
wxModalDialogHookExitGuard(wxDialog
* dialog
)
85 ~wxModalDialogHookExitGuard()
87 wxModalDialogHook::CallExit(m_dialog
);
91 wxDialog
* const m_dialog
;
93 wxDECLARE_NO_COPY_CLASS(wxModalDialogHookExitGuard
);
96 // This macro needs to be used at the top of every implementation of
97 // ShowModal() in order for wxModalDialogHook to work.
98 #define WX_HOOK_MODAL_DIALOG() \
99 const int modalDialogHookRC = wxModalDialogHook::CallEnter(this); \
100 if ( modalDialogHookRC != wxID_NONE ) \
101 return modalDialogHookRC; \
102 wxModalDialogHookExitGuard modalDialogHookExit(this)
104 #endif // _WX_MODALHOOK_H_