Split out some more 16-bit code.
[wine] / windows / winhelp.c
1 /*
2  * Windows Help
3  *
4  * Copyright 1996 Martin von Loewis
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include "wine/debug.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "wownt32.h"
33 #include "wine/winuser16.h"
34 #include "wine/winbase16.h"
35 #include "win.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
38
39
40 /* WinHelp internal structure */
41 typedef struct
42 {
43     WORD size;
44     WORD command;
45     LONG data;
46     LONG reserved;
47     WORD ofsFilename;
48     WORD ofsData;
49 } WINHELP,*LPWINHELP;
50
51 /**********************************************************************
52  *              WinHelp (USER.171)
53  */
54 BOOL16 WINAPI WinHelp16( HWND16 hWnd, LPCSTR lpHelpFile, UINT16 wCommand,
55                          DWORD dwData )
56 {
57   BOOL ret;
58   DWORD mutex_count;
59
60   /* We might call WinExec() */
61   ReleaseThunkLock( &mutex_count );
62
63   if (!(ret = WinHelpA( WIN_Handle32(hWnd), lpHelpFile, wCommand, (DWORD)MapSL(dwData) )))
64   {
65       /* try to start the 16-bit winhelp */
66       if (WinExec( "winhelp.exe -x", SW_SHOWNORMAL ) >= 32)
67       {
68           K32WOWYield16();
69           ret = WinHelpA( WIN_Handle32(hWnd), lpHelpFile, wCommand, (DWORD)MapSL(dwData) );
70       }
71   }
72
73   RestoreThunkLock( mutex_count );
74   return ret;
75 }
76
77
78 /**********************************************************************
79  *              WinHelpA (USER32.@)
80  */
81 BOOL WINAPI WinHelpA( HWND hWnd, LPCSTR lpHelpFile, UINT wCommand, ULONG_PTR dwData )
82 {
83         static WORD WM_WINHELP = 0;
84         HWND hDest;
85         LPWINHELP lpwh;
86         HGLOBAL16 hwh;
87         int size,dsize,nlen;
88
89
90         if(!WM_WINHELP)
91           {
92             WM_WINHELP=RegisterWindowMessageA("WM_WINHELP");
93             if(!WM_WINHELP)
94               return FALSE;
95           }
96
97         hDest = FindWindowA( "MS_WINHELP", NULL );
98         if(!hDest) {
99           if(wCommand == HELP_QUIT) return TRUE;
100           if (WinExec ( "winhlp32.exe -x", SW_SHOWNORMAL ) < 32) {
101               ERR("can't start winhlp32.exe -x ?\n");
102               return FALSE;
103           }
104           if ( ! ( hDest = FindWindowA ( "MS_WINHELP", NULL ) )) {
105               FIXME("did not find MS_WINHELP (FindWindow() failed, maybe global window handling still unimplemented)\n");
106               return FALSE;
107           }
108         }
109
110
111         switch(wCommand)
112         {
113                 case HELP_CONTEXT:
114                 case HELP_SETCONTENTS:
115                 case HELP_CONTENTS:
116                 case HELP_CONTEXTPOPUP:
117                 case HELP_FORCEFILE:
118                 case HELP_HELPONHELP:
119                 case HELP_FINDER:
120                 case HELP_QUIT:
121                         dsize=0;
122                         break;
123                 case HELP_KEY:
124                 case HELP_PARTIALKEY:
125                 case HELP_COMMAND:
126                         dsize = dwData ? strlen( (LPSTR)dwData )+1: 0;
127                         break;
128                 case HELP_MULTIKEY:
129                         dsize = ((LPMULTIKEYHELPA)dwData)->mkSize;
130                         break;
131                 case HELP_SETWINPOS:
132                         dsize = ((LPHELPWININFOA)dwData)->wStructSize;
133                         break;
134                 default:
135                         FIXME("Unknown help command %d\n",wCommand);
136                         return FALSE;
137         }
138         if(lpHelpFile)
139                 nlen = strlen(lpHelpFile)+1;
140         else
141                 nlen = 0;
142         size = sizeof(WINHELP) + nlen + dsize;
143         hwh = GlobalAlloc16(0,size);
144         lpwh = GlobalLock16(hwh);
145         lpwh->size = size;
146         lpwh->command = wCommand;
147         lpwh->data = dwData;
148         if(nlen) {
149                 strcpy(((char*)lpwh) + sizeof(WINHELP),lpHelpFile);
150                 lpwh->ofsFilename = sizeof(WINHELP);
151         } else
152                 lpwh->ofsFilename = 0;
153         if(dsize) {
154                 memcpy(((char*)lpwh)+sizeof(WINHELP)+nlen,(LPSTR)dwData,dsize);
155                 lpwh->ofsData = sizeof(WINHELP)+nlen;
156         } else
157                 lpwh->ofsData = 0;
158         GlobalUnlock16(hwh);
159         return SendMessage16(HWND_16(hDest),WM_WINHELP,HWND_16(hWnd),hwh);
160 }
161
162
163 /**********************************************************************
164  *              WinHelpW (USER32.@)
165  */
166 BOOL WINAPI WinHelpW( HWND hWnd, LPCWSTR helpFile, UINT command, ULONG_PTR dwData )
167 {
168     INT len;
169     LPSTR file;
170     BOOL ret = FALSE;
171
172     if (!helpFile) return WinHelpA( hWnd, NULL, command, dwData );
173
174     len = WideCharToMultiByte( CP_ACP, 0, helpFile, -1, NULL, 0, NULL, NULL );
175     if ((file = HeapAlloc( GetProcessHeap(), 0, len )))
176     {
177         WideCharToMultiByte( CP_ACP, 0, helpFile, -1, file, len, NULL, NULL );
178         ret = WinHelpA( hWnd, file, command, dwData );
179         HeapFree( GetProcessHeap(), 0, file );
180     }
181     return ret;
182 }