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