dpnet: Show SetSP adds a component.
[wine.git] / dlls / hhctrl.ocx / hhctrl.c
blob86a31f5ac50ddeef36ad1c4ecea9d952e584f08e
1 /*
2 * hhctrl implementation
4 * Copyright 2004 Krzysztof Foltman
5 * Copyright 2007 Jacek Caban for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
24 #include <stdarg.h>
26 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "htmlhelp.h"
33 #include "ole2.h"
34 #include "rpcproxy.h"
36 #define INIT_GUID
37 #include "hhctrl.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
41 HINSTANCE hhctrl_hinstance;
42 BOOL hh_process = FALSE;
44 extern struct list window_list;
46 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
48 TRACE("(%p,%d,%p)\n", hInstance, fdwReason, lpvReserved);
50 switch (fdwReason)
52 case DLL_PROCESS_ATTACH:
53 hhctrl_hinstance = hInstance;
54 DisableThreadLibraryCalls(hInstance);
55 break;
57 return TRUE;
60 static const char *command_to_string(UINT command)
62 #define X(x) case x: return #x
63 switch (command)
65 X( HH_DISPLAY_TOPIC );
66 X( HH_DISPLAY_TOC );
67 X( HH_DISPLAY_INDEX );
68 X( HH_DISPLAY_SEARCH );
69 X( HH_SET_WIN_TYPE );
70 X( HH_GET_WIN_TYPE );
71 X( HH_GET_WIN_HANDLE );
72 X( HH_ENUM_INFO_TYPE );
73 X( HH_SET_INFO_TYPE );
74 X( HH_SYNC );
75 X( HH_RESERVED1 );
76 X( HH_RESERVED2 );
77 X( HH_RESERVED3 );
78 X( HH_KEYWORD_LOOKUP );
79 X( HH_DISPLAY_TEXT_POPUP );
80 X( HH_HELP_CONTEXT );
81 X( HH_TP_HELP_CONTEXTMENU );
82 X( HH_TP_HELP_WM_HELP );
83 X( HH_CLOSE_ALL );
84 X( HH_ALINK_LOOKUP );
85 X( HH_GET_LAST_ERROR );
86 X( HH_ENUM_CATEGORY );
87 X( HH_ENUM_CATEGORY_IT );
88 X( HH_RESET_IT_FILTER );
89 X( HH_SET_INCLUSIVE_FILTER );
90 X( HH_SET_EXCLUSIVE_FILTER );
91 X( HH_INITIALIZE );
92 X( HH_UNINITIALIZE );
93 X( HH_SAFE_DISPLAY_TOPIC );
94 X( HH_PRETRANSLATEMESSAGE );
95 X( HH_SET_GLOBAL_PROPERTY );
96 default: return "???";
98 #undef X
101 static BOOL resolve_filename(const WCHAR *filename, WCHAR *fullname, DWORD buflen, WCHAR **index, WCHAR **window)
103 const WCHAR *extra;
104 WCHAR chm_file[MAX_PATH];
106 static const WCHAR helpW[] = {'\\','h','e','l','p','\\',0};
107 static const WCHAR delimW[] = {':',':',0};
108 static const WCHAR delim2W[] = {'>',0};
110 filename = skip_schema(filename);
112 /* the format is "helpFile[::/index][>window]" */
113 if (index) *index = NULL;
114 if (window) *window = NULL;
116 extra = strstrW(filename, delim2W);
117 if (extra)
119 memcpy(chm_file, filename, (extra-filename)*sizeof(WCHAR));
120 chm_file[extra-filename] = 0;
121 filename = chm_file;
122 if (window)
123 *window = strdupW(extra+1);
126 extra = strstrW(filename, delimW);
127 if (extra)
129 if (filename != chm_file)
130 memcpy(chm_file, filename, (extra-filename)*sizeof(WCHAR));
131 chm_file[extra-filename] = 0;
132 filename = chm_file;
133 if (index)
134 *index = strdupW(extra+2);
137 GetFullPathNameW(filename, buflen, fullname, NULL);
138 if (GetFileAttributesW(fullname) == INVALID_FILE_ATTRIBUTES)
140 GetWindowsDirectoryW(fullname, buflen);
141 strcatW(fullname, helpW);
142 strcatW(fullname, filename);
144 return (GetFileAttributesW(fullname) != INVALID_FILE_ATTRIBUTES);
147 /******************************************************************
148 * HtmlHelpW (HHCTRL.OCX.15)
150 HWND WINAPI HtmlHelpW(HWND caller, LPCWSTR filename, UINT command, DWORD_PTR data)
152 WCHAR fullname[MAX_PATH];
154 TRACE("(%p, %s, command=%s, data=%lx)\n",
155 caller, debugstr_w( filename ),
156 command_to_string( command ), data);
158 switch (command)
160 case HH_DISPLAY_TOPIC:
161 case HH_DISPLAY_TOC:
162 case HH_DISPLAY_INDEX:
163 case HH_DISPLAY_SEARCH:{
164 BOOL res;
165 NMHDR nmhdr;
166 HHInfo *info = NULL;
167 WCHAR *window = NULL;
168 const WCHAR *index = NULL;
169 WCHAR *default_index = NULL;
170 int tab_index = TAB_CONTENTS;
172 if (!filename)
173 return NULL;
175 if (!resolve_filename(filename, fullname, MAX_PATH, &default_index, &window))
177 WARN("can't find %s\n", debugstr_w(filename));
178 return 0;
180 index = default_index;
182 if (window)
183 info = find_window(window);
185 info = CreateHelpViewer(info, fullname, caller);
186 if(!info)
188 heap_free(default_index);
189 heap_free(window);
190 return NULL;
193 if(!index)
194 index = info->WinType.pszFile;
195 if(!info->WinType.pszType)
196 info->WinType.pszType = info->stringsW.pszType = window;
197 else
198 heap_free(window);
200 /* called to load a specified topic */
201 switch(command)
203 case HH_DISPLAY_TOPIC:
204 case HH_DISPLAY_TOC:
205 if (data)
207 static const WCHAR delimW[] = {':',':',0};
208 const WCHAR *i = (const WCHAR *)data;
210 index = strstrW(i, delimW);
211 if(index)
213 if(memcmp(info->pCHMInfo->szFile, i, index-i))
214 FIXME("Opening a CHM file in the context of another is not supported.\n");
215 index += strlenW(delimW);
217 else
218 index = i;
220 break;
223 res = NavigateToChm(info, info->pCHMInfo->szFile, index);
224 heap_free(default_index);
226 if(!res)
228 ReleaseHelpViewer(info);
229 return NULL;
232 switch(command)
234 case HH_DISPLAY_TOPIC:
235 case HH_DISPLAY_TOC:
236 tab_index = TAB_CONTENTS;
237 break;
238 case HH_DISPLAY_INDEX:
239 tab_index = TAB_INDEX;
240 if (data)
241 FIXME("Should select keyword '%s'.\n", debugstr_w((WCHAR *)data));
242 break;
243 case HH_DISPLAY_SEARCH:
244 tab_index = TAB_SEARCH;
245 if (data)
246 FIXME("Should display search specified by HH_FTS_QUERY structure.\n");
247 break;
249 /* open the requested tab */
250 memset(&nmhdr, 0, sizeof(nmhdr));
251 nmhdr.code = TCN_SELCHANGE;
252 SendMessageW(info->hwndTabCtrl, TCM_SETCURSEL, (WPARAM)info->tabs[tab_index].id, 0);
253 SendMessageW(info->WinType.hwndNavigation, WM_NOTIFY, 0, (LPARAM)&nmhdr);
255 return info->WinType.hwndHelp;
257 case HH_HELP_CONTEXT: {
258 WCHAR *window = NULL;
259 HHInfo *info = NULL;
260 LPWSTR url;
262 if (!filename)
263 return NULL;
265 if (!resolve_filename(filename, fullname, MAX_PATH, NULL, &window))
267 WARN("can't find %s\n", debugstr_w(filename));
268 return 0;
271 if (window)
272 info = find_window(window);
274 info = CreateHelpViewer(info, fullname, caller);
275 if(!info)
277 heap_free(window);
278 return NULL;
281 if(!info->WinType.pszType)
282 info->WinType.pszType = info->stringsW.pszType = window;
283 else
284 heap_free(window);
286 url = FindContextAlias(info->pCHMInfo, data);
287 if(!url)
289 if(!data) /* there may legitimately be no context alias for id 0 */
290 return info->WinType.hwndHelp;
291 ReleaseHelpViewer(info);
292 return NULL;
295 NavigateToUrl(info, url);
296 heap_free(url);
297 return info->WinType.hwndHelp;
299 case HH_PRETRANSLATEMESSAGE: {
300 static BOOL warned = FALSE;
302 if (!warned)
304 FIXME("HH_PRETRANSLATEMESSAGE unimplemented\n");
305 warned = TRUE;
307 return 0;
309 case HH_CLOSE_ALL: {
310 HHInfo *info, *next;
312 LIST_FOR_EACH_ENTRY_SAFE(info, next, &window_list, HHInfo, entry)
314 TRACE("Destroying window %s.\n", debugstr_w(info->WinType.pszType));
315 ReleaseHelpViewer(info);
317 return 0;
319 case HH_SET_WIN_TYPE: {
320 HH_WINTYPEW *wintype = (HH_WINTYPEW *)data;
321 WCHAR *window = NULL;
322 HHInfo *info = NULL;
324 if (!filename && wintype->pszType)
325 window = strdupW(wintype->pszType);
326 else if (!filename || !resolve_filename(filename, fullname, MAX_PATH, NULL, &window) || !window)
328 WARN("can't find window name: %s\n", debugstr_w(filename));
329 return 0;
331 info = find_window(window);
332 if (!info)
334 info = heap_alloc_zero(sizeof(HHInfo));
335 info->WinType.pszType = info->stringsW.pszType = window;
336 list_add_tail(&window_list, &info->entry);
338 else
339 heap_free(window);
341 TRACE("Changing WINTYPE, fsValidMembers=0x%x\n", wintype->fsValidMembers);
343 MergeChmProperties(wintype, info, TRUE);
344 UpdateHelpWindow(info);
345 return 0;
347 case HH_GET_WIN_TYPE: {
348 HH_WINTYPEW *wintype = (HH_WINTYPEW *)data;
349 WCHAR *window = NULL;
350 HHInfo *info = NULL;
352 if (!filename || !resolve_filename(filename, fullname, MAX_PATH, NULL, &window) || !window)
354 WARN("can't find window name: %s\n", debugstr_w(filename));
355 return 0;
357 info = find_window(window);
358 if (!info)
360 WARN("Could not find window named %s.\n", debugstr_w(window));
361 heap_free(window);
362 return (HWND)~0;
365 TRACE("Retrieving WINTYPE for %s.\n", debugstr_w(window));
366 *wintype = info->WinType;
367 heap_free(window);
368 return 0;
370 default:
371 FIXME("HH case %s not handled.\n", command_to_string( command ));
374 return 0;
377 static void wintypeAtoW(const HH_WINTYPEA *data, HH_WINTYPEW *wdata, struct wintype_stringsW *stringsW)
379 memcpy(wdata, data, sizeof(*data));
380 /* convert all of the ANSI strings to Unicode */
381 wdata->pszType = stringsW->pszType = strdupAtoW(data->pszType);
382 wdata->pszCaption = stringsW->pszCaption = strdupAtoW(data->pszCaption);
383 wdata->pszToc = stringsW->pszToc = strdupAtoW(data->pszToc);
384 wdata->pszIndex = stringsW->pszIndex = strdupAtoW(data->pszIndex);
385 wdata->pszFile = stringsW->pszFile = strdupAtoW(data->pszFile);
386 wdata->pszHome = stringsW->pszHome = strdupAtoW(data->pszHome);
387 wdata->pszJump1 = stringsW->pszJump1 = strdupAtoW(data->pszJump1);
388 wdata->pszJump2 = stringsW->pszJump2 = strdupAtoW(data->pszJump2);
389 wdata->pszUrlJump1 = stringsW->pszUrlJump1 = strdupAtoW(data->pszUrlJump1);
390 wdata->pszUrlJump2 = stringsW->pszUrlJump2 = strdupAtoW(data->pszUrlJump2);
391 wdata->pszCustomTabs = stringsW->pszCustomTabs = strdupAtoW(data->pszCustomTabs);
394 static void wintypeWtoA(const HH_WINTYPEW *wdata, HH_WINTYPEA *data, struct wintype_stringsA *stringsA)
396 memcpy(data, wdata, sizeof(*wdata));
397 /* convert all of the Unicode strings to ANSI */
398 data->pszType = stringsA->pszType = strdupWtoA(wdata->pszType);
399 data->pszCaption = stringsA->pszCaption = strdupWtoA(wdata->pszCaption);
400 data->pszToc = stringsA->pszToc = strdupWtoA(wdata->pszToc);
401 data->pszIndex = stringsA->pszFile = strdupWtoA(wdata->pszIndex);
402 data->pszFile = stringsA->pszFile = strdupWtoA(wdata->pszFile);
403 data->pszHome = stringsA->pszHome = strdupWtoA(wdata->pszHome);
404 data->pszJump1 = stringsA->pszJump1 = strdupWtoA(wdata->pszJump1);
405 data->pszJump2 = stringsA->pszJump2 = strdupWtoA(wdata->pszJump2);
406 data->pszUrlJump1 = stringsA->pszUrlJump1 = strdupWtoA(wdata->pszUrlJump1);
407 data->pszUrlJump2 = stringsA->pszUrlJump2 = strdupWtoA(wdata->pszUrlJump2);
408 data->pszCustomTabs = stringsA->pszCustomTabs = strdupWtoA(wdata->pszCustomTabs);
411 /******************************************************************
412 * HtmlHelpA (HHCTRL.OCX.14)
414 HWND WINAPI HtmlHelpA(HWND caller, LPCSTR filename, UINT command, DWORD_PTR data)
416 WCHAR *wfile = strdupAtoW( filename );
417 HWND result = 0;
419 if (data)
421 switch(command)
423 case HH_ALINK_LOOKUP:
424 case HH_DISPLAY_SEARCH:
425 case HH_DISPLAY_TEXT_POPUP:
426 case HH_GET_LAST_ERROR:
427 case HH_KEYWORD_LOOKUP:
428 case HH_SYNC:
429 FIXME("structures not handled yet\n");
430 break;
432 case HH_SET_WIN_TYPE:
434 struct wintype_stringsW stringsW;
435 HH_WINTYPEW wdata;
437 wintypeAtoW((HH_WINTYPEA *)data, &wdata, &stringsW);
438 result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)&wdata );
439 wintype_stringsW_free(&stringsW);
440 goto done;
442 case HH_GET_WIN_TYPE:
444 HH_WINTYPEW wdata;
445 HHInfo *info;
447 result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)&wdata );
448 if (!wdata.pszType) break;
449 info = find_window(wdata.pszType);
450 if (!info) break;
451 wintype_stringsA_free(&info->stringsA);
452 wintypeWtoA(&wdata, (HH_WINTYPEA *)data, &info->stringsA);
453 goto done;
456 case HH_DISPLAY_INDEX:
457 case HH_DISPLAY_TOPIC:
458 case HH_DISPLAY_TOC:
459 case HH_GET_WIN_HANDLE:
460 case HH_SAFE_DISPLAY_TOPIC:
462 WCHAR *wdata = strdupAtoW( (const char *)data );
463 result = HtmlHelpW( caller, wfile, command, (DWORD_PTR)wdata );
464 heap_free(wdata);
465 goto done;
468 case HH_CLOSE_ALL:
469 case HH_HELP_CONTEXT:
470 case HH_INITIALIZE:
471 case HH_PRETRANSLATEMESSAGE:
472 case HH_TP_HELP_CONTEXTMENU:
473 case HH_TP_HELP_WM_HELP:
474 case HH_UNINITIALIZE:
475 /* either scalar or pointer to scalar - do nothing */
476 break;
478 default:
479 FIXME("Unknown command: %s (%d)\n", command_to_string(command), command);
480 break;
484 result = HtmlHelpW( caller, wfile, command, data );
485 done:
486 heap_free(wfile);
487 return result;
490 /******************************************************************
491 * doWinMain (HHCTRL.OCX.13)
493 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
495 MSG msg;
496 int len, buflen, mapid = -1;
497 WCHAR *filename;
498 char *endq = NULL;
499 HWND hwnd;
501 hh_process = TRUE;
503 /* Parse command line option of the HTML Help command.
505 * Note: The only currently handled action is "mapid",
506 * which corresponds to opening a specific page.
508 while(*szCmdLine == '-')
510 LPSTR space, ptr;
512 ptr = szCmdLine + 1;
513 space = strchr(ptr, ' ');
514 if(!strncmp(ptr, "mapid", space-ptr))
516 char idtxt[10];
518 ptr += strlen("mapid")+1;
519 space = strchr(ptr, ' ');
520 /* command line ends without number */
521 if (!space)
522 return 0;
523 memcpy(idtxt, ptr, space-ptr);
524 idtxt[space-ptr] = '\0';
525 mapid = atoi(idtxt);
526 szCmdLine = space+1;
528 else
530 FIXME("Unhandled HTML Help command line parameter! (%.*s)\n", (int)(space-szCmdLine), szCmdLine);
531 return 0;
535 /* FIXME: Check szCmdLine for bad arguments */
536 if (*szCmdLine == '\"')
537 endq = strchr(++szCmdLine, '\"');
539 if (endq)
540 len = endq - szCmdLine;
541 else
542 len = strlen(szCmdLine);
544 /* no filename given */
545 if (!len)
546 return 0;
548 buflen = MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, NULL, 0) + 1;
549 filename = heap_alloc(buflen * sizeof(WCHAR));
550 MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, filename, buflen);
551 filename[buflen-1] = 0;
553 /* Open a specific help topic */
554 if(mapid != -1)
555 hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_HELP_CONTEXT, mapid);
556 else
557 hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_DISPLAY_TOPIC, 0);
559 heap_free(filename);
561 if (!hwnd)
563 ERR("Failed to open HTML Help file '%s'.\n", szCmdLine);
564 return 0;
567 while (GetMessageW(&msg, 0, 0, 0))
569 TranslateMessage(&msg);
570 DispatchMessageW(&msg);
573 return 0;
576 /******************************************************************
577 * DllGetClassObject (HHCTRL.OCX.@)
579 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
581 FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
582 return CLASS_E_CLASSNOTAVAILABLE;
585 /***********************************************************************
586 * DllRegisterServer (HHCTRL.OCX.@)
588 HRESULT WINAPI DllRegisterServer(void)
590 return __wine_register_resources( hhctrl_hinstance );
593 /***********************************************************************
594 * DllUnregisterServer (HHCTRL.OCX.@)
596 HRESULT WINAPI DllUnregisterServer(void)
598 return __wine_unregister_resources( hhctrl_hinstance );