d3dxof: Make SaveTemplates stub return DXFILE_OK.
[wine/multimedia.git] / dlls / hhctrl.ocx / content.c
blob9f468a28ce69bd7034229ff6a4d74b8c3c08357b
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"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
30 typedef enum {
31 INSERT_NEXT,
32 INSERT_CHILD
33 } insert_type_t;
35 static void free_content_item(ContentItem *item)
37 ContentItem *next;
39 while(item) {
40 next = item->next;
42 free_content_item(item->child);
44 heap_free(item->name);
45 heap_free(item->local);
46 heap_free(item->merge.chm_file);
47 heap_free(item->merge.chm_index);
49 item = next;
53 static void store_param(LPWSTR *param, const char *value, int len)
55 int wlen;
57 wlen = MultiByteToWideChar(CP_ACP, 0, value, len, NULL, 0);
58 *param = heap_alloc((wlen+1)*sizeof(WCHAR));
59 MultiByteToWideChar(CP_ACP, 0, value, len, *param, wlen);
60 (*param)[wlen] = 0;
63 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text)
65 const char *ptr;
66 LPWSTR *param, merge;
67 int len;
69 ptr = get_attr(text, "name", &len);
70 if(!ptr) {
71 WARN("name attr not found\n");
72 return;
75 if(!strncasecmp("name", ptr, len)) {
76 param = &item->name;
77 }else if(!strncasecmp("merge", ptr, len)) {
78 param = &merge;
79 }else if(!strncasecmp("local", ptr, len)) {
80 param = &item->local;
81 }else {
82 WARN("unhandled param %s\n", debugstr_an(ptr, len));
83 return;
86 ptr = get_attr(text, "value", &len);
87 if(!ptr) {
88 WARN("value attr not found\n");
89 return;
93 * "merge" parameter data (referencing another CHM file) can be incorporated into the "local" parameter
94 * by specifying the filename in the format:
95 * MS-ITS:file.chm::/local_path.htm
97 if(param == &item->local && strstr(ptr, "::"))
99 const char *local = strstr(ptr, "::")+2;
100 int local_len = len-(local-ptr);
102 store_param(&item->local, local, local_len);
103 param = &merge;
106 store_param(param, ptr, len);
108 if(param == &merge) {
109 SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
110 heap_free(merge);
114 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
116 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
118 if(!item)
119 return new_item;
121 if(!new_item)
122 return item;
124 switch(insert_type) {
125 case INSERT_NEXT:
126 item->next = new_item;
127 return new_item;
128 case INSERT_CHILD:
129 if(item->child) {
130 ContentItem *iter = item->child;
131 while(iter->next)
132 iter = iter->next;
133 iter->next = new_item;
134 }else {
135 item->child = new_item;
137 return item;
140 return NULL;
143 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
144 insert_type_t *insert_type)
146 strbuf_t node, node_name;
147 ContentItem *item;
149 *insert_type = INSERT_NEXT;
151 strbuf_init(&node);
152 strbuf_init(&node_name);
154 item = heap_alloc_zero(sizeof(ContentItem));
156 while(next_node(stream, &node)) {
157 get_node_name(&node, &node_name);
159 TRACE("%s\n", node.buf);
161 if(!strcasecmp(node_name.buf, "/object"))
162 break;
163 if(!strcasecmp(node_name.buf, "param"))
164 parse_obj_node_param(item, hhc_root, node.buf);
166 strbuf_zero(&node);
169 strbuf_free(&node);
170 strbuf_free(&node_name);
172 if(item->merge.chm_index) {
173 IStream *merge_stream;
175 merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
176 if(merge_stream) {
177 item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
178 IStream_Release(merge_stream);
179 }else {
180 WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
181 debugstr_w(item->merge.chm_file));
183 if(!item->name) {
184 free_content_item(item);
185 item = NULL;
191 return item;
194 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
196 strbuf_t node, node_name;
197 ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
198 insert_type_t it;
200 strbuf_init(&node);
201 strbuf_init(&node_name);
203 while(next_node(stream, &node)) {
204 get_node_name(&node, &node_name);
206 TRACE("%s\n", node.buf);
208 if(!strcasecmp(node_name.buf, "object")) {
209 const char *ptr;
210 int len;
212 static const char sz_text_sitemap[] = "text/sitemap";
214 ptr = get_attr(node.buf, "type", &len);
216 if(ptr && len == sizeof(sz_text_sitemap)-1
217 && !memcmp(ptr, sz_text_sitemap, len)) {
218 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
219 prev = insert_item(prev, new_item, it);
220 if(!ret)
221 ret = prev;
223 }else if(!strcasecmp(node_name.buf, "ul")) {
224 new_item = parse_ul(info, stream, hhc_root);
225 insert_item(prev, new_item, INSERT_CHILD);
226 }else if(!strcasecmp(node_name.buf, "/ul")) {
227 break;
230 strbuf_zero(&node);
233 strbuf_free(&node);
234 strbuf_free(&node_name);
236 return ret;
239 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
240 insert_type_t *insert_type)
242 stream_t stream;
243 strbuf_t node, node_name;
244 ContentItem *ret = NULL, *prev = NULL;
246 *insert_type = INSERT_NEXT;
248 strbuf_init(&node);
249 strbuf_init(&node_name);
251 stream_init(&stream, str);
253 while(next_node(&stream, &node)) {
254 get_node_name(&node, &node_name);
256 TRACE("%s\n", node.buf);
258 if(!strcasecmp(node_name.buf, "ul")) {
259 ContentItem *item = parse_ul(info, &stream, hhc_root);
260 prev = insert_item(prev, item, INSERT_CHILD);
261 if(!ret)
262 ret = prev;
263 *insert_type = INSERT_CHILD;
266 strbuf_zero(&node);
269 strbuf_free(&node);
270 strbuf_free(&node_name);
272 return ret;
275 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
277 TVINSERTSTRUCTW tvis;
279 memset(&tvis, 0, sizeof(tvis));
280 tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
281 tvis.u.item.cchTextMax = strlenW(item->name)+1;
282 tvis.u.item.pszText = item->name;
283 tvis.u.item.lParam = (LPARAM)item;
284 tvis.hParent = parent ? parent->id : 0;
285 tvis.hInsertAfter = TVI_LAST;
287 item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
290 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
292 while(item) {
293 if(item->name) {
294 insert_content_item(hwnd, parent, item);
295 fill_content_tree(hwnd, item, item->child);
296 }else {
297 fill_content_tree(hwnd, parent, item->child);
299 item = item->next;
303 static void set_item_parents(ContentItem *parent, ContentItem *item)
305 while(item) {
306 item->parent = parent;
307 set_item_parents(item, item->child);
308 item = item->next;
312 void InitContent(HHInfo *info)
314 IStream *stream;
315 insert_type_t insert_type;
317 info->content = heap_alloc_zero(sizeof(ContentItem));
318 SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
320 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
321 if(!stream) {
322 TRACE("Could not get content stream\n");
323 return;
326 info->content->child = parse_hhc(info, stream, info->content, &insert_type);
327 IStream_Release(stream);
329 set_item_parents(NULL, info->content);
330 fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
333 void ReleaseContent(HHInfo *info)
335 free_content_item(info->content);
338 void ActivateContentTopic(HWND hWnd, LPCWSTR filename, ContentItem *item)
340 if (lstrcmpiW(item->local, filename) == 0)
342 SendMessageW(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM) item->id);
343 return;
346 if (item->next)
347 ActivateContentTopic(hWnd, filename, item->next);
349 if (item->child)
350 ActivateContentTopic(hWnd, filename, item->child);