]> git.saurik.com Git - wxWidgets.git/blob - src/html/helpctrl.cpp
avoid multiple reallocations in wxString::PrintfV() if vsnprintf() returns the total...
[wxWidgets.git] / src / html / helpctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpctrl.cpp
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
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_WXHTML_HELP
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/intl.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/html/helpctrl.h"
27 #include "wx/busyinfo.h"
28
29 #ifdef __WXGTK__
30 // for the hack in AddGrabIfNeeded()
31 #include "wx/dialog.h"
32 #endif // __WXGTK__
33
34 #if wxUSE_HELP
35 #include "wx/tipwin.h"
36 #endif
37
38
39 #if wxUSE_LIBMSPACK
40 #include "wx/html/forcelnk.h"
41 FORCE_LINK(wxhtml_chm_support)
42 #endif
43
44 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpController, wxHelpControllerBase)
45
46 wxHtmlHelpController::wxHtmlHelpController(int style, wxWindow* parentWindow):
47 wxHelpControllerBase(parentWindow)
48 {
49 m_helpFrame = NULL;
50 m_Config = NULL;
51 m_ConfigRoot = wxEmptyString;
52 m_titleFormat = _("Help: %s");
53 m_FrameStyle = style;
54 }
55
56 wxHtmlHelpController::~wxHtmlHelpController()
57 {
58 if (m_Config)
59 WriteCustomization(m_Config, m_ConfigRoot);
60 if (m_helpFrame)
61 DestroyHelpWindow();
62 }
63
64
65 void wxHtmlHelpController::DestroyHelpWindow()
66 {
67 //if (m_Config) WriteCustomization(m_Config, m_ConfigRoot);
68 if (m_helpFrame)
69 m_helpFrame->Destroy();
70 }
71
72 void wxHtmlHelpController::OnCloseFrame(wxCloseEvent& evt)
73 {
74 evt.Skip();
75
76 OnQuit();
77
78 m_helpFrame->SetController((wxHelpControllerBase*) NULL);
79 m_helpFrame = NULL;
80 }
81
82 void wxHtmlHelpController::SetTitleFormat(const wxString& title)
83 {
84 m_titleFormat = title;
85 if (m_helpFrame)
86 m_helpFrame->SetTitleFormat(title);
87 }
88
89
90 bool wxHtmlHelpController::AddBook(const wxFileName& book_file, bool show_wait_msg)
91 {
92 return AddBook(wxFileSystem::FileNameToURL(book_file), show_wait_msg);
93 }
94
95 bool wxHtmlHelpController::AddBook(const wxString& book, bool show_wait_msg)
96 {
97 wxBusyCursor cur;
98 #if wxUSE_BUSYINFO
99 wxBusyInfo* busy = NULL;
100 wxString info;
101 if (show_wait_msg)
102 {
103 info.Printf(_("Adding book %s"), book.c_str());
104 busy = new wxBusyInfo(info);
105 }
106 #endif
107 bool retval = m_helpData.AddBook(book);
108 #if wxUSE_BUSYINFO
109 if (show_wait_msg)
110 delete busy;
111 #else
112 wxUnusedVar(show_wait_msg);
113 #endif
114 if (m_helpFrame)
115 m_helpFrame->RefreshLists();
116 return retval;
117 }
118
119
120
121 wxHtmlHelpFrame *wxHtmlHelpController::CreateHelpFrame(wxHtmlHelpData *data)
122 {
123 return new wxHtmlHelpFrame(data);
124 }
125
126
127 void wxHtmlHelpController::CreateHelpWindow()
128 {
129 if (m_helpFrame)
130 {
131 m_helpFrame->Raise();
132 return ;
133 }
134
135 if (m_Config == NULL)
136 {
137 m_Config = wxConfigBase::Get(false);
138 if (m_Config != NULL)
139 m_ConfigRoot = _T("wxWindows/wxHtmlHelpController");
140 }
141
142 m_helpFrame = CreateHelpFrame(&m_helpData);
143 m_helpFrame->SetController(this);
144
145 if (m_Config)
146 m_helpFrame->UseConfig(m_Config, m_ConfigRoot);
147
148 m_helpFrame->Create(GetParentWindow(), wxID_HTML_HELPFRAME, wxEmptyString, m_FrameStyle);
149 m_helpFrame->SetTitleFormat(m_titleFormat);
150
151 m_helpFrame->Show(true);
152 }
153
154 void wxHtmlHelpController::ReadCustomization(wxConfigBase* cfg, const wxString& path)
155 {
156 /* should not be called by the user; call UseConfig, and the controller
157 * will do the rest */
158 if (m_helpFrame && cfg)
159 m_helpFrame->ReadCustomization(cfg, path);
160 }
161
162 void wxHtmlHelpController::WriteCustomization(wxConfigBase* cfg, const wxString& path)
163 {
164 /* typically called by the controllers OnCloseFrame handler */
165 if (m_helpFrame && cfg)
166 m_helpFrame->WriteCustomization(cfg, path);
167 }
168
169 void wxHtmlHelpController::UseConfig(wxConfigBase *config, const wxString& rootpath)
170 {
171 m_Config = config;
172 m_ConfigRoot = rootpath;
173 if (m_helpFrame) m_helpFrame->UseConfig(config, rootpath);
174 ReadCustomization(config, rootpath);
175 }
176
177 //// Backward compatibility with wxHelpController API
178
179 bool wxHtmlHelpController::Initialize(const wxString& file)
180 {
181 wxString dir, filename, ext;
182 wxSplitPath(file, & dir, & filename, & ext);
183
184 if (!dir.empty())
185 dir = dir + wxFILE_SEP_PATH;
186
187 // Try to find a suitable file
188 wxString actualFilename = dir + filename + wxString(wxT(".zip"));
189 if (!wxFileExists(actualFilename))
190 {
191 actualFilename = dir + filename + wxString(wxT(".htb"));
192 if (!wxFileExists(actualFilename))
193 {
194 actualFilename = dir + filename + wxString(wxT(".hhp"));
195 if (!wxFileExists(actualFilename))
196 {
197 #if wxUSE_LIBMSPACK
198 actualFilename = dir + filename + wxString(wxT(".chm"));
199 if (!wxFileExists(actualFilename))
200 #endif
201 return false;
202 }
203 }
204 }
205 return AddBook(wxFileName(actualFilename));
206 }
207
208 bool wxHtmlHelpController::LoadFile(const wxString& WXUNUSED(file))
209 {
210 // Don't reload the file or we'll have it appear again, presumably.
211 return true;
212 }
213
214 bool wxHtmlHelpController::DisplaySection(int sectionNo)
215 {
216 return Display(sectionNo);
217 }
218
219 bool wxHtmlHelpController::DisplayTextPopup(const wxString& text, const wxPoint& WXUNUSED(pos))
220 {
221 #if wxUSE_TIPWINDOW
222 static wxTipWindow* s_tipWindow = NULL;
223
224 if (s_tipWindow)
225 {
226 // Prevent s_tipWindow being nulled in OnIdle,
227 // thereby removing the chance for the window to be closed by ShowHelp
228 s_tipWindow->SetTipWindowPtr(NULL);
229 s_tipWindow->Close();
230 }
231 s_tipWindow = NULL;
232
233 if ( !text.empty() )
234 {
235 s_tipWindow = new wxTipWindow(wxTheApp->GetTopWindow(), text, 100, & s_tipWindow);
236
237 return true;
238 }
239 #else
240 wxUnusedVar(text);
241 #endif // wxUSE_TIPWINDOW
242
243 return false;
244 }
245
246 void wxHtmlHelpController::SetFrameParameters(const wxString& title,
247 const wxSize& size,
248 const wxPoint& pos,
249 bool WXUNUSED(newFrameEachTime))
250 {
251 SetTitleFormat(title);
252 if (m_helpFrame)
253 {
254 m_helpFrame->SetSize(pos.x, pos.y, size.x, size.y);
255 }
256 }
257
258 wxFrame* wxHtmlHelpController::GetFrameParameters(wxSize *size,
259 wxPoint *pos,
260 bool *newFrameEachTime)
261 {
262 if (newFrameEachTime)
263 (* newFrameEachTime) = false;
264 if (size && m_helpFrame)
265 (* size) = m_helpFrame->GetSize();
266 if (pos && m_helpFrame)
267 (* pos) = m_helpFrame->GetPosition();
268 return m_helpFrame;
269 }
270
271 bool wxHtmlHelpController::Quit()
272 {
273 DestroyHelpWindow();
274 return true;
275 }
276
277 // Make the help controller's frame 'modal' if
278 // needed
279 void wxHtmlHelpController::AddGrabIfNeeded()
280 {
281 // So far, wxGTK only
282 #ifdef __WXGTK__
283 bool needGrab = false;
284
285 // Check if there are any modal windows present,
286 // in which case we need to add a grab.
287 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
288 node;
289 node = node->GetNext() )
290 {
291 wxWindow *win = node->GetData();
292 wxDialog *dialog = wxDynamicCast(win, wxDialog);
293
294 if (dialog && dialog->IsModal())
295 needGrab = true;
296 }
297
298 if (needGrab && m_helpFrame)
299 m_helpFrame->AddGrab();
300 #endif // __WXGTK__
301 }
302
303 bool wxHtmlHelpController::Display(const wxString& x)
304 {
305 CreateHelpWindow();
306 bool success = m_helpFrame->Display(x);
307 AddGrabIfNeeded();
308 return success;
309 }
310
311 bool wxHtmlHelpController::Display(int id)
312 {
313 CreateHelpWindow();
314 bool success = m_helpFrame->Display(id);
315 AddGrabIfNeeded();
316 return success;
317 }
318
319 bool wxHtmlHelpController::DisplayContents()
320 {
321 CreateHelpWindow();
322 bool success = m_helpFrame->DisplayContents();
323 AddGrabIfNeeded();
324 return success;
325 }
326
327 bool wxHtmlHelpController::DisplayIndex()
328 {
329 CreateHelpWindow();
330 bool success = m_helpFrame->DisplayIndex();
331 AddGrabIfNeeded();
332 return success;
333 }
334
335 bool wxHtmlHelpController::KeywordSearch(const wxString& keyword,
336 wxHelpSearchMode mode)
337 {
338 CreateHelpWindow();
339 bool success = m_helpFrame->KeywordSearch(keyword, mode);
340 AddGrabIfNeeded();
341 return success;
342 }
343
344 #endif // wxUSE_WXHTML_HELP
345