]> git.saurik.com Git - wxWidgets.git/blob - src/common/cshelp.cpp
uma.h added
[wxWidgets.git] / src / common / cshelp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cshelp.cpp
3 // Purpose: Context sensitive help class implementation
4 // Author: Julian Smart, Vadim Zeitlin
5 // Modified by:
6 // Created: 08/09/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Julian Smart, Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "cshelp.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_HELP
32
33 #ifndef WX_PRECOMP
34 #endif
35
36 #include "wx/tipwin.h"
37 #include "wx/app.h"
38 #include "wx/module.h"
39 #include "wx/cshelp.h"
40
41 // ----------------------------------------------------------------------------
42 // wxContextHelpEvtHandler private class
43 // ----------------------------------------------------------------------------
44
45 // This class exists in order to eat events until the left mouse button is
46 // pressed
47 class wxContextHelpEvtHandler: public wxEvtHandler
48 {
49 public:
50 wxContextHelpEvtHandler(wxContextHelp* contextHelp)
51 {
52 m_contextHelp = contextHelp;
53 }
54
55 virtual bool ProcessEvent(wxEvent& event);
56
57 //// Data
58 wxContextHelp* m_contextHelp;
59 };
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // wxContextHelp
67 // ----------------------------------------------------------------------------
68
69 /*
70 * Invokes context-sensitive help
71 */
72
73
74 IMPLEMENT_DYNAMIC_CLASS(wxContextHelp, wxObject)
75
76 wxContextHelp::wxContextHelp(wxWindow* win, bool beginHelp)
77 {
78 m_inHelp = FALSE;
79
80 if (beginHelp)
81 BeginContextHelp(win);
82 }
83
84 wxContextHelp::~wxContextHelp()
85 {
86 if (m_inHelp)
87 EndContextHelp();
88 }
89
90 // Not currently needed, but on some systems capture may not work as
91 // expected so we'll leave it here for now.
92 #if 0
93 static void wxPushOrPopEventHandlers(wxContextHelp* help, wxWindow* win, bool push)
94 {
95 if (push)
96 win->PushEventHandler(new wxContextHelpEvtHandler(help));
97 else
98 win->PopEventHandler();
99
100 wxNode* node = win->GetChildren().First();
101 while (node)
102 {
103 wxWindow* child = (wxWindow*) node->Data();
104 wxPushOrPopEventHandlers(help, child, push);
105
106 node = node->Next();
107 }
108 }
109 #endif
110
111 // Begin 'context help mode'
112 bool wxContextHelp::BeginContextHelp(wxWindow* win)
113 {
114 if (!win)
115 win = wxTheApp->GetTopWindow();
116 if (!win)
117 return FALSE;
118
119 wxCursor cursor(wxCURSOR_QUESTION_ARROW);
120 wxCursor oldCursor = win->GetCursor();
121 win->SetCursor(cursor);
122
123 #ifdef __WXMSW__
124 // wxSetCursor(cursor);
125 #endif
126
127 m_status = FALSE;
128
129 win->PushEventHandler(new wxContextHelpEvtHandler(this));
130 //wxPushOrPopEventHandlers(this, win, TRUE);
131
132 win->CaptureMouse();
133
134 EventLoop();
135
136 win->ReleaseMouse();
137
138 win->PopEventHandler(TRUE);
139 //wxPushOrPopEventHandlers(this, win, FALSE);
140
141 win->SetCursor(oldCursor);
142
143 if (m_status)
144 {
145 wxPoint pt;
146 wxWindow* winAtPtr = wxFindWindowAtPointer(pt);
147 /*
148 if (winAtPtr)
149 {
150 wxString msg;
151 msg.Printf("Picked %s (%d)", (const char*) winAtPtr->GetName(), winAtPtr->GetId());
152 cout << msg << '\n';
153 }
154 */
155
156 if (winAtPtr)
157 DispatchEvent(winAtPtr, pt);
158 }
159
160 return TRUE;
161 }
162
163 bool wxContextHelp::EndContextHelp()
164 {
165 m_inHelp = FALSE;
166
167 return TRUE;
168 }
169
170 bool wxContextHelp::EventLoop()
171 {
172 m_inHelp = TRUE;
173 while ( m_inHelp )
174 {
175 if (wxTheApp->Pending())
176 {
177 wxTheApp->Dispatch();
178 }
179 else
180 {
181 wxTheApp->ProcessIdle();
182 }
183 }
184 return TRUE;
185 }
186
187 bool wxContextHelpEvtHandler::ProcessEvent(wxEvent& event)
188 {
189 if (event.GetEventType() == wxEVT_LEFT_DOWN)
190 {
191 m_contextHelp->SetStatus(TRUE);
192 m_contextHelp->EndContextHelp();
193 return TRUE;
194 }
195
196 if ((event.GetEventType() == wxEVT_CHAR) ||
197 (event.GetEventType() == wxEVT_KEY_DOWN) ||
198 (event.GetEventType() == wxEVT_ACTIVATE) ||
199 (event.GetEventType() == wxEVT_MOUSE_CAPTURE_CHANGED))
200 {
201 // May have already been set to TRUE by a left-click
202 //m_contextHelp->SetStatus(FALSE);
203 m_contextHelp->EndContextHelp();
204 return TRUE;
205 }
206
207 if ((event.GetEventType() == wxEVT_PAINT) ||
208 (event.GetEventType() == wxEVT_ERASE_BACKGROUND))
209 {
210 event.Skip();
211 return FALSE;
212 }
213
214 return TRUE;
215 }
216
217 // Dispatch the help event to the relevant window
218 bool wxContextHelp::DispatchEvent(wxWindow* win, const wxPoint& pt)
219 {
220 wxWindow* subjectOfHelp = win;
221 bool eventProcessed = FALSE;
222 while (subjectOfHelp && !eventProcessed)
223 {
224 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(), pt) ;
225 helpEvent.SetEventObject(this);
226 eventProcessed = win->GetEventHandler()->ProcessEvent(helpEvent);
227
228 // Go up the window hierarchy until the event is handled (or not).
229 // I.e. keep submitting ancestor windows until one is recognised
230 // by the app code that processes the ids and displays help.
231 subjectOfHelp = subjectOfHelp->GetParent();
232 }
233 return eventProcessed;
234 }
235
236 // ----------------------------------------------------------------------------
237 // wxContextHelpButton
238 // ----------------------------------------------------------------------------
239
240 /*
241 * wxContextHelpButton
242 * You can add this to your dialogs (especially on non-Windows platforms)
243 * to put the application into context help mode.
244 */
245
246 #if !defined(__WXMSW__)
247 static const char * csquery_xpm[] = {
248 "12 11 2 1",
249 " c None",
250 ". c #000000",
251 " ",
252 " .... ",
253 " .. .. ",
254 " .. .. ",
255 " .. ",
256 " .. ",
257 " .. ",
258 " ",
259 " .. ",
260 " .. ",
261 " "};
262 #endif
263
264 IMPLEMENT_CLASS(wxContextHelpButton, wxBitmapButton)
265
266 BEGIN_EVENT_TABLE(wxContextHelpButton, wxBitmapButton)
267 EVT_BUTTON(wxID_CONTEXT_HELP, wxContextHelpButton::OnContextHelp)
268 END_EVENT_TABLE()
269
270 wxContextHelpButton::wxContextHelpButton(wxWindow* parent,
271 wxWindowID id,
272 const wxPoint& pos,
273 const wxSize& size,
274 long style)
275 : wxBitmapButton(parent, id, wxBITMAP(csquery),
276 pos, size, style)
277 {
278 }
279
280 void wxContextHelpButton::OnContextHelp(wxCommandEvent& WXUNUSED(event))
281 {
282 wxContextHelp contextHelp(GetParent());
283 }
284
285 // ----------------------------------------------------------------------------
286 // wxHelpProvider
287 // ----------------------------------------------------------------------------
288
289 wxHelpProvider *wxHelpProvider::ms_helpProvider = (wxHelpProvider *)NULL;
290
291 // trivial implementation of some methods which we don't want to make pure
292 // virtual for convenience
293
294 void wxHelpProvider::AddHelp(wxWindowBase * WXUNUSED(window),
295 const wxString& WXUNUSED(text))
296 {
297 }
298
299 void wxHelpProvider::AddHelp(wxWindowID WXUNUSED(id),
300 const wxString& WXUNUSED(text))
301 {
302 }
303
304 wxHelpProvider::~wxHelpProvider()
305 {
306 }
307
308 // ----------------------------------------------------------------------------
309 // wxSimpleHelpProvider
310 // ----------------------------------------------------------------------------
311
312 wxString wxSimpleHelpProvider::GetHelp(const wxWindowBase *window)
313 {
314 bool wasFound;
315 wxString text = m_hashWindows.Get((long)window, &wasFound);
316 if ( !wasFound )
317 text = m_hashIds.Get(window->GetId());
318
319 return text;
320 }
321
322 void wxSimpleHelpProvider::AddHelp(wxWindowBase *window, const wxString& text)
323 {
324 m_hashWindows.Put((long)window, text);
325 }
326
327 void wxSimpleHelpProvider::AddHelp(wxWindowID id, const wxString& text)
328 {
329 m_hashIds.Put(id, text);
330 }
331
332 bool wxSimpleHelpProvider::ShowHelp(wxWindowBase *window)
333 {
334 #if wxUSE_TIPWINDOW
335 static wxTipWindow* s_tipWindow = NULL;
336
337 if (s_tipWindow)
338 {
339 // Prevent s_tipWindow being nulled in OnIdle,
340 // thereby removing the chance for the window to be closed by ShowHelp
341 s_tipWindow->SetTipWindowPtr(NULL);
342 s_tipWindow->Close();
343 }
344 s_tipWindow = NULL;
345
346 wxString text = GetHelp(window);
347 if ( !text.empty() )
348 {
349 s_tipWindow = new wxTipWindow((wxWindow *)window, text, 100, & s_tipWindow);
350
351 return TRUE;
352 }
353 #endif // wxUSE_TIPWINDOW
354
355 return FALSE;
356 }
357
358 // ----------------------------------------------------------------------------
359 // wxHelpControllerHelpProvider
360 // ----------------------------------------------------------------------------
361
362 wxHelpControllerHelpProvider::wxHelpControllerHelpProvider(wxHelpControllerBase* hc)
363 {
364 m_helpController = hc;
365 }
366
367 bool wxHelpControllerHelpProvider::ShowHelp(wxWindowBase *window)
368 {
369 wxString text = GetHelp(window);
370 if ( !text.empty() )
371 {
372 if (m_helpController)
373 {
374 if (text.IsNumber())
375 return m_helpController->DisplayContextPopup(wxAtoi(text));
376
377 // If the help controller is capable of popping up the text...
378 else if (m_helpController->DisplayTextPopup(text, wxGetMousePosition()))
379 {
380 return TRUE;
381 }
382 else
383 // ...else use the default method.
384 return wxSimpleHelpProvider::ShowHelp(window);
385 }
386 else
387 return wxSimpleHelpProvider::ShowHelp(window);
388
389 }
390
391 return FALSE;
392 }
393
394 // Convenience function for turning context id into wxString
395 wxString wxContextId(int id)
396 {
397 return wxString(IntToString(id));
398 }
399
400 // ----------------------------------------------------------------------------
401 // wxHelpProviderModule: module responsible for cleaning up help provider.
402 // ----------------------------------------------------------------------------
403
404 class wxHelpProviderModule : public wxModule
405 {
406 public:
407 bool OnInit();
408 void OnExit();
409
410 private:
411 DECLARE_DYNAMIC_CLASS(wxHelpProviderModule)
412 };
413
414 IMPLEMENT_DYNAMIC_CLASS(wxHelpProviderModule, wxModule)
415
416 bool wxHelpProviderModule::OnInit()
417 {
418 // Probably we don't want to do anything by default,
419 // since it could pull in extra code
420 // wxHelpProvider::Set(new wxSimpleHelpProvider);
421
422 return TRUE;
423 }
424
425 void wxHelpProviderModule::OnExit()
426 {
427 if (wxHelpProvider::Get())
428 {
429 delete wxHelpProvider::Get();
430 wxHelpProvider::Set(NULL);
431 }
432 }
433
434 #endif // wxUSE_HELP