2 * Copyright 2007 Jacek Caban for CodeWeavers
3 * Copyright 2010 Erich Hoover
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
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
30 /* Fill the TreeView object corresponding to the Index items */
31 static void fill_index_tree(HWND hwnd, IndexItem *item)
37 TRACE("tree debug: %s\n", debugstr_w(item->keyword));
41 FIXME("HTML Help index item has no keyword.\n");
45 memset(&lvi, 0, sizeof(lvi));
47 lvi.mask = LVIF_TEXT|LVIF_PARAM;
48 lvi.cchTextMax = strlenW(item->keyword)+1;
49 lvi.pszText = item->keyword;
50 lvi.lParam = (LPARAM)item;
51 item->id = (HTREEITEM)SendMessageW(hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
56 /* Parse the attributes correspond to a list item, including sub-topics.
58 * Each list item has, at minimum, a param of type "keyword" and two
59 * parameters corresponding to a "sub-topic." For each sub-topic there
60 * must be a "name" param and a "local" param, if there is only one
61 * sub-topic then there isn't really a sub-topic, the index will jump
62 * directly to the requested item.
64 static void parse_index_obj_node_param(IndexItem *item, const char *text)
70 ptr = get_attr(text, "name", &len);
72 WARN("name attr not found\n");
76 /* Allocate a new sub-item, either on the first run or whenever a
77 * sub-topic has filled out both the "name" and "local" params.
79 if(item->itemFlags == 0x11 && (!strncasecmp("name", ptr, len) || !strncasecmp("local", ptr, len))) {
81 item->items = heap_realloc(item->items, sizeof(IndexSubItem)*item->nItems);
82 item->items[item->nItems-1].name = NULL;
83 item->items[item->nItems-1].local = NULL;
84 item->itemFlags = 0x00;
86 if(!strncasecmp("keyword", ptr, len)) {
87 param = &item->keyword;
88 }else if(!item->keyword && !strncasecmp("name", ptr, len)) {
89 /* Some HTML Help index files use an additional "name" parameter
90 * rather than the "keyword" parameter. In this case, the first
91 * occurance of the "name" parameter is the keyword.
93 param = &item->keyword;
94 }else if(!strncasecmp("name", ptr, len)) {
95 item->itemFlags |= 0x01;
96 param = &item->items[item->nItems-1].name;
97 }else if(!strncasecmp("local", ptr, len)) {
98 item->itemFlags |= 0x10;
99 param = &item->items[item->nItems-1].local;
101 WARN("unhandled param %s\n", debugstr_an(ptr, len));
105 ptr = get_attr(text, "value", &len);
107 WARN("value attr not found\n");
111 wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
112 *param = heap_alloc((wlen+1)*sizeof(WCHAR));
113 MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
117 /* Parse the object tag corresponding to a list item.
119 * At this step we look for all of the "param" child tags, using this information
120 * to build up the information about the list item. When we reach the </object>
121 * tag we know that we've finished parsing this list item.
123 static IndexItem *parse_index_sitemap_object(HHInfo *info, stream_t *stream)
125 strbuf_t node, node_name;
129 strbuf_init(&node_name);
131 item = heap_alloc_zero(sizeof(IndexItem));
133 item->items = heap_alloc_zero(0);
134 item->itemFlags = 0x11;
136 while(next_node(stream, &node)) {
137 get_node_name(&node, &node_name);
139 TRACE("%s\n", node.buf);
141 if(!strcasecmp(node_name.buf, "param")) {
142 parse_index_obj_node_param(item, node.buf);
143 }else if(!strcasecmp(node_name.buf, "/object")) {
146 WARN("Unhandled tag! %s\n", node_name.buf);
153 strbuf_free(&node_name);
158 /* Parse the HTML list item node corresponding to a specific help entry.
160 * At this stage we look for the only child tag we expect to find under
161 * the list item: the <OBJECT> tag. We also only expect to find object
162 * tags with the "type" attribute set to "text/sitemap".
164 static IndexItem *parse_li(HHInfo *info, stream_t *stream)
166 strbuf_t node, node_name;
167 IndexItem *ret = NULL;
170 strbuf_init(&node_name);
172 while(next_node(stream, &node)) {
173 get_node_name(&node, &node_name);
175 TRACE("%s\n", node.buf);
177 if(!strcasecmp(node_name.buf, "object")) {
181 static const char sz_text_sitemap[] = "text/sitemap";
183 ptr = get_attr(node.buf, "type", &len);
185 if(ptr && len == sizeof(sz_text_sitemap)-1
186 && !memcmp(ptr, sz_text_sitemap, len)) {
187 ret = parse_index_sitemap_object(info, stream);
191 WARN("Unhandled tag! %s\n", node_name.buf);
198 strbuf_free(&node_name);
203 /* Parse the HTML Help page corresponding to all of the Index items.
205 * At this high-level stage we locate out each HTML list item tag.
206 * Since there is no end-tag for the <LI> item, we must hope that
207 * the <LI> entry is parsed correctly or tags might get lost.
209 static void parse_hhindex(HHInfo *info, IStream *str, IndexItem *item)
212 strbuf_t node, node_name;
215 strbuf_init(&node_name);
217 stream_init(&stream, str);
219 while(next_node(&stream, &node)) {
220 get_node_name(&node, &node_name);
222 TRACE("%s\n", node.buf);
224 if(!strcasecmp(node_name.buf, "li")) {
225 item->next = parse_li(info, &stream);
226 item->next->merge = item->merge;
229 WARN("Unhandled tag! %s\n", node_name.buf);
236 strbuf_free(&node_name);
239 /* Initialize the HTML Help Index tab */
240 void InitIndex(HHInfo *info)
244 info->index = heap_alloc_zero(sizeof(IndexItem));
245 info->index->nItems = 0;
246 SetChmPath(&info->index->merge, info->pCHMInfo->szFile, info->WinType.pszIndex);
248 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->index->merge);
250 TRACE("Could not get index stream\n");
254 parse_hhindex(info, stream, info->index);
255 IStream_Release(stream);
257 fill_index_tree(info->tabs[TAB_INDEX].hwnd, info->index->next);
260 /* Free all of the Index items, including all of the "sub-items" that
261 * correspond to different sub-topics.
263 void ReleaseIndex(HHInfo *info)
265 IndexItem *item = info->index, *next;
268 /* Note: item->merge is identical for all items, only free once */
269 heap_free(item->merge.chm_file);
270 heap_free(item->merge.chm_index);
274 heap_free(item->keyword);
275 for(i=0;i<item->nItems;i++) {
276 heap_free(item->items[i].name);
277 heap_free(item->items[i].local);
279 heap_free(item->items);