modalhook.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /////////////////////////////////////////////////////////////////////////////
  8. /**
  9. Allows to intercept all modal dialog calls.
  10. This class can be used to hook into normal modal dialog handling for some
  11. special needs. One of the most common use cases is for testing: as
  12. automatic tests can't continue if a modal dialog is shown while they run,
  13. this class can be used to avoid showing the modal dialogs during unattended
  14. execution. wxModalDialogHook can also be used for disabling some background
  15. operation while a modal dialog is shown.
  16. To install a modal dialog hook, you need to derive your own class from this
  17. one and implement its pure virtual Enter() method. Then simply create an
  18. object of your class and call Register() on it to start receiving calls to
  19. your overridden Enter() (and possibly Exit()) methods:
  20. @code
  21. class MyModalDialogHook : public wxModalDialogHook
  22. {
  23. protected:
  24. virtual int Enter(wxDialog* dialog)
  25. {
  26. // Just for demonstration purposes, intercept all uses of
  27. // wxFileDialog. Notice that this doesn't provide any real
  28. // sandboxing, of course, the program can still read and write
  29. // files by not using wxFileDialog to ask the user for their
  30. // names.
  31. if ( wxDynamicCast(dialog, wxFileDialog) )
  32. {
  33. wxLogError("Access to file system disallowed.");
  34. // Skip showing the file dialog entirely.
  35. return wxID_CANCEL;
  36. }
  37. m_lastEnter = wxDateTime::Now();
  38. // Allow the dialog to be shown as usual.
  39. return wxID_NONE;
  40. }
  41. virtual void Exit(wxDialog* dialog)
  42. {
  43. // Again, just for demonstration purposes, show how long did
  44. // the user take to dismiss the dialog. Notice that we
  45. // shouldn't use wxLogMessage() here as this would result in
  46. // another modal dialog call and hence infinite recursion. In
  47. // general, the hooks should be as unintrusive as possible.
  48. wxLogDebug("%s dialog took %s to be dismissed",
  49. dialog->GetClassInfo()->GetClassName(),
  50. (wxDateTime::Now() - m_lastEnter).Format());
  51. }
  52. };
  53. class MyApp : public wxApp
  54. {
  55. public:
  56. virtual bool OnInit()
  57. {
  58. ...
  59. m_myHook.Register();
  60. ...
  61. }
  62. private:
  63. MyModalDialogHook m_myHook;
  64. };
  65. @endcode
  66. @since 2.9.5
  67. */
  68. class wxModalDialogHook
  69. {
  70. public:
  71. /**
  72. Default and trivial constructor.
  73. The constructor doesn't do anything, call Register() to make this hook
  74. active.
  75. */
  76. wxModalDialogHook();
  77. /**
  78. Destructor unregisters the hook if it's currently active.
  79. */
  80. virtual ~wxModalDialogHook();
  81. /**
  82. Register this hook as being active.
  83. After registering the hook, its Enter() and Exit() methods will be
  84. called whenever a modal dialog is shown.
  85. Notice that the order of registration matters: the last hook registered
  86. is called first, and if its Enter() returns a value different from
  87. ::wxID_NONE, the subsequent hooks are skipped.
  88. It is an error to register the same hook twice.
  89. */
  90. void Register();
  91. /**
  92. Unregister this hook.
  93. Notice that is done automatically from the destructor, so usually
  94. calling this method explicitly is unnecessary.
  95. The hook must be currently registered.
  96. */
  97. void Unregister();
  98. protected:
  99. /**
  100. Called by wxWidgets before showing any modal dialogs.
  101. Override this to be notified whenever a modal dialog is about to be
  102. shown.
  103. If the return value of this method is ::wxID_NONE, the dialog is shown
  104. as usual and Exit() will be called when it is dismissed. If the return
  105. value is anything else, the dialog is not shown at all and its
  106. wxDialog::ShowModal() simply returns with the given result. In this
  107. case, Exit() won't be called neither.
  108. @param dialog The dialog about to be shown, never @NULL.
  109. @return wxID_NONE to continue with showing the dialog or anything else
  110. to skip showing the dialog and just return this value from its
  111. ShowModal().
  112. */
  113. virtual int Enter(wxDialog* dialog) = 0;
  114. /**
  115. Called by wxWidgets after dismissing the modal dialog.
  116. Notice that it won't be called if Enter() hadn't been called because
  117. another modal hook, registered after this one, intercepted the dialog
  118. or if our Enter() was called but returned a value different from
  119. ::wxID_NONE.
  120. @param dialog The dialog that was shown and dismissed, never @NULL.
  121. */
  122. virtual void Exit(wxDialog* dialog);
  123. };