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
;
60 DECLARE_NO_COPY_CLASS(wxContextHelpEvtHandler
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
72 * Invokes context-sensitive help
76 IMPLEMENT_DYNAMIC_CLASS(wxContextHelp
, wxObject
)
78 wxContextHelp::wxContextHelp(wxWindow
* win
, bool beginHelp
)
83 BeginContextHelp(win
);
86 wxContextHelp::~wxContextHelp()
92 // Not currently needed, but on some systems capture may not work as
93 // expected so we'll leave it here for now.
95 static void wxPushOrPopEventHandlers(wxContextHelp
* help
, wxWindow
* win
, bool push
)
98 win
->PushEventHandler(new wxContextHelpEvtHandler(help
));
100 win
->PopEventHandler();
102 wxNode
* node
= win
->GetChildren().First();
105 wxWindow
* child
= (wxWindow
*) node
->Data();
106 wxPushOrPopEventHandlers(help
, child
, push
);
113 // Begin 'context help mode'
114 bool wxContextHelp::BeginContextHelp(wxWindow
* win
)
117 win
= wxTheApp
->GetTopWindow();
121 wxCursor
cursor(wxCURSOR_QUESTION_ARROW
);
122 wxCursor oldCursor
= win
->GetCursor();
123 win
->SetCursor(cursor
);
126 // wxSetCursor(cursor);
131 win
->PushEventHandler(new wxContextHelpEvtHandler(this));
132 //wxPushOrPopEventHandlers(this, win, TRUE);
140 win
->PopEventHandler(TRUE
);
141 //wxPushOrPopEventHandlers(this, win, FALSE);
143 win
->SetCursor(oldCursor
);
148 wxWindow
* winAtPtr
= wxFindWindowAtPointer(pt
);
153 msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId());
159 DispatchEvent(winAtPtr
, pt
);
165 bool wxContextHelp::EndContextHelp()
172 bool wxContextHelp::EventLoop()
178 if (wxTheApp
->Pending())
180 wxTheApp
->Dispatch();
184 wxTheApp
->ProcessIdle();
191 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent
& event
)
193 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
195 m_contextHelp
->SetStatus(TRUE
);
196 m_contextHelp
->EndContextHelp();
200 if ((event
.GetEventType() == wxEVT_CHAR
) ||
201 (event
.GetEventType() == wxEVT_KEY_DOWN
) ||
202 (event
.GetEventType() == wxEVT_ACTIVATE
) ||
203 (event
.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED
))
205 // May have already been set to TRUE by a left-click
206 //m_contextHelp->SetStatus(FALSE);
207 m_contextHelp
->EndContextHelp();
211 if ((event
.GetEventType() == wxEVT_PAINT
) ||
212 (event
.GetEventType() == wxEVT_ERASE_BACKGROUND
))
221 // Dispatch the help event to the relevant window
222 bool wxContextHelp::DispatchEvent(wxWindow
* win
, const wxPoint
& pt
)
224 wxWindow
* subjectOfHelp
= win
;
225 bool eventProcessed
= FALSE
;
226 while (subjectOfHelp
&& !eventProcessed
)
228 wxHelpEvent
helpEvent(wxEVT_HELP
, subjectOfHelp
->GetId(), pt
) ;
229 helpEvent
.SetEventObject(this);
231 eventProcessed
= win
->GetEventHandler()->ProcessEvent(helpEvent
);
233 // Go up the window hierarchy until the event is handled (or not).
234 // I.e. keep submitting ancestor windows until one is recognised
235 // by the app code that processes the ids and displays help.
236 subjectOfHelp
= subjectOfHelp
->GetParent();
238 return eventProcessed
;
241 // ----------------------------------------------------------------------------
242 // wxContextHelpButton
243 // ----------------------------------------------------------------------------
246 * wxContextHelpButton
247 * You can add this to your dialogs (especially on non-Windows platforms)
248 * to put the application into context help mode.
251 #if !defined(__WXMSW__)
252 static const char * csquery_xpm
[] = {
269 IMPLEMENT_CLASS(wxContextHelpButton
, wxBitmapButton
)
271 BEGIN_EVENT_TABLE(wxContextHelpButton
, wxBitmapButton
)
272 EVT_BUTTON(wxID_CONTEXT_HELP
, wxContextHelpButton::OnContextHelp
)
275 wxContextHelpButton::wxContextHelpButton(wxWindow
* parent
,
280 #if defined(__WXPM__)
281 : wxBitmapButton(parent
, id
, wxBitmap(wxCSQUERY_BITMAP
282 ,wxBITMAP_TYPE_RESOURCE
286 : wxBitmapButton(parent
, id
, wxBITMAP(csquery
),
292 void wxContextHelpButton::OnContextHelp(wxCommandEvent
& WXUNUSED(event
))
294 wxContextHelp
contextHelp(GetParent());
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 wxHelpProvider
*wxHelpProvider::ms_helpProvider
= (wxHelpProvider
*)NULL
;
303 // trivial implementation of some methods which we don't want to make pure
304 // virtual for convenience
306 void wxHelpProvider::AddHelp(wxWindowBase
* WXUNUSED(window
),
307 const wxString
& WXUNUSED(text
))
311 void wxHelpProvider::AddHelp(wxWindowID
WXUNUSED(id
),
312 const wxString
& WXUNUSED(text
))
316 // removes the association
317 void wxHelpProvider::RemoveHelp(wxWindowBase
* WXUNUSED(window
))
321 wxHelpProvider::~wxHelpProvider()
325 // ----------------------------------------------------------------------------
326 // wxSimpleHelpProvider
327 // ----------------------------------------------------------------------------
329 wxString
wxSimpleHelpProvider::GetHelp(const wxWindowBase
*window
)
332 wxString text
= m_hashWindows
.Get((long)window
, &wasFound
);
334 text
= m_hashIds
.Get(window
->GetId());
339 void wxSimpleHelpProvider::AddHelp(wxWindowBase
*window
, const wxString
& text
)
341 m_hashWindows
.Delete((long)window
);
342 m_hashWindows
.Put((long)window
, text
);
345 void wxSimpleHelpProvider::AddHelp(wxWindowID id
, const wxString
& text
)
347 m_hashIds
.Delete((long)id
);
348 m_hashIds
.Put(id
, text
);
351 // removes the association
352 void wxSimpleHelpProvider::RemoveHelp(wxWindowBase
* window
)
354 m_hashWindows
.Delete((long)window
);
357 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase
*window
)
360 static wxTipWindow
* s_tipWindow
= NULL
;
364 // Prevent s_tipWindow being nulled in OnIdle,
365 // thereby removing the chance for the window to be closed by ShowHelp
366 s_tipWindow
->SetTipWindowPtr(NULL
);
367 s_tipWindow
->Close();
371 wxString text
= GetHelp(window
);
374 s_tipWindow
= new wxTipWindow((wxWindow
*)window
, text
, 100, & s_tipWindow
);
378 #endif // wxUSE_TIPWINDOW
383 // ----------------------------------------------------------------------------
384 // wxHelpControllerHelpProvider
385 // ----------------------------------------------------------------------------
387 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase
* hc
)
389 m_helpController
= hc
;
392 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase
*window
)
394 wxString text
= GetHelp(window
);
397 if (m_helpController
)
400 return m_helpController
->DisplayContextPopup(wxAtoi(text
));
402 // If the help controller is capable of popping up the text...
403 else if (m_helpController
->DisplayTextPopup(text
, wxGetMousePosition()))
408 // ...else use the default method.
409 return wxSimpleHelpProvider::ShowHelp(window
);
412 return wxSimpleHelpProvider::ShowHelp(window
);
419 // Convenience function for turning context id into wxString
420 wxString
wxContextId(int id
)
422 return wxString(IntToString(id
));
425 // ----------------------------------------------------------------------------
426 // wxHelpProviderModule: module responsible for cleaning up help provider.
427 // ----------------------------------------------------------------------------
429 class wxHelpProviderModule
: public wxModule
436 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule
)
439 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule
, wxModule
)
441 bool wxHelpProviderModule::OnInit()
443 // Probably we don't want to do anything by default,
444 // since it could pull in extra code
445 // wxHelpProvider::Set(new wxSimpleHelpProvider);
450 void wxHelpProviderModule::OnExit()
452 if (wxHelpProvider::Get())
454 delete wxHelpProvider::Get();
455 wxHelpProvider::Set(NULL
);