user32: Move a number of 16-bit cursor/icon functions to user16.c.
[wine] / dlls / user32 / msg16.c
1 /*
2  * 16-bit messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "wine/winuser16.h"
29 #include "wownt32.h"
30 #include "winerror.h"
31 #include "win.h"
32 #include "dde.h"
33 #include "user_private.h"
34 #include "controls.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msg);
38
39 DWORD USER16_AlertableWait = 0;
40
41 static struct wow_handlers32 wow_handlers32;
42
43 static LRESULT cwp_hook_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
44                                   LRESULT *result, void *arg )
45 {
46     CWPSTRUCT cwp;
47
48     cwp.hwnd    = hwnd;
49     cwp.message = msg;
50     cwp.wParam  = wp;
51     cwp.lParam  = lp;
52     *result = 0;
53     return HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp, FALSE );
54 }
55
56 static LRESULT send_message_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
57                                       LRESULT *result, void *arg )
58 {
59     *result = SendMessageA( hwnd, msg, wp, lp );
60     return *result;
61 }
62
63 static LRESULT post_message_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
64                                       LRESULT *result, void *arg )
65 {
66     *result = 0;
67     return PostMessageA( hwnd, msg, wp, lp );
68 }
69
70 static LRESULT post_thread_message_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
71                                              LRESULT *result, void *arg )
72 {
73     DWORD_PTR tid = (DWORD_PTR)arg;
74     *result = 0;
75     return PostThreadMessageA( tid, msg, wp, lp );
76 }
77
78 static LRESULT get_message_callback( HWND16 hwnd, UINT16 msg, WPARAM16 wp, LPARAM lp,
79                                      LRESULT *result, void *arg )
80 {
81     MSG16 *msg16 = arg;
82
83     msg16->hwnd    = hwnd;
84     msg16->message = msg;
85     msg16->wParam  = wp;
86     msg16->lParam  = lp;
87     *result = 0;
88     return 0;
89 }
90
91 static LRESULT defdlg_proc_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
92                                      LRESULT *result, void *arg )
93 {
94     *result = DefDlgProcA( hwnd, msg, wp, lp );
95     return *result;
96 }
97
98 static LRESULT call_window_proc_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
99                                           LRESULT *result, void *arg )
100 {
101     WNDPROC proc = arg;
102     *result = CallWindowProcA( proc, hwnd, msg, wp, lp );
103     return *result;
104 }
105
106
107 /**********************************************************************
108  * Support for window procedure thunks
109  */
110
111 #include "pshpack1.h"
112 typedef struct
113 {
114     BYTE        popl_eax;        /* popl  %eax (return address) */
115     BYTE        pushl_func;      /* pushl $proc */
116     WNDPROC     proc;
117     BYTE        pushl_eax;       /* pushl %eax */
118     BYTE        ljmp;            /* ljmp relay*/
119     DWORD       relay_offset;    /* __wine_call_wndproc */
120     WORD        relay_sel;
121 } WINPROC_THUNK;
122 #include "poppack.h"
123
124 #define WINPROC_HANDLE (~0u >> 16)
125 #define MAX_WINPROCS32 4096
126 #define MAX_WINPROCS16 1024
127
128 static WNDPROC16 winproc16_array[MAX_WINPROCS16];
129 static unsigned int winproc16_used;
130
131 static WINPROC_THUNK *thunk_array;
132 static UINT thunk_selector;
133
134 /* return the window proc index for a given handle, or -1 for an invalid handle
135  * indices 0 .. MAX_WINPROCS32-1 are for 32-bit procs,
136  * indices MAX_WINPROCS32 .. MAX_WINPROCS32+MAX_WINPROCS16-1 for 16-bit procs */
137 static int winproc_to_index( WNDPROC16 handle )
138 {
139     unsigned int index;
140
141     if (HIWORD(handle) == thunk_selector)
142     {
143         index = LOWORD(handle) / sizeof(WINPROC_THUNK);
144         /* check alignment */
145         if (index * sizeof(WINPROC_THUNK) != LOWORD(handle)) return -1;
146         /* check array limits */
147         if (index >= MAX_WINPROCS32) return -1;
148     }
149     else
150     {
151         index = LOWORD(handle);
152         if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return -1;
153         /* check array limits */
154         if (index >= winproc16_used + MAX_WINPROCS32) return -1;
155     }
156     return index;
157 }
158
159 /* allocate a 16-bit thunk for an existing window proc */
160 static WNDPROC16 alloc_win16_thunk( WNDPROC handle )
161 {
162     static FARPROC16 relay;
163     WINPROC_THUNK *thunk;
164     UINT index = LOWORD( handle );
165
166     if (index >= MAX_WINPROCS32) return (WNDPROC16)handle;  /* already a 16-bit proc */
167
168     if (!thunk_array)  /* allocate the array and its selector */
169     {
170         LDT_ENTRY entry;
171
172         assert( MAX_WINPROCS16 * sizeof(WINPROC_THUNK) <= 0x10000 );
173
174         if (!(thunk_selector = wine_ldt_alloc_entries(1))) return NULL;
175         if (!(thunk_array = VirtualAlloc( NULL, MAX_WINPROCS16 * sizeof(WINPROC_THUNK), MEM_COMMIT,
176                                           PAGE_EXECUTE_READWRITE ))) return NULL;
177         wine_ldt_set_base( &entry, thunk_array );
178         wine_ldt_set_limit( &entry, MAX_WINPROCS16 * sizeof(WINPROC_THUNK) - 1 );
179         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
180         wine_ldt_set_entry( thunk_selector, &entry );
181         relay = GetProcAddress16( GetModuleHandle16("user"), "__wine_call_wndproc" );
182     }
183
184     thunk = &thunk_array[index];
185     thunk->popl_eax     = 0x58;   /* popl  %eax */
186     thunk->pushl_func   = 0x68;   /* pushl $proc */
187     thunk->proc         = handle;
188     thunk->pushl_eax    = 0x50;   /* pushl %eax */
189     thunk->ljmp         = 0xea;   /* ljmp   relay*/
190     thunk->relay_offset = OFFSETOF(relay);
191     thunk->relay_sel    = SELECTOROF(relay);
192     return (WNDPROC16)MAKESEGPTR( thunk_selector, index * sizeof(WINPROC_THUNK) );
193 }
194
195 /**********************************************************************
196  *           WINPROC_AllocProc16
197  */
198 WNDPROC WINPROC_AllocProc16( WNDPROC16 func )
199 {
200     int index;
201     WNDPROC ret;
202
203     if (!func) return NULL;
204
205     /* check if the function is already a win proc */
206     if ((index = winproc_to_index( func )) != -1)
207         return (WNDPROC)(ULONG_PTR)(index | (WINPROC_HANDLE << 16));
208
209     /* then check if we already have a winproc for that function */
210     for (index = 0; index < winproc16_used; index++)
211         if (winproc16_array[index] == func) goto done;
212
213     if (winproc16_used >= MAX_WINPROCS16)
214     {
215         FIXME( "too many winprocs, cannot allocate one for 16-bit %p\n", func );
216         return NULL;
217     }
218     winproc16_array[winproc16_used++] = func;
219
220 done:
221     ret = (WNDPROC)(ULONG_PTR)((index + MAX_WINPROCS32) | (WINPROC_HANDLE << 16));
222     TRACE( "returning %p for %p/16-bit (%d/%d used)\n",
223            ret, func, winproc16_used, MAX_WINPROCS16 );
224     return ret;
225 }
226
227 /**********************************************************************
228  *           WINPROC_GetProc16
229  *
230  * Get a window procedure pointer that can be passed to the Windows program.
231  */
232 WNDPROC16 WINPROC_GetProc16( WNDPROC proc, BOOL unicode )
233 {
234     WNDPROC winproc = wow_handlers32.alloc_winproc( proc, unicode );
235
236     if ((ULONG_PTR)winproc >> 16 != WINPROC_HANDLE) return (WNDPROC16)winproc;
237     return alloc_win16_thunk( winproc );
238 }
239
240 /* call a 16-bit window procedure */
241 static LRESULT call_window_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
242                                    LRESULT *result, void *arg )
243 {
244     WNDPROC16 func = arg;
245     int index = winproc_to_index( func );
246     CONTEXT86 context;
247     size_t size = 0;
248     struct
249     {
250         WORD params[5];
251         union
252         {
253             CREATESTRUCT16 cs16;
254             DRAWITEMSTRUCT16 dis16;
255             COMPAREITEMSTRUCT16 cis16;
256         } u;
257     } args;
258
259     USER_CheckNotLock();
260
261     if (index >= MAX_WINPROCS32) func = winproc16_array[index - MAX_WINPROCS32];
262
263     /* Window procedures want ax = hInstance, ds = es = ss */
264
265     memset(&context, 0, sizeof(context));
266     context.SegDs = context.SegEs = SELECTOROF(NtCurrentTeb()->WOW32Reserved);
267     context.SegFs = wine_get_fs();
268     context.SegGs = wine_get_gs();
269     if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
270     context.SegCs = SELECTOROF(func);
271     context.Eip   = OFFSETOF(func);
272     context.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME, bp);
273
274     if (lParam)
275     {
276         /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
277            work if structures passed in lParam are placed in the stack/data
278            segment. Programmers easily make the mistake of converting lParam
279            to a near rather than a far pointer, since Windows apparently
280            allows this. We copy the structures to the 16 bit stack; this is
281            ugly but makes these programs work. */
282         switch (msg)
283         {
284           case WM_CREATE:
285           case WM_NCCREATE:
286             size = sizeof(CREATESTRUCT16); break;
287           case WM_DRAWITEM:
288             size = sizeof(DRAWITEMSTRUCT16); break;
289           case WM_COMPAREITEM:
290             size = sizeof(COMPAREITEMSTRUCT16); break;
291         }
292         if (size)
293         {
294             memcpy( &args.u, MapSL(lParam), size );
295             lParam = PtrToUlong(NtCurrentTeb()->WOW32Reserved) - size;
296         }
297     }
298
299     args.params[4] = hwnd;
300     args.params[3] = msg;
301     args.params[2] = wParam;
302     args.params[1] = HIWORD(lParam);
303     args.params[0] = LOWORD(lParam);
304     WOWCallback16Ex( 0, WCB16_REGS, sizeof(args.params) + size, &args, (DWORD *)&context );
305     *result = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
306     return *result;
307 }
308
309 static LRESULT call_dialog_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wp, LPARAM lp,
310                                    LRESULT *result, void *arg )
311 {
312     LRESULT ret = call_window_proc16( hwnd, msg, wp, lp, result, arg );
313     *result = GetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT );
314     return LOWORD(ret);
315 }
316
317 static LRESULT call_window_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
318                                        LRESULT *result, void *arg )
319 {
320     return WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wp, lp, result, arg );
321 }
322
323 static LRESULT call_dialog_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
324                                        LRESULT *result, void *arg )
325 {
326     return WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wp, lp, result, arg );
327 }
328
329
330
331 /**********************************************************************
332  * Support for Edit word break proc thunks
333  */
334
335 #define MAX_THUNKS 32
336
337 #include <pshpack1.h>
338 static struct word_break_thunk
339 {
340     BYTE                popl_eax;       /* popl  %eax (return address) */
341     BYTE                pushl_proc16;   /* pushl proc16 */
342     EDITWORDBREAKPROC16 proc16;
343     BYTE                pushl_eax;      /* pushl %eax */
344     BYTE                jmp;            /* ljmp call_word_break_proc16 */
345     DWORD               callback;
346 } *word_break_thunks;
347 #include <poppack.h>
348
349 /**********************************************************************
350  *           call_word_break_proc16
351  */
352 static INT16 CALLBACK call_word_break_proc16( SEGPTR proc16, LPSTR text, INT index, INT count, INT action )
353 {
354     SEGPTR segptr;
355     WORD args[5];
356     DWORD result;
357
358     segptr = MapLS( text );
359     args[4] = SELECTOROF(segptr);
360     args[3] = OFFSETOF(segptr);
361     args[2] = index;
362     args[1] = count;
363     args[0] = action;
364     WOWCallback16Ex( proc16, WCB16_PASCAL, sizeof(args), args, &result );
365     UnMapLS( segptr );
366     return LOWORD(result);
367 }
368
369 /******************************************************************
370  *              add_word_break_thunk
371  */
372 static struct word_break_thunk *add_word_break_thunk( EDITWORDBREAKPROC16 proc16 )
373 {
374     struct word_break_thunk *thunk;
375
376     if (!word_break_thunks)
377     {
378         word_break_thunks = VirtualAlloc( NULL, MAX_THUNKS * sizeof(*thunk),
379                                           MEM_COMMIT, PAGE_EXECUTE_READWRITE );
380         if (!word_break_thunks) return NULL;
381
382         for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
383         {
384             thunk->popl_eax     = 0x58;   /* popl  %eax */
385             thunk->pushl_proc16 = 0x68;   /* pushl proc16 */
386             thunk->pushl_eax    = 0x50;   /* pushl %eax */
387             thunk->jmp          = 0xe9;   /* jmp call_word_break_proc16 */
388             thunk->callback     = (char *)call_word_break_proc16 - (char *)(&thunk->callback + 1);
389         }
390     }
391     for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
392         if (thunk->proc16 == proc16) return thunk;
393
394     for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
395     {
396         if (thunk->proc16) continue;
397         thunk->proc16 = proc16;
398         return thunk;
399     }
400     FIXME("Out of word break thunks\n");
401     return NULL;
402 }
403
404 /******************************************************************
405  *              get_word_break_thunk
406  */
407 static EDITWORDBREAKPROC16 get_word_break_thunk( EDITWORDBREAKPROCA proc )
408 {
409     struct word_break_thunk *thunk = (struct word_break_thunk *)proc;
410     if (word_break_thunks && thunk >= word_break_thunks && thunk < &word_break_thunks[MAX_THUNKS])
411         return thunk->proc16;
412     return NULL;
413 }
414
415
416 /***********************************************************************
417  * Support for 16<->32 message mapping
418  */
419
420 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
421 {
422     if (size >= need) return static_buffer;
423     return HeapAlloc( GetProcessHeap(), 0, need );
424 }
425
426 static inline void free_buffer( void *static_buffer, void *buffer )
427 {
428     if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
429 }
430
431 static void RECT16to32( const RECT16 *from, RECT *to )
432 {
433     to->left   = from->left;
434     to->top    = from->top;
435     to->right  = from->right;
436     to->bottom = from->bottom;
437 }
438
439 static void RECT32to16( const RECT *from, RECT16 *to )
440 {
441     to->left   = from->left;
442     to->top    = from->top;
443     to->right  = from->right;
444     to->bottom = from->bottom;
445 }
446
447 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
448 {
449     to->ptReserved.x     = from->ptReserved.x;
450     to->ptReserved.y     = from->ptReserved.y;
451     to->ptMaxSize.x      = from->ptMaxSize.x;
452     to->ptMaxSize.y      = from->ptMaxSize.y;
453     to->ptMaxPosition.x  = from->ptMaxPosition.x;
454     to->ptMaxPosition.y  = from->ptMaxPosition.y;
455     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
456     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
457     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
458     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
459 }
460
461 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
462 {
463     to->ptReserved.x     = from->ptReserved.x;
464     to->ptReserved.y     = from->ptReserved.y;
465     to->ptMaxSize.x      = from->ptMaxSize.x;
466     to->ptMaxSize.y      = from->ptMaxSize.y;
467     to->ptMaxPosition.x  = from->ptMaxPosition.x;
468     to->ptMaxPosition.y  = from->ptMaxPosition.y;
469     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
470     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
471     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
472     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
473 }
474
475 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
476 {
477     to->hwnd            = HWND_16(from->hwnd);
478     to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
479     to->x               = from->x;
480     to->y               = from->y;
481     to->cx              = from->cx;
482     to->cy              = from->cy;
483     to->flags           = from->flags;
484 }
485
486 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
487 {
488     to->hwnd            = WIN_Handle32(from->hwnd);
489     to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
490                            HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
491     to->x               = from->x;
492     to->y               = from->y;
493     to->cx              = from->cx;
494     to->cy              = from->cy;
495     to->flags           = from->flags;
496 }
497
498 /* The strings are not copied */
499 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
500 {
501     to->lpCreateParams = (SEGPTR)from->lpCreateParams;
502     to->hInstance      = HINSTANCE_16(from->hInstance);
503     to->hMenu          = HMENU_16(from->hMenu);
504     to->hwndParent     = HWND_16(from->hwndParent);
505     to->cy             = from->cy;
506     to->cx             = from->cx;
507     to->y              = from->y;
508     to->x              = from->x;
509     to->style          = from->style;
510     to->dwExStyle      = from->dwExStyle;
511 }
512
513 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
514
515 {
516     to->lpCreateParams = (LPVOID)from->lpCreateParams;
517     to->hInstance      = HINSTANCE_32(from->hInstance);
518     to->hMenu          = HMENU_32(from->hMenu);
519     to->hwndParent     = WIN_Handle32(from->hwndParent);
520     to->cy             = from->cy;
521     to->cx             = from->cx;
522     to->y              = from->y;
523     to->x              = from->x;
524     to->style          = from->style;
525     to->dwExStyle      = from->dwExStyle;
526     to->lpszName       = MapSL(from->lpszName);
527     to->lpszClass      = MapSL(from->lpszClass);
528 }
529
530 /* The strings are not copied */
531 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
532 {
533     to->hOwner = HINSTANCE_16(from->hOwner);
534     to->x      = from->x;
535     to->y      = from->y;
536     to->cx     = from->cx;
537     to->cy     = from->cy;
538     to->style  = from->style;
539     to->lParam = from->lParam;
540 }
541
542 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
543 {
544     to->hOwner = HINSTANCE_32(from->hOwner);
545     to->x      = from->x;
546     to->y      = from->y;
547     to->cx     = from->cx;
548     to->cy     = from->cy;
549     to->style  = from->style;
550     to->lParam = from->lParam;
551     to->szTitle = MapSL(from->szTitle);
552     to->szClass = MapSL(from->szClass);
553 }
554
555 static UINT_PTR convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
556 {
557     HANDLE      dst;
558     UINT        sz = GlobalSize16(src);
559     LPSTR       ptr16, ptr32;
560
561     if (!(dst = GlobalAlloc(flags, sz)))
562         return 0;
563     ptr16 = GlobalLock16(src);
564     ptr32 = GlobalLock(dst);
565     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
566     GlobalUnlock16(src);
567     GlobalUnlock(dst);
568
569     return (UINT_PTR)dst;
570 }
571
572 static HANDLE16 convert_handle_32_to_16(UINT_PTR src, unsigned int flags)
573 {
574     HANDLE16    dst;
575     UINT        sz = GlobalSize((HANDLE)src);
576     LPSTR       ptr16, ptr32;
577
578     if (!(dst = GlobalAlloc16(flags, sz)))
579         return 0;
580     ptr32 = GlobalLock((HANDLE)src);
581     ptr16 = GlobalLock16(dst);
582     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
583     GlobalUnlock((HANDLE)src);
584     GlobalUnlock16(dst);
585
586     return dst;
587 }
588
589 /**********************************************************************
590  *           WINPROC_CallProc16To32A
591  */
592 LRESULT WINPROC_CallProc16To32A( winproc_callback_t callback, HWND16 hwnd, UINT16 msg,
593                                  WPARAM16 wParam, LPARAM lParam, LRESULT *result, void *arg )
594 {
595     LRESULT ret = 0;
596     HWND hwnd32 = WIN_Handle32( hwnd );
597
598     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
599                  hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
600
601     switch(msg)
602     {
603     case WM_NCCREATE:
604     case WM_CREATE:
605         {
606             CREATESTRUCT16 *cs16 = MapSL(lParam);
607             CREATESTRUCTA cs;
608             MDICREATESTRUCTA mdi_cs;
609
610             CREATESTRUCT16to32A( cs16, &cs );
611             if (GetWindowLongW(hwnd32, GWL_EXSTYLE) & WS_EX_MDICHILD)
612             {
613                 MDICREATESTRUCT16 *mdi_cs16 = MapSL(cs16->lpCreateParams);
614                 MDICREATESTRUCT16to32A(mdi_cs16, &mdi_cs);
615                 cs.lpCreateParams = &mdi_cs;
616             }
617             ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
618             CREATESTRUCT32Ato16( &cs, cs16 );
619         }
620         break;
621     case WM_MDICREATE:
622         {
623             MDICREATESTRUCT16 *cs16 = MapSL(lParam);
624             MDICREATESTRUCTA cs;
625
626             MDICREATESTRUCT16to32A( cs16, &cs );
627             ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
628             MDICREATESTRUCT32Ato16( &cs, cs16 );
629         }
630         break;
631     case WM_MDIACTIVATE:
632         if (lParam)
633             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32( HIWORD(lParam) ),
634                             (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
635         else /* message sent to MDI client */
636             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
637         break;
638     case WM_MDIGETACTIVE:
639         {
640             BOOL maximized = FALSE;
641             ret = callback( hwnd32, msg, wParam, (LPARAM)&maximized, result, arg );
642             *result = MAKELRESULT( LOWORD(*result), maximized );
643         }
644         break;
645     case WM_MDISETMENU:
646         ret = callback( hwnd32, wParam ? WM_MDIREFRESHMENU : WM_MDISETMENU,
647                         (WPARAM)HMENU_32(LOWORD(lParam)), (LPARAM)HMENU_32(HIWORD(lParam)),
648                         result, arg );
649         break;
650     case WM_GETMINMAXINFO:
651         {
652             MINMAXINFO16 *mmi16 = MapSL(lParam);
653             MINMAXINFO mmi;
654
655             MINMAXINFO16to32( mmi16, &mmi );
656             ret = callback( hwnd32, msg, wParam, (LPARAM)&mmi, result, arg );
657             MINMAXINFO32to16( &mmi, mmi16 );
658         }
659         break;
660     case WM_WINDOWPOSCHANGING:
661     case WM_WINDOWPOSCHANGED:
662         {
663             WINDOWPOS16 *winpos16 = MapSL(lParam);
664             WINDOWPOS winpos;
665
666             WINDOWPOS16to32( winpos16, &winpos );
667             ret = callback( hwnd32, msg, wParam, (LPARAM)&winpos, result, arg );
668             WINDOWPOS32to16( &winpos, winpos16 );
669         }
670         break;
671     case WM_NCCALCSIZE:
672         {
673             NCCALCSIZE_PARAMS16 *nc16 = MapSL(lParam);
674             NCCALCSIZE_PARAMS nc;
675             WINDOWPOS winpos;
676
677             RECT16to32( &nc16->rgrc[0], &nc.rgrc[0] );
678             if (wParam)
679             {
680                 RECT16to32( &nc16->rgrc[1], &nc.rgrc[1] );
681                 RECT16to32( &nc16->rgrc[2], &nc.rgrc[2] );
682                 WINDOWPOS16to32( MapSL(nc16->lppos), &winpos );
683                 nc.lppos = &winpos;
684             }
685             ret = callback( hwnd32, msg, wParam, (LPARAM)&nc, result, arg );
686             RECT32to16( &nc.rgrc[0], &nc16->rgrc[0] );
687             if (wParam)
688             {
689                 RECT32to16( &nc.rgrc[1], &nc16->rgrc[1] );
690                 RECT32to16( &nc.rgrc[2], &nc16->rgrc[2] );
691                 WINDOWPOS32to16( &winpos, MapSL(nc16->lppos) );
692             }
693         }
694         break;
695     case WM_COMPAREITEM:
696         {
697             COMPAREITEMSTRUCT16* cis16 = MapSL(lParam);
698             COMPAREITEMSTRUCT cis;
699             cis.CtlType    = cis16->CtlType;
700             cis.CtlID      = cis16->CtlID;
701             cis.hwndItem   = WIN_Handle32( cis16->hwndItem );
702             cis.itemID1    = cis16->itemID1;
703             cis.itemData1  = cis16->itemData1;
704             cis.itemID2    = cis16->itemID2;
705             cis.itemData2  = cis16->itemData2;
706             cis.dwLocaleId = 0;  /* FIXME */
707             ret = callback( hwnd32, msg, wParam, (LPARAM)&cis, result, arg );
708         }
709         break;
710     case WM_DELETEITEM:
711         {
712             DELETEITEMSTRUCT16* dis16 = MapSL(lParam);
713             DELETEITEMSTRUCT dis;
714             dis.CtlType  = dis16->CtlType;
715             dis.CtlID    = dis16->CtlID;
716             dis.hwndItem = WIN_Handle32( dis16->hwndItem );
717             dis.itemData = dis16->itemData;
718             ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
719         }
720         break;
721     case WM_MEASUREITEM:
722         {
723             MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
724             MEASUREITEMSTRUCT mis;
725             mis.CtlType    = mis16->CtlType;
726             mis.CtlID      = mis16->CtlID;
727             mis.itemID     = mis16->itemID;
728             mis.itemWidth  = mis16->itemWidth;
729             mis.itemHeight = mis16->itemHeight;
730             mis.itemData   = mis16->itemData;
731             ret = callback( hwnd32, msg, wParam, (LPARAM)&mis, result, arg );
732             mis16->itemWidth  = (UINT16)mis.itemWidth;
733             mis16->itemHeight = (UINT16)mis.itemHeight;
734         }
735         break;
736     case WM_DRAWITEM:
737         {
738             DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
739             DRAWITEMSTRUCT dis;
740             dis.CtlType       = dis16->CtlType;
741             dis.CtlID         = dis16->CtlID;
742             dis.itemID        = dis16->itemID;
743             dis.itemAction    = dis16->itemAction;
744             dis.itemState     = dis16->itemState;
745             dis.hwndItem      = (dis.CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
746                                                           : WIN_Handle32( dis16->hwndItem );
747             dis.hDC           = HDC_32(dis16->hDC);
748             dis.itemData      = dis16->itemData;
749             dis.rcItem.left   = dis16->rcItem.left;
750             dis.rcItem.top    = dis16->rcItem.top;
751             dis.rcItem.right  = dis16->rcItem.right;
752             dis.rcItem.bottom = dis16->rcItem.bottom;
753             ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
754         }
755         break;
756     case WM_COPYDATA:
757         {
758             COPYDATASTRUCT16 *cds16 = MapSL(lParam);
759             COPYDATASTRUCT cds;
760             cds.dwData = cds16->dwData;
761             cds.cbData = cds16->cbData;
762             cds.lpData = MapSL(cds16->lpData);
763             ret = callback( hwnd32, msg, wParam, (LPARAM)&cds, result, arg );
764         }
765         break;
766     case WM_GETDLGCODE:
767         if (lParam)
768         {
769             MSG16 *msg16 = MapSL(lParam);
770             MSG msg32;
771             msg32.hwnd    = WIN_Handle32( msg16->hwnd );
772             msg32.message = msg16->message;
773             msg32.wParam  = msg16->wParam;
774             msg32.lParam  = msg16->lParam;
775             msg32.time    = msg16->time;
776             msg32.pt.x    = msg16->pt.x;
777             msg32.pt.y    = msg16->pt.y;
778             ret = callback( hwnd32, msg, wParam, (LPARAM)&msg32, result, arg );
779         }
780         else
781             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
782         break;
783     case WM_NEXTMENU:
784         {
785             MDINEXTMENU next;
786             next.hmenuIn   = (HMENU)lParam;
787             next.hmenuNext = 0;
788             next.hwndNext  = 0;
789             ret = callback( hwnd32, msg, wParam, (LPARAM)&next, result, arg );
790             *result = MAKELONG( HMENU_16(next.hmenuNext), HWND_16(next.hwndNext) );
791         }
792         break;
793     case WM_ACTIVATE:
794     case WM_CHARTOITEM:
795     case WM_COMMAND:
796     case WM_VKEYTOITEM:
797         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
798                         (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
799         break;
800     case WM_HSCROLL:
801     case WM_VSCROLL:
802         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
803                         (LPARAM)WIN_Handle32( HIWORD(lParam) ), result, arg );
804         break;
805     case WM_CTLCOLOR:
806         if (HIWORD(lParam) <= CTLCOLOR_STATIC)
807             ret = callback( hwnd32, WM_CTLCOLORMSGBOX + HIWORD(lParam),
808                             (WPARAM)HDC_32(wParam), (LPARAM)WIN_Handle32( LOWORD(lParam) ),
809                             result, arg );
810         break;
811     case WM_GETTEXT:
812     case WM_SETTEXT:
813     case WM_WININICHANGE:
814     case WM_DEVMODECHANGE:
815     case WM_ASKCBFORMATNAME:
816     case WM_NOTIFY:
817         ret = callback( hwnd32, msg, wParam, (LPARAM)MapSL(lParam), result, arg );
818         break;
819     case WM_MENUCHAR:
820         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
821                         (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
822         break;
823     case WM_MENUSELECT:
824         if((LOWORD(lParam) & MF_POPUP) && (LOWORD(lParam) != 0xFFFF))
825         {
826             HMENU hmenu = HMENU_32(HIWORD(lParam));
827             UINT pos = MENU_FindSubMenu( &hmenu, HMENU_32(wParam) );
828             if (pos == 0xffff) pos = 0;  /* NO_SELECTED_ITEM */
829             wParam = pos;
830         }
831         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
832                         (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
833         break;
834     case WM_PARENTNOTIFY:
835         if ((wParam == WM_CREATE) || (wParam == WM_DESTROY))
836             ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
837                             (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
838         else
839             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
840         break;
841     case WM_ACTIVATEAPP:
842         /* We need this when SetActiveWindow sends a Sendmessage16() to
843          * a 32-bit window. Might be superfluous with 32-bit interprocess
844          * message queues. */
845         if (lParam) lParam = HTASK_32(lParam);
846         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
847         break;
848     case WM_DDE_INITIATE:
849     case WM_DDE_TERMINATE:
850     case WM_DDE_UNADVISE:
851     case WM_DDE_REQUEST:
852         ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
853         break;
854     case WM_DDE_ADVISE:
855     case WM_DDE_DATA:
856     case WM_DDE_POKE:
857         {
858             HANDLE16 lo16 = LOWORD(lParam);
859             UINT_PTR lo32 = 0;
860             if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE))) break;
861             lParam = PackDDElParam( msg, lo32, HIWORD(lParam) );
862             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
863         }
864         break; /* FIXME don't know how to free allocated memory (handle)  !! */
865     case WM_DDE_ACK:
866         {
867             UINT_PTR lo = LOWORD(lParam);
868             UINT_PTR hi = HIWORD(lParam);
869             int flag = 0;
870             char buf[2];
871
872             if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
873             if (GlobalSize16(hi) != 0) flag |= 2;
874             switch (flag)
875             {
876             case 0:
877                 if (hi)
878                 {
879                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
880                     hi = 0;
881                 }
882                 break;
883             case 1:
884                 break; /* atom, nothing to do */
885             case 3:
886                 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
887                 /* fall thru */
888             case 2:
889                 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
890                 break;
891             }
892             lParam = PackDDElParam( WM_DDE_ACK, lo, hi );
893             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
894         }
895         break; /* FIXME don't know how to free allocated memory (handle) !! */
896     case WM_DDE_EXECUTE:
897         lParam = convert_handle_16_to_32( lParam, GMEM_DDESHARE );
898         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
899         break; /* FIXME don't know how to free allocated memory (handle) !! */
900     case WM_PAINTCLIPBOARD:
901     case WM_SIZECLIPBOARD:
902         FIXME_(msg)( "message %04x needs translation\n", msg );
903         break;
904     default:
905         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
906         break;
907     }
908     return ret;
909 }
910
911
912 /**********************************************************************
913  *           WINPROC_CallProc32ATo16
914  *
915  * Call a 16-bit window procedure, translating the 32-bit args.
916  */
917 LRESULT WINPROC_CallProc32ATo16( winproc_callback16_t callback, HWND hwnd, UINT msg,
918                                  WPARAM wParam, LPARAM lParam, LRESULT *result, void *arg )
919 {
920     LRESULT ret = 0;
921
922     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
923                 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
924
925     switch(msg)
926     {
927     case WM_NCCREATE:
928     case WM_CREATE:
929         {
930             CREATESTRUCTA *cs32 = (CREATESTRUCTA *)lParam;
931             CREATESTRUCT16 cs;
932             MDICREATESTRUCT16 mdi_cs16;
933             BOOL mdi_child = (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD);
934
935             CREATESTRUCT32Ato16( cs32, &cs );
936             cs.lpszName  = MapLS( cs32->lpszName );
937             cs.lpszClass = MapLS( cs32->lpszClass );
938
939             if (mdi_child)
940             {
941                 MDICREATESTRUCTA *mdi_cs = cs32->lpCreateParams;
942                 MDICREATESTRUCT32Ato16( mdi_cs, &mdi_cs16 );
943                 mdi_cs16.szTitle = MapLS( mdi_cs->szTitle );
944                 mdi_cs16.szClass = MapLS( mdi_cs->szClass );
945                 cs.lpCreateParams = MapLS( &mdi_cs16 );
946             }
947             lParam = MapLS( &cs );
948             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
949             UnMapLS( lParam );
950             UnMapLS( cs.lpszName );
951             UnMapLS( cs.lpszClass );
952             if (mdi_child)
953             {
954                 UnMapLS( cs.lpCreateParams );
955                 UnMapLS( mdi_cs16.szTitle );
956                 UnMapLS( mdi_cs16.szClass );
957             }
958         }
959         break;
960     case WM_MDICREATE:
961         {
962             MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)lParam;
963             MDICREATESTRUCT16 cs;
964
965             MDICREATESTRUCT32Ato16( cs32, &cs );
966             cs.szTitle = MapLS( cs32->szTitle );
967             cs.szClass = MapLS( cs32->szClass );
968             lParam = MapLS( &cs );
969             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
970             UnMapLS( lParam );
971             UnMapLS( cs.szTitle );
972             UnMapLS( cs.szClass );
973         }
974         break;
975     case WM_MDIACTIVATE:
976         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
977             ret = callback( HWND_16(hwnd), msg, ((HWND)lParam == hwnd),
978                             MAKELPARAM( LOWORD(lParam), LOWORD(wParam) ), result, arg );
979         else
980             ret = callback( HWND_16(hwnd), msg, HWND_16( wParam ), 0, result, arg );
981         break;
982     case WM_MDIGETACTIVE:
983         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
984         if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(*result);
985         *result = (LRESULT)WIN_Handle32( LOWORD(*result) );
986         break;
987     case WM_MDISETMENU:
988         ret = callback( HWND_16(hwnd), msg, (lParam == 0),
989                         MAKELPARAM( LOWORD(wParam), LOWORD(lParam) ), result, arg );
990         break;
991     case WM_GETMINMAXINFO:
992         {
993             MINMAXINFO *mmi32 = (MINMAXINFO *)lParam;
994             MINMAXINFO16 mmi;
995
996             MINMAXINFO32to16( mmi32, &mmi );
997             lParam = MapLS( &mmi );
998             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
999             UnMapLS( lParam );
1000             MINMAXINFO16to32( &mmi, mmi32 );
1001         }
1002         break;
1003     case WM_NCCALCSIZE:
1004         {
1005             NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)lParam;
1006             NCCALCSIZE_PARAMS16 nc;
1007             WINDOWPOS16 winpos;
1008
1009             RECT32to16( &nc32->rgrc[0], &nc.rgrc[0] );
1010             if (wParam)
1011             {
1012                 RECT32to16( &nc32->rgrc[1], &nc.rgrc[1] );
1013                 RECT32to16( &nc32->rgrc[2], &nc.rgrc[2] );
1014                 WINDOWPOS32to16( nc32->lppos, &winpos );
1015                 nc.lppos = MapLS( &winpos );
1016             }
1017             lParam = MapLS( &nc );
1018             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1019             UnMapLS( lParam );
1020             RECT16to32( &nc.rgrc[0], &nc32->rgrc[0] );
1021             if (wParam)
1022             {
1023                 RECT16to32( &nc.rgrc[1], &nc32->rgrc[1] );
1024                 RECT16to32( &nc.rgrc[2], &nc32->rgrc[2] );
1025                 WINDOWPOS16to32( &winpos, nc32->lppos );
1026                 UnMapLS( nc.lppos );
1027             }
1028         }
1029         break;
1030     case WM_WINDOWPOSCHANGING:
1031     case WM_WINDOWPOSCHANGED:
1032         {
1033             WINDOWPOS *winpos32 = (WINDOWPOS *)lParam;
1034             WINDOWPOS16 winpos;
1035
1036             WINDOWPOS32to16( winpos32, &winpos );
1037             lParam = MapLS( &winpos );
1038             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1039             UnMapLS( lParam );
1040             WINDOWPOS16to32( &winpos, winpos32 );
1041         }
1042         break;
1043     case WM_COMPAREITEM:
1044         {
1045             COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)lParam;
1046             COMPAREITEMSTRUCT16 cis;
1047             cis.CtlType    = cis32->CtlType;
1048             cis.CtlID      = cis32->CtlID;
1049             cis.hwndItem   = HWND_16( cis32->hwndItem );
1050             cis.itemID1    = cis32->itemID1;
1051             cis.itemData1  = cis32->itemData1;
1052             cis.itemID2    = cis32->itemID2;
1053             cis.itemData2  = cis32->itemData2;
1054             lParam = MapLS( &cis );
1055             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1056             UnMapLS( lParam );
1057         }
1058         break;
1059     case WM_DELETEITEM:
1060         {
1061             DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)lParam;
1062             DELETEITEMSTRUCT16 dis;
1063             dis.CtlType  = dis32->CtlType;
1064             dis.CtlID    = dis32->CtlID;
1065             dis.itemID   = dis32->itemID;
1066             dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
1067                                                      : HWND_16( dis32->hwndItem );
1068             dis.itemData = dis32->itemData;
1069             lParam = MapLS( &dis );
1070             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1071             UnMapLS( lParam );
1072         }
1073         break;
1074     case WM_DRAWITEM:
1075         {
1076             DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)lParam;
1077             DRAWITEMSTRUCT16 dis;
1078             dis.CtlType       = dis32->CtlType;
1079             dis.CtlID         = dis32->CtlID;
1080             dis.itemID        = dis32->itemID;
1081             dis.itemAction    = dis32->itemAction;
1082             dis.itemState     = dis32->itemState;
1083             dis.hwndItem      = HWND_16( dis32->hwndItem );
1084             dis.hDC           = HDC_16(dis32->hDC);
1085             dis.itemData      = dis32->itemData;
1086             dis.rcItem.left   = dis32->rcItem.left;
1087             dis.rcItem.top    = dis32->rcItem.top;
1088             dis.rcItem.right  = dis32->rcItem.right;
1089             dis.rcItem.bottom = dis32->rcItem.bottom;
1090             lParam = MapLS( &dis );
1091             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1092             UnMapLS( lParam );
1093         }
1094         break;
1095     case WM_MEASUREITEM:
1096         {
1097             MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)lParam;
1098             MEASUREITEMSTRUCT16 mis;
1099             mis.CtlType    = mis32->CtlType;
1100             mis.CtlID      = mis32->CtlID;
1101             mis.itemID     = mis32->itemID;
1102             mis.itemWidth  = mis32->itemWidth;
1103             mis.itemHeight = mis32->itemHeight;
1104             mis.itemData   = mis32->itemData;
1105             lParam = MapLS( &mis );
1106             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1107             UnMapLS( lParam );
1108             mis32->itemWidth  = mis.itemWidth;
1109             mis32->itemHeight = mis.itemHeight;
1110         }
1111         break;
1112     case WM_COPYDATA:
1113         {
1114             COPYDATASTRUCT *cds32 = (COPYDATASTRUCT *)lParam;
1115             COPYDATASTRUCT16 cds;
1116
1117             cds.dwData = cds32->dwData;
1118             cds.cbData = cds32->cbData;
1119             cds.lpData = MapLS( cds32->lpData );
1120             lParam = MapLS( &cds );
1121             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1122             UnMapLS( lParam );
1123             UnMapLS( cds.lpData );
1124         }
1125         break;
1126     case WM_GETDLGCODE:
1127         if (lParam)
1128         {
1129             MSG *msg32 = (MSG *)lParam;
1130             MSG16 msg16;
1131
1132             msg16.hwnd    = HWND_16( msg32->hwnd );
1133             msg16.message = msg32->message;
1134             msg16.wParam  = msg32->wParam;
1135             msg16.lParam  = msg32->lParam;
1136             msg16.time    = msg32->time;
1137             msg16.pt.x    = msg32->pt.x;
1138             msg16.pt.y    = msg32->pt.y;
1139             lParam = MapLS( &msg16 );
1140             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1141             UnMapLS( lParam );
1142         }
1143         else
1144             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1145         break;
1146     case WM_NEXTMENU:
1147         {
1148             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1149             ret = callback( HWND_16(hwnd), msg, wParam, (LPARAM)next->hmenuIn, result, arg );
1150             next->hmenuNext = HMENU_32( LOWORD(*result) );
1151             next->hwndNext  = WIN_Handle32( HIWORD(*result) );
1152             *result = 0;
1153         }
1154         break;
1155     case WM_GETTEXT:
1156     case WM_ASKCBFORMATNAME:
1157         wParam = min( wParam, 0xff80 ); /* Must be < 64K */
1158         /* fall through */
1159     case WM_NOTIFY:
1160     case WM_SETTEXT:
1161     case WM_WININICHANGE:
1162     case WM_DEVMODECHANGE:
1163         lParam = MapLS( (void *)lParam );
1164         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1165         UnMapLS( lParam );
1166         break;
1167     case WM_ACTIVATE:
1168     case WM_CHARTOITEM:
1169     case WM_COMMAND:
1170     case WM_VKEYTOITEM:
1171         ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ),
1172                         result, arg );
1173         break;
1174     case WM_HSCROLL:
1175     case WM_VSCROLL:
1176         ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( HIWORD(wParam), (HWND16)lParam ),
1177                         result, arg );
1178         break;
1179     case WM_CTLCOLORMSGBOX:
1180     case WM_CTLCOLOREDIT:
1181     case WM_CTLCOLORLISTBOX:
1182     case WM_CTLCOLORBTN:
1183     case WM_CTLCOLORDLG:
1184     case WM_CTLCOLORSCROLLBAR:
1185     case WM_CTLCOLORSTATIC:
1186         ret = callback( HWND_16(hwnd), WM_CTLCOLOR, wParam,
1187                         MAKELPARAM( (HWND16)lParam, msg - WM_CTLCOLORMSGBOX ), result, arg );
1188         break;
1189     case WM_MENUSELECT:
1190         if(HIWORD(wParam) & MF_POPUP)
1191         {
1192             HMENU hmenu;
1193             if ((HIWORD(wParam) != 0xffff) || lParam)
1194             {
1195                 if ((hmenu = GetSubMenu( (HMENU)lParam, LOWORD(wParam) )))
1196                 {
1197                     ret = callback( HWND_16(hwnd), msg, HMENU_16(hmenu),
1198                                     MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1199                     break;
1200                 }
1201             }
1202         }
1203         /* fall through */
1204     case WM_MENUCHAR:
1205         ret = callback( HWND_16(hwnd), msg, wParam,
1206                         MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1207         break;
1208     case WM_PARENTNOTIFY:
1209         if ((LOWORD(wParam) == WM_CREATE) || (LOWORD(wParam) == WM_DESTROY))
1210             ret = callback( HWND_16(hwnd), msg, wParam,
1211                             MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ), result, arg );
1212         else
1213             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1214         break;
1215     case WM_ACTIVATEAPP:
1216         ret = callback( HWND_16(hwnd), msg, wParam, HTASK_16( lParam ), result, arg );
1217         break;
1218     case WM_PAINT:
1219         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
1220             ret = callback( HWND_16(hwnd), WM_PAINTICON, 1, lParam, result, arg );
1221         else
1222             ret = callback( HWND_16(hwnd), WM_PAINT, wParam, lParam, result, arg );
1223         break;
1224     case WM_ERASEBKGND:
1225         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON )) msg = WM_ICONERASEBKGND;
1226         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1227         break;
1228     case WM_DDE_INITIATE:
1229     case WM_DDE_TERMINATE:
1230     case WM_DDE_UNADVISE:
1231     case WM_DDE_REQUEST:
1232         ret = callback( HWND_16(hwnd), msg, HWND_16(wParam), lParam, result, arg );
1233         break;
1234     case WM_DDE_ADVISE:
1235     case WM_DDE_DATA:
1236     case WM_DDE_POKE:
1237         {
1238             UINT_PTR lo32, hi;
1239             HANDLE16 lo16 = 0;
1240
1241             UnpackDDElParam( msg, lParam, &lo32, &hi );
1242             if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE))) break;
1243             ret = callback( HWND_16(hwnd), msg, HWND_16(wParam),
1244                             MAKELPARAM(lo16, hi), result, arg );
1245         }
1246         break; /* FIXME don't know how to free allocated memory (handle)  !! */
1247     case WM_DDE_ACK:
1248         {
1249             UINT_PTR lo, hi;
1250             int flag = 0;
1251             char buf[2];
1252
1253             UnpackDDElParam( msg, lParam, &lo, &hi );
1254
1255             if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
1256             if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
1257             switch (flag)
1258             {
1259             case 0:
1260                 if (hi)
1261                 {
1262                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1263                     hi = 0;
1264                 }
1265                 break;
1266             case 1:
1267                 break; /* atom, nothing to do */
1268             case 3:
1269                 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
1270                 /* fall thru */
1271             case 2:
1272                 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
1273                 break;
1274             }
1275             ret = callback( HWND_16(hwnd), msg, HWND_16(wParam),
1276                             MAKELPARAM(lo, hi), result, arg );
1277         }
1278         break; /* FIXME don't know how to free allocated memory (handle) !! */
1279     case WM_DDE_EXECUTE:
1280         lParam = convert_handle_32_to_16(lParam, GMEM_DDESHARE);
1281         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1282         break; /* FIXME don't know how to free allocated memory (handle) !! */
1283     case SBM_SETRANGE:
1284         ret = callback( HWND_16(hwnd), SBM_SETRANGE16, 0, MAKELPARAM(wParam, lParam), result, arg );
1285         break;
1286     case SBM_GETRANGE:
1287         ret = callback( HWND_16(hwnd), SBM_GETRANGE16, wParam, lParam, result, arg );
1288         *(LPINT)wParam = LOWORD(*result);
1289         *(LPINT)lParam = HIWORD(*result);
1290         break;
1291     case BM_GETCHECK:
1292     case BM_SETCHECK:
1293     case BM_GETSTATE:
1294     case BM_SETSTATE:
1295     case BM_SETSTYLE:
1296         ret = callback( HWND_16(hwnd), msg + BM_GETCHECK16 - BM_GETCHECK, wParam, lParam, result, arg );
1297         break;
1298     case EM_GETSEL:
1299     case EM_GETRECT:
1300     case EM_SETRECT:
1301     case EM_SETRECTNP:
1302     case EM_SCROLL:
1303     case EM_LINESCROLL:
1304     case EM_SCROLLCARET:
1305     case EM_GETMODIFY:
1306     case EM_SETMODIFY:
1307     case EM_GETLINECOUNT:
1308     case EM_LINEINDEX:
1309     case EM_SETHANDLE:
1310     case EM_GETHANDLE:
1311     case EM_GETTHUMB:
1312     case EM_LINELENGTH:
1313     case EM_REPLACESEL:
1314     case EM_GETLINE:
1315     case EM_LIMITTEXT:
1316     case EM_CANUNDO:
1317     case EM_UNDO:
1318     case EM_FMTLINES:
1319     case EM_LINEFROMCHAR:
1320     case EM_SETTABSTOPS:
1321     case EM_SETPASSWORDCHAR:
1322     case EM_EMPTYUNDOBUFFER:
1323     case EM_GETFIRSTVISIBLELINE:
1324     case EM_SETREADONLY:
1325     case EM_SETWORDBREAKPROC:
1326     case EM_GETWORDBREAKPROC:
1327     case EM_GETPASSWORDCHAR:
1328         ret = callback( HWND_16(hwnd), msg + EM_GETSEL16 - EM_GETSEL, wParam, lParam, result, arg );
1329         break;
1330     case EM_SETSEL:
1331         ret = callback( HWND_16(hwnd), EM_SETSEL16, 0, MAKELPARAM( wParam, lParam ), result, arg );
1332         break;
1333     case LB_CARETOFF:
1334     case LB_CARETON:
1335     case LB_DELETESTRING:
1336     case LB_GETANCHORINDEX:
1337     case LB_GETCARETINDEX:
1338     case LB_GETCOUNT:
1339     case LB_GETCURSEL:
1340     case LB_GETHORIZONTALEXTENT:
1341     case LB_GETITEMDATA:
1342     case LB_GETITEMHEIGHT:
1343     case LB_GETSEL:
1344     case LB_GETSELCOUNT:
1345     case LB_GETTEXTLEN:
1346     case LB_GETTOPINDEX:
1347     case LB_RESETCONTENT:
1348     case LB_SELITEMRANGE:
1349     case LB_SELITEMRANGEEX:
1350     case LB_SETANCHORINDEX:
1351     case LB_SETCARETINDEX:
1352     case LB_SETCOLUMNWIDTH:
1353     case LB_SETCURSEL:
1354     case LB_SETHORIZONTALEXTENT:
1355     case LB_SETITEMDATA:
1356     case LB_SETITEMHEIGHT:
1357     case LB_SETSEL:
1358     case LB_SETTOPINDEX:
1359         ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
1360         break;
1361     case LB_ADDSTRING:
1362     case LB_FINDSTRING:
1363     case LB_FINDSTRINGEXACT:
1364     case LB_INSERTSTRING:
1365     case LB_SELECTSTRING:
1366     case LB_GETTEXT:
1367     case LB_DIR:
1368     case LB_ADDFILE:
1369         lParam = MapLS( (LPSTR)lParam );
1370         ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
1371         UnMapLS( lParam );
1372         break;
1373     case LB_GETSELITEMS:
1374         {
1375             INT *items32 = (INT *)lParam;
1376             INT16 *items, buffer[512];
1377             unsigned int i;
1378
1379             wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
1380             if (!(items = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
1381             lParam = MapLS( items );
1382             ret = callback( HWND_16(hwnd), LB_GETSELITEMS16, wParam, lParam, result, arg );
1383             UnMapLS( lParam );
1384             for (i = 0; i < wParam; i++) items32[i] = items[i];
1385             free_buffer( buffer, items );
1386         }
1387         break;
1388     case LB_SETTABSTOPS:
1389         if (wParam)
1390         {
1391             INT *stops32 = (INT *)lParam;
1392             INT16 *stops, buffer[512];
1393             unsigned int i;
1394
1395             wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
1396             if (!(stops = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
1397             for (i = 0; i < wParam; i++) stops[i] = stops32[i];
1398             lParam = MapLS( stops );
1399             ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
1400             UnMapLS( lParam );
1401             free_buffer( buffer, stops );
1402         }
1403         else ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
1404         break;
1405     case CB_DELETESTRING:
1406     case CB_GETCOUNT:
1407     case CB_GETLBTEXTLEN:
1408     case CB_LIMITTEXT:
1409     case CB_RESETCONTENT:
1410     case CB_SETEDITSEL:
1411     case CB_GETCURSEL:
1412     case CB_SETCURSEL:
1413     case CB_SHOWDROPDOWN:
1414     case CB_SETITEMDATA:
1415     case CB_SETITEMHEIGHT:
1416     case CB_GETITEMHEIGHT:
1417     case CB_SETEXTENDEDUI:
1418     case CB_GETEXTENDEDUI:
1419     case CB_GETDROPPEDSTATE:
1420         ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
1421         break;
1422     case CB_GETEDITSEL:
1423         ret = callback( HWND_16(hwnd), CB_GETEDITSEL16, wParam, lParam, result, arg );
1424         if (wParam) *((PUINT)(wParam)) = LOWORD(*result);
1425         if (lParam) *((PUINT)(lParam)) = HIWORD(*result);  /* FIXME: substract 1? */
1426         break;
1427     case CB_ADDSTRING:
1428     case CB_FINDSTRING:
1429     case CB_FINDSTRINGEXACT:
1430     case CB_INSERTSTRING:
1431     case CB_SELECTSTRING:
1432     case CB_DIR:
1433     case CB_GETLBTEXT:
1434         lParam = MapLS( (LPSTR)lParam );
1435         ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
1436         UnMapLS( lParam );
1437         break;
1438     case LB_GETITEMRECT:
1439     case CB_GETDROPPEDCONTROLRECT:
1440         {
1441             RECT *r32 = (RECT *)lParam;
1442             RECT16 rect;
1443             lParam = MapLS( &rect );
1444             ret = callback( HWND_16(hwnd),
1445                             (msg == LB_GETITEMRECT) ? LB_GETITEMRECT16 : CB_GETDROPPEDCONTROLRECT16,
1446                             wParam, lParam, result, arg );
1447             UnMapLS( lParam );
1448             RECT16to32( &rect, r32 );
1449         }
1450         break;
1451     case WM_PAINTCLIPBOARD:
1452     case WM_SIZECLIPBOARD:
1453         FIXME_(msg)( "message %04x needs translation\n", msg );
1454         break;
1455     /* the following messages should not be sent to 16-bit apps */
1456     case WM_SIZING:
1457     case WM_MOVING:
1458     case WM_CAPTURECHANGED:
1459     case WM_STYLECHANGING:
1460     case WM_STYLECHANGED:
1461         break;
1462     default:
1463         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1464         break;
1465     }
1466     return ret;
1467 }
1468
1469
1470 /***********************************************************************
1471  *              SendMessage  (USER.111)
1472  */
1473 LRESULT WINAPI SendMessage16( HWND16 hwnd16, UINT16 msg, WPARAM16 wparam, LPARAM lparam )
1474 {
1475     LRESULT result;
1476     HWND hwnd = WIN_Handle32( hwnd16 );
1477
1478     if (hwnd != HWND_BROADCAST && WIN_IsCurrentThread(hwnd))
1479     {
1480         /* call 16-bit window proc directly */
1481         WNDPROC16 winproc;
1482
1483         /* first the WH_CALLWNDPROC hook */
1484         if (HOOK_IsHooked( WH_CALLWNDPROC ))
1485             WINPROC_CallProc16To32A( cwp_hook_callback, hwnd16, msg, wparam, lparam, &result, NULL );
1486
1487         if (!(winproc = (WNDPROC16)GetWindowLong16( hwnd16, GWLP_WNDPROC ))) return 0;
1488
1489         SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wparam, lparam );
1490         result = CallWindowProc16( winproc, hwnd16, msg, wparam, lparam );
1491         SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, result, wparam, lparam );
1492     }
1493     else  /* map to 32-bit unicode for inter-thread/process message */
1494     {
1495         WINPROC_CallProc16To32A( send_message_callback, hwnd16, msg, wparam, lparam, &result, NULL );
1496     }
1497     return result;
1498 }
1499
1500
1501 /***********************************************************************
1502  *              PostMessage  (USER.110)
1503  */
1504 BOOL16 WINAPI PostMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wparam, LPARAM lparam )
1505 {
1506     LRESULT unused;
1507     return WINPROC_CallProc16To32A( post_message_callback, hwnd, msg, wparam, lparam, &unused, NULL );
1508 }
1509
1510
1511 /***********************************************************************
1512  *              PostAppMessage (USER.116)
1513  */
1514 BOOL16 WINAPI PostAppMessage16( HTASK16 hTask, UINT16 msg, WPARAM16 wparam, LPARAM lparam )
1515 {
1516     LRESULT unused;
1517     DWORD_PTR tid = HTASK_32( hTask );
1518
1519     if (!tid) return FALSE;
1520     return WINPROC_CallProc16To32A( post_thread_message_callback, 0, msg, wparam, lparam,
1521                                     &unused, (void *)tid );
1522 }
1523
1524
1525 /**********************************************************************
1526  *              CallWindowProc (USER.122)
1527  */
1528 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
1529                                  WPARAM16 wParam, LPARAM lParam )
1530 {
1531     int index = winproc_to_index( func );
1532     LRESULT result;
1533
1534     if (!func) return 0;
1535
1536     if (index == -1 || index >= MAX_WINPROCS32)
1537         call_window_proc16( hwnd, msg, wParam, lParam, &result, func );
1538     else
1539     {
1540         WNDPROC proc = (WNDPROC)func;
1541         if (thunk_array && thunk_array[index].proc) proc = thunk_array[index].proc;
1542         WINPROC_CallProc16To32A( call_window_proc_callback, hwnd, msg, wParam, lParam, &result, proc );
1543     }
1544     return result;
1545 }
1546
1547
1548 /**********************************************************************
1549  *           __wine_call_wndproc   (USER.1010)
1550  */
1551 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam, WNDPROC proc )
1552 {
1553     LRESULT result;
1554     WINPROC_CallProc16To32A( call_window_proc_callback, hwnd, msg, wParam, lParam, &result, proc );
1555     return result;
1556 }
1557
1558
1559 /***********************************************************************
1560  *              InSendMessage  (USER.192)
1561  */
1562 BOOL16 WINAPI InSendMessage16(void)
1563 {
1564     return InSendMessage();
1565 }
1566
1567
1568 /***********************************************************************
1569  *              ReplyMessage  (USER.115)
1570  */
1571 void WINAPI ReplyMessage16( LRESULT result )
1572 {
1573     ReplyMessage( result );
1574 }
1575
1576
1577 /***********************************************************************
1578  *              PeekMessage32 (USER.819)
1579  */
1580 BOOL16 WINAPI PeekMessage32_16( MSG32_16 *msg16, HWND16 hwnd16,
1581                                 UINT16 first, UINT16 last, UINT16 flags,
1582                                 BOOL16 wHaveParamHigh )
1583 {
1584     MSG msg;
1585     LRESULT unused;
1586     HWND hwnd = WIN_Handle32( hwnd16 );
1587
1588     if(USER16_AlertableWait)
1589         MsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, MWMO_ALERTABLE );
1590     if (!PeekMessageA( &msg, hwnd, first, last, flags )) return FALSE;
1591
1592     msg16->msg.time    = msg.time;
1593     msg16->msg.pt.x    = (INT16)msg.pt.x;
1594     msg16->msg.pt.y    = (INT16)msg.pt.y;
1595     if (wHaveParamHigh) msg16->wParamHigh = HIWORD(msg.wParam);
1596     WINPROC_CallProc32ATo16( get_message_callback, msg.hwnd, msg.message, msg.wParam, msg.lParam,
1597                              &unused, &msg16->msg );
1598     return TRUE;
1599 }
1600
1601
1602 /***********************************************************************
1603  *              DefWindowProc (USER.107)
1604  */
1605 LRESULT WINAPI DefWindowProc16( HWND16 hwnd16, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
1606 {
1607     LRESULT result;
1608     HWND hwnd = WIN_Handle32( hwnd16 );
1609
1610     SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
1611
1612     switch(msg)
1613     {
1614     case WM_NCCREATE:
1615         {
1616             CREATESTRUCT16 *cs16 = MapSL(lParam);
1617             CREATESTRUCTA cs32;
1618
1619             cs32.lpCreateParams = ULongToPtr(cs16->lpCreateParams);
1620             cs32.hInstance      = HINSTANCE_32(cs16->hInstance);
1621             cs32.hMenu          = HMENU_32(cs16->hMenu);
1622             cs32.hwndParent     = WIN_Handle32(cs16->hwndParent);
1623             cs32.cy             = cs16->cy;
1624             cs32.cx             = cs16->cx;
1625             cs32.y              = cs16->y;
1626             cs32.x              = cs16->x;
1627             cs32.style          = cs16->style;
1628             cs32.dwExStyle      = cs16->dwExStyle;
1629             cs32.lpszName       = MapSL(cs16->lpszName);
1630             cs32.lpszClass      = MapSL(cs16->lpszClass);
1631             result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)&cs32 );
1632         }
1633         break;
1634
1635     case WM_NCCALCSIZE:
1636         {
1637             RECT16 *rect16 = MapSL(lParam);
1638             RECT rect32;
1639
1640             rect32.left    = rect16->left;
1641             rect32.top     = rect16->top;
1642             rect32.right   = rect16->right;
1643             rect32.bottom  = rect16->bottom;
1644
1645             result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)&rect32 );
1646
1647             rect16->left   = rect32.left;
1648             rect16->top    = rect32.top;
1649             rect16->right  = rect32.right;
1650             rect16->bottom = rect32.bottom;
1651         }
1652         break;
1653
1654     case WM_WINDOWPOSCHANGING:
1655     case WM_WINDOWPOSCHANGED:
1656         {
1657             WINDOWPOS16 *pos16 = MapSL(lParam);
1658             WINDOWPOS pos32;
1659
1660             pos32.hwnd             = WIN_Handle32(pos16->hwnd);
1661             pos32.hwndInsertAfter  = WIN_Handle32(pos16->hwndInsertAfter);
1662             pos32.x                = pos16->x;
1663             pos32.y                = pos16->y;
1664             pos32.cx               = pos16->cx;
1665             pos32.cy               = pos16->cy;
1666             pos32.flags            = pos16->flags;
1667
1668             result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)&pos32 );
1669
1670             pos16->hwnd            = HWND_16(pos32.hwnd);
1671             pos16->hwndInsertAfter = HWND_16(pos32.hwndInsertAfter);
1672             pos16->x               = pos32.x;
1673             pos16->y               = pos32.y;
1674             pos16->cx              = pos32.cx;
1675             pos16->cy              = pos32.cy;
1676             pos16->flags           = pos32.flags;
1677         }
1678         break;
1679
1680     case WM_GETTEXT:
1681     case WM_SETTEXT:
1682         result = DefWindowProcA( hwnd, msg, wParam, (LPARAM)MapSL(lParam) );
1683         break;
1684
1685     default:
1686         result = DefWindowProcA( hwnd, msg, wParam, lParam );
1687         break;
1688     }
1689
1690     SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result, wParam, lParam );
1691     return result;
1692 }
1693
1694
1695 /***********************************************************************
1696  *              DefDlgProc (USER.308)
1697  */
1698 LRESULT WINAPI DefDlgProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
1699 {
1700     LRESULT result;
1701     WINPROC_CallProc16To32A( defdlg_proc_callback, hwnd, msg, wParam, lParam, &result, 0 );
1702     return result;
1703 }
1704
1705
1706 /***********************************************************************
1707  *              PeekMessage  (USER.109)
1708  */
1709 BOOL16 WINAPI PeekMessage16( MSG16 *msg, HWND16 hwnd,
1710                              UINT16 first, UINT16 last, UINT16 flags )
1711 {
1712     return PeekMessage32_16( (MSG32_16 *)msg, hwnd, first, last, flags, FALSE );
1713 }
1714
1715
1716 /***********************************************************************
1717  *              GetMessage32  (USER.820)
1718  */
1719 BOOL16 WINAPI GetMessage32_16( MSG32_16 *msg16, HWND16 hwnd16, UINT16 first,
1720                                UINT16 last, BOOL16 wHaveParamHigh )
1721 {
1722     MSG msg;
1723     LRESULT unused;
1724     HWND hwnd = WIN_Handle32( hwnd16 );
1725
1726     if(USER16_AlertableWait)
1727         MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, 0, MWMO_ALERTABLE );
1728     GetMessageA( &msg, hwnd, first, last );
1729     msg16->msg.time    = msg.time;
1730     msg16->msg.pt.x    = (INT16)msg.pt.x;
1731     msg16->msg.pt.y    = (INT16)msg.pt.y;
1732     if (wHaveParamHigh) msg16->wParamHigh = HIWORD(msg.wParam);
1733     WINPROC_CallProc32ATo16( get_message_callback, msg.hwnd, msg.message, msg.wParam, msg.lParam,
1734                              &unused, &msg16->msg );
1735
1736     TRACE( "message %04x, hwnd %p, filter(%04x - %04x)\n",
1737            msg16->msg.message, hwnd, first, last );
1738
1739     return msg16->msg.message != WM_QUIT;
1740 }
1741
1742
1743 /***********************************************************************
1744  *              GetMessage  (USER.108)
1745  */
1746 BOOL16 WINAPI GetMessage16( MSG16 *msg, HWND16 hwnd, UINT16 first, UINT16 last )
1747 {
1748     return GetMessage32_16( (MSG32_16 *)msg, hwnd, first, last, FALSE );
1749 }
1750
1751
1752 /***********************************************************************
1753  *              TranslateMessage32 (USER.821)
1754  */
1755 BOOL16 WINAPI TranslateMessage32_16( const MSG32_16 *msg, BOOL16 wHaveParamHigh )
1756 {
1757     MSG msg32;
1758
1759     msg32.hwnd    = WIN_Handle32( msg->msg.hwnd );
1760     msg32.message = msg->msg.message;
1761     msg32.wParam  = MAKEWPARAM( msg->msg.wParam, wHaveParamHigh ? msg->wParamHigh : 0 );
1762     msg32.lParam  = msg->msg.lParam;
1763     return TranslateMessage( &msg32 );
1764 }
1765
1766
1767 /***********************************************************************
1768  *              TranslateMessage (USER.113)
1769  */
1770 BOOL16 WINAPI TranslateMessage16( const MSG16 *msg )
1771 {
1772     return TranslateMessage32_16( (const MSG32_16 *)msg, FALSE );
1773 }
1774
1775
1776 /***********************************************************************
1777  *              DispatchMessage (USER.114)
1778  */
1779 LONG WINAPI DispatchMessage16( const MSG16* msg )
1780 {
1781     WND * wndPtr;
1782     WNDPROC16 winproc;
1783     LONG retval;
1784     HWND hwnd = WIN_Handle32( msg->hwnd );
1785
1786       /* Process timer messages */
1787     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1788     {
1789         if (msg->lParam)
1790             return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
1791                                      msg->message, msg->wParam, GetTickCount() );
1792     }
1793
1794     if (!(wndPtr = WIN_GetPtr( hwnd )))
1795     {
1796         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1797         return 0;
1798     }
1799     if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
1800     {
1801         if (IsWindow( hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
1802         else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1803         return 0;
1804     }
1805     winproc = WINPROC_GetProc16( wndPtr->winproc, wndPtr->flags & WIN_ISUNICODE );
1806     WIN_ReleasePtr( wndPtr );
1807
1808     SPY_EnterMessage( SPY_DISPATCHMESSAGE16, hwnd, msg->message, msg->wParam, msg->lParam );
1809     retval = CallWindowProc16( winproc, msg->hwnd, msg->message, msg->wParam, msg->lParam );
1810     SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg->message, retval, msg->wParam, msg->lParam );
1811
1812     return retval;
1813 }
1814
1815
1816 /***********************************************************************
1817  *              DispatchMessage32 (USER.822)
1818  */
1819 LONG WINAPI DispatchMessage32_16( const MSG32_16 *msg16, BOOL16 wHaveParamHigh )
1820 {
1821     if (wHaveParamHigh == FALSE)
1822         return DispatchMessage16( &msg16->msg );
1823     else
1824     {
1825         MSG msg;
1826
1827         msg.hwnd    = WIN_Handle32( msg16->msg.hwnd );
1828         msg.message = msg16->msg.message;
1829         msg.wParam  = MAKEWPARAM( msg16->msg.wParam, msg16->wParamHigh );
1830         msg.lParam  = msg16->msg.lParam;
1831         msg.time    = msg16->msg.time;
1832         msg.pt.x    = msg16->msg.pt.x;
1833         msg.pt.y    = msg16->msg.pt.y;
1834         return DispatchMessageA( &msg );
1835     }
1836 }
1837
1838
1839 /***********************************************************************
1840  *              IsDialogMessage (USER.90)
1841  */
1842 BOOL16 WINAPI IsDialogMessage16( HWND16 hwndDlg, MSG16 *msg16 )
1843 {
1844     MSG msg;
1845     HWND hwndDlg32;
1846
1847     msg.hwnd  = WIN_Handle32(msg16->hwnd);
1848     hwndDlg32 = WIN_Handle32(hwndDlg);
1849
1850     switch(msg16->message)
1851     {
1852     case WM_KEYDOWN:
1853     case WM_CHAR:
1854     case WM_SYSCHAR:
1855         msg.message = msg16->message;
1856         msg.wParam  = msg16->wParam;
1857         msg.lParam  = msg16->lParam;
1858         return IsDialogMessageA( hwndDlg32, &msg );
1859     }
1860
1861     if ((hwndDlg32 != msg.hwnd) && !IsChild( hwndDlg32, msg.hwnd )) return FALSE;
1862     TranslateMessage16( msg16 );
1863     DispatchMessage16( msg16 );
1864     return TRUE;
1865 }
1866
1867
1868 /***********************************************************************
1869  *              MsgWaitForMultipleObjects  (USER.640)
1870  */
1871 DWORD WINAPI MsgWaitForMultipleObjects16( DWORD count, CONST HANDLE *handles,
1872                                           BOOL wait_all, DWORD timeout, DWORD mask )
1873 {
1874     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
1875                                         wait_all ? MWMO_WAITALL : 0 );
1876 }
1877
1878
1879 /**********************************************************************
1880  *              SetDoubleClickTime (USER.20)
1881  */
1882 void WINAPI SetDoubleClickTime16( UINT16 interval )
1883 {
1884     SetDoubleClickTime( interval );
1885 }
1886
1887
1888 /**********************************************************************
1889  *              GetDoubleClickTime (USER.21)
1890  */
1891 UINT16 WINAPI GetDoubleClickTime16(void)
1892 {
1893     return GetDoubleClickTime();
1894 }
1895
1896
1897 /***********************************************************************
1898  *              PostQuitMessage (USER.6)
1899  */
1900 void WINAPI PostQuitMessage16( INT16 exitCode )
1901 {
1902     PostQuitMessage( exitCode );
1903 }
1904
1905
1906 /**********************************************************************
1907  *              GetKeyState (USER.106)
1908  */
1909 INT16 WINAPI GetKeyState16(INT16 vkey)
1910 {
1911     return GetKeyState(vkey);
1912 }
1913
1914
1915 /**********************************************************************
1916  *              GetKeyboardState (USER.222)
1917  */
1918 BOOL WINAPI GetKeyboardState16( LPBYTE state )
1919 {
1920     return GetKeyboardState( state );
1921 }
1922
1923
1924 /**********************************************************************
1925  *              SetKeyboardState (USER.223)
1926  */
1927 BOOL WINAPI SetKeyboardState16( LPBYTE state )
1928 {
1929     return SetKeyboardState( state );
1930 }
1931
1932
1933 /***********************************************************************
1934  *              SetMessageQueue (USER.266)
1935  */
1936 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1937 {
1938     return SetMessageQueue( size );
1939 }
1940
1941
1942 /***********************************************************************
1943  *              UserYield (USER.332)
1944  */
1945 void WINAPI UserYield16(void)
1946 {
1947     MSG msg;
1948     PeekMessageW( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
1949 }
1950
1951
1952 /***********************************************************************
1953  *              GetQueueStatus (USER.334)
1954  */
1955 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1956 {
1957     return GetQueueStatus( flags );
1958 }
1959
1960
1961 /***********************************************************************
1962  *              GetInputState (USER.335)
1963  */
1964 BOOL16 WINAPI GetInputState16(void)
1965 {
1966     return GetInputState();
1967 }
1968
1969
1970 /**********************************************************************
1971  *           TranslateAccelerator      (USER.178)
1972  */
1973 INT16 WINAPI TranslateAccelerator16( HWND16 hwnd, HACCEL16 hAccel, LPMSG16 msg )
1974 {
1975     MSG msg32;
1976
1977     if (!msg) return 0;
1978     msg32.message = msg->message;
1979     /* msg32.hwnd not used */
1980     msg32.wParam  = msg->wParam;
1981     msg32.lParam  = msg->lParam;
1982     return TranslateAcceleratorW( WIN_Handle32(hwnd), HACCEL_32(hAccel), &msg32 );
1983 }
1984
1985
1986 /**********************************************************************
1987  *              TranslateMDISysAccel (USER.451)
1988  */
1989 BOOL16 WINAPI TranslateMDISysAccel16( HWND16 hwndClient, LPMSG16 msg )
1990 {
1991     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1992     {
1993         MSG msg32;
1994         msg32.hwnd    = WIN_Handle32(msg->hwnd);
1995         msg32.message = msg->message;
1996         msg32.wParam  = msg->wParam;
1997         msg32.lParam  = msg->lParam;
1998         /* MDICLIENTINFO is still the same for win32 and win16 ... */
1999         return TranslateMDISysAccel( WIN_Handle32(hwndClient), &msg32 );
2000     }
2001     return 0;
2002 }
2003
2004
2005 /***********************************************************************
2006  *           button_proc16
2007  */
2008 static LRESULT button_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2009 {
2010     static const UINT msg16_offset = BM_GETCHECK16 - BM_GETCHECK;
2011
2012     switch (msg)
2013     {
2014     case BM_GETCHECK16:
2015     case BM_SETCHECK16:
2016     case BM_GETSTATE16:
2017     case BM_SETSTATE16:
2018     case BM_SETSTYLE16:
2019         return wow_handlers32.button_proc( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
2020     default:
2021         return wow_handlers32.button_proc( hwnd, msg, wParam, lParam, unicode );
2022     }
2023 }
2024
2025
2026 /***********************************************************************
2027  *           combo_proc16
2028  */
2029 static LRESULT combo_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2030 {
2031     static const UINT msg16_offset = CB_GETEDITSEL16 - CB_GETEDITSEL;
2032
2033     switch (msg)
2034     {
2035     case CB_INSERTSTRING16:
2036     case CB_SELECTSTRING16:
2037     case CB_FINDSTRING16:
2038     case CB_FINDSTRINGEXACT16:
2039         wParam = (INT)(INT16)wParam;
2040         /* fall through */
2041     case CB_ADDSTRING16:
2042     {
2043         DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2044         if ((style & CBS_HASSTRINGS) || !(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)))
2045            lParam = (LPARAM)MapSL(lParam);
2046         msg -= msg16_offset;
2047         break;
2048     }
2049     case CB_SETITEMHEIGHT16:
2050     case CB_GETITEMHEIGHT16:
2051     case CB_SETCURSEL16:
2052     case CB_GETLBTEXTLEN16:
2053     case CB_GETITEMDATA16:
2054     case CB_SETITEMDATA16:
2055         wParam = (INT)(INT16)wParam;    /* signed integer */
2056         msg -= msg16_offset;
2057         break;
2058     case CB_GETDROPPEDCONTROLRECT16:
2059         lParam = (LPARAM)MapSL(lParam);
2060         if (lParam)
2061         {
2062             RECT r;
2063             RECT16 *r16 = (RECT16 *)lParam;
2064             wow_handlers32.combo_proc( hwnd, CB_GETDROPPEDCONTROLRECT, wParam, (LPARAM)&r, FALSE );
2065             r16->left   = r.left;
2066             r16->top    = r.top;
2067             r16->right  = r.right;
2068             r16->bottom = r.bottom;
2069         }
2070         return CB_OKAY;
2071     case CB_DIR16:
2072         if (wParam & DDL_DRIVES) wParam |= DDL_EXCLUSIVE;
2073         lParam = (LPARAM)MapSL(lParam);
2074         msg -= msg16_offset;
2075         break;
2076     case CB_GETLBTEXT16:
2077         wParam = (INT)(INT16)wParam;
2078         lParam = (LPARAM)MapSL(lParam);
2079         msg -= msg16_offset;
2080         break;
2081     case CB_GETEDITSEL16:
2082         wParam = lParam = 0;   /* just in case */
2083         msg -= msg16_offset;
2084         break;
2085     case CB_LIMITTEXT16:
2086     case CB_SETEDITSEL16:
2087     case CB_DELETESTRING16:
2088     case CB_RESETCONTENT16:
2089     case CB_GETDROPPEDSTATE16:
2090     case CB_SHOWDROPDOWN16:
2091     case CB_GETCOUNT16:
2092     case CB_GETCURSEL16:
2093     case CB_SETEXTENDEDUI16:
2094     case CB_GETEXTENDEDUI16:
2095         msg -= msg16_offset;
2096         break;
2097     default:
2098         return wow_handlers32.combo_proc( hwnd, msg, wParam, lParam, unicode );
2099     }
2100     return wow_handlers32.combo_proc( hwnd, msg, wParam, lParam, FALSE );
2101 }
2102
2103
2104 #define GWW_HANDLE16 sizeof(void*)
2105
2106 static void edit_lock_buffer( HWND hwnd )
2107 {
2108     STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2109     HLOCAL16 hloc16 = GetWindowWord( hwnd, GWW_HANDLE16 );
2110     HANDLE16 oldDS;
2111     HLOCAL hloc32;
2112     UINT size;
2113
2114     if (!hloc16) return;
2115     if (!(hloc32 = (HLOCAL)wow_handlers32.edit_proc( hwnd, EM_GETHANDLE, 0, 0, FALSE ))) return;
2116
2117     oldDS = stack16->ds;
2118     stack16->ds = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2119     size = LocalSize16(hloc16);
2120     if (LocalReAlloc( hloc32, size, LMEM_MOVEABLE ))
2121     {
2122         char *text = MapSL( LocalLock16( hloc16 ));
2123         char *dest = LocalLock( hloc32 );
2124         memcpy( dest, text, size );
2125         LocalUnlock( hloc32 );
2126         LocalUnlock16( hloc16 );
2127     }
2128     stack16->ds = oldDS;
2129
2130 }
2131
2132 static void edit_unlock_buffer( HWND hwnd )
2133 {
2134     STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2135     HLOCAL16 hloc16 = GetWindowWord( hwnd, GWW_HANDLE16 );
2136     HANDLE16 oldDS;
2137     HLOCAL hloc32;
2138     UINT size;
2139
2140     if (!hloc16) return;
2141     if (!(hloc32 = (HLOCAL)wow_handlers32.edit_proc( hwnd, EM_GETHANDLE, 0, 0, FALSE ))) return;
2142     size = LocalSize( hloc32 );
2143
2144     oldDS = stack16->ds;
2145     stack16->ds = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2146     if (LocalReAlloc16( hloc16, size, LMEM_MOVEABLE ))
2147     {
2148         char *text = LocalLock( hloc32 );
2149         char *dest = MapSL( LocalLock16( hloc16 ));
2150         memcpy( dest, text, size );
2151         LocalUnlock( hloc32 );
2152         LocalUnlock16( hloc16 );
2153     }
2154     stack16->ds = oldDS;
2155 }
2156
2157 static HLOCAL16 edit_get_handle( HWND hwnd )
2158 {
2159     CHAR *textA;
2160     UINT alloc_size;
2161     HLOCAL hloc;
2162     STACK16FRAME* stack16;
2163     HANDLE16 oldDS;
2164     HLOCAL16 hloc16 = GetWindowWord( hwnd, GWW_HANDLE16 );
2165
2166     if (hloc16) return hloc16;
2167
2168     if (!(hloc = (HLOCAL)wow_handlers32.edit_proc( hwnd, EM_GETHANDLE, 0, 0, FALSE ))) return 0;
2169     alloc_size = LocalSize( hloc );
2170
2171     stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2172     oldDS = stack16->ds;
2173     stack16->ds = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2174
2175     if (!LocalHeapSize16())
2176     {
2177         if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds)))
2178         {
2179             ERR("could not initialize local heap\n");
2180             goto done;
2181         }
2182     }
2183
2184     if (!(hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2185     {
2186         ERR("could not allocate new 16 bit buffer\n");
2187         goto done;
2188     }
2189
2190     if (!(textA = MapSL(LocalLock16( hloc16))))
2191     {
2192         ERR("could not lock new 16 bit buffer\n");
2193         LocalFree16(hloc16);
2194         hloc16 = 0;
2195         goto done;
2196     }
2197     memcpy( textA, LocalLock( hloc ), alloc_size );
2198     LocalUnlock( hloc );
2199     LocalUnlock16( hloc16 );
2200     SetWindowWord( hwnd, GWW_HANDLE16, hloc16 );
2201
2202 done:
2203     stack16->ds = oldDS;
2204     return hloc16;
2205 }
2206
2207 static void edit_set_handle( HWND hwnd, HLOCAL16 hloc16 )
2208 {
2209     STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2210     HINSTANCE16 hInstance = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2211     HANDLE16 oldDS = stack16->ds;
2212     HLOCAL hloc32;
2213     INT count;
2214     CHAR *text;
2215
2216     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & ES_MULTILINE)) return;
2217     if (!hloc16) return;
2218
2219     stack16->ds = hInstance;
2220     count = LocalSize16(hloc16);
2221     text = MapSL(LocalLock16(hloc16));
2222     if ((hloc32 = LocalAlloc(LMEM_MOVEABLE, count)))
2223     {
2224         memcpy( LocalLock(hloc32), text, count );
2225         LocalUnlock(hloc32);
2226         LocalUnlock16(hloc16);
2227         SetWindowWord( hwnd, GWW_HANDLE16, hloc16 );
2228     }
2229     stack16->ds = oldDS;
2230
2231     if (hloc32) wow_handlers32.edit_proc( hwnd, EM_SETHANDLE, (WPARAM)hloc32, 0, FALSE );
2232 }
2233
2234 static void edit_destroy_handle( HWND hwnd )
2235 {
2236     HLOCAL16 hloc16 = GetWindowWord( hwnd, GWW_HANDLE16 );
2237     if (hloc16)
2238     {
2239         STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2240         HANDLE16 oldDS = stack16->ds;
2241
2242         stack16->ds = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2243         while (LocalUnlock16(hloc16)) ;
2244         LocalFree16(hloc16);
2245         stack16->ds = oldDS;
2246         SetWindowWord( hwnd, GWW_HANDLE16, 0 );
2247     }
2248 }
2249
2250 /*********************************************************************
2251  *      edit_proc16
2252  */
2253 static LRESULT edit_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2254 {
2255     static const UINT msg16_offset = EM_GETSEL16 - EM_GETSEL;
2256     LRESULT result = 0;
2257
2258     edit_lock_buffer( hwnd );
2259     switch (msg)
2260     {
2261     case EM_SCROLL16:
2262     case EM_SCROLLCARET16:
2263     case EM_GETMODIFY16:
2264     case EM_SETMODIFY16:
2265     case EM_GETLINECOUNT16:
2266     case EM_GETTHUMB16:
2267     case EM_LINELENGTH16:
2268     case EM_LIMITTEXT16:
2269     case EM_CANUNDO16:
2270     case EM_UNDO16:
2271     case EM_FMTLINES16:
2272     case EM_LINEFROMCHAR16:
2273     case EM_SETPASSWORDCHAR16:
2274     case EM_EMPTYUNDOBUFFER16:
2275     case EM_SETREADONLY16:
2276     case EM_GETPASSWORDCHAR16:
2277         /* these messages missing from specs */
2278     case WM_USER+15:
2279     case WM_USER+16:
2280     case WM_USER+19:
2281     case WM_USER+26:
2282         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
2283         break;
2284     case EM_GETSEL16:
2285         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, 0, 0, FALSE );
2286         break;
2287     case EM_REPLACESEL16:
2288     case EM_GETLINE16:
2289         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, (LPARAM)MapSL(lParam), FALSE );
2290         break;
2291     case EM_LINESCROLL16:
2292         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, (INT)(SHORT)HIWORD(lParam),
2293                                            (INT)(SHORT)LOWORD(lParam), FALSE );
2294         break;
2295     case EM_LINEINDEX16:
2296         if ((INT16)wParam == -1) wParam = (WPARAM)-1;
2297         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
2298         break;
2299     case EM_SETSEL16:
2300         if ((short)LOWORD(lParam) == -1)
2301         {
2302             wParam = -1;
2303             lParam = 0;
2304         }
2305         else
2306         {
2307             wParam = LOWORD(lParam);
2308             lParam = HIWORD(lParam);
2309         }
2310         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
2311         break;
2312     case EM_GETRECT16:
2313         if (lParam)
2314         {
2315             RECT rect;
2316             RECT16 *r16 = MapSL(lParam);
2317             wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, (LPARAM)&rect, FALSE );
2318             r16->left   = rect.left;
2319             r16->top    = rect.top;
2320             r16->right  = rect.right;
2321             r16->bottom = rect.bottom;
2322         }
2323         break;
2324     case EM_SETRECT16:
2325     case EM_SETRECTNP16:
2326         if (lParam)
2327         {
2328             RECT rect;
2329             RECT16 *r16 = MapSL(lParam);
2330             rect.left   = r16->left;
2331             rect.top    = r16->top;
2332             rect.right  = r16->right;
2333             rect.bottom = r16->bottom;
2334             wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, (LPARAM)&rect, FALSE );
2335         }
2336         break;
2337     case EM_SETHANDLE16:
2338         edit_set_handle( hwnd, (HLOCAL16)wParam );
2339         break;
2340     case EM_GETHANDLE16:
2341         result = edit_get_handle( hwnd );
2342         break;
2343     case EM_SETTABSTOPS16:
2344     {
2345         INT16 *tabs16 = MapSL(lParam);
2346         INT i, count = wParam, *tabs = NULL;
2347         if (count > 0)
2348         {
2349             if (!(tabs = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*tabs) ))) return 0;
2350             for (i = 0; i < count; i++) tabs[i] = tabs16[i];
2351         }
2352         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, count, (LPARAM)tabs, FALSE );
2353         HeapFree( GetProcessHeap(), 0, tabs );
2354         break;
2355     }
2356     case EM_GETFIRSTVISIBLELINE16:
2357         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & ES_MULTILINE)) break;
2358         result = wow_handlers32.edit_proc( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
2359         break;
2360     case EM_SETWORDBREAKPROC16:
2361     {
2362         struct word_break_thunk *thunk = add_word_break_thunk( (EDITWORDBREAKPROC16)lParam );
2363         result = wow_handlers32.edit_proc( hwnd, EM_SETWORDBREAKPROC, wParam, (LPARAM)thunk, FALSE );
2364         break;
2365     }
2366     case EM_GETWORDBREAKPROC16:
2367         result = wow_handlers32.edit_proc( hwnd, EM_GETWORDBREAKPROC, wParam, lParam, FALSE );
2368         result = (LRESULT)get_word_break_thunk( (EDITWORDBREAKPROCA)result );
2369         break;
2370     case WM_NCDESTROY:
2371         edit_destroy_handle( hwnd );
2372         return wow_handlers32.edit_proc( hwnd, msg, wParam, lParam, unicode );  /* no unlock on destroy */
2373     case WM_HSCROLL:
2374     case WM_VSCROLL:
2375         if (LOWORD(wParam) == EM_GETTHUMB16 || LOWORD(wParam) == EM_LINESCROLL16) wParam -= msg16_offset;
2376         result = wow_handlers32.edit_proc( hwnd, msg, wParam, lParam, unicode );
2377         break;
2378     default:
2379         result = wow_handlers32.edit_proc( hwnd, msg, wParam, lParam, unicode );
2380         break;
2381     }
2382     edit_unlock_buffer( hwnd );
2383     return result;
2384 }
2385
2386
2387 /***********************************************************************
2388  *           listbox_proc16
2389  */
2390 static LRESULT listbox_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2391 {
2392     static const UINT msg16_offset = LB_ADDSTRING16 - LB_ADDSTRING;
2393     LRESULT ret;
2394
2395     switch (msg)
2396     {
2397     case LB_RESETCONTENT16:
2398     case LB_DELETESTRING16:
2399     case LB_GETITEMDATA16:
2400     case LB_SETITEMDATA16:
2401     case LB_GETCOUNT16:
2402     case LB_GETTEXTLEN16:
2403     case LB_GETCURSEL16:
2404     case LB_GETTOPINDEX16:
2405     case LB_GETITEMHEIGHT16:
2406     case LB_SETCARETINDEX16:
2407     case LB_GETCARETINDEX16:
2408     case LB_SETTOPINDEX16:
2409     case LB_SETCOLUMNWIDTH16:
2410     case LB_GETSELCOUNT16:
2411     case LB_SELITEMRANGE16:
2412     case LB_SELITEMRANGEEX16:
2413     case LB_GETHORIZONTALEXTENT16:
2414     case LB_SETHORIZONTALEXTENT16:
2415     case LB_GETANCHORINDEX16:
2416     case LB_CARETON16:
2417     case LB_CARETOFF16:
2418         msg -= msg16_offset;
2419         break;
2420     case LB_GETSEL16:
2421     case LB_SETSEL16:
2422     case LB_SETCURSEL16:
2423     case LB_SETANCHORINDEX16:
2424         wParam = (INT)(INT16)wParam;
2425         msg -= msg16_offset;
2426         break;
2427     case LB_INSERTSTRING16:
2428     case LB_FINDSTRING16:
2429     case LB_FINDSTRINGEXACT16:
2430     case LB_SELECTSTRING16:
2431         wParam = (INT)(INT16)wParam;
2432         /* fall through */
2433     case LB_ADDSTRING16:
2434     case LB_ADDFILE16:
2435     {
2436         DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2437         if ((style & LBS_HASSTRINGS) || !(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)))
2438             lParam = (LPARAM)MapSL(lParam);
2439         msg -= msg16_offset;
2440         break;
2441     }
2442     case LB_GETTEXT16:
2443         lParam = (LPARAM)MapSL(lParam);
2444         msg -= msg16_offset;
2445         break;
2446     case LB_SETITEMHEIGHT16:
2447         lParam = LOWORD(lParam);
2448         msg -= msg16_offset;
2449         break;
2450     case LB_GETITEMRECT16:
2451         {
2452             RECT rect;
2453             RECT16 *r16 = MapSL(lParam);
2454             ret = wow_handlers32.listbox_proc( hwnd, LB_GETITEMRECT, (INT16)wParam, (LPARAM)&rect, FALSE );
2455             r16->left   = rect.left;
2456             r16->top    = rect.top;
2457             r16->right  = rect.right;
2458             r16->bottom = rect.bottom;
2459             return ret;
2460         }
2461     case LB_GETSELITEMS16:
2462     {
2463         INT16 *array16 = MapSL( lParam );
2464         INT i, count = (INT16)wParam, *array;
2465         if (!(array = HeapAlloc( GetProcessHeap(), 0, wParam * sizeof(*array) ))) return LB_ERRSPACE;
2466         ret = wow_handlers32.listbox_proc( hwnd, LB_GETSELITEMS, count, (LPARAM)array, FALSE );
2467         for (i = 0; i < ret; i++) array16[i] = array[i];
2468         HeapFree( GetProcessHeap(), 0, array );
2469         return ret;
2470     }
2471     case LB_DIR16:
2472         /* according to Win16 docs, DDL_DRIVES should make DDL_EXCLUSIVE
2473          * be set automatically (this is different in Win32) */
2474         if (wParam & DDL_DRIVES) wParam |= DDL_EXCLUSIVE;
2475         lParam = (LPARAM)MapSL(lParam);
2476         msg -= msg16_offset;
2477         break;
2478     case LB_SETTABSTOPS16:
2479     {
2480         INT i, count, *tabs = NULL;
2481         INT16 *tabs16 = MapSL( lParam );
2482
2483         if ((count = (INT16)wParam) > 0)
2484         {
2485             if (!(tabs = HeapAlloc( GetProcessHeap(), 0, wParam * sizeof(*tabs) ))) return LB_ERRSPACE;
2486             for (i = 0; i < count; i++) tabs[i] = tabs16[i] << 1; /* FIXME */
2487         }
2488         ret = wow_handlers32.listbox_proc( hwnd, LB_SETTABSTOPS, count, (LPARAM)tabs, FALSE );
2489         HeapFree( GetProcessHeap(), 0, tabs );
2490         return ret;
2491     }
2492     default:
2493         return wow_handlers32.listbox_proc( hwnd, msg, wParam, lParam, unicode );
2494     }
2495     return wow_handlers32.listbox_proc( hwnd, msg, wParam, lParam, FALSE );
2496 }
2497
2498
2499 /***********************************************************************
2500  *           mdiclient_proc16
2501  */
2502 static LRESULT mdiclient_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2503 {
2504     if (msg == WM_CREATE)
2505     {
2506         LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
2507         WND *win;
2508         BOOL is_win32;
2509
2510         if (!(win = WIN_GetPtr( hwnd ))) return 0;
2511         is_win32 = (win == WND_OTHER_PROCESS || win == WND_DESKTOP || (win->flags & WIN_ISWIN32));
2512         WIN_ReleasePtr( win );
2513
2514         /* Translation layer doesn't know what's in the cs->lpCreateParams
2515          * so we have to keep track of what environment we're in. */
2516         if (!is_win32)
2517         {
2518             void *orig = cs->lpCreateParams;
2519             LRESULT ret;
2520             CLIENTCREATESTRUCT ccs;
2521             CLIENTCREATESTRUCT16 *ccs16 = MapSL( PtrToUlong( orig ));
2522
2523             ccs.hWindowMenu  = HMENU_32(ccs16->hWindowMenu);
2524             ccs.idFirstChild = ccs16->idFirstChild;
2525             cs->lpCreateParams = &ccs;
2526             ret = wow_handlers32.mdiclient_proc( hwnd, msg, wParam, lParam, unicode );
2527             cs->lpCreateParams = orig;
2528             return ret;
2529         }
2530     }
2531     return wow_handlers32.mdiclient_proc( hwnd, msg, wParam, lParam, unicode );
2532 }
2533
2534
2535 /***********************************************************************
2536  *           scrollbar_proc16
2537  */
2538 static LRESULT scrollbar_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2539 {
2540     static const UINT msg16_offset = SBM_SETPOS16 - SBM_SETPOS;
2541
2542     switch (msg)
2543     {
2544     case SBM_SETPOS16:
2545     case SBM_GETPOS16:
2546     case SBM_ENABLE_ARROWS16:
2547         msg -= msg16_offset;
2548         break;
2549     case SBM_SETRANGE16:
2550         msg = wParam ? SBM_SETRANGEREDRAW : SBM_SETRANGE;
2551         wParam = LOWORD(lParam);
2552         lParam = HIWORD(lParam);
2553         break;
2554     case SBM_GETRANGE16:
2555     {
2556         INT min, max;
2557         wow_handlers32.scrollbar_proc( hwnd, SBM_GETRANGE, (WPARAM)&min, (LPARAM)&max, FALSE );
2558         return MAKELRESULT(min, max);
2559     }
2560     default:
2561         return wow_handlers32.scrollbar_proc( hwnd, msg, wParam, lParam, unicode );
2562     }
2563     return wow_handlers32.scrollbar_proc( hwnd, msg, wParam, lParam, FALSE );
2564 }
2565
2566
2567 /***********************************************************************
2568  *           static_proc16
2569  */
2570 static LRESULT static_proc16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
2571 {
2572     switch (msg)
2573     {
2574     case STM_SETICON16:
2575         wParam = (WPARAM)HICON_32( (HICON16)wParam );
2576         return wow_handlers32.static_proc( hwnd, STM_SETICON, wParam, lParam, FALSE );
2577     case STM_GETICON16:
2578         return HICON_16( wow_handlers32.static_proc( hwnd, STM_GETICON, wParam, lParam, FALSE ));
2579     default:
2580         return wow_handlers32.static_proc( hwnd, msg, wParam, lParam, unicode );
2581     }
2582 }
2583
2584
2585 /***********************************************************************
2586  *           create_window16
2587  */
2588 HWND create_window16( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE instance, UINT flags )
2589 {
2590     /* map to module handle */
2591     if (instance && !((ULONG_PTR)instance >> 16))
2592         instance = HINSTANCE_32( GetExePtr( HINSTANCE_16(instance) ));
2593
2594     return wow_handlers32.create_window( cs, className, instance, flags );
2595 }
2596
2597
2598 void register_wow_handlers(void)
2599 {
2600     static const struct wow_handlers16 handlers16 =
2601     {
2602         button_proc16,
2603         combo_proc16,
2604         edit_proc16,
2605         listbox_proc16,
2606         mdiclient_proc16,
2607         scrollbar_proc16,
2608         static_proc16,
2609         create_window16,
2610         call_window_proc_Ato16,
2611         call_dialog_proc_Ato16
2612     };
2613
2614     UserRegisterWowHandlers( &handlers16, &wow_handlers32 );
2615 }