]> git.saurik.com Git - wxWidgets.git/blame - src/html/helpctrl.cpp
don't assert if MsgWaitForMultipleObjects() returns WAIT_OBJECT_0 but there are no...
[wxWidgets.git] / src / html / helpctrl.cpp
CommitLineData
8ec2b484
HH
1/////////////////////////////////////////////////////////////////////////////
2// Name: helpctrl.cpp
3// Purpose: wxHtmlHelpController
f42b1601 4// Notes: Based on htmlhelp.cpp, implementing a monolithic
8ec2b484
HH
5// HTML Help controller class, by Vaclav Slavik
6// Author: Harm van der Heijden and Vaclav Slavik
69941f05 7// RCS-ID: $Id$
8ec2b484 8// Copyright: (c) Harm van der Heijden and Vaclav Slavik
65571936 9// Licence: wxWindows licence
8ec2b484
HH
10/////////////////////////////////////////////////////////////////////////////
11
8ec2b484
HH
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
3379ed37 19#if wxUSE_WXHTML_HELP
8ec2b484 20
07b8d7ec
VZ
21#ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/intl.h"
24#endif // WX_PRECOMP
25
8ec2b484 26#include "wx/busyinfo.h"
3755cfa6 27#include "wx/html/helpctrl.h"
f3e156ef 28#include "wx/html/helpwnd.h"
3755cfa6
JS
29#include "wx/html/helpfrm.h"
30#include "wx/html/helpdlg.h"
c010d6a9 31
673dfcfa 32#if wxUSE_HELP
c010d6a9 33 #include "wx/tipwin.h"
673dfcfa 34#endif
d1da8872 35
3527f29c
VS
36#if wxUSE_LIBMSPACK
37#include "wx/html/forcelnk.h"
38FORCE_LINK(wxhtml_chm_support)
39#endif
673dfcfa 40
b4414c1f 41IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpController, wxHelpControllerBase)
f42b1601 42
3db52265
JS
43wxHtmlHelpController::wxHtmlHelpController(int style, wxWindow* parentWindow):
44 wxHelpControllerBase(parentWindow)
8ec2b484 45{
3755cfa6 46 m_helpWindow = NULL;
f3e156ef
JS
47 m_helpFrame = NULL;
48 m_helpDialog = NULL;
8ec2b484
HH
49 m_Config = NULL;
50 m_ConfigRoot = wxEmptyString;
51 m_titleFormat = _("Help: %s");
d5bb85a0 52 m_FrameStyle = style;
8ec2b484
HH
53}
54
55wxHtmlHelpController::~wxHtmlHelpController()
56{
f6bcfd97
BP
57 if (m_Config)
58 WriteCustomization(m_Config, m_ConfigRoot);
3755cfa6 59 if (m_helpWindow)
b854b7b8 60 DestroyHelpWindow();
8ec2b484
HH
61}
62
b854b7b8
VS
63
64void wxHtmlHelpController::DestroyHelpWindow()
65{
3755cfa6
JS
66 if (m_FrameStyle & wxHF_EMBEDDED)
67 return;
68
69 // Find top-most parent window
70 // If a modal dialog
71 wxWindow* parent = FindTopLevelWindow();
72 if (parent)
73 {
74 wxDialog* dialog = wxDynamicCast(parent, wxDialog);
75 if (dialog && dialog->IsModal())
76 {
77 dialog->EndModal(wxID_OK);
78 }
79 parent->Destroy();
80 m_helpWindow = NULL;
81 }
f3e156ef
JS
82 m_helpDialog = NULL;
83 m_helpFrame = NULL;
b854b7b8
VS
84}
85
b4414c1f 86void wxHtmlHelpController::OnCloseFrame(wxCloseEvent& evt)
b854b7b8 87{
3755cfa6
JS
88 if (m_Config)
89 WriteCustomization(m_Config, m_ConfigRoot);
664d1729 90
b4414c1f 91 evt.Skip();
b854b7b8 92
b4414c1f 93 OnQuit();
b854b7b8 94
664d1729
VS
95 if ( m_helpWindow )
96 m_helpWindow->SetController(NULL);
3755cfa6 97 m_helpWindow = NULL;
f3e156ef
JS
98 m_helpDialog = NULL;
99 m_helpFrame = NULL;
b4414c1f 100}
b854b7b8 101
8ec2b484
HH
102void wxHtmlHelpController::SetTitleFormat(const wxString& title)
103{
104 m_titleFormat = title;
3755cfa6
JS
105 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
106 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
107 if (frame)
108 {
109 frame->SetTitleFormat(title);
110 }
111 else if (dialog)
112 dialog->SetTitleFormat(title);
8ec2b484
HH
113}
114
3755cfa6
JS
115// Find the top-most parent window
116wxWindow* wxHtmlHelpController::FindTopLevelWindow()
117{
118 wxWindow* parent = m_helpWindow;
119 while (parent && !parent->IsTopLevel())
120 {
121 parent = parent->GetParent();
122 }
123 return parent;
124}
d5bb85a0 125
fcf77487
VS
126bool wxHtmlHelpController::AddBook(const wxFileName& book_file, bool show_wait_msg)
127{
128 return AddBook(wxFileSystem::FileNameToURL(book_file), show_wait_msg);
129}
130
8ec2b484
HH
131bool wxHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg)
132{
133 wxBusyCursor cur;
134#if wxUSE_BUSYINFO
69941f05 135 wxBusyInfo* busy = NULL;
8ec2b484 136 wxString info;
33ac7e6f 137 if (show_wait_msg)
4f9297b0 138 {
d5bb85a0
VS
139 info.Printf(_("Adding book %s"), book.c_str());
140 busy = new wxBusyInfo(info);
8ec2b484
HH
141 }
142#endif
143 bool retval = m_helpData.AddBook(book);
144#if wxUSE_BUSYINFO
145 if (show_wait_msg)
d5bb85a0 146 delete busy;
132a5928
WS
147#else
148 wxUnusedVar(show_wait_msg);
33ac7e6f 149#endif
3755cfa6
JS
150 if (m_helpWindow)
151 m_helpWindow->RefreshLists();
8ec2b484
HH
152 return retval;
153}
154
3755cfa6 155wxHtmlHelpFrame* wxHtmlHelpController::CreateHelpFrame(wxHtmlHelpData *data)
b854b7b8 156{
3755cfa6
JS
157 wxHtmlHelpFrame* frame = new wxHtmlHelpFrame(data);
158 frame->SetController(this);
18fc6059 159 frame->Create(m_parentWindow, -1, wxEmptyString, m_FrameStyle, m_Config, m_ConfigRoot);
3755cfa6 160 frame->SetTitleFormat(m_titleFormat);
f3e156ef 161 m_helpFrame = frame;
3755cfa6 162 return frame;
b854b7b8
VS
163}
164
3755cfa6
JS
165wxHtmlHelpDialog* wxHtmlHelpController::CreateHelpDialog(wxHtmlHelpData *data)
166{
167 wxHtmlHelpDialog* dialog = new wxHtmlHelpDialog(data);
168 dialog->SetController(this);
169 dialog->SetTitleFormat(m_titleFormat);
170 dialog->Create(m_parentWindow, -1, wxEmptyString, m_FrameStyle);
f3e156ef 171 m_helpDialog = dialog;
3755cfa6
JS
172 return dialog;
173}
b854b7b8 174
3755cfa6 175wxWindow* wxHtmlHelpController::CreateHelpWindow()
8ec2b484 176{
3755cfa6 177 if (m_helpWindow)
4f9297b0 178 {
3755cfa6
JS
179 if (m_FrameStyle & wxHF_EMBEDDED)
180 return m_helpWindow;
181
182 wxWindow* topLevelWindow = FindTopLevelWindow();
183 if (topLevelWindow)
184 topLevelWindow->Raise();
185 return m_helpWindow;
8ec2b484 186 }
d5bb85a0 187
33ac7e6f 188 if (m_Config == NULL)
74accc50 189 {
d1da8872 190 m_Config = wxConfigBase::Get(false);
74accc50 191 if (m_Config != NULL)
e7d9c398 192 m_ConfigRoot = _T("wxWindows/wxHtmlHelpController");
74accc50
VS
193 }
194
3755cfa6
JS
195 if (m_FrameStyle & wxHF_DIALOG)
196 {
197 wxHtmlHelpDialog* dialog = CreateHelpDialog(&m_helpData);
198 m_helpWindow = dialog->GetHelpWindow();
199 }
200 else if ((m_FrameStyle & wxHF_EMBEDDED) && m_parentWindow)
201 {
202 m_helpWindow = new wxHtmlHelpWindow(m_parentWindow, -1, wxDefaultPosition, wxDefaultSize,
203 wxTAB_TRAVERSAL|wxNO_BORDER, m_FrameStyle, &m_helpData);
204 }
205 else // wxHF_FRAME
206 {
207 wxHtmlHelpFrame* frame = CreateHelpFrame(&m_helpData);
208 m_helpWindow = frame->GetHelpWindow();
209 frame->Show(true);
210 }
7e521b01 211
3755cfa6 212 return m_helpWindow;
8ec2b484
HH
213}
214
215void wxHtmlHelpController::ReadCustomization(wxConfigBase* cfg, const wxString& path)
216{
217 /* should not be called by the user; call UseConfig, and the controller
218 * will do the rest */
3755cfa6
JS
219 if (m_helpWindow && cfg)
220 m_helpWindow->ReadCustomization(cfg, path);
8ec2b484
HH
221}
222
223void wxHtmlHelpController::WriteCustomization(wxConfigBase* cfg, const wxString& path)
224{
225 /* typically called by the controllers OnCloseFrame handler */
3755cfa6
JS
226 if (m_helpWindow && cfg)
227 m_helpWindow->WriteCustomization(cfg, path);
8ec2b484
HH
228}
229
721ab905
VS
230void wxHtmlHelpController::UseConfig(wxConfigBase *config, const wxString& rootpath)
231{
232 m_Config = config;
233 m_ConfigRoot = rootpath;
3755cfa6 234 if (m_helpWindow) m_helpWindow->UseConfig(config, rootpath);
721ab905
VS
235 ReadCustomization(config, rootpath);
236}
237
b4414c1f
JS
238//// Backward compatibility with wxHelpController API
239
240bool wxHtmlHelpController::Initialize(const wxString& file)
241{
242 wxString dir, filename, ext;
243 wxSplitPath(file, & dir, & filename, & ext);
244
ddc80eb4 245 if (!dir.empty())
0222a60b 246 dir = dir + wxFILE_SEP_PATH;
b4414c1f
JS
247
248 // Try to find a suitable file
e81910e0 249 wxString actualFilename = dir + filename + wxString(wxT(".zip"));
b4414c1f
JS
250 if (!wxFileExists(actualFilename))
251 {
e81910e0 252 actualFilename = dir + filename + wxString(wxT(".htb"));
b4414c1f
JS
253 if (!wxFileExists(actualFilename))
254 {
e81910e0 255 actualFilename = dir + filename + wxString(wxT(".hhp"));
b4414c1f 256 if (!wxFileExists(actualFilename))
3527f29c
VS
257 {
258#if wxUSE_LIBMSPACK
259 actualFilename = dir + filename + wxString(wxT(".chm"));
260 if (!wxFileExists(actualFilename))
261#endif
262 return false;
263 }
b4414c1f
JS
264 }
265 }
fcf77487 266 return AddBook(wxFileName(actualFilename));
b4414c1f
JS
267}
268
269bool wxHtmlHelpController::LoadFile(const wxString& WXUNUSED(file))
270{
271 // Don't reload the file or we'll have it appear again, presumably.
d1da8872 272 return true;
b4414c1f
JS
273}
274
275bool wxHtmlHelpController::DisplaySection(int sectionNo)
276{
277 return Display(sectionNo);
278}
279
673dfcfa
JS
280bool wxHtmlHelpController::DisplayTextPopup(const wxString& text, const wxPoint& WXUNUSED(pos))
281{
f38bcae5 282#if wxUSE_TIPWINDOW
673dfcfa
JS
283 static wxTipWindow* s_tipWindow = NULL;
284
285 if (s_tipWindow)
286 {
287 // Prevent s_tipWindow being nulled in OnIdle,
288 // thereby removing the chance for the window to be closed by ShowHelp
289 s_tipWindow->SetTipWindowPtr(NULL);
290 s_tipWindow->Close();
291 }
292 s_tipWindow = NULL;
293
294 if ( !text.empty() )
295 {
296 s_tipWindow = new wxTipWindow(wxTheApp->GetTopWindow(), text, 100, & s_tipWindow);
297
d1da8872 298 return true;
673dfcfa 299 }
ddc80eb4
WS
300#else
301 wxUnusedVar(text);
f38bcae5
VZ
302#endif // wxUSE_TIPWINDOW
303
d1da8872 304 return false;
673dfcfa
JS
305}
306
3755cfa6
JS
307void wxHtmlHelpController::SetHelpWindow(wxHtmlHelpWindow* helpWindow)
308{
309 m_helpWindow = helpWindow;
310 if (helpWindow)
311 helpWindow->SetController(this);
312}
313
b4414c1f
JS
314void wxHtmlHelpController::SetFrameParameters(const wxString& title,
315 const wxSize& size,
316 const wxPoint& pos,
317 bool WXUNUSED(newFrameEachTime))
318{
319 SetTitleFormat(title);
3755cfa6
JS
320 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
321 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
322 if (frame)
323 frame->SetSize(pos.x, pos.y, size.x, size.y);
324 else if (dialog)
325 dialog->SetSize(pos.x, pos.y, size.x, size.y);
b4414c1f
JS
326}
327
328wxFrame* wxHtmlHelpController::GetFrameParameters(wxSize *size,
329 wxPoint *pos,
330 bool *newFrameEachTime)
331{
332 if (newFrameEachTime)
d1da8872 333 (* newFrameEachTime) = false;
3755cfa6
JS
334
335 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
336 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
337 if (frame)
338 {
339 if (size)
340 (* size) = frame->GetSize();
341 if (pos)
342 (* pos) = frame->GetPosition();
343 return frame;
344 }
345 else if (dialog)
346 {
347 if (size)
348 (* size) = dialog->GetSize();
349 if (pos)
350 (* pos) = dialog->GetPosition();
351 return NULL;
352 }
353 return NULL;
b4414c1f
JS
354}
355
356bool wxHtmlHelpController::Quit()
357{
358 DestroyHelpWindow();
d1da8872 359 return true;
b4414c1f
JS
360}
361
5152b0e5
JS
362// Make the help controller's frame 'modal' if
363// needed
3755cfa6 364void wxHtmlHelpController::MakeModalIfNeeded()
5152b0e5 365{
3755cfa6 366 if ((m_FrameStyle & wxHF_EMBEDDED) == 0)
5152b0e5 367 {
3755cfa6
JS
368 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
369 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
370 if (frame)
371 frame->AddGrabIfNeeded();
372 else if (dialog && (m_FrameStyle & wxHF_MODAL))
373 {
374 dialog->ShowModal();
375 }
5152b0e5 376 }
5152b0e5
JS
377}
378
379bool wxHtmlHelpController::Display(const wxString& x)
380{
381 CreateHelpWindow();
3755cfa6
JS
382 bool success = m_helpWindow->Display(x);
383 MakeModalIfNeeded();
d1da8872 384 return success;
5152b0e5
JS
385}
386
387bool wxHtmlHelpController::Display(int id)
388{
389 CreateHelpWindow();
3755cfa6
JS
390 bool success = m_helpWindow->Display(id);
391 MakeModalIfNeeded();
5152b0e5
JS
392 return success;
393}
394
395bool wxHtmlHelpController::DisplayContents()
396{
397 CreateHelpWindow();
3755cfa6
JS
398 bool success = m_helpWindow->DisplayContents();
399 MakeModalIfNeeded();
5152b0e5
JS
400 return success;
401}
402
403bool wxHtmlHelpController::DisplayIndex()
404{
405 CreateHelpWindow();
3755cfa6
JS
406 bool success = m_helpWindow->DisplayIndex();
407 MakeModalIfNeeded();
5152b0e5
JS
408 return success;
409}
410
69b5cec2
VS
411bool wxHtmlHelpController::KeywordSearch(const wxString& keyword,
412 wxHelpSearchMode mode)
5152b0e5
JS
413{
414 CreateHelpWindow();
3755cfa6
JS
415 bool success = m_helpWindow->KeywordSearch(keyword, mode);
416 MakeModalIfNeeded();
5152b0e5
JS
417 return success;
418}
419
3755cfa6
JS
420/*
421 * wxHtmlModalHelp
422 * A convenience class, to use like this:
423 *
424 * wxHtmlModalHelp help(parent, helpFile, topic);
425 */
426
427wxHtmlModalHelp::wxHtmlModalHelp(wxWindow* parent, const wxString& helpFile, const wxString& topic, int style)
428{
429 // Force some mandatory styles
430 style |= wxHF_DIALOG | wxHF_MODAL;
431
432 wxHtmlHelpController controller(style, parent);
433 controller.Initialize(helpFile);
434
435 if (topic.IsEmpty())
436 controller.DisplayContents();
437 else
438 controller.DisplaySection(topic);
439}
440
3379ed37
VZ
441#endif // wxUSE_WXHTML_HELP
442