]>
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"
49 class wxExtHelpMapEntry
: public wxObject
55 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
56 { id
= iid
; url
= iurl
; doc
= idoc
; }
59 IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase
, wxHelpControllerBase
)
62 This class implements help via an external browser.
63 It requires the name of a directory containing the documentation
64 and a file mapping numerical Section numbers to relative URLS.
67 wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
69 m_MapList
= (wxList
*) NULL
;
74 wxHTMLHelpControllerBase::DeleteList()
78 wxNode
*node
= m_MapList
->First();
81 delete (wxExtHelpMapEntry
*)node
->Data();
83 node
= m_MapList
->First();
86 m_MapList
= (wxList
*) NULL
;
90 wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase()
95 /** This must be called to tell the controller where to find the
97 @param file - NOT a filename, but a directory name.
98 @return true on success
101 wxHTMLHelpControllerBase::Initialize(const wxString
& file
)
103 return LoadFile(file
);
107 // ifile is the name of the base help directory
109 wxHTMLHelpControllerBase::LoadFile(const wxString
& ifile
)
111 wxString mapFile
, file
, url
, doc
;
113 char buffer
[WXEXTHELP_BUFLEN
];
115 wxBusyCursor b
; // display a busy cursor
117 if(! ifile
.IsEmpty())
120 if(! wxIsAbsolutePath(file
))
122 wxChar
* f
= wxGetWorkingDirectory();
124 delete[] f
; // wxGetWorkingDirectory returns new memory
125 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
131 // If a locale is set, look in file/localename, i.e.
132 // If passed "/usr/local/myapp/help" and the current wxLocale is
133 // set to be "de", then look in "/usr/local/myapp/help/de/"
134 // first and fall back to "/usr/local/myapp/help" if that
136 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
139 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
140 if(wxDirExists(newfile
))
144 newfile
= WXEXTHELP_SEPARATOR
;
145 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
146 while(*cptr
&& *cptr
!= _T('_'))
147 newfile
<< *(cptr
++);
148 if(wxDirExists(newfile
))
154 if(! wxDirExists(file
))
157 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
159 else // try to reload old file
162 if(! wxFileExists(mapFile
))
166 m_MapList
= new wxList
;
169 FILE *input
= fopen(mapFile
.fn_str(),"rt");
174 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
176 len
= strlen(buffer
);
177 if(buffer
[len
-1] == '\n')
178 buffer
[len
-1] = '\0'; // cut of trailing newline
179 if(sscanf(buffer
,"%d", &id
) != 1)
181 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
182 ; // find begin of URL
184 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
185 WXEXTHELP_COMMENTCHAR
)
187 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
191 doc
= (buffer
+ i
+ 1); // skip the comment character
192 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
195 }while(! feof(input
));
198 m_MapFile
= file
; // now it's valid
204 wxHTMLHelpControllerBase::DisplayContents()
210 wxNode
*node
= m_MapList
->First();
211 wxExtHelpMapEntry
*entry
;
214 entry
= (wxExtHelpMapEntry
*)node
->Data();
215 if(entry
->id
== CONTENTS_ID
)
217 contents
= entry
->url
;
225 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
226 if(file
.Contains(_T('#')))
227 file
= file
.BeforeLast(_T('#'));
228 if(contents
.Length() && wxFileExists(file
))
229 rc
= DisplaySection(CONTENTS_ID
);
231 // if not found, open homemade toc:
232 return rc
? TRUE
: KeywordSearch(_T(""));
236 wxHTMLHelpControllerBase::DisplaySection(int sectionNo
)
241 wxBusyCursor b
; // display a busy cursor
242 wxNode
*node
= m_MapList
->First();
243 wxExtHelpMapEntry
*entry
;
246 entry
= (wxExtHelpMapEntry
*)node
->Data();
247 if(entry
->id
== sectionNo
)
248 return DisplayHelp(entry
->url
);
255 wxHTMLHelpControllerBase::DisplayBlock(long blockNo
)
257 return DisplaySection((int)blockNo
);
261 wxHTMLHelpControllerBase::KeywordSearch(const wxString
& k
)
266 wxString
*choices
= new wxString
[m_NumOfEntries
];
267 wxString
*urls
= new wxString
[m_NumOfEntries
];
268 wxString compA
, compB
;
272 bool showAll
= k
.IsEmpty();
273 wxNode
*node
= m_MapList
->First();
274 wxExtHelpMapEntry
*entry
;
277 wxBusyCursor b
; // display a busy cursor
278 compA
= k
; compA
.LowerCase(); // we compare case insensitive
281 entry
= (wxExtHelpMapEntry
*)node
->Data();
282 compB
= entry
->doc
; compB
.LowerCase();
283 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
285 urls
[idx
] = entry
->url
;
287 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
288 //if(choices[idx].IsEmpty()) // didn't contain the ';'
289 // choices[idx] = (**i).doc;
291 for(j
=0;entry
->doc
.c_str()[j
]
292 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
293 choices
[idx
] << entry
->doc
.c_str()[j
];
301 rc
= DisplayHelp(urls
[0]);
304 wxMessageBox(_("No entries found."));
309 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
310 showAll
? _("Help Index") : _("Entries found"),
313 rc
= DisplayHelp(urls
[idx
]);
325 wxHTMLHelpControllerBase::Quit()
331 wxHTMLHelpControllerBase::OnQuit()