hhctrl.ocx: Default to the correct tab for different HH_DISPLAY_* values.
[wine] / dlls / hhctrl.ocx / content.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  * Copyright 2011 Owen Rudge for CodeWeavers
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 typedef enum {
31     INSERT_NEXT,
32     INSERT_CHILD
33 } insert_type_t;
34
35 static void free_content_item(ContentItem *item)
36 {
37     ContentItem *next;
38
39     while(item) {
40         next = item->next;
41
42         free_content_item(item->child);
43
44         heap_free(item->name);
45         heap_free(item->local);
46         heap_free(item->merge.chm_file);
47         heap_free(item->merge.chm_index);
48
49         item = next;
50     }
51 }
52
53 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text)
54 {
55     const char *ptr;
56     LPWSTR *param, merge;
57     int len, wlen;
58
59     ptr = get_attr(text, "name", &len);
60     if(!ptr) {
61         WARN("name attr not found\n");
62         return;
63     }
64
65     if(!strncasecmp("name", ptr, len)) {
66         param = &item->name;
67     }else if(!strncasecmp("merge", ptr, len)) {
68         param = &merge;
69     }else if(!strncasecmp("local", ptr, len)) {
70         param = &item->local;
71     }else {
72         WARN("unhandled param %s\n", debugstr_an(ptr, len));
73         return;
74     }
75
76     ptr = get_attr(text, "value", &len);
77     if(!ptr) {
78         WARN("value attr not found\n");
79         return;
80     }
81
82     wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
83     *param = heap_alloc((wlen+1)*sizeof(WCHAR));
84     MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
85     (*param)[wlen] = 0;
86
87     if(param == &merge) {
88         SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
89         heap_free(merge);
90     }
91 }
92
93 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
94
95 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
96 {
97     if(!item)
98         return new_item;
99
100     if(!new_item)
101         return item;
102
103     switch(insert_type) {
104     case INSERT_NEXT:
105         item->next = new_item;
106         return new_item;
107     case INSERT_CHILD:
108         if(item->child) {
109             ContentItem *iter = item->child;
110             while(iter->next)
111                 iter = iter->next;
112             iter->next = new_item;
113         }else {
114             item->child = new_item;
115         }
116         return item;
117     }
118
119     return NULL;
120 }
121
122 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
123         insert_type_t *insert_type)
124 {
125     strbuf_t node, node_name;
126     ContentItem *item;
127
128     *insert_type = INSERT_NEXT;
129
130     strbuf_init(&node);
131     strbuf_init(&node_name);
132
133     item = heap_alloc_zero(sizeof(ContentItem));
134
135     while(next_node(stream, &node)) {
136         get_node_name(&node, &node_name);
137
138         TRACE("%s\n", node.buf);
139
140         if(!strcasecmp(node_name.buf, "/object"))
141             break;
142         if(!strcasecmp(node_name.buf, "param"))
143             parse_obj_node_param(item, hhc_root, node.buf);
144
145         strbuf_zero(&node);
146     }
147
148     strbuf_free(&node);
149     strbuf_free(&node_name);
150
151     if(item->merge.chm_index) {
152         IStream *merge_stream;
153
154         merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
155         if(merge_stream) {
156             item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
157             IStream_Release(merge_stream);
158         }else {
159             WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
160                  debugstr_w(item->merge.chm_file));
161
162             if(!item->name) {
163                 free_content_item(item);
164                 item = NULL;
165             }
166         }
167
168     }
169
170     return item;
171 }
172
173 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
174 {
175     strbuf_t node, node_name;
176     ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
177     insert_type_t it;
178
179     strbuf_init(&node);
180     strbuf_init(&node_name);
181
182     while(next_node(stream, &node)) {
183         get_node_name(&node, &node_name);
184
185         TRACE("%s\n", node.buf);
186
187         if(!strcasecmp(node_name.buf, "object")) {
188             const char *ptr;
189             int len;
190
191             static const char sz_text_sitemap[] = "text/sitemap";
192
193             ptr = get_attr(node.buf, "type", &len);
194
195             if(ptr && len == sizeof(sz_text_sitemap)-1
196                && !memcmp(ptr, sz_text_sitemap, len)) {
197                 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
198                 prev = insert_item(prev, new_item, it);
199                 if(!ret)
200                     ret = prev;
201             }
202         }else if(!strcasecmp(node_name.buf, "ul")) {
203             new_item = parse_ul(info, stream, hhc_root);
204             insert_item(prev, new_item, INSERT_CHILD);
205         }else if(!strcasecmp(node_name.buf, "/ul")) {
206             break;
207         }
208
209         strbuf_zero(&node);
210     }
211
212     strbuf_free(&node);
213     strbuf_free(&node_name);
214
215     return ret;
216 }
217
218 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
219         insert_type_t *insert_type)
220 {
221     stream_t stream;
222     strbuf_t node, node_name;
223     ContentItem *ret = NULL, *prev = NULL;
224
225     *insert_type = INSERT_NEXT;
226
227     strbuf_init(&node);
228     strbuf_init(&node_name);
229
230     stream_init(&stream, str);
231
232     while(next_node(&stream, &node)) {
233         get_node_name(&node, &node_name);
234
235         TRACE("%s\n", node.buf);
236
237         if(!strcasecmp(node_name.buf, "ul")) {
238             ContentItem *item = parse_ul(info, &stream, hhc_root);
239             prev = insert_item(prev, item, INSERT_CHILD);
240             if(!ret)
241                 ret = prev;
242             *insert_type = INSERT_CHILD;
243         }
244
245         strbuf_zero(&node);
246     }
247
248     strbuf_free(&node);
249     strbuf_free(&node_name);
250
251     return ret;
252 }
253
254 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
255 {
256     TVINSERTSTRUCTW tvis;
257
258     memset(&tvis, 0, sizeof(tvis));
259     tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
260     tvis.u.item.cchTextMax = strlenW(item->name)+1;
261     tvis.u.item.pszText = item->name;
262     tvis.u.item.lParam = (LPARAM)item;
263     tvis.hParent = parent ? parent->id : 0;
264     tvis.hInsertAfter = TVI_LAST;
265
266     item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
267 }
268
269 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
270 {
271     while(item) {
272         if(item->name) {
273             insert_content_item(hwnd, parent, item);
274             fill_content_tree(hwnd, item, item->child);
275         }else {
276             fill_content_tree(hwnd, parent, item->child);
277         }
278         item = item->next;
279     }
280 }
281
282 static void set_item_parents(ContentItem *parent, ContentItem *item)
283 {
284     while(item) {
285         item->parent = parent;
286         set_item_parents(item, item->child);
287         item = item->next;
288     }
289 }
290
291 void InitContent(HHInfo *info)
292 {
293     IStream *stream;
294     insert_type_t insert_type;
295
296     info->content = heap_alloc_zero(sizeof(ContentItem));
297     SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
298
299     stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
300     if(!stream) {
301         TRACE("Could not get content stream\n");
302         return;
303     }
304
305     info->content->child = parse_hhc(info, stream, info->content, &insert_type);
306     IStream_Release(stream);
307
308     set_item_parents(NULL, info->content);
309     fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
310 }
311
312 void ReleaseContent(HHInfo *info)
313 {
314     free_content_item(info->content);
315 }
316
317 void ActivateContentTopic(HWND hWnd, LPCWSTR filename, ContentItem *item)
318 {
319     if (lstrcmpiW(item->local, filename) == 0)
320     {
321         SendMessageW(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM) item->id);
322         return;
323     }
324
325     if (item->next)
326         ActivateContentTopic(hWnd, filename, item->next);
327
328     if (item->child)
329         ActivateContentTopic(hWnd, filename, item->child);
330 }