spoolss/tests: Change skip() to win_skip().
[wine] / dlls / hhctrl.ocx / hhctrl.c
1 /*
2  * hhctrl implementation
3  *
4  * Copyright 2004 Krzysztof Foltman
5  * Copyright 2007 Jacek Caban for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "wine/debug.h"
23
24 #include <stdarg.h>
25
26 #define COBJMACROS
27
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"
35
36 #define INIT_GUID
37 #include "hhctrl.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
40
41 HINSTANCE hhctrl_hinstance;
42 BOOL hh_process = FALSE;
43
44 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
45 {
46     TRACE("(%p,%d,%p)\n", hInstance, fdwReason, lpvReserved);
47
48     switch (fdwReason)
49     {
50     case DLL_PROCESS_ATTACH:
51         hhctrl_hinstance = hInstance;
52         DisableThreadLibraryCalls(hInstance);
53         break;
54     case DLL_PROCESS_DETACH:
55         break;
56     }
57     return TRUE;
58 }
59
60 static const char *command_to_string(UINT command)
61 {
62 #define X(x) case x: return #x
63     switch (command)
64     {
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 "???";
97     }
98 #undef X
99 }
100
101 static BOOL resolve_filename(const WCHAR *filename, WCHAR *fullname, DWORD buflen)
102 {
103     static const WCHAR helpW[] = {'\\','h','e','l','p','\\',0};
104
105     GetFullPathNameW(filename, buflen, fullname, NULL);
106     if (GetFileAttributesW(fullname) == INVALID_FILE_ATTRIBUTES)
107     {
108         GetWindowsDirectoryW(fullname, buflen);
109         strcatW(fullname, helpW);
110         strcatW(fullname, filename);
111     }
112     return (GetFileAttributesW(fullname) != INVALID_FILE_ATTRIBUTES);
113 }
114
115 /******************************************************************
116  *              HtmlHelpW (HHCTRL.OCX.15)
117  */
118 HWND WINAPI HtmlHelpW(HWND caller, LPCWSTR filename, UINT command, DWORD_PTR data)
119 {
120     WCHAR fullname[MAX_PATH];
121
122     TRACE("(%p, %s, command=%s, data=%lx)\n",
123           caller, debugstr_w( filename ),
124           command_to_string( command ), data);
125
126     switch (command)
127     {
128     case HH_DISPLAY_TOPIC:
129     case HH_DISPLAY_TOC:
130     case HH_DISPLAY_SEARCH:{
131         static const WCHAR delimW[] = {':',':',0};
132         HHInfo *info;
133         BOOL res;
134         WCHAR chm_file[MAX_PATH];
135         const WCHAR *index;
136
137         FIXME("Not all HH cases handled correctly\n");
138
139         if (!filename)
140             return NULL;
141
142         index = strstrW(filename, delimW);
143         if (index)
144         {
145             memcpy(chm_file, filename, (index-filename)*sizeof(WCHAR));
146             chm_file[index-filename] = 0;
147             filename = chm_file;
148             index += 2; /* advance beyond "::" for calling NavigateToChm() later */
149         }
150
151         if (!resolve_filename(filename, fullname, MAX_PATH))
152         {
153             WARN("can't find %s\n", debugstr_w(filename));
154             return 0;
155         }
156
157         info = CreateHelpViewer(fullname);
158         if(!info)
159             return NULL;
160
161         if(!index)
162             index = info->WinType.pszFile;
163
164         res = NavigateToChm(info, info->pCHMInfo->szFile, index);
165         if(!res)
166         {
167             ReleaseHelpViewer(info);
168             return NULL;
169         }
170         return info->WinType.hwndHelp;
171     }
172     case HH_HELP_CONTEXT: {
173         HHInfo *info;
174         LPWSTR url;
175
176         if (!filename)
177             return NULL;
178
179         if (!resolve_filename(filename, fullname, MAX_PATH))
180         {
181             WARN("can't find %s\n", debugstr_w(filename));
182             return 0;
183         }
184
185         info = CreateHelpViewer(fullname);
186         if(!info)
187             return NULL;
188
189         url = FindContextAlias(info->pCHMInfo, data);
190         if(!url)
191         {
192             ReleaseHelpViewer(info);
193             return NULL;
194         }
195
196         NavigateToUrl(info, url);
197         heap_free(url);
198         return info->WinType.hwndHelp;
199     }
200     case HH_PRETRANSLATEMESSAGE: {
201         static BOOL warned = FALSE;
202
203         if (!warned)
204         {
205             FIXME("HH_PRETRANSLATEMESSAGE unimplemented\n");
206             warned = TRUE;
207         }
208         return 0;
209     }
210     default:
211         FIXME("HH case %s not handled.\n", command_to_string( command ));
212     }
213
214     return 0;
215 }
216
217 /******************************************************************
218  *              HtmlHelpA (HHCTRL.OCX.14)
219  */
220 HWND WINAPI HtmlHelpA(HWND caller, LPCSTR filename, UINT command, DWORD_PTR data)
221 {
222     WCHAR *wfile = NULL, *wdata = NULL;
223     DWORD len;
224     HWND result;
225
226     if (filename)
227     {
228         len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
229         wfile = heap_alloc(len*sizeof(WCHAR));
230         MultiByteToWideChar( CP_ACP, 0, filename, -1, wfile, len );
231     }
232
233     if (data)
234     {
235         switch(command)
236         {
237         case HH_ALINK_LOOKUP:
238         case HH_DISPLAY_SEARCH:
239         case HH_DISPLAY_TEXT_POPUP:
240         case HH_GET_LAST_ERROR:
241         case HH_GET_WIN_TYPE:
242         case HH_KEYWORD_LOOKUP:
243         case HH_SET_WIN_TYPE:
244         case HH_SYNC:
245             FIXME("structures not handled yet\n");
246             break;
247
248         case HH_DISPLAY_INDEX:
249         case HH_DISPLAY_TOPIC:
250         case HH_DISPLAY_TOC:
251         case HH_GET_WIN_HANDLE:
252         case HH_SAFE_DISPLAY_TOPIC:
253             len = MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, NULL, 0 );
254             wdata = heap_alloc(len*sizeof(WCHAR));
255             MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, wdata, len );
256             break;
257
258         case HH_CLOSE_ALL:
259         case HH_HELP_CONTEXT:
260         case HH_INITIALIZE:
261         case HH_PRETRANSLATEMESSAGE:
262         case HH_TP_HELP_CONTEXTMENU:
263         case HH_TP_HELP_WM_HELP:
264         case HH_UNINITIALIZE:
265             /* either scalar or pointer to scalar - do nothing */
266             break;
267
268         default:
269             FIXME("Unknown command: %s (%d)\n", command_to_string(command), command);
270             break;
271         }
272     }
273
274     result = HtmlHelpW( caller, wfile, command, wdata ? (DWORD_PTR)wdata : data );
275
276     heap_free(wfile);
277     heap_free(wdata);
278     return result;
279 }
280
281 /******************************************************************
282  *              doWinMain (HHCTRL.OCX.13)
283  */
284 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
285 {
286     MSG msg;
287     int len, buflen, mapid = -1;
288     WCHAR *filename;
289     char *endq = NULL;
290
291     hh_process = TRUE;
292
293     /* Parse command line option of the HTML Help command.
294      *
295      * Note: The only currently handled action is "mapid",
296      *  which corresponds to opening a specific page.
297      */
298     while(*szCmdLine == '-')
299     {
300         LPSTR space, ptr;
301
302         ptr = szCmdLine + 1;
303         space = strchr(ptr, ' ');
304         if(!strncmp(ptr, "mapid", space-ptr))
305         {
306             char idtxt[10];
307
308             ptr += strlen("mapid")+1;
309             space = strchr(ptr, ' ');
310             /* command line ends without number */
311             if (!space)
312                 return 0;
313             memcpy(idtxt, ptr, space-ptr);
314             idtxt[space-ptr] = '\0';
315             mapid = atoi(idtxt);
316             szCmdLine = space+1;
317         }
318         else
319         {
320             FIXME("Unhandled HTML Help command line parameter! (%.*s)\n", (int)(space-szCmdLine), szCmdLine);
321             return 0;
322         }
323     }
324
325     /* FIXME: Check szCmdLine for bad arguments */
326     if (*szCmdLine == '\"')
327         endq = strchr(++szCmdLine, '\"');
328
329     if (endq)
330         len = endq - szCmdLine;
331     else
332         len = strlen(szCmdLine);
333
334     /* no filename given */
335     if (!len)
336         return 0;
337
338     buflen = MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, NULL, 0) + 1;
339     filename = heap_alloc(buflen * sizeof(WCHAR));
340     MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, filename, buflen);
341     filename[buflen-1] = 0;
342
343     /* Open a specific help topic */
344     if(mapid != -1)
345         HtmlHelpW(GetDesktopWindow(), filename, HH_HELP_CONTEXT, mapid);
346     else
347         HtmlHelpW(GetDesktopWindow(), filename, HH_DISPLAY_TOPIC, 0);
348
349     heap_free(filename);
350
351     while (GetMessageW(&msg, 0, 0, 0))
352     {
353         TranslateMessage(&msg);
354         DispatchMessageW(&msg);
355     }
356
357     return 0;
358 }
359
360 /******************************************************************
361  *              DllGetClassObject (HHCTRL.OCX.@)
362  */
363 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
364 {
365     FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
366     return CLASS_E_CLASSNOTAVAILABLE;
367 }
368
369 /***********************************************************************
370  *              DllRegisterServer (HHCTRL.OCX.@)
371  */
372 HRESULT WINAPI DllRegisterServer(void)
373 {
374     return __wine_register_resources( hhctrl_hinstance, NULL );
375 }
376
377 /***********************************************************************
378  *              DllUnregisterServer (HHCTRL.OCX.@)
379  */
380 HRESULT WINAPI DllUnregisterServer(void)
381 {
382     return __wine_unregister_resources( hhctrl_hinstance, NULL );
383 }