1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlHelpController
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
26 #include "wx/busyinfo.h"
27 #include "wx/html/helpctrl.h"
28 #include "wx/html/helpwnd.h"
29 #include "wx/html/helpfrm.h"
30 #include "wx/html/helpdlg.h"
33 #include "wx/tipwin.h"
37 #include "wx/html/forcelnk.h"
38 FORCE_LINK(wxhtml_chm_support)
41 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpController, wxHelpControllerBase)
43 wxHtmlHelpController::wxHtmlHelpController(int style, wxWindow* parentWindow):
44 wxHelpControllerBase(parentWindow)
50 m_ConfigRoot = wxEmptyString;
51 m_titleFormat = _("Help: %s");
55 wxHtmlHelpController::~wxHtmlHelpController()
58 WriteCustomization(m_Config, m_ConfigRoot);
64 void wxHtmlHelpController::DestroyHelpWindow()
66 if (m_FrameStyle & wxHF_EMBEDDED)
69 // Find top-most parent window
71 wxWindow* parent = FindTopLevelWindow();
74 wxDialog* dialog = wxDynamicCast(parent, wxDialog);
75 if (dialog && dialog->IsModal())
77 dialog->EndModal(wxID_OK);
86 void wxHtmlHelpController::OnCloseFrame(wxCloseEvent& evt)
89 WriteCustomization(m_Config, m_ConfigRoot);
95 m_helpWindow->SetController(NULL);
101 void wxHtmlHelpController::SetTitleFormat(const wxString& title)
103 m_titleFormat = title;
104 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
105 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
108 frame->SetTitleFormat(title);
111 dialog->SetTitleFormat(title);
114 // Find the top-most parent window
115 wxWindow* wxHtmlHelpController::FindTopLevelWindow()
117 wxWindow* parent = m_helpWindow;
118 while (parent && !parent->IsTopLevel())
120 parent = parent->GetParent();
125 bool wxHtmlHelpController::AddBook(const wxFileName& book_file, bool show_wait_msg)
127 return AddBook(wxFileSystem::FileNameToURL(book_file), show_wait_msg);
130 bool wxHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg)
134 wxBusyInfo* busy = NULL;
138 info.Printf(_("Adding book %s"), book.c_str());
139 busy = new wxBusyInfo(info);
142 bool retval = m_helpData.AddBook(book);
147 wxUnusedVar(show_wait_msg);
150 m_helpWindow->RefreshLists();
154 wxHtmlHelpFrame* wxHtmlHelpController::CreateHelpFrame(wxHtmlHelpData *data)
156 wxHtmlHelpFrame* frame = new wxHtmlHelpFrame(data);
157 frame->SetController(this);
158 frame->SetTitleFormat(m_titleFormat);
159 frame->Create(m_parentWindow, -1, wxEmptyString, m_FrameStyle);
164 wxHtmlHelpDialog* wxHtmlHelpController::CreateHelpDialog(wxHtmlHelpData *data)
166 wxHtmlHelpDialog* dialog = new wxHtmlHelpDialog(data);
167 dialog->SetController(this);
168 dialog->SetTitleFormat(m_titleFormat);
169 dialog->Create(m_parentWindow, -1, wxEmptyString, m_FrameStyle);
170 m_helpDialog = dialog;
174 wxWindow* wxHtmlHelpController::CreateHelpWindow()
178 if (m_FrameStyle & wxHF_EMBEDDED)
181 wxWindow* topLevelWindow = FindTopLevelWindow();
183 topLevelWindow->Raise();
187 if (m_Config == NULL)
189 m_Config = wxConfigBase::Get(false);
190 if (m_Config != NULL)
191 m_ConfigRoot = _T("wxWindows/wxHtmlHelpController");
194 if (m_FrameStyle & wxHF_DIALOG)
196 wxHtmlHelpDialog* dialog = CreateHelpDialog(&m_helpData);
197 m_helpWindow = dialog->GetHelpWindow();
199 else if ((m_FrameStyle & wxHF_EMBEDDED) && m_parentWindow)
201 m_helpWindow = new wxHtmlHelpWindow(m_parentWindow, -1, wxDefaultPosition, wxDefaultSize,
202 wxTAB_TRAVERSAL|wxNO_BORDER, m_FrameStyle, &m_helpData);
206 wxHtmlHelpFrame* frame = CreateHelpFrame(&m_helpData);
207 m_helpWindow = frame->GetHelpWindow();
214 void wxHtmlHelpController::ReadCustomization(wxConfigBase* cfg, const wxString& path)
216 /* should not be called by the user; call UseConfig, and the controller
217 * will do the rest */
218 if (m_helpWindow && cfg)
219 m_helpWindow->ReadCustomization(cfg, path);
222 void wxHtmlHelpController::WriteCustomization(wxConfigBase* cfg, const wxString& path)
224 /* typically called by the controllers OnCloseFrame handler */
225 if (m_helpWindow && cfg)
226 m_helpWindow->WriteCustomization(cfg, path);
229 void wxHtmlHelpController::UseConfig(wxConfigBase *config, const wxString& rootpath)
232 m_ConfigRoot = rootpath;
233 if (m_helpWindow) m_helpWindow->UseConfig(config, rootpath);
234 ReadCustomization(config, rootpath);
237 //// Backward compatibility with wxHelpController API
239 bool wxHtmlHelpController::Initialize(const wxString& file)
241 wxString dir, filename, ext;
242 wxSplitPath(file, & dir, & filename, & ext);
245 dir = dir + wxFILE_SEP_PATH;
247 // Try to find a suitable file
248 wxString actualFilename = dir + filename + wxString(wxT(".zip"));
249 if (!wxFileExists(actualFilename))
251 actualFilename = dir + filename + wxString(wxT(".htb"));
252 if (!wxFileExists(actualFilename))
254 actualFilename = dir + filename + wxString(wxT(".hhp"));
255 if (!wxFileExists(actualFilename))
258 actualFilename = dir + filename + wxString(wxT(".chm"));
259 if (!wxFileExists(actualFilename))
265 return AddBook(wxFileName(actualFilename));
268 bool wxHtmlHelpController::LoadFile(const wxString& WXUNUSED(file))
270 // Don't reload the file or we'll have it appear again, presumably.
274 bool wxHtmlHelpController::DisplaySection(int sectionNo)
276 return Display(sectionNo);
279 bool wxHtmlHelpController::DisplayTextPopup(const wxString& text, const wxPoint& WXUNUSED(pos))
282 static wxTipWindow* s_tipWindow = NULL;
286 // Prevent s_tipWindow being nulled in OnIdle,
287 // thereby removing the chance for the window to be closed by ShowHelp
288 s_tipWindow->SetTipWindowPtr(NULL);
289 s_tipWindow->Close();
295 s_tipWindow = new wxTipWindow(wxTheApp->GetTopWindow(), text, 100, & s_tipWindow);
301 #endif // wxUSE_TIPWINDOW
306 void wxHtmlHelpController::SetHelpWindow(wxHtmlHelpWindow* helpWindow)
308 m_helpWindow = helpWindow;
310 helpWindow->SetController(this);
313 void wxHtmlHelpController::SetFrameParameters(const wxString& title,
316 bool WXUNUSED(newFrameEachTime))
318 SetTitleFormat(title);
319 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
320 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
322 frame->SetSize(pos.x, pos.y, size.x, size.y);
324 dialog->SetSize(pos.x, pos.y, size.x, size.y);
327 wxFrame* wxHtmlHelpController::GetFrameParameters(wxSize *size,
329 bool *newFrameEachTime)
331 if (newFrameEachTime)
332 (* newFrameEachTime) = false;
334 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
335 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
339 (* size) = frame->GetSize();
341 (* pos) = frame->GetPosition();
347 (* size) = dialog->GetSize();
349 (* pos) = dialog->GetPosition();
355 bool wxHtmlHelpController::Quit()
361 // Make the help controller's frame 'modal' if
363 void wxHtmlHelpController::MakeModalIfNeeded()
365 if ((m_FrameStyle & wxHF_EMBEDDED) == 0)
367 wxHtmlHelpFrame* frame = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpFrame);
368 wxHtmlHelpDialog* dialog = wxDynamicCast(FindTopLevelWindow(), wxHtmlHelpDialog);
370 frame->AddGrabIfNeeded();
371 else if (dialog && (m_FrameStyle & wxHF_MODAL))
378 bool wxHtmlHelpController::Display(const wxString& x)
381 bool success = m_helpWindow->Display(x);
386 bool wxHtmlHelpController::Display(int id)
389 bool success = m_helpWindow->Display(id);
394 bool wxHtmlHelpController::DisplayContents()
397 bool success = m_helpWindow->DisplayContents();
402 bool wxHtmlHelpController::DisplayIndex()
405 bool success = m_helpWindow->DisplayIndex();
410 bool wxHtmlHelpController::KeywordSearch(const wxString& keyword,
411 wxHelpSearchMode mode)
414 bool success = m_helpWindow->KeywordSearch(keyword, mode);
421 * A convenience class, to use like this:
423 * wxHtmlModalHelp help(parent, helpFile, topic);
426 wxHtmlModalHelp::wxHtmlModalHelp(wxWindow* parent, const wxString& helpFile, const wxString& topic, int style)
428 // Force some mandatory styles
429 style |= wxHF_DIALOG | wxHF_MODAL;
431 wxHtmlHelpController controller(style, parent);
432 controller.Initialize(helpFile);
435 controller.DisplayContents();
437 controller.DisplaySection(topic);
440 #endif // wxUSE_WXHTML_HELP