]>
Commit | Line | Data |
---|---|---|
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 | wxString file; | |
221 | file << m_MapFile << WXEXTHELP_SEPARATOR << contents; | |
222 | if(file.Contains(_T('#'))) | |
223 | file = file.BeforeLast(_T('#')); | |
224 | if(contents.Length() && wxFileExists(file)) | |
225 | rc = DisplaySection(CONTENTS_ID); | |
226 | ||
227 | // if not found, open homemade toc: | |
228 | return rc ? TRUE : KeywordSearch(_T("")); | |
229 | } | |
230 | ||
231 | bool | |
232 | wxHTMLHelpControllerBase::DisplaySection(int sectionNo) | |
233 | { | |
234 | if(! m_NumOfEntries) | |
235 | return FALSE; | |
236 | ||
237 | wxBusyCursor b; // display a busy cursor | |
238 | wxNode *node = m_MapList->First(); | |
239 | wxExtHelpMapEntry *entry; | |
240 | while(node) | |
241 | { | |
242 | entry = (wxExtHelpMapEntry *)node->Data(); | |
243 | if(entry->id == sectionNo) | |
244 | return DisplayHelp(entry->url); | |
245 | node = node->Next(); | |
246 | } | |
247 | return FALSE; | |
248 | } | |
249 | ||
250 | bool | |
251 | wxHTMLHelpControllerBase::DisplayBlock(long blockNo) | |
252 | { | |
253 | return DisplaySection((int)blockNo); | |
254 | } | |
255 | ||
256 | bool | |
257 | wxHTMLHelpControllerBase::KeywordSearch(const wxString& k) | |
258 | { | |
259 | if(! m_NumOfEntries) | |
260 | return FALSE; | |
261 | ||
262 | wxBusyCursor b; // display a busy cursor | |
263 | wxString *choices = new wxString[m_NumOfEntries]; | |
264 | wxString *urls = new wxString[m_NumOfEntries]; | |
265 | wxString compA, compB; | |
266 | ||
267 | int idx = 0, j; | |
268 | bool rc; | |
269 | bool showAll = k.IsEmpty(); | |
270 | wxNode *node = m_MapList->First(); | |
271 | wxExtHelpMapEntry *entry; | |
272 | ||
273 | compA = k; compA.LowerCase(); // we compare case insensitive | |
274 | while(node) | |
275 | { | |
276 | entry = (wxExtHelpMapEntry *)node->Data(); | |
277 | compB = entry->doc; compB.LowerCase(); | |
278 | if((showAll || compB.Contains(k)) && ! compB.IsEmpty()) | |
279 | { | |
280 | urls[idx] = entry->url; | |
281 | // doesn't work: | |
282 | // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR)); | |
283 | //if(choices[idx].IsEmpty()) // didn't contain the ';' | |
284 | // choices[idx] = (**i).doc; | |
285 | choices[idx] = ""; | |
286 | for(j=0;entry->doc.c_str()[j] | |
287 | && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++) | |
288 | choices[idx] << entry->doc.c_str()[j]; | |
289 | idx++; | |
290 | } | |
291 | node = node->Next(); | |
292 | } | |
293 | ||
294 | if(idx == 1) | |
295 | rc = DisplayHelp(urls[0]); | |
296 | else if(idx == 0) | |
297 | { | |
298 | wxMessageBox(_("No entries found.")); | |
299 | rc = FALSE; | |
300 | } | |
301 | else | |
302 | { | |
303 | idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"), | |
304 | showAll ? _("Help Index") : _("Entries found"), | |
305 | idx,choices); | |
306 | if(idx != -1) | |
307 | rc = DisplayHelp(urls[idx]); | |
308 | else | |
309 | rc = FALSE; | |
310 | } | |
311 | delete[] urls; | |
312 | delete[] choices; | |
313 | ||
314 | return rc; | |
315 | } | |
316 | ||
317 | ||
318 | bool | |
319 | wxHTMLHelpControllerBase::Quit() | |
320 | { | |
321 | return TRUE; | |
322 | } | |
323 | ||
324 | void | |
325 | wxHTMLHelpControllerBase::OnQuit() | |
326 | { | |
327 | } | |
328 | ||
329 | #endif // wxUSE_HELP |