]>
Commit | Line | Data |
---|---|---|
f96b60aa VZ |
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 | ||
afcaf277 KB |
12 | #ifdef __GNUG__ |
13 | # pragma implementation "helphtml.h" | |
14 | #endif | |
15 | ||
f96b60aa VZ |
16 | #include "wx/wxprec.h" |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
afcaf277 | 21 | |
31528cd3 VZ |
22 | #if wxUSE_HELP |
23 | ||
f96b60aa VZ |
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 | |
afcaf277 | 31 | |
f96b60aa VZ |
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 | |
afcaf277 | 42 | |
8d184505 KB |
43 | #define CONTENTS_ID 0 |
44 | ||
afcaf277 KB |
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 | ||
afcaf277 | 55 | IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase, wxHelpControllerBase) |
85833f5c | 56 | |
afcaf277 KB |
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 | ||
85833f5c | 63 | wxHTMLHelpControllerBase::wxHTMLHelpControllerBase() |
afcaf277 KB |
64 | { |
65 | m_MapList = (wxList*) NULL; | |
66 | m_NumOfEntries = 0; | |
67 | } | |
68 | ||
69 | void | |
85833f5c | 70 | wxHTMLHelpControllerBase::DeleteList() |
afcaf277 KB |
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 | ||
85833f5c | 86 | wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase() |
afcaf277 KB |
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]; | |
85833f5c | 110 | |
afcaf277 KB |
111 | wxBusyCursor b; // display a busy cursor |
112 | ||
113 | if(! ifile.IsEmpty()) | |
114 | { | |
115 | file = ifile; | |
116 | if(! wxIsAbsolutePath(file)) | |
117 | { | |
87138c52 | 118 | wxChar* f = wxGetWorkingDirectory(); |
afcaf277 KB |
119 | file = f; |
120 | delete[] f; // wxGetWorkingDirectory returns new memory | |
121 | file << WXEXTHELP_SEPARATOR << ifile; | |
122 | } | |
123 | else | |
124 | file = ifile; | |
125 | ||
ce4169a4 | 126 | #if wxUSE_INTL |
afcaf277 KB |
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; | |
cfad3750 KB |
138 | else |
139 | { | |
140 | newfile = WXEXTHELP_SEPARATOR; | |
87138c52 OK |
141 | const wxChar *cptr = wxGetLocale()->GetName().c_str(); |
142 | while(*cptr && *cptr != _T('_')) | |
cfad3750 KB |
143 | newfile << *(cptr++); |
144 | if(wxDirExists(newfile)) | |
145 | file = newfile; | |
146 | } | |
afcaf277 | 147 | } |
ce4169a4 | 148 | #endif |
85833f5c | 149 | |
afcaf277 KB |
150 | if(! wxDirExists(file)) |
151 | return FALSE; | |
85833f5c | 152 | |
afcaf277 KB |
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; | |
85833f5c | 164 | |
87138c52 | 165 | FILE *input = fopen(mapFile.fn_str(),"rt"); |
afcaf277 KB |
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); | |
85833f5c | 193 | |
afcaf277 KB |
194 | m_MapFile = file; // now it's valid |
195 | return TRUE; | |
196 | } | |
197 | ||
198 | ||
199 | bool | |
85833f5c | 200 | wxHTMLHelpControllerBase::DisplayContents() |
afcaf277 KB |
201 | { |
202 | if(! m_NumOfEntries) | |
203 | return FALSE; | |
8dd71e2b | 204 | |
8d184505 KB |
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; | |
3bcf00ab KB |
220 | wxString file; |
221 | file << m_MapFile << WXEXTHELP_SEPARATOR << contents; | |
3a5815b5 OK |
222 | if(file.Contains(_T('#'))) |
223 | file = file.BeforeLast(_T('#')); | |
3bcf00ab | 224 | if(contents.Length() && wxFileExists(file)) |
8d184505 KB |
225 | rc = DisplaySection(CONTENTS_ID); |
226 | ||
227 | // if not found, open homemade toc: | |
3a5815b5 | 228 | return rc ? TRUE : KeywordSearch(_T("")); |
afcaf277 | 229 | } |
85833f5c | 230 | |
afcaf277 KB |
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; | |
85833f5c | 266 | |
afcaf277 KB |
267 | int idx = 0, j; |
268 | bool rc; | |
269 | bool showAll = k.IsEmpty(); | |
270 | wxNode *node = m_MapList->First(); | |
271 | wxExtHelpMapEntry *entry; | |
85833f5c | 272 | |
afcaf277 KB |
273 | compA = k; compA.LowerCase(); // we compare case insensitive |
274 | while(node) | |
275 | { | |
276 | entry = (wxExtHelpMapEntry *)node->Data(); | |
277 | compB = entry->doc; compB.LowerCase(); | |
85833f5c | 278 | if((showAll || compB.Contains(k)) && ! compB.IsEmpty()) |
afcaf277 KB |
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; | |
85833f5c | 313 | |
afcaf277 KB |
314 | return rc; |
315 | } | |
316 | ||
317 | ||
318 | bool | |
85833f5c | 319 | wxHTMLHelpControllerBase::Quit() |
afcaf277 KB |
320 | { |
321 | return TRUE; | |
322 | } | |
323 | ||
324 | void | |
85833f5c | 325 | wxHTMLHelpControllerBase::OnQuit() |
afcaf277 KB |
326 | { |
327 | } | |
328 | ||
31528cd3 | 329 | #endif // wxUSE_HELP |