]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmlhelp.cpp | |
3 | // Purpose: Help controller | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | ||
10 | //#ifdef __GNUG__ | |
11 | //#pragma implementation "htmlhelp.h" | |
12 | //#endif | |
13 | // --- already in htmlhelp.cpp | |
14 | ||
15 | #include <wx/wxprec.h> | |
16 | ||
17 | #include "wx/defs.h" | |
18 | #if wxUSE_HTML | |
19 | ||
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
25 | #include <wx/wx.h> | |
26 | #endif | |
27 | ||
28 | ||
29 | #include <wx/wxhtml.h> | |
30 | #include <wx/busyinfo.h> | |
31 | ||
32 | ||
33 | ||
34 | ||
35 | class HP_Parser : public wxHtmlParser | |
36 | { | |
37 | public: | |
38 | void AddText(const char* text) {} | |
39 | wxObject* GetProduct() {return NULL;} | |
40 | }; | |
41 | ||
42 | ||
43 | ||
44 | class HP_TagHandler : public wxHtmlTagHandler | |
45 | { | |
46 | private: | |
47 | wxString m_Name, m_Page; | |
48 | int m_Level; | |
49 | int m_ID; | |
50 | int m_Index; | |
51 | HtmlContentsItem *m_Items; | |
52 | int m_ItemsCnt; | |
53 | HtmlBookRecord *m_Book; | |
54 | ||
55 | public: | |
56 | HP_TagHandler(HtmlBookRecord *b) : wxHtmlTagHandler() {m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString; m_Level = 0;} | |
57 | wxString GetSupportedTags() {return "UL,OBJECT,PARAM";} | |
58 | bool HandleTag(const wxHtmlTag& tag); | |
59 | void WriteOut(HtmlContentsItem*& array, int& size); | |
60 | void ReadIn(HtmlContentsItem* array, int size); | |
61 | }; | |
62 | ||
63 | ||
64 | bool HP_TagHandler::HandleTag(const wxHtmlTag& tag) | |
65 | { | |
66 | if (tag.GetName() == "UL") { | |
67 | m_Level++; | |
68 | ParseInner(tag); | |
69 | m_Level--; | |
70 | return TRUE; | |
71 | } | |
72 | ||
73 | else if (tag.GetName() == "OBJECT") { | |
74 | m_Name = m_Page = wxEmptyString; | |
75 | ParseInner(tag); | |
76 | if (m_Page != wxEmptyString) { | |
77 | if (m_ItemsCnt % HTML_REALLOC_STEP == 0) | |
78 | m_Items = (HtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + HTML_REALLOC_STEP) * sizeof(HtmlContentsItem)); | |
79 | m_Items[m_ItemsCnt].m_Level = m_Level; | |
80 | m_Items[m_ItemsCnt].m_ID = m_ID; | |
2776d7c3 | 81 | m_Items[m_ItemsCnt].m_Page = new char[m_Page.Length() + 1]; |
5526e819 | 82 | strcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str()); |
2776d7c3 | 83 | m_Items[m_ItemsCnt].m_Name = new char [m_Name.Length() + 1]; |
5526e819 VS |
84 | strcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str()); |
85 | m_Items[m_ItemsCnt].m_Book = m_Book; | |
86 | m_ItemsCnt++; | |
87 | } | |
88 | return TRUE; | |
89 | } | |
90 | ||
91 | else { // "PARAM" | |
92 | if (m_Name == wxEmptyString && tag.GetParam("NAME") == "Name") m_Name = tag.GetParam("VALUE"); | |
93 | if (tag.GetParam("NAME") == "Local") m_Page = tag.GetParam("VALUE"); | |
94 | if (tag.GetParam("NAME") == "ID") tag.ScanParam("VALUE", "%i", &m_ID); | |
95 | return FALSE; | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | ||
101 | void HP_TagHandler::WriteOut(HtmlContentsItem*& array, int& size) | |
102 | { | |
103 | array = m_Items; | |
104 | size = m_ItemsCnt; | |
105 | m_Items = NULL; | |
106 | m_ItemsCnt = 0; | |
107 | } | |
108 | ||
109 | void HP_TagHandler::ReadIn(HtmlContentsItem* array, int size) | |
110 | { | |
111 | m_Items = array; | |
112 | m_ItemsCnt = size; | |
113 | } | |
114 | ||
115 | ||
116 | ||
117 | ||
118 | void wxHtmlHelpController::LoadMSProject(HtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile, bool show_wait_msg) | |
119 | { | |
120 | wxFSFile *f; | |
121 | char *buf; | |
122 | int sz; | |
123 | wxString string; | |
124 | wxBusyInfo *busyinfo = (show_wait_msg) ? new wxBusyInfo(_("Importing help file : \n") + book -> m_Title) : NULL; | |
125 | ||
126 | HP_Parser parser; | |
127 | HP_TagHandler *handler = new HP_TagHandler(book); | |
128 | parser.AddTagHandler(handler); | |
129 | ||
130 | f = fsys.OpenFile(contentsfile); | |
131 | if (f) { | |
259d1674 | 132 | sz = f -> GetStream() -> GetSize(); |
2776d7c3 | 133 | buf = new char[sz+1]; |
5526e819 VS |
134 | buf[sz] = 0; |
135 | f -> GetStream() -> Read(buf, sz); | |
136 | delete f; | |
137 | handler -> ReadIn(m_Contents, m_ContentsCnt); | |
138 | parser.Parse(buf); | |
139 | handler -> WriteOut(m_Contents, m_ContentsCnt); | |
2776d7c3 | 140 | delete[] buf; |
5526e819 VS |
141 | } |
142 | ||
143 | f = fsys.OpenFile(indexfile); | |
144 | if (f) { | |
259d1674 | 145 | sz = f -> GetStream() -> GetSize(); |
2776d7c3 | 146 | buf = new char[sz+1]; |
5526e819 VS |
147 | buf[sz] = 0; |
148 | f -> GetStream() -> Read(buf, sz); | |
149 | delete f; | |
150 | handler -> ReadIn(m_Index, m_IndexCnt); | |
151 | parser.Parse(buf); | |
152 | handler -> WriteOut(m_Index, m_IndexCnt); | |
2776d7c3 | 153 | delete[] buf; |
5526e819 VS |
154 | } |
155 | if (show_wait_msg) delete busyinfo; | |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | ||
161 | ||
162 | ||
163 | void wxHtmlHelpController::LoadCachedBook(HtmlBookRecord *book, wxInputStream *f) | |
164 | { | |
165 | int i, st; | |
166 | int x; | |
167 | ||
168 | /* load contents : */ | |
169 | ||
170 | f -> Read(&x, sizeof(x)); | |
171 | st = m_ContentsCnt; | |
172 | m_ContentsCnt += x; | |
173 | m_Contents = (HtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(HtmlContentsItem)); | |
174 | for (i = st; i < m_ContentsCnt; i++) { | |
175 | f -> Read(&x, sizeof(x)); | |
176 | m_Contents[i].m_Level = x; | |
177 | f -> Read(&x, sizeof(x)); | |
178 | m_Contents[i].m_ID = x; | |
179 | f -> Read(&x, sizeof(x)); | |
2776d7c3 | 180 | m_Contents[i].m_Name = new char[x]; |
5526e819 VS |
181 | f -> Read(m_Contents[i].m_Name, x); |
182 | f -> Read(&x, sizeof(x)); | |
2776d7c3 | 183 | m_Contents[i].m_Page = new char[x]; |
5526e819 VS |
184 | f -> Read(m_Contents[i].m_Page, x); |
185 | m_Contents[i].m_Book = book; | |
186 | } | |
187 | ||
188 | /* load index : */ | |
189 | ||
190 | f -> Read(&x, sizeof(x)); | |
191 | st = m_IndexCnt; | |
192 | m_IndexCnt += x; | |
193 | m_Index = (HtmlContentsItem*) realloc(m_Index, (m_IndexCnt / HTML_REALLOC_STEP + 1) * HTML_REALLOC_STEP * sizeof(HtmlContentsItem)); | |
194 | for (i = st; i < m_IndexCnt; i++) { | |
195 | f -> Read(&x, sizeof(x)); | |
2776d7c3 | 196 | m_Index[i].m_Name = new char[x]; |
5526e819 VS |
197 | f -> Read(m_Index[i].m_Name, x); |
198 | f -> Read(&x, sizeof(x)); | |
2776d7c3 | 199 | m_Index[i].m_Page = new char[x]; |
5526e819 VS |
200 | f -> Read(m_Index[i].m_Page, x); |
201 | m_Index[i].m_Book = book; | |
202 | } | |
203 | } | |
204 | ||
205 | ||
206 | ||
207 | ||
208 | ||
209 | ||
210 | void wxHtmlHelpController::SaveCachedBook(HtmlBookRecord *book, wxOutputStream *f) | |
211 | { | |
212 | int i; | |
213 | int x; | |
214 | ||
215 | /* save contents : */ | |
216 | ||
217 | x = 0; | |
218 | for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++; | |
219 | f -> Write(&x, sizeof(x)); | |
220 | for (i = 0; i < m_ContentsCnt; i++) { | |
221 | if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue; | |
222 | x = m_Contents[i].m_Level; | |
223 | f -> Write(&x, sizeof(x)); | |
224 | x = m_Contents[i].m_ID; | |
225 | f -> Write(&x, sizeof(x)); | |
226 | x = strlen(m_Contents[i].m_Name) + 1; | |
227 | f -> Write(&x, sizeof(x)); | |
228 | f -> Write(m_Contents[i].m_Name, x); | |
229 | x = strlen(m_Contents[i].m_Page) + 1; | |
230 | f -> Write(&x, sizeof(x)); | |
231 | f -> Write(m_Contents[i].m_Page, x); | |
232 | } | |
233 | ||
234 | /* save index : */ | |
235 | ||
236 | x = 0; | |
237 | for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++; | |
238 | f -> Write(&x, sizeof(x)); | |
239 | for (i = 0; i < m_IndexCnt; i++) { | |
240 | if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue; | |
241 | x = strlen(m_Index[i].m_Name) + 1; | |
242 | f -> Write(&x, sizeof(x)); | |
243 | f -> Write(m_Index[i].m_Name, x); | |
244 | x = strlen(m_Index[i].m_Page) + 1; | |
245 | f -> Write(&x, sizeof(x)); | |
246 | f -> Write(m_Index[i].m_Page, x); | |
247 | } | |
248 | } | |
249 | ||
250 | #endif |