]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helphtml.cpp
fixed SelectItem call with an invalid item
[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
f96b60aa
VZ
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
afcaf277 29
f96b60aa
VZ
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
afcaf277
KB
40
41class wxExtHelpMapEntry : public wxObject
42{
43public:
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
afcaf277 51IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase, wxHelpControllerBase)
85833f5c 52
afcaf277
KB
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
85833f5c 59wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
afcaf277
KB
60{
61 m_MapList = (wxList*) NULL;
62 m_NumOfEntries = 0;
63}
64
65void
85833f5c 66wxHTMLHelpControllerBase::DeleteList()
afcaf277
KB
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
85833f5c 82wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase()
afcaf277
KB
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*/
92bool
93wxHTMLHelpControllerBase::Initialize(const wxString& file)
94{
95 return LoadFile(file);
96}
97
98
99// ifile is the name of the base help directory
100bool
101wxHTMLHelpControllerBase::LoadFile(const wxString& ifile)
102{
103 wxString mapFile, file, url, doc;
104 int id,i,len;
105 char buffer[WXEXTHELP_BUFLEN];
85833f5c 106
afcaf277
KB
107 wxBusyCursor b; // display a busy cursor
108
109 if(! ifile.IsEmpty())
110 {
111 file = ifile;
112 if(! wxIsAbsolutePath(file))
113 {
87138c52 114 wxChar* f = wxGetWorkingDirectory();
afcaf277
KB
115 file = f;
116 delete[] f; // wxGetWorkingDirectory returns new memory
117 file << WXEXTHELP_SEPARATOR << ifile;
118 }
119 else
120 file = ifile;
121
ce4169a4 122#if wxUSE_INTL
afcaf277
KB
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;
cfad3750
KB
134 else
135 {
136 newfile = WXEXTHELP_SEPARATOR;
87138c52
OK
137 const wxChar *cptr = wxGetLocale()->GetName().c_str();
138 while(*cptr && *cptr != _T('_'))
cfad3750
KB
139 newfile << *(cptr++);
140 if(wxDirExists(newfile))
141 file = newfile;
142 }
afcaf277 143 }
ce4169a4 144#endif
85833f5c 145
afcaf277
KB
146 if(! wxDirExists(file))
147 return FALSE;
85833f5c 148
afcaf277
KB
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;
85833f5c 160
87138c52 161 FILE *input = fopen(mapFile.fn_str(),"rt");
afcaf277
KB
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);
85833f5c 189
afcaf277
KB
190 m_MapFile = file; // now it's valid
191 return TRUE;
192}
193
194
195bool
85833f5c 196wxHTMLHelpControllerBase::DisplayContents()
afcaf277
KB
197{
198 if(! m_NumOfEntries)
199 return FALSE;
200 wxBusyCursor b; // display a busy cursor
201 return KeywordSearch("");
202}
85833f5c 203
afcaf277
KB
204bool
205wxHTMLHelpControllerBase::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
223bool
224wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
225{
226 return DisplaySection((int)blockNo);
227}
228
229bool
230wxHTMLHelpControllerBase::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;
85833f5c 239
afcaf277
KB
240 int idx = 0, j;
241 bool rc;
242 bool showAll = k.IsEmpty();
243 wxNode *node = m_MapList->First();
244 wxExtHelpMapEntry *entry;
85833f5c 245
afcaf277
KB
246 compA = k; compA.LowerCase(); // we compare case insensitive
247 while(node)
248 {
249 entry = (wxExtHelpMapEntry *)node->Data();
250 compB = entry->doc; compB.LowerCase();
85833f5c 251 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
afcaf277
KB
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;
85833f5c 286
afcaf277
KB
287 return rc;
288}
289
290
291bool
85833f5c 292wxHTMLHelpControllerBase::Quit()
afcaf277
KB
293{
294 return TRUE;
295}
296
297void
85833f5c 298wxHTMLHelpControllerBase::OnQuit()
afcaf277
KB
299{
300}
301