vbscript: Added more equality expressions parser/compiler implementation.
[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_SEARCH:{
163         HHInfo *info;
164         BOOL res;
165         const WCHAR *index = NULL;
166
167         FIXME("Not all HH cases handled correctly\n");
168
169         if (!filename)
170             return NULL;
171
172         if (!resolve_filename(filename, fullname, MAX_PATH, &index, NULL))
173         {
174             WARN("can't find %s\n", debugstr_w(filename));
175             return 0;
176         }
177
178         info = CreateHelpViewer(fullname);
179         if(!info)
180             return NULL;
181
182         if(!index)
183             index = info->WinType.pszFile;
184
185         res = NavigateToChm(info, info->pCHMInfo->szFile, index);
186
187         if (index != info->WinType.pszFile)
188             heap_free((WCHAR*)index);
189
190         if(!res)
191         {
192             ReleaseHelpViewer(info);
193             return NULL;
194         }
195         return info->WinType.hwndHelp;
196     }
197     case HH_HELP_CONTEXT: {
198         HHInfo *info;
199         LPWSTR url;
200
201         if (!filename)
202             return NULL;
203
204         if (!resolve_filename(filename, fullname, MAX_PATH, NULL, NULL))
205         {
206             WARN("can't find %s\n", debugstr_w(filename));
207             return 0;
208         }
209
210         info = CreateHelpViewer(fullname);
211         if(!info)
212             return NULL;
213
214         url = FindContextAlias(info->pCHMInfo, data);
215         if(!url)
216         {
217             ReleaseHelpViewer(info);
218             return NULL;
219         }
220
221         NavigateToUrl(info, url);
222         heap_free(url);
223         return info->WinType.hwndHelp;
224     }
225     case HH_PRETRANSLATEMESSAGE: {
226         static BOOL warned = FALSE;
227
228         if (!warned)
229         {
230             FIXME("HH_PRETRANSLATEMESSAGE unimplemented\n");
231             warned = TRUE;
232         }
233         return 0;
234     }
235     default:
236         FIXME("HH case %s not handled.\n", command_to_string( command ));
237     }
238
239     return 0;
240 }
241
242 /******************************************************************
243  *              HtmlHelpA (HHCTRL.OCX.14)
244  */
245 HWND WINAPI HtmlHelpA(HWND caller, LPCSTR filename, UINT command, DWORD_PTR data)
246 {
247     WCHAR *wfile = NULL, *wdata = NULL;
248     DWORD len;
249     HWND result;
250
251     if (filename)
252     {
253         len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
254         wfile = heap_alloc(len*sizeof(WCHAR));
255         MultiByteToWideChar( CP_ACP, 0, filename, -1, wfile, len );
256     }
257
258     if (data)
259     {
260         switch(command)
261         {
262         case HH_ALINK_LOOKUP:
263         case HH_DISPLAY_SEARCH:
264         case HH_DISPLAY_TEXT_POPUP:
265         case HH_GET_LAST_ERROR:
266         case HH_GET_WIN_TYPE:
267         case HH_KEYWORD_LOOKUP:
268         case HH_SET_WIN_TYPE:
269         case HH_SYNC:
270             FIXME("structures not handled yet\n");
271             break;
272
273         case HH_DISPLAY_INDEX:
274         case HH_DISPLAY_TOPIC:
275         case HH_DISPLAY_TOC:
276         case HH_GET_WIN_HANDLE:
277         case HH_SAFE_DISPLAY_TOPIC:
278             len = MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, NULL, 0 );
279             wdata = heap_alloc(len*sizeof(WCHAR));
280             MultiByteToWideChar( CP_ACP, 0, (const char*)data, -1, wdata, len );
281             break;
282
283         case HH_CLOSE_ALL:
284         case HH_HELP_CONTEXT:
285         case HH_INITIALIZE:
286         case HH_PRETRANSLATEMESSAGE:
287         case HH_TP_HELP_CONTEXTMENU:
288         case HH_TP_HELP_WM_HELP:
289         case HH_UNINITIALIZE:
290             /* either scalar or pointer to scalar - do nothing */
291             break;
292
293         default:
294             FIXME("Unknown command: %s (%d)\n", command_to_string(command), command);
295             break;
296         }
297     }
298
299     result = HtmlHelpW( caller, wfile, command, wdata ? (DWORD_PTR)wdata : data );
300
301     heap_free(wfile);
302     heap_free(wdata);
303     return result;
304 }
305
306 /******************************************************************
307  *              doWinMain (HHCTRL.OCX.13)
308  */
309 int WINAPI doWinMain(HINSTANCE hInstance, LPSTR szCmdLine)
310 {
311     MSG msg;
312     int len, buflen, mapid = -1;
313     WCHAR *filename;
314     char *endq = NULL;
315
316     hh_process = TRUE;
317
318     /* Parse command line option of the HTML Help command.
319      *
320      * Note: The only currently handled action is "mapid",
321      *  which corresponds to opening a specific page.
322      */
323     while(*szCmdLine == '-')
324     {
325         LPSTR space, ptr;
326
327         ptr = szCmdLine + 1;
328         space = strchr(ptr, ' ');
329         if(!strncmp(ptr, "mapid", space-ptr))
330         {
331             char idtxt[10];
332
333             ptr += strlen("mapid")+1;
334             space = strchr(ptr, ' ');
335             /* command line ends without number */
336             if (!space)
337                 return 0;
338             memcpy(idtxt, ptr, space-ptr);
339             idtxt[space-ptr] = '\0';
340             mapid = atoi(idtxt);
341             szCmdLine = space+1;
342         }
343         else
344         {
345             FIXME("Unhandled HTML Help command line parameter! (%.*s)\n", (int)(space-szCmdLine), szCmdLine);
346             return 0;
347         }
348     }
349
350     /* FIXME: Check szCmdLine for bad arguments */
351     if (*szCmdLine == '\"')
352         endq = strchr(++szCmdLine, '\"');
353
354     if (endq)
355         len = endq - szCmdLine;
356     else
357         len = strlen(szCmdLine);
358
359     /* no filename given */
360     if (!len)
361         return 0;
362
363     buflen = MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, NULL, 0) + 1;
364     filename = heap_alloc(buflen * sizeof(WCHAR));
365     MultiByteToWideChar(CP_ACP, 0, szCmdLine, len, filename, buflen);
366     filename[buflen-1] = 0;
367
368     /* Open a specific help topic */
369     if(mapid != -1)
370         HtmlHelpW(GetDesktopWindow(), filename, HH_HELP_CONTEXT, mapid);
371     else
372         HtmlHelpW(GetDesktopWindow(), filename, HH_DISPLAY_TOPIC, 0);
373
374     heap_free(filename);
375
376     while (GetMessageW(&msg, 0, 0, 0))
377     {
378         TranslateMessage(&msg);
379         DispatchMessageW(&msg);
380     }
381
382     return 0;
383 }
384
385 /******************************************************************
386  *              DllGetClassObject (HHCTRL.OCX.@)
387  */
388 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
389 {
390     FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
391     return CLASS_E_CLASSNOTAVAILABLE;
392 }
393
394 /***********************************************************************
395  *              DllRegisterServer (HHCTRL.OCX.@)
396  */
397 HRESULT WINAPI DllRegisterServer(void)
398 {
399     return __wine_register_resources( hhctrl_hinstance );
400 }
401
402 /***********************************************************************
403  *              DllUnregisterServer (HHCTRL.OCX.@)
404  */
405 HRESULT WINAPI DllUnregisterServer(void)
406 {
407     return __wine_unregister_resources( hhctrl_hinstance );
408 }