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 static void wxPushOrPopEventHandlers(wxContextHelp
* help
, wxWindow
* win
, bool push
)
93 win
->PushEventHandler(new wxContextHelpEvtHandler(help
));
95 win
->PopEventHandler();
97 wxNode
* node
= win
->GetChildren().First();
100 wxWindow
* child
= (wxWindow
*) node
->Data();
101 wxPushOrPopEventHandlers(help
, child
, push
);
107 // Begin 'context help mode'
108 bool wxContextHelp::BeginContextHelp(wxWindow
* win
)
111 win
= wxTheApp
->GetTopWindow();
115 wxCursor
cursor(wxCURSOR_QUESTION_ARROW
);
116 wxCursor oldCursor
= win
->GetCursor();
117 win
->SetCursor(cursor
);
120 // wxSetCursor(cursor);
125 // win->PushEventHandler(new wxContextHelpEvtHandler(this));
126 wxPushOrPopEventHandlers(this, win
, TRUE
);
134 // win->PopEventHandler(TRUE);
135 wxPushOrPopEventHandlers(this, win
, FALSE
);
137 win
->SetCursor(oldCursor
);
142 wxWindow
* winAtPtr
= wxFindWindowAtPointer(pt
);
147 msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId());
153 DispatchEvent(winAtPtr
, pt
);
159 bool wxContextHelp::EndContextHelp()
166 bool wxContextHelp::EventLoop()
171 if (wxTheApp
->Pending())
173 wxTheApp
->Dispatch();
177 wxTheApp
->ProcessIdle();
183 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
185 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
187 m_contextHelp
->SetStatus(TRUE
);
188 m_contextHelp
->EndContextHelp();
192 if ((event
.GetEventType() == wxEVT_CHAR
) ||
193 (event
.GetEventType() == wxEVT_KEY_DOWN
) ||
194 (event
.GetEventType() == wxEVT_ACTIVATE
) ||
195 (event
.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED
))
197 // May have already been set to TRUE by a left-click
198 //m_contextHelp->SetStatus(FALSE);
199 m_contextHelp
->EndContextHelp();
203 if ((event
.GetEventType() == wxEVT_PAINT
) ||
204 (event
.GetEventType() == wxEVT_ERASE_BACKGROUND
))
213 // Dispatch the help event to the relevant window
214 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
216 wxWindow
* subjectOfHelp
= win
;
217 bool eventProcessed
= FALSE
;
218 while (subjectOfHelp
&& !eventProcessed
)
220 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
221 helpEvent
.SetEventObject(this);
222 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
224 // Go up the window hierarchy until the event is handled (or not).
225 // I.e. keep submitting ancestor windows until one is recognised
226 // by the app code that processes the ids and displays help.
227 subjectOfHelp
= subjectOfHelp
->GetParent();
229 return eventProcessed
;
232 // ----------------------------------------------------------------------------
233 // wxContextHelpButton
234 // ----------------------------------------------------------------------------
237 * wxContextHelpButton
238 * You can add this to your dialogs (especially on non-Windows platforms)
239 * to put the application into context help mode.
242 #if !defined(__WXMSW__)
243 static const char * csquery_xpm
[] = {
260 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
262 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
263 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
266 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
271 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
276 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& WXUNUSED(event
))
278 wxContextHelp
contextHelp(GetParent());
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
287 // trivial implementation of some methods which we don't want to make pure
288 // virtual for convenience
290 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
291 const wxString
& WXUNUSED(text
))
295 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
296 const wxString
& WXUNUSED(text
))
300 wxHelpProvider::~wxHelpProvider()
304 // ----------------------------------------------------------------------------
305 // wxSimpleHelpProvider
306 // ----------------------------------------------------------------------------
308 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
311 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
313 text
= m_hashIds
.Get(window
->GetId());
318 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
320 m_hashWindows
.Put((long)window
, text
);
323 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
325 m_hashIds
.Put(id
, text
);
328 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
331 static wxTipWindow
* s_tipWindow
= NULL
;
335 // Prevent s_tipWindow being nulled in OnIdle,
336 // thereby removing the chance for the window to be closed by ShowHelp
337 s_tipWindow
->SetTipWindowPtr(NULL
);
338 s_tipWindow
->Close();
342 wxString text
= GetHelp(window
);
345 s_tipWindow
= new wxTipWindow((wxWindow
*)window
, text
, 100, & s_tipWindow
);
349 #endif // wxUSE_TIPWINDOW
354 // ----------------------------------------------------------------------------
355 // wxHelpControllerHelpProvider
356 // ----------------------------------------------------------------------------
358 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
360 m_helpController
= hc
;
363 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
365 wxString text
= GetHelp(window
);
368 if (m_helpController
)
371 return m_helpController
->DisplayContextPopup(wxAtoi(text
));
373 // If the help controller is capable of popping up the text...
374 else if (m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()))
379 // ...else use the default method.
380 return wxSimpleHelpProvider::ShowHelp(window
);
383 return wxSimpleHelpProvider::ShowHelp(window
);
390 // Convenience function for turning context id into wxString
391 wxString
wxContextId(int id
)
393 return wxString(IntToString(id
));
396 // ----------------------------------------------------------------------------
397 // wxHelpProviderModule: module responsible for cleaning up help provider.
398 // ----------------------------------------------------------------------------
400 class wxHelpProviderModule
: public wxModule
407 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
410 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
412 bool wxHelpProviderModule::OnInit()
414 // Probably we don't want to do anything by default,
415 // since it could pull in extra code
416 // wxHelpProvider::Set(new wxSimpleHelpProvider);
421 void wxHelpProviderModule::OnExit()
423 if (wxHelpProvider::Get())
425 delete wxHelpProvider::Get();
426 wxHelpProvider::Set(NULL
);