Release 1.4-rc5.
[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, const WCHAR **index, const WCHAR **window)
102 {
103     const WCHAR *extra;
104     WCHAR chm_file[MAX_PATH];
105
106     static const WCHAR helpW[] = {'\\','h','e','l','p','\\',0};
107     static const WCHAR delimW[] = {':',':',0};
108     static const WCHAR delim2W[] = {'>',0};
109
110     filename = skip_schema(filename);
111
112     /* the format is "helpFile[::/index][>window]" */
113     if (index) *index = NULL;
114     if (window) *window = NULL;
115
116     extra = strstrW(filename, delim2W);
117     if (extra)
118     {
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);
124     }
125
126     extra = strstrW(filename, delimW);
127     if (extra)
128     {
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);
135     }
136
137     GetFullPathNameW(filename, buflen, fullname, NULL);
138     if (GetFileAttributesW(fullname) == INVALID_FILE_ATTRIBUTES)
139     {
140         GetWindowsDirectoryW(fullname, buflen);
141         strcatW(fullname, helpW);
142         strcatW(fullname, filename);
143     }
144     return (GetFileAttributesW(fullname) != INVALID_FILE_ATTRIBUTES);
145 }
146
147 /******************************************************************
148  *              HtmlHelpW (HHCTRL.OCX.15)
149  */
150 HWND WINAPI HtmlHelpW(HWND caller, LPCWSTR filename, UINT command, DWORD_PTR data)
151 {
152     WCHAR fullname[MAX_PATH];
153
154     TRACE("(%p, %s, command=%s, data=%lx)\n",
155           caller, debugstr_w( filename ),
156           command_to_string( command ), data);
157
158     switch (command)
159     {
160     case HH_DISPLAY_TOPIC:
161     case HH_DISPLAY_TOC:
162     case HH_DISPLAY_INDEX:
163     case HH_DISPLAY_SEARCH:{
164         HHInfo *info;
165         BOOL res;
166         NMHDR nmhdr;
167         const WCHAR *index = NULL;
168         int tab_index = TAB_CONTENTS;
169         const WCHAR *default_index = NULL;
170
171         if (!filename)
172             return NULL;
173
174         if (!resolve_filename(filename, fullname, MAX_PATH, &default_index, NULL))
175         {
176             WARN("can't find %s\n", debugstr_w(filename));
177             return 0;
178         }
179         index = default_index;
180
181         info = CreateHelpViewer(fullname);
182         if(!info)
183             return NULL;
184
185         if(!index)
186             index = info->WinType.pszFile;
187
188         /* called to load a specified topic */
189         switch(command)
190         {
191         case HH_DISPLAY_TOPIC:
192         case HH_DISPLAY_TOC:
193             if (data)
194                 index = (const WCHAR *)data;
195             break;
196         }
197
198         res = NavigateToChm(info, info->pCHMInfo->szFile, index);
199
200         if (default_index)
201             heap_free((WCHAR*)default_index);
202
203         if(!res)
204         {
205             ReleaseHelpViewer(info);
206             return NULL;
207         }
208
209         switch(command)
210         {
211         case HH_DISPLAY_TOPIC:
212         case HH_DISPLAY_TOC:
213             tab_index = TAB_CONTENTS;
214             break;
215         case HH_DISPLAY_INDEX:
216             tab_index = TAB_INDEX;
217             if (data)
218                 FIXME("Should select keyword '%s'.\n", debugstr_w((WCHAR *)data));
219             break;
220         case HH_DISPLAY_SEARCH:
221             tab_index = TAB_SEARCH;
222             if (data)
223                 FIXME("Should display search specified by HH_FTS_QUERY structure.\n");
224             break;
225         }
226         /* open the requested tab */
227         memset(&nmhdr, 0, sizeof(nmhdr));
228         nmhdr.code = TCN_SELCHANGE;
229         SendMessageW(info->hwndTabCtrl, TCM_SETCURSEL, (WPARAM)info->tabs[tab_index].id, 0);
230         SendMessageW(info->WinType.hwndNavigation, WM_NOTIFY, 0, (LPARAM)&nmhdr);
231
232         return info->WinType.hwndHelp;
233     }
234     case HH_HELP_CONTEXT: {
235         HHInfo *info;
236         LPWSTR url;
237
238         if (!filename)
239             return NULL;
240
241         if (!resolve_filename(filename, fullname, MAX_PATH, NULL, NULL))
242         {
243             WARN("can't find %s\n", debugstr_w(filename));
244             return 0;
245         }
246
247         info = CreateHelpViewer(fullname);
248         if(!info)
249             return NULL;
250
251         url = FindContextAlias(info->pCHMInfo, data);
252         if(!url)
253         {
254             ReleaseHelpViewer(info);
255             return NULL;
256         }
257
258         NavigateToUrl(info, url);
259         heap_free(url);
260         return info->WinType.hwndHelp;
261     }
262     case HH_PRETRANSLATEMESSAGE: {
263         static BOOL warned = FALSE;
264
265         if (!warned)
266         {
267             FIXME("HH_PRETRANSLATEMESSAGE unimplemented\n");
268             warned = TRUE;
269         }
270         return 0;
271     }
272     default:
273         FIXME("HH case %s not handled.\n", command_to_string( command ));
274     }
275
276     return 0;
277 }
278
279 /******************************************************************
280  *              HtmlHelpA (HHCTRL.OCX.14)
281  */
282 HWND WINAPI HtmlHelpA(HWND caller, LPCSTR filename, UINT command, DWORD_PTR data)
283 {
284     WCHAR *wfile = NULL, *wdata = NULL;
285     DWORD len;
286     HWND result;
287
288     if (filename)
289     {
290         len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
291         wfile = heap_alloc(len*sizeof(WCHAR));
292         MultiByteToWideChar( CP_ACP, 0, filename, -1, wfile, len );
293     }
294
295     if (data)
296     {
297         switch(command)
298         {
299         case HH_ALINK_LOOKUP:
300         case HH_DISPLAY_SEARCH:
301         case HH_DISPLAY_TEXT_POPUP:
302         case HH_GET_LAST_ERROR:
303         case HH_GET_WIN_TYPE:
304         case HH_KEYWORD_LOOKUP:
305         case HH_SET_WIN_TYPE:
306         case HH_SYNC:
307             FIXME("structures not handled yet\n");
308             break;
309
310         case HH_DISPLAY_INDEX:
311         case HH_DISPLAY_TOPIC:
312         case HH_DISPLAY_TOC:
313         case HH_GET_WIN_HANDLE:
314         case HH_SAFE_DISPLAY_TOPIC:
315             len = MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, NULL, 0 );
316             wdata = heap_alloc(len*sizeof(WCHAR));
317             MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, wdata, len );
318             break;
319
320         case HH_CLOSE_ALL:
321         case HH_HELP_CONTEXT:
322         case HH_INITIALIZE:
323         case HH_PRETRANSLATEMESSAGE:
324         case HH_TP_HELP_CONTEXTMENU:
325         case HH_TP_HELP_WM_HELP:
326         case HH_UNINITIALIZE:
327             /* either scalar or pointer to scalar - do nothing */
328             break;
329
330         default:
331             FIXME("Unknown command: %s (%d)\n", command_to_string(command), command);
332             break;
333         }
334     }
335
336     result = HtmlHelpW( caller, wfile, command, wdata ? (DWORD_PTR)wdata : data );
337
338     heap_free(wfile);
339     heap_free(wdata);
340     return result;
341 }
342
343 /******************************************************************
344  *              doWinMain (HHCTRL.OCX.13)
345  */
346 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
347 {
348     MSG msg;
349     int len, buflen, mapid = -1;
350     WCHAR *filename;
351     char *endq = NULL;
352     HWND hwnd;
353
354     hh_process = TRUE;
355
356     /* Parse command line option of the HTML Help command.
357      *
358      * Note: The only currently handled action is "mapid",
359      *  which corresponds to opening a specific page.
360      */
361     while(*szCmdLine == '-')
362     {
363         LPSTR space, ptr;
364
365         ptr = szCmdLine + 1;
366         space = strchr(ptr, ' ');
367         if(!strncmp(ptr, "mapid", space-ptr))
368         {
369             char idtxt[10];
370
371             ptr += strlen("mapid")+1;
372             space = strchr(ptr, ' ');
373             /* command line ends without number */
374             if (!space)
375                 return 0;
376             memcpy(idtxt, ptr, space-ptr);
377             idtxt[space-ptr] = '\0';
378             mapid = atoi(idtxt);
379             szCmdLine = space+1;
380         }
381         else
382         {
383             FIXME("Unhandled HTML Help command line parameter! (%.*s)\n", (int)(space-szCmdLine), szCmdLine);
384             return 0;
385         }
386     }
387
388     /* FIXME: Check szCmdLine for bad arguments */
389     if (*szCmdLine == '\"')
390         endq = strchr(++szCmdLine, '\"');
391
392     if (endq)
393         len = endq - szCmdLine;
394     else
395         len = strlen(szCmdLine);
396
397     /* no filename given */
398     if (!len)
399         return 0;
400
401     buflen = MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, NULL, 0) + 1;
402     filename = heap_alloc(buflen * sizeof(WCHAR));
403     MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, filename, buflen);
404     filename[buflen-1] = 0;
405
406     /* Open a specific help topic */
407     if(mapid != -1)
408         hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_HELP_CONTEXT, mapid);
409     else
410         hwnd = HtmlHelpW(GetDesktopWindow(), filename, HH_DISPLAY_TOPIC, 0);
411
412     heap_free(filename);
413
414     if (!hwnd)
415     {
416         ERR("Failed to open HTML Help file '%s'.\n", szCmdLine);
417         return 0;
418     }
419
420     while (GetMessageW(&msg, 0, 0, 0))
421     {
422         TranslateMessage(&msg);
423         DispatchMessageW(&msg);
424     }
425
426     return 0;
427 }
428
429 /******************************************************************
430  *              DllGetClassObject (HHCTRL.OCX.@)
431  */
432 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
433 {
434     FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
435     return CLASS_E_CLASSNOTAVAILABLE;
436 }
437
438 /***********************************************************************
439  *              DllRegisterServer (HHCTRL.OCX.@)
440  */
441 HRESULT WINAPI DllRegisterServer(void)
442 {
443     return __wine_register_resources( hhctrl_hinstance );
444 }
445
446 /***********************************************************************
447  *              DllUnregisterServer (HHCTRL.OCX.@)
448  */
449 HRESULT WINAPI DllUnregisterServer(void)
450 {
451     return __wine_unregister_resources( hhctrl_hinstance );
452 }