1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: an external help controller for wxWindows
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 # pragma implementation "wxexthlp.h"
16 #include "wx/wxprec.h"
22 #if wxUSE_HELP && !defined(__WXWINCE__)
26 #include "wx/string.h"
30 #include "wx/msgdlg.h"
31 #include "wx/choicdlg.h"
34 #include "wx/helpbase.h"
35 #include "wx/generic/helpext.h"
41 #if !defined(__WINDOWS__) && !defined(__OS2__)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 /// Name for map file.
54 #define WXEXTHELP_MAPFILE _T("wxhelp.map")
55 /// Maximum line length in map file.
56 #define WXEXTHELP_BUFLEN 512
57 /// Character introducing comments/documentation field in map file.
58 #define WXEXTHELP_COMMENTCHAR ';'
62 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
64 /// Name of environment variable to set help browser.
65 #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
66 /// Is browser a netscape browser?
67 #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
70 This class implements help via an external browser.
71 It requires the name of a directory containing the documentation
72 and a file mapping numerical Section numbers to relative URLS.
75 wxExtHelpController::wxExtHelpController()
77 m_MapList
= (wxList
*) NULL
;
79 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
80 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
82 wxChar
*browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSER
);
85 m_BrowserName
= browser
;
86 browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
87 m_BrowserIsNetscape
= browser
&& (wxAtoi(browser
) != 0);
91 wxExtHelpController::~wxExtHelpController()
96 void wxExtHelpController::SetBrowser(const wxString
& browsername
, bool isNetscape
)
98 m_BrowserName
= browsername
;
99 m_BrowserIsNetscape
= isNetscape
;
102 // Set viewer: new, generic name for SetBrowser
103 void wxExtHelpController::SetViewer(const wxString
& viewer
, long flags
)
105 SetBrowser(viewer
, ((flags
& wxHELP_NETSCAPE
) == wxHELP_NETSCAPE
));
109 wxExtHelpController::DisplayHelp(const wxString
&relativeURL
)
111 wxBusyCursor b
; // display a busy cursor
114 #if defined(__WXMSW__)
116 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
117 bool bOk
= (int)ShellExecute(NULL
, wxT("open"), url
.c_str(),
118 NULL
, NULL
, SW_SHOWNORMAL
) > 32;
121 wxLogSysError(_("Cannot open URL '%s'"), relativeURL
.c_str());
127 #elif defined(__WXPM__)
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;
136 // wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
142 #elif defined(__DOS__)
145 command
= m_BrowserName
;
146 command
<< wxT(" file://")
147 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
148 return wxExecute(command
) != 0;
154 if(m_BrowserIsNetscape
) // try re-loading first
157 wxGetHomeDir(&lockfile
);
159 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape]lock.");
161 if(stat(lockfile
.fn_str(), &statbuf
) == 0)
163 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape/lock");
165 if(lstat(lockfile
.fn_str(), &statbuf
) == 0)
166 // cannot use wxFileExists, because it's a link pointing to a
167 // non-existing location if(wxFileExists(lockfile))
171 command
<< m_BrowserName
<< wxT(" -remote openURL(")
172 << wxT("file://") << m_MapFile
173 << WXEXTHELP_SEPARATOR
<< relativeURL
<< wxT(")");
174 success
= wxExecute(command
);
175 if(success
!= 0 ) // returns PID on success
180 command
= m_BrowserName
;
181 command
<< wxT(" file://")
182 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
183 return wxExecute(command
) != 0;
187 class wxExtHelpMapEntry
: public wxObject
193 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
194 { id
= iid
; url
= iurl
; doc
= idoc
; }
197 void wxExtHelpController::DeleteList()
201 wxNode
*node
= m_MapList
->GetFirst();
204 delete (wxExtHelpMapEntry
*)node
->GetData();
206 node
= m_MapList
->GetFirst();
209 m_MapList
= (wxList
*) NULL
;
213 /** This must be called to tell the controller where to find the
215 @param file - NOT a filename, but a directory name.
216 @return true on success
219 wxExtHelpController::Initialize(const wxString
& file
)
221 return LoadFile(file
);
225 // ifile is the name of the base help directory
226 bool wxExtHelpController::LoadFile(const wxString
& ifile
)
228 wxString mapFile
, file
, url
, doc
;
230 char buffer
[WXEXTHELP_BUFLEN
];
232 wxBusyCursor b
; // display a busy cursor
234 if(! ifile
.IsEmpty())
237 if(! wxIsAbsolutePath(file
))
239 wxChar
* f
= wxGetWorkingDirectory();
241 delete[] f
; // wxGetWorkingDirectory returns new memory
245 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
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
257 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
260 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
261 if(wxDirExists(newfile
))
265 newfile
= WXEXTHELP_SEPARATOR
;
266 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
267 while(*cptr
&& *cptr
!= wxT('_'))
268 newfile
<< *(cptr
++);
269 if(wxDirExists(newfile
))
275 if(! wxDirExists(file
))
278 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
280 else // try to reload old file
283 if(! wxFileExists(mapFile
))
287 m_MapList
= new wxList
;
290 FILE *input
= wxFopen(mapFile
,wxT("rt"));
295 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
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)
302 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
303 ; // find begin of URL
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
)
312 doc
= wxString::FromAscii( (buffer
+ i
+ 1) ); // skip the comment character
313 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
316 }while(! feof(input
));
319 m_MapFile
= file
; // now it's valid
325 wxExtHelpController::DisplayContents()
331 wxNode
*node
= m_MapList
->GetFirst();
332 wxExtHelpMapEntry
*entry
;
335 entry
= (wxExtHelpMapEntry
*)node
->GetData();
336 if(entry
->id
== CONTENTS_ID
)
338 contents
= entry
->url
;
341 node
= node
->GetNext();
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
);
352 // if not found, open homemade toc:
353 return rc
? TRUE
: KeywordSearch(wxT(""));
357 wxExtHelpController::DisplaySection(int sectionNo
)
362 wxBusyCursor b
; // display a busy cursor
363 wxNode
*node
= m_MapList
->GetFirst();
364 wxExtHelpMapEntry
*entry
;
367 entry
= (wxExtHelpMapEntry
*)node
->GetData();
368 if(entry
->id
== sectionNo
)
369 return DisplayHelp(entry
->url
);
370 node
= node
->GetNext();
375 bool wxExtHelpController::DisplaySection(const wxString
& section
)
377 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
380 return DisplayHelp(section
);
382 return KeywordSearch(section
);
386 wxExtHelpController::DisplayBlock(long blockNo
)
388 return DisplaySection((int)blockNo
);
392 wxExtHelpController::KeywordSearch(const wxString
& k
)
397 wxString
*choices
= new wxString
[m_NumOfEntries
];
398 wxString
*urls
= new wxString
[m_NumOfEntries
];
399 wxString compA
, compB
;
403 bool showAll
= k
.IsEmpty();
404 wxNode
*node
= m_MapList
->GetFirst();
405 wxExtHelpMapEntry
*entry
;
408 wxBusyCursor b
; // display a busy cursor
409 compA
= k
; compA
.LowerCase(); // we compare case insensitive
412 entry
= (wxExtHelpMapEntry
*)node
->GetData();
413 compB
= entry
->doc
; compB
.LowerCase();
414 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
416 urls
[idx
] = entry
->url
;
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
];
427 node
= node
->GetNext();
432 rc
= DisplayHelp(urls
[0]);
435 wxMessageBox(_("No entries found."));
440 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
441 showAll
? _("Help Index") : _("Entries found"),
444 rc
= DisplayHelp(urls
[idx
]);
455 bool wxExtHelpController::Quit()
460 void wxExtHelpController::OnQuit()