Split up wxStream doc files; added wxTCP... files; added wxBusyCursor;
[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(void)
45 {
46 m_MapList = (wxList*) NULL;
47 m_NumOfEntries = 0;
48 }
49
50 void
51 wxHTMLHelpControllerBase::DeleteList(void)
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(void)
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 char* 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 }
119
120 if(! wxDirExists(file))
121 return FALSE;
122
123 mapFile << file << WXEXTHELP_SEPARATOR << WXEXTHELP_MAPFILE;
124 }
125 else // try to reload old file
126 mapFile = m_MapFile;
127
128 if(! wxFileExists(mapFile))
129 return FALSE;
130
131 DeleteList();
132 m_MapList = new wxList;
133 m_NumOfEntries = 0;
134
135 FILE *input = fopen(mapFile.c_str(),"rt");
136 if(! input)
137 return FALSE;
138 do
139 {
140 if(fgets(buffer,WXEXTHELP_BUFLEN,input) && *buffer != WXEXTHELP_COMMENTCHAR)
141 {
142 len = strlen(buffer);
143 if(buffer[len-1] == '\n')
144 buffer[len-1] = '\0'; // cut of trailing newline
145 if(sscanf(buffer,"%d", &id) != 1)
146 break; // error
147 for(i=0; isdigit(buffer[i])||isspace(buffer[i])||buffer[i]=='-'; i++)
148 ; // find begin of URL
149 url = "";
150 while(buffer[i] && ! isspace(buffer[i]) && buffer[i] !=
151 WXEXTHELP_COMMENTCHAR)
152 url << buffer[i++];
153 while(buffer[i] && buffer[i] != WXEXTHELP_COMMENTCHAR)
154 i++;
155 doc = "";
156 if(buffer[i])
157 doc = (buffer + i + 1); // skip the comment character
158 m_MapList->Append(new wxExtHelpMapEntry(id,url,doc));
159 m_NumOfEntries++;
160 }
161 }while(! feof(input));
162 fclose(input);
163
164 m_MapFile = file; // now it's valid
165 return TRUE;
166 }
167
168
169 bool
170 wxHTMLHelpControllerBase::DisplayContents(void)
171 {
172 if(! m_NumOfEntries)
173 return FALSE;
174 wxBusyCursor b; // display a busy cursor
175 return KeywordSearch("");
176 }
177
178 bool
179 wxHTMLHelpControllerBase::DisplaySection(int sectionNo)
180 {
181 if(! m_NumOfEntries)
182 return FALSE;
183
184 wxBusyCursor b; // display a busy cursor
185 wxNode *node = m_MapList->First();
186 wxExtHelpMapEntry *entry;
187 while(node)
188 {
189 entry = (wxExtHelpMapEntry *)node->Data();
190 if(entry->id == sectionNo)
191 return DisplayHelp(entry->url);
192 node = node->Next();
193 }
194 return FALSE;
195 }
196
197 bool
198 wxHTMLHelpControllerBase::DisplayBlock(long blockNo)
199 {
200 return DisplaySection((int)blockNo);
201 }
202
203 bool
204 wxHTMLHelpControllerBase::KeywordSearch(const wxString& k)
205 {
206 if(! m_NumOfEntries)
207 return FALSE;
208
209 wxBusyCursor b; // display a busy cursor
210 wxString *choices = new wxString[m_NumOfEntries];
211 wxString *urls = new wxString[m_NumOfEntries];
212 wxString compA, compB;
213
214 int idx = 0, j;
215 bool rc;
216 bool showAll = k.IsEmpty();
217 wxNode *node = m_MapList->First();
218 wxExtHelpMapEntry *entry;
219
220 compA = k; compA.LowerCase(); // we compare case insensitive
221 while(node)
222 {
223 entry = (wxExtHelpMapEntry *)node->Data();
224 compB = entry->doc; compB.LowerCase();
225 if((showAll || compB.Contains(k)) && ! compB.IsEmpty())
226 {
227 urls[idx] = entry->url;
228 // doesn't work:
229 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
230 //if(choices[idx].IsEmpty()) // didn't contain the ';'
231 // choices[idx] = (**i).doc;
232 choices[idx] = "";
233 for(j=0;entry->doc.c_str()[j]
234 && entry->doc.c_str()[j] != WXEXTHELP_COMMENTCHAR; j++)
235 choices[idx] << entry->doc.c_str()[j];
236 idx++;
237 }
238 node = node->Next();
239 }
240
241 if(idx == 1)
242 rc = DisplayHelp(urls[0]);
243 else if(idx == 0)
244 {
245 wxMessageBox(_("No entries found."));
246 rc = FALSE;
247 }
248 else
249 {
250 idx = wxGetSingleChoiceIndex(showAll ? _("Help Index") : _("Relevant entries:"),
251 showAll ? _("Help Index") : _("Entries found"),
252 idx,choices);
253 if(idx != -1)
254 rc = DisplayHelp(urls[idx]);
255 else
256 rc = FALSE;
257 }
258 delete[] urls;
259 delete[] choices;
260
261 return rc;
262 }
263
264
265 bool
266 wxHTMLHelpControllerBase::Quit(void)
267 {
268 return TRUE;
269 }
270
271 void
272 wxHTMLHelpControllerBase::OnQuit(void)
273 {
274 }
275