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 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
166 m_contextHelp
->SetStatus(TRUE
);
167 m_contextHelp
->EndContextHelp();
171 if ((event
.GetEventType() == wxEVT_CHAR
) ||
172 (event
.GetEventType() == wxEVT_KEY_DOWN
) ||
173 (event
.GetEventType() == wxEVT_ACTIVATE
) ||
174 (event
.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED
))
176 m_contextHelp
->SetStatus(FALSE
);
177 m_contextHelp
->EndContextHelp();
181 if ((event
.GetEventType() == wxEVT_PAINT
) ||
182 (event
.GetEventType() == wxEVT_ERASE_BACKGROUND
))
191 // Dispatch the help event to the relevant window
192 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
194 wxWindow
* subjectOfHelp
= win
;
195 bool eventProcessed
= FALSE
;
196 while (subjectOfHelp
&& !eventProcessed
)
198 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
199 helpEvent
.SetEventObject(this);
200 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
202 // Go up the window hierarchy until the event is handled (or not).
203 // I.e. keep submitting ancestor windows until one is recognised
204 // by the app code that processes the ids and displays help.
205 subjectOfHelp
= subjectOfHelp
->GetParent();
207 return eventProcessed
;
210 // ----------------------------------------------------------------------------
211 // wxContextHelpButton
212 // ----------------------------------------------------------------------------
215 * wxContextHelpButton
216 * You can add this to your dialogs (especially on non-Windows platforms)
217 * to put the application into context help mode.
220 #if !defined(__WXMSW__)
221 static char * csquery_xpm
[] = {
238 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
240 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
241 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
244 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
249 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
254 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& event
)
256 wxContextHelp
contextHelp(GetParent());
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
265 // trivial implementation of some methods which we don't want to make pure
266 // virtual for convenience
268 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
269 const wxString
& WXUNUSED(text
))
273 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
274 const wxString
& WXUNUSED(text
))
278 wxHelpProvider::~wxHelpProvider()
282 // ----------------------------------------------------------------------------
283 // wxSimpleHelpProvider
284 // ----------------------------------------------------------------------------
286 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
289 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
291 text
= m_hashIds
.Get(window
->GetId());
296 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
298 m_hashWindows
.Put((long)window
, text
);
301 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
303 m_hashIds
.Put(id
, text
);
306 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
308 wxString text
= GetHelp(window
);
311 new wxTipWindow((wxWindow
*)window
, text
);
319 // ----------------------------------------------------------------------------
320 // wxHelpControllerHelpProvider
321 // ----------------------------------------------------------------------------
323 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
325 m_helpController
= hc
;
328 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
330 wxString text
= GetHelp(window
);
333 if (m_helpController
)
336 return m_helpController
->DisplayContextPopup(wxAtoi(text
));
338 // If the help controller is capable of popping up the text...
339 else if (m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()))
344 // ...else use the default method.
345 return wxSimpleHelpProvider::ShowHelp(window
);
348 return wxSimpleHelpProvider::ShowHelp(window
);
355 // Convenience function for turning context id into wxString
356 wxString
wxContextId(int id
)
358 return wxString(IntToString(id
));
361 // ----------------------------------------------------------------------------
362 // wxHelpProviderModule: module responsible for cleaning up help provider.
363 // ----------------------------------------------------------------------------
365 class wxHelpProviderModule
: public wxModule
372 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
375 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
377 bool wxHelpProviderModule::OnInit()
379 // Probably we don't want to do anything by default,
380 // since it could pull in extra code
381 // wxHelpProvider::Set(new wxSimpleHelpProvider);
386 void wxHelpProviderModule::OnExit()
388 if (wxHelpProvider::Get())
390 delete wxHelpProvider::Get();
391 wxHelpProvider::Set(NULL
);