| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: helpdata.h |
| 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 | #ifndef _WX_HELPDATA_H_ |
| 13 | #define _WX_HELPDATA_H_ |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_HTML |
| 18 | |
| 19 | #include "wx/object.h" |
| 20 | #include "wx/string.h" |
| 21 | #include "wx/filesys.h" |
| 22 | #include "wx/dynarray.h" |
| 23 | #include "wx/font.h" |
| 24 | |
| 25 | class WXDLLIMPEXP_HTML wxHtmlHelpData; |
| 26 | |
| 27 | //-------------------------------------------------------------------------------- |
| 28 | // helper classes & structs |
| 29 | //-------------------------------------------------------------------------------- |
| 30 | |
| 31 | class WXDLLIMPEXP_HTML wxHtmlBookRecord |
| 32 | { |
| 33 | public: |
| 34 | wxHtmlBookRecord(const wxString& bookfile, const wxString& basepath, |
| 35 | const wxString& title, const wxString& start) |
| 36 | { |
| 37 | m_BookFile = bookfile; |
| 38 | m_BasePath = basepath; |
| 39 | m_Title = title; |
| 40 | m_Start = start; |
| 41 | // for debugging, give the contents index obvious default values |
| 42 | m_ContentsStart = m_ContentsEnd = -1; |
| 43 | } |
| 44 | wxString GetBookFile() const { return m_BookFile; } |
| 45 | wxString GetTitle() const { return m_Title; } |
| 46 | wxString GetStart() const { return m_Start; } |
| 47 | wxString GetBasePath() const { return m_BasePath; } |
| 48 | /* SetContentsRange: store in the bookrecord where in the index/contents lists the |
| 49 | * book's records are stored. This to facilitate searching in a specific book. |
| 50 | * This code will have to be revised when loading/removing books becomes dynamic. |
| 51 | * (as opposed to appending only) |
| 52 | * Note that storing index range is pointless, because the index is alphab. sorted. */ |
| 53 | void SetContentsRange(int start, int end) { m_ContentsStart = start; m_ContentsEnd = end; } |
| 54 | int GetContentsStart() const { return m_ContentsStart; } |
| 55 | int GetContentsEnd() const { return m_ContentsEnd; } |
| 56 | |
| 57 | void SetTitle(const wxString& title) { m_Title = title; } |
| 58 | void SetBasePath(const wxString& path) { m_BasePath = path; } |
| 59 | void SetStart(const wxString& start) { m_Start = start; } |
| 60 | |
| 61 | // returns full filename of page (which is part of the book), |
| 62 | // i.e. with book's basePath prepended. If page is already absolute |
| 63 | // path, basePath is _not_ prepended. |
| 64 | wxString GetFullPath(const wxString &page) const; |
| 65 | |
| 66 | protected: |
| 67 | wxString m_BookFile; |
| 68 | wxString m_BasePath; |
| 69 | wxString m_Title; |
| 70 | wxString m_Start; |
| 71 | int m_ContentsStart; |
| 72 | int m_ContentsEnd; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | WX_DECLARE_USER_EXPORTED_OBJARRAY(wxHtmlBookRecord, wxHtmlBookRecArray, |
| 77 | WXDLLIMPEXP_HTML); |
| 78 | |
| 79 | struct WXDLLIMPEXP_HTML wxHtmlHelpDataItem |
| 80 | { |
| 81 | wxHtmlHelpDataItem() : level(0), parent(NULL), id(wxID_ANY), book(NULL) {} |
| 82 | |
| 83 | int level; |
| 84 | wxHtmlHelpDataItem *parent; |
| 85 | int id; |
| 86 | wxString name; |
| 87 | wxString page; |
| 88 | wxHtmlBookRecord *book; |
| 89 | |
| 90 | // returns full filename of m_Page, i.e. with book's basePath prepended |
| 91 | wxString GetFullPath() const { return book->GetFullPath(page); } |
| 92 | |
| 93 | // returns item indented with spaces if it has level>1: |
| 94 | wxString GetIndentedName() const; |
| 95 | }; |
| 96 | |
| 97 | WX_DECLARE_USER_EXPORTED_OBJARRAY(wxHtmlHelpDataItem, wxHtmlHelpDataItems, |
| 98 | WXDLLIMPEXP_HTML); |
| 99 | |
| 100 | #if WXWIN_COMPATIBILITY_2_4 |
| 101 | // old interface to contents and index: |
| 102 | struct wxHtmlContentsItem |
| 103 | { |
| 104 | wxHtmlContentsItem(); |
| 105 | wxHtmlContentsItem(const wxHtmlHelpDataItem& d); |
| 106 | wxHtmlContentsItem& operator=(const wxHtmlContentsItem& d); |
| 107 | ~wxHtmlContentsItem(); |
| 108 | |
| 109 | int m_Level; |
| 110 | int m_ID; |
| 111 | wxChar *m_Name; |
| 112 | wxChar *m_Page; |
| 113 | wxHtmlBookRecord *m_Book; |
| 114 | |
| 115 | // returns full filename of m_Page, i.e. with book's basePath prepended |
| 116 | wxString GetFullPath() const { return m_Book->GetFullPath(m_Page); } |
| 117 | |
| 118 | private: |
| 119 | bool m_autofree; |
| 120 | }; |
| 121 | #endif |
| 122 | |
| 123 | |
| 124 | //------------------------------------------------------------------------------ |
| 125 | // wxHtmlSearchEngine |
| 126 | // This class takes input streams and scans them for occurence |
| 127 | // of keyword(s) |
| 128 | //------------------------------------------------------------------------------ |
| 129 | |
| 130 | class WXDLLIMPEXP_HTML wxHtmlSearchEngine : public wxObject |
| 131 | { |
| 132 | public: |
| 133 | wxHtmlSearchEngine() : wxObject() {} |
| 134 | ~wxHtmlSearchEngine() {} |
| 135 | |
| 136 | // Sets the keyword we will be searching for |
| 137 | virtual void LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only); |
| 138 | |
| 139 | // Scans the stream for the keyword. |
| 140 | // Returns true if the stream contains keyword, fALSE otherwise |
| 141 | virtual bool Scan(const wxFSFile& file); |
| 142 | |
| 143 | private: |
| 144 | wxString m_Keyword; |
| 145 | bool m_CaseSensitive; |
| 146 | bool m_WholeWords; |
| 147 | |
| 148 | DECLARE_NO_COPY_CLASS(wxHtmlSearchEngine) |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | // State information of a search action. I'd have preferred to make this a |
| 153 | // nested class inside wxHtmlHelpData, but that's against coding standards :-( |
| 154 | // Never construct this class yourself, obtain a copy from |
| 155 | // wxHtmlHelpData::PrepareKeywordSearch(const wxString& key) |
| 156 | class WXDLLIMPEXP_HTML wxHtmlSearchStatus |
| 157 | { |
| 158 | public: |
| 159 | // constructor; supply wxHtmlHelpData ptr, the keyword and (optionally) the |
| 160 | // title of the book to search. By default, all books are searched. |
| 161 | wxHtmlSearchStatus(wxHtmlHelpData* base, const wxString& keyword, |
| 162 | bool case_sensitive, bool whole_words_only, |
| 163 | const wxString& book = wxEmptyString); |
| 164 | bool Search(); // do the next iteration |
| 165 | bool IsActive() { return m_Active; } |
| 166 | int GetCurIndex() { return m_CurIndex; } |
| 167 | int GetMaxIndex() { return m_MaxIndex; } |
| 168 | const wxString& GetName() { return m_Name; } |
| 169 | |
| 170 | const wxHtmlHelpDataItem *GetCurItem() const { return m_CurItem; } |
| 171 | #if WXWIN_COMPATIBILITY_2_4 |
| 172 | wxDEPRECATED( wxHtmlContentsItem* GetContentsItem() ); |
| 173 | #endif |
| 174 | |
| 175 | private: |
| 176 | wxHtmlHelpData* m_Data; |
| 177 | wxHtmlSearchEngine m_Engine; |
| 178 | wxString m_Keyword, m_Name; |
| 179 | wxString m_LastPage; |
| 180 | wxHtmlHelpDataItem* m_CurItem; |
| 181 | bool m_Active; // search is not finished |
| 182 | int m_CurIndex; // where we are now |
| 183 | int m_MaxIndex; // number of files we search |
| 184 | // For progress bar: 100*curindex/maxindex = % complete |
| 185 | |
| 186 | DECLARE_NO_COPY_CLASS(wxHtmlSearchStatus) |
| 187 | }; |
| 188 | |
| 189 | class WXDLLIMPEXP_HTML wxHtmlHelpData : public wxObject |
| 190 | { |
| 191 | DECLARE_DYNAMIC_CLASS(wxHtmlHelpData) |
| 192 | friend class wxHtmlSearchStatus; |
| 193 | |
| 194 | public: |
| 195 | wxHtmlHelpData(); |
| 196 | ~wxHtmlHelpData(); |
| 197 | |
| 198 | // Sets directory where temporary files are stored. |
| 199 | // These temp files are index & contents file in binary (much faster to read) |
| 200 | // form. These files are NOT deleted on program's exit. |
| 201 | void SetTempDir(const wxString& path); |
| 202 | |
| 203 | // Adds new book. 'book' is location of .htb file (stands for "html book"). |
| 204 | // See documentation for details on its format. |
| 205 | // Returns success. |
| 206 | bool AddBook(const wxString& book); |
| 207 | bool AddBookParam(const wxFSFile& bookfile, |
| 208 | wxFontEncoding encoding, |
| 209 | const wxString& title, const wxString& contfile, |
| 210 | const wxString& indexfile = wxEmptyString, |
| 211 | const wxString& deftopic = wxEmptyString, |
| 212 | const wxString& path = wxEmptyString); |
| 213 | |
| 214 | // Some accessing stuff: |
| 215 | |
| 216 | // returns URL of page on basis of (file)name |
| 217 | wxString FindPageByName(const wxString& page); |
| 218 | // returns URL of page on basis of MS id |
| 219 | wxString FindPageById(int id); |
| 220 | |
| 221 | const wxHtmlBookRecArray& GetBookRecArray() const { return m_bookRecords; } |
| 222 | |
| 223 | const wxHtmlHelpDataItems& GetContentsArray() const { return m_contents; } |
| 224 | const wxHtmlHelpDataItems& GetIndexArray() const { return m_index; } |
| 225 | |
| 226 | #if WXWIN_COMPATIBILITY_2_4 |
| 227 | // deprecated interface, new interface is arrays-based (see above) |
| 228 | wxDEPRECATED( wxHtmlContentsItem* GetContents() ); |
| 229 | wxDEPRECATED( int GetContentsCnt() ); |
| 230 | wxDEPRECATED( wxHtmlContentsItem* GetIndex() ); |
| 231 | wxDEPRECATED( int GetIndexCnt() ); |
| 232 | #endif |
| 233 | |
| 234 | protected: |
| 235 | wxString m_tempPath; |
| 236 | |
| 237 | // each book has one record in this array: |
| 238 | wxHtmlBookRecArray m_bookRecords; |
| 239 | |
| 240 | wxHtmlHelpDataItems m_contents; // list of all available books and pages |
| 241 | wxHtmlHelpDataItems m_index; // list of index itesm |
| 242 | |
| 243 | #if WXWIN_COMPATIBILITY_2_4 |
| 244 | // deprecated data structures, set only if GetContents(), GetIndex() |
| 245 | // called |
| 246 | wxHtmlContentsItem* m_cacheContents; |
| 247 | wxHtmlContentsItem* m_cacheIndex; |
| 248 | private: |
| 249 | void CleanCompatibilityData(); |
| 250 | #endif |
| 251 | |
| 252 | protected: |
| 253 | // Imports .hhp files (MS HTML Help Workshop) |
| 254 | bool LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, |
| 255 | const wxString& indexfile, const wxString& contentsfile); |
| 256 | // Reads binary book |
| 257 | bool LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f); |
| 258 | // Writes binary book |
| 259 | bool SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f); |
| 260 | |
| 261 | DECLARE_NO_COPY_CLASS(wxHtmlHelpData) |
| 262 | }; |
| 263 | |
| 264 | #endif |
| 265 | |
| 266 | #endif |