cshelp.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cshelp.h
  3. // Purpose: Context-sensitive help support classes
  4. // Author: Julian Smart, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 08/09/2000
  7. // Copyright: (c) 2000 Julian Smart, Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CSHELP_H_
  11. #define _WX_CSHELP_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_HELP
  14. #include "wx/help.h"
  15. #include "wx/hashmap.h"
  16. #if wxUSE_BMPBUTTON
  17. #include "wx/bmpbuttn.h"
  18. #endif
  19. #include "wx/event.h"
  20. // ----------------------------------------------------------------------------
  21. // classes used to implement context help UI
  22. // ----------------------------------------------------------------------------
  23. /*
  24. * wxContextHelp
  25. * Invokes context-sensitive help. When the user
  26. * clicks on a window, a wxEVT_HELP event will be sent to that
  27. * window for the application to display help for.
  28. */
  29. class WXDLLIMPEXP_CORE wxContextHelp : public wxObject
  30. {
  31. public:
  32. wxContextHelp(wxWindow* win = NULL, bool beginHelp = true);
  33. virtual ~wxContextHelp();
  34. bool BeginContextHelp(wxWindow* win);
  35. bool EndContextHelp();
  36. bool EventLoop();
  37. bool DispatchEvent(wxWindow* win, const wxPoint& pt);
  38. void SetStatus(bool status) { m_status = status; }
  39. protected:
  40. bool m_inHelp;
  41. bool m_status; // true if the user left-clicked
  42. private:
  43. DECLARE_DYNAMIC_CLASS(wxContextHelp)
  44. };
  45. #if wxUSE_BMPBUTTON
  46. /*
  47. * wxContextHelpButton
  48. * You can add this to your dialogs (especially on non-Windows platforms)
  49. * to put the application into context help mode.
  50. */
  51. class WXDLLIMPEXP_CORE wxContextHelpButton : public wxBitmapButton
  52. {
  53. public:
  54. wxContextHelpButton(wxWindow* parent,
  55. wxWindowID id = wxID_CONTEXT_HELP,
  56. const wxPoint& pos = wxDefaultPosition,
  57. const wxSize& size = wxDefaultSize,
  58. long style = wxBU_AUTODRAW);
  59. void OnContextHelp(wxCommandEvent& event);
  60. private:
  61. DECLARE_DYNAMIC_CLASS_NO_COPY(wxContextHelpButton)
  62. DECLARE_EVENT_TABLE()
  63. };
  64. #endif
  65. // ----------------------------------------------------------------------------
  66. // classes used to implement context help support
  67. // ----------------------------------------------------------------------------
  68. // wxHelpProvider is an abstract class used by the program implementing context help to
  69. // show the help text (or whatever: it may be HTML page or anything else) for
  70. // the given window.
  71. //
  72. // The current help provider must be explicitly set by the application using
  73. // wxHelpProvider::Set().
  74. //
  75. // Special note about ShowHelpAtPoint() and ShowHelp(): we want to be able to
  76. // override ShowHelpAtPoint() when we need to use different help messages for
  77. // different parts of the window, but it should also be possible to override
  78. // just ShowHelp() both for backwards compatibility and just because most
  79. // often the help does not, in fact, depend on the position and so
  80. // implementing just ShowHelp() is simpler and more natural, so by default
  81. // ShowHelpAtPoint() forwards to ShowHelp(). But this means that
  82. // wxSimpleHelpProvider has to override ShowHelp() and not ShowHelpAtPoint()
  83. // for backwards compatibility as otherwise the existing code deriving from it
  84. // and overriding ShowHelp() but calling the base class version wouldn't work
  85. // any more, which forces us to use a rather ugly hack and pass the extra
  86. // parameters of ShowHelpAtPoint() to ShowHelp() via member variables.
  87. class WXDLLIMPEXP_CORE wxHelpProvider
  88. {
  89. public:
  90. // get/set the current (application-global) help provider (Set() returns
  91. // the previous one)
  92. static wxHelpProvider *Set(wxHelpProvider *helpProvider)
  93. {
  94. wxHelpProvider *helpProviderOld = ms_helpProvider;
  95. ms_helpProvider = helpProvider;
  96. return helpProviderOld;
  97. }
  98. // unlike some other class, the help provider is not created on demand,
  99. // this must be explicitly done by the application
  100. static wxHelpProvider *Get() { return ms_helpProvider; }
  101. // get the help string (whose interpretation is help provider dependent
  102. // except that empty string always means that no help is associated with
  103. // the window) for this window
  104. virtual wxString GetHelp(const wxWindowBase *window) = 0;
  105. // do show help for the given window (uses window->GetHelpAtPoint()
  106. // internally if applicable), return true if it was done or false
  107. // if no help available for this window
  108. virtual bool ShowHelpAtPoint(wxWindowBase *window,
  109. const wxPoint& pt,
  110. wxHelpEvent::Origin origin)
  111. {
  112. wxCHECK_MSG( window, false, wxT("window must not be NULL") );
  113. m_helptextAtPoint = pt;
  114. m_helptextOrigin = origin;
  115. return ShowHelp(window);
  116. }
  117. // show help for the given window, see ShowHelpAtPoint() above
  118. virtual bool ShowHelp(wxWindowBase * WXUNUSED(window)) { return false; }
  119. // associate the text with the given window or id: although all help
  120. // providers have these functions to allow making wxWindow::SetHelpText()
  121. // work, not all of them implement them
  122. virtual void AddHelp(wxWindowBase *window, const wxString& text);
  123. // this version associates the given text with all window with this id
  124. // (may be used to set the same help string for all [Cancel] buttons in
  125. // the application, for example)
  126. virtual void AddHelp(wxWindowID id, const wxString& text);
  127. // removes the association
  128. virtual void RemoveHelp(wxWindowBase* window);
  129. // virtual dtor for any base class
  130. virtual ~wxHelpProvider();
  131. protected:
  132. wxHelpProvider()
  133. : m_helptextAtPoint(wxDefaultPosition),
  134. m_helptextOrigin(wxHelpEvent::Origin_Unknown)
  135. {
  136. }
  137. // helper method used by ShowHelp(): returns the help string to use by
  138. // using m_helptextAtPoint/m_helptextOrigin if they're set or just GetHelp
  139. // otherwise
  140. wxString GetHelpTextMaybeAtPoint(wxWindowBase *window);
  141. // parameters of the last ShowHelpAtPoint() call, used by ShowHelp()
  142. wxPoint m_helptextAtPoint;
  143. wxHelpEvent::Origin m_helptextOrigin;
  144. private:
  145. static wxHelpProvider *ms_helpProvider;
  146. };
  147. WX_DECLARE_EXPORTED_HASH_MAP( wxUIntPtr, wxString, wxIntegerHash,
  148. wxIntegerEqual, wxSimpleHelpProviderHashMap );
  149. // wxSimpleHelpProvider is an implementation of wxHelpProvider which supports
  150. // only plain text help strings and shows the string associated with the
  151. // control (if any) in a tooltip
  152. class WXDLLIMPEXP_CORE wxSimpleHelpProvider : public wxHelpProvider
  153. {
  154. public:
  155. // implement wxHelpProvider methods
  156. virtual wxString GetHelp(const wxWindowBase *window);
  157. // override ShowHelp() and not ShowHelpAtPoint() as explained above
  158. virtual bool ShowHelp(wxWindowBase *window);
  159. virtual void AddHelp(wxWindowBase *window, const wxString& text);
  160. virtual void AddHelp(wxWindowID id, const wxString& text);
  161. virtual void RemoveHelp(wxWindowBase* window);
  162. protected:
  163. // we use 2 hashes for storing the help strings associated with windows
  164. // and the ids
  165. wxSimpleHelpProviderHashMap m_hashWindows,
  166. m_hashIds;
  167. };
  168. // wxHelpControllerHelpProvider is an implementation of wxHelpProvider which supports
  169. // both context identifiers and plain text help strings. If the help text is an integer,
  170. // it is passed to wxHelpController::DisplayContextPopup. Otherwise, it shows the string
  171. // in a tooltip as per wxSimpleHelpProvider.
  172. class WXDLLIMPEXP_CORE wxHelpControllerHelpProvider : public wxSimpleHelpProvider
  173. {
  174. public:
  175. // Note that it doesn't own the help controller. The help controller
  176. // should be deleted separately.
  177. wxHelpControllerHelpProvider(wxHelpControllerBase* hc = NULL);
  178. // implement wxHelpProvider methods
  179. // again (see above): this should be ShowHelpAtPoint() but we need to
  180. // override ShowHelp() to avoid breaking existing code
  181. virtual bool ShowHelp(wxWindowBase *window);
  182. // Other accessors
  183. void SetHelpController(wxHelpControllerBase* hc) { m_helpController = hc; }
  184. wxHelpControllerBase* GetHelpController() const { return m_helpController; }
  185. protected:
  186. wxHelpControllerBase* m_helpController;
  187. wxDECLARE_NO_COPY_CLASS(wxHelpControllerHelpProvider);
  188. };
  189. // Convenience function for turning context id into wxString
  190. WXDLLIMPEXP_CORE wxString wxContextId(int id);
  191. #endif // wxUSE_HELP
  192. #endif // _WX_CSHELP_H_