Suppressed a spurious error message, now contents are looked for in entry
[wxWidgets.git] / src / generic / helphtml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helphtml.cpp
3 // Purpose: base class for html help systems
4 // Author: Karsten Ballueder
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation "helphtml.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/setup.h"
24 #include "wx/string.h"
25 #include "wx/utils.h"
26 #include "wx/list.h"
27 #include "wx/intl.h"
28 #endif
29
30 #include "wx/helpbase.h"
31 #include "wx/generic/helpext.h"
32
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <sys/stat.h>
36
37 #ifndef __WINDOWS__
38 #include <unistd.h>
39 #endif
40
41 #define CONTENTS_ID 0
42
43 class wxExtHelpMapEntry : public wxObject
44 {
45 public:
46 int id;
47 wxString url;
48 wxString doc;
49 wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc)
50 { id = iid; url = iurl; doc = idoc; }
51 };
52
53 IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase, wxHelpControllerBase)
54
55 /**
56 This class implements help via an external browser.
57 It requires the name of a directory containing the documentation
58 and a file mapping numerical Section numbers to relative URLS.
59 */
60
61 wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
62 {
63 m_MapList = (wxList*) NULL;
64 m_NumOfEntries = 0;
65 }
66
67 void
68 wxHTMLHelpControllerBase::DeleteList()
69 {
70 if(m_MapList)
71 {
72 wxNode *node = m_MapList->First();
73 while (node)
74 {
75 delete (wxExtHelpMapEntry *)node->Data();
76 delete node;
77 node = m_MapList->First();
78 }
79 delete m_MapList;
80 m_MapList = (wxList*) NULL;
81 }
82 }
83
84 wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase()
85 {
86 DeleteList();
87 }
88
89 /** This must be called to tell the controller where to find the
90 documentation.
91 @param file - NOT a filename, but a directory name.
92 @return true on success
93 */
94 bool
95 wxHTMLHelpControllerBase::Initialize(const wxString& file)
96 {
97 return LoadFile(file);
98 }
99
100
101 // ifile is the name of the base help directory
102 bool
103 wxHTMLHelpControllerBase::LoadFile(const wxString& ifile)
104 {
105 wxString mapFile, file, url, doc;
106 int id,i,len;
107 char buffer[WXEXTHELP_BUFLEN];
108
109 wxBusyCursor b; // display a busy cursor
110
111 if(! ifile.IsEmpty())
112 {
113 file = ifile;
114 if(! wxIsAbsolutePath(file))
115 {
116 wxChar* f = wxGetWorkingDirectory();
117 file = f;
118 delete[] f; // wxGetWorkingDirectory returns new memory
119 file << WXEXTHELP_SEPARATOR << ifile;
120 }
121 else
122 file = ifile;
123
124 #if wxUSE_INTL
125 // If a locale is set, look in file/localename, i.e.
126 // If passed "/usr/local/myapp/help" and the current wxLocale is
127 // set to be "de", then look in "/usr/local/myapp/help/de/"
128 // first and fall back to "/usr/local/myapp/help" if that
129 // doesn't exist.
130 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
131 {
132 wxString newfile;
133 newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName();
134 if(wxDirExists(newfile))
135 file = newfile;
136 else
137 {
138 newfile = WXEXTHELP_SEPARATOR;
139 const wxChar *cptr = wxGetLocale()->GetName().c_str();
140 while(*cptr && *cptr != _T('_'))
141 newfile << *(cptr++);
142 if(wxDirExists(newfile))
143 file = newfile;
144 }
145 }
146 #endif
147
148 if(! wxDirExists(file))
149 return FALSE;
150
151 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
152 }
153 else // try to reload old file
154 mapFile = m_MapFile;
155
156 if(! wxFileExists(mapFile))
157 return FALSE;
158
159 DeleteList();
160 m_MapList = new wxList;
161 m_NumOfEntries = 0;
162
163 FILE *input = fopen(mapFile.fn_str(),"rt");
164 if(! input)
165 return FALSE;
166 do
167 {
168 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
169 {
170 len = strlen(buffer);
171 if(buffer[len-1] == '\n')
172 buffer[len-1] = '\0'; // cut of trailing newline
173 if(sscanf(buffer,"%d", &id) != 1)
174 break; // error
175 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
176 ; // find begin of URL
177 url = "";
178 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
179 WXEXTHELP_COMMENTCHAR)
180 url << buffer[i++];
181 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
182 i++;
183 doc = "";
184 if(buffer[i])
185 doc = (buffer + i + 1); // skip the comment character
186 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
187 m_NumOfEntries++;
188 }
189 }while(! feof(input));
190 fclose(input);
191
192 m_MapFile = file; // now it's valid
193 return TRUE;
194 }
195
196
197 bool
198 wxHTMLHelpControllerBase::DisplayContents()
199 {
200 if(! m_NumOfEntries)
201 return FALSE;
202
203 wxString contents;
204 wxNode *node = m_MapList->First();
205 wxExtHelpMapEntry *entry;
206 while(node)
207 {
208 entry = (wxExtHelpMapEntry *)node->Data();
209 if(entry->id == CONTENTS_ID)
210 {
211 contents = entry->url;
212 break;
213 }
214 node = node->Next();
215 }
216
217 bool rc = FALSE;
218 if(contents.Length() && wxFileExists(contents.BeforeLast('#')))
219 rc = DisplaySection(CONTENTS_ID);
220
221 // if not found, open homemade toc:
222 return rc ? TRUE : KeywordSearch("");
223 }
224
225 bool
226 wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
227 {
228 if(! m_NumOfEntries)
229 return FALSE;
230
231 wxBusyCursor b; // display a busy cursor
232 wxNode *node = m_MapList->First();
233 wxExtHelpMapEntry *entry;
234 while(node)
235 {
236 entry = (wxExtHelpMapEntry *)node->Data();
237 if(entry->id == sectionNo)
238 return DisplayHelp(entry->url);
239 node = node->Next();
240 }
241 return FALSE;
242 }
243
244 bool
245 wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
246 {
247 return DisplaySection((int)blockNo);
248 }
249
250 bool
251 wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
252 {
253 if(! m_NumOfEntries)
254 return FALSE;
255
256 wxBusyCursor b; // display a busy cursor
257 wxString *choices = new wxString[m_NumOfEntries];
258 wxString *urls = new wxString[m_NumOfEntries];
259 wxString compA, compB;
260
261 int idx = 0, j;
262 bool rc;
263 bool showAll = k.IsEmpty();
264 wxNode *node = m_MapList->First();
265 wxExtHelpMapEntry *entry;
266
267 compA = k; compA.LowerCase(); // we compare case insensitive
268 while(node)
269 {
270 entry = (wxExtHelpMapEntry *)node->Data();
271 compB = entry->doc; compB.LowerCase();
272 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
273 {
274 urls[idx] = entry->url;
275 // doesn't work:
276 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
277 //if(choices[idx].IsEmpty()) // didn't contain the ';'
278 // choices[idx] = (**i).doc;
279 choices[idx] = "";
280 for(j=0;entry->doc.c_str()[j]
281 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
282 choices[idx] << entry->doc.c_str()[j];
283 idx++;
284 }
285 node = node->Next();
286 }
287
288 if(idx == 1)
289 rc = DisplayHelp(urls[0]);
290 else if(idx == 0)
291 {
292 wxMessageBox(_("No entries found."));
293 rc = FALSE;
294 }
295 else
296 {
297 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
298 showAll ? _("Help Index") : _("Entries found"),
299 idx,choices);
300 if(idx != -1)
301 rc = DisplayHelp(urls[idx]);
302 else
303 rc = FALSE;
304 }
305 delete[] urls;
306 delete[] choices;
307
308 return rc;
309 }
310
311
312 bool
313 wxHTMLHelpControllerBase::Quit()
314 {
315 return TRUE;
316 }
317
318 void
319 wxHTMLHelpControllerBase::OnQuit()
320 {
321 }
322