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