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