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 // Begin 'context help mode'
91 bool wxContextHelp::BeginContextHelp(wxWindow
* win
)
94 win
= wxTheApp
->GetTopWindow();
98 wxCursor
cursor(wxCURSOR_QUESTION_ARROW
);
99 wxCursor oldCursor
= win
->GetCursor();
100 win
->SetCursor(cursor
);
103 // wxSetCursor(cursor);
106 win
->PushEventHandler(new wxContextHelpEvtHandler(this));
114 win
->PopEventHandler(TRUE
);
116 win
->SetCursor(oldCursor
);
121 wxWindow
* winAtPtr
= wxFindWindowAtPointer(pt
);
126 msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId());
132 DispatchEvent(winAtPtr
, pt
);
138 bool wxContextHelp::EndContextHelp()
145 bool wxContextHelp::EventLoop()
150 if (wxTheApp
->Pending())
152 wxTheApp
->Dispatch();
156 wxTheApp
->ProcessIdle();
162 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
164 switch (event
.GetEventType())
166 case wxEVT_LEFT_DOWN
:
168 //wxMouseEvent& mouseEvent = (wxMouseEvent&) event;
169 m_contextHelp
->SetStatus(TRUE
);
170 m_contextHelp
->EndContextHelp();
177 case wxEVT_MOUSE_CAPTURE_CHANGED
:
179 m_contextHelp
->SetStatus(FALSE
);
180 m_contextHelp
->EndContextHelp();
185 case wxEVT_ERASE_BACKGROUND
:
196 // Dispatch the help event to the relevant window
197 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
199 wxWindow
* subjectOfHelp
= win
;
200 bool eventProcessed
= FALSE
;
201 while (subjectOfHelp
&& !eventProcessed
)
203 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
204 helpEvent
.SetEventObject(this);
205 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
207 // Go up the window hierarchy until the event is handled (or not).
208 // I.e. keep submitting ancestor windows until one is recognised
209 // by the app code that processes the ids and displays help.
210 subjectOfHelp
= subjectOfHelp
->GetParent();
212 return eventProcessed
;
215 // ----------------------------------------------------------------------------
216 // wxContextHelpButton
217 // ----------------------------------------------------------------------------
220 * wxContextHelpButton
221 * You can add this to your dialogs (especially on non-Windows platforms)
222 * to put the application into context help mode.
225 #if !defined(__WXMSW__)
226 static char * csquery_xpm
[] = {
243 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
245 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
246 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
249 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
254 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
259 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& event
)
261 wxContextHelp
contextHelp(GetParent());
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
270 // trivial implementation of some methods which we don't want to make pure
271 // virtual for convenience
273 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
274 const wxString
& WXUNUSED(text
))
278 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
279 const wxString
& WXUNUSED(text
))
283 wxHelpProvider::~wxHelpProvider()
287 // ----------------------------------------------------------------------------
288 // wxSimpleHelpProvider
289 // ----------------------------------------------------------------------------
291 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
294 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
296 text
= m_hashIds
.Get(window
->GetId());
301 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
303 m_hashWindows
.Put((long)window
, text
);
306 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
308 m_hashIds
.Put(id
, text
);
311 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
313 wxString text
= GetHelp(window
);
316 new wxTipWindow((wxWindow
*)window
, text
);
324 // ----------------------------------------------------------------------------
325 // wxHelpControllerHelpProvider
326 // ----------------------------------------------------------------------------
328 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
330 m_helpController
= hc
;
333 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
335 wxString text
= GetHelp(window
);
338 if (m_helpController
)
341 return m_helpController
->DisplayContextPopup(wxAtoi(text
));
343 // If the help controller is capable of popping up the text...
344 else if (m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()))
349 // ...else use the default method.
350 return wxSimpleHelpProvider::ShowHelp(window
);
353 return wxSimpleHelpProvider::ShowHelp(window
);
360 // Convenience function for turning context id into wxString
361 wxString
wxContextId(int id
)
363 return wxString(IntToString(id
));
366 // ----------------------------------------------------------------------------
367 // wxHelpProviderModule: module responsible for cleaning up help provider.
368 // ----------------------------------------------------------------------------
370 class wxHelpProviderModule
: public wxModule
377 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
380 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
382 bool wxHelpProviderModule::OnInit()
384 // Probably we don't want to do anything by default,
385 // since it could pull in extra code
386 // wxHelpProvider::Set(new wxSimpleHelpProvider);
391 void wxHelpProviderModule::OnExit()
393 if (wxHelpProvider::Get())
395 delete wxHelpProvider::Get();
396 wxHelpProvider::Set(NULL
);