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