]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helpext.cpp
fixed warning in non-DLL build
[wxWidgets.git] / src / generic / helpext.cpp
CommitLineData
f96b60aa
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: helpext.cpp
3// Purpose: an external help controller for wxWindows
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
e9aad10a
KB
12#ifdef __GNUG__
13# pragma implementation "wxexthlp.h"
14#endif
15
f96b60aa
VZ
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
7a026580 22#if wxUSE_HELP && !defined(__WXWINCE__)
31528cd3 23
f96b60aa
VZ
24#ifndef WX_PRECOMP
25 #include "wx/setup.h"
26 #include "wx/string.h"
27 #include "wx/utils.h"
28 #include "wx/list.h"
29 #include "wx/intl.h"
29d0a26e
MB
30 #include "wx/msgdlg.h"
31 #include "wx/choicdlg.h"
1020103f 32 #include "wx/log.h"
f96b60aa
VZ
33#endif
34
35#include "wx/helpbase.h"
36#include "wx/generic/helpext.h"
37
38#include <stdio.h>
39#include <ctype.h>
40#include <sys/stat.h>
41
004fd0c8 42#if !defined(__WINDOWS__) && !defined(__OS2__)
f96b60aa
VZ
43 #include <unistd.h>
44#endif
e9aad10a 45
583f6c5c
JS
46#ifdef __WXMSW__
47#include <windows.h>
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
69108ccb 76wxExtHelpController::wxExtHelpController()
e9aad10a 77{
69108ccb
JS
78 m_MapList = (wxList*) NULL;
79 m_NumOfEntries = 0;
e9aad10a
KB
80 m_BrowserName = WXEXTHELP_DEFAULTBROWSER;
81 m_BrowserIsNetscape = WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE;
82
2b5f62a0 83 wxChar *browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSER);
e9aad10a
KB
84 if(browser)
85 {
86 m_BrowserName = browser;
2b5f62a0
VZ
87 browser = wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
88 m_BrowserIsNetscape = browser && (wxAtoi(browser) != 0);
e9aad10a
KB
89 }
90}
91
69108ccb
JS
92wxExtHelpController::~wxExtHelpController()
93{
94 DeleteList();
95}
e9aad10a 96
69108ccb 97void wxExtHelpController::SetBrowser(const wxString& browsername, bool isNetscape)
e9aad10a
KB
98{
99 m_BrowserName = browsername;
100 m_BrowserIsNetscape = isNetscape;
101}
102
33b64e6f
JS
103// Set viewer: new, generic name for SetBrowser
104void wxExtHelpController::SetViewer(const wxString& viewer, long flags)
105{
106 SetBrowser(viewer, ((flags & wxHELP_NETSCAPE) == wxHELP_NETSCAPE));
107}
108
e9aad10a 109bool
f6bcfd97 110wxExtHelpController::DisplayHelp(const wxString &relativeURL)
e9aad10a
KB
111{
112 wxBusyCursor b; // display a busy cursor
6e5fefdf
KB
113
114
004fd0c8 115#if defined(__WXMSW__)
f96b60aa
VZ
116 wxString url;
117 url << m_MapFile << '\\' << relativeURL.BeforeFirst('#');
f6bcfd97 118 bool bOk = (int)ShellExecute(NULL, wxT("open"), url.c_str(),
f96b60aa 119 NULL, NULL, SW_SHOWNORMAL ) > 32;
6e5fefdf
KB
120 if ( !bOk )
121 {
122 wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
123 return false;
124 }
125 else
126 return true;
b13f5fbf 127
004fd0c8 128#elif defined(__WXPM__)
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
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
5dcf05ae 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
235 if(! ifile.IsEmpty())
236 {
237 file = ifile;
238 if(! wxIsAbsolutePath(file))
239 {
240 wxChar* f = wxGetWorkingDirectory();
241 file = f;
242 delete[] f; // wxGetWorkingDirectory returns new memory
243#ifdef __WXMAC__
244 file << ifile;
245#else
246 file << WXEXTHELP_SEPARATOR << ifile;
247#endif
248 }
249 else
250 file = ifile;
251
252#if wxUSE_INTL
253 // If a locale is set, look in file/localename, i.e.
254 // If passed "/usr/local/myapp/help" and the current wxLocale is
255 // set to be "de", then look in "/usr/local/myapp/help/de/"
256 // first and fall back to "/usr/local/myapp/help" if that
257 // doesn't exist.
258 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
259 {
260 wxString newfile;
261 newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName();
262 if(wxDirExists(newfile))
263 file = newfile;
264 else
265 {
266 newfile = WXEXTHELP_SEPARATOR;
267 const wxChar *cptr = wxGetLocale()->GetName().c_str();
268 while(*cptr && *cptr != wxT('_'))
269 newfile << *(cptr++);
270 if(wxDirExists(newfile))
271 file = newfile;
272 }
273 }
274#endif
275
276 if(! wxDirExists(file))
277 return FALSE;
278
279 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
280 }
281 else // try to reload old file
282 mapFile = m_MapFile;
283
284 if(! wxFileExists(mapFile))
285 return FALSE;
286
287 DeleteList();
288 m_MapList = new wxList;
289 m_NumOfEntries = 0;
290
291 FILE *input = wxFopen(mapFile,wxT("rt"));
292 if(! input)
293 return FALSE;
294 do
295 {
296 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
297 {
298 len = strlen(buffer);
299 if(buffer[len-1] == '\n')
300 buffer[len-1] = '\0'; // cut of trailing newline
301 if(sscanf(buffer,"%d", &id) != 1)
302 break; // error
303 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
304 ; // find begin of URL
305 url = wxT("");
306 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
307 WXEXTHELP_COMMENTCHAR)
308 url << (wxChar) buffer[i++];
309 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
310 i++;
311 doc = wxT("");
312 if(buffer[i])
313 doc = wxString::FromAscii( (buffer + i + 1) ); // skip the comment character
314 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
315 m_NumOfEntries++;
316 }
317 }while(! feof(input));
318 fclose(input);
319
320 m_MapFile = file; // now it's valid
321 return TRUE;
322}
323
324
325bool
326wxExtHelpController::DisplayContents()
327{
328 if(! m_NumOfEntries)
329 return FALSE;
330
331 wxString contents;
f7b83689 332 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
333 wxExtHelpMapEntry *entry;
334 while(node)
335 {
336 entry = (wxExtHelpMapEntry *)node->GetData();
337 if(entry->id == CONTENTS_ID)
338 {
339 contents = entry->url;
340 break;
341 }
342 node = node->GetNext();
343 }
344
345 bool rc = FALSE;
346 wxString file;
347 file << m_MapFile << WXEXTHELP_SEPARATOR << contents;
348 if(file.Contains(wxT('#')))
349 file = file.BeforeLast(wxT('#'));
350 if(contents.Length() && wxFileExists(file))
351 rc = DisplaySection(CONTENTS_ID);
352
353 // if not found, open homemade toc:
354 return rc ? TRUE : KeywordSearch(wxT(""));
355}
356
357bool
358wxExtHelpController::DisplaySection(int sectionNo)
359{
360 if(! m_NumOfEntries)
361 return FALSE;
362
363 wxBusyCursor b; // display a busy cursor
f7b83689 364 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
365 wxExtHelpMapEntry *entry;
366 while(node)
367 {
368 entry = (wxExtHelpMapEntry *)node->GetData();
369 if(entry->id == sectionNo)
370 return DisplayHelp(entry->url);
371 node = node->GetNext();
372 }
373 return FALSE;
374}
375
376bool wxExtHelpController::DisplaySection(const wxString& section)
377{
378 bool isFilename = (section.Find(wxT(".htm")) != -1);
379
380 if (isFilename)
381 return DisplayHelp(section);
382 else
383 return KeywordSearch(section);
384}
385
386bool
387wxExtHelpController::DisplayBlock(long blockNo)
388{
389 return DisplaySection((int)blockNo);
390}
391
392bool
393wxExtHelpController::KeywordSearch(const wxString& k)
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.IsEmpty();
f7b83689 405 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
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.IsEmpty())
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].IsEmpty()) // didn't contain the ';'
421 // choices[idx] = (**i).doc;
422 choices[idx] = wxT("");
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
456bool wxExtHelpController::Quit()
457{
458 return TRUE;
459}
460
461void wxExtHelpController::OnQuit()
462{
463}
464
465
31528cd3 466#endif // wxUSE_HELP
e9aad10a 467