]>
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
->GetFirst();
92 delete (wxExtHelpMapEntry
*)node
->GetData();
94 node
= m_MapList
->GetFirst();
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
139 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
146 // If a locale is set, look in file/localename, i.e.
147 // If passed "/usr/local/myapp/help" and the current wxLocale is
148 // set to be "de", then look in "/usr/local/myapp/help/de/"
149 // first and fall back to "/usr/local/myapp/help" if that
151 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
154 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
155 if(wxDirExists(newfile
))
159 newfile
= WXEXTHELP_SEPARATOR
;
160 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
161 while(*cptr
&& *cptr
!= wxT('_'))
162 newfile
<< *(cptr
++);
163 if(wxDirExists(newfile
))
169 if(! wxDirExists(file
))
172 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
174 else // try to reload old file
177 if(! wxFileExists(mapFile
))
181 m_MapList
= new wxList
;
184 FILE *input
= wxFopen(mapFile
,wxT("rt"));
189 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
191 len
= strlen(buffer
);
192 if(buffer
[len
-1] == '\n')
193 buffer
[len
-1] = '\0'; // cut of trailing newline
194 if(sscanf(buffer
,"%d", &id
) != 1)
196 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
197 ; // find begin of URL
199 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
200 WXEXTHELP_COMMENTCHAR
)
201 url
<< (wxChar
) buffer
[i
++];
202 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
206 doc
= wxString::FromAscii( (buffer
+ i
+ 1) ); // skip the comment character
207 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
210 }while(! feof(input
));
213 m_MapFile
= file
; // now it's valid
219 wxHTMLHelpControllerBase::DisplayContents()
225 wxNode
*node
= m_MapList
->GetFirst();
226 wxExtHelpMapEntry
*entry
;
229 entry
= (wxExtHelpMapEntry
*)node
->GetData();
230 if(entry
->id
== CONTENTS_ID
)
232 contents
= entry
->url
;
235 node
= node
->GetNext();
240 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
241 if(file
.Contains(wxT('#')))
242 file
= file
.BeforeLast(wxT('#'));
243 if(contents
.Length() && wxFileExists(file
))
244 rc
= DisplaySection(CONTENTS_ID
);
246 // if not found, open homemade toc:
247 return rc
? TRUE
: KeywordSearch(wxT(""));
251 wxHTMLHelpControllerBase::DisplaySection(int sectionNo
)
256 wxBusyCursor b
; // display a busy cursor
257 wxNode
*node
= m_MapList
->GetFirst();
258 wxExtHelpMapEntry
*entry
;
261 entry
= (wxExtHelpMapEntry
*)node
->GetData();
262 if(entry
->id
== sectionNo
)
263 return DisplayHelp(entry
->url
);
264 node
= node
->GetNext();
269 bool wxHTMLHelpControllerBase::DisplaySection(const wxString
& section
)
271 bool isFilename
= (section
.Find(wxT(".htm")) != -1);
274 return DisplayHelp(section
);
276 return KeywordSearch(section
);
280 wxHTMLHelpControllerBase::DisplayBlock(long blockNo
)
282 return DisplaySection((int)blockNo
);
286 wxHTMLHelpControllerBase::KeywordSearch(const wxString
& k
)
291 wxString
*choices
= new wxString
[m_NumOfEntries
];
292 wxString
*urls
= new wxString
[m_NumOfEntries
];
293 wxString compA
, compB
;
297 bool showAll
= k
.IsEmpty();
298 wxNode
*node
= m_MapList
->GetFirst();
299 wxExtHelpMapEntry
*entry
;
302 wxBusyCursor b
; // display a busy cursor
303 compA
= k
; compA
.LowerCase(); // we compare case insensitive
306 entry
= (wxExtHelpMapEntry
*)node
->GetData();
307 compB
= entry
->doc
; compB
.LowerCase();
308 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
310 urls
[idx
] = entry
->url
;
312 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
313 //if(choices[idx].IsEmpty()) // didn't contain the ';'
314 // choices[idx] = (**i).doc;
315 choices
[idx
] = wxT("");
316 for(j
=0;entry
->doc
.c_str()[j
]
317 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
318 choices
[idx
] << entry
->doc
.c_str()[j
];
321 node
= node
->GetNext();
326 rc
= DisplayHelp(urls
[0]);
329 wxMessageBox(_("No entries found."));
334 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
335 showAll
? _("Help Index") : _("Entries found"),
338 rc
= DisplayHelp(urls
[idx
]);
350 wxHTMLHelpControllerBase::Quit()
356 wxHTMLHelpControllerBase::OnQuit()