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