1 /////////////////////////////////////////////////////////////////////////////
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__))
22 #include "wx/string.h"
26 #include "wx/msgdlg.h"
27 #include "wx/choicdlg.h"
31 #include "wx/helpbase.h"
32 #include "wx/generic/helpext.h"
38 #if !defined(__WINDOWS__) && !defined(__OS2__)
44 #include "wx/msw/winundef.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 /// Name for map file.
52 #define WXEXTHELP_MAPFILE _T("wxhelp.map")
53 /// Maximum line length in map file.
54 #define WXEXTHELP_BUFLEN 512
55 /// Character introducing comments/documentation field in map file.
56 #define WXEXTHELP_COMMENTCHAR ';'
60 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
62 /// Name of environment variable to set help browser.
63 #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
64 /// Is browser a netscape browser?
65 #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
68 This class implements help via an external browser.
69 It requires the name of a directory containing the documentation
70 and a file mapping numerical Section numbers to relative URLS.
73 wxExtHelpController::wxExtHelpController()
75 m_MapList
= (wxList
*) NULL
;
77 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
78 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
80 wxChar
*browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSER
);
83 m_BrowserName
= browser
;
84 browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
85 m_BrowserIsNetscape
= browser
&& (wxAtoi(browser
) != 0);
89 wxExtHelpController::~wxExtHelpController()
94 void wxExtHelpController::SetBrowser(const wxString
& browsername
, bool isNetscape
)
96 m_BrowserName
= browsername
;
97 m_BrowserIsNetscape
= isNetscape
;
100 // Set viewer: new, generic name for SetBrowser
101 void wxExtHelpController::SetViewer(const wxString
& viewer
, long flags
)
103 SetBrowser(viewer
, ((flags
& wxHELP_NETSCAPE
) == wxHELP_NETSCAPE
));
107 wxExtHelpController::DisplayHelp(const wxString
&relativeURL
)
109 wxBusyCursor b
; // display a busy cursor
112 #if defined(__WXMSW__)
114 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
115 bool bOk
= (int)ShellExecute(NULL
, wxT("open"), url
.c_str(),
116 NULL
, NULL
, SW_SHOWNORMAL
) > 32;
119 wxLogSysError(_("Cannot open URL '%s'"), relativeURL
.c_str());
124 #elif defined(__WXPM__)
127 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
128 // will have to fix for OS/2, later.....DW
129 // bool bOk = (int)ShellExecute(NULL, "open", url,
130 // NULL, NULL, SW_SHOWNORMAL ) > 32;
133 // wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
139 #elif defined(__DOS__)
142 command
= m_BrowserName
;
143 command
<< wxT(" file://")
144 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
145 return wxExecute(command
) != 0;
151 if(m_BrowserIsNetscape
) // try re-loading first
154 wxGetHomeDir(&lockfile
);
156 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape]lock.");
158 if(stat(lockfile
.fn_str(), &statbuf
) == 0)
160 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape/lock");
162 if(lstat(lockfile
.fn_str(), &statbuf
) == 0)
163 // cannot use wxFileExists, because it's a link pointing to a
164 // non-existing location if(wxFileExists(lockfile))
168 command
<< m_BrowserName
<< wxT(" -remote openURL(")
169 << wxT("file://") << m_MapFile
170 << WXEXTHELP_SEPARATOR
<< relativeURL
<< wxT(")");
171 success
= wxExecute(command
);
172 if(success
!= 0 ) // returns PID on success
177 command
= m_BrowserName
;
178 command
<< wxT(" file://")
179 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
180 return wxExecute(command
) != 0;
184 class wxExtHelpMapEntry
: public wxObject
190 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
191 { id
= iid
; url
= iurl
; doc
= idoc
; }
194 void wxExtHelpController::DeleteList()
198 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
201 delete (wxExtHelpMapEntry
*)node
->GetData();
202 m_MapList
->Erase(node
);
203 node
= m_MapList
->GetFirst();
206 m_MapList
= (wxList
*) NULL
;
210 /** This must be called to tell the controller where to find the
212 @param file - NOT a filename, but a directory name.
213 @return true on success
216 wxExtHelpController::Initialize(const wxString
& file
)
218 return LoadFile(file
);
222 // ifile is the name of the base help directory
223 bool wxExtHelpController::LoadFile(const wxString
& ifile
)
225 wxString mapFile
, file
, url
, doc
;
227 char buffer
[WXEXTHELP_BUFLEN
];
229 wxBusyCursor b
; // display a busy cursor
234 if(! wxIsAbsolutePath(file
))
236 wxChar
* f
= wxGetWorkingDirectory();
238 delete[] f
; // wxGetWorkingDirectory returns new memory
242 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
249 // If a locale is set, look in file/localename, i.e.
250 // If passed "/usr/local/myapp/help" and the current wxLocale is
251 // set to be "de", then look in "/usr/local/myapp/help/de/"
252 // first and fall back to "/usr/local/myapp/help" if that
254 if(wxGetLocale() && !wxGetLocale()->GetName().empty())
257 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
258 if(wxDirExists(newfile
))
262 newfile
= WXEXTHELP_SEPARATOR
;
263 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
264 while(*cptr
&& *cptr
!= wxT('_'))
265 newfile
<< *(cptr
++);
266 if(wxDirExists(newfile
))
272 if(! wxDirExists(file
))
275 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
277 else // try to reload old file
280 if(! wxFileExists(mapFile
))
284 m_MapList
= new wxList
;
287 FILE *input
= wxFopen(mapFile
,wxT("rt"));
292 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
294 len
= strlen(buffer
);
295 if(buffer
[len
-1] == '\n')
296 buffer
[len
-1] = '\0'; // cut of trailing newline
297 if(sscanf(buffer
,"%d", &id
) != 1)
299 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
300 ; // find begin of URL
302 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
303 WXEXTHELP_COMMENTCHAR
)
304 url
<< (wxChar
) buffer
[i
++];
305 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
309 doc
= wxString::FromAscii( (buffer
+ i
+ 1) ); // skip the comment character
310 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
313 }while(! feof(input
));
316 m_MapFile
= file
; // now it's valid
322 wxExtHelpController::DisplayContents()
328 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
329 wxExtHelpMapEntry
*entry
;
332 entry
= (wxExtHelpMapEntry
*)node
->GetData();
333 if(entry
->id
== CONTENTS_ID
)
335 contents
= entry
->url
;
338 node
= node
->GetNext();
343 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
344 if(file
.Contains(wxT('#')))
345 file
= file
.BeforeLast(wxT('#'));
346 if(contents
.Length() && wxFileExists(file
))
347 rc
= DisplaySection(CONTENTS_ID
);
349 // if not found, open homemade toc:
350 return rc
? true : KeywordSearch(wxEmptyString
);
354 wxExtHelpController::DisplaySection(int sectionNo
)
359 wxBusyCursor b
; // display a busy cursor
360 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
361 wxExtHelpMapEntry
*entry
;
364 entry
= (wxExtHelpMapEntry
*)node
->GetData();
365 if(entry
->id
== sectionNo
)
366 return DisplayHelp(entry
->url
);
367 node
= node
->GetNext();
372 bool wxExtHelpController::DisplaySection(const wxString
& section
)
374 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
377 return DisplayHelp(section
);
379 return KeywordSearch(section
);
383 wxExtHelpController::DisplayBlock(long blockNo
)
385 return DisplaySection((int)blockNo
);
389 wxExtHelpController::KeywordSearch(const wxString
& k
,
390 wxHelpSearchMode
WXUNUSED(mode
))
395 wxString
*choices
= new wxString
[m_NumOfEntries
];
396 wxString
*urls
= new wxString
[m_NumOfEntries
];
397 wxString compA
, compB
;
401 bool showAll
= k
.empty();
402 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
403 wxExtHelpMapEntry
*entry
;
406 wxBusyCursor b
; // display a busy cursor
407 compA
= k
; compA
.LowerCase(); // we compare case insensitive
410 entry
= (wxExtHelpMapEntry
*)node
->GetData();
411 compB
= entry
->doc
; compB
.LowerCase();
412 if((showAll
|| compB
.Contains(k
)) && ! compB
.empty())
414 urls
[idx
] = entry
->url
;
416 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
417 //if(choices[idx].empty()) // didn't contain the ';'
418 // choices[idx] = (**i).doc;
419 choices
[idx
] = wxEmptyString
;
420 for(j
=0;entry
->doc
.c_str()[j
]
421 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
422 choices
[idx
] << entry
->doc
.c_str()[j
];
425 node
= node
->GetNext();
430 rc
= DisplayHelp(urls
[0]);
433 wxMessageBox(_("No entries found."));
438 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
439 showAll
? _("Help Index") : _("Entries found"),
442 rc
= DisplayHelp(urls
[idx
]);
453 bool wxExtHelpController::Quit()
458 void wxExtHelpController::OnQuit()