1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cshelp.cpp
3 // Purpose: Context sensitive help class implementation
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) 2000 Julian Smart, Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/module.h"
34 #include "wx/tipwin.h"
35 #include "wx/cshelp.h"
37 #if wxUSE_MS_HTML_HELP
38 #include "wx/msw/helpchm.h" // for ShowContextHelpPopup
39 #include "wx/utils.h" // for wxGetMousePosition()
42 // ----------------------------------------------------------------------------
43 // wxContextHelpEvtHandler private class
44 // ----------------------------------------------------------------------------
46 // This class exists in order to eat events until the left mouse button is
48 class wxContextHelpEvtHandler
: public wxEvtHandler
51 wxContextHelpEvtHandler(wxContextHelp
* contextHelp
)
53 m_contextHelp
= contextHelp
;
56 virtual bool ProcessEvent(wxEvent
& event
);
59 wxContextHelp
* m_contextHelp
;
61 DECLARE_NO_COPY_CLASS(wxContextHelpEvtHandler
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
73 * Invokes context-sensitive help
77 IMPLEMENT_DYNAMIC_CLASS(wxContextHelp
, wxObject
)
79 wxContextHelp::wxContextHelp(wxWindow
* win
, bool beginHelp
)
84 BeginContextHelp(win
);
87 wxContextHelp::~wxContextHelp()
93 // Not currently needed, but on some systems capture may not work as
94 // expected so we'll leave it here for now.
96 static void wxPushOrPopEventHandlers(wxContextHelp
* help
, wxWindow
* win
, bool push
)
99 win
->PushEventHandler(new wxContextHelpEvtHandler(help
));
101 win
->PopEventHandler(true);
103 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
106 wxWindow
* child
= node
->GetData();
107 wxPushOrPopEventHandlers(help
, child
, push
);
109 node
= node
->GetNext();
114 // Begin 'context help mode'
115 bool wxContextHelp::BeginContextHelp(wxWindow
* win
)
118 win
= wxTheApp
->GetTopWindow();
122 wxCursor
cursor(wxCURSOR_QUESTION_ARROW
);
123 wxCursor oldCursor
= win
->GetCursor();
124 win
->SetCursor(cursor
);
127 // wxSetCursor(cursor);
133 wxPushOrPopEventHandlers(this, win
, true);
135 win
->PushEventHandler(new wxContextHelpEvtHandler(this));
145 wxPushOrPopEventHandlers(this, win
, false);
147 win
->PopEventHandler(true);
150 win
->SetCursor(oldCursor
);
155 wxWindow
* winAtPtr
= wxFindWindowAtPointer(pt
);
160 printf("Picked %s (%d)\n", winAtPtr
->GetName().c_str(),
166 DispatchEvent(winAtPtr
, pt
);
172 bool wxContextHelp::EndContextHelp()
179 bool wxContextHelp::EventLoop()
185 if (wxTheApp
->Pending())
187 wxTheApp
->Dispatch();
191 wxTheApp
->ProcessIdle();
198 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
200 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
202 m_contextHelp
->SetStatus(true);
203 m_contextHelp
->EndContextHelp();
207 if ((event
.GetEventType() == wxEVT_CHAR
) ||
208 (event
.GetEventType() == wxEVT_KEY_DOWN
) ||
209 (event
.GetEventType() == wxEVT_ACTIVATE
) ||
210 (event
.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED
))
212 // May have already been set to true by a left-click
213 //m_contextHelp->SetStatus(false);
214 m_contextHelp
->EndContextHelp();
218 if ((event
.GetEventType() == wxEVT_PAINT
) ||
219 (event
.GetEventType() == wxEVT_ERASE_BACKGROUND
))
228 // Dispatch the help event to the relevant window
229 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
231 wxCHECK_MSG( win
, false, _T("win parameter can't be NULL") );
233 wxHelpEvent
helpEvent(wxEVT_HELP
, win
->GetId(), pt
,
234 wxHelpEvent::Origin_HelpButton
);
235 helpEvent
.SetEventObject(win
);
237 return win
->GetEventHandler()->ProcessEvent(helpEvent
);
240 // ----------------------------------------------------------------------------
241 // wxContextHelpButton
242 // ----------------------------------------------------------------------------
245 * wxContextHelpButton
246 * You can add this to your dialogs (especially on non-Windows platforms)
247 * to put the application into context help mode.
252 static const char * csquery_xpm
[] = {
270 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
272 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
273 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
276 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
281 #if defined(__WXPM__)
282 : wxBitmapButton(parent
, id
, wxBitmap(wxCSQUERY_BITMAP
283 ,wxBITMAP_TYPE_RESOURCE
287 : wxBitmapButton(parent
, id
, wxBitmap(csquery_xpm
),
293 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& WXUNUSED(event
))
295 wxContextHelp
contextHelp(GetParent());
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
304 // trivial implementation of some methods which we don't want to make pure
305 // virtual for convenience
307 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
308 const wxString
& WXUNUSED(text
))
312 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
313 const wxString
& WXUNUSED(text
))
317 // removes the association
318 void wxHelpProvider::RemoveHelp(wxWindowBase
* WXUNUSED(window
))
322 wxHelpProvider::~wxHelpProvider()
326 wxString
wxHelpProvider::GetHelpTextMaybeAtPoint(wxWindowBase
*window
)
328 if ( m_helptextAtPoint
!= wxDefaultPosition
||
329 m_helptextOrigin
!= wxHelpEvent::Origin_Unknown
)
331 wxCHECK_MSG( window
, wxEmptyString
, _T("window must not be NULL") );
333 wxPoint pt
= m_helptextAtPoint
;
334 wxHelpEvent::Origin origin
= m_helptextOrigin
;
336 m_helptextAtPoint
= wxDefaultPosition
;
337 m_helptextOrigin
= wxHelpEvent::Origin_Unknown
;
339 return window
->GetHelpTextAtPoint(pt
, origin
);
342 return GetHelp(window
);
345 // ----------------------------------------------------------------------------
346 // wxSimpleHelpProvider
347 // ----------------------------------------------------------------------------
349 #define WINHASH_KEY(w) wxPtrToUInt(w)
351 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
353 wxSimpleHelpProviderHashMap::iterator it
= m_hashWindows
.find(WINHASH_KEY(window
));
355 if ( it
== m_hashWindows
.end() )
357 it
= m_hashIds
.find(window
->GetId());
358 if ( it
== m_hashIds
.end() )
359 return wxEmptyString
;
365 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
367 m_hashWindows
.erase(WINHASH_KEY(window
));
368 m_hashWindows
[WINHASH_KEY(window
)] = text
;
371 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
373 wxSimpleHelpProviderHashMap::key_type key
= (wxSimpleHelpProviderHashMap::key_type
)id
;
374 m_hashIds
.erase(key
);
375 m_hashIds
[key
] = text
;
378 // removes the association
379 void wxSimpleHelpProvider::RemoveHelp(wxWindowBase
* window
)
381 m_hashWindows
.erase(WINHASH_KEY(window
));
384 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
386 #if wxUSE_MS_HTML_HELP || wxUSE_TIPWINDOW
387 const wxString text
= GetHelpTextMaybeAtPoint(window
);
391 // use the native help popup style if it's available
392 #if wxUSE_MS_HTML_HELP
393 if ( !wxCHMHelpController::ShowContextHelpPopup
396 wxGetMousePosition(),
399 #endif // wxUSE_MS_HTML_HELP
402 static wxTipWindow
* s_tipWindow
= NULL
;
406 // Prevent s_tipWindow being nulled in OnIdle, thereby removing
407 // the chance for the window to be closed by ShowHelp
408 s_tipWindow
->SetTipWindowPtr(NULL
);
409 s_tipWindow
->Close();
412 s_tipWindow
= new wxTipWindow((wxWindow
*)window
, text
,
414 #else // !wxUSE_TIPWINDOW
415 // we tried wxCHMHelpController but it failed and we don't have
416 // wxTipWindow to fall back on, so
418 #endif // wxUSE_TIPWINDOW
423 #else // !wxUSE_MS_HTML_HELP && !wxUSE_TIPWINDOW
425 #endif // wxUSE_MS_HTML_HELP || wxUSE_TIPWINDOW
430 // ----------------------------------------------------------------------------
431 // wxHelpControllerHelpProvider
432 // ----------------------------------------------------------------------------
434 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
436 m_helpController
= hc
;
439 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
441 const wxString text
= GetHelpTextMaybeAtPoint(window
);
446 if ( m_helpController
)
448 // if it's a numeric topic, show it
450 if ( text
.ToLong(&topic
) )
451 return m_helpController
->DisplayContextPopup(topic
);
453 // otherwise show the text directly
454 if ( m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()) )
458 // if there is no help controller or it's not capable of showing the help,
459 // fallback to the default method
460 return wxSimpleHelpProvider::ShowHelp(window
);
463 // Convenience function for turning context id into wxString
464 wxString
wxContextId(int id
)
466 return wxString::Format(_T("%d"), id
);
469 // ----------------------------------------------------------------------------
470 // wxHelpProviderModule: module responsible for cleaning up help provider.
471 // ----------------------------------------------------------------------------
473 class wxHelpProviderModule
: public wxModule
480 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
483 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
485 bool wxHelpProviderModule::OnInit()
487 // Probably we don't want to do anything by default,
488 // since it could pull in extra code
489 // wxHelpProvider::Set(new wxSimpleHelpProvider);
494 void wxHelpProviderModule::OnExit()
496 if (wxHelpProvider::Get())
498 delete wxHelpProvider::Get();
499 wxHelpProvider::Set(NULL
);