This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / lib / user32 / misc / winhelp.c
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program 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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  *
21  * PROJECT:         ReactOS user32.dll
22  * FILE:            lib/user32/misc/winhelp.c
23  * PURPOSE:         WinHelp
24  * PROGRAMMER:      Robert Dickenson(robd@reactos.org)
25  * UPDATE HISTORY:
26  *      23-08-2002  RDD  Created from wine sources
27  */
28
29 /* INCLUDES ******************************************************************/
30
31 #include <string.h>
32 #include <windows.h>
33 #include <user32.h>
34 #include <debug.h>
35
36 /* WinHelp internal structure */
37 typedef struct
38 {
39     WORD size;
40     WORD command;
41     LONG data;
42     LONG reserved;
43     WORD ofsFilename;
44     WORD ofsData;
45 } WINHELP,*LPWINHELP;
46
47
48 /* FUNCTIONS *****************************************************************/
49
50 WINBOOL
51 STDCALL
52 WinHelpA(HWND hWnd, LPCSTR lpszHelp, UINT uCommand, DWORD dwData)
53 {
54         static WORD WM_WINHELP = 0;
55         HWND hDest;
56         LPWINHELP lpwh;
57         HGLOBAL hwh;
58         int size,dsize,nlen;
59
60         if (!WM_WINHELP) {
61             WM_WINHELP = RegisterWindowMessageA("WM_WINHELP");
62             if (!WM_WINHELP)
63               return FALSE;
64     }
65
66         hDest = FindWindowA("MS_WINHELP", NULL);
67         if (!hDest) {
68             if (uCommand == HELP_QUIT) return TRUE;
69         if (WinExec("winhlp32.exe -x", SW_SHOWNORMAL) < 32) {
70             //ERR("can't start winhlp32.exe -x ?\n");
71             return FALSE;
72         } 
73             if (!(hDest = FindWindowA("MS_WINHELP", NULL))) {
74                 //FIXME("did not find MS_WINHELP (FindWindow() failed, maybe global window handling still unimplemented)\n");
75                 return FALSE;
76         }
77     }
78         switch (uCommand) {
79                 case HELP_CONTEXT:
80                 case HELP_SETCONTENTS:
81                 case HELP_CONTENTS:
82                 case HELP_CONTEXTPOPUP:
83                 case HELP_FORCEFILE:
84                 case HELP_HELPONHELP:
85                 case HELP_FINDER:
86                 case HELP_QUIT:
87                         dsize=0;
88                         break;
89                 case HELP_KEY:
90                 case HELP_PARTIALKEY:
91                 case HELP_COMMAND:
92                         dsize = dwData ? strlen( (LPSTR)dwData )+1: 0;
93                         break;
94                 case HELP_MULTIKEY:
95                         dsize = ((LPMULTIKEYHELPA)dwData)->mkSize;
96                         break;
97                 case HELP_SETWINPOS:
98                         dsize = ((LPHELPWININFOA)dwData)->wStructSize;
99                         break;
100                 default:
101                         //FIXME("Unknown help command %d\n",uCommand);
102                         return FALSE;
103         }
104         if (lpszHelp)
105                 nlen = strlen(lpszHelp)+1;
106         else
107                 nlen = 0;
108         size = sizeof(WINHELP) + nlen + dsize;
109         hwh = GlobalAlloc(0,size);
110         lpwh = GlobalLock(hwh);
111         lpwh->size = size;
112         lpwh->command = uCommand;
113         lpwh->data = dwData;
114         if (nlen) {
115                 strcpy(((char*)lpwh) + sizeof(WINHELP), lpszHelp);
116                 lpwh->ofsFilename = sizeof(WINHELP);
117         } else {
118                 lpwh->ofsFilename = 0;
119         }
120         if (dsize) {
121                 memcpy(((char*)lpwh)+sizeof(WINHELP)+nlen,(LPSTR)dwData,dsize);
122                 lpwh->ofsData = sizeof(WINHELP)+nlen;
123         } else {
124                 lpwh->ofsData = 0;
125         }
126         GlobalUnlock(hwh);
127         return SendMessage(hDest, WM_WINHELP, (WPARAM)hWnd, (LPARAM)hwh);
128 }
129
130 WINBOOL
131 STDCALL
132 WinHelpW(HWND hWnd, LPCWSTR lpszHelp, UINT uCommand, DWORD dwData)
133 {
134     INT len;
135     LPSTR file;
136     BOOL ret = FALSE;
137
138     if (!lpszHelp) return WinHelpA(hWnd, NULL, uCommand, dwData);
139
140     len = WideCharToMultiByte(CP_ACP, 0, lpszHelp, -1, NULL, 0, NULL, NULL);
141     if ((file = HeapAlloc(GetProcessHeap(), 0, len))) {
142         WideCharToMultiByte(CP_ACP, 0, lpszHelp, -1, file, len, NULL, NULL);
143         ret = WinHelpA(hWnd, file, uCommand, dwData);
144         HeapFree(GetProcessHeap(), 0, file);
145     }
146     return ret;
147 }
148