wxHTMLHelpController compiles (and somewhat works) under Windows, added a
[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 wxBusyCursor b; // display a busy cursor
201 return KeywordSearch("");
202 }
203
204 bool
205 wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
206 {
207 if(! m_NumOfEntries)
208 return FALSE;
209
210 wxBusyCursor b; // display a busy cursor
211 wxNode *node = m_MapList->First();
212 wxExtHelpMapEntry *entry;
213 while(node)
214 {
215 entry = (wxExtHelpMapEntry *)node->Data();
216 if(entry->id == sectionNo)
217 return DisplayHelp(entry->url);
218 node = node->Next();
219 }
220 return FALSE;
221 }
222
223 bool
224 wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
225 {
226 return DisplaySection((int)blockNo);
227 }
228
229 bool
230 wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
231 {
232 if(! m_NumOfEntries)
233 return FALSE;
234
235 wxBusyCursor b; // display a busy cursor
236 wxString *choices = new wxString[m_NumOfEntries];
237 wxString *urls = new wxString[m_NumOfEntries];
238 wxString compA, compB;
239
240 int idx = 0, j;
241 bool rc;
242 bool showAll = k.IsEmpty();
243 wxNode *node = m_MapList->First();
244 wxExtHelpMapEntry *entry;
245
246 compA = k; compA.LowerCase(); // we compare case insensitive
247 while(node)
248 {
249 entry = (wxExtHelpMapEntry *)node->Data();
250 compB = entry->doc; compB.LowerCase();
251 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
252 {
253 urls[idx] = entry->url;
254 // doesn't work:
255 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
256 //if(choices[idx].IsEmpty()) // didn't contain the ';'
257 // choices[idx] = (**i).doc;
258 choices[idx] = "";
259 for(j=0;entry->doc.c_str()[j]
260 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
261 choices[idx] << entry->doc.c_str()[j];
262 idx++;
263 }
264 node = node->Next();
265 }
266
267 if(idx == 1)
268 rc = DisplayHelp(urls[0]);
269 else if(idx == 0)
270 {
271 wxMessageBox(_("No entries found."));
272 rc = FALSE;
273 }
274 else
275 {
276 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
277 showAll ? _("Help Index") : _("Entries found"),
278 idx,choices);
279 if(idx != -1)
280 rc = DisplayHelp(urls[idx]);
281 else
282 rc = FALSE;
283 }
284 delete[] urls;
285 delete[] choices;
286
287 return rc;
288 }
289
290
291 bool
292 wxHTMLHelpControllerBase::Quit()
293 {
294 return TRUE;
295 }
296
297 void
298 wxHTMLHelpControllerBase::OnQuit()
299 {
300 }
301