]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/helphtml.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: base class for html help systems
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 # pragma implementation "helphtml.h"
16 #include "wx/wxprec.h"
26 #include "wx/string.h"
30 #include "wx/msgdlg.h"
31 #include "wx/choicdlg.h"
34 #include "wx/helpbase.h"
35 #include "wx/generic/helpext.h"
43 #if !defined(__WINDOWS__) && !defined(__OS2__)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 /// Name for map file.
52 #define WXEXTHELP_MAPFILE _T("wxhelp.map")
53 /// Maximum line length in map file.
54 #define WXEXTHELP_BUFLEN 512
55 /// Character introducing comments/documentation field in map file.
56 #define WXEXTHELP_COMMENTCHAR ';'
60 class wxExtHelpMapEntry
: public wxObject
66 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
67 { id
= iid
; url
= iurl
; doc
= idoc
; }
70 IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase
, wxHelpControllerBase
)
73 This class implements help via an external browser.
74 It requires the name of a directory containing the documentation
75 and a file mapping numerical Section numbers to relative URLS.
78 wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
80 m_MapList
= (wxList
*) NULL
;
85 wxHTMLHelpControllerBase::DeleteList()
89 wxNode
*node
= m_MapList
->First();
92 delete (wxExtHelpMapEntry
*)node
->Data();
94 node
= m_MapList
->First();
97 m_MapList
= (wxList
*) NULL
;
101 wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase()
106 /** This must be called to tell the controller where to find the
108 @param file - NOT a filename, but a directory name.
109 @return true on success
112 wxHTMLHelpControllerBase::Initialize(const wxString
& file
)
114 return LoadFile(file
);
118 // ifile is the name of the base help directory
120 wxHTMLHelpControllerBase::LoadFile(const wxString
& ifile
)
122 wxString mapFile
, file
, url
, doc
;
124 char buffer
[WXEXTHELP_BUFLEN
];
126 wxBusyCursor b
; // display a busy cursor
128 if(! ifile
.IsEmpty())
131 if(! wxIsAbsolutePath(file
))
133 wxChar
* f
= wxGetWorkingDirectory();
135 delete[] f
; // wxGetWorkingDirectory returns new memory
136 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
142 // If a locale is set, look in file/localename, i.e.
143 // If passed "/usr/local/myapp/help" and the current wxLocale is
144 // set to be "de", then look in "/usr/local/myapp/help/de/"
145 // first and fall back to "/usr/local/myapp/help" if that
147 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
150 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
151 if(wxDirExists(newfile
))
155 newfile
= WXEXTHELP_SEPARATOR
;
156 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
157 while(*cptr
&& *cptr
!= wxT('_'))
158 newfile
<< *(cptr
++);
159 if(wxDirExists(newfile
))
165 if(! wxDirExists(file
))
168 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
170 else // try to reload old file
173 if(! wxFileExists(mapFile
))
177 m_MapList
= new wxList
;
180 FILE *input
= wxFopen(mapFile
,wxT("rt"));
185 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
187 len
= strlen(buffer
);
188 if(buffer
[len
-1] == '\n')
189 buffer
[len
-1] = '\0'; // cut of trailing newline
190 if(sscanf(buffer
,"%d", &id
) != 1)
192 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
193 ; // find begin of URL
195 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
196 WXEXTHELP_COMMENTCHAR
)
198 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
202 doc
= (buffer
+ i
+ 1); // skip the comment character
203 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
206 }while(! feof(input
));
209 m_MapFile
= file
; // now it's valid
215 wxHTMLHelpControllerBase::DisplayContents()
221 wxNode
*node
= m_MapList
->First();
222 wxExtHelpMapEntry
*entry
;
225 entry
= (wxExtHelpMapEntry
*)node
->Data();
226 if(entry
->id
== CONTENTS_ID
)
228 contents
= entry
->url
;
236 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
237 if(file
.Contains(wxT('#')))
238 file
= file
.BeforeLast(wxT('#'));
239 if(contents
.Length() && wxFileExists(file
))
240 rc
= DisplaySection(CONTENTS_ID
);
242 // if not found, open homemade toc:
243 return rc
? TRUE
: KeywordSearch(wxT(""));
247 wxHTMLHelpControllerBase::DisplaySection(int sectionNo
)
252 wxBusyCursor b
; // display a busy cursor
253 wxNode
*node
= m_MapList
->First();
254 wxExtHelpMapEntry
*entry
;
257 entry
= (wxExtHelpMapEntry
*)node
->Data();
258 if(entry
->id
== sectionNo
)
259 return DisplayHelp(entry
->url
);
265 bool wxHTMLHelpControllerBase::DisplaySection(const wxString
& section
)
267 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
270 return DisplayHelp(section
);
272 return KeywordSearch(section
);
276 wxHTMLHelpControllerBase::DisplayBlock(long blockNo
)
278 return DisplaySection((int)blockNo
);
282 wxHTMLHelpControllerBase::KeywordSearch(const wxString
& k
)
287 wxString
*choices
= new wxString
[m_NumOfEntries
];
288 wxString
*urls
= new wxString
[m_NumOfEntries
];
289 wxString compA
, compB
;
293 bool showAll
= k
.IsEmpty();
294 wxNode
*node
= m_MapList
->First();
295 wxExtHelpMapEntry
*entry
;
298 wxBusyCursor b
; // display a busy cursor
299 compA
= k
; compA
.LowerCase(); // we compare case insensitive
302 entry
= (wxExtHelpMapEntry
*)node
->Data();
303 compB
= entry
->doc
; compB
.LowerCase();
304 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
306 urls
[idx
] = entry
->url
;
308 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
309 //if(choices[idx].IsEmpty()) // didn't contain the ';'
310 // choices[idx] = (**i).doc;
312 for(j
=0;entry
->doc
.c_str()[j
]
313 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
314 choices
[idx
] << entry
->doc
.c_str()[j
];
322 rc
= DisplayHelp(urls
[0]);
325 wxMessageBox(_("No entries found."));
330 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
331 showAll
? _("Help Index") : _("Entries found"),
334 rc
= DisplayHelp(urls
[idx
]);
346 wxHTMLHelpControllerBase::Quit()
352 wxHTMLHelpControllerBase::OnQuit()