hhctrl.ocx: Fix HTML Help Index tab with alternative 'keyword' style.
[wine] / dlls / hhctrl.ocx / index.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  * Copyright 2010 Erich Hoover
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22
23 #include "hhctrl.h"
24 #include "stream.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
29
30 /* Fill the TreeView object corresponding to the Index items */
31 static void fill_index_tree(HWND hwnd, IndexItem *item)
32 {
33     int index = 0;
34     LVITEMW lvi;
35
36     while(item) {
37         TRACE("tree debug: %s\n", debugstr_w(item->keyword));
38
39         if(!item->keyword)
40         {
41             FIXME("HTML Help index item has no keyword.\n");
42             item = item->next;
43             continue;
44         }
45         memset(&lvi, 0, sizeof(lvi));
46         lvi.iItem = index++;
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);
52         item = item->next;
53     }
54 }
55
56 /* Parse the attributes correspond to a list item, including sub-topics.
57  *
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.
63  */
64 static void parse_index_obj_node_param(IndexItem *item, const char *text)
65 {
66     const char *ptr;
67     LPWSTR *param;
68     int len, wlen;
69
70     ptr = get_attr(text, "name", &len);
71     if(!ptr) {
72         WARN("name attr not found\n");
73         return;
74     }
75
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.
78      */
79     if(item->itemFlags == 0x11 && (!strncasecmp("name", ptr, len) || !strncasecmp("local", ptr, len))) {
80         item->nItems++;
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;
85     }
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.
92          */
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;
100     }else {
101         WARN("unhandled param %s\n", debugstr_an(ptr, len));
102         return;
103     }
104
105     ptr = get_attr(text, "value", &len);
106     if(!ptr) {
107         WARN("value attr not found\n");
108         return;
109     }
110
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);
114     (*param)[wlen] = 0;
115 }
116
117 /* Parse the object tag corresponding to a list item.
118  *
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.
122  */
123 static IndexItem *parse_index_sitemap_object(HHInfo *info, stream_t *stream)
124 {
125     strbuf_t node, node_name;
126     IndexItem *item;
127
128     strbuf_init(&node);
129     strbuf_init(&node_name);
130
131     item = heap_alloc_zero(sizeof(IndexItem));
132     item->nItems = 0;
133     item->items = heap_alloc_zero(0);
134     item->itemFlags = 0x11;
135
136     while(next_node(stream, &node)) {
137         get_node_name(&node, &node_name);
138
139         TRACE("%s\n", node.buf);
140
141         if(!strcasecmp(node_name.buf, "param")) {
142             parse_index_obj_node_param(item, node.buf);
143         }else if(!strcasecmp(node_name.buf, "/object")) {
144             break;
145         }else {
146             WARN("Unhandled tag! %s\n", node_name.buf);
147         }
148
149         strbuf_zero(&node);
150     }
151
152     strbuf_free(&node);
153     strbuf_free(&node_name);
154
155     return item;
156 }
157
158 /* Parse the HTML list item node corresponding to a specific help entry.
159  *
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".
163  */
164 static IndexItem *parse_li(HHInfo *info, stream_t *stream)
165 {
166     strbuf_t node, node_name;
167     IndexItem *ret = NULL;
168
169     strbuf_init(&node);
170     strbuf_init(&node_name);
171
172     while(next_node(stream, &node)) {
173         get_node_name(&node, &node_name);
174
175         TRACE("%s\n", node.buf);
176
177         if(!strcasecmp(node_name.buf, "object")) {
178             const char *ptr;
179             int len;
180
181             static const char sz_text_sitemap[] = "text/sitemap";
182
183             ptr = get_attr(node.buf, "type", &len);
184
185             if(ptr && len == sizeof(sz_text_sitemap)-1
186                && !memcmp(ptr, sz_text_sitemap, len)) {
187                 ret = parse_index_sitemap_object(info, stream);
188                 break;
189             }
190         }else {
191             WARN("Unhandled tag! %s\n", node_name.buf);
192         }
193
194         strbuf_zero(&node);
195     }
196
197     strbuf_free(&node);
198     strbuf_free(&node_name);
199
200     return ret;
201 }
202
203 /* Parse the HTML Help page corresponding to all of the Index items.
204  *
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.
208  */
209 static void parse_hhindex(HHInfo *info, IStream *str, IndexItem *item)
210 {
211     stream_t stream;
212     strbuf_t node, node_name;
213
214     strbuf_init(&node);
215     strbuf_init(&node_name);
216
217     stream_init(&stream, str);
218
219     while(next_node(&stream, &node)) {
220         get_node_name(&node, &node_name);
221
222         TRACE("%s\n", node.buf);
223
224         if(!strcasecmp(node_name.buf, "li")) {
225             item->next = parse_li(info, &stream);
226             item->next->merge = item->merge;
227             item = item->next;
228         }else {
229             WARN("Unhandled tag! %s\n", node_name.buf);
230         }
231
232         strbuf_zero(&node);
233     }
234
235     strbuf_free(&node);
236     strbuf_free(&node_name);
237 }
238
239 /* Initialize the HTML Help Index tab */
240 void InitIndex(HHInfo *info)
241 {
242     IStream *stream;
243
244     info->index = heap_alloc_zero(sizeof(IndexItem));
245     info->index->nItems = 0;
246     SetChmPath(&info->index->merge, info->pCHMInfo->szFile, info->WinType.pszIndex);
247
248     stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->index->merge);
249     if(!stream) {
250         TRACE("Could not get index stream\n");
251         return;
252     }
253
254     parse_hhindex(info, stream, info->index);
255     IStream_Release(stream);
256
257     fill_index_tree(info->tabs[TAB_INDEX].hwnd, info->index->next);
258 }
259
260 /* Free all of the Index items, including all of the "sub-items" that
261  * correspond to different sub-topics.
262  */
263 void ReleaseIndex(HHInfo *info)
264 {
265     IndexItem *item = info->index, *next;
266     int i;
267
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);
271     while(item) {
272         next = item->next;
273
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);
278         }
279         heap_free(item->items);
280
281         item = next;
282     }
283 }