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