]>
Commit | Line | Data |
---|---|---|
f96b60aa | 1 | ///////////////////////////////////////////////////////////////////////////// |
7fc65a03 | 2 | // Name: src/generic/helpext.cpp |
77ffb593 | 3 | // Purpose: an external help controller for wxWidgets |
f96b60aa VZ |
4 | // Author: Karsten Ballueder |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Karsten Ballueder | |
65571936 | 9 | // Licence: wxWindows licence |
f96b60aa VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f96b60aa VZ |
12 | #include "wx/wxprec.h" |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
7e00fd89 | 18 | #if wxUSE_HELP && !defined(__WXWINCE__) && (!defined(__WXMAC__) || defined(__WXMAC_OSX__)) |
31528cd3 | 19 | |
f96b60aa VZ |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/setup.h" | |
22 | #include "wx/string.h" | |
23 | #include "wx/utils.h" | |
24 | #include "wx/list.h" | |
25 | #include "wx/intl.h" | |
29d0a26e MB |
26 | #include "wx/msgdlg.h" |
27 | #include "wx/choicdlg.h" | |
1020103f | 28 | #include "wx/log.h" |
f96b60aa VZ |
29 | #endif |
30 | ||
31 | #include "wx/helpbase.h" | |
32 | #include "wx/generic/helpext.h" | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <ctype.h> | |
36 | #include <sys/stat.h> | |
37 | ||
004fd0c8 | 38 | #if !defined(__WINDOWS__) && !defined(__OS2__) |
f96b60aa VZ |
39 | #include <unistd.h> |
40 | #endif | |
e9aad10a | 41 | |
583f6c5c JS |
42 | #ifdef __WXMSW__ |
43 | #include <windows.h> | |
7acf6a92 | 44 | #include "wx/msw/winundef.h" |
583f6c5c JS |
45 | #endif |
46 | ||
69108ccb JS |
47 | // ---------------------------------------------------------------------------- |
48 | // constants | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | /// Name for map file. | |
52 | #define WXEXTHELP_MAPFILE _T("wxhelp.map") | |
53 | /// Maximum line length in map file. | |
54 | #define WXEXTHELP_BUFLEN 512 | |
55 | /// Character introducing comments/documentation field in map file. | |
56 | #define WXEXTHELP_COMMENTCHAR ';' | |
57 | ||
58 | #define CONTENTS_ID 0 | |
59 | ||
60 | IMPLEMENT_CLASS(wxExtHelpController, wxHelpControllerBase) | |
31528cd3 | 61 | |
aa0ffd1d | 62 | /// Name of environment variable to set help browser. |
2b5f62a0 | 63 | #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER") |
aa0ffd1d | 64 | /// Is browser a netscape browser? |
2b5f62a0 | 65 | #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS") |
aa0ffd1d | 66 | |
e9aad10a KB |
67 | /** |
68 | This class implements help via an external browser. | |
69 | It requires the name of a directory containing the documentation | |
70 | and a file mapping numerical Section numbers to relative URLS. | |
71 | */ | |
72 | ||
3db52265 JS |
73 | wxExtHelpController::wxExtHelpController(wxWindow* parentWindow): |
74 | wxHelpControllerBase(parentWindow) | |
e9aad10a | 75 | { |
69108ccb JS |
76 | m_MapList = (wxList*) NULL; |
77 | m_NumOfEntries = 0; | |
e9aad10a KB |
78 | m_BrowserName = WXEXTHELP_DEFAULTBROWSER; |
79 | m_BrowserIsNetscape = WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE; | |
80 | ||
2b5f62a0 | 81 | wxChar *browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSER); |
e9aad10a KB |
82 | if(browser) |
83 | { | |
84 | m_BrowserName = browser; | |
2b5f62a0 VZ |
85 | browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE); |
86 | m_BrowserIsNetscape = browser && (wxAtoi(browser) != 0); | |
e9aad10a KB |
87 | } |
88 | } | |
89 | ||
69108ccb JS |
90 | wxExtHelpController::~wxExtHelpController() |
91 | { | |
92 | DeleteList(); | |
93 | } | |
e9aad10a | 94 | |
69108ccb | 95 | void wxExtHelpController::SetBrowser(const wxString& browsername, bool isNetscape) |
e9aad10a KB |
96 | { |
97 | m_BrowserName = browsername; | |
98 | m_BrowserIsNetscape = isNetscape; | |
99 | } | |
100 | ||
33b64e6f JS |
101 | // Set viewer: new, generic name for SetBrowser |
102 | void wxExtHelpController::SetViewer(const wxString& viewer, long flags) | |
103 | { | |
104 | SetBrowser(viewer, ((flags & wxHELP_NETSCAPE) == wxHELP_NETSCAPE)); | |
105 | } | |
106 | ||
e9aad10a | 107 | bool |
f6bcfd97 | 108 | wxExtHelpController::DisplayHelp(const wxString &relativeURL) |
e9aad10a KB |
109 | { |
110 | wxBusyCursor b; // display a busy cursor | |
6e5fefdf KB |
111 | |
112 | ||
004fd0c8 | 113 | #if defined(__WXMSW__) |
f96b60aa VZ |
114 | wxString url; |
115 | url << m_MapFile << '\\' << relativeURL.BeforeFirst('#'); | |
f6bcfd97 | 116 | bool bOk = (int)ShellExecute(NULL, wxT("open"), url.c_str(), |
f96b60aa | 117 | NULL, NULL, SW_SHOWNORMAL ) > 32; |
6e5fefdf KB |
118 | if ( !bOk ) |
119 | { | |
120 | wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str()); | |
121 | return false; | |
122 | } | |
b13f5fbf | 123 | |
17ff4e8f | 124 | return true; |
7fc65a03 | 125 | #elif defined(__OS2__) |
b13f5fbf | 126 | |
004fd0c8 DW |
127 | wxString url; |
128 | url << m_MapFile << '\\' << relativeURL.BeforeFirst('#'); | |
129 | // will have to fix for OS/2, later.....DW | |
130 | // bool bOk = (int)ShellExecute(NULL, "open", url, | |
131 | // NULL, NULL, SW_SHOWNORMAL ) > 32; | |
132 | // if ( !bOk ) | |
133 | // { | |
134 | // wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str()); | |
135 | // return false; | |
136 | // } | |
137 | // else | |
ca65c044 | 138 | return true; |
b13f5fbf VS |
139 | |
140 | #elif defined(__DOS__) | |
141 | ||
142 | wxString command; | |
143 | command = m_BrowserName; | |
144 | command << wxT(" file://") | |
145 | << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL; | |
146 | return wxExecute(command) != 0; | |
147 | ||
148 | #else // UNIX | |
e9aad10a KB |
149 | wxString command; |
150 | ||
de560321 | 151 | #ifndef __EMX__ |
e9aad10a KB |
152 | if(m_BrowserIsNetscape) // try re-loading first |
153 | { | |
154 | wxString lockfile; | |
155 | wxGetHomeDir(&lockfile); | |
e0b3e85b JJ |
156 | #ifdef __VMS__ |
157 | lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape]lock."); | |
158 | struct stat statbuf; | |
159 | if(stat(lockfile.fn_str(), &statbuf) == 0) | |
160 | #else | |
223d09f6 | 161 | lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape/lock"); |
e9aad10a | 162 | struct stat statbuf; |
87138c52 | 163 | if(lstat(lockfile.fn_str(), &statbuf) == 0) |
e9aad10a KB |
164 | // cannot use wxFileExists, because it's a link pointing to a |
165 | // non-existing location if(wxFileExists(lockfile)) | |
e0b3e85b | 166 | #endif |
aa0ffd1d | 167 | { |
e9aad10a | 168 | long success; |
223d09f6 KB |
169 | command << m_BrowserName << wxT(" -remote openURL(") |
170 | << wxT("file://") << m_MapFile | |
171 | << WXEXTHELP_SEPARATOR << relativeURL << wxT(")"); | |
e9aad10a KB |
172 | success = wxExecute(command); |
173 | if(success != 0 ) // returns PID on success | |
ca65c044 | 174 | return true; |
e9aad10a KB |
175 | } |
176 | } | |
de560321 | 177 | #endif |
e9aad10a | 178 | command = m_BrowserName; |
223d09f6 | 179 | command << wxT(" file://") |
31528cd3 VZ |
180 | << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL; |
181 | return wxExecute(command) != 0; | |
6e5fefdf | 182 | #endif |
e9aad10a KB |
183 | } |
184 | ||
69108ccb JS |
185 | class wxExtHelpMapEntry : public wxObject |
186 | { | |
187 | public: | |
188 | int id; | |
189 | wxString url; | |
190 | wxString doc; | |
191 | wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc) | |
192 | { id = iid; url = iurl; doc = idoc; } | |
193 | }; | |
194 | ||
195 | void wxExtHelpController::DeleteList() | |
196 | { | |
197 | if(m_MapList) | |
198 | { | |
f7b83689 | 199 | wxList::compatibility_iterator node = m_MapList->GetFirst(); |
69108ccb JS |
200 | while (node) |
201 | { | |
202 | delete (wxExtHelpMapEntry *)node->GetData(); | |
f7b83689 | 203 | m_MapList->Erase(node); |
69108ccb JS |
204 | node = m_MapList->GetFirst(); |
205 | } | |
206 | delete m_MapList; | |
207 | m_MapList = (wxList*) NULL; | |
208 | } | |
209 | } | |
210 | ||
211 | /** This must be called to tell the controller where to find the | |
212 | documentation. | |
213 | @param file - NOT a filename, but a directory name. | |
214 | @return true on success | |
215 | */ | |
216 | bool | |
217 | wxExtHelpController::Initialize(const wxString& file) | |
218 | { | |
219 | return LoadFile(file); | |
220 | } | |
221 | ||
222 | ||
223 | // ifile is the name of the base help directory | |
224 | bool wxExtHelpController::LoadFile(const wxString& ifile) | |
225 | { | |
226 | wxString mapFile, file, url, doc; | |
227 | int id,i,len; | |
228 | char buffer[WXEXTHELP_BUFLEN]; | |
229 | ||
230 | wxBusyCursor b; // display a busy cursor | |
231 | ||
f50a1c3d | 232 | if(! ifile.empty()) |
69108ccb JS |
233 | { |
234 | file = ifile; | |
235 | if(! wxIsAbsolutePath(file)) | |
236 | { | |
237 | wxChar* f = wxGetWorkingDirectory(); | |
238 | file = f; | |
239 | delete[] f; // wxGetWorkingDirectory returns new memory | |
240 | #ifdef __WXMAC__ | |
241 | file << ifile; | |
242 | #else | |
243 | file << WXEXTHELP_SEPARATOR << ifile; | |
244 | #endif | |
245 | } | |
246 | else | |
247 | file = ifile; | |
248 | ||
249 | #if wxUSE_INTL | |
250 | // If a locale is set, look in file/localename, i.e. | |
251 | // If passed "/usr/local/myapp/help" and the current wxLocale is | |
252 | // set to be "de", then look in "/usr/local/myapp/help/de/" | |
253 | // first and fall back to "/usr/local/myapp/help" if that | |
254 | // doesn't exist. | |
f50a1c3d | 255 | if(wxGetLocale() && !wxGetLocale()->GetName().empty()) |
69108ccb JS |
256 | { |
257 | wxString newfile; | |
258 | newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName(); | |
da865fdd | 259 | if(wxDirExists(newfile)) |
69108ccb JS |
260 | file = newfile; |
261 | else | |
262 | { | |
263 | newfile = WXEXTHELP_SEPARATOR; | |
264 | const wxChar *cptr = wxGetLocale()->GetName().c_str(); | |
265 | while(*cptr && *cptr != wxT('_')) | |
266 | newfile << *(cptr++); | |
da865fdd | 267 | if(wxDirExists(newfile)) |
69108ccb JS |
268 | file = newfile; |
269 | } | |
270 | } | |
271 | #endif | |
272 | ||
da865fdd | 273 | if(! wxDirExists(file)) |
ca65c044 | 274 | return false; |
69108ccb JS |
275 | |
276 | mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE; | |
277 | } | |
278 | else // try to reload old file | |
279 | mapFile = m_MapFile; | |
280 | ||
281 | if(! wxFileExists(mapFile)) | |
ca65c044 | 282 | return false; |
69108ccb JS |
283 | |
284 | DeleteList(); | |
285 | m_MapList = new wxList; | |
286 | m_NumOfEntries = 0; | |
287 | ||
78743282 | 288 | FILE *input = wxFopen(mapFile,wxT("rt")); |
69108ccb | 289 | if(! input) |
ca65c044 | 290 | return false; |
69108ccb JS |
291 | do |
292 | { | |
293 | if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR) | |
294 | { | |
295 | len = strlen(buffer); | |
296 | if(buffer[len-1] == '\n') | |
297 | buffer[len-1] = '\0'; // cut of trailing newline | |
298 | if(sscanf(buffer,"%d", &id) != 1) | |
299 | break; // error | |
300 | for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++) | |
301 | ; // find begin of URL | |
ca65c044 | 302 | url = wxEmptyString; |
69108ccb JS |
303 | while(buffer[i] && ! isspace(buffer[i]) && buffer[i] != |
304 | WXEXTHELP_COMMENTCHAR) | |
305 | url << (wxChar) buffer[i++]; | |
306 | while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR) | |
307 | i++; | |
ca65c044 | 308 | doc = wxEmptyString; |
69108ccb JS |
309 | if(buffer[i]) |
310 | doc = wxString::FromAscii( (buffer + i + 1) ); // skip the comment character | |
311 | m_MapList->Append(new wxExtHelpMapEntry(id,url,doc)); | |
312 | m_NumOfEntries++; | |
313 | } | |
314 | }while(! feof(input)); | |
315 | fclose(input); | |
316 | ||
317 | m_MapFile = file; // now it's valid | |
ca65c044 | 318 | return true; |
69108ccb JS |
319 | } |
320 | ||
321 | ||
322 | bool | |
323 | wxExtHelpController::DisplayContents() | |
324 | { | |
325 | if(! m_NumOfEntries) | |
ca65c044 | 326 | return false; |
69108ccb JS |
327 | |
328 | wxString contents; | |
f7b83689 | 329 | wxList::compatibility_iterator node = m_MapList->GetFirst(); |
69108ccb JS |
330 | wxExtHelpMapEntry *entry; |
331 | while(node) | |
332 | { | |
333 | entry = (wxExtHelpMapEntry *)node->GetData(); | |
334 | if(entry->id == CONTENTS_ID) | |
335 | { | |
336 | contents = entry->url; | |
337 | break; | |
338 | } | |
339 | node = node->GetNext(); | |
340 | } | |
341 | ||
ca65c044 | 342 | bool rc = false; |
69108ccb JS |
343 | wxString file; |
344 | file << m_MapFile << WXEXTHELP_SEPARATOR << contents; | |
345 | if(file.Contains(wxT('#'))) | |
346 | file = file.BeforeLast(wxT('#')); | |
347 | if(contents.Length() && wxFileExists(file)) | |
348 | rc = DisplaySection(CONTENTS_ID); | |
349 | ||
350 | // if not found, open homemade toc: | |
ca65c044 | 351 | return rc ? true : KeywordSearch(wxEmptyString); |
69108ccb JS |
352 | } |
353 | ||
354 | bool | |
355 | wxExtHelpController::DisplaySection(int sectionNo) | |
356 | { | |
357 | if(! m_NumOfEntries) | |
ca65c044 | 358 | return false; |
69108ccb JS |
359 | |
360 | wxBusyCursor b; // display a busy cursor | |
f7b83689 | 361 | wxList::compatibility_iterator node = m_MapList->GetFirst(); |
69108ccb JS |
362 | wxExtHelpMapEntry *entry; |
363 | while(node) | |
364 | { | |
365 | entry = (wxExtHelpMapEntry *)node->GetData(); | |
366 | if(entry->id == sectionNo) | |
367 | return DisplayHelp(entry->url); | |
368 | node = node->GetNext(); | |
369 | } | |
ca65c044 | 370 | return false; |
69108ccb JS |
371 | } |
372 | ||
373 | bool wxExtHelpController::DisplaySection(const wxString& section) | |
374 | { | |
375 | bool isFilename = (section.Find(wxT(".htm")) != -1); | |
376 | ||
377 | if (isFilename) | |
378 | return DisplayHelp(section); | |
379 | else | |
380 | return KeywordSearch(section); | |
381 | } | |
382 | ||
383 | bool | |
384 | wxExtHelpController::DisplayBlock(long blockNo) | |
385 | { | |
386 | return DisplaySection((int)blockNo); | |
387 | } | |
388 | ||
389 | bool | |
cb07c544 VZ |
390 | wxExtHelpController::KeywordSearch(const wxString& k, |
391 | wxHelpSearchMode WXUNUSED(mode)) | |
69108ccb JS |
392 | { |
393 | if(! m_NumOfEntries) | |
ca65c044 | 394 | return false; |
69108ccb JS |
395 | |
396 | wxString *choices = new wxString[m_NumOfEntries]; | |
397 | wxString *urls = new wxString[m_NumOfEntries]; | |
398 | wxString compA, compB; | |
399 | ||
400 | int idx = 0, j; | |
401 | bool rc; | |
f50a1c3d | 402 | bool showAll = k.empty(); |
f7b83689 | 403 | wxList::compatibility_iterator node = m_MapList->GetFirst(); |
69108ccb JS |
404 | wxExtHelpMapEntry *entry; |
405 | ||
406 | { | |
407 | wxBusyCursor b; // display a busy cursor | |
408 | compA = k; compA.LowerCase(); // we compare case insensitive | |
409 | while(node) | |
410 | { | |
411 | entry = (wxExtHelpMapEntry *)node->GetData(); | |
412 | compB = entry->doc; compB.LowerCase(); | |
f50a1c3d | 413 | if((showAll || compB.Contains(k)) && ! compB.empty()) |
69108ccb JS |
414 | { |
415 | urls[idx] = entry->url; | |
416 | // doesn't work: | |
417 | // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR)); | |
f50a1c3d | 418 | //if(choices[idx].empty()) // didn't contain the ';' |
69108ccb | 419 | // choices[idx] = (**i).doc; |
ca65c044 | 420 | choices[idx] = wxEmptyString; |
69108ccb JS |
421 | for(j=0;entry->doc.c_str()[j] |
422 | && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++) | |
423 | choices[idx] << entry->doc.c_str()[j]; | |
424 | idx++; | |
425 | } | |
426 | node = node->GetNext(); | |
427 | } | |
428 | } | |
429 | ||
430 | if(idx == 1) | |
431 | rc = DisplayHelp(urls[0]); | |
432 | else if(idx == 0) | |
433 | { | |
434 | wxMessageBox(_("No entries found.")); | |
ca65c044 | 435 | rc = false; |
69108ccb JS |
436 | } |
437 | else | |
438 | { | |
439 | idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"), | |
440 | showAll ? _("Help Index") : _("Entries found"), | |
441 | idx,choices); | |
442 | if(idx != -1) | |
443 | rc = DisplayHelp(urls[idx]); | |
444 | else | |
ca65c044 | 445 | rc = false; |
69108ccb JS |
446 | } |
447 | delete[] urls; | |
448 | delete[] choices; | |
449 | ||
450 | return rc; | |
451 | } | |
452 | ||
453 | ||
454 | bool wxExtHelpController::Quit() | |
455 | { | |
ca65c044 | 456 | return true; |
69108ccb JS |
457 | } |
458 | ||
459 | void wxExtHelpController::OnQuit() | |
460 | { | |
461 | } | |
462 | ||
463 | ||
31528cd3 | 464 | #endif // wxUSE_HELP |
e9aad10a | 465 |