]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helpext.cpp
fixed GetItemSpacing() inconsistency by deprecating the old function and adding a...
[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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9aad10a
KB
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 }
b13f5fbf 125
17ff4e8f 126 return true;
004fd0c8 127#elif defined(__WXPM__)
b13f5fbf 128
004fd0c8
DW
129 wxString url;
130 url << m_MapFile << '\\' << relativeURL.BeforeFirst('#');
131// will have to fix for OS/2, later.....DW
132// bool bOk = (int)ShellExecute(NULL, "open", url,
133// NULL, NULL, SW_SHOWNORMAL ) > 32;
134// if ( !bOk )
135// {
136// wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
137// return false;
138// }
139// else
140 return TRUE;
b13f5fbf
VS
141
142#elif defined(__DOS__)
143
144 wxString command;
145 command = m_BrowserName;
146 command << wxT(" file://")
147 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
148 return wxExecute(command) != 0;
149
150#else // UNIX
e9aad10a
KB
151 wxString command;
152
de560321 153#ifndef __EMX__
e9aad10a
KB
154 if(m_BrowserIsNetscape) // try re-loading first
155 {
156 wxString lockfile;
157 wxGetHomeDir(&lockfile);
e0b3e85b
JJ
158#ifdef __VMS__
159 lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape]lock.");
160 struct stat statbuf;
161 if(stat(lockfile.fn_str(), &statbuf) == 0)
162#else
223d09f6 163 lockfile << WXEXTHELP_SEPARATOR << wxT(".netscape/lock");
e9aad10a 164 struct stat statbuf;
87138c52 165 if(lstat(lockfile.fn_str(), &statbuf) == 0)
e9aad10a
KB
166 // cannot use wxFileExists, because it's a link pointing to a
167 // non-existing location if(wxFileExists(lockfile))
e0b3e85b 168#endif
aa0ffd1d 169 {
e9aad10a 170 long success;
223d09f6
KB
171 command << m_BrowserName << wxT(" -remote openURL(")
172 << wxT("file://") << m_MapFile
173 << WXEXTHELP_SEPARATOR << relativeURL << wxT(")");
e9aad10a
KB
174 success = wxExecute(command);
175 if(success != 0 ) // returns PID on success
5dcf05ae 176 return TRUE;
e9aad10a
KB
177 }
178 }
de560321 179#endif
e9aad10a 180 command = m_BrowserName;
223d09f6 181 command << wxT(" file://")
31528cd3
VZ
182 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
183 return wxExecute(command) != 0;
6e5fefdf 184#endif
e9aad10a
KB
185}
186
69108ccb
JS
187class wxExtHelpMapEntry : public wxObject
188{
189public:
190 int id;
191 wxString url;
192 wxString doc;
193 wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc)
194 { id = iid; url = iurl; doc = idoc; }
195};
196
197void wxExtHelpController::DeleteList()
198{
199 if(m_MapList)
200 {
f7b83689 201 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
202 while (node)
203 {
204 delete (wxExtHelpMapEntry *)node->GetData();
f7b83689 205 m_MapList->Erase(node);
69108ccb
JS
206 node = m_MapList->GetFirst();
207 }
208 delete m_MapList;
209 m_MapList = (wxList*) NULL;
210 }
211}
212
213/** This must be called to tell the controller where to find the
214 documentation.
215 @param file - NOT a filename, but a directory name.
216 @return true on success
217*/
218bool
219wxExtHelpController::Initialize(const wxString& file)
220{
221 return LoadFile(file);
222}
223
224
225// ifile is the name of the base help directory
226bool wxExtHelpController::LoadFile(const wxString& ifile)
227{
228 wxString mapFile, file, url, doc;
229 int id,i,len;
230 char buffer[WXEXTHELP_BUFLEN];
231
232 wxBusyCursor b; // display a busy cursor
233
234 if(! ifile.IsEmpty())
235 {
236 file = ifile;
237 if(! wxIsAbsolutePath(file))
238 {
239 wxChar* f = wxGetWorkingDirectory();
240 file = f;
241 delete[] f; // wxGetWorkingDirectory returns new memory
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().IsEmpty())
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 = wxT("");
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 = wxT("");
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
324bool
325wxExtHelpController::DisplayContents()
326{
327 if(! m_NumOfEntries)
328 return FALSE;
329
330 wxString contents;
f7b83689 331 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
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(wxT(""));
354}
355
356bool
357wxExtHelpController::DisplaySection(int sectionNo)
358{
359 if(! m_NumOfEntries)
360 return FALSE;
361
362 wxBusyCursor b; // display a busy cursor
f7b83689 363 wxList::compatibility_iterator node = m_MapList->GetFirst();
69108ccb
JS
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
375bool 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
385bool
386wxExtHelpController::DisplayBlock(long blockNo)
387{
388 return DisplaySection((int)blockNo);
389}
390
391bool
392wxExtHelpController::KeywordSearch(const wxString& k)
393{
394 if(! m_NumOfEntries)
395 return FALSE;
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;
403 bool showAll = k.IsEmpty();
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();
414 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
415 {
416 urls[idx] = entry->url;
417 // doesn't work:
418 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
419 //if(choices[idx].IsEmpty()) // didn't contain the ';'
420 // choices[idx] = (**i).doc;
421 choices[idx] = wxT("");
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."));
436 rc = FALSE;
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
446 rc = FALSE;
447 }
448 delete[] urls;
449 delete[] choices;
450
451 return rc;
452}
453
454
455bool wxExtHelpController::Quit()
456{
457 return TRUE;
458}
459
460void wxExtHelpController::OnQuit()
461{
462}
463
464
31528cd3 465#endif // wxUSE_HELP
e9aad10a 466