Test some more ntdll types.
[wine] / windows / winproc.c
1 /*
2  * Window procedure callbacks
3  *
4  * Copyright 1995 Martin von Loewis
5  * Copyright 1996 Alexandre Julliard
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "wownt32.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "stackframe.h"
35 #include "controls.h"
36 #include "heap.h"
37 #include "win.h"
38 #include "winproc.h"
39 #include "message.h"
40 #include "thread.h"
41 #include "dde.h"
42 #include "winternl.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DECLARE_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DEFAULT_DEBUG_CHANNEL(win);
49
50 #include "pshpack1.h"
51
52 /* Window procedure 16-to-32-bit thunk */
53 typedef struct
54 {
55     BYTE       popl_eax;             /* popl  %eax (return address) */
56     BYTE       pushl_func;           /* pushl $proc */
57     WNDPROC    proc;
58     BYTE       pushl_eax;            /* pushl %eax */
59     BYTE       ljmp;                 /* ljmp relay*/
60     DWORD      relay_offset;         /* __wine_call_wndproc_32A/W */
61     WORD       relay_sel;
62 } WINPROC_THUNK_FROM16;
63
64 /* Window procedure 32-to-16-bit thunk */
65 typedef struct
66 {
67     BYTE       popl_eax;             /* popl  %eax (return address) */
68     BYTE       pushl_func;           /* pushl $proc */
69     WNDPROC16  proc;
70     BYTE       pushl_eax;            /* pushl %eax */
71     BYTE       jmp;                  /* jmp   relay (relative jump)*/
72     void     (*relay)();             /* WINPROC_CallProc32ATo16() */
73 } WINPROC_THUNK_FROM32;
74
75 /* Simple jmp to call 32-bit procedure directly */
76 typedef struct
77 {
78     BYTE       jmp;                  /* jmp  proc (relative jump) */
79     WNDPROC    proc;
80 } WINPROC_JUMP;
81 #include "poppack.h"
82
83 typedef union
84 {
85     WINPROC_THUNK_FROM16  t_from16;
86     WINPROC_THUNK_FROM32  t_from32;
87 } WINPROC_THUNK;
88
89 typedef struct tagWINDOWPROC
90 {
91     WINPROC_THUNK         thunk;    /* Thunk */
92     WINPROC_JUMP          jmp;      /* Jump */
93     struct tagWINDOWPROC *next;     /* Next window proc */
94     UINT                magic;    /* Magic number */
95     WINDOWPROCTYPE        type;     /* Function type */
96     WINDOWPROCUSER        user;     /* Function user */
97 } WINDOWPROC;
98
99 #define WINPROC_MAGIC  ('W' | ('P' << 8) | ('R' << 16) | ('C' << 24))
100
101 #define WINPROC_THUNKPROC(pproc) \
102     (((pproc)->type == WIN_PROC_16) ? \
103           (WNDPROC16)((pproc)->thunk.t_from32.proc) : \
104           (WNDPROC16)((pproc)->thunk.t_from16.proc))
105
106 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
107                                                UINT msg, WPARAM wParam,
108                                                LPARAM lParam );
109 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
110                                                UINT msg, WPARAM wParam,
111                                                LPARAM lParam );
112
113 #define MAX_WINPROCS  (0x10000 / sizeof(WINDOWPROC))
114
115 static WINDOWPROC winproc_array[MAX_WINPROCS];
116 static WINDOWPROC *winproc_first_free;
117 static UINT winproc_used;
118
119 static CRITICAL_SECTION winproc_cs;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
121 {
122     0, 0, &winproc_cs,
123     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124       0, 0, { 0, (DWORD)(__FILE__ ": winproc_cs") }
125 };
126 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
127
128 /* allocate a window procedure from the global array */
129 static WINDOWPROC *alloc_winproc(void)
130 {
131     WINDOWPROC *ret = NULL;
132
133     EnterCriticalSection( &winproc_cs );
134     if ((ret = winproc_first_free))
135         winproc_first_free = ret->next;
136     else if (winproc_used < MAX_WINPROCS)
137         ret = &winproc_array[winproc_used++];
138     LeaveCriticalSection( &winproc_cs );
139     return ret;
140 }
141
142 static void free_winproc( WINDOWPROC *proc )
143 {
144     EnterCriticalSection( &winproc_cs );
145     proc->magic = 0;
146     proc->next  = winproc_first_free;
147     winproc_first_free = proc;
148     LeaveCriticalSection( &winproc_cs );
149 }
150
151 static BOOL is_valid_winproc( WINDOWPROC *proc )
152 {
153     if (proc < winproc_array || proc >= winproc_array + MAX_WINPROCS) return FALSE;
154     if (proc != winproc_array + (proc - winproc_array)) return FALSE;
155     return (proc->magic == WINPROC_MAGIC);
156 }
157
158 static WORD get_winproc_selector(void)
159 {
160     static LONG winproc_selector;
161     WORD ret;
162
163     if (!(ret = winproc_selector))
164     {
165         LDT_ENTRY entry;
166         WORD sel = wine_ldt_alloc_entries(1);
167         wine_ldt_set_base( &entry, winproc_array );
168         wine_ldt_set_limit( &entry, sizeof(winproc_array) - 1 );
169         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
170         wine_ldt_set_entry( sel, &entry );
171         if (!(ret = InterlockedCompareExchange( &winproc_selector, sel, 0 ))) ret = sel;
172         else wine_ldt_free_entries( sel, 1 );  /* somebody beat us to it */
173     }
174     return ret;
175 }
176
177
178 #ifdef __i386__
179 /* Some window procedures modify register they shouldn't, or are not
180  * properly declared stdcall; so we need a small assembly wrapper to
181  * call them. */
182 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
183                                 WPARAM wParam, LPARAM lParam );
184 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
185                    "pushl %ebp\n\t"
186                    "movl %esp,%ebp\n\t"
187                    "pushl %edi\n\t"
188                    "pushl %esi\n\t"
189                    "pushl %ebx\n\t"
190                    "pushl 24(%ebp)\n\t"
191                    "pushl 20(%ebp)\n\t"
192                    "pushl 16(%ebp)\n\t"
193                    "pushl 12(%ebp)\n\t"
194                    "movl 8(%ebp),%eax\n\t"
195                    "call *%eax\n\t"
196                    "leal -12(%ebp),%esp\n\t"
197                    "popl %ebx\n\t"
198                    "popl %esi\n\t"
199                    "popl %edi\n\t"
200                    "leave\n\t"
201                    "ret" );
202 #else
203 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
204                                        WPARAM wParam, LPARAM lParam )
205 {
206     return proc( hwnd, msg, wParam, lParam );
207 }
208 #endif  /* __i386__ */
209
210
211 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
212 {
213     to->ptReserved.x     = from->ptReserved.x;
214     to->ptReserved.y     = from->ptReserved.y;
215     to->ptMaxSize.x      = from->ptMaxSize.x;
216     to->ptMaxSize.y      = from->ptMaxSize.y;
217     to->ptMaxPosition.x  = from->ptMaxPosition.x;
218     to->ptMaxPosition.y  = from->ptMaxPosition.y;
219     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
220     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
221     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
222     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
223 }
224
225 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
226 {
227     to->ptReserved.x     = from->ptReserved.x;
228     to->ptReserved.y     = from->ptReserved.y;
229     to->ptMaxSize.x      = from->ptMaxSize.x;
230     to->ptMaxSize.y      = from->ptMaxSize.y;
231     to->ptMaxPosition.x  = from->ptMaxPosition.x;
232     to->ptMaxPosition.y  = from->ptMaxPosition.y;
233     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
234     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
235     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
236     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
237 }
238
239 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
240 {
241     to->hwnd            = HWND_16(from->hwnd);
242     to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
243     to->x               = from->x;
244     to->y               = from->y;
245     to->cx              = from->cx;
246     to->cy              = from->cy;
247     to->flags           = from->flags;
248 }
249
250 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
251 {
252     to->hwnd            = WIN_Handle32(from->hwnd);
253     to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
254                            HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
255     to->x               = from->x;
256     to->y               = from->y;
257     to->cx              = from->cx;
258     to->cy              = from->cy;
259     to->flags           = from->flags;
260 }
261
262 /* The strings are not copied */
263 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
264 {
265     to->lpCreateParams = (SEGPTR)from->lpCreateParams;
266     to->hInstance      = HINSTANCE_16(from->hInstance);
267     to->hMenu          = HMENU_16(from->hMenu);
268     to->hwndParent     = HWND_16(from->hwndParent);
269     to->cy             = from->cy;
270     to->cx             = from->cx;
271     to->y              = from->y;
272     to->x              = from->x;
273     to->style          = from->style;
274     to->dwExStyle      = from->dwExStyle;
275 }
276
277 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
278
279 {
280     to->lpCreateParams = (LPVOID)from->lpCreateParams;
281     to->hInstance      = HINSTANCE_32(from->hInstance);
282     to->hMenu          = HMENU_32(from->hMenu);
283     to->hwndParent     = WIN_Handle32(from->hwndParent);
284     to->cy             = from->cy;
285     to->cx             = from->cx;
286     to->y              = from->y;
287     to->x              = from->x;
288     to->style          = from->style;
289     to->dwExStyle      = from->dwExStyle;
290 }
291
292 /* The strings are not copied */
293 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
294 {
295     to->hOwner = HINSTANCE_16(from->hOwner);
296     to->x      = from->x;
297     to->y      = from->y;
298     to->cx     = from->cx;
299     to->cy     = from->cy;
300     to->style  = from->style;
301     to->lParam = from->lParam;
302 }
303
304 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
305 {
306     to->hOwner = HINSTANCE_32(from->hOwner);
307     to->x      = from->x;
308     to->y      = from->y;
309     to->cx     = from->cx;
310     to->cy     = from->cy;
311     to->style  = from->style;
312     to->lParam = from->lParam;
313 }
314
315 /**********************************************************************
316  *           WINPROC_CallWndProc32
317  *
318  * Call a 32-bit WndProc.
319  */
320 static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
321                                       WPARAM wParam, LPARAM lParam )
322 {
323     LRESULT retvalue;
324     int iWndsLocks;
325
326     hwnd = WIN_GetFullHandle( hwnd );
327     if (TRACE_ON(relay))
328         DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
329                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
330     /* To avoid any deadlocks, all the locks on the windows structures
331        must be suspended before the control is passed to the application */
332     iWndsLocks = WIN_SuspendWndsLock();
333     retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
334     WIN_RestoreWndsLock(iWndsLocks);
335
336     if (TRACE_ON(relay))
337         DPRINTF( "%04lx:Ret  window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
338                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
339     return retvalue;
340 }
341
342 /***********************************************************************
343  *           WINPROC_CallWndProc16
344  *
345  * Call a 16-bit window procedure
346  */
347 static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
348                                              UINT16 msg, WPARAM16 wParam,
349                                              LPARAM lParam )
350 {
351     CONTEXT86 context;
352     LRESULT ret;
353     WORD args[5];
354     DWORD offset = 0;
355     TEB *teb = NtCurrentTeb();
356     int iWndsLocks;
357
358     /* Window procedures want ax = hInstance, ds = es = ss */
359
360     memset(&context, 0, sizeof(context));
361     context.SegDs = context.SegEs = SELECTOROF(teb->cur_stack);
362     context.SegFs = wine_get_fs();
363     context.SegGs = wine_get_gs();
364     if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWL_HINSTANCE ))) context.Eax = context.SegDs;
365     context.SegCs = SELECTOROF(proc);
366     context.Eip   = OFFSETOF(proc);
367     context.Ebp   = OFFSETOF(teb->cur_stack)
368                         + (WORD)&((STACK16FRAME*)0)->bp;
369
370     if (lParam)
371     {
372         /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
373            work if structures passed in lParam are placed in the stack/data
374            segment. Programmers easily make the mistake of converting lParam
375            to a near rather than a far pointer, since Windows apparently
376            allows this. We copy the structures to the 16 bit stack; this is
377            ugly but makes these programs work. */
378         switch (msg)
379         {
380           case WM_CREATE:
381           case WM_NCCREATE:
382             offset = sizeof(CREATESTRUCT16); break;
383           case WM_DRAWITEM:
384             offset = sizeof(DRAWITEMSTRUCT16); break;
385           case WM_COMPAREITEM:
386             offset = sizeof(COMPAREITEMSTRUCT16); break;
387         }
388         if (offset)
389         {
390             void *s = MapSL(lParam);
391             lParam = stack16_push( offset );
392             memcpy( MapSL(lParam), s, offset );
393         }
394     }
395
396     iWndsLocks = WIN_SuspendWndsLock();
397
398     args[4] = hwnd;
399     args[3] = msg;
400     args[2] = wParam;
401     args[1] = HIWORD(lParam);
402     args[0] = LOWORD(lParam);
403     WOWCallback16Ex( 0, WCB16_REGS, sizeof(args), args, (DWORD *)&context );
404     ret = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
405
406     if (offset) stack16_pop( offset );
407
408     WIN_RestoreWndsLock(iWndsLocks);
409
410     return ret;
411 }
412
413
414 /**********************************************************************
415  *           WINPROC_GetPtr
416  *
417  * Return a pointer to the win proc.
418  */
419 static WINDOWPROC *WINPROC_GetPtr( WNDPROC handle )
420 {
421     BYTE *ptr;
422     WINDOWPROC *proc;
423
424     /* ptr cannot be < 64K */
425     if (!HIWORD(handle)) return NULL;
426
427     /* Check for a linear pointer */
428
429     ptr = (BYTE *)handle;
430     /* First check if it is the jmp address */
431     proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->jmp);
432     if (is_valid_winproc(proc)) return proc;
433
434     /* Now it must be the thunk address */
435     proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->thunk);
436     if (is_valid_winproc(proc)) return proc;
437
438     /* Check for a segmented pointer */
439
440     if (!IsBadReadPtr16( (SEGPTR)handle, sizeof(proc->thunk) ))
441     {
442         ptr = MapSL( (SEGPTR)handle );
443         /* It must be the thunk address */
444         proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->thunk);
445         if (is_valid_winproc(proc)) return proc;
446     }
447
448     return NULL;
449 }
450
451
452 /**********************************************************************
453  *           WINPROC_AllocWinProc
454  *
455  * Allocate a new window procedure.
456  */
457 static WINDOWPROC *WINPROC_AllocWinProc( WNDPROC func, WINDOWPROCTYPE type,
458                                          WINDOWPROCUSER user )
459 {
460     static FARPROC16 relay_32A, relay_32W;
461
462     WINDOWPROC *proc, *oldproc;
463
464     /* Allocate a window procedure */
465
466     if (!(proc = alloc_winproc())) return 0;
467
468     /* Check if the function is already a win proc */
469
470     if ((oldproc = WINPROC_GetPtr( func )))
471     {
472         *proc = *oldproc;
473     }
474     else
475     {
476         switch(type)
477         {
478         case WIN_PROC_16:
479             proc->thunk.t_from32.popl_eax    = 0x58;   /* popl  %eax */
480             proc->thunk.t_from32.pushl_func  = 0x68;   /* pushl $proc */
481             proc->thunk.t_from32.proc        = (WNDPROC16)func;
482             proc->thunk.t_from32.pushl_eax   = 0x50;   /* pushl %eax */
483             proc->thunk.t_from32.jmp         = 0xe9;   /* jmp   relay*/
484             proc->thunk.t_from32.relay =  /* relative jump */
485                 (void(*)())((DWORD)WINPROC_CallProc32ATo16 -
486                                      (DWORD)(&proc->thunk.t_from32.relay + 1));
487             break;
488         case WIN_PROC_32A:
489             if (!relay_32A) relay_32A = GetProcAddress16( GetModuleHandle16("user"),
490                                                           "__wine_call_wndproc_32A" );
491             proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
492             proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
493             proc->thunk.t_from16.proc         = func;
494             proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
495             proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
496             proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32A);
497             proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32A);
498             proc->jmp.jmp  = 0xe9;
499             /* Fixup relative jump */
500             proc->jmp.proc = (WNDPROC)((DWORD)func - (DWORD)(&proc->jmp.proc + 1));
501             break;
502         case WIN_PROC_32W:
503             if (!relay_32W) relay_32W = GetProcAddress16( GetModuleHandle16("user"),
504                                                           "__wine_call_wndproc_32W" );
505             proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
506             proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
507             proc->thunk.t_from16.proc         = func;
508             proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
509             proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
510             proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32W);
511             proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32W);
512             proc->jmp.jmp  = 0xe9;
513             /* Fixup relative jump */
514             proc->jmp.proc = (WNDPROC)((char *)func - (char *)(&proc->jmp.proc + 1));
515             break;
516         default:
517             /* Should not happen */
518             break;
519         }
520         proc->magic = WINPROC_MAGIC;
521         proc->type  = type;
522         proc->user  = user;
523     }
524     proc->next  = NULL;
525     TRACE("(%p,%d): returning %p\n", func, type, proc );
526     return proc;
527 }
528
529
530 /**********************************************************************
531  *           WINPROC_GetProc
532  *
533  * Get a window procedure pointer that can be passed to the Windows program.
534  */
535 WNDPROC16 WINPROC_GetProc( WNDPROC proc, WINDOWPROCTYPE type )
536 {
537     WINDOWPROC *ptr = (WINDOWPROC *)proc;
538
539     if (!proc) return NULL;
540     if (type == WIN_PROC_16)  /* We want a 16:16 address */
541     {
542         if (ptr->type == WIN_PROC_16)
543             return ptr->thunk.t_from32.proc;
544         else
545             return (WNDPROC16)MAKESEGPTR( get_winproc_selector(),
546                                           (char *)&ptr->thunk - (char *)winproc_array );
547     }
548     else  /* We want a 32-bit address */
549     {
550         if (ptr->type == WIN_PROC_16)
551             return (WNDPROC16)&ptr->thunk;
552         else if (type != ptr->type)
553             /* Have to return the jmp address if types don't match */
554             return (WNDPROC16)&ptr->jmp;
555         else
556             /* Some Win16 programs want to get back the proc they set */
557             return (WNDPROC16)ptr->thunk.t_from16.proc;
558     }
559 }
560
561
562 /**********************************************************************
563  *           WINPROC_SetProc
564  *
565  * Set the window procedure for a window or class. There are
566  * three tree classes of winproc callbacks:
567  *
568  * 1) class  -> wp                      -       not subclassed
569  *    class  -> wp -> wp -> wp -> wp    -       SetClassLong()
570  *             /           /
571  * 2) window -'           /             -       not subclassed
572  *    window -> wp -> wp '              -       SetWindowLong()
573  *
574  * 3) timer  -> wp                      -       SetTimer()
575  *
576  * Initially, winproc of the window points to the current winproc
577  * thunk of its class. Subclassing prepends a new thunk to the
578  * window winproc chain at the head of the list. Thus, window thunk
579  * list includes class thunks and the latter are preserved when the
580  * window is destroyed.
581  *
582  */
583 BOOL WINPROC_SetProc( WNDPROC *pFirst, WNDPROC func,
584                       WINDOWPROCTYPE type, WINDOWPROCUSER user )
585 {
586     BOOL bRecycle = FALSE;
587     WINDOWPROC *proc, **ppPrev;
588
589     /* Check if function is already in the list */
590
591     ppPrev = (WINDOWPROC **)pFirst;
592     proc = WINPROC_GetPtr( func );
593     while (*ppPrev)
594     {
595         if (proc)
596         {
597             if (*ppPrev == proc)
598             {
599                 if ((*ppPrev)->user != user)
600                 {
601                     /* terminal thunk is being restored */
602
603                     WINPROC_FreeProc( *pFirst, (*ppPrev)->user );
604                     *(WINDOWPROC **)pFirst = *ppPrev;
605                     return TRUE;
606                 }
607                 bRecycle = TRUE;
608                 break;
609             }
610         }
611         else
612         {
613             if (((*ppPrev)->type == type) &&
614                 (func == (WNDPROC)WINPROC_THUNKPROC(*ppPrev)))
615             {
616                 if((*ppPrev)->user == user)
617                 {
618                     bRecycle = TRUE;
619                 }
620                 else
621                 {
622                     WINPROC_FreeProc( (WNDPROC)*ppPrev, user );
623                     *ppPrev = NULL;
624                 }
625                 break;
626             }
627         }
628
629         /* WPF_CLASS thunk terminates window thunk list */
630         if ((*ppPrev)->user != user) break;
631         ppPrev = &(*ppPrev)->next;
632     }
633
634     if (bRecycle)
635     {
636         /* Extract this thunk from the list */
637         proc = *ppPrev;
638         *ppPrev = proc->next;
639     }
640     else  /* Allocate a new one */
641     {
642         if (proc)  /* Was already a win proc */
643         {
644             type = proc->type;
645             func = (WNDPROC)WINPROC_THUNKPROC(proc);
646         }
647         proc = WINPROC_AllocWinProc( func, type, user );
648         if (!proc) return FALSE;
649     }
650
651     /* Add the win proc at the head of the list */
652
653     TRACE("(%p,%p,%d): res=%p\n", *pFirst, func, type, proc );
654     proc->next  = *(WINDOWPROC **)pFirst;
655     *(WINDOWPROC **)pFirst = proc;
656     return TRUE;
657 }
658
659
660 /**********************************************************************
661  *           WINPROC_FreeProc
662  *
663  * Free a list of win procs.
664  */
665 void WINPROC_FreeProc( WNDPROC proc, WINDOWPROCUSER user )
666 {
667     WINDOWPROC *ptr = (WINDOWPROC *)proc;
668     while (ptr)
669     {
670         WINDOWPROC *next = ptr->next;
671         if (ptr->user != user) break;
672         TRACE("freeing %p (%d)\n", ptr, user);
673         free_winproc( ptr );
674         ptr = next;
675     }
676 }
677
678
679 /**********************************************************************
680  *           WINPROC_GetProcType
681  *
682  * Return the window procedure type.
683  */
684 WINDOWPROCTYPE WINPROC_GetProcType( WNDPROC proc )
685 {
686     if (!proc ||
687         (((WINDOWPROC *)proc)->magic != WINPROC_MAGIC))
688         return WIN_PROC_INVALID;
689     return ((WINDOWPROC *)proc)->type;
690 }
691 /**********************************************************************
692  *           WINPROC_TestCBForStr
693  *
694  * Return TRUE if the lparam is a string
695  */
696 inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
697 {
698     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
699     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
700 }
701 /**********************************************************************
702  *           WINPROC_TestLBForStr
703  *
704  * Return TRUE if the lparam is a string
705  */
706 inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
707 {
708     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
709     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
710
711 }
712 /**********************************************************************
713  *           WINPROC_MapMsg32ATo32W
714  *
715  * Map a message from Ansi to Unicode.
716  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
717  *
718  * FIXME:
719  *  WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
720  *  the first four bytes are the handle of the icon
721  *  when the WM_SETTEXT message has been used to set the icon
722  */
723 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
724 {
725     switch(msg)
726     {
727     case WM_GETTEXT:
728     case WM_ASKCBFORMATNAME:
729         {
730             LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
731                                      *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
732             if (!ptr) return -1;
733             *ptr++ = *plparam;  /* Store previous lParam */
734             *plparam = (LPARAM)ptr;
735         }
736         return 1;
737     /* lparam is string (0-terminated) */
738     case WM_SETTEXT:
739     case WM_WININICHANGE:
740     case WM_DEVMODECHANGE:
741     case CB_DIR:
742     case LB_DIR:
743     case LB_ADDFILE:
744     case EM_REPLACESEL:
745         {
746             DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
747             WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
748             len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
749             *plparam = (LPARAM)buf;
750             return (*plparam ? 1 : -1);
751         }
752     case WM_GETTEXTLENGTH:
753     case CB_GETLBTEXTLEN:
754     case LB_GETTEXTLEN:
755         return 1;  /* need to map result */
756     case WM_NCCREATE:
757     case WM_CREATE:
758         {
759             UNICODE_STRING usBuffer;
760             struct s
761             { CREATESTRUCTW cs;         /* new structure */
762               LPCWSTR lpszName;         /* allocated Name */
763               LPCWSTR lpszClass;        /* allocated Class */
764             };
765
766             struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
767             if (!xs) return -1;
768             xs->cs = *(CREATESTRUCTW *)*plparam;
769             if (HIWORD(xs->cs.lpszName))
770             {
771                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
772                 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
773             }
774             if (HIWORD(xs->cs.lpszClass))
775             {
776                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
777                 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
778             }
779
780             if (GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
781             {
782                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
783                                                                          sizeof(*mdi_cs));
784                 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
785                 if (HIWORD(mdi_cs->szTitle))
786                 {
787                     RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
788                     mdi_cs->szTitle = usBuffer.Buffer;
789                 }
790                 if (HIWORD(mdi_cs->szClass))
791                 {
792                     RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
793                     mdi_cs->szClass = usBuffer.Buffer;
794                 }
795                 xs->cs.lpCreateParams = mdi_cs;
796             }
797
798             *plparam = (LPARAM)xs;
799         }
800         return 1;
801     case WM_MDICREATE:
802         {
803             MDICREATESTRUCTW *cs =
804                 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
805             if (!cs) return -1;
806             *cs = *(MDICREATESTRUCTW *)*plparam;
807             if (HIWORD(cs->szClass))
808             {
809                 UNICODE_STRING usBuffer;
810                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
811                 cs->szClass = usBuffer.Buffer;
812             }
813             if (HIWORD(cs->szTitle))
814             {
815                 UNICODE_STRING usBuffer;
816                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
817                 cs->szTitle = usBuffer.Buffer;
818             }
819             *plparam = (LPARAM)cs;
820         }
821         return 1;
822
823 /* Listbox */
824     case LB_ADDSTRING:
825     case LB_INSERTSTRING:
826     case LB_FINDSTRING:
827     case LB_FINDSTRINGEXACT:
828     case LB_SELECTSTRING:
829         if(!*plparam) return 0;
830         if ( WINPROC_TestLBForStr( hwnd ))
831         {
832             UNICODE_STRING usBuffer;
833             RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
834             *plparam = (LPARAM)usBuffer.Buffer;
835         }
836         return (*plparam ? 1 : -1);
837
838     case LB_GETTEXT:                /* FIXME: fixed sized buffer */
839         { if ( WINPROC_TestLBForStr( hwnd ))
840           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
841             if (!ptr) return -1;
842             *ptr++ = *plparam;  /* Store previous lParam */
843             *plparam = (LPARAM)ptr;
844           }
845         }
846         return 1;
847
848 /* Combobox */
849     case CB_ADDSTRING:
850     case CB_INSERTSTRING:
851     case CB_FINDSTRINGEXACT:
852     case CB_FINDSTRING:
853     case CB_SELECTSTRING:
854         if(!*plparam) return 0;
855         if ( WINPROC_TestCBForStr( hwnd ))
856         {
857             UNICODE_STRING usBuffer;
858             RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
859             *plparam = (LPARAM)usBuffer.Buffer;
860         }
861         return (*plparam ? 1 : -1);
862
863     case CB_GETLBTEXT:    /* FIXME: fixed sized buffer */
864         { if ( WINPROC_TestCBForStr( hwnd ))
865           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
866             if (!ptr) return -1;
867             *ptr++ = *plparam;  /* Store previous lParam */
868             *plparam = (LPARAM)ptr;
869           }
870         }
871         return 1;
872
873 /* Multiline edit */
874     case EM_GETLINE:
875         { WORD len = (WORD)*plparam;
876           LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
877           if (!ptr) return -1;
878           *ptr++ = *plparam;  /* Store previous lParam */
879           *((WORD *) ptr) = len;   /* Store the length */
880           *plparam = (LPARAM)ptr;
881         }
882         return 1;
883
884     case WM_CHARTOITEM:
885     case WM_MENUCHAR:
886     case WM_CHAR:
887     case WM_DEADCHAR:
888     case WM_SYSCHAR:
889     case WM_SYSDEADCHAR:
890     case EM_SETPASSWORDCHAR:
891         {
892             BYTE ch = LOWORD(*pwparam);
893             WCHAR wch;
894             MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
895             *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
896         }
897         return 0;
898
899     case WM_IME_CHAR:
900         {
901             BYTE ch[2];
902             WCHAR wch;
903             ch[0] = (*pwparam >> 8);
904             ch[1] = *pwparam & 0xff;
905             if (ch[0])
906                 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
907             else
908                 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
909             *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
910         }
911         return 0;
912
913     case WM_PAINTCLIPBOARD:
914     case WM_SIZECLIPBOARD:
915         FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
916         return -1;
917     default:  /* No translation needed */
918         return 0;
919     }
920 }
921
922
923 /**********************************************************************
924  *           WINPROC_UnmapMsg32ATo32W
925  *
926  * Unmap a message that was mapped from Ansi to Unicode.
927  */
928 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
929                                   LRESULT result )
930 {
931     switch(msg)
932     {
933     case WM_GETTEXT:
934     case WM_ASKCBFORMATNAME:
935         {
936             LPARAM *ptr = (LPARAM *)lParam - 1;
937             if (!wParam) result = 0;
938             else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
939                                                     (LPSTR)*ptr, wParam, NULL, NULL )))
940             {
941                 ((LPSTR)*ptr)[wParam-1] = 0;
942                 result = wParam - 1;
943             }
944             else result--;  /* do not count terminating null */
945             HeapFree( GetProcessHeap(), 0, ptr );
946         }
947         break;
948     case WM_GETTEXTLENGTH:
949     case CB_GETLBTEXTLEN:
950     case LB_GETTEXTLEN:
951         /* there may be one DBCS char for each Unicode char */
952         return result * 2;
953     case WM_NCCREATE:
954     case WM_CREATE:
955         {
956             struct s
957             { CREATESTRUCTW cs;         /* new structure */
958               LPWSTR lpszName;          /* allocated Name */
959               LPWSTR lpszClass;         /* allocated Class */
960             };
961             struct s *xs = (struct s *)lParam;
962             if (xs->lpszName)  HeapFree( GetProcessHeap(), 0, xs->lpszName );
963             if (xs->lpszClass) HeapFree( GetProcessHeap(), 0, xs->lpszClass );
964
965             if (GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
966             {
967                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
968                 if (HIWORD(mdi_cs->szTitle))
969                     HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
970                 if (HIWORD(mdi_cs->szClass))
971                     HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
972                 HeapFree(GetProcessHeap(), 0, mdi_cs);
973             }
974             HeapFree( GetProcessHeap(), 0, xs );
975         }
976         break;
977
978     case WM_MDICREATE:
979         {
980             MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
981             if (HIWORD(cs->szTitle))
982                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
983             if (HIWORD(cs->szClass))
984                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
985             HeapFree( GetProcessHeap(), 0, cs );
986         }
987         break;
988
989     case WM_SETTEXT:
990     case WM_WININICHANGE:
991     case WM_DEVMODECHANGE:
992     case CB_DIR:
993     case LB_DIR:
994     case LB_ADDFILE:
995     case EM_REPLACESEL:
996         HeapFree( GetProcessHeap(), 0, (void *)lParam );
997         break;
998
999 /* Listbox */
1000     case LB_ADDSTRING:
1001     case LB_INSERTSTRING:
1002     case LB_FINDSTRING:
1003     case LB_FINDSTRINGEXACT:
1004     case LB_SELECTSTRING:
1005         if ( WINPROC_TestLBForStr( hwnd ))
1006           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1007         break;
1008
1009     case LB_GETTEXT:
1010         if ( WINPROC_TestLBForStr( hwnd ))
1011         {
1012             LPARAM *ptr = (LPARAM *)lParam - 1;
1013             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
1014                                           (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
1015             HeapFree( GetProcessHeap(), 0, ptr );
1016         }
1017         break;
1018
1019 /* Combobox */
1020     case CB_ADDSTRING:
1021     case CB_INSERTSTRING:
1022     case CB_FINDSTRING:
1023     case CB_FINDSTRINGEXACT:
1024     case CB_SELECTSTRING:
1025         if ( WINPROC_TestCBForStr( hwnd ))
1026           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1027         break;
1028
1029     case CB_GETLBTEXT:
1030         if ( WINPROC_TestCBForStr( hwnd ))
1031         {
1032             LPARAM *ptr = (LPARAM *)lParam - 1;
1033             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
1034                                           (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
1035             HeapFree( GetProcessHeap(), 0, ptr );
1036         }
1037         break;
1038
1039 /* Multiline edit */
1040     case EM_GETLINE:
1041         {
1042             LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lParam */
1043             WORD len = *(WORD *) lParam;
1044             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
1045                                           (LPSTR)*ptr, len, NULL, NULL );
1046             if (result < len) ((LPSTR)*ptr)[result] = 0;
1047             HeapFree( GetProcessHeap(), 0, ptr );
1048         }
1049         break;
1050     }
1051     return result;
1052 }
1053
1054
1055 /**********************************************************************
1056  *           WINPROC_MapMsg32WTo32A
1057  *
1058  * Map a message from Unicode to Ansi.
1059  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1060  */
1061 static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
1062 {
1063     switch(msg)
1064     {
1065     case WM_GETTEXT:
1066     case WM_ASKCBFORMATNAME:
1067         {
1068             LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
1069                                                *pwparam + sizeof(LPARAM) );
1070             if (!ptr) return -1;
1071             *ptr++ = *plparam;  /* Store previous lParam */
1072             *plparam = (LPARAM)ptr;
1073         }
1074         return 1;
1075
1076     case WM_SETTEXT:
1077     case WM_WININICHANGE:
1078     case WM_DEVMODECHANGE:
1079     case CB_DIR:
1080     case LB_DIR:
1081     case LB_ADDFILE:
1082     case EM_REPLACESEL:
1083         if(!*plparam) return 0;
1084         *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1085         return (*plparam ? 1 : -1);
1086
1087     case WM_MDICREATE:
1088         {
1089             MDICREATESTRUCTA *cs =
1090                 (MDICREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1091             if (!cs) return -1;
1092             *cs = *(MDICREATESTRUCTA *)*plparam;
1093             if (HIWORD(cs->szTitle))
1094                 cs->szTitle = HEAP_strdupWtoA( GetProcessHeap(), 0,
1095                                                (LPCWSTR)cs->szTitle );
1096             if (HIWORD(cs->szClass))
1097                 cs->szClass = HEAP_strdupWtoA( GetProcessHeap(), 0,
1098                                                (LPCWSTR)cs->szClass );
1099             *plparam = (LPARAM)cs;
1100         }
1101         return 1;
1102
1103 /* Listbox */
1104     case LB_ADDSTRING:
1105     case LB_INSERTSTRING:
1106     case LB_FINDSTRING:
1107     case LB_FINDSTRINGEXACT:
1108     case LB_SELECTSTRING:
1109         if(!*plparam) return 0;
1110         if ( WINPROC_TestLBForStr( hwnd ))
1111           *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1112         return (*plparam ? 1 : -1);
1113
1114     case LB_GETTEXT:                    /* FIXME: fixed sized buffer */
1115         { if ( WINPROC_TestLBForStr( hwnd ))
1116           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1117             if (!ptr) return -1;
1118             *ptr++ = *plparam;  /* Store previous lParam */
1119             *plparam = (LPARAM)ptr;
1120           }
1121         }
1122         return 1;
1123
1124 /* Combobox */
1125     case CB_ADDSTRING:
1126     case CB_INSERTSTRING:
1127     case CB_FINDSTRING:
1128     case CB_FINDSTRINGEXACT:
1129     case CB_SELECTSTRING:
1130         if(!*plparam) return 0;
1131         if ( WINPROC_TestCBForStr( hwnd ))
1132           *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1133         return (*plparam ? 1 : -1);
1134
1135     case CB_GETLBTEXT:          /* FIXME: fixed sized buffer */
1136         { if ( WINPROC_TestCBForStr( hwnd ))
1137           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1138             if (!ptr) return -1;
1139             *ptr++ = *plparam;  /* Store previous lParam */
1140             *plparam = (LPARAM)ptr;
1141           }
1142         }
1143         return 1;
1144
1145 /* Multiline edit */
1146     case EM_GETLINE:
1147         { WORD len = (WORD)*plparam;
1148           LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1149           if (!ptr) return -1;
1150           *ptr++ = *plparam;  /* Store previous lParam */
1151           *((WORD *) ptr) = len;   /* Store the length */
1152           *plparam = (LPARAM)ptr;
1153         }
1154         return 1;
1155
1156     case WM_CHARTOITEM:
1157     case WM_MENUCHAR:
1158     case WM_CHAR:
1159     case WM_DEADCHAR:
1160     case WM_SYSCHAR:
1161     case WM_SYSDEADCHAR:
1162     case EM_SETPASSWORDCHAR:
1163         {
1164             WCHAR wch = LOWORD(*pwparam);
1165             BYTE ch;
1166             WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
1167             *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1168         }
1169         return 0;
1170
1171     case WM_IME_CHAR:
1172         {
1173             WCHAR wch = LOWORD(*pwparam);
1174             BYTE ch[2];
1175
1176             if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
1177                 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1178             else
1179                 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1180         }
1181         return 0;
1182
1183     case WM_PAINTCLIPBOARD:
1184     case WM_SIZECLIPBOARD:
1185         FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1186         return -1;
1187     default:  /* No translation needed */
1188         return 0;
1189     }
1190 }
1191
1192
1193 /**********************************************************************
1194  *           WINPROC_UnmapMsg32WTo32A
1195  *
1196  * Unmap a message that was mapped from Unicode to Ansi.
1197  */
1198 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1199 {
1200     switch(msg)
1201     {
1202     case WM_GETTEXT:
1203     case WM_ASKCBFORMATNAME:
1204         {
1205             LPARAM *ptr = (LPARAM *)lParam - 1;
1206             if (!wParam) result = 0;
1207             else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
1208                                                      (LPWSTR)*ptr, wParam )))
1209             {
1210                 ((LPWSTR)*ptr)[wParam-1] = 0;
1211                 result = wParam - 1;
1212             }
1213             else result--;  /* do not count terminating null */
1214             HeapFree( GetProcessHeap(), 0, ptr );
1215         }
1216         break;
1217
1218     case WM_SETTEXT:
1219     case WM_WININICHANGE:
1220     case WM_DEVMODECHANGE:
1221     case CB_DIR:
1222     case LB_DIR:
1223     case LB_ADDFILE:
1224     case EM_REPLACESEL:
1225         HeapFree( GetProcessHeap(), 0, (void *)lParam );
1226         break;
1227
1228     case WM_MDICREATE:
1229         {
1230             MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1231             if (HIWORD(cs->szTitle))
1232                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1233             if (HIWORD(cs->szClass))
1234                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1235             HeapFree( GetProcessHeap(), 0, cs );
1236         }
1237         break;
1238
1239 /* Listbox */
1240     case LB_ADDSTRING:
1241     case LB_INSERTSTRING:
1242     case LB_FINDSTRING:
1243     case LB_FINDSTRINGEXACT:
1244     case LB_SELECTSTRING:
1245         if ( WINPROC_TestLBForStr( hwnd ))
1246           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1247         break;
1248
1249     case LB_GETTEXT:
1250         if ( WINPROC_TestLBForStr( hwnd ))
1251         {
1252             LPARAM *ptr = (LPARAM *)lParam - 1;
1253             result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1254             HeapFree( GetProcessHeap(), 0, ptr );
1255         }
1256         break;
1257
1258 /* Combobox */
1259     case CB_ADDSTRING:
1260     case CB_INSERTSTRING:
1261     case CB_FINDSTRING:
1262     case CB_FINDSTRINGEXACT:
1263     case CB_SELECTSTRING:
1264         if ( WINPROC_TestCBForStr( hwnd ))
1265           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1266         break;
1267
1268     case CB_GETLBTEXT:
1269         if ( WINPROC_TestCBForStr( hwnd ))
1270         {
1271             LPARAM *ptr = (LPARAM *)lParam - 1;
1272             result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1273             HeapFree( GetProcessHeap(), 0, ptr );
1274         }
1275         break;
1276
1277 /* Multiline edit */
1278     case EM_GETLINE:
1279         {
1280             LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lparam */
1281             WORD len = *(WORD *)ptr;
1282             if (len)
1283             {
1284                 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1285                 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1286             }
1287             HeapFree( GetProcessHeap(), 0, ptr );
1288         }
1289         break;
1290     }
1291     return result;
1292 }
1293
1294 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1295 {
1296     HANDLE      dst;
1297     UINT        sz = GlobalSize16(src);
1298     LPSTR       ptr16, ptr32;
1299
1300     if (!(dst = GlobalAlloc(flags, sz)))
1301         return 0;
1302     ptr16 = GlobalLock16(src);
1303     ptr32 = GlobalLock(dst);
1304     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1305     GlobalUnlock16(src);
1306     GlobalUnlock(dst);
1307
1308     return (UINT)dst;
1309 }
1310
1311 /**********************************************************************
1312  *           WINPROC_MapMsg16To32A
1313  *
1314  * Map a message from 16- to 32-bit Ansi.
1315  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1316  */
1317 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1318                              WPARAM *pwparam32, LPARAM *plparam )
1319 {
1320     *pmsg32 = (UINT)msg16;
1321     *pwparam32 = (WPARAM)wParam16;
1322     switch(msg16)
1323     {
1324     case WM_ACTIVATE:
1325     case WM_CHARTOITEM:
1326     case WM_COMMAND:
1327     case WM_VKEYTOITEM:
1328         *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1329         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1330         return 0;
1331     case WM_HSCROLL:
1332     case WM_VSCROLL:
1333         *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1334         *plparam   = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1335         return 0;
1336     case WM_CTLCOLOR:
1337         if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1338         *pmsg32    = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1339         *pwparam32 = (WPARAM)HDC_32(wParam16);
1340         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1341         return 0;
1342     case WM_COMPAREITEM:
1343         {
1344             COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1345             COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)
1346                                         HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1347             if (!cis) return -1;
1348             cis->CtlType    = cis16->CtlType;
1349             cis->CtlID      = cis16->CtlID;
1350             cis->hwndItem   = WIN_Handle32( cis16->hwndItem );
1351             cis->itemID1    = cis16->itemID1;
1352             cis->itemData1  = cis16->itemData1;
1353             cis->itemID2    = cis16->itemID2;
1354             cis->itemData2  = cis16->itemData2;
1355             cis->dwLocaleId = 0;  /* FIXME */
1356             *plparam = (LPARAM)cis;
1357         }
1358         return 1;
1359     case WM_DELETEITEM:
1360         {
1361             DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1362             DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)
1363                                         HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1364             if (!dis) return -1;
1365             dis->CtlType  = dis16->CtlType;
1366             dis->CtlID    = dis16->CtlID;
1367             dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1368             dis->itemData = dis16->itemData;
1369             *plparam = (LPARAM)dis;
1370         }
1371         return 1;
1372     case WM_MEASUREITEM:
1373         {
1374             MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1375             MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)
1376                                         HeapAlloc(GetProcessHeap(), 0,
1377                                                 sizeof(*mis) + sizeof(LPARAM));
1378             if (!mis) return -1;
1379             mis->CtlType    = mis16->CtlType;
1380             mis->CtlID      = mis16->CtlID;
1381             mis->itemID     = mis16->itemID;
1382             mis->itemWidth  = mis16->itemWidth;
1383             mis->itemHeight = mis16->itemHeight;
1384             mis->itemData   = mis16->itemData;
1385             *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
1386             *plparam = (LPARAM)mis;
1387         }
1388         return 1;
1389     case WM_DRAWITEM:
1390         {
1391             DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1392             DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)HeapAlloc(GetProcessHeap(), 0,
1393                                                                  sizeof(*dis));
1394             if (!dis) return -1;
1395             dis->CtlType       = dis16->CtlType;
1396             dis->CtlID         = dis16->CtlID;
1397             dis->itemID        = dis16->itemID;
1398             dis->itemAction    = dis16->itemAction;
1399             dis->itemState     = dis16->itemState;
1400             dis->hwndItem      = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1401                                                             : WIN_Handle32( dis16->hwndItem );
1402             dis->hDC           = HDC_32(dis16->hDC);
1403             dis->itemData      = dis16->itemData;
1404             dis->rcItem.left   = dis16->rcItem.left;
1405             dis->rcItem.top    = dis16->rcItem.top;
1406             dis->rcItem.right  = dis16->rcItem.right;
1407             dis->rcItem.bottom = dis16->rcItem.bottom;
1408             *plparam = (LPARAM)dis;
1409         }
1410         return 1;
1411     case WM_GETMINMAXINFO:
1412         {
1413             MINMAXINFO *mmi = (MINMAXINFO *)HeapAlloc( GetProcessHeap(), 0,
1414                                                 sizeof(*mmi) + sizeof(LPARAM));
1415             if (!mmi) return -1;
1416             MINMAXINFO16to32( MapSL(*plparam), mmi );
1417             *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
1418             *plparam = (LPARAM)mmi;
1419         }
1420         return 1;
1421     case WM_GETTEXT:
1422     case WM_SETTEXT:
1423     case WM_WININICHANGE:
1424     case WM_DEVMODECHANGE:
1425     case WM_ASKCBFORMATNAME:
1426         *plparam = (LPARAM)MapSL(*plparam);
1427         return 0;
1428     case WM_MDICREATE:
1429         {
1430             MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1431             MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1432             if (!cs) return -1;
1433             MDICREATESTRUCT16to32A( cs16, cs );
1434             cs->szTitle = MapSL(cs16->szTitle);
1435             cs->szClass = MapSL(cs16->szClass);
1436             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1437             *plparam = (LPARAM)cs;
1438         }
1439         return 1;
1440     case WM_MDIGETACTIVE:
1441         *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1442         *(BOOL*)(*plparam) = 0;
1443         return 1;
1444     case WM_MDISETMENU:
1445         if(wParam16==TRUE)
1446            *pmsg32=WM_MDIREFRESHMENU;
1447         *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1448         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1449         return 0;
1450     case WM_MENUCHAR:
1451         *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1452         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1453         return 0;
1454     case WM_MENUSELECT:
1455         if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1456         {
1457             HMENU hmenu=HMENU_32(HIWORD(*plparam));
1458             UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1459             if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1460             *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1461         }
1462         else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1463         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1464         return 0;
1465     case WM_MDIACTIVATE:
1466         if( *plparam )
1467         {
1468             *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1469             *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1470         }
1471         else /* message sent to MDI client */
1472             *pwparam32 = wParam16;
1473         return 0;
1474     case WM_NCCALCSIZE:
1475         {
1476             NCCALCSIZE_PARAMS16 *nc16;
1477             NCCALCSIZE_PARAMS *nc;
1478
1479             nc = (NCCALCSIZE_PARAMS *)HeapAlloc( GetProcessHeap(), 0,
1480                                                 sizeof(*nc) + sizeof(LPARAM) );
1481             if (!nc) return -1;
1482             nc16 = MapSL(*plparam);
1483             nc->rgrc[0].left   = nc16->rgrc[0].left;
1484             nc->rgrc[0].top    = nc16->rgrc[0].top;
1485             nc->rgrc[0].right  = nc16->rgrc[0].right;
1486             nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1487             if (wParam16)
1488             {
1489                 nc->lppos = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1490                                                       sizeof(*nc->lppos) );
1491                 nc->rgrc[1].left   = nc16->rgrc[1].left;
1492                 nc->rgrc[1].top    = nc16->rgrc[1].top;
1493                 nc->rgrc[1].right  = nc16->rgrc[1].right;
1494                 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1495                 nc->rgrc[2].left   = nc16->rgrc[2].left;
1496                 nc->rgrc[2].top    = nc16->rgrc[2].top;
1497                 nc->rgrc[2].right  = nc16->rgrc[2].right;
1498                 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1499                 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1500             }
1501             *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
1502             *plparam = (LPARAM)nc;
1503         }
1504         return 1;
1505     case WM_NCCREATE:
1506     case WM_CREATE:
1507         {
1508             CREATESTRUCT16 *cs16 = MapSL(*plparam);
1509             CREATESTRUCTA *cs = (CREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0,
1510                                                 sizeof(*cs) + sizeof(LPARAM) );
1511             if (!cs) return -1;
1512             CREATESTRUCT16to32A( cs16, cs );
1513             cs->lpszName  = MapSL(cs16->lpszName);
1514             cs->lpszClass = MapSL(cs16->lpszClass);
1515
1516             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1517             {
1518                 MDICREATESTRUCT16 *mdi_cs16;
1519                 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)HeapAlloc(GetProcessHeap(), 0,
1520                                                                            sizeof(*mdi_cs));
1521                 if (!mdi_cs)
1522                 {
1523                     HeapFree(GetProcessHeap(), 0, cs);
1524                     return -1;
1525                 }
1526                 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1527                 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1528                 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1529                 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1530
1531                 cs->lpCreateParams = mdi_cs;
1532             }
1533             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1534             *plparam = (LPARAM)cs;
1535         }
1536         return 1;
1537     case WM_PARENTNOTIFY:
1538         if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1539         {
1540             *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1541             *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1542         }
1543         return 0;
1544     case WM_WINDOWPOSCHANGING:
1545     case WM_WINDOWPOSCHANGED:
1546         {
1547             WINDOWPOS *wp = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1548                                                 sizeof(*wp) + sizeof(LPARAM) );
1549             if (!wp) return -1;
1550             WINDOWPOS16to32( MapSL(*plparam), wp );
1551             *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
1552             *plparam = (LPARAM)wp;
1553         }
1554         return 1;
1555     case WM_GETDLGCODE:
1556         if (*plparam)
1557         {
1558             LPMSG16 msg16 = MapSL(*plparam);
1559             LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1560
1561             if (!msg32) return -1;
1562             msg32->hwnd = WIN_Handle32( msg16->hwnd );
1563             msg32->lParam = msg16->lParam;
1564             msg32->time = msg16->time;
1565             msg32->pt.x = msg16->pt.x;
1566             msg32->pt.y = msg16->pt.y;
1567             /* this is right, right? */
1568             if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1569                                      &msg32->message,&msg32->wParam,
1570                                      &msg32->lParam)<0) {
1571                 HeapFree( GetProcessHeap(), 0, msg32 );
1572                 return -1;
1573             }
1574             *plparam = (LPARAM)msg32;
1575             return 1;
1576         }
1577         else return 0;
1578     case WM_NOTIFY:
1579         *plparam = (LPARAM)MapSL(*plparam);
1580         return 0;
1581     case WM_ACTIVATEAPP:
1582         /* We need this when SetActiveWindow sends a Sendmessage16() to
1583          * a 32bit window. Might be superflous with 32bit interprocess
1584          * message queues. */
1585         if (*plparam) *plparam = HTASK_32( *plparam );
1586         return 0;
1587     case WM_NEXTMENU:
1588         {
1589             MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1590             if (!next) return -1;
1591             next->hmenuIn = (HMENU)*plparam;
1592             next->hmenuNext = 0;
1593             next->hwndNext = 0;
1594             *plparam = (LPARAM)next;
1595             return 1;
1596         }
1597     case WM_PAINTCLIPBOARD:
1598     case WM_SIZECLIPBOARD:
1599         FIXME_(msg)("message %04x needs translation\n",msg16 );
1600         return -1;
1601     case WM_DDE_INITIATE:
1602     case WM_DDE_TERMINATE:
1603     case WM_DDE_UNADVISE:
1604     case WM_DDE_REQUEST:
1605         *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1606         return 0;
1607     case WM_DDE_ADVISE:
1608     case WM_DDE_DATA:
1609     case WM_DDE_POKE:
1610         {
1611             HANDLE16    lo16;
1612             ATOM        hi;
1613             UINT lo32 = 0;
1614
1615             *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1616             lo16 = LOWORD(*plparam);
1617             hi = HIWORD(*plparam);
1618             if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1619                 return -1;
1620             *plparam = PackDDElParam(msg16, lo32, hi);
1621         }
1622         return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
1623     case WM_DDE_ACK:
1624         {
1625             UINT        lo, hi;
1626             int         flag = 0;
1627             char        buf[2];
1628
1629             *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1630
1631             lo = LOWORD(*plparam);
1632             hi = HIWORD(*plparam);
1633
1634             if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1635             if (GlobalSize16(hi) != 0) flag |= 2;
1636             switch (flag)
1637             {
1638             case 0:
1639                 if (hi)
1640                 {
1641                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1642                     hi = 0;
1643                 }
1644                 break;
1645             case 1:
1646                 break; /* atom, nothing to do */
1647             case 3:
1648                 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1649                 /* fall thru */
1650             case 2:
1651                 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1652                 break;
1653             }
1654             *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1655         }
1656         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1657     case WM_DDE_EXECUTE:
1658         *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1659         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1660     default:  /* No translation needed */
1661         return 0;
1662     }
1663 }
1664
1665
1666 /**********************************************************************
1667  *           WINPROC_UnmapMsg16To32A
1668  *
1669  * Unmap a message that was mapped from 16- to 32-bit Ansi.
1670  */
1671 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1672                                  LRESULT result )
1673 {
1674     switch(msg)
1675     {
1676     case WM_COMPAREITEM:
1677     case WM_DELETEITEM:
1678     case WM_DRAWITEM:
1679         HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1680         break;
1681     case WM_MEASUREITEM:
1682         {
1683             MEASUREITEMSTRUCT16 *mis16;
1684             MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1685             lParam = *(LPARAM *)(mis + 1);
1686             mis16 = MapSL(lParam);
1687             mis16->itemWidth  = (UINT16)mis->itemWidth;
1688             mis16->itemHeight = (UINT16)mis->itemHeight;
1689             HeapFree( GetProcessHeap(), 0, mis );
1690         }
1691         break;
1692     case WM_GETMINMAXINFO:
1693         {
1694             MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1695             lParam = *(LPARAM *)(mmi + 1);
1696             MINMAXINFO32to16( mmi, MapSL(lParam));
1697             HeapFree( GetProcessHeap(), 0, mmi );
1698         }
1699         break;
1700     case WM_MDICREATE:
1701         {
1702             MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1703             lParam = *(LPARAM *)(cs + 1);
1704             MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1705             HeapFree( GetProcessHeap(), 0, cs );
1706         }
1707         break;
1708     case WM_MDIGETACTIVE:
1709         result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1710         HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1711         break;
1712     case WM_NCCALCSIZE:
1713         {
1714             NCCALCSIZE_PARAMS16 *nc16;
1715             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1716             lParam = *(LPARAM *)(nc + 1);
1717             nc16 = MapSL(lParam);
1718             nc16->rgrc[0].left   = nc->rgrc[0].left;
1719             nc16->rgrc[0].top    = nc->rgrc[0].top;
1720             nc16->rgrc[0].right  = nc->rgrc[0].right;
1721             nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1722             if (wParam)
1723             {
1724                 nc16->rgrc[1].left   = nc->rgrc[1].left;
1725                 nc16->rgrc[1].top    = nc->rgrc[1].top;
1726                 nc16->rgrc[1].right  = nc->rgrc[1].right;
1727                 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1728                 nc16->rgrc[2].left   = nc->rgrc[2].left;
1729                 nc16->rgrc[2].top    = nc->rgrc[2].top;
1730                 nc16->rgrc[2].right  = nc->rgrc[2].right;
1731                 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1732                 if (nc->lppos)
1733                 {
1734                     WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1735                     HeapFree( GetProcessHeap(), 0, nc->lppos );
1736                 }
1737             }
1738             HeapFree( GetProcessHeap(), 0, nc );
1739         }
1740         break;
1741     case WM_NCCREATE:
1742     case WM_CREATE:
1743         {
1744             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1745             lParam = *(LPARAM *)(cs + 1);
1746             CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1747
1748             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1749                 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1750
1751             HeapFree( GetProcessHeap(), 0, cs );
1752         }
1753         break;
1754     case WM_WINDOWPOSCHANGING:
1755     case WM_WINDOWPOSCHANGED:
1756         {
1757             WINDOWPOS *wp = (WINDOWPOS *)lParam;
1758             lParam = *(LPARAM *)(wp + 1);
1759             WINDOWPOS32to16(wp, MapSL(lParam));
1760             HeapFree( GetProcessHeap(), 0, wp );
1761         }
1762         break;
1763     case WM_GETDLGCODE:
1764         if (lParam)
1765         {
1766             LPMSG msg32 = (LPMSG)lParam;
1767
1768             WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1769                                      result);
1770             HeapFree( GetProcessHeap(), 0, msg32 );
1771         }
1772         break;
1773     case WM_NEXTMENU:
1774         {
1775             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1776             result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1777             HeapFree( GetProcessHeap(), 0, next );
1778         }
1779         break;
1780     }
1781     return result;
1782 }
1783
1784
1785 /**********************************************************************
1786  *           WINPROC_MapMsg16To32W
1787  *
1788  * Map a message from 16- to 32-bit Unicode.
1789  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1790  */
1791 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1792                            WPARAM *pwparam32, LPARAM *plparam )
1793 {
1794     BYTE ch;
1795     WCHAR wch;
1796
1797     *pmsg32=(UINT)msg16;
1798     *pwparam32 = (WPARAM)wParam16;
1799     switch(msg16)
1800     {
1801     case WM_GETTEXT:
1802     case WM_SETTEXT:
1803     case WM_WININICHANGE:
1804     case WM_DEVMODECHANGE:
1805     case WM_ASKCBFORMATNAME:
1806         *plparam = (LPARAM)MapSL(*plparam);
1807         return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1808     case WM_GETTEXTLENGTH:
1809     case CB_GETLBTEXTLEN:
1810     case LB_GETTEXTLEN:
1811         return 1;  /* need to map result */
1812     case WM_NCCREATE:
1813     case WM_CREATE:
1814         {
1815             CREATESTRUCT16 *cs16 = MapSL(*plparam);
1816             CREATESTRUCTW *cs = (CREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1817                                                 sizeof(*cs) + sizeof(LPARAM) );
1818             if (!cs) return -1;
1819             CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1820             cs->lpszName  = map_str_16_to_32W(cs16->lpszName);
1821             cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1822
1823             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1824             {
1825                 MDICREATESTRUCT16 *mdi_cs16;
1826                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
1827                                                                            sizeof(*mdi_cs));
1828                 if (!mdi_cs)
1829                 {
1830                     HeapFree(GetProcessHeap(), 0, cs);
1831                     return -1;
1832                 }
1833                 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1834                 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1835                 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1836                 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1837
1838                 cs->lpCreateParams = mdi_cs;
1839             }
1840             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1841             *plparam = (LPARAM)cs;
1842         }
1843         return 1;
1844     case WM_MDICREATE:
1845         {
1846             MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1847             MDICREATESTRUCTW *cs =
1848                 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1849                                                 sizeof(*cs) + sizeof(LPARAM) );
1850             if (!cs) return -1;
1851             MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1852             cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1853             cs->szClass = map_str_16_to_32W(cs16->szClass);
1854             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1855             *plparam = (LPARAM)cs;
1856         }
1857         return 1;
1858     case WM_GETDLGCODE:
1859         if (*plparam)
1860         {
1861             LPMSG16 msg16 = MapSL(*plparam);
1862             LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1863
1864             if (!msg32) return -1;
1865             msg32->hwnd = WIN_Handle32( msg16->hwnd );
1866             msg32->lParam = msg16->lParam;
1867             msg32->time = msg16->time;
1868             msg32->pt.x = msg16->pt.x;
1869             msg32->pt.y = msg16->pt.y;
1870             /* this is right, right? */
1871             if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1872                                      &msg32->message,&msg32->wParam,
1873                                      &msg32->lParam)<0) {
1874                 HeapFree( GetProcessHeap(), 0, msg32 );
1875                 return -1;
1876             }
1877             *plparam = (LPARAM)msg32;
1878             return 1;
1879         }
1880         else return 0;
1881
1882     case WM_CHARTOITEM:
1883         ch = wParam16;
1884         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1885         *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1886         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1887         return 0;
1888     case WM_MENUCHAR:
1889         ch = wParam16;
1890         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1891         *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1892         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1893         return 0;
1894     case WM_CHAR:
1895     case WM_DEADCHAR:
1896     case WM_SYSCHAR:
1897     case WM_SYSDEADCHAR:
1898         ch = wParam16;
1899         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1900         *pwparam32 = wch;
1901         return 0;
1902     case WM_IME_CHAR:
1903         return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1904
1905     default:  /* No Unicode translation needed */
1906         return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1907                                       pwparam32, plparam );
1908     }
1909 }
1910
1911
1912 /**********************************************************************
1913  *           WINPROC_UnmapMsg16To32W
1914  *
1915  * Unmap a message that was mapped from 16- to 32-bit Unicode.
1916  */
1917 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1918                                  LRESULT result )
1919 {
1920     switch(msg)
1921     {
1922     case WM_GETTEXT:
1923     case WM_SETTEXT:
1924     case WM_GETTEXTLENGTH:
1925     case CB_GETLBTEXTLEN:
1926     case LB_GETTEXTLEN:
1927     case WM_ASKCBFORMATNAME:
1928         return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
1929     case WM_NCCREATE:
1930     case WM_CREATE:
1931         {
1932             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1933             lParam = *(LPARAM *)(cs + 1);
1934             CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1935             unmap_str_16_to_32W( cs->lpszName );
1936             unmap_str_16_to_32W( cs->lpszClass );
1937
1938             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1939             {
1940                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1941                 unmap_str_16_to_32W( mdi_cs->szTitle );
1942                 unmap_str_16_to_32W( mdi_cs->szClass );
1943                 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1944             }
1945             HeapFree( GetProcessHeap(), 0, cs );
1946         }
1947         break;
1948     case WM_MDICREATE:
1949         {
1950             MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1951             lParam = *(LPARAM *)(cs + 1);
1952             MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1953             unmap_str_16_to_32W( cs->szTitle );
1954             unmap_str_16_to_32W( cs->szClass );
1955             HeapFree( GetProcessHeap(), 0, cs );
1956         }
1957         break;
1958     case WM_GETDLGCODE:
1959         if (lParam)
1960         {
1961             LPMSG msg32 = (LPMSG)lParam;
1962
1963             WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1964                                      result);
1965             HeapFree( GetProcessHeap(), 0, msg32 );
1966         }
1967         break;
1968     default:
1969         return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1970     }
1971     return result;
1972 }
1973
1974 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1975 {
1976     HANDLE16    dst;
1977     UINT        sz = GlobalSize((HANDLE)src);
1978     LPSTR       ptr16, ptr32;
1979
1980     if (!(dst = GlobalAlloc16(flags, sz)))
1981         return 0;
1982     ptr32 = GlobalLock((HANDLE)src);
1983     ptr16 = GlobalLock16(dst);
1984     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1985     GlobalUnlock((HANDLE)src);
1986     GlobalUnlock16(dst);
1987
1988     return dst;
1989 }
1990
1991
1992 /**********************************************************************
1993  *           WINPROC_MapMsg32ATo16
1994  *
1995  * Map a message from 32-bit Ansi to 16-bit.
1996  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1997  */
1998 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1999                              UINT16 *pmsg16, WPARAM16 *pwparam16,
2000                              LPARAM *plparam )
2001 {
2002     *pmsg16 = (UINT16)msg32;
2003     *pwparam16 = (WPARAM16)LOWORD(wParam32);
2004     switch(msg32)
2005     {
2006     case SBM_SETRANGE:
2007         *pmsg16 = SBM_SETRANGE16;
2008         *plparam = MAKELPARAM(wParam32, *plparam);
2009         *pwparam16 = 0;
2010         return 0;
2011
2012     case SBM_GETRANGE:
2013         *pmsg16 = SBM_GETRANGE16;
2014         return 1;
2015
2016     case BM_GETCHECK:
2017     case BM_SETCHECK:
2018     case BM_GETSTATE:
2019     case BM_SETSTATE:
2020     case BM_SETSTYLE:
2021         *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
2022         return 0;
2023
2024     case EM_GETSEL:
2025     case EM_GETRECT:
2026     case EM_SETRECT:
2027     case EM_SETRECTNP:
2028     case EM_SCROLL:
2029     case EM_LINESCROLL:
2030     case EM_SCROLLCARET:
2031     case EM_GETMODIFY:
2032     case EM_SETMODIFY:
2033     case EM_GETLINECOUNT:
2034     case EM_LINEINDEX:
2035     case EM_SETHANDLE:
2036     case EM_GETHANDLE:
2037     case EM_GETTHUMB:
2038     case EM_LINELENGTH:
2039     case EM_REPLACESEL:
2040     case EM_GETLINE:
2041     case EM_LIMITTEXT:
2042     case EM_CANUNDO:
2043     case EM_UNDO:
2044     case EM_FMTLINES:
2045     case EM_LINEFROMCHAR:
2046     case EM_SETTABSTOPS:
2047     case EM_SETPASSWORDCHAR:
2048     case EM_EMPTYUNDOBUFFER:
2049     case EM_GETFIRSTVISIBLELINE:
2050     case EM_SETREADONLY:
2051     case EM_SETWORDBREAKPROC:
2052     case EM_GETWORDBREAKPROC:
2053     case EM_GETPASSWORDCHAR:
2054         *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
2055         return 0;
2056
2057     case LB_CARETOFF:
2058     case LB_CARETON:
2059     case LB_DELETESTRING:
2060     case LB_GETANCHORINDEX:
2061     case LB_GETCARETINDEX:
2062     case LB_GETCOUNT:
2063     case LB_GETCURSEL:
2064     case LB_GETHORIZONTALEXTENT:
2065     case LB_GETITEMDATA:
2066     case LB_GETITEMHEIGHT:
2067     case LB_GETSEL:
2068     case LB_GETSELCOUNT:
2069     case LB_GETTEXTLEN:
2070     case LB_GETTOPINDEX:
2071     case LB_RESETCONTENT:
2072     case LB_SELITEMRANGE:
2073     case LB_SELITEMRANGEEX:
2074     case LB_SETANCHORINDEX:
2075     case LB_SETCARETINDEX:
2076     case LB_SETCOLUMNWIDTH:
2077     case LB_SETCURSEL:
2078     case LB_SETHORIZONTALEXTENT:
2079     case LB_SETITEMDATA:
2080     case LB_SETITEMHEIGHT:
2081     case LB_SETSEL:
2082     case LB_SETTOPINDEX:
2083         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2084         return 0;
2085     case CB_DELETESTRING:
2086     case CB_GETCOUNT:
2087     case CB_GETLBTEXTLEN:
2088     case CB_LIMITTEXT:
2089     case CB_RESETCONTENT:
2090     case CB_SETEDITSEL:
2091     case CB_GETCURSEL:
2092     case CB_SETCURSEL:
2093     case CB_SHOWDROPDOWN:
2094     case CB_SETITEMDATA:
2095     case CB_SETITEMHEIGHT:
2096     case CB_GETITEMHEIGHT:
2097     case CB_SETEXTENDEDUI:
2098     case CB_GETEXTENDEDUI:
2099     case CB_GETDROPPEDSTATE:
2100         *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2101         return 0;
2102     case CB_GETEDITSEL:
2103         *pmsg16 = CB_GETEDITSEL16;
2104         return 1;
2105
2106     case LB_ADDSTRING:
2107     case LB_FINDSTRING:
2108     case LB_FINDSTRINGEXACT:
2109     case LB_INSERTSTRING:
2110     case LB_SELECTSTRING:
2111     case LB_DIR:
2112     case LB_ADDFILE:
2113         *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2114         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2115         return 1;
2116
2117     case CB_ADDSTRING:
2118     case CB_FINDSTRING:
2119     case CB_FINDSTRINGEXACT:
2120     case CB_INSERTSTRING:
2121     case CB_SELECTSTRING:
2122     case CB_DIR:
2123         *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2124         *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2125         return 1;
2126
2127     case LB_GETITEMRECT:
2128         {
2129             RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2130             if (!rect) return -1;
2131             *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2132             *plparam = MapLS( rect );
2133         }
2134         *pmsg16 = LB_GETITEMRECT16;
2135         return 1;
2136     case LB_GETSELITEMS:
2137         {
2138             LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2139
2140             *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2141             if (!(items = HeapAlloc( GetProcessHeap(), 0,
2142                                      *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2143             *items++ = *plparam;  /* Store the previous lParam */
2144             *plparam = MapLS( items );
2145         }
2146         *pmsg16 = LB_GETSELITEMS16;
2147         return 1;
2148     case LB_SETTABSTOPS:
2149         if (wParam32)
2150         {
2151             INT i;
2152             LPINT16 stops;
2153             *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2154             if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2155                                      *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2156             for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2157             *plparam = MapLS( stops );
2158             return 1;
2159         }
2160         *pmsg16 = LB_SETTABSTOPS16;
2161         return 0;
2162
2163     case CB_GETDROPPEDCONTROLRECT:
2164         {
2165             RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2166             if (!rect) return -1;
2167             *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2168             *plparam = (LPARAM)MapLS(rect);
2169         }
2170         *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2171         return 1;
2172
2173     case LB_GETTEXT:
2174         *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2175         *pmsg16 = LB_GETTEXT16;
2176         return 1;
2177
2178     case CB_GETLBTEXT:
2179         *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2180         *pmsg16 = CB_GETLBTEXT16;
2181         return 1;
2182
2183     case EM_SETSEL:
2184         *pwparam16 = 0;
2185         *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2186         *pmsg16 = EM_SETSEL16;
2187         return 0;
2188
2189     case WM_ACTIVATE:
2190     case WM_CHARTOITEM:
2191     case WM_COMMAND:
2192     case WM_VKEYTOITEM:
2193         *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2194         return 0;
2195     case WM_HSCROLL:
2196     case WM_VSCROLL:
2197         *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2198         return 0;
2199     case WM_CTLCOLORMSGBOX:
2200     case WM_CTLCOLOREDIT:
2201     case WM_CTLCOLORLISTBOX:
2202     case WM_CTLCOLORBTN:
2203     case WM_CTLCOLORDLG:
2204     case WM_CTLCOLORSCROLLBAR:
2205     case WM_CTLCOLORSTATIC:
2206         *pmsg16  = WM_CTLCOLOR;
2207         *plparam = MAKELPARAM( (HWND16)*plparam,
2208                                (WORD)msg32 - WM_CTLCOLORMSGBOX );
2209         return 0;
2210     case WM_COMPAREITEM:
2211         {
2212             COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2213             COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2214             if (!cis) return -1;
2215             cis->CtlType    = (UINT16)cis32->CtlType;
2216             cis->CtlID      = (UINT16)cis32->CtlID;
2217             cis->hwndItem   = HWND_16( cis32->hwndItem );
2218             cis->itemID1    = (UINT16)cis32->itemID1;
2219             cis->itemData1  = cis32->itemData1;
2220             cis->itemID2    = (UINT16)cis32->itemID2;
2221             cis->itemData2  = cis32->itemData2;
2222             *plparam = MapLS( cis );
2223         }
2224         return 1;
2225     case WM_DELETEITEM:
2226         {
2227             DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2228             DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2229             if (!dis) return -1;
2230             dis->CtlType  = (UINT16)dis32->CtlType;
2231             dis->CtlID    = (UINT16)dis32->CtlID;
2232             dis->itemID   = (UINT16)dis32->itemID;
2233             dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2234                                                        : HWND_16( dis32->hwndItem );
2235             dis->itemData = dis32->itemData;
2236             *plparam = MapLS( dis );
2237         }
2238         return 1;
2239     case WM_DRAWITEM:
2240         {
2241             DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2242             DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2243             if (!dis) return -1;
2244             dis->CtlType       = (UINT16)dis32->CtlType;
2245             dis->CtlID         = (UINT16)dis32->CtlID;
2246             dis->itemID        = (UINT16)dis32->itemID;
2247             dis->itemAction    = (UINT16)dis32->itemAction;
2248             dis->itemState     = (UINT16)dis32->itemState;
2249             dis->hwndItem      = HWND_16( dis32->hwndItem );
2250             dis->hDC           = HDC_16(dis32->hDC);
2251             dis->itemData      = dis32->itemData;
2252             dis->rcItem.left   = dis32->rcItem.left;
2253             dis->rcItem.top    = dis32->rcItem.top;
2254             dis->rcItem.right  = dis32->rcItem.right;
2255             dis->rcItem.bottom = dis32->rcItem.bottom;
2256             *plparam = MapLS( dis );
2257         }
2258         return 1;
2259     case WM_MEASUREITEM:
2260         {
2261             MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2262             MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2263             if (!mis) return -1;
2264             mis->CtlType    = (UINT16)mis32->CtlType;
2265             mis->CtlID      = (UINT16)mis32->CtlID;
2266             mis->itemID     = (UINT16)mis32->itemID;
2267             mis->itemWidth  = (UINT16)mis32->itemWidth;
2268             mis->itemHeight = (UINT16)mis32->itemHeight;
2269             mis->itemData   = mis32->itemData;
2270             *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
2271             *plparam = MapLS( mis );
2272         }
2273         return 1;
2274     case WM_GETMINMAXINFO:
2275         {
2276             MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2277             if (!mmi) return -1;
2278             MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2279             *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
2280             *plparam = MapLS( mmi );
2281         }
2282         return 1;
2283     case WM_GETTEXT:
2284     case WM_ASKCBFORMATNAME:
2285         {
2286             LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2287             *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2288             if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2289             *str++ = *plparam;  /* Store the previous lParam */
2290             *plparam = MapLS( str );
2291         }
2292         return 1;
2293     case WM_MDICREATE:
2294         {
2295             MDICREATESTRUCT16 *cs;
2296             MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2297
2298             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2299             MDICREATESTRUCT32Ato16( cs32, cs );
2300             cs->szTitle = MapLS( cs32->szTitle );
2301             cs->szClass = MapLS( cs32->szClass );
2302             *plparam = MapLS( cs );
2303         }
2304         return 1;
2305     case WM_MDIGETACTIVE:
2306         return 1;
2307     case WM_MDISETMENU:
2308         *plparam   = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2309                                  (HMENU16)LOWORD(*plparam) );
2310         *pwparam16 = (*plparam == 0);
2311         return 0;
2312     case WM_MENUSELECT:
2313         if(HIWORD(wParam32) & MF_POPUP)
2314         {
2315             HMENU hmenu;
2316             if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2317             {
2318                 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2319                     *pwparam16=HMENU_16(hmenu);
2320             }
2321         }
2322         /* fall through */
2323     case WM_MENUCHAR:
2324         *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2325         return 0;
2326     case WM_MDIACTIVATE:
2327         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2328         {
2329             *pwparam16 = ((HWND)*plparam == hwnd);
2330             *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2331                                    (HWND16)LOWORD(wParam32) );
2332         }
2333         else
2334         {
2335             *pwparam16 = HWND_16( (HWND)wParam32 );
2336             *plparam = 0;
2337         }
2338         return 0;
2339     case WM_NCCALCSIZE:
2340         {
2341             NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2342             NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2343             if (!nc) return -1;
2344
2345             nc->rgrc[0].left   = nc32->rgrc[0].left;
2346             nc->rgrc[0].top    = nc32->rgrc[0].top;
2347             nc->rgrc[0].right  = nc32->rgrc[0].right;
2348             nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2349             if (wParam32)
2350             {
2351                 WINDOWPOS16 *wp;
2352                 nc->rgrc[1].left   = nc32->rgrc[1].left;
2353                 nc->rgrc[1].top    = nc32->rgrc[1].top;
2354                 nc->rgrc[1].right  = nc32->rgrc[1].right;
2355                 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2356                 nc->rgrc[2].left   = nc32->rgrc[2].left;
2357                 nc->rgrc[2].top    = nc32->rgrc[2].top;
2358                 nc->rgrc[2].right  = nc32->rgrc[2].right;
2359                 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2360                 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2361                 {
2362                     HeapFree( GetProcessHeap(), 0, nc );
2363                     return -1;
2364                 }
2365                 WINDOWPOS32to16( nc32->lppos, wp );
2366                 nc->lppos = MapLS( wp );
2367             }
2368             *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
2369             *plparam = MapLS( nc );
2370         }
2371         return 1;
2372     case WM_NCCREATE:
2373     case WM_CREATE:
2374         {
2375             CREATESTRUCT16 *cs;
2376             CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2377
2378             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2379             CREATESTRUCT32Ato16( cs32, cs );
2380             cs->lpszName  = MapLS( cs32->lpszName );
2381             cs->lpszClass = MapLS( cs32->lpszClass );
2382
2383             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2384             {
2385                 MDICREATESTRUCT16 *mdi_cs16;
2386                 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2387                 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2388                 if (!mdi_cs16)
2389                 {
2390                     HeapFree(GetProcessHeap(), 0, cs);
2391                     return -1;
2392                 }
2393                 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2394                 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2395                 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2396                 cs->lpCreateParams = MapLS( mdi_cs16 );
2397             }
2398             *plparam = MapLS( cs );
2399         }
2400         return 1;
2401     case WM_PARENTNOTIFY:
2402         if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2403             *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2404         /* else nothing to do */
2405         return 0;
2406     case WM_NOTIFY:
2407         *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2408         return 1;
2409     case WM_SETTEXT:
2410     case WM_WININICHANGE:
2411     case WM_DEVMODECHANGE:
2412         *plparam = MapLS( (LPSTR)*plparam );
2413         return 1;
2414     case WM_WINDOWPOSCHANGING:
2415     case WM_WINDOWPOSCHANGED:
2416         {
2417             WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2418             if (!wp) return -1;
2419             WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2420             *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
2421             *plparam = MapLS( wp );
2422         }
2423         return 1;
2424     case WM_GETDLGCODE:
2425          if (*plparam) {
2426             LPMSG msg32 = (LPMSG) *plparam;
2427             LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2428
2429             if (!msg16) return -1;
2430             msg16->hwnd = HWND_16( msg32->hwnd );
2431             msg16->lParam = msg32->lParam;
2432             msg16->time = msg32->time;
2433             msg16->pt.x = msg32->pt.x;
2434             msg16->pt.y = msg32->pt.y;
2435             /* this is right, right? */
2436             if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2437                          &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2438             {
2439                 HeapFree( GetProcessHeap(), 0, msg16 );
2440                 return -1;
2441             }
2442             *plparam = MapLS( msg16 );
2443             return 1;
2444         }
2445         return 0;
2446
2447     case WM_ACTIVATEAPP:
2448         if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2449         return 0;
2450     case WM_NEXTMENU:
2451         {
2452             MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2453             *plparam = (LPARAM)next->hmenuIn;
2454             return 1;
2455         }
2456     case WM_PAINTCLIPBOARD:
2457     case WM_SIZECLIPBOARD:
2458         FIXME_(msg)("message %04x needs translation\n", msg32 );
2459         return -1;
2460     /* following messages should not be sent to 16-bit apps */
2461     case WM_SIZING:
2462     case WM_MOVING:
2463     case WM_CAPTURECHANGED:
2464     case WM_STYLECHANGING:
2465     case WM_STYLECHANGED:
2466         return -1;
2467     case WM_DDE_INITIATE:
2468     case WM_DDE_TERMINATE:
2469     case WM_DDE_UNADVISE:
2470     case WM_DDE_REQUEST:
2471         *pwparam16 = HWND_16((HWND)wParam32);
2472         return 0;
2473     case WM_DDE_ADVISE:
2474     case WM_DDE_DATA:
2475     case WM_DDE_POKE:
2476         {
2477             UINT lo32, hi;
2478             HANDLE16    lo16 = 0;
2479
2480             *pwparam16 = HWND_16((HWND)wParam32);
2481             UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2482             if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2483                 return -1;
2484             *plparam = MAKELPARAM(lo16, hi);
2485         }
2486         return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
2487     case WM_DDE_ACK:
2488         {
2489             UINT        lo, hi;
2490             int         flag = 0;
2491             char        buf[2];
2492
2493             *pwparam16 = HWND_16((HWND)wParam32);
2494
2495             UnpackDDElParam(msg32, *plparam, &lo, &hi);
2496
2497             if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2498             if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2499             switch (flag)
2500             {
2501             case 0:
2502                 if (hi)
2503                 {
2504                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2505                     hi = 0;
2506                 }
2507                 break;
2508             case 1:
2509                 break; /* atom, nothing to do */
2510             case 3:
2511                 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2512                 /* fall thru */
2513             case 2:
2514                 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2515                 break;
2516             }
2517             *plparam = MAKELPARAM(lo, hi);
2518         }
2519         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2520     case WM_DDE_EXECUTE:
2521         *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2522         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2523     default:  /* No translation needed */
2524         return 0;
2525     }
2526 }
2527
2528
2529 /**********************************************************************
2530  *           WINPROC_UnmapMsg32ATo16
2531  *
2532  * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2533  */
2534 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2535                               MSGPARAM16* p16 )
2536 {
2537     switch(msg)
2538     {
2539     case SBM_GETRANGE:
2540         *(LPINT)wParam = LOWORD(p16->lResult);
2541         *(LPINT)lParam = HIWORD(p16->lResult);
2542         break;
2543
2544     case LB_ADDFILE:
2545     case LB_ADDSTRING:
2546     case LB_DIR:
2547     case LB_FINDSTRING:
2548     case LB_FINDSTRINGEXACT:
2549     case LB_INSERTSTRING:
2550     case LB_SELECTSTRING:
2551     case LB_GETTEXT:
2552     case CB_ADDSTRING:
2553     case CB_FINDSTRING:
2554     case CB_FINDSTRINGEXACT:
2555     case CB_INSERTSTRING:
2556     case CB_SELECTSTRING:
2557     case CB_DIR:
2558     case CB_GETLBTEXT:
2559     case WM_SETTEXT:
2560     case WM_WININICHANGE:
2561     case WM_DEVMODECHANGE:
2562         UnMapLS( (SEGPTR)p16->lParam );
2563         break;
2564     case LB_SETTABSTOPS:
2565     case WM_COMPAREITEM:
2566     case WM_DELETEITEM:
2567     case WM_DRAWITEM:
2568         {
2569             void *ptr = MapSL( p16->lParam );
2570             UnMapLS( p16->lParam );
2571             HeapFree( GetProcessHeap(), 0, ptr );
2572         }
2573         break;
2574     case CB_GETDROPPEDCONTROLRECT:
2575     case LB_GETITEMRECT:
2576         {
2577             RECT *r32;
2578             RECT16 *rect = MapSL(p16->lParam);
2579             UnMapLS( p16->lParam );
2580             p16->lParam = *(LPARAM *)(rect + 1);
2581             r32 = (RECT *)p16->lParam;
2582             r32->left   = rect->left;
2583             r32->top    = rect->top;
2584             r32->right  = rect->right;
2585             r32->bottom = rect->bottom;
2586             HeapFree( GetProcessHeap(), 0, rect );
2587         }
2588         break;
2589     case LB_GETSELITEMS:
2590         {
2591             INT i;
2592             LPINT16 items = MapSL(p16->lParam);
2593             UnMapLS( p16->lParam );
2594             p16->lParam = *((LPARAM *)items - 1);
2595             for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2596             HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2597         }
2598         break;
2599
2600     case CB_GETEDITSEL:
2601         if( wParam )
2602             *((PUINT)(wParam)) = LOWORD(p16->lResult);
2603         if( lParam )
2604             *((PUINT)(lParam)) = HIWORD(p16->lResult);  /* FIXME: substract 1? */
2605         break;
2606
2607     case WM_MEASUREITEM:
2608         {
2609             MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2610             MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2611             mis32->itemWidth  = mis->itemWidth;
2612             mis32->itemHeight = mis->itemHeight;
2613             UnMapLS( p16->lParam );
2614             HeapFree( GetProcessHeap(), 0, mis );
2615         }
2616         break;
2617     case WM_GETMINMAXINFO:
2618         {
2619             MINMAXINFO16 *mmi = MapSL(p16->lParam);
2620             UnMapLS( p16->lParam );
2621             p16->lParam = *(LPARAM *)(mmi + 1);
2622             MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2623             HeapFree( GetProcessHeap(), 0, mmi );
2624         }
2625         break;
2626     case WM_GETTEXT:
2627     case WM_ASKCBFORMATNAME:
2628         {
2629             LPSTR str = MapSL(p16->lParam);
2630             UnMapLS( p16->lParam );
2631             p16->lParam = *((LPARAM *)str - 1);
2632             lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2633             HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2634         }
2635         break;
2636     case WM_MDICREATE:
2637         {
2638             MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2639             UnMapLS( cs->szTitle );
2640             UnMapLS( cs->szClass );
2641             UnMapLS( p16->lParam );
2642             HeapFree( GetProcessHeap(), 0, cs );
2643         }
2644         break;
2645     case WM_MDIGETACTIVE:
2646         if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2647         p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2648         break;
2649     case WM_NCCALCSIZE:
2650         {
2651             NCCALCSIZE_PARAMS *nc32;
2652             NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2653             UnMapLS( p16->lParam );
2654             p16->lParam = *(LPARAM *)(nc + 1);
2655             nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2656             nc32->rgrc[0].left   = nc->rgrc[0].left;
2657             nc32->rgrc[0].top    = nc->rgrc[0].top;
2658             nc32->rgrc[0].right  = nc->rgrc[0].right;
2659             nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2660             if (p16->wParam)
2661             {
2662                 WINDOWPOS16 *pos = MapSL(nc->lppos);
2663                 UnMapLS( nc->lppos );
2664                 nc32->rgrc[1].left   = nc->rgrc[1].left;
2665                 nc32->rgrc[1].top    = nc->rgrc[1].top;
2666                 nc32->rgrc[1].right  = nc->rgrc[1].right;
2667                 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2668                 nc32->rgrc[2].left   = nc->rgrc[2].left;
2669                 nc32->rgrc[2].top    = nc->rgrc[2].top;
2670                 nc32->rgrc[2].right  = nc->rgrc[2].right;
2671                 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2672                 WINDOWPOS16to32( pos, nc32->lppos );
2673                 HeapFree( GetProcessHeap(), 0, pos );
2674             }
2675             HeapFree( GetProcessHeap(), 0, nc );
2676         }
2677         break;
2678     case WM_NCCREATE:
2679     case WM_CREATE:
2680         {
2681             CREATESTRUCT16 *cs = MapSL(p16->lParam);
2682             UnMapLS( p16->lParam );
2683             UnMapLS( cs->lpszName );
2684             UnMapLS( cs->lpszClass );
2685             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2686             {
2687                 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2688                 UnMapLS( cs->lpCreateParams );
2689                 UnMapLS( mdi_cs16->szTitle );
2690                 UnMapLS( mdi_cs16->szClass );
2691                 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2692             }
2693             HeapFree( GetProcessHeap(), 0, cs );
2694         }
2695         break;
2696     case WM_WINDOWPOSCHANGING:
2697     case WM_WINDOWPOSCHANGED:
2698         {
2699             WINDOWPOS16 *wp = MapSL(p16->lParam);
2700             UnMapLS( p16->lParam );
2701             p16->lParam = *(LPARAM *)(wp + 1);
2702             WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2703             HeapFree( GetProcessHeap(), 0, wp );
2704         }
2705         break;
2706     case WM_NOTIFY:
2707         UnMapLS(p16->lParam);
2708         break;
2709     case WM_GETDLGCODE:
2710         if (p16->lParam)
2711         {
2712             LPMSG16 msg16 = MapSL(p16->lParam);
2713             MSGPARAM16 msgp16;
2714             UnMapLS( p16->lParam );
2715             msgp16.wParam=msg16->wParam;
2716             msgp16.lParam=msg16->lParam;
2717             WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2718                     ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2719                     &msgp16 );
2720             HeapFree( GetProcessHeap(), 0, msg16 );
2721         }
2722         break;
2723     case WM_NEXTMENU:
2724         {
2725             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2726             next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2727             next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2728             p16->lResult = 0;
2729         }
2730         break;
2731     }
2732 }
2733
2734
2735 /**********************************************************************
2736  *           WINPROC_MapMsg32WTo16
2737  *
2738  * Map a message from 32-bit Unicode to 16-bit.
2739  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2740  */
2741 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2742                              UINT16 *pmsg16, WPARAM16 *pwparam16,
2743                              LPARAM *plparam )
2744 {
2745     BYTE ch;
2746     WCHAR wch;
2747
2748     *pmsg16    = LOWORD(msg32);
2749     *pwparam16 = LOWORD(wParam32);
2750     switch(msg32)
2751     {
2752     case LB_ADDSTRING:
2753     case LB_FINDSTRING:
2754     case LB_FINDSTRINGEXACT:
2755     case LB_INSERTSTRING:
2756     case LB_SELECTSTRING:
2757     case LB_DIR:
2758     case LB_ADDFILE:
2759         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2760         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2761         return 1;
2762
2763     case CB_ADDSTRING:
2764     case CB_FINDSTRING:
2765     case CB_FINDSTRINGEXACT:
2766     case CB_INSERTSTRING:
2767     case CB_SELECTSTRING:
2768     case CB_DIR:
2769         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2770         *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2771         return 1;
2772
2773     case WM_NCCREATE:
2774     case WM_CREATE:
2775         {
2776             CREATESTRUCT16 *cs;
2777             CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2778
2779             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2780             CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2781             cs->lpszName  = map_str_32W_to_16( cs32->lpszName );
2782             cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2783
2784             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2785             {
2786                 MDICREATESTRUCT16 *mdi_cs16;
2787                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2788                 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2789                 if (!mdi_cs16)
2790                 {
2791                     HeapFree(GetProcessHeap(), 0, cs);
2792                     return -1;
2793                 }
2794                 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2795                 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2796                 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2797                 cs->lpCreateParams = MapLS(mdi_cs16);
2798             }
2799             *plparam   = MapLS(cs);
2800         }
2801         return 1;
2802     case WM_MDICREATE:
2803         {
2804             MDICREATESTRUCT16 *cs;
2805             MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2806
2807             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2808             MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2809             cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2810             cs->szClass = map_str_32W_to_16( cs32->szClass );
2811             *plparam   = MapLS(cs);
2812         }
2813         return 1;
2814     case WM_SETTEXT:
2815     case WM_WININICHANGE:
2816     case WM_DEVMODECHANGE:
2817         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2818         return 1;
2819     case LB_GETTEXT:
2820         if ( WINPROC_TestLBForStr( hwnd ))
2821         {
2822             LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2823             if (!str) return -1;
2824             *pmsg16    = LB_GETTEXT16;
2825             *plparam   = (LPARAM)MapLS(str);
2826         }
2827         return 1;
2828     case CB_GETLBTEXT:
2829         if ( WINPROC_TestCBForStr( hwnd ))
2830         {
2831             LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2832             if (!str) return -1;
2833             *pmsg16    = CB_GETLBTEXT16;
2834             *plparam   = (LPARAM)MapLS(str);
2835         }
2836         return 1;
2837
2838     case WM_CHARTOITEM:
2839         wch = LOWORD(wParam32);
2840         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2841         *pwparam16 = ch;
2842         *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2843         return 0;
2844     case WM_MENUCHAR:
2845         wch = LOWORD(wParam32);
2846         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2847         *pwparam16 = ch;
2848         *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2849         return 0;
2850     case WM_CHAR:
2851     case WM_DEADCHAR:
2852     case WM_SYSCHAR:
2853     case WM_SYSDEADCHAR:
2854         wch = wParam32;
2855         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2856         *pwparam16 = ch;
2857         return 0;
2858     case WM_IME_CHAR:
2859         {
2860             BYTE ch[2];
2861
2862             wch = wParam32;
2863             if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
2864                 *pwparam16 = (ch[0] << 8) | ch[1];
2865             else
2866                 *pwparam16 = ch[0];
2867         }
2868         return 0;
2869
2870     default:  /* No Unicode translation needed (?) */
2871         return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2872                                       pwparam16, plparam );
2873     }
2874 }
2875
2876
2877 /**********************************************************************
2878  *           WINPROC_UnmapMsg32WTo16
2879  *
2880  * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2881  */
2882 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2883                               MSGPARAM16* p16 )
2884 {
2885     switch(msg)
2886     {
2887     case LB_ADDSTRING:
2888     case LB_FINDSTRING:
2889     case LB_FINDSTRINGEXACT:
2890     case LB_INSERTSTRING:
2891     case LB_SELECTSTRING:
2892     case LB_DIR:
2893     case LB_ADDFILE:
2894     case CB_ADDSTRING:
2895     case CB_FINDSTRING:
2896     case CB_FINDSTRINGEXACT:
2897     case CB_INSERTSTRING:
2898     case CB_SELECTSTRING:
2899     case CB_DIR:
2900     case WM_SETTEXT:
2901     case WM_WININICHANGE:
2902     case WM_DEVMODECHANGE:
2903         unmap_str_32W_to_16( p16->lParam );
2904         break;
2905     case WM_NCCREATE:
2906     case WM_CREATE:
2907         {
2908             CREATESTRUCT16 *cs = MapSL(p16->lParam);
2909             UnMapLS( p16->lParam );
2910             unmap_str_32W_to_16( cs->lpszName );
2911             unmap_str_32W_to_16( cs->lpszClass );
2912
2913             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2914             {
2915                 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2916                 UnMapLS( cs->lpCreateParams );
2917                 unmap_str_32W_to_16(mdi_cs16->szTitle);
2918                 unmap_str_32W_to_16(mdi_cs16->szClass);
2919                 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2920             }
2921             HeapFree( GetProcessHeap(), 0, cs );
2922         }
2923         break;
2924     case WM_MDICREATE:
2925         {
2926             MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2927             UnMapLS( p16->lParam );
2928             unmap_str_32W_to_16( cs->szTitle );
2929             unmap_str_32W_to_16( cs->szClass );
2930             HeapFree( GetProcessHeap(), 0, cs );
2931         }
2932         break;
2933     case WM_GETTEXT:
2934     case WM_ASKCBFORMATNAME:
2935         {
2936             LPSTR str = MapSL(p16->lParam);
2937             UnMapLS( p16->lParam );
2938             p16->lParam = *((LPARAM *)str - 1);
2939             MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2940             p16->lResult = strlenW( (LPWSTR)p16->lParam );
2941             HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2942         }
2943         break;
2944     case LB_GETTEXT:
2945         if ( WINPROC_TestLBForStr( hwnd ))
2946         {
2947             LPSTR str = MapSL(p16->lParam);
2948             UnMapLS( p16->lParam );
2949             p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2950             HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2951         }
2952         break;
2953     case CB_GETLBTEXT:
2954         if ( WINPROC_TestCBForStr( hwnd ))
2955         {
2956             LPSTR str = MapSL(p16->lParam);
2957             UnMapLS( p16->lParam );
2958             p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2959             HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2960         }
2961         break;
2962     default:
2963         WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2964         break;
2965     }
2966 }
2967
2968
2969 /**********************************************************************
2970  *           WINPROC_CallProc32ATo32W
2971  *
2972  * Call a window procedure, translating args from Ansi to Unicode.
2973  */
2974 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
2975                                          UINT msg, WPARAM wParam,
2976                                          LPARAM lParam )
2977 {
2978     LRESULT result;
2979     int unmap;
2980
2981     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2982         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2983
2984     if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2985         ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2986                        SPY_GetMsgName(msg, hwnd), wParam, lParam );
2987         return 0;
2988     }
2989     result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2990     if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
2991     return result;
2992 }
2993
2994
2995 /**********************************************************************
2996  *           WINPROC_CallProc32WTo32A_fast
2997  *
2998  */
2999 static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
3000                                            UINT msg, WPARAM wParam,
3001                                            LPARAM lParam, LRESULT *result )
3002 {
3003     switch(msg)
3004     {
3005     case WM_NCCREATE:
3006     case WM_CREATE:
3007         {   /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
3008              * at this point.
3009              */
3010             char buffer[1024];
3011             char *cls = buffer, *name;
3012             CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
3013             CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
3014             DWORD name_lenA, name_lenW, class_lenA, class_lenW;
3015
3016             class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
3017             RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
3018
3019             if (csW->lpszName)
3020             {
3021                 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
3022                 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
3023             }
3024             else
3025                 name_lenW = name_lenA = 0;
3026
3027             if (class_lenA + name_lenA + 2 > sizeof(buffer))
3028             {
3029                 cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
3030                 if (!cls) return FALSE;
3031             }
3032
3033             RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
3034             cls[class_lenA] = 0;
3035             csA.lpszClass = cls;
3036
3037             if (csW->lpszName)
3038             {
3039                 name = cls + class_lenA + 1;
3040                 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3041                 name[name_lenA] = 0;
3042                 csA.lpszName = name;
3043             }
3044
3045             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3046             {
3047                 MDICREATESTRUCTA mdi_cs;
3048
3049                 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3050                 mdi_cs.szTitle = csA.lpszName;
3051                 mdi_cs.szClass = csA.lpszClass;
3052                 csA.lpCreateParams = &mdi_cs;
3053             }
3054
3055             lParam = (LPARAM)&csA;
3056             *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);
3057
3058             if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
3059         }
3060         return TRUE;
3061
3062     default:
3063         return FALSE;
3064     }
3065 }
3066
3067 /**********************************************************************
3068  *           WINPROC_CallProc32WTo32A
3069  *
3070  * Call a window procedure, translating args from Unicode to Ansi.
3071  */
3072 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
3073                                          UINT msg, WPARAM wParam,
3074                                          LPARAM lParam )
3075 {
3076     LRESULT result;
3077     int unmap;
3078
3079     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3080         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3081
3082     if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
3083         return result;
3084
3085     if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3086         ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3087                        SPY_GetMsgName(msg, hwnd), wParam, lParam );
3088         return 0;
3089     }
3090     result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3091     if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3092     return result;
3093 }
3094
3095
3096 /**********************************************************************
3097  *           __wine_call_wndproc_32A   (USER.1010)
3098  */
3099 LRESULT WINAPI __wine_call_wndproc_32A( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3100                                         WNDPROC func )
3101 {
3102     LRESULT result;
3103     UINT msg32;
3104     WPARAM wParam32;
3105     HWND hwnd32 = WIN_Handle32( hwnd );
3106
3107     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3108                  func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3109
3110     if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3111         return 0;
3112     result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3113     return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3114 }
3115
3116
3117 /**********************************************************************
3118  *           __wine_call_wndproc_32W   (USER.1011)
3119  */
3120 LRESULT WINAPI  __wine_call_wndproc_32W( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3121                                          WNDPROC func )
3122 {
3123     LRESULT result;
3124     UINT msg32;
3125     WPARAM wParam32;
3126     HWND hwnd32 = WIN_Handle32( hwnd );
3127
3128     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3129                  func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3130
3131     if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3132         return 0;
3133     result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3134     return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result );
3135 }
3136
3137
3138 /**********************************************************************
3139  *           WINPROC_CallProc32ATo16
3140  *
3141  * Call a 16-bit window procedure, translating the 32-bit args.
3142  */
3143 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
3144                                                UINT msg, WPARAM wParam,
3145                                                LPARAM lParam )
3146 {
3147     UINT16 msg16;
3148     MSGPARAM16 mp16;
3149
3150     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3151         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3152
3153     mp16.lParam = lParam;
3154     if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3155         return 0;
3156     mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3157                                           mp16.wParam, mp16.lParam );
3158     WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3159     return mp16.lResult;
3160 }
3161
3162
3163 /**********************************************************************
3164  *           WINPROC_CallProc32WTo16
3165  *
3166  * Call a 16-bit window procedure, translating the 32-bit args.
3167  */
3168 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
3169                                                UINT msg, WPARAM wParam,
3170                                                LPARAM lParam )
3171 {
3172     UINT16 msg16;
3173     MSGPARAM16 mp16;
3174
3175     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3176         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3177
3178     mp16.lParam = lParam;
3179     if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
3180                                &mp16.lParam ) == -1)
3181         return 0;
3182     mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3183                                           mp16.wParam, mp16.lParam );
3184     WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3185     return mp16.lResult;
3186 }
3187
3188
3189 /**********************************************************************
3190  *              CallWindowProc (USER.122)
3191  */
3192 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3193                                  WPARAM16 wParam, LPARAM lParam )
3194 {
3195     WINDOWPROC *proc;
3196
3197     if (!func) return 0;
3198
3199     if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3200         return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3201
3202 #if testing
3203     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_16 );
3204     return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3205 #endif
3206
3207     switch(proc->type)
3208     {
3209     case WIN_PROC_16:
3210         if (!proc->thunk.t_from32.proc) return 0;
3211         return WINPROC_CallWndProc16( proc->thunk.t_from32.proc,
3212                                       hwnd, msg, wParam, lParam );
3213     case WIN_PROC_32A:
3214         if (!proc->thunk.t_from16.proc) return 0;
3215         return __wine_call_wndproc_32A( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3216     case WIN_PROC_32W:
3217         if (!proc->thunk.t_from16.proc) return 0;
3218         return __wine_call_wndproc_32W( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3219     default:
3220         WARN_(relay)("Invalid proc %p\n", proc );
3221         return 0;
3222     }
3223 }
3224
3225
3226 /**********************************************************************
3227  *              CallWindowProcA (USER32.@)
3228  *
3229  * The CallWindowProc() function invokes the windows procedure _func_,
3230  * with _hwnd_ as the target window, the message specified by _msg_, and
3231  * the message parameters _wParam_ and _lParam_.
3232  *
3233  * Some kinds of argument conversion may be done, I'm not sure what.
3234  *
3235  * CallWindowProc() may be used for windows subclassing. Use
3236  * SetWindowLong() to set a new windows procedure for windows of the
3237  * subclass, and handle subclassed messages in the new windows
3238  * procedure. The new windows procedure may then use CallWindowProc()
3239  * with _func_ set to the parent class's windows procedure to dispatch
3240  * the message to the superclass.
3241  *
3242  * RETURNS
3243  *
3244  *    The return value is message dependent.
3245  *
3246  * CONFORMANCE
3247  *
3248  *   ECMA-234, Win32
3249  */
3250 LRESULT WINAPI CallWindowProcA(
3251     WNDPROC func,  /* [in] window procedure */
3252     HWND hwnd,     /* [in] target window */
3253     UINT msg,      /* [in] message */
3254     WPARAM wParam, /* [in] message dependent parameter */
3255     LPARAM lParam  /* [in] message dependent parameter */
3256 ) {
3257     WINDOWPROC *proc;
3258
3259     if (!func) return 0;
3260
3261     if (!(proc = WINPROC_GetPtr( func )))
3262         return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3263
3264 #if testing
3265     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32A );
3266     return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3267 #endif
3268
3269     switch(proc->type)
3270     {
3271     case WIN_PROC_16:
3272         if (!proc->thunk.t_from32.proc) return 0;
3273         return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
3274                                         hwnd, msg, wParam, lParam );
3275     case WIN_PROC_32A:
3276         if (!proc->thunk.t_from16.proc) return 0;
3277         return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3278                                       hwnd, msg, wParam, lParam );
3279     case WIN_PROC_32W:
3280         if (!proc->thunk.t_from16.proc) return 0;
3281         return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
3282                                          hwnd, msg, wParam, lParam );
3283     default:
3284         WARN_(relay)("Invalid proc %p\n", proc );
3285         return 0;
3286     }
3287 }
3288
3289
3290 /**********************************************************************
3291  *              CallWindowProcW (USER32.@)
3292  */
3293 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3294                                   WPARAM wParam, LPARAM lParam )
3295 {
3296     WINDOWPROC *proc;
3297
3298     if (!func) return 0;
3299
3300     if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3301         return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3302
3303 #if testing
3304     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32W );
3305     return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3306 #endif
3307
3308     switch(proc->type)
3309     {
3310     case WIN_PROC_16:
3311         if (!proc->thunk.t_from32.proc) return 0;
3312         return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
3313                                         hwnd, msg, wParam, lParam );
3314     case WIN_PROC_32A:
3315         if (!proc->thunk.t_from16.proc) return 0;
3316         return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
3317                                          hwnd, msg, wParam, lParam );
3318     case WIN_PROC_32W:
3319         if (!proc->thunk.t_from16.proc) return 0;
3320         return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3321                                       hwnd, msg, wParam, lParam );
3322     default:
3323         WARN_(relay)("Invalid proc %p\n", proc );
3324         return 0;
3325     }
3326 }