]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helpext.cpp
another i.c.e.
[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 m_NumOfEntries = 0;
56
57 char *browser = getenv(WXEXTHELP_ENVVAR_BROWSER);
58 if(browser)
59 {
60 m_BrowserName = browser;
61 browser = getenv(WXEXTHELP_ENVVAR_BROWSERISNETSCAPE);
62 m_BrowserIsNetscape = browser && (atoi(browser) != 0);
63 }
64 }
65
66 void
67 wxExtHelpController::DeleteList(void)
68 {
69 if(m_MapList)
70 {
71 wxNode *node = m_MapList->First();
72 while (node)
73 {
74 delete (wxExtHelpMapEntry *)node->Data();
75 delete node;
76 node = m_MapList->First();
77 }
78 delete m_MapList;
79 m_MapList = NULL;
80 }
81 }
82
83 wxExtHelpController::~wxExtHelpController(void)
84 {
85 DeleteList();
86 }
87
88 void
89 wxExtHelpController::SetBrowser(wxString const & browsername, bool isNetscape)
90 {
91 m_BrowserName = browsername;
92 m_BrowserIsNetscape = isNetscape;
93 }
94
95 /** This must be called to tell the controller where to find the
96 documentation.
97 @param file - NOT a filename, but a directory name.
98 @return true on success
99 */
100 bool
101 wxExtHelpController::Initialize(const wxString& file)
102 {
103 return LoadFile(file);
104 }
105
106
107 bool
108 wxExtHelpController::LoadFile(const wxString& ifile = "")
109 {
110 wxString mapFile, file, url, doc;
111 int id,i,len;
112 char buffer[WXEXTHELP_BUFLEN];
113
114 wxBusyCursor b; // display a busy cursor
115
116 if(! ifile.IsEmpty())
117 {
118 file = ifile;
119 if(! wxIsAbsolutePath(file))
120 {
121 file = wxGetWorkingDirectory();
122 file << WXEXTHELP_SEPARATOR << ifile;
123 }
124 else
125 file = ifile;
126
127 if(! wxDirExists(file))
128 return false;
129
130 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
131 }
132 else // try to reload old file
133 mapFile = m_MapFile;
134
135 if(! wxFileExists(mapFile))
136 return false;
137
138 DeleteList();
139 m_MapList = new wxList;
140 m_NumOfEntries = 0;
141
142 FILE *input = fopen(mapFile.c_str(),"rt");
143 if(! input)
144 return false;
145 do
146 {
147 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
148 {
149 len = strlen(buffer);
150 if(buffer[len-1] == '\n')
151 buffer[len-1] = '\0'; // cut of trailing newline
152 if(sscanf(buffer,"%d", &id) != 1)
153 break; // error
154 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
155 ; // find begin of URL
156 url = "";
157 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
158 WXEXTHELP_COMMENTCHAR)
159 url << buffer[i++];
160 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
161 i++;
162 doc = "";
163 if(buffer[i])
164 doc = (buffer + i + 1); // skip the comment character
165 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
166 m_NumOfEntries++;
167 }
168 }while(! feof(input));
169 fclose(input);
170
171 m_MapFile = file; // now it's valid
172 return true;
173 }
174
175 bool
176 wxExtHelpController::CallBrowser(wxString const &relativeURL)
177 {
178 wxBusyCursor b; // display a busy cursor
179 wxString command;
180
181 if(m_BrowserIsNetscape) // try re-loading first
182 {
183 wxString lockfile;
184 wxGetHomeDir(&lockfile);
185 lockfile << WXEXTHELP_SEPARATOR << ".netscape/lock";
186 struct stat statbuf;
187 if(lstat(lockfile.c_str(), &statbuf) == 0)
188 // cannot use wxFileExists, because it's a link pointing to a
189 // non-existing location if(wxFileExists(lockfile))
190 {
191 long success;
192 command << m_BrowserName << " -remote openURL("
193 << "file://" << m_MapFile
194 << WXEXTHELP_SEPARATOR << relativeURL << ")";
195 success = wxExecute(command);
196 if(success != 0 ) // returns PID on success
197 return true;
198 }
199 }
200 command = m_BrowserName;
201 command << " file://"
202 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
203 return wxExecute(command) != 0;
204 }
205
206 bool
207 wxExtHelpController::DisplayContents(void)
208 {
209 if(! m_NumOfEntries)
210 return false;
211 wxBusyCursor b; // display a busy cursor
212 return KeywordSearch("");
213 }
214
215 bool
216 wxExtHelpController::DisplaySection(int sectionNo)
217 {
218 if(! m_NumOfEntries)
219 return false;
220
221 wxBusyCursor b; // display a busy cursor
222 wxNode *node = m_MapList->First();
223 wxExtHelpMapEntry *entry;
224 while(node)
225 {
226 entry = (wxExtHelpMapEntry *)node->Data();
227 if(entry->id == sectionNo)
228 return CallBrowser(entry->url);
229 node = node->Next();
230 }
231 return false;
232 }
233
234 bool
235 wxExtHelpController::DisplayBlock(long blockNo)
236 {
237 return DisplaySection((int)blockNo);
238 }
239
240 bool
241 wxExtHelpController::KeywordSearch(const wxString& k)
242 {
243 if(! m_NumOfEntries)
244 return false;
245
246 wxBusyCursor b; // display a busy cursor
247 wxString *choices = new wxString[m_NumOfEntries];
248 wxString *urls = new wxString[m_NumOfEntries];
249 wxString compA, compB;
250
251 int idx = 0, j;
252 bool rc;
253 bool showAll = k.IsEmpty();
254 wxNode *node = m_MapList->First();
255 wxExtHelpMapEntry *entry;
256
257 compA = k; compA.LowerCase(); // we compare case insensitive
258 while(node)
259 {
260 entry = (wxExtHelpMapEntry *)node->Data();
261 compB = entry->doc; compB.LowerCase();
262 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
263 {
264 urls[idx] = entry->url;
265 // doesn't work:
266 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
267 //if(choices[idx].IsEmpty()) // didn't contain the ';'
268 // choices[idx] = (**i).doc;
269 choices[idx] = "";
270 for(j=0;entry->doc.c_str()[j]
271 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
272 choices[idx] << entry->doc.c_str()[j];
273 idx++;
274 }
275 node = node->Next();
276 }
277
278 if(idx == 1)
279 rc = CallBrowser(urls[0]);
280 else if(idx == 0)
281 {
282 wxMessageBox(_("No entries found."));
283 rc = false;
284 }
285 else
286 {
287 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
288 showAll ? _("Help Index") : _("Entries found"),
289 idx,choices);
290 if(idx != -1)
291 rc = CallBrowser(urls[idx]);
292 else
293 rc = false;
294 }
295 delete[] urls;
296 delete[] choices;
297
298 return rc;
299 }
300
301
302 bool
303 wxExtHelpController::Quit(void)
304 {
305 return true;
306 }
307
308 void
309 wxExtHelpController::OnQuit(void)
310 {
311 }
312