1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/helpext.cpp
3 // Purpose: an external help controller for wxWidgets
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_HELP && !defined(__WXWINCE__) && (!defined(__WXMAC__) || defined(__WXMAC_OSX__))
21 #include "wx/string.h"
25 #include "wx/msgdlg.h"
26 #include "wx/choicdlg.h"
30 #include "wx/filename.h"
31 #include "wx/textfile.h"
32 #include "wx/generic/helpext.h"
38 #if !defined(__WINDOWS__) && !defined(__OS2__)
43 #include "wx/msw/mslu.h"
48 #include "wx/msw/winundef.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 /// Name for map file.
56 #define WXEXTHELP_MAPFILE _T("wxhelp.map")
58 /// Character introducing comments/documentation field in map file.
59 #define WXEXTHELP_COMMENTCHAR ';'
63 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
65 /// Name of environment variable to set help browser.
66 #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
67 /// Is browser a netscape browser?
68 #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
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.
76 wxExtHelpController::wxExtHelpController(wxWindow
* parentWindow
):
77 wxHelpControllerBase(parentWindow
)
79 m_MapList
= (wxList
*) NULL
;
81 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
82 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
84 wxChar
*browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSER
);
87 m_BrowserName
= browser
;
88 browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
89 m_BrowserIsNetscape
= browser
&& (wxAtoi(browser
) != 0);
93 wxExtHelpController::~wxExtHelpController()
98 void wxExtHelpController::SetBrowser(const wxString
& browsername
, bool isNetscape
)
100 m_BrowserName
= browsername
;
101 m_BrowserIsNetscape
= isNetscape
;
104 // Set viewer: new, generic name for SetBrowser
105 void wxExtHelpController::SetViewer(const wxString
& viewer
, long flags
)
107 SetBrowser(viewer
, ((flags
& wxHELP_NETSCAPE
) == wxHELP_NETSCAPE
));
111 wxExtHelpController::DisplayHelp(const wxString
&relativeURL
)
113 wxBusyCursor b
; // display a busy cursor
116 #if defined(__WXMSW__)
118 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
119 bool bOk
= (int)ShellExecute(NULL
, wxT("open"), url
.c_str(),
120 NULL
, NULL
, SW_SHOWNORMAL
) > 32;
123 wxLogSysError(_("Cannot open URL '%s'"), relativeURL
.c_str());
128 #elif defined(__OS2__)
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;
137 // wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
143 #elif defined(__DOS__)
146 command
= m_BrowserName
;
147 command
<< wxT(" file://")
148 << m_MapFile
<< wxFILE_SEP_PATH
<< relativeURL
;
149 return wxExecute(command
) != 0;
155 if(m_BrowserIsNetscape
) // try re-loading first
158 wxGetHomeDir(&lockfile
);
160 lockfile
<< wxFILE_SEP_PATH
<< wxT(".netscape]lock.");
162 if(stat(lockfile
.fn_str(), &statbuf
) == 0)
164 lockfile
<< wxFILE_SEP_PATH
<< wxT(".netscape/lock");
166 if(lstat(lockfile
.fn_str(), &statbuf
) == 0)
167 // cannot use wxFileExists, because it's a link pointing to a
168 // non-existing location if(wxFileExists(lockfile))
172 command
<< m_BrowserName
<< wxT(" -remote openURL(")
173 << wxT("file://") << m_MapFile
174 << wxFILE_SEP_PATH
<< relativeURL
<< wxT(")");
175 success
= wxExecute(command
);
176 if(success
!= 0 ) // returns PID on success
181 command
= m_BrowserName
;
182 command
<< wxT(" file://")
183 << m_MapFile
<< wxFILE_SEP_PATH
<< relativeURL
;
184 return wxExecute(command
) != 0;
188 class wxExtHelpMapEntry
: public wxObject
194 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
195 { id
= iid
; url
= iurl
; doc
= idoc
; }
198 void wxExtHelpController::DeleteList()
202 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
205 delete (wxExtHelpMapEntry
*)node
->GetData();
206 m_MapList
->Erase(node
);
207 node
= m_MapList
->GetFirst();
210 m_MapList
= (wxList
*) NULL
;
214 /** This must be called to tell the controller where to find the
216 @param file - NOT a filename, but a directory name.
217 @return true on success
220 wxExtHelpController::Initialize(const wxString
& file
)
222 return LoadFile(file
);
226 bool wxExtHelpController::ParseMapFileLine(const wxString
& line
)
228 const wxChar
*p
= line
.c_str();
231 while ( isascii(*p
) && isspace(*p
) )
234 // skip empty lines and comments
235 if ( *p
== _T('\0') || *p
== WXEXTHELP_COMMENTCHAR
)
238 // the line is of the form "num url" so we must have an integer now
240 const unsigned long id
= wxStrtoul(p
, &end
, 0);
246 while ( isascii(*p
) && isspace(*p
) )
249 // next should be the URL
251 url
.reserve(line
.length());
252 while ( isascii(*p
) && !isspace(*p
) )
255 while ( isascii(*p
) && isspace(*p
) )
258 // and finally the optional description of the entry after comment
260 if ( *p
== WXEXTHELP_COMMENTCHAR
)
263 while ( isascii(*p
) && isspace(*p
) )
268 m_MapList
->Append(new wxExtHelpMapEntry(id
, url
, doc
));
274 // file is a misnomer as it's the name of the base help directory
275 bool wxExtHelpController::LoadFile(const wxString
& file
)
277 wxFileName
helpDir(wxFileName::DirName(file
));
278 helpDir
.MakeAbsolute();
280 bool dirExists
= false;
283 // If a locale is set, look in file/localename, i.e. If passed
284 // "/usr/local/myapp/help" and the current wxLocale is set to be "de", then
285 // look in "/usr/local/myapp/help/de/" first and fall back to
286 // "/usr/local/myapp/help" if that doesn't exist.
287 const wxLocale
* const loc
= wxGetLocale();
290 wxString locName
= loc
->GetName();
292 // the locale is in general of the form xx_YY.zzzz, try the full firm
293 // first and then also more general ones
294 wxFileName
helpDirLoc(helpDir
);
295 helpDirLoc
.AppendDir(locName
);
296 dirExists
= helpDirLoc
.DirExists();
300 // try without encoding
301 const wxString locNameWithoutEncoding
= locName
.BeforeLast(_T('.'));
302 if ( !locNameWithoutEncoding
.empty() )
304 helpDirLoc
= helpDir
;
305 helpDirLoc
.AppendDir(locNameWithoutEncoding
);
306 dirExists
= helpDirLoc
.DirExists();
312 // try without country part
313 wxString locNameWithoutCountry
= locName
.BeforeLast(_T('_'));
314 if ( !locNameWithoutCountry
.empty() )
316 helpDirLoc
= helpDir
;
317 helpDirLoc
.AppendDir(locNameWithoutCountry
);
318 dirExists
= helpDirLoc
.DirExists();
323 helpDir
= helpDirLoc
;
327 if ( !dirExists
&& !helpDir
.DirExists() )
329 wxLogError(_("Help directory \"%s\" not found."),
330 helpDir
.GetFullPath().c_str());
334 const wxFileName
mapFile(helpDir
.GetFullPath(), WXEXTHELP_MAPFILE
);
335 if ( !mapFile
.FileExists() )
337 wxLogError(_("Help file \"%s\" not found."),
338 mapFile
.GetFullPath().c_str());
343 m_MapList
= new wxList
;
347 if ( !input
.Open(mapFile
.GetFullPath()) )
350 for ( wxString
& line
= input
.GetFirstLine();
352 line
= input
.GetNextLine() )
354 if ( !ParseMapFileLine(line
) )
356 wxLogWarning(_("Line %lu of map file \"%s\" has invalid syntax, skipped."),
357 (unsigned long)input
.GetCurrentLine(),
358 mapFile
.GetFullPath().c_str());
362 if ( !m_NumOfEntries
)
364 wxLogError(_("No valid mappings found in the file \"%s\"."),
365 mapFile
.GetFullPath().c_str());
369 m_MapFile
= helpDir
.GetFullPath(); // now it's valid
375 wxExtHelpController::DisplayContents()
381 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
382 wxExtHelpMapEntry
*entry
;
385 entry
= (wxExtHelpMapEntry
*)node
->GetData();
386 if(entry
->id
== CONTENTS_ID
)
388 contents
= entry
->url
;
391 node
= node
->GetNext();
396 file
<< m_MapFile
<< wxFILE_SEP_PATH
<< contents
;
397 if(file
.Contains(wxT('#')))
398 file
= file
.BeforeLast(wxT('#'));
399 if(contents
.length() && wxFileExists(file
))
400 rc
= DisplaySection(CONTENTS_ID
);
402 // if not found, open homemade toc:
403 return rc
? true : KeywordSearch(wxEmptyString
);
407 wxExtHelpController::DisplaySection(int sectionNo
)
412 wxBusyCursor b
; // display a busy cursor
413 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
414 wxExtHelpMapEntry
*entry
;
417 entry
= (wxExtHelpMapEntry
*)node
->GetData();
418 if(entry
->id
== sectionNo
)
419 return DisplayHelp(entry
->url
);
420 node
= node
->GetNext();
425 bool wxExtHelpController::DisplaySection(const wxString
& section
)
427 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
430 return DisplayHelp(section
);
432 return KeywordSearch(section
);
436 wxExtHelpController::DisplayBlock(long blockNo
)
438 return DisplaySection((int)blockNo
);
442 wxExtHelpController::KeywordSearch(const wxString
& k
,
443 wxHelpSearchMode
WXUNUSED(mode
))
448 wxString
*choices
= new wxString
[m_NumOfEntries
];
449 wxString
*urls
= new wxString
[m_NumOfEntries
];
450 wxString compA
, compB
;
454 bool showAll
= k
.empty();
455 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
456 wxExtHelpMapEntry
*entry
;
459 wxBusyCursor b
; // display a busy cursor
460 compA
= k
; compA
.LowerCase(); // we compare case insensitive
463 entry
= (wxExtHelpMapEntry
*)node
->GetData();
464 compB
= entry
->doc
; compB
.LowerCase();
465 if((showAll
|| compB
.Contains(k
)) && ! compB
.empty())
467 urls
[idx
] = entry
->url
;
469 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
470 //if(choices[idx].empty()) // didn't contain the ';'
471 // choices[idx] = (**i).doc;
472 choices
[idx
] = wxEmptyString
;
473 for(j
=0;entry
->doc
.c_str()[j
]
474 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
475 choices
[idx
] << entry
->doc
.c_str()[j
];
478 node
= node
->GetNext();
483 rc
= DisplayHelp(urls
[0]);
486 wxMessageBox(_("No entries found."));
491 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
492 showAll
? _("Help Index") : _("Entries found"),
495 rc
= DisplayHelp(urls
[idx
]);
506 bool wxExtHelpController::Quit()
511 void wxExtHelpController::OnQuit()