configure: Add a check for sys/ucontext.h and include it where appropriate.
[wine.git] / dlls / hhctrl.ocx / content.c
blob28bda119b0b4411066d323f4b05a3f883aed7ec2
1 /*
2 * Copyright 2007 Jacek Caban for CodeWeavers
3 * Copyright 2011 Owen Rudge for CodeWeavers
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
23 #include "hhctrl.h"
24 #include "stream.h"
25 #include "resource.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
31 typedef enum {
32 INSERT_NEXT,
33 INSERT_CHILD
34 } insert_type_t;
36 static void free_content_item(ContentItem *item)
38 ContentItem *next;
40 while(item) {
41 next = item->next;
43 free_content_item(item->child);
45 heap_free(item->name);
46 heap_free(item->local);
47 heap_free(item->merge.chm_file);
48 heap_free(item->merge.chm_index);
50 item = next;
54 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text, UINT code_page)
56 const char *ptr;
57 LPWSTR *param, merge;
58 int len;
60 ptr = get_attr(text, "name", &len);
61 if(!ptr) {
62 WARN("name attr not found\n");
63 return;
66 if(!strncasecmp("name", ptr, len)) {
67 param = &item->name;
68 }else if(!strncasecmp("merge", ptr, len)) {
69 param = &merge;
70 }else if(!strncasecmp("local", ptr, len)) {
71 param = &item->local;
72 }else {
73 WARN("unhandled param %s\n", debugstr_an(ptr, len));
74 return;
77 ptr = get_attr(text, "value", &len);
78 if(!ptr) {
79 WARN("value attr not found\n");
80 return;
84 * "merge" parameter data (referencing another CHM file) can be incorporated into the "local" parameter
85 * by specifying the filename in the format:
86 * MS-ITS:file.chm::/local_path.htm
88 if(param == &item->local && strstr(ptr, "::"))
90 const char *local = strstr(ptr, "::")+2;
91 int local_len = len-(local-ptr);
93 item->local = decode_html(local, local_len, code_page);
94 param = &merge;
97 *param = decode_html(ptr, len, code_page);
99 if(param == &merge) {
100 SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
101 heap_free(merge);
105 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
107 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
109 if(!item)
110 return new_item;
112 if(!new_item)
113 return item;
115 switch(insert_type) {
116 case INSERT_NEXT:
117 item->next = new_item;
118 return new_item;
119 case INSERT_CHILD:
120 if(item->child) {
121 ContentItem *iter = item->child;
122 while(iter->next)
123 iter = iter->next;
124 iter->next = new_item;
125 }else {
126 item->child = new_item;
128 return item;
131 return NULL;
134 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
135 insert_type_t *insert_type)
137 strbuf_t node, node_name;
138 ContentItem *item;
140 *insert_type = INSERT_NEXT;
142 strbuf_init(&node);
143 strbuf_init(&node_name);
145 item = heap_alloc_zero(sizeof(ContentItem));
147 while(next_node(stream, &node)) {
148 get_node_name(&node, &node_name);
150 TRACE("%s\n", node.buf);
152 if(!strcasecmp(node_name.buf, "/object"))
153 break;
154 if(!strcasecmp(node_name.buf, "param"))
155 parse_obj_node_param(item, hhc_root, node.buf, info->pCHMInfo->codePage);
157 strbuf_zero(&node);
160 strbuf_free(&node);
161 strbuf_free(&node_name);
163 if(item->merge.chm_index) {
164 IStream *merge_stream;
166 merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
167 if(merge_stream) {
168 item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
169 IStream_Release(merge_stream);
170 }else {
171 WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
172 debugstr_w(item->merge.chm_file));
174 if(!item->name) {
175 free_content_item(item);
176 item = NULL;
182 return item;
185 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
187 strbuf_t node, node_name;
188 ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
189 insert_type_t it;
191 strbuf_init(&node);
192 strbuf_init(&node_name);
194 while(next_node(stream, &node)) {
195 get_node_name(&node, &node_name);
197 TRACE("%s\n", node.buf);
199 if(!strcasecmp(node_name.buf, "object")) {
200 const char *ptr;
201 int len;
203 static const char sz_text_sitemap[] = "text/sitemap";
205 ptr = get_attr(node.buf, "type", &len);
207 if(ptr && len == sizeof(sz_text_sitemap)-1
208 && !memcmp(ptr, sz_text_sitemap, len)) {
209 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
210 prev = insert_item(prev, new_item, it);
211 if(!ret)
212 ret = prev;
214 }else if(!strcasecmp(node_name.buf, "ul")) {
215 new_item = parse_ul(info, stream, hhc_root);
216 insert_item(prev, new_item, INSERT_CHILD);
217 }else if(!strcasecmp(node_name.buf, "/ul")) {
218 break;
221 strbuf_zero(&node);
224 strbuf_free(&node);
225 strbuf_free(&node_name);
227 return ret;
230 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
231 insert_type_t *insert_type)
233 stream_t stream;
234 strbuf_t node, node_name;
235 ContentItem *ret = NULL, *prev = NULL;
237 *insert_type = INSERT_NEXT;
239 strbuf_init(&node);
240 strbuf_init(&node_name);
242 stream_init(&stream, str);
244 while(next_node(&stream, &node)) {
245 get_node_name(&node, &node_name);
247 TRACE("%s\n", node.buf);
249 if(!strcasecmp(node_name.buf, "ul")) {
250 ContentItem *item = parse_ul(info, &stream, hhc_root);
251 prev = insert_item(prev, item, INSERT_CHILD);
252 if(!ret)
253 ret = prev;
254 *insert_type = INSERT_CHILD;
257 strbuf_zero(&node);
260 strbuf_free(&node);
261 strbuf_free(&node_name);
263 return ret;
266 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
268 TVINSERTSTRUCTW tvis;
270 memset(&tvis, 0, sizeof(tvis));
271 tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
272 tvis.u.item.cchTextMax = strlenW(item->name)+1;
273 tvis.u.item.pszText = item->name;
274 tvis.u.item.lParam = (LPARAM)item;
275 tvis.u.item.iImage = item->child ? HHTV_FOLDER : HHTV_DOCUMENT;
276 tvis.u.item.iSelectedImage = item->child ? HHTV_FOLDER : HHTV_DOCUMENT;
277 tvis.hParent = parent ? parent->id : 0;
278 tvis.hInsertAfter = TVI_LAST;
280 item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
283 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
285 while(item) {
286 if(item->name) {
287 insert_content_item(hwnd, parent, item);
288 fill_content_tree(hwnd, item, item->child);
289 }else {
290 fill_content_tree(hwnd, parent, item->child);
292 item = item->next;
296 static void set_item_parents(ContentItem *parent, ContentItem *item)
298 while(item) {
299 item->parent = parent;
300 set_item_parents(item, item->child);
301 item = item->next;
305 void InitContent(HHInfo *info)
307 IStream *stream;
308 insert_type_t insert_type;
310 info->content = heap_alloc_zero(sizeof(ContentItem));
311 SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
313 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
314 if(!stream) {
315 TRACE("Could not get content stream\n");
316 return;
319 info->content->child = parse_hhc(info, stream, info->content, &insert_type);
320 IStream_Release(stream);
322 set_item_parents(NULL, info->content);
323 fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
326 void ReleaseContent(HHInfo *info)
328 free_content_item(info->content);
331 void ActivateContentTopic(HWND hWnd, LPCWSTR filename, ContentItem *item)
333 if (lstrcmpiW(item->local, filename) == 0)
335 SendMessageW(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM) item->id);
336 return;
339 if (item->next)
340 ActivateContentTopic(hWnd, filename, item->next);
342 if (item->child)
343 ActivateContentTopic(hWnd, filename, item->child);