]> git.saurik.com Git - wxWidgets.git/blob - src/generic/helphtml.cpp
Added OS/2 patches to wxGTK.
[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 a locale is set, look in file/localename, i.e.
108 // If passed "/usr/local/myapp/help" and the current wxLocale is
109 // set to be "de", then look in "/usr/local/myapp/help/de/"
110 // first and fall back to "/usr/local/myapp/help" if that
111 // doesn't exist.
112 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
113 {
114 wxString newfile;
115 newfile << WXEXTHELP_SEPARATOR << wxGetLocale()->GetName();
116 if(wxDirExists(newfile))
117 file = newfile;
118 else
119 {
120 newfile = WXEXTHELP_SEPARATOR;
121 const wxChar *cptr = wxGetLocale()->GetName().c_str();
122 while(*cptr && *cptr != _T('_'))
123 newfile << *(cptr++);
124 if(wxDirExists(newfile))
125 file = newfile;
126 }
127 }
128
129 if(! wxDirExists(file))
130 return FALSE;
131
132 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
133 }
134 else // try to reload old file
135 mapFile = m_MapFile;
136
137 if(! wxFileExists(mapFile))
138 return FALSE;
139
140 DeleteList();
141 m_MapList = new wxList;
142 m_NumOfEntries = 0;
143
144 FILE *input = fopen(mapFile.fn_str(),"rt");
145 if(! input)
146 return FALSE;
147 do
148 {
149 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
150 {
151 len = strlen(buffer);
152 if(buffer[len-1] == '\n')
153 buffer[len-1] = '\0'; // cut of trailing newline
154 if(sscanf(buffer,"%d", &id) != 1)
155 break; // error
156 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
157 ; // find begin of URL
158 url = "";
159 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
160 WXEXTHELP_COMMENTCHAR)
161 url << buffer[i++];
162 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
163 i++;
164 doc = "";
165 if(buffer[i])
166 doc = (buffer + i + 1); // skip the comment character
167 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
168 m_NumOfEntries++;
169 }
170 }while(! feof(input));
171 fclose(input);
172
173 m_MapFile = file; // now it's valid
174 return TRUE;
175 }
176
177
178 bool
179 wxHTMLHelpControllerBase::DisplayContents()
180 {
181 if(! m_NumOfEntries)
182 return FALSE;
183 wxBusyCursor b; // display a busy cursor
184 return KeywordSearch("");
185 }
186
187 bool
188 wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
189 {
190 if(! m_NumOfEntries)
191 return FALSE;
192
193 wxBusyCursor b; // display a busy cursor
194 wxNode *node = m_MapList->First();
195 wxExtHelpMapEntry *entry;
196 while(node)
197 {
198 entry = (wxExtHelpMapEntry *)node->Data();
199 if(entry->id == sectionNo)
200 return DisplayHelp(entry->url);
201 node = node->Next();
202 }
203 return FALSE;
204 }
205
206 bool
207 wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
208 {
209 return DisplaySection((int)blockNo);
210 }
211
212 bool
213 wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
214 {
215 if(! m_NumOfEntries)
216 return FALSE;
217
218 wxBusyCursor b; // display a busy cursor
219 wxString *choices = new wxString[m_NumOfEntries];
220 wxString *urls = new wxString[m_NumOfEntries];
221 wxString compA, compB;
222
223 int idx = 0, j;
224 bool rc;
225 bool showAll = k.IsEmpty();
226 wxNode *node = m_MapList->First();
227 wxExtHelpMapEntry *entry;
228
229 compA = k; compA.LowerCase(); // we compare case insensitive
230 while(node)
231 {
232 entry = (wxExtHelpMapEntry *)node->Data();
233 compB = entry->doc; compB.LowerCase();
234 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
235 {
236 urls[idx] = entry->url;
237 // doesn't work:
238 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
239 //if(choices[idx].IsEmpty()) // didn't contain the ';'
240 // choices[idx] = (**i).doc;
241 choices[idx] = "";
242 for(j=0;entry->doc.c_str()[j]
243 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
244 choices[idx] << entry->doc.c_str()[j];
245 idx++;
246 }
247 node = node->Next();
248 }
249
250 if(idx == 1)
251 rc = DisplayHelp(urls[0]);
252 else if(idx == 0)
253 {
254 wxMessageBox(_("No entries found."));
255 rc = FALSE;
256 }
257 else
258 {
259 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
260 showAll ? _("Help Index") : _("Entries found"),
261 idx,choices);
262 if(idx != -1)
263 rc = DisplayHelp(urls[idx]);
264 else
265 rc = FALSE;
266 }
267 delete[] urls;
268 delete[] choices;
269
270 return rc;
271 }
272
273
274 bool
275 wxHTMLHelpControllerBase::Quit()
276 {
277 return TRUE;
278 }
279
280 void
281 wxHTMLHelpControllerBase::OnQuit()
282 {
283 }
284