]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helpext.cpp
wxMGL revitalised with OpenWatcom.
[wxWidgets.git] / src / generic / helpext.cpp
CommitLineData
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
ec904840
CE
42#ifdef __WINDOWS__
43#include "wx/msw/mslu.h"
44#endif
45
583f6c5c
JS
46#ifdef __WXMSW__
47#include <windows.h>
7acf6a92 48#include "wx/msw/winundef.h"
583f6c5c
JS
49#endif
50
69108ccb
JS
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
64IMPLEMENT_CLASS(wxExtHelpController, wxHelpControllerBase)
31528cd3 65
aa0ffd1d 66/// Name of environment variable to set help browser.
2b5f62a0 67#define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
aa0ffd1d 68/// Is browser a netscape browser?
2b5f62a0 69#define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
aa0ffd1d 70
e9aad10a
KB
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
3db52265
JS
77wxExtHelpController::wxExtHelpController(wxWindow* parentWindow):
78 wxHelpControllerBase(parentWindow)
e9aad10a 79{
69108ccb
JS
80 m_MapList = (wxList*) NULL;
81 m_NumOfEntries = 0;
e9aad10a
KB
82 m_BrowserName = WXEXTHELP_DEFAULTBROWSER;
83 m_BrowserIsNetscape = WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE;
84
2b5f62a0 85 wxChar *browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSER);
e9aad10a
KB
86 if(browser)
87 {
88 m_BrowserName = browser;
2b5f62a0
VZ
89 browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
90 m_BrowserIsNetscape = browser && (wxAtoi(browser) != 0);
e9aad10a
KB
91 }
92}
93
69108ccb
JS
94wxExtHelpController::~wxExtHelpController()
95{
96 DeleteList();
97}
e9aad10a 98
69108ccb 99void wxExtHelpController::SetBrowser(const wxString& browsername, bool isNetscape)
e9aad10a
KB
100{
101 m_BrowserName = browsername;
102 m_BrowserIsNetscape = isNetscape;
103}
104
33b64e6f
JS
105// Set viewer: new, generic name for SetBrowser
106void wxExtHelpController::SetViewer(const wxString& viewer, long flags)
107{
108 SetBrowser(viewer, ((flags & wxHELP_NETSCAPE) == wxHELP_NETSCAPE));
109}
110
e9aad10a 111bool
f6bcfd97 112wxExtHelpController::DisplayHelp(const wxString &relativeURL)
e9aad10a
KB
113{
114 wxBusyCursor b; // display a busy cursor
6e5fefdf
KB
115
116
004fd0c8 117#if defined(__WXMSW__)
f96b60aa
VZ
118 wxString url;
119 url << m_MapFile << '\\' << relativeURL.BeforeFirst('#');
f6bcfd97 120 bool bOk = (int)ShellExecute(NULL, wxT("open"), url.c_str(),
f96b60aa 121 NULL, NULL, SW_SHOWNORMAL ) > 32;
6e5fefdf
KB
122 if ( !bOk )
123 {
124 wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
125 return false;
126 }
b13f5fbf 127
17ff4e8f 128 return true;
7fc65a03 129#elif defined(__OS2__)
b13f5fbf 130
004fd0c8
DW
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
ca65c044 142 return true;
b13f5fbf
VS
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
e9aad10a
KB
153 wxString command;
154
de560321 155#ifndef __EMX__
e9aad10a
KB
156 if(m_BrowserIsNetscape) // try re-loading first
157 {
158 wxString lockfile;
159 wxGetHomeDir(&lockfile);
e0b3e85b
JJ
160#ifdef __VMS__
161 lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape]lock.");
162 struct stat statbuf;
163 if(stat(lockfile.fn_str(), &statbuf) == 0)
164#else
223d09f6 165 lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape/lock");
e9aad10a 166 struct stat statbuf;
87138c52 167 if(lstat(lockfile.fn_str(), &statbuf) == 0)
e9aad10a
KB
168 // cannot use wxFileExists, because it's a link pointing to a
169 // non-existing location if(wxFileExists(lockfile))
e0b3e85b 170#endif
aa0ffd1d 171 {
e9aad10a 172 long success;
223d09f6
KB
173 command << m_BrowserName << wxT(" -remote openURL(")
174 << wxT("file://") << m_MapFile
175 << WXEXTHELP_SEPARATOR << relativeURL << wxT(")");
e9aad10a
KB
176 success = wxExecute(command);
177 if(success != 0 ) // returns PID on success
ca65c044 178 return true;
e9aad10a
KB
179 }
180 }
de560321 181#endif
e9aad10a 182 command = m_BrowserName;
223d09f6 183 command << wxT(" file://")
31528cd3
VZ
184 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
185 return wxExecute(command) != 0;
6e5fefdf 186#endif
e9aad10a
KB
187}
188
69108ccb
JS
189class wxExtHelpMapEntry : public wxObject
190{
191public:
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
199void wxExtHelpController::DeleteList()
200{
201 if(m_MapList)
202 {
f7b83689 203 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
204 while (node)
205 {
206 delete (wxExtHelpMapEntry *)node->GetData();
f7b83689 207 m_MapList->Erase(node);
69108ccb
JS
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*/
220bool
221wxExtHelpController::Initialize(const wxString& file)
222{
223 return LoadFile(file);
224}
225
226
227// ifile is the name of the base help directory
228bool 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
f50a1c3d 236 if(! ifile.empty())
69108ccb
JS
237 {
238 file = ifile;
239 if(! wxIsAbsolutePath(file))
240 {
241 wxChar* f = wxGetWorkingDirectory();
242 file = f;
243 delete[] f; // wxGetWorkingDirectory returns new memory
244#ifdef __WXMAC__
245 file << ifile;
246#else
247 file << WXEXTHELP_SEPARATOR << ifile;
248#endif
249 }
250 else
251 file = ifile;
252
253#if wxUSE_INTL
254 // If a locale is set, look in file/localename, i.e.
255 // If passed "/usr/local/myapp/help" and the current wxLocale is
256 // set to be "de", then look in "/usr/local/myapp/help/de/"
257 // first and fall back to "/usr/local/myapp/help" if that
258 // doesn't exist.
f50a1c3d 259 if(wxGetLocale() && !wxGetLocale()->GetName().empty())
69108ccb
JS
260 {
261 wxString newfile;
262 newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName();
da865fdd 263 if(wxDirExists(newfile))
69108ccb
JS
264 file = newfile;
265 else
266 {
267 newfile = WXEXTHELP_SEPARATOR;
268 const wxChar *cptr = wxGetLocale()->GetName().c_str();
269 while(*cptr && *cptr != wxT('_'))
270 newfile << *(cptr++);
da865fdd 271 if(wxDirExists(newfile))
69108ccb
JS
272 file = newfile;
273 }
274 }
275#endif
276
da865fdd 277 if(! wxDirExists(file))
ca65c044 278 return false;
69108ccb
JS
279
280 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
281 }
282 else // try to reload old file
283 mapFile = m_MapFile;
284
285 if(! wxFileExists(mapFile))
ca65c044 286 return false;
69108ccb
JS
287
288 DeleteList();
289 m_MapList = new wxList;
290 m_NumOfEntries = 0;
291
78743282 292 FILE *input = wxFopen(mapFile,wxT("rt"));
69108ccb 293 if(! input)
ca65c044 294 return false;
69108ccb
JS
295 do
296 {
297 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
298 {
299 len = strlen(buffer);
300 if(buffer[len-1] == '\n')
301 buffer[len-1] = '\0'; // cut of trailing newline
302 if(sscanf(buffer,"%d", &id) != 1)
303 break; // error
304 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
305 ; // find begin of URL
ca65c044 306 url = wxEmptyString;
69108ccb
JS
307 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
308 WXEXTHELP_COMMENTCHAR)
309 url << (wxChar) buffer[i++];
310 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
311 i++;
ca65c044 312 doc = wxEmptyString;
69108ccb
JS
313 if(buffer[i])
314 doc = wxString::FromAscii( (buffer + i + 1) ); // skip the comment character
315 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
316 m_NumOfEntries++;
317 }
318 }while(! feof(input));
319 fclose(input);
320
321 m_MapFile = file; // now it's valid
ca65c044 322 return true;
69108ccb
JS
323}
324
325
326bool
327wxExtHelpController::DisplayContents()
328{
329 if(! m_NumOfEntries)
ca65c044 330 return false;
69108ccb
JS
331
332 wxString contents;
f7b83689 333 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
334 wxExtHelpMapEntry *entry;
335 while(node)
336 {
337 entry = (wxExtHelpMapEntry *)node->GetData();
338 if(entry->id == CONTENTS_ID)
339 {
340 contents = entry->url;
341 break;
342 }
343 node = node->GetNext();
344 }
345
ca65c044 346 bool rc = false;
69108ccb
JS
347 wxString file;
348 file << m_MapFile << WXEXTHELP_SEPARATOR << contents;
349 if(file.Contains(wxT('#')))
350 file = file.BeforeLast(wxT('#'));
351 if(contents.Length() && wxFileExists(file))
352 rc = DisplaySection(CONTENTS_ID);
353
354 // if not found, open homemade toc:
ca65c044 355 return rc ? true : KeywordSearch(wxEmptyString);
69108ccb
JS
356}
357
358bool
359wxExtHelpController::DisplaySection(int sectionNo)
360{
361 if(! m_NumOfEntries)
ca65c044 362 return false;
69108ccb
JS
363
364 wxBusyCursor b; // display a busy cursor
f7b83689 365 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
366 wxExtHelpMapEntry *entry;
367 while(node)
368 {
369 entry = (wxExtHelpMapEntry *)node->GetData();
370 if(entry->id == sectionNo)
371 return DisplayHelp(entry->url);
372 node = node->GetNext();
373 }
ca65c044 374 return false;
69108ccb
JS
375}
376
377bool wxExtHelpController::DisplaySection(const wxString& section)
378{
379 bool isFilename = (section.Find(wxT(".htm")) != -1);
380
381 if (isFilename)
382 return DisplayHelp(section);
383 else
384 return KeywordSearch(section);
385}
386
387bool
388wxExtHelpController::DisplayBlock(long blockNo)
389{
390 return DisplaySection((int)blockNo);
391}
392
393bool
cb07c544
VZ
394wxExtHelpController::KeywordSearch(const wxString& k,
395 wxHelpSearchMode WXUNUSED(mode))
69108ccb
JS
396{
397 if(! m_NumOfEntries)
ca65c044 398 return false;
69108ccb
JS
399
400 wxString *choices = new wxString[m_NumOfEntries];
401 wxString *urls = new wxString[m_NumOfEntries];
402 wxString compA, compB;
403
404 int idx = 0, j;
405 bool rc;
f50a1c3d 406 bool showAll = k.empty();
f7b83689 407 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
408 wxExtHelpMapEntry *entry;
409
410 {
411 wxBusyCursor b; // display a busy cursor
412 compA = k; compA.LowerCase(); // we compare case insensitive
413 while(node)
414 {
415 entry = (wxExtHelpMapEntry *)node->GetData();
416 compB = entry->doc; compB.LowerCase();
f50a1c3d 417 if((showAll || compB.Contains(k)) && ! compB.empty())
69108ccb
JS
418 {
419 urls[idx] = entry->url;
420 // doesn't work:
421 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
f50a1c3d 422 //if(choices[idx].empty()) // didn't contain the ';'
69108ccb 423 // choices[idx] = (**i).doc;
ca65c044 424 choices[idx] = wxEmptyString;
69108ccb
JS
425 for(j=0;entry->doc.c_str()[j]
426 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
427 choices[idx] << entry->doc.c_str()[j];
428 idx++;
429 }
430 node = node->GetNext();
431 }
432 }
433
434 if(idx == 1)
435 rc = DisplayHelp(urls[0]);
436 else if(idx == 0)
437 {
438 wxMessageBox(_("No entries found."));
ca65c044 439 rc = false;
69108ccb
JS
440 }
441 else
442 {
443 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
444 showAll ? _("Help Index") : _("Entries found"),
445 idx,choices);
446 if(idx != -1)
447 rc = DisplayHelp(urls[idx]);
448 else
ca65c044 449 rc = false;
69108ccb
JS
450 }
451 delete[] urls;
452 delete[] choices;
453
454 return rc;
455}
456
457
458bool wxExtHelpController::Quit()
459{
ca65c044 460 return true;
69108ccb
JS
461}
462
463void wxExtHelpController::OnQuit()
464{
465}
466
467
31528cd3 468#endif // wxUSE_HELP
e9aad10a 469