]>
Commit | Line | Data |
---|---|---|
fb6261e9 | 1 | ///////////////////////////////////////////////////////////////////////////// |
bd83cb56 | 2 | // Name: src/common/cshelp.cpp |
fb6261e9 | 3 | // Purpose: Context sensitive help class implementation |
bd83cb56 | 4 | // Author: Julian Smart, Vadim Zeitlin |
fb6261e9 JS |
5 | // Modified by: |
6 | // Created: 08/09/2000 | |
7 | // RCS-ID: $Id$ | |
bd83cb56 | 8 | // Copyright: (c) 2000 Julian Smart, Vadim Zeitlin |
ad9fb033 | 9 | // Licence: wxWindows licence |
fb6261e9 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bd83cb56 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
fb6261e9 | 16 | #ifdef __GNUG__ |
bd83cb56 | 17 | #pragma implementation "cshelp.h" |
fb6261e9 JS |
18 | #endif |
19 | ||
bd83cb56 VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
fb6261e9 JS |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
bd83cb56 | 28 | #pragma hdrstop |
fb6261e9 JS |
29 | #endif |
30 | ||
bd83cb56 VZ |
31 | #if wxUSE_HELP |
32 | ||
fb6261e9 | 33 | #ifndef WX_PRECOMP |
fb6261e9 JS |
34 | #endif |
35 | ||
afb02ca5 | 36 | #include "wx/tipwin.h" |
fb6261e9 | 37 | #include "wx/app.h" |
129caadd | 38 | #include "wx/module.h" |
fb6261e9 JS |
39 | #include "wx/cshelp.h" |
40 | ||
bd83cb56 VZ |
41 | // ---------------------------------------------------------------------------- |
42 | // wxContextHelpEvtHandler private class | |
43 | // ---------------------------------------------------------------------------- | |
fb6261e9 | 44 | |
bd83cb56 VZ |
45 | // This class exists in order to eat events until the left mouse button is |
46 | // pressed | |
fb6261e9 JS |
47 | class wxContextHelpEvtHandler: public wxEvtHandler |
48 | { | |
49 | public: | |
50 | wxContextHelpEvtHandler(wxContextHelp* contextHelp) | |
51 | { | |
52 | m_contextHelp = contextHelp; | |
53 | } | |
54 | ||
55 | virtual bool ProcessEvent(wxEvent& event); | |
56 | ||
57 | //// Data | |
58 | wxContextHelp* m_contextHelp; | |
22f3361e VZ |
59 | |
60 | DECLARE_NO_COPY_CLASS(wxContextHelpEvtHandler) | |
fb6261e9 JS |
61 | }; |
62 | ||
bd83cb56 VZ |
63 | // ============================================================================ |
64 | // implementation | |
65 | // ============================================================================ | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // wxContextHelp | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | /* | |
72 | * Invokes context-sensitive help | |
73 | */ | |
74 | ||
75 | ||
fb6261e9 JS |
76 | IMPLEMENT_DYNAMIC_CLASS(wxContextHelp, wxObject) |
77 | ||
78 | wxContextHelp::wxContextHelp(wxWindow* win, bool beginHelp) | |
79 | { | |
80 | m_inHelp = FALSE; | |
81 | ||
82 | if (beginHelp) | |
83 | BeginContextHelp(win); | |
84 | } | |
85 | ||
86 | wxContextHelp::~wxContextHelp() | |
87 | { | |
88 | if (m_inHelp) | |
89 | EndContextHelp(); | |
90 | } | |
91 | ||
449d48f9 JS |
92 | // Not currently needed, but on some systems capture may not work as |
93 | // expected so we'll leave it here for now. | |
94 | #if 0 | |
b8b9d8a7 JS |
95 | static void wxPushOrPopEventHandlers(wxContextHelp* help, wxWindow* win, bool push) |
96 | { | |
97 | if (push) | |
98 | win->PushEventHandler(new wxContextHelpEvtHandler(help)); | |
99 | else | |
100 | win->PopEventHandler(); | |
101 | ||
102 | wxNode* node = win->GetChildren().First(); | |
103 | while (node) | |
104 | { | |
105 | wxWindow* child = (wxWindow*) node->Data(); | |
106 | wxPushOrPopEventHandlers(help, child, push); | |
107 | ||
108 | node = node->Next(); | |
109 | } | |
110 | } | |
449d48f9 | 111 | #endif |
b8b9d8a7 | 112 | |
fb6261e9 JS |
113 | // Begin 'context help mode' |
114 | bool wxContextHelp::BeginContextHelp(wxWindow* win) | |
115 | { | |
116 | if (!win) | |
117 | win = wxTheApp->GetTopWindow(); | |
118 | if (!win) | |
119 | return FALSE; | |
120 | ||
121 | wxCursor cursor(wxCURSOR_QUESTION_ARROW); | |
122 | wxCursor oldCursor = win->GetCursor(); | |
123 | win->SetCursor(cursor); | |
124 | ||
125 | #ifdef __WXMSW__ | |
126 | // wxSetCursor(cursor); | |
127 | #endif | |
128 | ||
b8b9d8a7 JS |
129 | m_status = FALSE; |
130 | ||
449d48f9 JS |
131 | win->PushEventHandler(new wxContextHelpEvtHandler(this)); |
132 | //wxPushOrPopEventHandlers(this, win, TRUE); | |
fb6261e9 JS |
133 | |
134 | win->CaptureMouse(); | |
135 | ||
136 | EventLoop(); | |
137 | ||
138 | win->ReleaseMouse(); | |
139 | ||
449d48f9 JS |
140 | win->PopEventHandler(TRUE); |
141 | //wxPushOrPopEventHandlers(this, win, FALSE); | |
fb6261e9 JS |
142 | |
143 | win->SetCursor(oldCursor); | |
144 | ||
145 | if (m_status) | |
146 | { | |
147 | wxPoint pt; | |
148 | wxWindow* winAtPtr = wxFindWindowAtPointer(pt); | |
ad9fb033 | 149 | /* |
d1c8aaa3 JS |
150 | if (winAtPtr) |
151 | { | |
ad9fb033 | 152 | wxString msg; |
d1c8aaa3 JS |
153 | msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId()); |
154 | cout << msg << '\n'; | |
155 | } | |
ad9fb033 | 156 | */ |
d1c8aaa3 | 157 | |
fb6261e9 JS |
158 | if (winAtPtr) |
159 | DispatchEvent(winAtPtr, pt); | |
160 | } | |
161 | ||
162 | return TRUE; | |
163 | } | |
164 | ||
165 | bool wxContextHelp::EndContextHelp() | |
166 | { | |
167 | m_inHelp = FALSE; | |
168 | ||
169 | return TRUE; | |
170 | } | |
171 | ||
172 | bool wxContextHelp::EventLoop() | |
173 | { | |
174 | m_inHelp = TRUE; | |
dc4211aa | 175 | |
fb6261e9 JS |
176 | while ( m_inHelp ) |
177 | { | |
178 | if (wxTheApp->Pending()) | |
179 | { | |
180 | wxTheApp->Dispatch(); | |
181 | } | |
182 | else | |
183 | { | |
184 | wxTheApp->ProcessIdle(); | |
185 | } | |
186 | } | |
dc4211aa | 187 | |
fb6261e9 JS |
188 | return TRUE; |
189 | } | |
190 | ||
191 | bool wxContextHelpEvtHandler::ProcessEvent(wxEvent& event) | |
192 | { | |
77d4384e | 193 | if (event.GetEventType() == wxEVT_LEFT_DOWN) |
fb6261e9 | 194 | { |
77d4384e RR |
195 | m_contextHelp->SetStatus(TRUE); |
196 | m_contextHelp->EndContextHelp(); | |
197 | return TRUE; | |
fb6261e9 | 198 | } |
33ac7e6f | 199 | |
77d4384e RR |
200 | if ((event.GetEventType() == wxEVT_CHAR) || |
201 | (event.GetEventType() == wxEVT_KEY_DOWN) || | |
202 | (event.GetEventType() == wxEVT_ACTIVATE) || | |
203 | (event.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED)) | |
204 | { | |
b8b9d8a7 JS |
205 | // May have already been set to TRUE by a left-click |
206 | //m_contextHelp->SetStatus(FALSE); | |
77d4384e RR |
207 | m_contextHelp->EndContextHelp(); |
208 | return TRUE; | |
209 | } | |
33ac7e6f | 210 | |
77d4384e RR |
211 | if ((event.GetEventType() == wxEVT_PAINT) || |
212 | (event.GetEventType() == wxEVT_ERASE_BACKGROUND)) | |
213 | { | |
214 | event.Skip(); | |
215 | return FALSE; | |
216 | } | |
33ac7e6f | 217 | |
fb6261e9 JS |
218 | return TRUE; |
219 | } | |
220 | ||
221 | // Dispatch the help event to the relevant window | |
222 | bool wxContextHelp::DispatchEvent(wxWindow* win, const wxPoint& pt) | |
223 | { | |
224 | wxWindow* subjectOfHelp = win; | |
225 | bool eventProcessed = FALSE; | |
226 | while (subjectOfHelp && !eventProcessed) | |
227 | { | |
228 | wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(), pt) ; | |
229 | helpEvent.SetEventObject(this); | |
dc4211aa | 230 | |
fb6261e9 | 231 | eventProcessed = win->GetEventHandler()->ProcessEvent(helpEvent); |
dc4211aa | 232 | |
fb6261e9 JS |
233 | // Go up the window hierarchy until the event is handled (or not). |
234 | // I.e. keep submitting ancestor windows until one is recognised | |
235 | // by the app code that processes the ids and displays help. | |
236 | subjectOfHelp = subjectOfHelp->GetParent(); | |
237 | } | |
238 | return eventProcessed; | |
239 | } | |
240 | ||
bd83cb56 VZ |
241 | // ---------------------------------------------------------------------------- |
242 | // wxContextHelpButton | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
fb6261e9 JS |
245 | /* |
246 | * wxContextHelpButton | |
247 | * You can add this to your dialogs (especially on non-Windows platforms) | |
248 | * to put the application into context help mode. | |
249 | */ | |
250 | ||
251 | #if !defined(__WXMSW__) | |
90350682 | 252 | static const char * csquery_xpm[] = { |
fb6261e9 | 253 | "12 11 2 1", |
57591e0e JS |
254 | " c None", |
255 | ". c #000000", | |
fb6261e9 JS |
256 | " ", |
257 | " .... ", | |
258 | " .. .. ", | |
259 | " .. .. ", | |
260 | " .. ", | |
261 | " .. ", | |
262 | " .. ", | |
263 | " ", | |
264 | " .. ", | |
265 | " .. ", | |
266 | " "}; | |
267 | #endif | |
268 | ||
269 | IMPLEMENT_CLASS(wxContextHelpButton, wxBitmapButton) | |
270 | ||
271 | BEGIN_EVENT_TABLE(wxContextHelpButton, wxBitmapButton) | |
272 | EVT_BUTTON(wxID_CONTEXT_HELP, wxContextHelpButton::OnContextHelp) | |
273 | END_EVENT_TABLE() | |
274 | ||
84bfc0d5 VZ |
275 | wxContextHelpButton::wxContextHelpButton(wxWindow* parent, |
276 | wxWindowID id, | |
277 | const wxPoint& pos, | |
278 | const wxSize& size, | |
279 | long style) | |
dc4211aa DW |
280 | #if defined(__WXPM__) |
281 | : wxBitmapButton(parent, id, wxBitmap(wxCSQUERY_BITMAP | |
282 | ,wxBITMAP_TYPE_RESOURCE | |
283 | ), | |
284 | pos, size, style) | |
285 | #else | |
84bfc0d5 VZ |
286 | : wxBitmapButton(parent, id, wxBITMAP(csquery), |
287 | pos, size, style) | |
dc4211aa | 288 | #endif |
fb6261e9 | 289 | { |
fb6261e9 JS |
290 | } |
291 | ||
33ac7e6f | 292 | void wxContextHelpButton::OnContextHelp(wxCommandEvent& WXUNUSED(event)) |
fb6261e9 | 293 | { |
57591e0e | 294 | wxContextHelp contextHelp(GetParent()); |
fb6261e9 JS |
295 | } |
296 | ||
bd83cb56 VZ |
297 | // ---------------------------------------------------------------------------- |
298 | // wxHelpProvider | |
299 | // ---------------------------------------------------------------------------- | |
300 | ||
301 | wxHelpProvider *wxHelpProvider::ms_helpProvider = (wxHelpProvider *)NULL; | |
302 | ||
303 | // trivial implementation of some methods which we don't want to make pure | |
304 | // virtual for convenience | |
305 | ||
306 | void wxHelpProvider::AddHelp(wxWindowBase * WXUNUSED(window), | |
307 | const wxString& WXUNUSED(text)) | |
308 | { | |
309 | } | |
310 | ||
311 | void wxHelpProvider::AddHelp(wxWindowID WXUNUSED(id), | |
312 | const wxString& WXUNUSED(text)) | |
313 | { | |
314 | } | |
315 | ||
53e112a0 JS |
316 | // removes the association |
317 | void wxHelpProvider::RemoveHelp(wxWindowBase* WXUNUSED(window)) | |
318 | { | |
319 | } | |
320 | ||
bd83cb56 VZ |
321 | wxHelpProvider::~wxHelpProvider() |
322 | { | |
323 | } | |
324 | ||
325 | // ---------------------------------------------------------------------------- | |
326 | // wxSimpleHelpProvider | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
329 | wxString wxSimpleHelpProvider::GetHelp(const wxWindowBase *window) | |
330 | { | |
331 | bool wasFound; | |
332 | wxString text = m_hashWindows.Get((long)window, &wasFound); | |
333 | if ( !wasFound ) | |
334 | text = m_hashIds.Get(window->GetId()); | |
335 | ||
336 | return text; | |
337 | } | |
338 | ||
339 | void wxSimpleHelpProvider::AddHelp(wxWindowBase *window, const wxString& text) | |
340 | { | |
5a07e974 | 341 | m_hashWindows.Delete((long)window); |
bd83cb56 VZ |
342 | m_hashWindows.Put((long)window, text); |
343 | } | |
344 | ||
345 | void wxSimpleHelpProvider::AddHelp(wxWindowID id, const wxString& text) | |
346 | { | |
5a07e974 | 347 | m_hashIds.Delete((long)id); |
bd83cb56 VZ |
348 | m_hashIds.Put(id, text); |
349 | } | |
350 | ||
53e112a0 JS |
351 | // removes the association |
352 | void wxSimpleHelpProvider::RemoveHelp(wxWindowBase* window) | |
353 | { | |
354 | m_hashWindows.Delete((long)window); | |
355 | } | |
356 | ||
bd83cb56 VZ |
357 | bool wxSimpleHelpProvider::ShowHelp(wxWindowBase *window) |
358 | { | |
f38bcae5 | 359 | #if wxUSE_TIPWINDOW |
173e8bbf JS |
360 | static wxTipWindow* s_tipWindow = NULL; |
361 | ||
362 | if (s_tipWindow) | |
363 | { | |
364 | // Prevent s_tipWindow being nulled in OnIdle, | |
365 | // thereby removing the chance for the window to be closed by ShowHelp | |
366 | s_tipWindow->SetTipWindowPtr(NULL); | |
367 | s_tipWindow->Close(); | |
368 | } | |
369 | s_tipWindow = NULL; | |
370 | ||
bd83cb56 VZ |
371 | wxString text = GetHelp(window); |
372 | if ( !text.empty() ) | |
373 | { | |
173e8bbf | 374 | s_tipWindow = new wxTipWindow((wxWindow *)window, text, 100, & s_tipWindow); |
bd83cb56 VZ |
375 | |
376 | return TRUE; | |
377 | } | |
f38bcae5 | 378 | #endif // wxUSE_TIPWINDOW |
bd83cb56 VZ |
379 | |
380 | return FALSE; | |
381 | } | |
382 | ||
5100cabf JS |
383 | // ---------------------------------------------------------------------------- |
384 | // wxHelpControllerHelpProvider | |
385 | // ---------------------------------------------------------------------------- | |
386 | ||
387 | wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase* hc) | |
388 | { | |
389 | m_helpController = hc; | |
390 | } | |
391 | ||
392 | bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase *window) | |
393 | { | |
394 | wxString text = GetHelp(window); | |
395 | if ( !text.empty() ) | |
396 | { | |
397 | if (m_helpController) | |
398 | { | |
399 | if (text.IsNumber()) | |
400 | return m_helpController->DisplayContextPopup(wxAtoi(text)); | |
401 | ||
402 | // If the help controller is capable of popping up the text... | |
403 | else if (m_helpController->DisplayTextPopup(text, wxGetMousePosition())) | |
404 | { | |
405 | return TRUE; | |
406 | } | |
407 | else | |
408 | // ...else use the default method. | |
409 | return wxSimpleHelpProvider::ShowHelp(window); | |
410 | } | |
411 | else | |
412 | return wxSimpleHelpProvider::ShowHelp(window); | |
413 | ||
414 | } | |
415 | ||
416 | return FALSE; | |
417 | } | |
418 | ||
419 | // Convenience function for turning context id into wxString | |
420 | wxString wxContextId(int id) | |
421 | { | |
422 | return wxString(IntToString(id)); | |
423 | } | |
424 | ||
129caadd JS |
425 | // ---------------------------------------------------------------------------- |
426 | // wxHelpProviderModule: module responsible for cleaning up help provider. | |
427 | // ---------------------------------------------------------------------------- | |
428 | ||
429 | class wxHelpProviderModule : public wxModule | |
430 | { | |
431 | public: | |
432 | bool OnInit(); | |
433 | void OnExit(); | |
434 | ||
435 | private: | |
436 | DECLARE_DYNAMIC_CLASS(wxHelpProviderModule) | |
437 | }; | |
438 | ||
439 | IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule, wxModule) | |
440 | ||
441 | bool wxHelpProviderModule::OnInit() | |
442 | { | |
443 | // Probably we don't want to do anything by default, | |
444 | // since it could pull in extra code | |
445 | // wxHelpProvider::Set(new wxSimpleHelpProvider); | |
446 | ||
447 | return TRUE; | |
448 | } | |
449 | ||
450 | void wxHelpProviderModule::OnExit() | |
451 | { | |
452 | if (wxHelpProvider::Get()) | |
453 | { | |
454 | delete wxHelpProvider::Get(); | |
455 | wxHelpProvider::Set(NULL); | |
456 | } | |
457 | } | |
458 | ||
fb6261e9 | 459 | #endif // wxUSE_HELP |