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 // ============================================================================
17 #pragma implementation "cshelp.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/tipwin.h"
38 #include "wx/module.h"
39 #include "wx/cshelp.h"
41 // ----------------------------------------------------------------------------
42 // wxContextHelpEvtHandler private class
43 // ----------------------------------------------------------------------------
45 // This class exists in order to eat events until the left mouse button is
47 class wxContextHelpEvtHandler
: public wxEvtHandler
50 wxContextHelpEvtHandler(wxContextHelp
* contextHelp
)
52 m_contextHelp
= contextHelp
;
55 virtual bool ProcessEvent(wxEvent
& event
);
58 wxContextHelp
* m_contextHelp
;
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
70 * Invokes context-sensitive help
74 IMPLEMENT_DYNAMIC_CLASS(wxContextHelp
, wxObject
)
76 wxContextHelp::wxContextHelp(wxWindow
* win
, bool beginHelp
)
81 BeginContextHelp(win
);
84 wxContextHelp::~wxContextHelp()
90 // Not currently needed, but on some systems capture may not work as
91 // expected so we'll leave it here for now.
93 static void wxPushOrPopEventHandlers(wxContextHelp
* help
, wxWindow
* win
, bool push
)
96 win
->PushEventHandler(new wxContextHelpEvtHandler(help
));
98 win
->PopEventHandler();
100 wxNode
* node
= win
->GetChildren().First();
103 wxWindow
* child
= (wxWindow
*) node
->Data();
104 wxPushOrPopEventHandlers(help
, child
, push
);
111 // Begin 'context help mode'
112 bool wxContextHelp::BeginContextHelp(wxWindow
* win
)
115 win
= wxTheApp
->GetTopWindow();
119 wxCursor
cursor(wxCURSOR_QUESTION_ARROW
);
120 wxCursor oldCursor
= win
->GetCursor();
121 win
->SetCursor(cursor
);
124 // wxSetCursor(cursor);
129 win
->PushEventHandler(new wxContextHelpEvtHandler(this));
130 //wxPushOrPopEventHandlers(this, win, TRUE);
138 win
->PopEventHandler(TRUE
);
139 //wxPushOrPopEventHandlers(this, win, FALSE);
141 win
->SetCursor(oldCursor
);
146 wxWindow
* winAtPtr
= wxFindWindowAtPointer(pt
);
151 msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId());
157 DispatchEvent(winAtPtr
, pt
);
163 bool wxContextHelp::EndContextHelp()
170 bool wxContextHelp::EventLoop()
176 if (wxTheApp
->Pending())
178 wxTheApp
->Dispatch();
182 wxTheApp
->ProcessIdle();
189 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
191 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
193 m_contextHelp
->SetStatus(TRUE
);
194 m_contextHelp
->EndContextHelp();
198 if ((event
.GetEventType() == wxEVT_CHAR
) ||
199 (event
.GetEventType() == wxEVT_KEY_DOWN
) ||
200 (event
.GetEventType() == wxEVT_ACTIVATE
) ||
201 (event
.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED
))
203 // May have already been set to TRUE by a left-click
204 //m_contextHelp->SetStatus(FALSE);
205 m_contextHelp
->EndContextHelp();
209 if ((event
.GetEventType() == wxEVT_PAINT
) ||
210 (event
.GetEventType() == wxEVT_ERASE_BACKGROUND
))
219 // Dispatch the help event to the relevant window
220 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
222 wxWindow
* subjectOfHelp
= win
;
223 bool eventProcessed
= FALSE
;
224 while (subjectOfHelp
&& !eventProcessed
)
226 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
227 helpEvent
.SetEventObject(this);
229 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
231 // Go up the window hierarchy until the event is handled (or not).
232 // I.e. keep submitting ancestor windows until one is recognised
233 // by the app code that processes the ids and displays help.
234 subjectOfHelp
= subjectOfHelp
->GetParent();
236 return eventProcessed
;
239 // ----------------------------------------------------------------------------
240 // wxContextHelpButton
241 // ----------------------------------------------------------------------------
244 * wxContextHelpButton
245 * You can add this to your dialogs (especially on non-Windows platforms)
246 * to put the application into context help mode.
249 #if !defined(__WXMSW__)
250 static const char * csquery_xpm
[] = {
267 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
269 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
270 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
273 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
278 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
283 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& WXUNUSED(event
))
285 wxContextHelp
contextHelp(GetParent());
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
294 // trivial implementation of some methods which we don't want to make pure
295 // virtual for convenience
297 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
298 const wxString
& WXUNUSED(text
))
302 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
303 const wxString
& WXUNUSED(text
))
307 // removes the association
308 void wxHelpProvider::RemoveHelp(wxWindowBase
* WXUNUSED(window
))
312 wxHelpProvider::~wxHelpProvider()
316 // ----------------------------------------------------------------------------
317 // wxSimpleHelpProvider
318 // ----------------------------------------------------------------------------
320 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
323 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
325 text
= m_hashIds
.Get(window
->GetId());
330 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
332 m_hashWindows
.Put((long)window
, text
);
335 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
337 m_hashIds
.Put(id
, text
);
340 // removes the association
341 void wxSimpleHelpProvider::RemoveHelp(wxWindowBase
* window
)
343 m_hashWindows
.Delete((long)window
);
346 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
349 static wxTipWindow
* s_tipWindow
= NULL
;
353 // Prevent s_tipWindow being nulled in OnIdle,
354 // thereby removing the chance for the window to be closed by ShowHelp
355 s_tipWindow
->SetTipWindowPtr(NULL
);
356 s_tipWindow
->Close();
360 wxString text
= GetHelp(window
);
363 s_tipWindow
= new wxTipWindow((wxWindow
*)window
, text
, 100, & s_tipWindow
);
367 #endif // wxUSE_TIPWINDOW
372 // ----------------------------------------------------------------------------
373 // wxHelpControllerHelpProvider
374 // ----------------------------------------------------------------------------
376 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
378 m_helpController
= hc
;
381 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
383 wxString text
= GetHelp(window
);
386 if (m_helpController
)
389 return m_helpController
->DisplayContextPopup(wxAtoi(text
));
391 // If the help controller is capable of popping up the text...
392 else if (m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()))
397 // ...else use the default method.
398 return wxSimpleHelpProvider::ShowHelp(window
);
401 return wxSimpleHelpProvider::ShowHelp(window
);
408 // Convenience function for turning context id into wxString
409 wxString
wxContextId(int id
)
411 return wxString(IntToString(id
));
414 // ----------------------------------------------------------------------------
415 // wxHelpProviderModule: module responsible for cleaning up help provider.
416 // ----------------------------------------------------------------------------
418 class wxHelpProviderModule
: public wxModule
425 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
428 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
430 bool wxHelpProviderModule::OnInit()
432 // Probably we don't want to do anything by default,
433 // since it could pull in extra code
434 // wxHelpProvider::Set(new wxSimpleHelpProvider);
439 void wxHelpProviderModule::OnExit()
441 if (wxHelpProvider::Get())
443 delete wxHelpProvider::Get();
444 wxHelpProvider::Set(NULL
);