]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpext.cpp
Replaced kbList with wxList.
[wxWidgets.git] / src / generic / helpext.cpp
1 /*-*- c++ -*-********************************************************
2 * wxexthlp.cpp - an external help controller for wxWindows *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8 #ifdef __GNUG__
9 # pragma implementation "wxexthlp.h"
10 #endif
11
12 #include "wx/setup.h"
13 #include "wx/helpbase.h"
14 #include "wx/generic/helpext.h"
15 #include "wx/string.h"
16 #include "wx/utils.h"
17 #include "wx/list.h"
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23
24
25 class wxExtHelpMapEntry : public wxObject
26 {
27 public:
28 int id;
29 wxString url;
30 wxString doc;
31 wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc)
32 { id = iid; url = iurl; doc = idoc; }
33 };
34
35
36 struct wxBusyCursor
37 {
38 wxBusyCursor() { wxBeginBusyCursor(); }
39 ~wxBusyCursor() { wxEndBusyCursor(); }
40 };
41
42 IMPLEMENT_CLASS(wxExtHelpController, wxHelpControllerBase)
43
44 /**
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.
48 */
49
50 wxExtHelpController::wxExtHelpController(void)
51 {
52 m_MapList = NULL;
53 m_BrowserName = WXEXTHELP_DEFAULTBROWSER;
54 m_BrowserIsNetscape = WXEXTHELP_DEFAULTBROWSER_IS_NETSCAPE;
55
56 char *browser = getenv(WXEXTHELP_ENVVAR_BROWSER);
57 if(browser)
58 {
59 m_BrowserName = browser;
60 browser = getenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
61 m_BrowserIsNetscape = browser && (atoi(browser) != 0);
62 }
63 }
64
65 void
66 wxExtHelpController::DeleteList(void)
67 {
68 if(m_MapList)
69 {
70 wxNode *node = m_MapList->First();
71 while (node)
72 {
73 delete (wxExtHelpMapEntry *)node->Data();
74 delete node;
75 node = m_MapList->First();
76 }
77 delete m_MapList;
78 m_MapList = NULL;
79 }
80 }
81
82 wxExtHelpController::~wxExtHelpController(void)
83 {
84 DeleteList();
85 }
86
87 void
88 wxExtHelpController::SetBrowser(wxString const & browsername, bool isNetscape)
89 {
90 m_BrowserName = browsername;
91 m_BrowserIsNetscape = isNetscape;
92 }
93
94 /** This must be called to tell the controller where to find the
95 documentation.
96 @param file - NOT a filename, but a directory name.
97 @return true on success
98 */
99 bool
100 wxExtHelpController::Initialize(const wxString& file)
101 {
102 return LoadFile(file);
103 }
104
105
106 bool
107 wxExtHelpController::LoadFile(const wxString& ifile = "")
108 {
109 wxString mapFile, file, url, doc;
110 int id,i,len;
111 char buffer[WXEXTHELP_BUFLEN];
112
113 wxBusyCursor b; // display a busy cursor
114
115 if(! ifile.IsEmpty())
116 {
117 file = ifile;
118 if(! wxIsAbsolutePath(file))
119 {
120 file = wxGetWorkingDirectory();
121 file << WXEXTHELP_SEPARATOR << ifile;
122 }
123 else
124 file = ifile;
125
126 if(! wxDirExists(file))
127 return false;
128
129 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
130 }
131 else // try to reload old file
132 mapFile = m_MapFile;
133
134 if(! wxFileExists(mapFile))
135 return false;
136
137 DeleteList();
138 m_MapList = new wxList;
139 m_NumOfEntries = 0;
140
141 FILE *input = fopen(mapFile.c_str(),"rt");
142 if(! input)
143 return false;
144 do
145 {
146 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
147 {
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)
152 break; // error
153 for(i=0; isdigit(buffer[i])||isspace(buffer[i]); i++)
154 ; // find begin of URL
155 url = "";
156 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
157 WXEXTHELP_COMMENTCHAR)
158 url << buffer[i++];
159 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
160 i++;
161 doc = "";
162 if(buffer[i])
163 doc = (buffer + i + 1); // skip the comment character
164 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
165 m_NumOfEntries++;
166 }
167 else
168 perror("");
169 }while(! feof(input));
170 fclose(input);
171
172 m_MapFile = file; // now it's valid
173 return true;
174 }
175
176 bool
177 wxExtHelpController::CallBrowser(wxString const &relativeURL)
178 {
179 wxBusyCursor b; // display a busy cursor
180 wxString command;
181
182 if(m_BrowserIsNetscape) // try re-loading first
183 {
184 wxString lockfile;
185 wxGetHomeDir(&lockfile);
186 lockfile << WXEXTHELP_SEPARATOR << ".netscape/lock";
187 struct stat statbuf;
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))
191 {
192 long success;
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
198 return true;
199 }
200 }
201 command = m_BrowserName;
202 command << " file://"
203 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
204 return wxExecute(command) != 0;
205 }
206
207 bool
208 wxExtHelpController::DisplayContents(void)
209 {
210 if(! m_NumOfEntries)
211 return false;
212 wxBusyCursor b; // display a busy cursor
213 return KeywordSearch("");
214 }
215
216 bool
217 wxExtHelpController::DisplaySection(int sectionNo)
218 {
219 if(! m_NumOfEntries)
220 return false;
221
222 wxBusyCursor b; // display a busy cursor
223 wxNode *node = m_MapList->First();
224 wxExtHelpMapEntry *entry;
225 while(node)
226 {
227 entry = (wxExtHelpMapEntry *)node->Data();
228 if(entry->id == sectionNo)
229 return CallBrowser(entry->url);
230 node = node->Next();
231 }
232 return false;
233 }
234
235 bool
236 wxExtHelpController::DisplayBlock(long blockNo)
237 {
238 return DisplaySection((int)blockNo);
239 }
240
241 bool
242 wxExtHelpController::KeywordSearch(const wxString& k)
243 {
244 if(! m_NumOfEntries)
245 return false;
246
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;
251
252 int idx = 0, j;
253 bool rc;
254 bool showAll = k.IsEmpty();
255 wxNode *node = m_MapList->First();
256 wxExtHelpMapEntry *entry;
257
258 compA = k; compA.LowerCase(); // we compare case insensitive
259 while(node)
260 {
261 entry = (wxExtHelpMapEntry *)node->Data();
262 compB = entry->doc; compB.LowerCase();
263 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
264 {
265 urls[idx] = entry->url;
266 // doesn't work:
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;
270 choices[idx] = "";
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];
274 idx++;
275 }
276 node = node->Next();
277 }
278
279 if(idx == 1)
280 rc = CallBrowser(urls[0]);
281 else if(idx == 0)
282 {
283 wxMessageBox(_("No entries found."));
284 rc = false;
285 }
286 else
287 {
288 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
289 showAll ? _("Help Index") : _("Entries found"),
290 idx,choices);
291 if(idx != -1)
292 rc = CallBrowser(urls[idx]);
293 else
294 rc = false;
295 }
296 delete[] urls;
297 delete[] choices;
298
299 return rc;
300 }
301
302
303 bool
304 wxExtHelpController::Quit(void)
305 {
306 return true;
307 }
308
309 void
310 wxExtHelpController::OnQuit(void)
311 {
312 }
313