Fixed possible endless loop.
[wine] / windows / winhelp.c
1 /*
2  * Windows Help
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "debug.h"
10 #include "wine/winuser16.h"
11 #include "wine/winbase16.h"
12 #include "heap.h"
13 #include "ldt.h"
14
15
16 /**********************************************************************
17  *             WinHelp16   (USER.171)
18  */
19 BOOL16 WINAPI WinHelp16( HWND16 hWnd, LPCSTR lpHelpFile, UINT16 wCommand,
20                          DWORD dwData )
21 {
22     return WinHelpA( hWnd, lpHelpFile, wCommand,
23                        (DWORD)PTR_SEG_TO_LIN(dwData) );
24 }
25
26
27 /**********************************************************************
28  *             WinHelp32A   (USER32.579)
29  */
30 BOOL WINAPI WinHelpA( HWND hWnd, LPCSTR lpHelpFile, UINT wCommand,
31                           DWORD dwData )
32 {
33         static WORD WM_WINHELP = 0;
34         HWND hDest;
35         LPWINHELP lpwh;
36         HGLOBAL16 hwh;
37         int size,dsize,nlen;
38         if (wCommand != HELP_QUIT)  /* FIXME */
39         {
40             if (WinExec("winhelp.exe -x",SW_SHOWNORMAL) <= 32)
41                 return FALSE;
42
43             /* NOTE: Probably, this should be directed yield, 
44                      to let winhelp open the window in all cases. */
45             Yield16();
46         }
47
48         if(!WM_WINHELP) 
49         {
50                 WM_WINHELP=RegisterWindowMessageA("WM_WINHELP");
51                 if(!WM_WINHELP)
52                         return FALSE;
53         }
54
55         hDest = FindWindowA( "MS_WINHELP", NULL );
56         if(!hDest)
57         {
58                 if(wCommand == HELP_QUIT)
59                         return TRUE;
60                 else
61                         return FALSE;
62         }
63         switch(wCommand)
64         {
65                 case HELP_CONTEXT:
66                 case HELP_SETCONTENTS:
67                 case HELP_CONTENTS:
68                 case HELP_CONTEXTPOPUP:
69                 case HELP_FORCEFILE:
70                 case HELP_HELPONHELP:
71                 case HELP_FINDER:
72                 case HELP_QUIT:
73                         dsize=0;
74                         break;
75                 case HELP_KEY:
76                 case HELP_PARTIALKEY:
77                 case HELP_COMMAND:
78                         dsize = strlen( (LPSTR)dwData )+1;
79                         break;
80                 case HELP_MULTIKEY:
81                         dsize = ((LPMULTIKEYHELP)dwData)->mkSize;
82                         break;
83                 case HELP_SETWINPOS:
84                         dsize = ((LPHELPWININFO)dwData)->wStructSize;
85                         break;
86                 default:
87                         WARN( win,"Unknown help command %d\n",wCommand);
88                         return FALSE;
89         }
90         if(lpHelpFile)
91                 nlen = strlen(lpHelpFile)+1;
92         else
93                 nlen = 0;
94         size = sizeof(WINHELP) + nlen + dsize;
95         hwh = GlobalAlloc16(0,size);
96         lpwh = GlobalLock16(hwh);
97         lpwh->size = size;
98         lpwh->command = wCommand;
99         lpwh->data = dwData;
100         if(nlen) {
101                 strcpy(((char*)lpwh) + sizeof(WINHELP),lpHelpFile);
102                 lpwh->ofsFilename = sizeof(WINHELP);
103         } else
104                 lpwh->ofsFilename = 0;
105         if(dsize) {
106                 memcpy(((char*)lpwh)+sizeof(WINHELP)+nlen,(LPSTR)dwData,dsize);
107                 lpwh->ofsData = sizeof(WINHELP)+nlen;
108         } else
109                 lpwh->ofsData = 0;
110         GlobalUnlock16(hwh);
111         return SendMessage16(hDest,WM_WINHELP,hWnd,hwh);
112 }
113
114
115 /**********************************************************************
116  *             WinHelp32W   (USER32.580)
117  */
118 BOOL WINAPI WinHelpW( HWND hWnd, LPCWSTR helpFile, UINT command,
119                           DWORD dwData )
120 {
121     LPSTR file = HEAP_strdupWtoA( GetProcessHeap(), 0, helpFile );
122     BOOL ret = WinHelpA( hWnd, file, command, dwData );
123     HeapFree( GetProcessHeap(), 0, file );
124     return ret;
125 }