hhctrl.ocx: Allow ListView navigation with the Return/Enter key.
[wine] / dlls / hhctrl.ocx / content.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
21
22 #include "hhctrl.h"
23 #include "stream.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
28
29 typedef enum {
30     INSERT_NEXT,
31     INSERT_CHILD
32 } insert_type_t;
33
34 static void free_content_item(ContentItem *item)
35 {
36     ContentItem *next;
37
38     while(item) {
39         next = item->next;
40
41         free_content_item(item->child);
42
43         heap_free(item->name);
44         heap_free(item->local);
45         heap_free(item->merge.chm_file);
46         heap_free(item->merge.chm_index);
47
48         item = next;
49     }
50 }
51
52 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text)
53 {
54     const char *ptr;
55     LPWSTR *param, merge;
56     int len, wlen;
57
58     ptr = get_attr(text, "name", &len);
59     if(!ptr) {
60         WARN("name attr not found\n");
61         return;
62     }
63
64     if(!strncasecmp("name", ptr, len)) {
65         param = &item->name;
66     }else if(!strncasecmp("merge", ptr, len)) {
67         param = &merge;
68     }else if(!strncasecmp("local", ptr, len)) {
69         param = &item->local;
70     }else {
71         WARN("unhandled param %s\n", debugstr_an(ptr, len));
72         return;
73     }
74
75     ptr = get_attr(text, "value", &len);
76     if(!ptr) {
77         WARN("value attr not found\n");
78         return;
79     }
80
81     wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
82     *param = heap_alloc((wlen+1)*sizeof(WCHAR));
83     MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
84     (*param)[wlen] = 0;
85
86     if(param == &merge) {
87         SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
88         heap_free(merge);
89     }
90 }
91
92 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
93
94 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
95 {
96     if(!item)
97         return new_item;
98
99     if(!new_item)
100         return item;
101
102     switch(insert_type) {
103     case INSERT_NEXT:
104         item->next = new_item;
105         return new_item;
106     case INSERT_CHILD:
107         if(item->child) {
108             ContentItem *iter = item->child;
109             while(iter->next)
110                 iter = iter->next;
111             iter->next = new_item;
112         }else {
113             item->child = new_item;
114         }
115         return item;
116     }
117
118     return NULL;
119 }
120
121 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
122         insert_type_t *insert_type)
123 {
124     strbuf_t node, node_name;
125     ContentItem *item;
126
127     *insert_type = INSERT_NEXT;
128
129     strbuf_init(&node);
130     strbuf_init(&node_name);
131
132     item = heap_alloc_zero(sizeof(ContentItem));
133
134     while(next_node(stream, &node)) {
135         get_node_name(&node, &node_name);
136
137         TRACE("%s\n", node.buf);
138
139         if(!strcasecmp(node_name.buf, "/object"))
140             break;
141         if(!strcasecmp(node_name.buf, "param"))
142             parse_obj_node_param(item, hhc_root, node.buf);
143
144         strbuf_zero(&node);
145     }
146
147     strbuf_free(&node);
148     strbuf_free(&node_name);
149
150     if(item->merge.chm_index) {
151         IStream *merge_stream;
152
153         merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
154         if(merge_stream) {
155             item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
156             IStream_Release(merge_stream);
157         }else {
158             WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
159                  debugstr_w(item->merge.chm_file));
160
161             if(!item->name) {
162                 free_content_item(item);
163                 item = NULL;
164             }
165         }
166
167     }
168
169     return item;
170 }
171
172 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
173 {
174     strbuf_t node, node_name;
175     ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
176     insert_type_t it;
177
178     strbuf_init(&node);
179     strbuf_init(&node_name);
180
181     while(next_node(stream, &node)) {
182         get_node_name(&node, &node_name);
183
184         TRACE("%s\n", node.buf);
185
186         if(!strcasecmp(node_name.buf, "object")) {
187             const char *ptr;
188             int len;
189
190             static const char sz_text_sitemap[] = "text/sitemap";
191
192             ptr = get_attr(node.buf, "type", &len);
193
194             if(ptr && len == sizeof(sz_text_sitemap)-1
195                && !memcmp(ptr, sz_text_sitemap, len)) {
196                 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
197                 prev = insert_item(prev, new_item, it);
198                 if(!ret)
199                     ret = prev;
200             }
201         }else if(!strcasecmp(node_name.buf, "ul")) {
202             new_item = parse_ul(info, stream, hhc_root);
203             insert_item(prev, new_item, INSERT_CHILD);
204         }else if(!strcasecmp(node_name.buf, "/ul")) {
205             break;
206         }
207
208         strbuf_zero(&node);
209     }
210
211     strbuf_free(&node);
212     strbuf_free(&node_name);
213
214     return ret;
215 }
216
217 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
218         insert_type_t *insert_type)
219 {
220     stream_t stream;
221     strbuf_t node, node_name;
222     ContentItem *ret = NULL, *prev = NULL;
223
224     *insert_type = INSERT_NEXT;
225
226     strbuf_init(&node);
227     strbuf_init(&node_name);
228
229     stream_init(&stream, str);
230
231     while(next_node(&stream, &node)) {
232         get_node_name(&node, &node_name);
233
234         TRACE("%s\n", node.buf);
235
236         if(!strcasecmp(node_name.buf, "ul")) {
237             ContentItem *item = parse_ul(info, &stream, hhc_root);
238             prev = insert_item(prev, item, INSERT_CHILD);
239             if(!ret)
240                 ret = prev;
241             *insert_type = INSERT_CHILD;
242         }
243
244         strbuf_zero(&node);
245     }
246
247     strbuf_free(&node);
248     strbuf_free(&node_name);
249
250     return ret;
251 }
252
253 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
254 {
255     TVINSERTSTRUCTW tvis;
256
257     memset(&tvis, 0, sizeof(tvis));
258     tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
259     tvis.u.item.cchTextMax = strlenW(item->name)+1;
260     tvis.u.item.pszText = item->name;
261     tvis.u.item.lParam = (LPARAM)item;
262     tvis.hParent = parent ? parent->id : 0;
263     tvis.hInsertAfter = TVI_LAST;
264
265     item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
266 }
267
268 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
269 {
270     while(item) {
271         if(item->name) {
272             insert_content_item(hwnd, parent, item);
273             fill_content_tree(hwnd, item, item->child);
274         }else {
275             fill_content_tree(hwnd, parent, item->child);
276         }
277         item = item->next;
278     }
279 }
280
281 static void set_item_parents(ContentItem *parent, ContentItem *item)
282 {
283     while(item) {
284         item->parent = parent;
285         set_item_parents(item, item->child);
286         item = item->next;
287     }
288 }
289
290 void InitContent(HHInfo *info)
291 {
292     IStream *stream;
293     insert_type_t insert_type;
294
295     info->content = heap_alloc_zero(sizeof(ContentItem));
296     SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
297
298     stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
299     if(!stream) {
300         TRACE("Could not get content stream\n");
301         return;
302     }
303
304     info->content->child = parse_hhc(info, stream, info->content, &insert_type);
305     IStream_Release(stream);
306
307     set_item_parents(NULL, info->content);
308     fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
309 }
310
311 void ReleaseContent(HHInfo *info)
312 {
313     free_content_item(info->content);
314 }