modalhook.h 3.7 KB

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