1 /*-*- c++ -*-********************************************************
2 * wxexthlp.cpp - an external help controller for wxWindows *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
9 # pragma implementation "wxexthlp.h"
13 #include "wx/helpbase.h"
14 #include "wx/generic/helpext.h"
15 #include "wx/string.h"
25 class wxExtHelpMapEntry
: public wxObject
31 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
32 { id
= iid
; url
= iurl
; doc
= idoc
; }
38 wxBusyCursor() { wxBeginBusyCursor(); }
39 ~wxBusyCursor() { wxEndBusyCursor(); }
42 IMPLEMENT_CLASS(wxExtHelpController
, wxHelpControllerBase
)
45 This class implements help via an external browser.
46 It requires the name of a directory containing the documentation
47 and a file mapping numerical Section numbers to relative URLS.
50 wxExtHelpController::wxExtHelpController(void)
53 m_BrowserName
= WXEXTHELP_DEFAULTBROWSER
;
54 m_BrowserIsNetscape
= WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE
;
56 char *browser
= getenv(WXEXTHELP_ENVVAR_BROWSER
);
59 m_BrowserName
= browser
;
60 browser
= getenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE
);
61 m_BrowserIsNetscape
= browser
&& (atoi(browser
) != 0);
66 wxExtHelpController::DeleteList(void)
70 wxNode
*node
= m_MapList
->First();
73 delete (wxExtHelpMapEntry
*)node
->Data();
75 node
= m_MapList
->First();
82 wxExtHelpController::~wxExtHelpController(void)
88 wxExtHelpController::SetBrowser(wxString
const & browsername
, bool isNetscape
)
90 m_BrowserName
= browsername
;
91 m_BrowserIsNetscape
= isNetscape
;
94 /** This must be called to tell the controller where to find the
96 @param file - NOT a filename, but a directory name.
97 @return true on success
100 wxExtHelpController::Initialize(const wxString
& file
)
102 return LoadFile(file
);
107 wxExtHelpController::LoadFile(const wxString
& ifile
= "")
109 wxString mapFile
, file
, url
, doc
;
111 char buffer
[WXEXTHELP_BUFLEN
];
113 wxBusyCursor b
; // display a busy cursor
115 if(! ifile
.IsEmpty())
118 if(! wxIsAbsolutePath(file
))
120 file
= wxGetWorkingDirectory();
121 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
126 if(! wxDirExists(file
))
129 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
131 else // try to reload old file
134 if(! wxFileExists(mapFile
))
138 m_MapList
= new wxList
;
141 FILE *input
= fopen(mapFile
.c_str(),"rt");
146 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
148 len
= strlen(buffer
);
149 if(buffer
[len
-1] == '\n')
150 buffer
[len
-1] = '\0'; // cut of trailing newline
151 if(sscanf(buffer
,"%d", &id
) != 1)
153 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
]); i
++)
154 ; // find begin of URL
156 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
157 WXEXTHELP_COMMENTCHAR
)
159 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
163 doc
= (buffer
+ i
+ 1); // skip the comment character
164 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
169 }while(! feof(input
));
172 m_MapFile
= file
; // now it's valid
177 wxExtHelpController::CallBrowser(wxString
const &relativeURL
)
179 wxBusyCursor b
; // display a busy cursor
182 if(m_BrowserIsNetscape
) // try re-loading first
185 wxGetHomeDir(&lockfile
);
186 lockfile
<< WXEXTHELP_SEPARATOR
<< ".netscape/lock";
188 if(lstat(lockfile
.c_str(), &statbuf
) == 0)
189 // cannot use wxFileExists, because it's a link pointing to a
190 // non-existing location if(wxFileExists(lockfile))
193 command
<< m_BrowserName
<< " -remote openURL("
194 << "file://" << m_MapFile
195 << WXEXTHELP_SEPARATOR
<< relativeURL
<< ")";
196 success
= wxExecute(command
);
197 if(success
!= 0 ) // returns PID on success
201 command
= m_BrowserName
;
202 command
<< " file://"
203 << m_MapFile
<< WXEXTHELP_SEPARATOR
<< relativeURL
;
204 return wxExecute(command
) != 0;
208 wxExtHelpController::DisplayContents(void)
212 wxBusyCursor b
; // display a busy cursor
213 return KeywordSearch("");
217 wxExtHelpController::DisplaySection(int sectionNo
)
222 wxBusyCursor b
; // display a busy cursor
223 wxNode
*node
= m_MapList
->First();
224 wxExtHelpMapEntry
*entry
;
227 entry
= (wxExtHelpMapEntry
*)node
->Data();
228 if(entry
->id
== sectionNo
)
229 return CallBrowser(entry
->url
);
236 wxExtHelpController::DisplayBlock(long blockNo
)
238 return DisplaySection((int)blockNo
);
242 wxExtHelpController::KeywordSearch(const wxString
& k
)
247 wxBusyCursor b
; // display a busy cursor
248 wxString
*choices
= new wxString
[m_NumOfEntries
];
249 wxString
*urls
= new wxString
[m_NumOfEntries
];
250 wxString compA
, compB
;
254 bool showAll
= k
.IsEmpty();
255 wxNode
*node
= m_MapList
->First();
256 wxExtHelpMapEntry
*entry
;
258 compA
= k
; compA
.LowerCase(); // we compare case insensitive
261 entry
= (wxExtHelpMapEntry
*)node
->Data();
262 compB
= entry
->doc
; compB
.LowerCase();
263 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
265 urls
[idx
] = entry
->url
;
267 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
268 //if(choices[idx].IsEmpty()) // didn't contain the ';'
269 // choices[idx] = (**i).doc;
271 for(j
=0;entry
->doc
.c_str()[j
]
272 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
273 choices
[idx
] << entry
->doc
.c_str()[j
];
280 rc
= CallBrowser(urls
[0]);
283 wxMessageBox(_("No entries found."));
288 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
289 showAll
? _("Help Index") : _("Entries found"),
292 rc
= CallBrowser(urls
[idx
]);
304 wxExtHelpController::Quit(void)
310 wxExtHelpController::OnQuit(void)