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