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