]> git.saurik.com Git - wxWidgets.git/blob - src/html/helpdata.cpp
fixed handling of the accelerators of menu items added to already attached submenus
[wxWidgets.git] / src / html / helpdata.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpdata.cpp
3 // Purpose: wxHtmlHelpData
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "helpdata.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #if wxUSE_HTML && wxUSE_STREAMS
26
27 #ifndef WXPRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #endif
31
32 #include <ctype.h>
33
34 #include "wx/html/helpdata.h"
35 #include "wx/tokenzr.h"
36 #include "wx/wfstream.h"
37 #include "wx/busyinfo.h"
38 #include "wx/encconv.h"
39 #include "wx/fontmap.h"
40 #include "wx/log.h"
41 #include "wx/html/htmlpars.h"
42 #include "wx/html/htmldefs.h"
43 #include "wx/html/htmlfilt.h"
44 #include "wx/filename.h"
45
46 #include "wx/arrimpl.cpp"
47 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray)
48 WX_DEFINE_OBJARRAY(wxHtmlHelpDataItems)
49
50 //-----------------------------------------------------------------------------
51 // static helper functions
52 //-----------------------------------------------------------------------------
53
54 // Reads one line, stores it into buf and returns pointer to new line or NULL.
55 static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize)
56 {
57 wxChar *writeptr = buf;
58 wxChar *endptr = buf + bufsize - 1;
59 const wxChar *readptr = line;
60
61 while (*readptr != 0 && *readptr != _T('\r') && *readptr != _T('\n') &&
62 writeptr != endptr)
63 *(writeptr++) = *(readptr++);
64 *writeptr = 0;
65 while (*readptr == _T('\r') || *readptr == _T('\n'))
66 readptr++;
67 if (*readptr == 0)
68 return NULL;
69 else
70 return readptr;
71 }
72
73
74
75 static int
76 wxHtmlHelpIndexCompareFunc(wxHtmlHelpDataItem **a, wxHtmlHelpDataItem **b)
77 {
78 wxHtmlHelpDataItem *ia = *a;
79 wxHtmlHelpDataItem *ib = *b;
80
81 if (ia == NULL)
82 return -1;
83 if (ib == NULL)
84 return 1;
85
86 if (ia->parent == ib->parent)
87 {
88 return ia->name.CmpNoCase(ib->name);
89 }
90 else if (ia->level == ib->level)
91 {
92 return wxHtmlHelpIndexCompareFunc(&ia->parent, &ib->parent);
93 }
94 else
95 {
96 wxHtmlHelpDataItem *ia2 = ia;
97 wxHtmlHelpDataItem *ib2 = ib;
98
99 while (ia2->level > ib2->level)
100 {
101 ia2 = ia2->parent;
102 }
103 while (ib2->level > ia2->level)
104 {
105 ib2 = ib2->parent;
106 }
107
108 wxASSERT(ia2);
109 wxASSERT(ib2);
110 int res = wxHtmlHelpIndexCompareFunc(&ia2, &ib2);
111 if (res != 0)
112 return res;
113 else if (ia->level > ib->level)
114 return 1;
115 else
116 return -1;
117 }
118 }
119
120 //-----------------------------------------------------------------------------
121 // HP_Parser
122 //-----------------------------------------------------------------------------
123
124 class HP_Parser : public wxHtmlParser
125 {
126 public:
127 HP_Parser()
128 {
129 GetEntitiesParser()->SetEncoding(wxFONTENCODING_ISO8859_1);
130 }
131
132 wxObject* GetProduct() { return NULL; }
133
134 protected:
135 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
136
137 DECLARE_NO_COPY_CLASS(HP_Parser)
138 };
139
140
141 //-----------------------------------------------------------------------------
142 // HP_TagHandler
143 //-----------------------------------------------------------------------------
144
145 class HP_TagHandler : public wxHtmlTagHandler
146 {
147 private:
148 wxString m_name, m_page;
149 int m_level;
150 int m_id;
151 int m_index;
152 int m_count;
153 wxHtmlHelpDataItem *m_parentItem;
154 wxHtmlBookRecord *m_book;
155
156 wxHtmlHelpDataItems *m_data;
157
158 public:
159 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler()
160 {
161 m_data = NULL;
162 m_book = b;
163 m_name = m_page = wxEmptyString;
164 m_level = 0;
165 m_id = wxID_ANY;
166 m_count = 0;
167 m_parentItem = NULL;
168 }
169 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
170 bool HandleTag(const wxHtmlTag& tag);
171
172 void Reset(wxHtmlHelpDataItems& data)
173 {
174 m_data = &data;
175 m_count = 0;
176 m_level = 0;
177 m_parentItem = NULL;
178 }
179
180 DECLARE_NO_COPY_CLASS(HP_TagHandler)
181 };
182
183
184 bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
185 {
186 if (tag.GetName() == wxT("UL"))
187 {
188 wxHtmlHelpDataItem *oldparent = m_parentItem;
189 m_level++;
190 m_parentItem = (m_count > 0) ? &(*m_data)[m_data->size()-1] : NULL;
191 ParseInner(tag);
192 m_level--;
193 m_parentItem = oldparent;
194 return true;
195 }
196 else if (tag.GetName() == wxT("OBJECT"))
197 {
198 m_name = m_page = wxEmptyString;
199 ParseInner(tag);
200
201 #if 0
202 if (!page.IsEmpty())
203 /* Valid HHW's file may contain only two object tags:
204
205 <OBJECT type="text/site properties">
206 <param name="ImageType" value="Folder">
207 </OBJECT>
208
209 or
210
211 <OBJECT type="text/sitemap">
212 <param name="Name" value="main page">
213 <param name="Local" value="another.htm">
214 </OBJECT>
215
216 We're interested in the latter. !page.IsEmpty() is valid
217 condition because text/site properties does not contain Local param
218 */
219 #endif
220 if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
221 {
222 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem();
223 item->parent = m_parentItem;
224 item->level = m_level;
225 item->id = m_id;
226 item->page = m_page;
227 item->name = m_name;
228
229 item->book = m_book;
230 m_data->Add(item);
231 m_count++;
232 }
233
234 return true;
235 }
236 else
237 { // "PARAM"
238 if (m_name.empty() && tag.GetParam(wxT("NAME")) == wxT("Name"))
239 m_name = tag.GetParam(wxT("VALUE"));
240 if (tag.GetParam(wxT("NAME")) == wxT("Local"))
241 m_page = tag.GetParam(wxT("VALUE"));
242 if (tag.GetParam(wxT("NAME")) == wxT("ID"))
243 tag.GetParamAsInt(wxT("VALUE"), &m_id);
244 return false;
245 }
246 }
247
248
249 //-----------------------------------------------------------------------------
250 // wxHtmlHelpData
251 //-----------------------------------------------------------------------------
252
253 wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const
254 {
255 if (wxIsAbsolutePath(page))
256 return page;
257 else
258 return m_BasePath + page;
259 }
260
261 wxString wxHtmlHelpDataItem::GetIndentedName() const
262 {
263 wxString s;
264 for (int i = 1; i < level; i++)
265 s << _T(" ");
266 s << name;
267 return s;
268 }
269
270
271 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
272
273 wxHtmlHelpData::wxHtmlHelpData()
274 {
275 #if WXWIN_COMPATIBILITY_2_4
276 m_cacheContents = NULL;
277 m_cacheIndex = NULL;
278 #endif
279 }
280
281 wxHtmlHelpData::~wxHtmlHelpData()
282 {
283 #if WXWIN_COMPATIBILITY_2_4
284 CleanCompatibilityData();
285 #endif
286 }
287
288 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys,
289 const wxString& indexfile,
290 const wxString& contentsfile)
291 {
292 wxFSFile *f;
293 wxHtmlFilterHTML filter;
294 wxString buf;
295 wxString string;
296
297 HP_Parser parser;
298 HP_TagHandler *handler = new HP_TagHandler(book);
299 parser.AddTagHandler(handler);
300
301 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
302 if (f)
303 {
304 buf.clear();
305 buf = filter.ReadFile(*f);
306 delete f;
307 handler->Reset(m_contents);
308 parser.Parse(buf);
309 }
310 else
311 {
312 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
313 }
314
315 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
316 if (f)
317 {
318 buf.clear();
319 buf = filter.ReadFile(*f);
320 delete f;
321 handler->Reset(m_index);
322 parser.Parse(buf);
323 }
324 else if (!indexfile.IsEmpty())
325 {
326 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
327 }
328 return true;
329 }
330
331 inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value)
332 {
333 wxInt32 x = wxINT32_SWAP_ON_BE(value);
334 f->Write(&x, sizeof(x));
335 }
336
337 inline static wxInt32 CacheReadInt32(wxInputStream *f)
338 {
339 wxInt32 x;
340 f->Read(&x, sizeof(x));
341 return wxINT32_SWAP_ON_BE(x);
342 }
343
344 inline static void CacheWriteString(wxOutputStream *f, const wxString& str)
345 {
346 const wxWX2MBbuf mbstr = str.mb_str(wxConvUTF8);
347 size_t len = strlen((const char*)mbstr)+1;
348 CacheWriteInt32(f, len);
349 f->Write((const char*)mbstr, len);
350 }
351
352 inline static wxString CacheReadString(wxInputStream *f)
353 {
354 size_t len = (size_t)CacheReadInt32(f);
355 wxCharBuffer str(len-1);
356 f->Read(str.data(), len);
357 return wxString(str, wxConvUTF8);
358 }
359
360 #define CURRENT_CACHED_BOOK_VERSION 5
361
362 // Additional flags to detect incompatibilities of the runtime environment:
363 #define CACHED_BOOK_FORMAT_FLAGS \
364 (wxUSE_UNICODE << 0)
365
366
367 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
368 {
369 int i, st, newsize;
370 wxInt32 version;
371
372 /* load header - version info : */
373 version = CacheReadInt32(f);
374
375 if (version != CURRENT_CACHED_BOOK_VERSION)
376 {
377 // NB: We can just silently return false here and don't worry about
378 // it anymore, because AddBookParam will load the MS project in
379 // absence of (properly versioned) .cached file and automatically
380 // create new .cached file immediately afterward.
381 return false;
382 }
383
384 if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS)
385 return false;
386
387 /* load contents : */
388 st = m_contents.size();
389 newsize = st + CacheReadInt32(f);
390 m_contents.Alloc(newsize);
391 for (i = st; i < newsize; i++)
392 {
393 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem;
394 item->level = CacheReadInt32(f);
395 item->id = CacheReadInt32(f);
396 item->name = CacheReadString(f);
397 item->page = CacheReadString(f);
398 item->book = book;
399 m_contents.Add(item);
400 }
401
402 /* load index : */
403 st = m_index.size();
404 newsize = st + CacheReadInt32(f);
405 m_index.Alloc(newsize);
406 for (i = st; i < newsize; i++)
407 {
408 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem;
409 item->name = CacheReadString(f);
410 item->page = CacheReadString(f);
411 item->level = CacheReadInt32(f);
412 item->book = book;
413 int parentShift = CacheReadInt32(f);
414 if (parentShift != 0)
415 item->parent = &m_index[m_index.size() - parentShift];
416 m_index.Add(item);
417 }
418 return true;
419 }
420
421
422 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
423 {
424 int i;
425 wxInt32 cnt;
426
427 /* save header - version info : */
428 CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION);
429 CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS);
430
431 /* save contents : */
432 int len = m_contents.size();
433 for (cnt = 0, i = 0; i < len; i++)
434 if (m_contents[i].book == book && m_contents[i].level > 0)
435 cnt++;
436 CacheWriteInt32(f, cnt);
437
438 for (i = 0; i < len; i++)
439 {
440 if (m_contents[i].book != book || m_contents[i].level == 0)
441 continue;
442 CacheWriteInt32(f, m_contents[i].level);
443 CacheWriteInt32(f, m_contents[i].id);
444 CacheWriteString(f, m_contents[i].name);
445 CacheWriteString(f, m_contents[i].page);
446 }
447
448 /* save index : */
449 len = m_index.size();
450 for (cnt = 0, i = 0; i < len; i++)
451 if (m_index[i].book == book && m_index[i].level > 0)
452 cnt++;
453 CacheWriteInt32(f, cnt);
454
455 for (i = 0; i < len; i++)
456 {
457 if (m_index[i].book != book || m_index[i].level == 0)
458 continue;
459 CacheWriteString(f, m_index[i].name);
460 CacheWriteString(f, m_index[i].page);
461 CacheWriteInt32(f, m_index[i].level);
462 // save distance to parent item, if any:
463 if (m_index[i].parent == NULL)
464 {
465 CacheWriteInt32(f, 0);
466 }
467 else
468 {
469 int cnt = 0;
470 wxHtmlHelpDataItem *parent = m_index[i].parent;
471 for (int j = i-1; j >= 0; j--)
472 {
473 if (m_index[j].book == book && m_index[j].level > 0)
474 cnt++;
475 if (&m_index[j] == parent)
476 break;
477 }
478 wxASSERT(cnt > 0);
479 CacheWriteInt32(f, cnt);
480 }
481 }
482 return true;
483 }
484
485
486 void wxHtmlHelpData::SetTempDir(const wxString& path)
487 {
488 if (path.empty())
489 m_tempPath = path;
490 else
491 {
492 if (wxIsAbsolutePath(path)) m_tempPath = path;
493 else m_tempPath = wxGetCwd() + _T("/") + path;
494
495 if (m_tempPath[m_tempPath.Length() - 1] != _T('/'))
496 m_tempPath << _T('/');
497 }
498 }
499
500
501
502 static wxString SafeFileName(const wxString& s)
503 {
504 wxString res(s);
505 res.Replace(wxT("#"), wxT("_"));
506 res.Replace(wxT(":"), wxT("_"));
507 res.Replace(wxT("\\"), wxT("_"));
508 res.Replace(wxT("/"), wxT("_"));
509 return res;
510 }
511
512 bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
513 wxFontEncoding encoding,
514 const wxString& title, const wxString& contfile,
515 const wxString& indexfile, const wxString& deftopic,
516 const wxString& path)
517 {
518 wxFileSystem fsys;
519 wxFSFile *fi;
520 wxHtmlBookRecord *bookr;
521
522 int IndexOld = m_index.size(),
523 ContentsOld = m_contents.size();
524
525 if (!path.IsEmpty())
526 fsys.ChangePathTo(path, true);
527
528 size_t booksCnt = m_bookRecords.GetCount();
529 for (size_t i = 0; i < booksCnt; i++)
530 {
531 if ( m_bookRecords[i].GetBookFile() == bookfile.GetLocation() )
532 return true; // book is (was) loaded
533 }
534
535 bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title, deftopic);
536
537 wxHtmlHelpDataItem *bookitem = new wxHtmlHelpDataItem;
538 bookitem->level = 0;
539 bookitem->id = 0;
540 bookitem->page = deftopic;
541 bookitem->name = title;
542 bookitem->book = bookr;
543
544 // store the contents index for later
545 int cont_start = m_contents.size();
546
547 m_contents.Add(bookitem);
548
549 // Try to find cached binary versions:
550 // 1. save file as book, but with .hhp.cached extension
551 // 2. same as 1. but in temp path
552 // 3. otherwise or if cache load failed, load it from MS.
553
554 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
555
556 if (fi == NULL ||
557 #if wxUSE_DATETIME
558 fi->GetModificationTime() < bookfile.GetModificationTime() ||
559 #endif // wxUSE_DATETIME
560 !LoadCachedBook(bookr, fi->GetStream()))
561 {
562 if (fi != NULL) delete fi;
563 fi = fsys.OpenFile(m_tempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
564 if (m_tempPath.empty() || fi == NULL ||
565 #if wxUSE_DATETIME
566 fi->GetModificationTime() < bookfile.GetModificationTime() ||
567 #endif // wxUSE_DATETIME
568 !LoadCachedBook(bookr, fi->GetStream()))
569 {
570 LoadMSProject(bookr, fsys, indexfile, contfile);
571 if (!m_tempPath.empty())
572 {
573 wxFileOutputStream *outs = new wxFileOutputStream(m_tempPath +
574 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
575 SaveCachedBook(bookr, outs);
576 delete outs;
577 }
578 }
579 }
580
581 if (fi != NULL) delete fi;
582
583 // Now store the contents range
584 bookr->SetContentsRange(cont_start, m_contents.size());
585
586 #if wxUSE_WCHAR_T
587 // MS HTML Help files [written by MS HTML Help Workshop] are broken
588 // in that the data are iso-8859-1 (including HTML entities), but must
589 // be interpreted as being in language's windows charset. Correct the
590 // differences here and also convert to wxConvLocal in ANSI build
591 if (encoding != wxFONTENCODING_SYSTEM)
592 {
593 #if wxUSE_UNICODE
594 #define CORRECT_STR(str, conv) \
595 str = wxString((str).mb_str(wxConvISO8859_1), conv)
596 #else
597 #define CORRECT_STR(str, conv) \
598 str = wxString((str).wc_str(conv), wxConvLocal)
599 #endif
600 wxCSConv conv(encoding);
601 size_t IndexCnt = m_index.size();
602 size_t ContentsCnt = m_contents.size();
603 size_t i;
604 for (i = IndexOld; i < IndexCnt; i++)
605 {
606 CORRECT_STR(m_index[i].name, conv);
607 }
608 for (i = ContentsOld; i < ContentsCnt; i++)
609 {
610 CORRECT_STR(m_contents[i].name, conv);
611 }
612 #undef CORRECT_STR
613 }
614 #else
615 wxUnusedVar(IndexOld);
616 wxUnusedVar(ContentsOld);
617 wxASSERT_MSG(encoding == wxFONTENCODING_SYSTEM, wxT("Help files need charset conversion, but wxUSE_WCHAR_T is 0"));
618 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
619
620 m_bookRecords.Add(bookr);
621 if (!m_index.empty())
622 {
623 m_index.Sort(wxHtmlHelpIndexCompareFunc);
624 }
625
626 return true;
627 }
628
629
630 bool wxHtmlHelpData::AddBook(const wxString& book)
631 {
632 wxString extension(book.Right(4).Lower());
633 if (extension == wxT(".zip") ||
634 #if wxUSE_LIBMSPACK
635 extension == wxT(".chm") /*compressed html help book*/ ||
636 #endif
637 extension == wxT(".htb") /*html book*/)
638 {
639 wxFileSystem fsys;
640 wxString s;
641 bool rt = false;
642
643 #if wxUSE_LIBMSPACK
644 if (extension == wxT(".chm"))
645 s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE);
646 else
647 #endif
648 s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE);
649
650 while (!s.IsEmpty())
651 {
652 if (AddBook(s)) rt = true;
653 s = fsys.FindNext();
654 }
655
656 return rt;
657 }
658
659 wxFSFile *fi;
660 wxFileSystem fsys;
661
662 wxString title = _("noname"),
663 safetitle,
664 start = wxEmptyString,
665 contents = wxEmptyString,
666 index = wxEmptyString,
667 charset = wxEmptyString;
668
669 fi = fsys.OpenFile(book);
670 if (fi == NULL)
671 {
672 wxLogError(_("Cannot open HTML help book: %s"), book.c_str());
673 return false;
674 }
675 fsys.ChangePathTo(book);
676
677 const wxChar *lineptr;
678 wxChar linebuf[300];
679 wxString tmp;
680 wxHtmlFilterPlainText filter;
681 tmp = filter.ReadFile(*fi);
682 lineptr = tmp.c_str();
683
684 do
685 {
686 lineptr = ReadLine(lineptr, linebuf, 300);
687
688 for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++)
689 *ch = (wxChar)wxTolower(*ch);
690
691 if (wxStrstr(linebuf, _T("title=")) == linebuf)
692 title = linebuf + wxStrlen(_T("title="));
693 if (wxStrstr(linebuf, _T("default topic=")) == linebuf)
694 start = linebuf + wxStrlen(_T("default topic="));
695 if (wxStrstr(linebuf, _T("index file=")) == linebuf)
696 index = linebuf + wxStrlen(_T("index file="));
697 if (wxStrstr(linebuf, _T("contents file=")) == linebuf)
698 contents = linebuf + wxStrlen(_T("contents file="));
699 if (wxStrstr(linebuf, _T("charset=")) == linebuf)
700 charset = linebuf + wxStrlen(_T("charset="));
701 } while (lineptr != NULL);
702
703 wxFontEncoding enc = wxFONTENCODING_SYSTEM;
704 #if wxUSE_FONTMAP
705 if (charset != wxEmptyString)
706 enc = wxFontMapper::Get()->CharsetToEncoding(charset);
707 #endif
708
709 bool rtval = AddBookParam(*fi, enc,
710 title, contents, index, start, fsys.GetPath());
711 delete fi;
712
713 #if WXWIN_COMPATIBILITY_2_4
714 CleanCompatibilityData();
715 #endif
716
717 return rtval;
718 }
719
720 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
721 {
722 int cnt;
723 int i;
724 wxFileSystem fsys;
725 wxFSFile *f;
726
727 /* 1. try to open given file: */
728
729 cnt = m_bookRecords.GetCount();
730 for (i = 0; i < cnt; i++)
731 {
732 f = fsys.OpenFile(m_bookRecords[i].GetFullPath(x));
733 if (f)
734 {
735 wxString url = m_bookRecords[i].GetFullPath(x);
736 delete f;
737 return url;
738 }
739 }
740
741
742 /* 2. try to find a book: */
743
744 for (i = 0; i < cnt; i++)
745 {
746 if (m_bookRecords[i].GetTitle() == x)
747 return m_bookRecords[i].GetFullPath(m_bookRecords[i].GetStart());
748 }
749
750 /* 3. try to find in contents: */
751
752 cnt = m_contents.size();
753 for (i = 0; i < cnt; i++)
754 {
755 if (m_contents[i].name == x)
756 return m_contents[i].GetFullPath();
757 }
758
759
760 /* 4. try to find in index: */
761
762 cnt = m_index.size();
763 for (i = 0; i < cnt; i++)
764 {
765 if (m_index[i].name == x)
766 return m_index[i].GetFullPath();
767 }
768
769 return wxEmptyString;
770 }
771
772 wxString wxHtmlHelpData::FindPageById(int id)
773 {
774 size_t cnt = m_contents.size();
775 for (size_t i = 0; i < cnt; i++)
776 {
777 if (m_contents[i].id == id)
778 {
779 return m_contents[i].GetFullPath();
780 }
781 }
782
783 return wxEmptyString;
784 }
785
786 #if WXWIN_COMPATIBILITY_2_4
787 wxHtmlContentsItem::wxHtmlContentsItem()
788 : m_Level(0), m_ID(wxID_ANY), m_Name(NULL), m_Page(NULL), m_Book(NULL),
789 m_autofree(false)
790 {
791 }
792
793 wxHtmlContentsItem::wxHtmlContentsItem(const wxHtmlHelpDataItem& d)
794 {
795 m_autofree = true;
796 m_Level = d.level;
797 m_ID = d.id;
798 m_Name = wxStrdup(d.name.c_str());
799 m_Page = wxStrdup(d.page.c_str());
800 m_Book = d.book;
801 }
802
803 wxHtmlContentsItem& wxHtmlContentsItem::operator=(const wxHtmlContentsItem& d)
804 {
805 if (m_autofree)
806 {
807 free(m_Name);
808 free(m_Page);
809 }
810 m_autofree = true;
811 m_Level = d.m_Level;
812 m_ID = d.m_ID;
813 m_Name = d.m_Name ? wxStrdup(d.m_Name) : NULL;
814 m_Page = d.m_Page ? wxStrdup(d.m_Page) : NULL;
815 m_Book = d.m_Book;
816 return *this;
817 }
818
819 wxHtmlContentsItem::~wxHtmlContentsItem()
820 {
821 if (m_autofree)
822 {
823 free(m_Name);
824 free(m_Page);
825 }
826 }
827
828 wxHtmlContentsItem* wxHtmlHelpData::GetContents()
829 {
830 if (!m_cacheContents && !m_contents.empty())
831 {
832 size_t len = m_contents.size();
833 m_cacheContents = new wxHtmlContentsItem[len];
834 for (size_t i = 0; i < len; i++)
835 m_cacheContents[i] = m_contents[i];
836 }
837 return m_cacheContents;
838 }
839
840 int wxHtmlHelpData::GetContentsCnt()
841 {
842 return m_contents.size();
843 }
844
845 wxHtmlContentsItem* wxHtmlHelpData::GetIndex()
846 {
847 if (!m_cacheContents && !m_index.empty())
848 {
849 size_t len = m_index.size();
850 m_cacheContents = new wxHtmlContentsItem[len];
851 for (size_t i = 0; i < len; i++)
852 m_cacheContents[i] = m_index[i];
853 }
854 return m_cacheContents;
855 }
856
857 int wxHtmlHelpData::GetIndexCnt()
858 {
859 return m_index.size();
860 }
861
862 void wxHtmlHelpData::CleanCompatibilityData()
863 {
864 delete[] m_cacheContents;
865 m_cacheContents = NULL;
866 delete[] m_cacheIndex;
867 m_cacheIndex = NULL;
868 }
869 #endif // WXWIN_COMPATIBILITY_2_4
870
871 //----------------------------------------------------------------------------------
872 // wxHtmlSearchStatus functions
873 //----------------------------------------------------------------------------------
874
875 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
876 bool case_sensitive, bool whole_words_only,
877 const wxString& book)
878 {
879 m_Data = data;
880 m_Keyword = keyword;
881 wxHtmlBookRecord* bookr = NULL;
882 if (book != wxEmptyString)
883 {
884 // we have to search in a specific book. Find it first
885 int i, cnt = data->m_bookRecords.GetCount();
886 for (i = 0; i < cnt; i++)
887 if (data->m_bookRecords[i].GetTitle() == book)
888 {
889 bookr = &(data->m_bookRecords[i]);
890 m_CurIndex = bookr->GetContentsStart();
891 m_MaxIndex = bookr->GetContentsEnd();
892 break;
893 }
894 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
895 wxASSERT(bookr);
896 }
897 if (! bookr)
898 {
899 // no book specified; search all books
900 m_CurIndex = 0;
901 m_MaxIndex = m_Data->m_contents.size();
902 }
903 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
904 m_Active = (m_CurIndex < m_MaxIndex);
905 }
906
907 #if WXWIN_COMPATIBILITY_2_4
908 wxHtmlContentsItem* wxHtmlSearchStatus::GetContentsItem()
909 {
910 static wxHtmlContentsItem it;
911 it = wxHtmlContentsItem(*m_CurItem);
912 return &it;
913 }
914 #endif
915
916 bool wxHtmlSearchStatus::Search()
917 {
918 wxFSFile *file;
919 int i = m_CurIndex; // shortcut
920 bool found = false;
921 wxString thepage;
922
923 if (!m_Active)
924 {
925 // sanity check. Illegal use, but we'll try to prevent a crash anyway
926 wxASSERT(m_Active);
927 return false;
928 }
929
930 m_Name = wxEmptyString;
931 m_CurItem = NULL;
932 thepage = m_Data->m_contents[i].page;
933
934 m_Active = (++m_CurIndex < m_MaxIndex);
935 // check if it is same page with different anchor:
936 if (!m_LastPage.empty())
937 {
938 const wxChar *p1, *p2;
939 for (p1 = thepage.c_str(), p2 = m_LastPage.c_str();
940 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
941
942 m_LastPage = thepage;
943
944 if (*p1 == 0 || *p1 == _T('#'))
945 return false;
946 }
947 else m_LastPage = thepage;
948
949 wxFileSystem fsys;
950 file = fsys.OpenFile(m_Data->m_contents[i].book->GetFullPath(thepage));
951 if (file)
952 {
953 if (m_Engine.Scan(*file))
954 {
955 m_Name = m_Data->m_contents[i].name;
956 m_CurItem = &m_Data->m_contents[i];
957 found = true;
958 }
959 delete file;
960 }
961 return found;
962 }
963
964
965
966
967
968
969
970
971 //--------------------------------------------------------------------------------
972 // wxHtmlSearchEngine
973 //--------------------------------------------------------------------------------
974
975 void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
976 {
977 m_CaseSensitive = case_sensitive;
978 m_WholeWords = whole_words_only;
979 m_Keyword = keyword;
980
981 if (!m_CaseSensitive)
982 m_Keyword.LowerCase();
983 }
984
985
986 static inline bool WHITESPACE(wxChar c)
987 {
988 return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t');
989 }
990
991 bool wxHtmlSearchEngine::Scan(const wxFSFile& file)
992 {
993 wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
994
995 int i, j;
996 int wrd = m_Keyword.Length();
997 bool found = false;
998 wxHtmlFilterHTML filter;
999 wxString tmp = filter.ReadFile(file);
1000 int lng = tmp.length();
1001 const wxChar *buf = tmp.c_str();
1002
1003 if (!m_CaseSensitive)
1004 tmp.LowerCase();
1005
1006 const wxChar *kwd = m_Keyword.c_str();
1007
1008 if (m_WholeWords)
1009 {
1010 for (i = 0; i < lng - wrd; i++)
1011 {
1012 if (WHITESPACE(buf[i])) continue;
1013 j = 0;
1014 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1015 if (j == wrd && WHITESPACE(buf[i + j])) { found = true; break; }
1016 }
1017 }
1018
1019 else
1020 {
1021 for (i = 0; i < lng - wrd; i++)
1022 {
1023 j = 0;
1024 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1025 if (j == wrd) { found = true; break; }
1026 }
1027 }
1028
1029 return found;
1030 }
1031
1032
1033
1034 #endif