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__))
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__)
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")
57 /// Maximum line length in map file.
58 #define WXEXTHELP_BUFLEN 512
59 /// Character introducing comments/documentation field in map file.
60 #define WXEXTHELP_COMMENTCHAR ';'
64 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
66 /// Name of environment variable to set help browser.
67 #define WXEXTHELP_ENVVAR_BROWSER wxT("WX_HELPBROWSER")
68 /// Is browser a netscape browser?
69 #define WXEXTHELP_ENVVAR_BROWSERISNETSCAPE wxT("WX_HELPBROWSER_NS")
72 This class implements help via an external browser.
73 It requires the name of a directory containing the documentation
74 and a file mapping numerical Section numbers to relative URLS.
77 wxExtHelpController::wxExtHelpController(wxWindow
* parentWindow
):
78 wxHelpControllerBase(parentWindow
)
80 m_MapList
= (wxList
*) NULL
;
82 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
83 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
85 wxChar
*browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSER
);
88 m_BrowserName
= browser
;
89 browser
= wxGetenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
90 m_BrowserIsNetscape
= browser
&& (wxAtoi(browser
) != 0);
94 wxExtHelpController::~wxExtHelpController()
99 void wxExtHelpController::SetBrowser(const wxString
& browsername
, bool isNetscape
)
101 m_BrowserName
= browsername
;
102 m_BrowserIsNetscape
= isNetscape
;
105 // Set viewer: new, generic name for SetBrowser
106 void wxExtHelpController::SetViewer(const wxString
& viewer
, long flags
)
108 SetBrowser(viewer
, ((flags
& wxHELP_NETSCAPE
) == wxHELP_NETSCAPE
));
112 wxExtHelpController::DisplayHelp(const wxString
&relativeURL
)
114 wxBusyCursor b
; // display a busy cursor
117 #if defined(__WXMSW__)
119 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
120 bool bOk
= (int)ShellExecute(NULL
, wxT("open"), url
.c_str(),
121 NULL
, NULL
, SW_SHOWNORMAL
) > 32;
124 wxLogSysError(_("Cannot open URL '%s'"), relativeURL
.c_str());
129 #elif defined(__OS2__)
132 url
<< m_MapFile
<< '\\' << relativeURL
.BeforeFirst('#');
133 // will have to fix for OS/2, later.....DW
134 // bool bOk = (int)ShellExecute(NULL, "open", url,
135 // NULL, NULL, SW_SHOWNORMAL ) > 32;
138 // wxLogSysError(_("Cannot open URL '%s'"), relativeURL.c_str());
144 #elif defined(__DOS__)
147 command
= m_BrowserName
;
148 command
<< wxT(" file://")
149 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
150 return wxExecute(command
) != 0;
156 if(m_BrowserIsNetscape
) // try re-loading first
159 wxGetHomeDir(&lockfile
);
161 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape]lock.");
163 if(stat(lockfile
.fn_str(), &statbuf
) == 0)
165 lockfile
<< WXEXTHELP_SEPARATOR
<< wxT(".netscape/lock");
167 if(lstat(lockfile
.fn_str(), &statbuf
) == 0)
168 // cannot use wxFileExists, because it's a link pointing to a
169 // non-existing location if(wxFileExists(lockfile))
173 command
<< m_BrowserName
<< wxT(" -remote openURL(")
174 << wxT("file://") << m_MapFile
175 << WXEXTHELP_SEPARATOR
<< relativeURL
<< wxT(")");
176 success
= wxExecute(command
);
177 if(success
!= 0 ) // returns PID on success
182 command
= m_BrowserName
;
183 command
<< wxT(" file://")
184 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
185 return wxExecute(command
) != 0;
189 class wxExtHelpMapEntry
: public wxObject
195 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
196 { id
= iid
; url
= iurl
; doc
= idoc
; }
199 void wxExtHelpController::DeleteList()
203 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
206 delete (wxExtHelpMapEntry
*)node
->GetData();
207 m_MapList
->Erase(node
);
208 node
= m_MapList
->GetFirst();
211 m_MapList
= (wxList
*) NULL
;
215 /** This must be called to tell the controller where to find the
217 @param file - NOT a filename, but a directory name.
218 @return true on success
221 wxExtHelpController::Initialize(const wxString
& file
)
223 return LoadFile(file
);
227 // ifile is the name of the base help directory
228 bool wxExtHelpController::LoadFile(const wxString
& ifile
)
230 wxString mapFile
, file
, url
, doc
;
232 char buffer
[WXEXTHELP_BUFLEN
];
234 wxBusyCursor b
; // display a busy cursor
239 if(! wxIsAbsolutePath(file
))
241 wxChar
* f
= wxGetWorkingDirectory();
243 delete[] f
; // wxGetWorkingDirectory returns new memory
247 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
254 // If a locale is set, look in file/localename, i.e.
255 // If passed "/usr/local/myapp/help" and the current wxLocale is
256 // set to be "de", then look in "/usr/local/myapp/help/de/"
257 // first and fall back to "/usr/local/myapp/help" if that
259 if(wxGetLocale() && !wxGetLocale()->GetName().empty())
262 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
263 if(wxDirExists(newfile
))
267 newfile
= WXEXTHELP_SEPARATOR
;
268 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
269 while(*cptr
&& *cptr
!= wxT('_'))
270 newfile
<< *(cptr
++);
271 if(wxDirExists(newfile
))
277 if(! wxDirExists(file
))
280 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
282 else // try to reload old file
285 if(! wxFileExists(mapFile
))
289 m_MapList
= new wxList
;
292 FILE *input
= wxFopen(mapFile
,wxT("rt"));
297 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
299 len
= strlen(buffer
);
300 if(buffer
[len
-1] == '\n')
301 buffer
[len
-1] = '\0'; // cut of trailing newline
302 if(sscanf(buffer
,"%d", &id
) != 1)
304 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
305 ; // find begin of URL
307 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
308 WXEXTHELP_COMMENTCHAR
)
309 url
<< (wxChar
) buffer
[i
++];
310 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
314 doc
= wxString::FromAscii( (buffer
+ i
+ 1) ); // skip the comment character
315 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
318 }while(! feof(input
));
321 m_MapFile
= file
; // now it's valid
327 wxExtHelpController::DisplayContents()
333 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
334 wxExtHelpMapEntry
*entry
;
337 entry
= (wxExtHelpMapEntry
*)node
->GetData();
338 if(entry
->id
== CONTENTS_ID
)
340 contents
= entry
->url
;
343 node
= node
->GetNext();
348 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
349 if(file
.Contains(wxT('#')))
350 file
= file
.BeforeLast(wxT('#'));
351 if(contents
.Length() && wxFileExists(file
))
352 rc
= DisplaySection(CONTENTS_ID
);
354 // if not found, open homemade toc:
355 return rc
? true : KeywordSearch(wxEmptyString
);
359 wxExtHelpController::DisplaySection(int sectionNo
)
364 wxBusyCursor b
; // display a busy cursor
365 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
366 wxExtHelpMapEntry
*entry
;
369 entry
= (wxExtHelpMapEntry
*)node
->GetData();
370 if(entry
->id
== sectionNo
)
371 return DisplayHelp(entry
->url
);
372 node
= node
->GetNext();
377 bool wxExtHelpController::DisplaySection(const wxString
& section
)
379 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
382 return DisplayHelp(section
);
384 return KeywordSearch(section
);
388 wxExtHelpController::DisplayBlock(long blockNo
)
390 return DisplaySection((int)blockNo
);
394 wxExtHelpController::KeywordSearch(const wxString
& k
,
395 wxHelpSearchMode
WXUNUSED(mode
))
400 wxString
*choices
= new wxString
[m_NumOfEntries
];
401 wxString
*urls
= new wxString
[m_NumOfEntries
];
402 wxString compA
, compB
;
406 bool showAll
= k
.empty();
407 wxList::compatibility_iterator node
= m_MapList
->GetFirst();
408 wxExtHelpMapEntry
*entry
;
411 wxBusyCursor b
; // display a busy cursor
412 compA
= k
; compA
.LowerCase(); // we compare case insensitive
415 entry
= (wxExtHelpMapEntry
*)node
->GetData();
416 compB
= entry
->doc
; compB
.LowerCase();
417 if((showAll
|| compB
.Contains(k
)) && ! compB
.empty())
419 urls
[idx
] = entry
->url
;
421 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
422 //if(choices[idx].empty()) // didn't contain the ';'
423 // choices[idx] = (**i).doc;
424 choices
[idx
] = wxEmptyString
;
425 for(j
=0;entry
->doc
.c_str()[j
]
426 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
427 choices
[idx
] << entry
->doc
.c_str()[j
];
430 node
= node
->GetNext();
435 rc
= DisplayHelp(urls
[0]);
438 wxMessageBox(_("No entries found."));
443 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
444 showAll
? _("Help Index") : _("Entries found"),
447 rc
= DisplayHelp(urls
[idx
]);
458 bool wxExtHelpController::Quit()
463 void wxExtHelpController::OnQuit()