]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helphtml.cpp
Updated wxDataStream (added ByteOrder)
[wxWidgets.git] / src / generic / helphtml.cpp
CommitLineData
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
45class wxExtHelpMapEntry : public wxObject
46{
47public:
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 55IMPLEMENT_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 63wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
afcaf277
KB
64{
65 m_MapList = (wxList*) NULL;
66 m_NumOfEntries = 0;
67}
68
69void
85833f5c 70wxHTMLHelpControllerBase::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 86wxHTMLHelpControllerBase::~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*/
96bool
97wxHTMLHelpControllerBase::Initialize(const wxString& file)
98{
99 return LoadFile(file);
100}
101
102
103// ifile is the name of the base help directory
104bool
105wxHTMLHelpControllerBase::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
199bool
85833f5c 200wxHTMLHelpControllerBase::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;
220 if(contents.Length() && wxFileExists(contents.BeforeLast('#')))
221 rc = DisplaySection(CONTENTS_ID);
222
223 // if not found, open homemade toc:
224 return rc ? TRUE : KeywordSearch("");
afcaf277 225}
85833f5c 226
afcaf277
KB
227bool
228wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
229{
230 if(! m_NumOfEntries)
231 return FALSE;
232
233 wxBusyCursor b; // display a busy cursor
234 wxNode *node = m_MapList->First();
235 wxExtHelpMapEntry *entry;
236 while(node)
237 {
238 entry = (wxExtHelpMapEntry *)node->Data();
239 if(entry->id == sectionNo)
240 return DisplayHelp(entry->url);
241 node = node->Next();
242 }
243 return FALSE;
244}
245
246bool
247wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
248{
249 return DisplaySection((int)blockNo);
250}
251
252bool
253wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
254{
255 if(! m_NumOfEntries)
256 return FALSE;
257
258 wxBusyCursor b; // display a busy cursor
259 wxString *choices = new wxString[m_NumOfEntries];
260 wxString *urls = new wxString[m_NumOfEntries];
261 wxString compA, compB;
85833f5c 262
afcaf277
KB
263 int idx = 0, j;
264 bool rc;
265 bool showAll = k.IsEmpty();
266 wxNode *node = m_MapList->First();
267 wxExtHelpMapEntry *entry;
85833f5c 268
afcaf277
KB
269 compA = k; compA.LowerCase(); // we compare case insensitive
270 while(node)
271 {
272 entry = (wxExtHelpMapEntry *)node->Data();
273 compB = entry->doc; compB.LowerCase();
85833f5c 274 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
afcaf277
KB
275 {
276 urls[idx] = entry->url;
277 // doesn't work:
278 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
279 //if(choices[idx].IsEmpty()) // didn't contain the ';'
280 // choices[idx] = (**i).doc;
281 choices[idx] = "";
282 for(j=0;entry->doc.c_str()[j]
283 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
284 choices[idx] << entry->doc.c_str()[j];
285 idx++;
286 }
287 node = node->Next();
288 }
289
290 if(idx == 1)
291 rc = DisplayHelp(urls[0]);
292 else if(idx == 0)
293 {
294 wxMessageBox(_("No entries found."));
295 rc = FALSE;
296 }
297 else
298 {
299 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
300 showAll ? _("Help Index") : _("Entries found"),
301 idx,choices);
302 if(idx != -1)
303 rc = DisplayHelp(urls[idx]);
304 else
305 rc = FALSE;
306 }
307 delete[] urls;
308 delete[] choices;
85833f5c 309
afcaf277
KB
310 return rc;
311}
312
313
314bool
85833f5c 315wxHTMLHelpControllerBase::Quit()
afcaf277
KB
316{
317 return TRUE;
318}
319
320void
85833f5c 321wxHTMLHelpControllerBase::OnQuit()
afcaf277
KB
322{
323}
324
31528cd3 325#endif // wxUSE_HELP