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