]>
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"
47 class wxExtHelpMapEntry
: public wxObject
53 wxExtHelpMapEntry(int iid
, wxString
const &iurl
, wxString
const &idoc
)
54 { id
= iid
; url
= iurl
; doc
= idoc
; }
57 IMPLEMENT_ABSTRACT_CLASS(wxHTMLHelpControllerBase
, wxHelpControllerBase
)
60 This class implements help via an external browser.
61 It requires the name of a directory containing the documentation
62 and a file mapping numerical Section numbers to relative URLS.
65 wxHTMLHelpControllerBase::wxHTMLHelpControllerBase()
67 m_MapList
= (wxList
*) NULL
;
72 wxHTMLHelpControllerBase::DeleteList()
76 wxNode
*node
= m_MapList
->First();
79 delete (wxExtHelpMapEntry
*)node
->Data();
81 node
= m_MapList
->First();
84 m_MapList
= (wxList
*) NULL
;
88 wxHTMLHelpControllerBase::~wxHTMLHelpControllerBase()
93 /** This must be called to tell the controller where to find the
95 @param file - NOT a filename, but a directory name.
96 @return true on success
99 wxHTMLHelpControllerBase::Initialize(const wxString
& file
)
101 return LoadFile(file
);
105 // ifile is the name of the base help directory
107 wxHTMLHelpControllerBase::LoadFile(const wxString
& ifile
)
109 wxString mapFile
, file
, url
, doc
;
111 char buffer
[WXEXTHELP_BUFLEN
];
113 wxBusyCursor b
; // display a busy cursor
115 if(! ifile
.IsEmpty())
118 if(! wxIsAbsolutePath(file
))
120 wxChar
* f
= wxGetWorkingDirectory();
122 delete[] f
; // wxGetWorkingDirectory returns new memory
123 file
<< WXEXTHELP_SEPARATOR
<< ifile
;
129 // If a locale is set, look in file/localename, i.e.
130 // If passed "/usr/local/myapp/help" and the current wxLocale is
131 // set to be "de", then look in "/usr/local/myapp/help/de/"
132 // first and fall back to "/usr/local/myapp/help" if that
134 if(wxGetLocale() && !wxGetLocale()->GetName().IsEmpty())
137 newfile
<< WXEXTHELP_SEPARATOR
<< wxGetLocale()->GetName();
138 if(wxDirExists(newfile
))
142 newfile
= WXEXTHELP_SEPARATOR
;
143 const wxChar
*cptr
= wxGetLocale()->GetName().c_str();
144 while(*cptr
&& *cptr
!= _T('_'))
145 newfile
<< *(cptr
++);
146 if(wxDirExists(newfile
))
152 if(! wxDirExists(file
))
155 mapFile
<< file
<< WXEXTHELP_SEPARATOR
<< WXEXTHELP_MAPFILE
;
157 else // try to reload old file
160 if(! wxFileExists(mapFile
))
164 m_MapList
= new wxList
;
167 FILE *input
= fopen(mapFile
.fn_str(),"rt");
172 if(fgets(buffer
,WXEXTHELP_BUFLEN
,input
) && *buffer
!= WXEXTHELP_COMMENTCHAR
)
174 len
= strlen(buffer
);
175 if(buffer
[len
-1] == '\n')
176 buffer
[len
-1] = '\0'; // cut of trailing newline
177 if(sscanf(buffer
,"%d", &id
) != 1)
179 for(i
=0; isdigit(buffer
[i
])||isspace(buffer
[i
])||buffer
[i
]=='-'; i
++)
180 ; // find begin of URL
182 while(buffer
[i
] && ! isspace(buffer
[i
]) && buffer
[i
] !=
183 WXEXTHELP_COMMENTCHAR
)
185 while(buffer
[i
] && buffer
[i
] != WXEXTHELP_COMMENTCHAR
)
189 doc
= (buffer
+ i
+ 1); // skip the comment character
190 m_MapList
->Append(new wxExtHelpMapEntry(id
,url
,doc
));
193 }while(! feof(input
));
196 m_MapFile
= file
; // now it's valid
202 wxHTMLHelpControllerBase::DisplayContents()
208 wxNode
*node
= m_MapList
->First();
209 wxExtHelpMapEntry
*entry
;
212 entry
= (wxExtHelpMapEntry
*)node
->Data();
213 if(entry
->id
== CONTENTS_ID
)
215 contents
= entry
->url
;
223 file
<< m_MapFile
<< WXEXTHELP_SEPARATOR
<< contents
;
224 if(file
.Contains(_T('#')))
225 file
= file
.BeforeLast(_T('#'));
226 if(contents
.Length() && wxFileExists(file
))
227 rc
= DisplaySection(CONTENTS_ID
);
229 // if not found, open homemade toc:
230 return rc
? TRUE
: KeywordSearch(_T(""));
234 wxHTMLHelpControllerBase::DisplaySection(int sectionNo
)
239 wxBusyCursor b
; // display a busy cursor
240 wxNode
*node
= m_MapList
->First();
241 wxExtHelpMapEntry
*entry
;
244 entry
= (wxExtHelpMapEntry
*)node
->Data();
245 if(entry
->id
== sectionNo
)
246 return DisplayHelp(entry
->url
);
253 wxHTMLHelpControllerBase::DisplayBlock(long blockNo
)
255 return DisplaySection((int)blockNo
);
259 wxHTMLHelpControllerBase::KeywordSearch(const wxString
& k
)
264 wxString
*choices
= new wxString
[m_NumOfEntries
];
265 wxString
*urls
= new wxString
[m_NumOfEntries
];
266 wxString compA
, compB
;
270 bool showAll
= k
.IsEmpty();
271 wxNode
*node
= m_MapList
->First();
272 wxExtHelpMapEntry
*entry
;
275 wxBusyCursor b
; // display a busy cursor
276 compA
= k
; compA
.LowerCase(); // we compare case insensitive
279 entry
= (wxExtHelpMapEntry
*)node
->Data();
280 compB
= entry
->doc
; compB
.LowerCase();
281 if((showAll
|| compB
.Contains(k
)) && ! compB
.IsEmpty())
283 urls
[idx
] = entry
->url
;
285 // choices[idx] = (**i).doc.Contains((**i).doc.Before(WXEXTHELP_COMMENTCHAR));
286 //if(choices[idx].IsEmpty()) // didn't contain the ';'
287 // choices[idx] = (**i).doc;
289 for(j
=0;entry
->doc
.c_str()[j
]
290 && entry
->doc
.c_str()[j
] != WXEXTHELP_COMMENTCHAR
; j
++)
291 choices
[idx
] << entry
->doc
.c_str()[j
];
299 rc
= DisplayHelp(urls
[0]);
302 wxMessageBox(_("No entries found."));
307 idx
= wxGetSingleChoiceIndex(showAll
? _("Help Index") : _("Relevant entries:"),
308 showAll
? _("Help Index") : _("Entries found"),
311 rc
= DisplayHelp(urls
[idx
]);
323 wxHTMLHelpControllerBase::Quit()
329 wxHTMLHelpControllerBase::OnQuit()