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|LVIF_INDENT;
48 lvi.iIndent = item->indentLevel;
49 lvi.cchTextMax = strlenW(item->keyword)+1;
50 lvi.pszText = item->keyword;
51 lvi.lParam = (LPARAM)item;
52 item->id = (HTREEITEM)SendMessageW(hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
57 /* Parse the attributes correspond to a list item, including sub-topics.
59 * Each list item has, at minimum, a param of type "keyword" and two
60 * parameters corresponding to a "sub-topic." For each sub-topic there
61 * must be a "name" param and a "local" param, if there is only one
62 * sub-topic then there isn't really a sub-topic, the index will jump
63 * directly to the requested item.
65 static void parse_index_obj_node_param(IndexItem *item, const char *text, UINT code_page)
71 ptr = get_attr(text, "name", &len);
73 WARN("name attr not found\n");
77 /* Allocate a new sub-item, either on the first run or whenever a
78 * sub-topic has filled out both the "name" and "local" params.
80 if(item->itemFlags == 0x11 && (!strncasecmp("name", ptr, len) || !strncasecmp("local", ptr, len))) {
82 item->items = heap_realloc(item->items, sizeof(IndexSubItem)*item->nItems);
83 item->items[item->nItems-1].name = NULL;
84 item->items[item->nItems-1].local = NULL;
85 item->itemFlags = 0x00;
87 if(!strncasecmp("keyword", ptr, len)) {
88 param = &item->keyword;
89 }else if(!item->keyword && !strncasecmp("name", ptr, len)) {
90 /* Some HTML Help index files use an additional "name" parameter
91 * rather than the "keyword" parameter. In this case, the first
92 * occurrence of the "name" parameter is the keyword.
94 param = &item->keyword;
95 }else if(!strncasecmp("name", ptr, len)) {
96 item->itemFlags |= 0x01;
97 param = &item->items[item->nItems-1].name;
98 }else if(!strncasecmp("local", ptr, len)) {
99 item->itemFlags |= 0x10;
100 param = &item->items[item->nItems-1].local;
102 WARN("unhandled param %s\n", debugstr_an(ptr, len));
106 ptr = get_attr(text, "value", &len);
108 WARN("value attr not found\n");
112 *param = decode_html(ptr, len, code_page);
115 /* Parse the object tag corresponding to a list item.
117 * At this step we look for all of the "param" child tags, using this information
118 * to build up the information about the list item. When we reach the </object>
119 * tag we know that we've finished parsing this list item.
121 static IndexItem *parse_index_sitemap_object(HHInfo *info, stream_t *stream)
123 strbuf_t node, node_name;
127 strbuf_init(&node_name);
129 item = heap_alloc_zero(sizeof(IndexItem));
131 item->items = heap_alloc_zero(0);
132 item->itemFlags = 0x11;
134 while(next_node(stream, &node)) {
135 get_node_name(&node, &node_name);
137 TRACE("%s\n", node.buf);
139 if(!strcasecmp(node_name.buf, "param")) {
140 parse_index_obj_node_param(item, node.buf, info->pCHMInfo->codePage);
141 }else if(!strcasecmp(node_name.buf, "/object")) {
144 WARN("Unhandled tag! %s\n", node_name.buf);
151 strbuf_free(&node_name);
156 /* Parse the HTML list item node corresponding to a specific help entry.
158 * At this stage we look for the only child tag we expect to find under
159 * the list item: the <OBJECT> tag. We also only expect to find object
160 * tags with the "type" attribute set to "text/sitemap".
162 static IndexItem *parse_li(HHInfo *info, stream_t *stream)
164 strbuf_t node, node_name;
165 IndexItem *ret = NULL;
168 strbuf_init(&node_name);
170 while(next_node(stream, &node)) {
171 get_node_name(&node, &node_name);
173 TRACE("%s\n", node.buf);
175 if(!strcasecmp(node_name.buf, "object")) {
179 static const char sz_text_sitemap[] = "text/sitemap";
181 ptr = get_attr(node.buf, "type", &len);
183 if(ptr && len == sizeof(sz_text_sitemap)-1
184 && !memcmp(ptr, sz_text_sitemap, len)) {
185 ret = parse_index_sitemap_object(info, stream);
189 WARN("Unhandled tag! %s\n", node_name.buf);
196 strbuf_free(&node_name);
201 /* Parse the HTML Help page corresponding to all of the Index items.
203 * At this high-level stage we locate out each HTML list item tag.
204 * Since there is no end-tag for the <LI> item, we must hope that
205 * the <LI> entry is parsed correctly or tags might get lost.
207 * Within each entry it is also possible to encounter an additional
208 * <UL> tag. When this occurs the tag indicates that the topics
209 * contained within it are related to the parent <LI> topic and
210 * should be inset by an indent.
212 static void parse_hhindex(HHInfo *info, IStream *str, IndexItem *item)
215 strbuf_t node, node_name;
216 int indent_level = -1;
219 strbuf_init(&node_name);
221 stream_init(&stream, str);
223 while(next_node(&stream, &node)) {
224 get_node_name(&node, &node_name);
226 TRACE("%s\n", node.buf);
228 if(!strcasecmp(node_name.buf, "li")) {
229 item->next = parse_li(info, &stream);
230 item->next->merge = item->merge;
232 item->indentLevel = indent_level;
233 }else if(!strcasecmp(node_name.buf, "ul")) {
235 }else if(!strcasecmp(node_name.buf, "/ul")) {
238 WARN("Unhandled tag! %s\n", node_name.buf);
245 strbuf_free(&node_name);
248 /* Initialize the HTML Help Index tab */
249 void InitIndex(HHInfo *info)
253 info->index = heap_alloc_zero(sizeof(IndexItem));
254 info->index->nItems = 0;
255 SetChmPath(&info->index->merge, info->pCHMInfo->szFile, info->WinType.pszIndex);
257 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->index->merge);
259 TRACE("Could not get index stream\n");
263 parse_hhindex(info, stream, info->index);
264 IStream_Release(stream);
266 fill_index_tree(info->tabs[TAB_INDEX].hwnd, info->index->next);
269 /* Free all of the Index items, including all of the "sub-items" that
270 * correspond to different sub-topics.
272 void ReleaseIndex(HHInfo *info)
274 IndexItem *item = info->index, *next;
278 /* Note: item->merge is identical for all items, only free once */
279 heap_free(item->merge.chm_file);
280 heap_free(item->merge.chm_index);
284 heap_free(item->keyword);
285 for(i=0;i<item->nItems;i++) {
286 heap_free(item->items[i].name);
287 heap_free(item->items[i].local);
289 heap_free(item->items);