2 * Copyright 2007 Jacek Caban for CodeWeavers
3 * Copyright 2011 Owen Rudge for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 static void free_content_item(ContentItem
*item
)
34 free_content_item(item
->child
);
36 heap_free(item
->name
);
37 heap_free(item
->local
);
38 heap_free(item
->merge
.chm_file
);
39 heap_free(item
->merge
.chm_index
);
45 static void parse_obj_node_param(ContentItem
*item
, ContentItem
*hhc_root
, const char *text
, UINT code_page
)
51 ptr
= get_attr(text
, "name", &len
);
53 WARN("name attr not found\n");
57 if(!strncasecmp("name", ptr
, len
)) {
59 }else if(!strncasecmp("merge", ptr
, len
)) {
61 }else if(!strncasecmp("local", ptr
, len
)) {
64 WARN("unhandled param %s\n", debugstr_an(ptr
, len
));
68 ptr
= get_attr(text
, "value", &len
);
70 WARN("value attr not found\n");
75 * "merge" parameter data (referencing another CHM file) can be incorporated into the "local" parameter
76 * by specifying the filename in the format:
77 * MS-ITS:file.chm::/local_path.htm
79 if(param
== &item
->local
&& strstr(ptr
, "::"))
81 const char *local
= strstr(ptr
, "::")+2;
82 int local_len
= len
-(local
-ptr
);
84 item
->local
= decode_html(local
, local_len
, code_page
);
88 *param
= decode_html(ptr
, len
, code_page
);
91 SetChmPath(&item
->merge
, hhc_root
->merge
.chm_file
, merge
);
96 static ContentItem
*parse_hhc(HHInfo
*,IStream
*,ContentItem
*,insert_type_t
*);
98 static ContentItem
*insert_item(ContentItem
*item
, ContentItem
*new_item
, insert_type_t insert_type
)
106 switch(insert_type
) {
108 item
->next
= new_item
;
112 ContentItem
*iter
= item
->child
;
115 iter
->next
= new_item
;
117 item
->child
= new_item
;
125 static ContentItem
*parse_sitemap_object(HHInfo
*info
, stream_t
*stream
, ContentItem
*hhc_root
,
126 insert_type_t
*insert_type
)
128 strbuf_t node
, node_name
;
131 *insert_type
= INSERT_NEXT
;
134 strbuf_init(&node_name
);
136 item
= heap_alloc_zero(sizeof(ContentItem
));
138 while(next_node(stream
, &node
)) {
139 get_node_name(&node
, &node_name
);
141 TRACE("%s\n", node
.buf
);
143 if(!strcasecmp(node_name
.buf
, "/object"))
145 if(!strcasecmp(node_name
.buf
, "param"))
146 parse_obj_node_param(item
, hhc_root
, node
.buf
, info
->pCHMInfo
->codePage
);
152 strbuf_free(&node_name
);
154 if(item
->merge
.chm_index
) {
155 IStream
*merge_stream
;
157 merge_stream
= GetChmStream(info
->pCHMInfo
, item
->merge
.chm_file
, &item
->merge
);
159 item
->child
= parse_hhc(info
, merge_stream
, hhc_root
, insert_type
);
160 IStream_Release(merge_stream
);
162 WARN("Could not get %s::%s stream\n", debugstr_w(item
->merge
.chm_file
),
163 debugstr_w(item
->merge
.chm_file
));
166 free_content_item(item
);
176 static ContentItem
*parse_ul(HHInfo
*info
, stream_t
*stream
, ContentItem
*hhc_root
)
178 strbuf_t node
, node_name
;
179 ContentItem
*ret
= NULL
, *prev
= NULL
, *new_item
= NULL
;
183 strbuf_init(&node_name
);
185 while(next_node(stream
, &node
)) {
186 get_node_name(&node
, &node_name
);
188 TRACE("%s\n", node
.buf
);
190 if(!strcasecmp(node_name
.buf
, "object")) {
194 static const char sz_text_sitemap
[] = "text/sitemap";
196 ptr
= get_attr(node
.buf
, "type", &len
);
198 if(ptr
&& len
== sizeof(sz_text_sitemap
)-1
199 && !memcmp(ptr
, sz_text_sitemap
, len
)) {
200 new_item
= parse_sitemap_object(info
, stream
, hhc_root
, &it
);
201 prev
= insert_item(prev
, new_item
, it
);
205 }else if(!strcasecmp(node_name
.buf
, "ul")) {
206 new_item
= parse_ul(info
, stream
, hhc_root
);
207 insert_item(prev
, new_item
, INSERT_CHILD
);
208 }else if(!strcasecmp(node_name
.buf
, "/ul")) {
216 strbuf_free(&node_name
);
221 static ContentItem
*parse_hhc(HHInfo
*info
, IStream
*str
, ContentItem
*hhc_root
,
222 insert_type_t
*insert_type
)
225 strbuf_t node
, node_name
;
226 ContentItem
*ret
= NULL
, *prev
= NULL
;
228 *insert_type
= INSERT_NEXT
;
231 strbuf_init(&node_name
);
233 stream_init(&stream
, str
);
235 while(next_node(&stream
, &node
)) {
236 get_node_name(&node
, &node_name
);
238 TRACE("%s\n", node
.buf
);
240 if(!strcasecmp(node_name
.buf
, "ul")) {
241 ContentItem
*item
= parse_ul(info
, &stream
, hhc_root
);
242 prev
= insert_item(prev
, item
, INSERT_CHILD
);
245 *insert_type
= INSERT_CHILD
;
252 strbuf_free(&node_name
);
257 static void insert_content_item(HWND hwnd
, ContentItem
*parent
, ContentItem
*item
)
259 TVINSERTSTRUCTW tvis
;
261 memset(&tvis
, 0, sizeof(tvis
));
262 tvis
.u
.item
.mask
= TVIF_TEXT
|TVIF_PARAM
|TVIF_IMAGE
|TVIF_SELECTEDIMAGE
;
263 tvis
.u
.item
.cchTextMax
= strlenW(item
->name
)+1;
264 tvis
.u
.item
.pszText
= item
->name
;
265 tvis
.u
.item
.lParam
= (LPARAM
)item
;
266 tvis
.u
.item
.iImage
= item
->child
? HHTV_FOLDER
: HHTV_DOCUMENT
;
267 tvis
.u
.item
.iSelectedImage
= item
->child
? HHTV_FOLDER
: HHTV_DOCUMENT
;
268 tvis
.hParent
= parent
? parent
->id
: 0;
269 tvis
.hInsertAfter
= TVI_LAST
;
271 item
->id
= (HTREEITEM
)SendMessageW(hwnd
, TVM_INSERTITEMW
, 0, (LPARAM
)&tvis
);
274 static void fill_content_tree(HWND hwnd
, ContentItem
*parent
, ContentItem
*item
)
278 insert_content_item(hwnd
, parent
, item
);
279 fill_content_tree(hwnd
, item
, item
->child
);
281 fill_content_tree(hwnd
, parent
, item
->child
);
287 static void set_item_parents(ContentItem
*parent
, ContentItem
*item
)
290 item
->parent
= parent
;
291 set_item_parents(item
, item
->child
);
296 void InitContent(HHInfo
*info
)
299 insert_type_t insert_type
;
301 info
->content
= heap_alloc_zero(sizeof(ContentItem
));
302 SetChmPath(&info
->content
->merge
, info
->pCHMInfo
->szFile
, info
->WinType
.pszToc
);
304 stream
= GetChmStream(info
->pCHMInfo
, info
->pCHMInfo
->szFile
, &info
->content
->merge
);
306 TRACE("Could not get content stream\n");
310 info
->content
->child
= parse_hhc(info
, stream
, info
->content
, &insert_type
);
311 IStream_Release(stream
);
313 set_item_parents(NULL
, info
->content
);
314 fill_content_tree(info
->tabs
[TAB_CONTENTS
].hwnd
, NULL
, info
->content
);
317 void ReleaseContent(HHInfo
*info
)
319 free_content_item(info
->content
);
322 void ActivateContentTopic(HWND hWnd
, LPCWSTR filename
, ContentItem
*item
)
324 if (lstrcmpiW(item
->local
, filename
) == 0)
326 SendMessageW(hWnd
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
) item
->id
);
331 ActivateContentTopic(hWnd
, filename
, item
->next
);
334 ActivateContentTopic(hWnd
, filename
, item
->child
);