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
);
123 DispatchEvent(winAtPtr
, pt
);
129 bool wxContextHelp::EndContextHelp()
136 bool wxContextHelp::EventLoop()
141 if (wxTheApp
->Pending())
143 wxTheApp
->Dispatch();
147 wxTheApp
->ProcessIdle();
153 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
155 switch (event
.GetEventType())
157 case wxEVT_LEFT_DOWN
:
159 //wxMouseEvent& mouseEvent = (wxMouseEvent&) event;
160 m_contextHelp
->SetStatus(TRUE
);
161 m_contextHelp
->EndContextHelp();
168 case wxEVT_MOUSE_CAPTURE_CHANGED
:
170 m_contextHelp
->SetStatus(FALSE
);
171 m_contextHelp
->EndContextHelp();
176 case wxEVT_ERASE_BACKGROUND
:
187 // Dispatch the help event to the relevant window
188 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
190 wxWindow
* subjectOfHelp
= win
;
191 bool eventProcessed
= FALSE
;
192 while (subjectOfHelp
&& !eventProcessed
)
194 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
195 helpEvent
.SetEventObject(this);
196 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
198 // Go up the window hierarchy until the event is handled (or not).
199 // I.e. keep submitting ancestor windows until one is recognised
200 // by the app code that processes the ids and displays help.
201 subjectOfHelp
= subjectOfHelp
->GetParent();
203 return eventProcessed
;
206 // ----------------------------------------------------------------------------
207 // wxContextHelpButton
208 // ----------------------------------------------------------------------------
211 * wxContextHelpButton
212 * You can add this to your dialogs (especially on non-Windows platforms)
213 * to put the application into context help mode.
216 #if !defined(__WXMSW__)
217 static char * csquery_xpm
[] = {
234 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
236 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
237 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
240 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
245 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
250 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& event
)
252 wxContextHelp
contextHelp(GetParent());
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
261 // trivial implementation of some methods which we don't want to make pure
262 // virtual for convenience
264 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
265 const wxString
& WXUNUSED(text
))
269 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
270 const wxString
& WXUNUSED(text
))
274 wxHelpProvider::~wxHelpProvider()
278 // ----------------------------------------------------------------------------
279 // wxSimpleHelpProvider
280 // ----------------------------------------------------------------------------
282 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
285 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
287 text
= m_hashIds
.Get(window
->GetId());
292 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
294 m_hashWindows
.Put((long)window
, text
);
297 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
299 m_hashIds
.Put(id
, text
);
302 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
304 wxString text
= GetHelp(window
);
307 new wxTipWindow((wxWindow
*)window
, text
);
315 // ----------------------------------------------------------------------------
316 // wxHelpProviderModule: module responsible for cleaning up help provider.
317 // ----------------------------------------------------------------------------
319 class wxHelpProviderModule
: public wxModule
326 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
329 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
331 bool wxHelpProviderModule::OnInit()
333 // Probably we don't want to do anything by default,
334 // since it could pull in extra code
335 // wxHelpProvider::Set(new wxSimpleHelpProvider);
340 void wxHelpProviderModule::OnExit()
342 if (wxHelpProvider::Get())
344 delete wxHelpProvider::Get();
345 wxHelpProvider::Set(NULL
);