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__)
22 #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 // ----------------------------------------------------------------------------
56 #define WXEXTHELP_MAPFILE wxT("wxhelp.map")
58 // Character introducing comments/documentation field in map file.
59 #define WXEXTHELP_COMMENTCHAR ';'
61 // The ID of the Contents section
62 #define WXEXTHELP_CONTENTS_ID 0
64 // Name of environment variable to set help browser.
65 #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
67 // Is browser a netscape browser?
68 #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
70 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
72 wxExtHelpController::wxExtHelpController(wxWindow
* parentWindow
)
73 : wxHelpControllerBase(parentWindow
)
77 m_BrowserIsNetscape
= false;
79 wxChar
*browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSER
);
82 m_BrowserName
= browser
;
83 browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
84 m_BrowserIsNetscape
= browser
&& (wxAtoi(browser
) != 0);
88 wxExtHelpController::~wxExtHelpController()
93 #if WXWIN_COMPATIBILITY_2_8
94 void wxExtHelpController::SetBrowser(const wxString
& browsername
, bool isNetscape
)
96 m_BrowserName
= browsername
;
97 m_BrowserIsNetscape
= isNetscape
;
101 void wxExtHelpController::SetViewer(const wxString
& viewer
, long flags
)
103 m_BrowserName
= viewer
;
104 m_BrowserIsNetscape
= (flags
& wxHELP_NETSCAPE
) != 0;
107 bool wxExtHelpController::DisplayHelp(const wxString
&relativeURL
)
109 // construct hte URL to open -- it's just a file
110 wxString
url(wxT("file://") + m_helpDir
);
111 url
<< wxFILE_SEP_PATH
<< relativeURL
;
113 // use the explicit browser program if specified
114 if ( !m_BrowserName
.empty() )
116 if ( m_BrowserIsNetscape
)
119 command
<< m_BrowserName
120 << wxT(" -remote openURL(") << url
<< wxT(')');
121 if ( wxExecute(command
, wxEXEC_SYNC
) != -1 )
125 if ( wxExecute(m_BrowserName
+ wxT(' ') + url
, wxEXEC_SYNC
) != -1 )
128 //else: either no browser explicitly specified or we failed to open it
130 // just use default browser
131 return wxLaunchDefaultBrowser(url
);
134 class wxExtHelpMapEntry
: public wxObject
141 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
142 { id
= iid
; url
= iurl
; doc
= idoc
; }
145 void wxExtHelpController::DeleteList()
149 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
152 delete (wxExtHelpMapEntry
*)node
->GetData();
153 m_MapList
->Erase(node
);
154 node
= m_MapList
->GetFirst();
161 // This must be called to tell the controller where to find the documentation.
162 // @param file - NOT a filename, but a directory name.
163 // @return true on success
164 bool wxExtHelpController::Initialize(const wxString
& file
)
166 return LoadFile(file
);
169 bool wxExtHelpController::ParseMapFileLine(const wxString
& line
)
171 const wxChar
*p
= line
.c_str();
174 while ( isascii(*p
) && wxIsspace(*p
) )
177 // skip empty lines and comments
178 if ( *p
== wxT('\0') || *p
== WXEXTHELP_COMMENTCHAR
)
181 // the line is of the form "num url" so we must have an integer now
183 const unsigned long id
= wxStrtoul(p
, &end
, 0);
189 while ( isascii(*p
) && wxIsspace(*p
) )
192 // next should be the URL
194 url
.reserve(line
.length());
195 while ( isascii(*p
) && !wxIsspace(*p
) )
198 while ( isascii(*p
) && wxIsspace(*p
) )
201 // and finally the optional description of the entry after comment
203 if ( *p
== WXEXTHELP_COMMENTCHAR
)
206 while ( isascii(*p
) && wxIsspace(*p
) )
211 m_MapList
->Append(new wxExtHelpMapEntry(id
, url
, doc
));
217 // file is a misnomer as it's the name of the base help directory
218 bool wxExtHelpController::LoadFile(const wxString
& file
)
220 wxFileName
helpDir(wxFileName::DirName(file
));
221 helpDir
.MakeAbsolute();
223 bool dirExists
= false;
226 // If a locale is set, look in file/localename, i.e. If passed
227 // "/usr/local/myapp/help" and the current wxLocale is set to be "de", then
228 // look in "/usr/local/myapp/help/de/" first and fall back to
229 // "/usr/local/myapp/help" if that doesn't exist.
230 const wxLocale
* const loc
= wxGetLocale();
233 wxString locName
= loc
->GetName();
235 // the locale is in general of the form xx_YY.zzzz, try the full firm
236 // first and then also more general ones
237 wxFileName
helpDirLoc(helpDir
);
238 helpDirLoc
.AppendDir(locName
);
239 dirExists
= helpDirLoc
.DirExists();
243 // try without encoding
244 const wxString locNameWithoutEncoding
= locName
.BeforeLast(wxT('.'));
245 if ( !locNameWithoutEncoding
.empty() )
247 helpDirLoc
= helpDir
;
248 helpDirLoc
.AppendDir(locNameWithoutEncoding
);
249 dirExists
= helpDirLoc
.DirExists();
255 // try without country part
256 wxString locNameWithoutCountry
= locName
.BeforeLast(wxT('_'));
257 if ( !locNameWithoutCountry
.empty() )
259 helpDirLoc
= helpDir
;
260 helpDirLoc
.AppendDir(locNameWithoutCountry
);
261 dirExists
= helpDirLoc
.DirExists();
266 helpDir
= helpDirLoc
;
270 if ( ! dirExists
&& !helpDir
.DirExists() )
272 wxLogError(_("Help directory \"%s\" not found."),
273 helpDir
.GetFullPath().c_str());
277 const wxFileName
mapFile(helpDir
.GetFullPath(), WXEXTHELP_MAPFILE
);
278 if ( ! mapFile
.FileExists() )
280 wxLogError(_("Help file \"%s\" not found."),
281 mapFile
.GetFullPath().c_str());
286 m_MapList
= new wxList
;
290 if ( !input
.Open(mapFile
.GetFullPath()) )
293 for ( wxString
& line
= input
.GetFirstLine();
295 line
= input
.GetNextLine() )
297 if ( !ParseMapFileLine(line
) )
299 wxLogWarning(_("Line %lu of map file \"%s\" has invalid syntax, skipped."),
300 (unsigned long)input
.GetCurrentLine(),
301 mapFile
.GetFullPath().c_str());
305 if ( !m_NumOfEntries
)
307 wxLogError(_("No valid mappings found in the file \"%s\"."),
308 mapFile
.GetFullPath().c_str());
312 m_helpDir
= helpDir
.GetFullPath(); // now it's valid
317 bool wxExtHelpController::DisplayContents()
319 if (! m_NumOfEntries
)
323 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
324 wxExtHelpMapEntry
*entry
;
327 entry
= (wxExtHelpMapEntry
*)node
->GetData();
328 if (entry
->id
== WXEXTHELP_CONTENTS_ID
)
330 contents
= entry
->url
;
334 node
= node
->GetNext();
339 file
<< m_helpDir
<< wxFILE_SEP_PATH
<< contents
;
340 if (file
.Contains(wxT('#')))
341 file
= file
.BeforeLast(wxT('#'));
342 if (contents
.length() && wxFileExists(file
))
343 rc
= DisplaySection(WXEXTHELP_CONTENTS_ID
);
345 // if not found, open homemade toc:
346 return rc
? true : KeywordSearch(wxEmptyString
);
349 bool wxExtHelpController::DisplaySection(int sectionNo
)
351 if (! m_NumOfEntries
)
354 wxBusyCursor b
; // display a busy cursor
355 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
356 wxExtHelpMapEntry
*entry
;
359 entry
= (wxExtHelpMapEntry
*)node
->GetData();
360 if (entry
->id
== sectionNo
)
361 return DisplayHelp(entry
->url
);
362 node
= node
->GetNext();
368 bool wxExtHelpController::DisplaySection(const wxString
& section
)
370 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
373 return DisplayHelp(section
);
375 return KeywordSearch(section
);
378 bool wxExtHelpController::DisplayBlock(long blockNo
)
380 return DisplaySection((int)blockNo
);
383 bool wxExtHelpController::KeywordSearch(const wxString
& k
,
384 wxHelpSearchMode
WXUNUSED(mode
))
386 if (! m_NumOfEntries
)
389 wxString
*choices
= new wxString
[m_NumOfEntries
];
390 wxString
*urls
= new wxString
[m_NumOfEntries
];
394 bool showAll
= k
.empty();
396 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
399 // display a busy cursor
401 wxString compA
, compB
;
402 wxExtHelpMapEntry
*entry
;
404 // we compare case insensitive
413 entry
= (wxExtHelpMapEntry
*)node
->GetData();
416 bool testTarget
= ! compB
.empty();
417 if (testTarget
&& ! showAll
)
420 testTarget
= compB
.Contains(compA
);
425 urls
[idx
] = entry
->url
;
427 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
428 //if (choices[idx].empty()) // didn't contain the ';'
429 // choices[idx] = (**i).doc;
430 choices
[idx
] = wxEmptyString
;
433 wxChar targetChar
= entry
->doc
.c_str()[j
];
434 if ((targetChar
== 0) || (targetChar
== WXEXTHELP_COMMENTCHAR
))
437 choices
[idx
] << targetChar
;
443 node
= node
->GetNext();
450 wxMessageBox(_("No entries found."));
454 rc
= DisplayHelp(urls
[0]);
459 idx
= wxGetSingleChoiceIndex(_("Help Index"),
463 idx
= wxGetSingleChoiceIndex(_("Relevant entries:"),
468 rc
= DisplayHelp(urls
[idx
]);
479 bool wxExtHelpController::Quit()
484 void wxExtHelpController::OnQuit()