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