Various wxMotif changes including size optimisation and debugging operator fix.
[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 char* f = wxGetWorkingDirectory();
122 file = f;
123 delete[] f; // wxGetWorkingDirectory returns new memory
124 file << WXEXTHELP_SEPARATOR << ifile;
125 }
126 else
127 file = ifile;
128
129 if(! wxDirExists(file))
130 return false;
131
132 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
133 }
134 else // try to reload old file
135 mapFile = m_MapFile;
136
137 if(! wxFileExists(mapFile))
138 return false;
139
140 DeleteList();
141 m_MapList = new wxList;
142 m_NumOfEntries = 0;
143
144 FILE *input = fopen(mapFile.c_str(),"rt");
145 if(! input)
146 return false;
147 do
148 {
149 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
150 {
151 len = strlen(buffer);
152 if(buffer[len-1] == '\n')
153 buffer[len-1] = '\0'; // cut of trailing newline
154 if(sscanf(buffer,"%d", &id) != 1)
155 break; // error
156 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
157 ; // find begin of URL
158 url = "";
159 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
160 WXEXTHELP_COMMENTCHAR)
161 url << buffer[i++];
162 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
163 i++;
164 doc = "";
165 if(buffer[i])
166 doc = (buffer + i + 1); // skip the comment character
167 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
168 m_NumOfEntries++;
169 }
170 }while(! feof(input));
171 fclose(input);
172
173 m_MapFile = file; // now it's valid
174 return true;
175 }
176
177 bool
178 wxExtHelpController::CallBrowser(wxString const &relativeURL)
179 {
180 wxBusyCursor b; // display a busy cursor
181 wxString command;
182
183 if(m_BrowserIsNetscape) // try re-loading first
184 {
185 wxString lockfile;
186 wxGetHomeDir(&lockfile);
187 lockfile << WXEXTHELP_SEPARATOR << ".netscape/lock";
188 struct stat statbuf;
189 if(lstat(lockfile.c_str(), &statbuf) == 0)
190 // cannot use wxFileExists, because it's a link pointing to a
191 // non-existing location if(wxFileExists(lockfile))
192 {
193 long success;
194 command << m_BrowserName << " -remote openURL("
195 << "file://" << m_MapFile
196 << WXEXTHELP_SEPARATOR << relativeURL << ")";
197 success = wxExecute(command);
198 if(success != 0 ) // returns PID on success
199 return true;
200 }
201 }
202 command = m_BrowserName;
203 command << " file://"
204 << m_MapFile << WXEXTHELP_SEPARATOR << relativeURL;
205 return wxExecute(command) != 0;
206 }
207
208 bool
209 wxExtHelpController::DisplayContents(void)
210 {
211 if(! m_NumOfEntries)
212 return false;
213 wxBusyCursor b; // display a busy cursor
214 return KeywordSearch("");
215 }
216
217 bool
218 wxExtHelpController::DisplaySection(int sectionNo)
219 {
220 if(! m_NumOfEntries)
221 return false;
222
223 wxBusyCursor b; // display a busy cursor
224 wxNode *node = m_MapList->First();
225 wxExtHelpMapEntry *entry;
226 while(node)
227 {
228 entry = (wxExtHelpMapEntry *)node->Data();
229 if(entry->id == sectionNo)
230 return CallBrowser(entry->url);
231 node = node->Next();
232 }
233 return false;
234 }
235
236 bool
237 wxExtHelpController::DisplayBlock(long blockNo)
238 {
239 return DisplaySection((int)blockNo);
240 }
241
242 bool
243 wxExtHelpController::KeywordSearch(const wxString& k)
244 {
245 if(! m_NumOfEntries)
246 return false;
247
248 wxBusyCursor b; // display a busy cursor
249 wxString *choices = new wxString[m_NumOfEntries];
250 wxString *urls = new wxString[m_NumOfEntries];
251 wxString compA, compB;
252
253 int idx = 0, j;
254 bool rc;
255 bool showAll = k.IsEmpty();
256 wxNode *node = m_MapList->First();
257 wxExtHelpMapEntry *entry;
258
259 compA = k; compA.LowerCase(); // we compare case insensitive
260 while(node)
261 {
262 entry = (wxExtHelpMapEntry *)node->Data();
263 compB = entry->doc; compB.LowerCase();
264 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
265 {
266 urls[idx] = entry->url;
267 // doesn't work:
268 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
269 //if(choices[idx].IsEmpty()) // didn't contain the ';'
270 // choices[idx] = (**i).doc;
271 choices[idx] = "";
272 for(j=0;entry->doc.c_str()[j]
273 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
274 choices[idx] << entry->doc.c_str()[j];
275 idx++;
276 }
277 node = node->Next();
278 }
279
280 if(idx == 1)
281 rc = CallBrowser(urls[0]);
282 else if(idx == 0)
283 {
284 wxMessageBox(_("No entries found."));
285 rc = false;
286 }
287 else
288 {
289 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
290 showAll ? _("Help Index") : _("Entries found"),
291 idx,choices);
292 if(idx != -1)
293 rc = CallBrowser(urls[idx]);
294 else
295 rc = false;
296 }
297 delete[] urls;
298 delete[] choices;
299
300 return rc;
301 }
302
303
304 bool
305 wxExtHelpController::Quit(void)
306 {
307 return true;
308 }
309
310 void
311 wxExtHelpController::OnQuit(void)
312 {
313 }
314