Got wxHelpContext working, plus wxFindWindowAtPointer, wxGetMousePosition,
[wxWidgets.git] / src / common / helpbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpbase.cpp
3 // Purpose: Help system base classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "helpbase.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #include "wx/helpbase.h"
28 #include "wx/app.h"
29
30 #if wxUSE_HELP
31
32 IMPLEMENT_CLASS(wxHelpControllerBase, wxObject)
33
34 /*
35 * Invokes context-sensitive help
36 */
37
38 // This class exists in order to eat events until the left mouse
39 // button is pressed
40 class wxContextHelpEvtHandler: public wxEvtHandler
41 {
42 public:
43 wxContextHelpEvtHandler(wxContextHelp* contextHelp)
44 {
45 m_contextHelp = contextHelp;
46 }
47
48 virtual bool ProcessEvent(wxEvent& event);
49
50 //// Data
51 wxContextHelp* m_contextHelp;
52 };
53
54 IMPLEMENT_DYNAMIC_CLASS(wxContextHelp, wxObject)
55
56 wxContextHelp::wxContextHelp(wxWindow* win, bool beginHelp)
57 {
58 m_inHelp = FALSE;
59
60 if (beginHelp)
61 BeginContextHelp(win);
62 }
63
64 wxContextHelp::~wxContextHelp()
65 {
66 if (m_inHelp)
67 EndContextHelp();
68 }
69
70 // Begin 'context help mode'
71 bool wxContextHelp::BeginContextHelp(wxWindow* win)
72 {
73 if (!win)
74 win = wxTheApp->GetTopWindow();
75 if (!win)
76 return FALSE;
77
78 wxCursor cursor(wxCURSOR_QUESTION_ARROW);
79 wxCursor oldCursor = win->GetCursor();
80 win->SetCursor(cursor);
81
82 #ifdef __WXMSW__
83 // wxSetCursor(cursor);
84 #endif
85
86 win->PushEventHandler(new wxContextHelpEvtHandler(this));
87
88 win->CaptureMouse();
89
90 EventLoop();
91
92 win->ReleaseMouse();
93
94 win->PopEventHandler(TRUE);
95
96 win->SetCursor(oldCursor);
97
98 if (m_status)
99 {
100 wxPoint pt;
101 wxWindow* winAtPtr = wxFindWindowAtPointer(pt);
102 if (winAtPtr)
103 DispatchEvent(winAtPtr, pt);
104 }
105
106 return TRUE;
107 }
108
109 bool wxContextHelp::EndContextHelp()
110 {
111 m_inHelp = FALSE;
112
113 return TRUE;
114 }
115
116 bool wxContextHelp::EventLoop()
117 {
118 m_inHelp = TRUE;
119 while ( m_inHelp )
120 {
121 if (wxTheApp->Pending())
122 {
123 wxTheApp->Dispatch();
124 }
125 else
126 {
127 wxTheApp->ProcessIdle();
128 }
129 }
130 return TRUE;
131 }
132
133 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent& event)
134 {
135 switch (event.GetEventType())
136 {
137 case wxEVT_LEFT_DOWN:
138 {
139 //wxMouseEvent& mouseEvent = (wxMouseEvent&) event;
140 m_contextHelp->SetStatus(TRUE);
141 m_contextHelp->EndContextHelp();
142 return TRUE;
143 break;
144 }
145 case wxEVT_CHAR:
146 case wxEVT_KEY_DOWN:
147 case wxEVT_ACTIVATE:
148 case wxEVT_MOUSE_CAPTURE_CHANGED:
149 {
150 m_contextHelp->SetStatus(FALSE);
151 m_contextHelp->EndContextHelp();
152 return TRUE;
153 break;
154 }
155 case wxEVT_PAINT:
156 case wxEVT_ERASE_BACKGROUND:
157 {
158 event.Skip();
159 return FALSE;
160 break;
161 }
162 }
163
164 return TRUE;
165 }
166
167 // Dispatch the help event to the relevant window
168 bool wxContextHelp::DispatchEvent(wxWindow* win, const wxPoint& pt)
169 {
170 wxWindow* subjectOfHelp = win;
171 bool eventProcessed = FALSE;
172 while (subjectOfHelp && !eventProcessed)
173 {
174 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(), pt) ;
175 helpEvent.SetEventObject(this);
176 eventProcessed = win->GetEventHandler()->ProcessEvent(helpEvent);
177
178 // Go up the window hierarchy until the event is handled (or not).
179 // I.e. keep submitting ancestor windows until one is recognised
180 // by the app code that processes the ids and displays help.
181 subjectOfHelp = subjectOfHelp->GetParent();
182 }
183 return eventProcessed;
184 }
185
186 #endif // wxUSE_HELP