Split part of wxExtHelpController into wxHTMLHelpControllerBase to share
[wxWidgets.git] / src / generic / helphtml.cpp
1 /*-*- c++ -*-********************************************************
2 * helphtml.cpp - base class for html help systems *
3 * *
4 * (C) 1999 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8 #ifdef __GNUG__
9 # pragma implementation "helphtml.h"
10 #endif
11
12 #include "wx/setup.h"
13 #include "wx/helpbase.h"
14 #include "wx/generic/helpext.h"
15 #include "wx/string.h"
16 #include "wx/utils.h"
17 #include "wx/list.h"
18 #include "wx/intl.h"
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24
25
26 class wxExtHelpMapEntry : public wxObject
27 {
28 public:
29 int id;
30 wxString url;
31 wxString doc;
32 wxExtHelpMapEntry(int iid, wxString const &iurl, wxString const &idoc)
33 { id = iid; url = iurl; doc = idoc; }
34 };
35
36
37 struct wxBusyCursor
38 {
39 wxBusyCursor() { wxBeginBusyCursor(); }
40 ~wxBusyCursor() { wxEndBusyCursor(); }
41 };
42
43 IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase, wxHelpControllerBase)
44
45 /**
46 This class implements help via an external browser.
47 It requires the name of a directory containing the documentation
48 and a file mapping numerical Section numbers to relative URLS.
49 */
50
51 wxHTMLHelpControllerBase::wxHTMLHelpControllerBase(void)
52 {
53 m_MapList = (wxList*) NULL;
54 m_NumOfEntries = 0;
55 }
56
57 void
58 wxHTMLHelpControllerBase::DeleteList(void)
59 {
60 if(m_MapList)
61 {
62 wxNode *node = m_MapList->First();
63 while (node)
64 {
65 delete (wxExtHelpMapEntry *)node->Data();
66 delete node;
67 node = m_MapList->First();
68 }
69 delete m_MapList;
70 m_MapList = (wxList*) NULL;
71 }
72 }
73
74 wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase(void)
75 {
76 DeleteList();
77 }
78
79 /** This must be called to tell the controller where to find the
80 documentation.
81 @param file - NOT a filename, but a directory name.
82 @return true on success
83 */
84 bool
85 wxHTMLHelpControllerBase::Initialize(const wxString& file)
86 {
87 return LoadFile(file);
88 }
89
90
91 // ifile is the name of the base help directory
92 bool
93 wxHTMLHelpControllerBase::LoadFile(const wxString& ifile)
94 {
95 wxString mapFile, file, url, doc;
96 int id,i,len;
97 char buffer[WXEXTHELP_BUFLEN];
98
99 wxBusyCursor b; // display a busy cursor
100
101 if(! ifile.IsEmpty())
102 {
103 file = ifile;
104 if(! wxIsAbsolutePath(file))
105 {
106 char* f = wxGetWorkingDirectory();
107 file = f;
108 delete[] f; // wxGetWorkingDirectory returns new memory
109 file << WXEXTHELP_SEPARATOR << ifile;
110 }
111 else
112 file = ifile;
113
114 // If a locale is set, look in file/localename, i.e.
115 // If passed "/usr/local/myapp/help" and the current wxLocale is
116 // set to be "de", then look in "/usr/local/myapp/help/de/"
117 // first and fall back to "/usr/local/myapp/help" if that
118 // doesn't exist.
119 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
120 {
121 wxString newfile;
122 newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName();
123 if(wxDirExists(newfile))
124 file = newfile;
125 }
126
127 if(! wxDirExists(file))
128 return FALSE;
129
130 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
131 }
132 else // try to reload old file
133 mapFile = m_MapFile;
134
135 if(! wxFileExists(mapFile))
136 return FALSE;
137
138 DeleteList();
139 m_MapList = new wxList;
140 m_NumOfEntries = 0;
141
142 FILE *input = fopen(mapFile.c_str(),"rt");
143 if(! input)
144 return FALSE;
145 do
146 {
147 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
148 {
149 len = strlen(buffer);
150 if(buffer[len-1] == '\n')
151 buffer[len-1] = '\0'; // cut of trailing newline
152 if(sscanf(buffer,"%d", &id) != 1)
153 break; // error
154 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
155 ; // find begin of URL
156 url = "";
157 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
158 WXEXTHELP_COMMENTCHAR)
159 url << buffer[i++];
160 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
161 i++;
162 doc = "";
163 if(buffer[i])
164 doc = (buffer + i + 1); // skip the comment character
165 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
166 m_NumOfEntries++;
167 }
168 }while(! feof(input));
169 fclose(input);
170
171 m_MapFile = file; // now it's valid
172 return TRUE;
173 }
174
175
176 bool
177 wxHTMLHelpControllerBase::DisplayContents(void)
178 {
179 if(! m_NumOfEntries)
180 return FALSE;
181 wxBusyCursor b; // display a busy cursor
182 return KeywordSearch("");
183 }
184
185 bool
186 wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
187 {
188 if(! m_NumOfEntries)
189 return FALSE;
190
191 wxBusyCursor b; // display a busy cursor
192 wxNode *node = m_MapList->First();
193 wxExtHelpMapEntry *entry;
194 while(node)
195 {
196 entry = (wxExtHelpMapEntry *)node->Data();
197 if(entry->id == sectionNo)
198 return DisplayHelp(entry->url);
199 node = node->Next();
200 }
201 return FALSE;
202 }
203
204 bool
205 wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
206 {
207 return DisplaySection((int)blockNo);
208 }
209
210 bool
211 wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
212 {
213 if(! m_NumOfEntries)
214 return FALSE;
215
216 wxBusyCursor b; // display a busy cursor
217 wxString *choices = new wxString[m_NumOfEntries];
218 wxString *urls = new wxString[m_NumOfEntries];
219 wxString compA, compB;
220
221 int idx = 0, j;
222 bool rc;
223 bool showAll = k.IsEmpty();
224 wxNode *node = m_MapList->First();
225 wxExtHelpMapEntry *entry;
226
227 compA = k; compA.LowerCase(); // we compare case insensitive
228 while(node)
229 {
230 entry = (wxExtHelpMapEntry *)node->Data();
231 compB = entry->doc; compB.LowerCase();
232 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
233 {
234 urls[idx] = entry->url;
235 // doesn't work:
236 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
237 //if(choices[idx].IsEmpty()) // didn't contain the ';'
238 // choices[idx] = (**i).doc;
239 choices[idx] = "";
240 for(j=0;entry->doc.c_str()[j]
241 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
242 choices[idx] << entry->doc.c_str()[j];
243 idx++;
244 }
245 node = node->Next();
246 }
247
248 if(idx == 1)
249 rc = DisplayHelp(urls[0]);
250 else if(idx == 0)
251 {
252 wxMessageBox(_("No entries found."));
253 rc = FALSE;
254 }
255 else
256 {
257 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
258 showAll ? _("Help Index") : _("Entries found"),
259 idx,choices);
260 if(idx != -1)
261 rc = DisplayHelp(urls[idx]);
262 else
263 rc = FALSE;
264 }
265 delete[] urls;
266 delete[] choices;
267
268 return rc;
269 }
270
271
272 bool
273 wxHTMLHelpControllerBase::Quit(void)
274 {
275 return TRUE;
276 }
277
278 void
279 wxHTMLHelpControllerBase::OnQuit(void)
280 {
281 }
282