ntdll: Pass the NtQueryDirectoryFile info class down into the various helper functions.
[wine] / dlls / user32 / winproc.c
1 /*
2  * Window procedure callbacks
3  *
4  * Copyright 1995 Martin von Loewis
5  * Copyright 1996 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wownt32.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "controls.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "dde.h"
38 #include "winternl.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 WINE_DECLARE_DEBUG_CHANNEL(msg);
43 WINE_DECLARE_DEBUG_CHANNEL(relay);
44 WINE_DEFAULT_DEBUG_CHANNEL(win);
45
46 typedef struct tagWINDOWPROC
47 {
48     WNDPROC16      proc16;   /* 16-bit window proc */
49     WNDPROC        procA;    /* ASCII window proc */
50     WNDPROC        procW;    /* Unicode window proc */
51 } WINDOWPROC;
52
53 #define WINPROC_HANDLE (~0u >> 16)
54 #define MAX_WINPROCS  8192
55 #define BUILTIN_WINPROCS 9  /* first BUILTIN_WINPROCS entries are reserved for builtin procs */
56 #define MAX_WINPROC_RECURSION  64
57
58 WNDPROC EDIT_winproc_handle = 0;
59
60 static WINDOWPROC winproc_array[MAX_WINPROCS];
61 static UINT builtin_used;
62 static UINT winproc_used = BUILTIN_WINPROCS;
63
64 static CRITICAL_SECTION winproc_cs;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
66 {
67     0, 0, &winproc_cs,
68     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69       0, 0, { (DWORD_PTR)(__FILE__ ": winproc_cs") }
70 };
71 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
72
73 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
74 {
75     if (size >= need) return static_buffer;
76     return HeapAlloc( GetProcessHeap(), 0, need );
77 }
78
79 static inline void free_buffer( void *static_buffer, void *buffer )
80 {
81     if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
82 }
83
84 /* find an existing winproc for a given 16-bit function and type */
85 /* FIXME: probably should do something more clever than a linear search */
86 static inline WINDOWPROC *find_winproc16( WNDPROC16 func )
87 {
88     unsigned int i;
89
90     for (i = BUILTIN_WINPROCS; i < winproc_used; i++)
91     {
92         if (winproc_array[i].proc16 == func) return &winproc_array[i];
93     }
94     return NULL;
95 }
96
97 /* find an existing winproc for a given function and type */
98 /* FIXME: probably should do something more clever than a linear search */
99 static inline WINDOWPROC *find_winproc( WNDPROC funcA, WNDPROC funcW )
100 {
101     unsigned int i;
102
103     for (i = 0; i < builtin_used; i++)
104     {
105         /* match either proc, some apps confuse A and W */
106         if (funcA && winproc_array[i].procA != funcA && winproc_array[i].procW != funcA) continue;
107         if (funcW && winproc_array[i].procA != funcW && winproc_array[i].procW != funcW) continue;
108         return &winproc_array[i];
109     }
110     for (i = BUILTIN_WINPROCS; i < winproc_used; i++)
111     {
112         if (funcA && winproc_array[i].procA != funcA) continue;
113         if (funcW && winproc_array[i].procW != funcW) continue;
114         return &winproc_array[i];
115     }
116     return NULL;
117 }
118
119 /* return the window proc for a given handle, or NULL for an invalid handle */
120 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
121 {
122     UINT index = LOWORD(handle);
123     if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
124     if (index >= winproc_used) return NULL;
125     return &winproc_array[index];
126 }
127
128 /* create a handle for a given window proc */
129 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
130 {
131     return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
132 }
133
134 /* allocate and initialize a new winproc */
135 static inline WINDOWPROC *alloc_winproc( WNDPROC funcA, WNDPROC funcW )
136 {
137     WINDOWPROC *proc;
138
139     /* check if the function is already a win proc */
140     if (funcA && (proc = handle_to_proc( funcA ))) return proc;
141     if (funcW && (proc = handle_to_proc( funcW ))) return proc;
142     if (!funcA && !funcW) return NULL;
143
144     EnterCriticalSection( &winproc_cs );
145
146     /* check if we already have a winproc for that function */
147     if (!(proc = find_winproc( funcA, funcW )))
148     {
149         if (funcA && funcW)
150         {
151             assert( builtin_used < BUILTIN_WINPROCS );
152             proc = &winproc_array[builtin_used++];
153             proc->procA = funcA;
154             proc->procW = funcW;
155             TRACE( "allocated %p for builtin %p/%p (%d/%d used)\n",
156                    proc_to_handle(proc), funcA, funcW, builtin_used, BUILTIN_WINPROCS );
157         }
158         else if (winproc_used < MAX_WINPROCS)
159         {
160             proc = &winproc_array[winproc_used++];
161             proc->procA = funcA;
162             proc->procW = funcW;
163             TRACE( "allocated %p for %c %p (%d/%d used)\n",
164                    proc_to_handle(proc), funcA ? 'A' : 'W', funcA ? funcA : funcW,
165                    winproc_used, MAX_WINPROCS );
166         }
167         else FIXME( "too many winprocs, cannot allocate one for %p/%p\n", funcA, funcW );
168     }
169     else TRACE( "reusing %p for %p/%p\n", proc_to_handle(proc), funcA, funcW );
170
171     LeaveCriticalSection( &winproc_cs );
172     return proc;
173 }
174
175
176 #ifdef __i386__
177
178 #include "pshpack1.h"
179
180 /* Window procedure 16-to-32-bit thunk */
181 typedef struct
182 {
183     BYTE        popl_eax;        /* popl  %eax (return address) */
184     BYTE        pushl_func;      /* pushl $proc */
185     WINDOWPROC *proc;
186     BYTE        pushl_eax;       /* pushl %eax */
187     BYTE        ljmp;            /* ljmp relay*/
188     DWORD       relay_offset;    /* __wine_call_wndproc */
189     WORD        relay_sel;
190 } WINPROC_THUNK;
191
192 #include "poppack.h"
193
194 #define MAX_THUNKS  (0x10000 / sizeof(WINPROC_THUNK))
195
196 static WINPROC_THUNK *thunk_array;
197 static UINT thunk_selector;
198 static UINT thunk_used;
199
200 /* return the window proc for a given handle, or NULL for an invalid handle */
201 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
202 {
203     if (HIWORD(handle) == thunk_selector)
204     {
205         UINT index = LOWORD(handle) / sizeof(WINPROC_THUNK);
206         /* check alignment */
207         if (index * sizeof(WINPROC_THUNK) != LOWORD(handle)) return NULL;
208         /* check array limits */
209         if (index >= thunk_used) return NULL;
210         return thunk_array[index].proc;
211     }
212     return handle_to_proc( (WNDPROC)handle );
213 }
214
215 /* allocate a 16-bit thunk for an existing window proc */
216 static WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
217 {
218     static FARPROC16 relay;
219     UINT i;
220
221     if (proc->proc16) return proc->proc16;
222
223     EnterCriticalSection( &winproc_cs );
224
225     if (!thunk_array)  /* allocate the array and its selector */
226     {
227         LDT_ENTRY entry;
228
229         if (!(thunk_selector = wine_ldt_alloc_entries(1))) goto done;
230         if (!(thunk_array = VirtualAlloc( NULL, MAX_THUNKS * sizeof(WINPROC_THUNK), MEM_COMMIT,
231                                           PAGE_EXECUTE_READWRITE ))) goto done;
232         wine_ldt_set_base( &entry, thunk_array );
233         wine_ldt_set_limit( &entry, MAX_THUNKS * sizeof(WINPROC_THUNK) - 1 );
234         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
235         wine_ldt_set_entry( thunk_selector, &entry );
236         relay = GetProcAddress16( GetModuleHandle16("user"), "__wine_call_wndproc" );
237     }
238
239     /* check if it already exists */
240     for (i = 0; i < thunk_used; i++) if (thunk_array[i].proc == proc) break;
241
242     if (i == thunk_used)  /* create a new one */
243     {
244         WINPROC_THUNK *thunk;
245
246         if (thunk_used >= MAX_THUNKS) goto done;
247         thunk = &thunk_array[thunk_used++];
248         thunk->popl_eax     = 0x58;   /* popl  %eax */
249         thunk->pushl_func   = 0x68;   /* pushl $proc */
250         thunk->proc         = proc;
251         thunk->pushl_eax    = 0x50;   /* pushl %eax */
252         thunk->ljmp         = 0xea;   /* ljmp   relay*/
253         thunk->relay_offset = OFFSETOF(relay);
254         thunk->relay_sel    = SELECTOROF(relay);
255     }
256     proc->proc16 = (WNDPROC16)MAKESEGPTR( thunk_selector, i * sizeof(WINPROC_THUNK) );
257 done:
258     LeaveCriticalSection( &winproc_cs );
259     return proc->proc16;
260 }
261
262 #else  /* __i386__ */
263
264 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
265 {
266     return handle_to_proc( (WNDPROC)handle );
267 }
268
269 static inline WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
270 {
271     return 0;
272 }
273
274 #endif  /* __i386__ */
275
276
277 #ifdef __i386__
278 /* Some window procedures modify register they shouldn't, or are not
279  * properly declared stdcall; so we need a small assembly wrapper to
280  * call them. */
281 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
282                                 WPARAM wParam, LPARAM lParam );
283 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
284                    "pushl %ebp\n\t"
285                    __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
286                    __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
287                    "movl %esp,%ebp\n\t"
288                    __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
289                    "pushl %edi\n\t"
290                    __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
291                    "pushl %esi\n\t"
292                    __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
293                    "pushl %ebx\n\t"
294                    __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
295                    "subl $12,%esp\n\t"
296                    "pushl 24(%ebp)\n\t"
297                    "pushl 20(%ebp)\n\t"
298                    "pushl 16(%ebp)\n\t"
299                    "pushl 12(%ebp)\n\t"
300                    "movl 8(%ebp),%eax\n\t"
301                    "call *%eax\n\t"
302                    "leal -12(%ebp),%esp\n\t"
303                    "popl %ebx\n\t"
304                    __ASM_CFI(".cfi_same_value %ebx\n\t")
305                    "popl %esi\n\t"
306                    __ASM_CFI(".cfi_same_value %esi\n\t")
307                    "popl %edi\n\t"
308                    __ASM_CFI(".cfi_same_value %edi\n\t")
309                    "leave\n\t"
310                    __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
311                    __ASM_CFI(".cfi_same_value %ebp\n\t")
312                    "ret" )
313 #else
314 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
315                                        WPARAM wParam, LPARAM lParam )
316 {
317     return proc( hwnd, msg, wParam, lParam );
318 }
319 #endif  /* __i386__ */
320
321 static void RECT16to32( const RECT16 *from, RECT *to )
322 {
323     to->left   = from->left;
324     to->top    = from->top;
325     to->right  = from->right;
326     to->bottom = from->bottom;
327 }
328
329 static void RECT32to16( const RECT *from, RECT16 *to )
330 {
331     to->left   = from->left;
332     to->top    = from->top;
333     to->right  = from->right;
334     to->bottom = from->bottom;
335 }
336
337 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
338 {
339     to->ptReserved.x     = from->ptReserved.x;
340     to->ptReserved.y     = from->ptReserved.y;
341     to->ptMaxSize.x      = from->ptMaxSize.x;
342     to->ptMaxSize.y      = from->ptMaxSize.y;
343     to->ptMaxPosition.x  = from->ptMaxPosition.x;
344     to->ptMaxPosition.y  = from->ptMaxPosition.y;
345     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
346     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
347     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
348     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
349 }
350
351 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
352 {
353     to->ptReserved.x     = from->ptReserved.x;
354     to->ptReserved.y     = from->ptReserved.y;
355     to->ptMaxSize.x      = from->ptMaxSize.x;
356     to->ptMaxSize.y      = from->ptMaxSize.y;
357     to->ptMaxPosition.x  = from->ptMaxPosition.x;
358     to->ptMaxPosition.y  = from->ptMaxPosition.y;
359     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
360     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
361     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
362     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
363 }
364
365 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
366 {
367     to->hwnd            = HWND_16(from->hwnd);
368     to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
369     to->x               = from->x;
370     to->y               = from->y;
371     to->cx              = from->cx;
372     to->cy              = from->cy;
373     to->flags           = from->flags;
374 }
375
376 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
377 {
378     to->hwnd            = WIN_Handle32(from->hwnd);
379     to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
380                            HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
381     to->x               = from->x;
382     to->y               = from->y;
383     to->cx              = from->cx;
384     to->cy              = from->cy;
385     to->flags           = from->flags;
386 }
387
388 /* The strings are not copied */
389 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
390 {
391     to->lpCreateParams = (SEGPTR)from->lpCreateParams;
392     to->hInstance      = HINSTANCE_16(from->hInstance);
393     to->hMenu          = HMENU_16(from->hMenu);
394     to->hwndParent     = HWND_16(from->hwndParent);
395     to->cy             = from->cy;
396     to->cx             = from->cx;
397     to->y              = from->y;
398     to->x              = from->x;
399     to->style          = from->style;
400     to->dwExStyle      = from->dwExStyle;
401 }
402
403 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
404
405 {
406     to->lpCreateParams = (LPVOID)from->lpCreateParams;
407     to->hInstance      = HINSTANCE_32(from->hInstance);
408     to->hMenu          = HMENU_32(from->hMenu);
409     to->hwndParent     = WIN_Handle32(from->hwndParent);
410     to->cy             = from->cy;
411     to->cx             = from->cx;
412     to->y              = from->y;
413     to->x              = from->x;
414     to->style          = from->style;
415     to->dwExStyle      = from->dwExStyle;
416     to->lpszName       = MapSL(from->lpszName);
417     to->lpszClass      = MapSL(from->lpszClass);
418 }
419
420 /* The strings are not copied */
421 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
422 {
423     to->hOwner = HINSTANCE_16(from->hOwner);
424     to->x      = from->x;
425     to->y      = from->y;
426     to->cx     = from->cx;
427     to->cy     = from->cy;
428     to->style  = from->style;
429     to->lParam = from->lParam;
430 }
431
432 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
433 {
434     to->hOwner = HINSTANCE_32(from->hOwner);
435     to->x      = from->x;
436     to->y      = from->y;
437     to->cx     = from->cx;
438     to->cy     = from->cy;
439     to->style  = from->style;
440     to->lParam = from->lParam;
441     to->szTitle = MapSL(from->szTitle);
442     to->szClass = MapSL(from->szClass);
443 }
444
445 static WPARAM map_wparam_char_WtoA( WPARAM wParam, DWORD len )
446 {
447     WCHAR wch = wParam;
448     BYTE ch[2];
449
450     RtlUnicodeToMultiByteN( (LPSTR)ch, len, &len, &wch, sizeof(wch) );
451     if (len == 2)
452         return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
453     else
454         return MAKEWPARAM( ch[0], HIWORD(wParam) );
455 }
456
457 /* call a 32-bit window procedure */
458 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
459 {
460     WNDPROC proc = arg;
461
462     USER_CheckNotLock();
463
464     hwnd = WIN_GetFullHandle( hwnd );
465     if (TRACE_ON(relay))
466         DPRINTF( "%04x:Call window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
467                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
468
469     *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
470
471     if (TRACE_ON(relay))
472         DPRINTF( "%04x:Ret  window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx\n",
473                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
474     return *result;
475 }
476
477 /* call a 32-bit dialog procedure */
478 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
479 {
480     WNDPROC proc = arg;
481     LRESULT ret;
482
483     USER_CheckNotLock();
484
485     hwnd = WIN_GetFullHandle( hwnd );
486     if (TRACE_ON(relay))
487         DPRINTF( "%04x:Call dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
488                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
489
490     ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
491     *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
492
493     if (TRACE_ON(relay))
494         DPRINTF( "%04x:Ret  dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx result=%08lx\n",
495                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
496     return ret;
497 }
498
499 /* call a 16-bit window procedure */
500 static LRESULT call_window_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
501                                    LRESULT *result, void *arg )
502 {
503     WNDPROC16 proc = arg;
504     CONTEXT86 context;
505     size_t size = 0;
506     struct
507     {
508         WORD params[5];
509         union
510         {
511             CREATESTRUCT16 cs16;
512             DRAWITEMSTRUCT16 dis16;
513             COMPAREITEMSTRUCT16 cis16;
514         } u;
515     } args;
516
517     USER_CheckNotLock();
518
519     /* Window procedures want ax = hInstance, ds = es = ss */
520
521     memset(&context, 0, sizeof(context));
522     context.SegDs = context.SegEs = SELECTOROF(NtCurrentTeb()->WOW32Reserved);
523     context.SegFs = wine_get_fs();
524     context.SegGs = wine_get_gs();
525     if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
526     context.SegCs = SELECTOROF(proc);
527     context.Eip   = OFFSETOF(proc);
528     context.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME, bp);
529
530     if (lParam)
531     {
532         /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
533            work if structures passed in lParam are placed in the stack/data
534            segment. Programmers easily make the mistake of converting lParam
535            to a near rather than a far pointer, since Windows apparently
536            allows this. We copy the structures to the 16 bit stack; this is
537            ugly but makes these programs work. */
538         switch (msg)
539         {
540           case WM_CREATE:
541           case WM_NCCREATE:
542             size = sizeof(CREATESTRUCT16); break;
543           case WM_DRAWITEM:
544             size = sizeof(DRAWITEMSTRUCT16); break;
545           case WM_COMPAREITEM:
546             size = sizeof(COMPAREITEMSTRUCT16); break;
547         }
548         if (size)
549         {
550             memcpy( &args.u, MapSL(lParam), size );
551             lParam = PtrToUlong(NtCurrentTeb()->WOW32Reserved) - size;
552         }
553     }
554
555     args.params[4] = hwnd;
556     args.params[3] = msg;
557     args.params[2] = wParam;
558     args.params[1] = HIWORD(lParam);
559     args.params[0] = LOWORD(lParam);
560     WOWCallback16Ex( 0, WCB16_REGS, sizeof(args.params) + size, &args, (DWORD *)&context );
561     *result = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
562     return *result;
563 }
564
565 /* call a 16-bit dialog procedure */
566 static LRESULT call_dialog_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wp, LPARAM lp,
567                                    LRESULT *result, void *arg )
568 {
569     LRESULT ret = call_window_proc16( hwnd, msg, wp, lp, result, arg );
570     *result = GetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT );
571     return LOWORD(ret);
572 }
573
574 /* helper callback for 32W->16 conversion */
575 static LRESULT call_window_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
576                                        LRESULT *result, void *arg )
577 {
578     return WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wp, lp, result, arg );
579 }
580
581 /* helper callback for 32W->16 conversion */
582 static LRESULT call_dialog_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
583                                        LRESULT *result, void *arg )
584 {
585     return WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wp, lp, result, arg );
586 }
587
588 /* helper callback for 16->32W conversion */
589 static LRESULT call_window_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
590                                       LRESULT *result, void *arg )
591 {
592     return WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wp, lp, result,
593                                  arg, WMCHAR_MAP_CALLWINDOWPROC );
594 }
595
596 /* helper callback for 16->32W conversion */
597 static LRESULT call_dialog_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
598                                       LRESULT *result, void *arg )
599 {
600     return WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wp, lp, result,
601                                  arg, WMCHAR_MAP_CALLWINDOWPROC );
602 }
603
604
605 /**********************************************************************
606  *           WINPROC_GetProc16
607  *
608  * Get a window procedure pointer that can be passed to the Windows program.
609  */
610 WNDPROC16 WINPROC_GetProc16( WNDPROC proc, BOOL unicode )
611 {
612     WINDOWPROC *ptr;
613
614     if (unicode) ptr = alloc_winproc( NULL, proc );
615     else ptr = alloc_winproc( proc, NULL );
616
617     if (!ptr) return 0;
618     return alloc_win16_thunk( ptr );
619 }
620
621
622 /**********************************************************************
623  *           WINPROC_GetProc
624  *
625  * Get a window procedure pointer that can be passed to the Windows program.
626  */
627 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
628 {
629     WINDOWPROC *ptr = handle_to_proc( proc );
630
631     if (!ptr) return proc;
632     if (unicode)
633     {
634         if (ptr->procW) return ptr->procW;
635         return proc;
636     }
637     else
638     {
639         if (ptr->procA) return ptr->procA;
640         return proc;
641     }
642 }
643
644
645 /**********************************************************************
646  *           WINPROC_AllocProc16
647  *
648  * Allocate a window procedure for a window or class.
649  *
650  * Note that allocated winprocs are never freed; the idea is that even if an app creates a
651  * lot of windows, it will usually only have a limited number of window procedures, so the
652  * array won't grow too large, and this way we avoid the need to track allocations per window.
653  */
654 WNDPROC WINPROC_AllocProc16( WNDPROC16 func )
655 {
656     WINDOWPROC *proc;
657
658     if (!func) return NULL;
659
660     /* check if the function is already a win proc */
661     if (!(proc = handle16_to_proc( func )))
662     {
663         EnterCriticalSection( &winproc_cs );
664
665         /* then check if we already have a winproc for that function */
666         if (!(proc = find_winproc16( func )))
667         {
668             if (winproc_used < MAX_WINPROCS)
669             {
670                 proc = &winproc_array[winproc_used++];
671                 proc->proc16 = func;
672                 TRACE( "allocated %p for %p/16-bit (%d/%d used)\n",
673                        proc_to_handle(proc), func, winproc_used, MAX_WINPROCS );
674             }
675             else FIXME( "too many winprocs, cannot allocate one for 16-bit %p\n", func );
676         }
677         else TRACE( "reusing %p for %p/16-bit\n", proc_to_handle(proc), func );
678
679         LeaveCriticalSection( &winproc_cs );
680     }
681     return proc_to_handle( proc );
682 }
683
684
685 /**********************************************************************
686  *           WINPROC_AllocProc
687  *
688  * Allocate a window procedure for a window or class.
689  *
690  * Note that allocated winprocs are never freed; the idea is that even if an app creates a
691  * lot of windows, it will usually only have a limited number of window procedures, so the
692  * array won't grow too large, and this way we avoid the need to track allocations per window.
693  */
694 WNDPROC WINPROC_AllocProc( WNDPROC funcA, WNDPROC funcW )
695 {
696     WINDOWPROC *proc;
697
698     if (!(proc = alloc_winproc( funcA, funcW ))) return NULL;
699     return proc_to_handle( proc );
700 }
701
702
703 /**********************************************************************
704  *           WINPROC_IsUnicode
705  *
706  * Return the window procedure type, or the default value if not a winproc handle.
707  */
708 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
709 {
710     WINDOWPROC *ptr = handle_to_proc( proc );
711
712     if (!ptr) return def_val;
713     if (ptr->procA && ptr->procW) return def_val;  /* can be both */
714     return (ptr->procW != NULL);
715 }
716
717
718 /**********************************************************************
719  *           WINPROC_TestLBForStr
720  *
721  * Return TRUE if the lparam is a string
722  */
723 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
724 {
725     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
726     if (msg <= CB_MSGMAX)
727         return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
728     else
729         return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
730
731 }
732
733
734 static UINT_PTR convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
735 {
736     HANDLE      dst;
737     UINT        sz = GlobalSize16(src);
738     LPSTR       ptr16, ptr32;
739
740     if (!(dst = GlobalAlloc(flags, sz)))
741         return 0;
742     ptr16 = GlobalLock16(src);
743     ptr32 = GlobalLock(dst);
744     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
745     GlobalUnlock16(src);
746     GlobalUnlock(dst);
747
748     return (UINT_PTR)dst;
749 }
750
751 static HANDLE16 convert_handle_32_to_16(UINT_PTR src, unsigned int flags)
752 {
753     HANDLE16    dst;
754     UINT        sz = GlobalSize((HANDLE)src);
755     LPSTR       ptr16, ptr32;
756
757     if (!(dst = GlobalAlloc16(flags, sz)))
758         return 0;
759     ptr32 = GlobalLock((HANDLE)src);
760     ptr16 = GlobalLock16(dst);
761     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
762     GlobalUnlock((HANDLE)src);
763     GlobalUnlock16(dst);
764
765     return dst;
766 }
767
768
769 /**********************************************************************
770  *           WINPROC_CallProcAtoW
771  *
772  * Call a window procedure, translating args from Ansi to Unicode.
773  */
774 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
775                               LPARAM lParam, LRESULT *result, void *arg, enum wm_char_mapping mapping )
776 {
777     LRESULT ret = 0;
778
779     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
780                 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
781
782     switch(msg)
783     {
784     case WM_NCCREATE:
785     case WM_CREATE:
786         {
787             WCHAR *ptr, buffer[512];
788             CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
789             CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
790             MDICREATESTRUCTW mdi_cs;
791             DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
792
793             if (!IS_INTRESOURCE(csA->lpszClass))
794             {
795                 class_lenA = strlen(csA->lpszClass) + 1;
796                 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
797             }
798             if (!IS_INTRESOURCE(csA->lpszName))
799             {
800                 name_lenA = strlen(csA->lpszName) + 1;
801                 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
802             }
803
804             if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
805
806             if (class_lenW)
807             {
808                 csW.lpszClass = ptr;
809                 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
810             }
811             if (name_lenW)
812             {
813                 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
814                 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
815                                         csA->lpszName, name_lenA );
816             }
817
818             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
819             {
820                 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
821                 mdi_cs.szTitle = csW.lpszName;
822                 mdi_cs.szClass = csW.lpszClass;
823                 csW.lpCreateParams = &mdi_cs;
824             }
825
826             ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
827             free_buffer( buffer, ptr );
828         }
829         break;
830
831     case WM_MDICREATE:
832         {
833             WCHAR *ptr, buffer[512];
834             DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
835             MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
836             MDICREATESTRUCTW csW;
837
838             memcpy( &csW, csA, sizeof(csW) );
839
840             if (!IS_INTRESOURCE(csA->szTitle))
841             {
842                 title_lenA = strlen(csA->szTitle) + 1;
843                 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
844             }
845             if (!IS_INTRESOURCE(csA->szClass))
846             {
847                 class_lenA = strlen(csA->szClass) + 1;
848                 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
849             }
850
851             if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
852
853             if (title_lenW)
854             {
855                 csW.szTitle = ptr;
856                 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
857             }
858             if (class_lenW)
859             {
860                 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
861                 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
862                                         csA->szClass, class_lenA );
863             }
864             ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
865             free_buffer( buffer, ptr );
866         }
867         break;
868
869     case WM_GETTEXT:
870     case WM_ASKCBFORMATNAME:
871         {
872             WCHAR *ptr, buffer[512];
873             LPSTR str = (LPSTR)lParam;
874             DWORD len = wParam * sizeof(WCHAR);
875
876             if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
877             ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
878             if (wParam)
879             {
880                 len = 0;
881                 if (*result)
882                     RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, strlenW(ptr) * sizeof(WCHAR) );
883                 str[len] = 0;
884                 *result = len;
885             }
886             free_buffer( buffer, ptr );
887         }
888         break;
889
890     case LB_ADDSTRING:
891     case LB_INSERTSTRING:
892     case LB_FINDSTRING:
893     case LB_FINDSTRINGEXACT:
894     case LB_SELECTSTRING:
895     case CB_ADDSTRING:
896     case CB_INSERTSTRING:
897     case CB_FINDSTRING:
898     case CB_FINDSTRINGEXACT:
899     case CB_SELECTSTRING:
900         if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
901         {
902             ret = callback( hwnd, msg, wParam, lParam, result, arg );
903             break;
904         }
905         /* fall through */
906     case WM_SETTEXT:
907     case WM_WININICHANGE:
908     case WM_DEVMODECHANGE:
909     case CB_DIR:
910     case LB_DIR:
911     case LB_ADDFILE:
912     case EM_REPLACESEL:
913         if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
914         else
915         {
916             WCHAR *ptr, buffer[512];
917             LPCSTR strA = (LPCSTR)lParam;
918             DWORD lenW, lenA = strlen(strA) + 1;
919
920             RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
921             if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
922             {
923                 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
924                 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
925                 free_buffer( buffer, ptr );
926             }
927         }
928         break;
929
930     case LB_GETTEXT:
931     case CB_GETLBTEXT:
932         if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
933         {
934             WCHAR buffer[512];  /* FIXME: fixed sized buffer */
935
936             ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
937             if (*result >= 0)
938             {
939                 DWORD len;
940                 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
941                                         buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
942                 *result = len - 1;
943             }
944         }
945         else ret = callback( hwnd, msg, wParam, lParam, result, arg );
946         break;
947
948     case EM_GETLINE:
949         {
950             WCHAR *ptr, buffer[512];
951             WORD len = *(WORD *)lParam;
952
953             if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
954             *((WORD *)ptr) = len;   /* store the length */
955             ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
956             if (*result)
957             {
958                 DWORD reslen;
959                 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
960                 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
961                 *result = reslen;
962             }
963             free_buffer( buffer, ptr );
964         }
965         break;
966
967     case WM_GETDLGCODE:
968         if (lParam)
969         {
970             MSG newmsg = *(MSG *)lParam;
971             if (map_wparam_AtoW( newmsg.message, &newmsg.wParam, WMCHAR_MAP_NOMAPPING ))
972                 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
973         }
974         else ret = callback( hwnd, msg, wParam, lParam, result, arg );
975         break;
976
977     case WM_CHARTOITEM:
978     case WM_MENUCHAR:
979     case WM_CHAR:
980     case WM_DEADCHAR:
981     case WM_SYSCHAR:
982     case WM_SYSDEADCHAR:
983     case EM_SETPASSWORDCHAR:
984     case WM_IME_CHAR:
985         if (map_wparam_AtoW( msg, &wParam, mapping ))
986             ret = callback( hwnd, msg, wParam, lParam, result, arg );
987         break;
988
989     case WM_GETTEXTLENGTH:
990     case CB_GETLBTEXTLEN:
991     case LB_GETTEXTLEN:
992         ret = callback( hwnd, msg, wParam, lParam, result, arg );
993         if (*result >= 0)
994         {
995             WCHAR *ptr, buffer[512];
996             LRESULT tmp;
997             DWORD len = *result + 1;
998             /* Determine respective GETTEXT message */
999             UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
1000                               ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
1001             /* wParam differs between the messages */
1002             WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
1003
1004             if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
1005
1006             if (callback == call_window_proc)  /* FIXME: hack */
1007                 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
1008             else
1009                 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
1010             RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
1011             *result = len;
1012             free_buffer( buffer, ptr );
1013         }
1014         break;
1015
1016     case WM_PAINTCLIPBOARD:
1017     case WM_SIZECLIPBOARD:
1018         FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
1019                      SPY_GetMsgName(msg, hwnd), msg );
1020         break;
1021
1022     default:
1023         ret = callback( hwnd, msg, wParam, lParam, result, arg );
1024         break;
1025     }
1026     return ret;
1027 }
1028
1029
1030 /**********************************************************************
1031  *           WINPROC_CallProcWtoA
1032  *
1033  * Call a window procedure, translating args from Unicode to Ansi.
1034  */
1035 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
1036                                      LPARAM lParam, LRESULT *result, void *arg )
1037 {
1038     LRESULT ret = 0;
1039
1040     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
1041                 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1042
1043     switch(msg)
1044     {
1045     case WM_NCCREATE:
1046     case WM_CREATE:
1047         {
1048             char buffer[1024], *cls;
1049             CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
1050             CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
1051             MDICREATESTRUCTA mdi_cs;
1052             DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
1053
1054             if (!IS_INTRESOURCE(csW->lpszClass))
1055             {
1056                 class_lenW = (strlenW(csW->lpszClass) + 1) * sizeof(WCHAR);
1057                 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
1058             }
1059             if (!IS_INTRESOURCE(csW->lpszName))
1060             {
1061                 name_lenW = (strlenW(csW->lpszName) + 1) * sizeof(WCHAR);
1062                 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
1063             }
1064
1065             if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA ))) break;
1066
1067             if (class_lenA)
1068             {
1069                 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
1070                 csA.lpszClass = cls;
1071             }
1072             if (name_lenA)
1073             {
1074                 char *name = cls + class_lenA;
1075                 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
1076                 csA.lpszName = name;
1077             }
1078
1079             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1080             {
1081                 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
1082                 mdi_cs.szTitle = csA.lpszName;
1083                 mdi_cs.szClass = csA.lpszClass;
1084                 csA.lpCreateParams = &mdi_cs;
1085             }
1086
1087             ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1088             free_buffer( buffer, cls );
1089         }
1090         break;
1091
1092     case WM_GETTEXT:
1093     case WM_ASKCBFORMATNAME:
1094         {
1095             char *ptr, buffer[512];
1096             DWORD len = wParam * 2;
1097
1098             if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
1099             ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1100             if (len)
1101             {
1102                 if (*result)
1103                 {
1104                     RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
1105                     *result = len/sizeof(WCHAR) - 1;  /* do not count terminating null */
1106                 }
1107                 ((LPWSTR)lParam)[*result] = 0;
1108             }
1109             free_buffer( buffer, ptr );
1110         }
1111         break;
1112
1113     case LB_ADDSTRING:
1114     case LB_INSERTSTRING:
1115     case LB_FINDSTRING:
1116     case LB_FINDSTRINGEXACT:
1117     case LB_SELECTSTRING:
1118     case CB_ADDSTRING:
1119     case CB_INSERTSTRING:
1120     case CB_FINDSTRING:
1121     case CB_FINDSTRINGEXACT:
1122     case CB_SELECTSTRING:
1123         if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
1124         {
1125             ret = callback( hwnd, msg, wParam, lParam, result, arg );
1126             break;
1127         }
1128         /* fall through */
1129     case WM_SETTEXT:
1130     case WM_WININICHANGE:
1131     case WM_DEVMODECHANGE:
1132     case CB_DIR:
1133     case LB_DIR:
1134     case LB_ADDFILE:
1135     case EM_REPLACESEL:
1136         if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
1137         else
1138         {
1139             char *ptr, buffer[512];
1140             LPCWSTR strW = (LPCWSTR)lParam;
1141             DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
1142
1143             RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
1144             if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
1145             {
1146                 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
1147                 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1148                 free_buffer( buffer, ptr );
1149             }
1150         }
1151         break;
1152
1153     case WM_MDICREATE:
1154         {
1155             char *ptr, buffer[1024];
1156             DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
1157             MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1158             MDICREATESTRUCTA csA;
1159
1160             memcpy( &csA, csW, sizeof(csA) );
1161
1162             if (!IS_INTRESOURCE(csW->szTitle))
1163             {
1164                 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
1165                 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
1166             }
1167             if (!IS_INTRESOURCE(csW->szClass))
1168             {
1169                 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
1170                 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
1171             }
1172
1173             if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
1174
1175             if (title_lenA)
1176             {
1177                 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
1178                 csA.szTitle = ptr;
1179             }
1180             if (class_lenA)
1181             {
1182                 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
1183                 csA.szClass = ptr + title_lenA;
1184             }
1185             ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1186             free_buffer( buffer, ptr );
1187         }
1188         break;
1189
1190     case LB_GETTEXT:
1191     case CB_GETLBTEXT:
1192         if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
1193         {
1194             char buffer[512];  /* FIXME: fixed sized buffer */
1195
1196             ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
1197             if (*result >= 0)
1198             {
1199                 DWORD len;
1200                 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
1201                 *result = len / sizeof(WCHAR) - 1;
1202             }
1203         }
1204         else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1205         break;
1206
1207     case EM_GETLINE:
1208         {
1209             char *ptr, buffer[512];
1210             WORD len = *(WORD *)lParam;
1211
1212             if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
1213             *((WORD *)ptr) = len * 2;   /* store the length */
1214             ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1215             if (*result)
1216             {
1217                 DWORD reslen;
1218                 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
1219                 *result = reslen / sizeof(WCHAR);
1220                 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
1221             }
1222             free_buffer( buffer, ptr );
1223         }
1224         break;
1225
1226     case WM_GETDLGCODE:
1227         if (lParam)
1228         {
1229             MSG newmsg = *(MSG *)lParam;
1230             switch(newmsg.message)
1231             {
1232             case WM_CHAR:
1233             case WM_DEADCHAR:
1234             case WM_SYSCHAR:
1235             case WM_SYSDEADCHAR:
1236                 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
1237                 break;
1238             case WM_IME_CHAR:
1239                 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
1240                 break;
1241             }
1242             ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
1243         }
1244         else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1245         break;
1246
1247     case WM_CHAR:
1248         {
1249             WCHAR wch = wParam;
1250             char ch[2];
1251             DWORD len;
1252
1253             RtlUnicodeToMultiByteN( ch, 2, &len, &wch, sizeof(wch) );
1254             ret = callback( hwnd, msg, (BYTE)ch[0], lParam, result, arg );
1255             if (len == 2) ret = callback( hwnd, msg, (BYTE)ch[1], lParam, result, arg );
1256         }
1257         break;
1258
1259     case WM_CHARTOITEM:
1260     case WM_MENUCHAR:
1261     case WM_DEADCHAR:
1262     case WM_SYSCHAR:
1263     case WM_SYSDEADCHAR:
1264     case EM_SETPASSWORDCHAR:
1265         ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
1266         break;
1267
1268     case WM_IME_CHAR:
1269         ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
1270         break;
1271
1272     case WM_PAINTCLIPBOARD:
1273     case WM_SIZECLIPBOARD:
1274         FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
1275                      SPY_GetMsgName(msg, hwnd), msg );
1276         break;
1277
1278     default:
1279         ret = callback( hwnd, msg, wParam, lParam, result, arg );
1280         break;
1281     }
1282
1283     return ret;
1284 }
1285
1286
1287 /**********************************************************************
1288  *           WINPROC_CallProc16To32A
1289  */
1290 LRESULT WINPROC_CallProc16To32A( winproc_callback_t callback, HWND16 hwnd, UINT16 msg,
1291                                  WPARAM16 wParam, LPARAM lParam, LRESULT *result, void *arg )
1292 {
1293     LRESULT ret = 0;
1294     HWND hwnd32 = WIN_Handle32( hwnd );
1295
1296     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
1297                  hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
1298
1299     switch(msg)
1300     {
1301     case WM_NCCREATE:
1302     case WM_CREATE:
1303         {
1304             CREATESTRUCT16 *cs16 = MapSL(lParam);
1305             CREATESTRUCTA cs;
1306             MDICREATESTRUCTA mdi_cs;
1307
1308             CREATESTRUCT16to32A( cs16, &cs );
1309             if (GetWindowLongW(hwnd32, GWL_EXSTYLE) & WS_EX_MDICHILD)
1310             {
1311                 MDICREATESTRUCT16 *mdi_cs16 = MapSL(cs16->lpCreateParams);
1312                 MDICREATESTRUCT16to32A(mdi_cs16, &mdi_cs);
1313                 cs.lpCreateParams = &mdi_cs;
1314             }
1315             ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1316             CREATESTRUCT32Ato16( &cs, cs16 );
1317         }
1318         break;
1319     case WM_MDICREATE:
1320         {
1321             MDICREATESTRUCT16 *cs16 = MapSL(lParam);
1322             MDICREATESTRUCTA cs;
1323
1324             MDICREATESTRUCT16to32A( cs16, &cs );
1325             ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1326             MDICREATESTRUCT32Ato16( &cs, cs16 );
1327         }
1328         break;
1329     case WM_MDIACTIVATE:
1330         if (lParam)
1331             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32( HIWORD(lParam) ),
1332                             (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1333         else /* message sent to MDI client */
1334             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1335         break;
1336     case WM_MDIGETACTIVE:
1337         {
1338             BOOL maximized = FALSE;
1339             ret = callback( hwnd32, msg, wParam, (LPARAM)&maximized, result, arg );
1340             *result = MAKELRESULT( LOWORD(*result), maximized );
1341         }
1342         break;
1343     case WM_MDISETMENU:
1344         ret = callback( hwnd32, wParam ? WM_MDIREFRESHMENU : WM_MDISETMENU,
1345                         (WPARAM)HMENU_32(LOWORD(lParam)), (LPARAM)HMENU_32(HIWORD(lParam)),
1346                         result, arg );
1347         break;
1348     case WM_GETMINMAXINFO:
1349         {
1350             MINMAXINFO16 *mmi16 = MapSL(lParam);
1351             MINMAXINFO mmi;
1352
1353             MINMAXINFO16to32( mmi16, &mmi );
1354             ret = callback( hwnd32, msg, wParam, (LPARAM)&mmi, result, arg );
1355             MINMAXINFO32to16( &mmi, mmi16 );
1356         }
1357         break;
1358     case WM_WINDOWPOSCHANGING:
1359     case WM_WINDOWPOSCHANGED:
1360         {
1361             WINDOWPOS16 *winpos16 = MapSL(lParam);
1362             WINDOWPOS winpos;
1363
1364             WINDOWPOS16to32( winpos16, &winpos );
1365             ret = callback( hwnd32, msg, wParam, (LPARAM)&winpos, result, arg );
1366             WINDOWPOS32to16( &winpos, winpos16 );
1367         }
1368         break;
1369     case WM_NCCALCSIZE:
1370         {
1371             NCCALCSIZE_PARAMS16 *nc16 = MapSL(lParam);
1372             NCCALCSIZE_PARAMS nc;
1373             WINDOWPOS winpos;
1374
1375             RECT16to32( &nc16->rgrc[0], &nc.rgrc[0] );
1376             if (wParam)
1377             {
1378                 RECT16to32( &nc16->rgrc[1], &nc.rgrc[1] );
1379                 RECT16to32( &nc16->rgrc[2], &nc.rgrc[2] );
1380                 WINDOWPOS16to32( MapSL(nc16->lppos), &winpos );
1381                 nc.lppos = &winpos;
1382             }
1383             ret = callback( hwnd32, msg, wParam, (LPARAM)&nc, result, arg );
1384             RECT32to16( &nc.rgrc[0], &nc16->rgrc[0] );
1385             if (wParam)
1386             {
1387                 RECT32to16( &nc.rgrc[1], &nc16->rgrc[1] );
1388                 RECT32to16( &nc.rgrc[2], &nc16->rgrc[2] );
1389                 WINDOWPOS32to16( &winpos, MapSL(nc16->lppos) );
1390             }
1391         }
1392         break;
1393     case WM_COMPAREITEM:
1394         {
1395             COMPAREITEMSTRUCT16* cis16 = MapSL(lParam);
1396             COMPAREITEMSTRUCT cis;
1397             cis.CtlType    = cis16->CtlType;
1398             cis.CtlID      = cis16->CtlID;
1399             cis.hwndItem   = WIN_Handle32( cis16->hwndItem );
1400             cis.itemID1    = cis16->itemID1;
1401             cis.itemData1  = cis16->itemData1;
1402             cis.itemID2    = cis16->itemID2;
1403             cis.itemData2  = cis16->itemData2;
1404             cis.dwLocaleId = 0;  /* FIXME */
1405             ret = callback( hwnd32, msg, wParam, (LPARAM)&cis, result, arg );
1406         }
1407         break;
1408     case WM_DELETEITEM:
1409         {
1410             DELETEITEMSTRUCT16* dis16 = MapSL(lParam);
1411             DELETEITEMSTRUCT dis;
1412             dis.CtlType  = dis16->CtlType;
1413             dis.CtlID    = dis16->CtlID;
1414             dis.hwndItem = WIN_Handle32( dis16->hwndItem );
1415             dis.itemData = dis16->itemData;
1416             ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1417         }
1418         break;
1419     case WM_MEASUREITEM:
1420         {
1421             MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
1422             MEASUREITEMSTRUCT mis;
1423             mis.CtlType    = mis16->CtlType;
1424             mis.CtlID      = mis16->CtlID;
1425             mis.itemID     = mis16->itemID;
1426             mis.itemWidth  = mis16->itemWidth;
1427             mis.itemHeight = mis16->itemHeight;
1428             mis.itemData   = mis16->itemData;
1429             ret = callback( hwnd32, msg, wParam, (LPARAM)&mis, result, arg );
1430             mis16->itemWidth  = (UINT16)mis.itemWidth;
1431             mis16->itemHeight = (UINT16)mis.itemHeight;
1432         }
1433         break;
1434     case WM_DRAWITEM:
1435         {
1436             DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
1437             DRAWITEMSTRUCT dis;
1438             dis.CtlType       = dis16->CtlType;
1439             dis.CtlID         = dis16->CtlID;
1440             dis.itemID        = dis16->itemID;
1441             dis.itemAction    = dis16->itemAction;
1442             dis.itemState     = dis16->itemState;
1443             dis.hwndItem      = (dis.CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1444                                                           : WIN_Handle32( dis16->hwndItem );
1445             dis.hDC           = HDC_32(dis16->hDC);
1446             dis.itemData      = dis16->itemData;
1447             dis.rcItem.left   = dis16->rcItem.left;
1448             dis.rcItem.top    = dis16->rcItem.top;
1449             dis.rcItem.right  = dis16->rcItem.right;
1450             dis.rcItem.bottom = dis16->rcItem.bottom;
1451             ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1452         }
1453         break;
1454     case WM_COPYDATA:
1455         {
1456             COPYDATASTRUCT16 *cds16 = MapSL(lParam);
1457             COPYDATASTRUCT cds;
1458             cds.dwData = cds16->dwData;
1459             cds.cbData = cds16->cbData;
1460             cds.lpData = MapSL(cds16->lpData);
1461             ret = callback( hwnd32, msg, wParam, (LPARAM)&cds, result, arg );
1462         }
1463         break;
1464     case WM_GETDLGCODE:
1465         if (lParam)
1466         {
1467             MSG16 *msg16 = MapSL(lParam);
1468             MSG msg32;
1469             msg32.hwnd    = WIN_Handle32( msg16->hwnd );
1470             msg32.message = msg16->message;
1471             msg32.wParam  = msg16->wParam;
1472             msg32.lParam  = msg16->lParam;
1473             msg32.time    = msg16->time;
1474             msg32.pt.x    = msg16->pt.x;
1475             msg32.pt.y    = msg16->pt.y;
1476             ret = callback( hwnd32, msg, wParam, (LPARAM)&msg32, result, arg );
1477         }
1478         else
1479             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1480         break;
1481     case WM_NEXTMENU:
1482         {
1483             MDINEXTMENU next;
1484             next.hmenuIn   = (HMENU)lParam;
1485             next.hmenuNext = 0;
1486             next.hwndNext  = 0;
1487             ret = callback( hwnd32, msg, wParam, (LPARAM)&next, result, arg );
1488             *result = MAKELONG( HMENU_16(next.hmenuNext), HWND_16(next.hwndNext) );
1489         }
1490         break;
1491     case WM_ACTIVATE:
1492     case WM_CHARTOITEM:
1493     case WM_COMMAND:
1494     case WM_VKEYTOITEM:
1495         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1496                         (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1497         break;
1498     case WM_HSCROLL:
1499     case WM_VSCROLL:
1500         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1501                         (LPARAM)WIN_Handle32( HIWORD(lParam) ), result, arg );
1502         break;
1503     case WM_CTLCOLOR:
1504         if (HIWORD(lParam) <= CTLCOLOR_STATIC)
1505             ret = callback( hwnd32, WM_CTLCOLORMSGBOX + HIWORD(lParam),
1506                             (WPARAM)HDC_32(wParam), (LPARAM)WIN_Handle32( LOWORD(lParam) ),
1507                             result, arg );
1508         break;
1509     case WM_GETTEXT:
1510     case WM_SETTEXT:
1511     case WM_WININICHANGE:
1512     case WM_DEVMODECHANGE:
1513     case WM_ASKCBFORMATNAME:
1514     case WM_NOTIFY:
1515         ret = callback( hwnd32, msg, wParam, (LPARAM)MapSL(lParam), result, arg );
1516         break;
1517     case WM_MENUCHAR:
1518         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1519                         (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1520         break;
1521     case WM_MENUSELECT:
1522         if((LOWORD(lParam) & MF_POPUP) && (LOWORD(lParam) != 0xFFFF))
1523         {
1524             HMENU hmenu = HMENU_32(HIWORD(lParam));
1525             UINT pos = MENU_FindSubMenu( &hmenu, HMENU_32(wParam) );
1526             if (pos == 0xffff) pos = 0;  /* NO_SELECTED_ITEM */
1527             wParam = pos;
1528         }
1529         ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1530                         (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1531         break;
1532     case WM_PARENTNOTIFY:
1533         if ((wParam == WM_CREATE) || (wParam == WM_DESTROY))
1534             ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1535                             (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1536         else
1537             ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1538         break;
1539     case WM_ACTIVATEAPP:
1540         /* We need this when SetActiveWindow sends a Sendmessage16() to
1541          * a 32-bit window. Might be superfluous with 32-bit interprocess
1542          * message queues. */
1543         if (lParam) lParam = HTASK_32(lParam);
1544         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1545         break;
1546     case WM_DDE_INITIATE:
1547     case WM_DDE_TERMINATE:
1548     case WM_DDE_UNADVISE:
1549     case WM_DDE_REQUEST:
1550         ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1551         break;
1552     case WM_DDE_ADVISE:
1553     case WM_DDE_DATA:
1554     case WM_DDE_POKE:
1555         {
1556             HANDLE16 lo16 = LOWORD(lParam);
1557             UINT_PTR lo32 = 0;
1558             if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE))) break;
1559             lParam = PackDDElParam( msg, lo32, HIWORD(lParam) );
1560             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1561         }
1562         break; /* FIXME don't know how to free allocated memory (handle)  !! */
1563     case WM_DDE_ACK:
1564         {
1565             UINT_PTR lo = LOWORD(lParam);
1566             UINT_PTR hi = HIWORD(lParam);
1567             int flag = 0;
1568             char buf[2];
1569
1570             if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1571             if (GlobalSize16(hi) != 0) flag |= 2;
1572             switch (flag)
1573             {
1574             case 0:
1575                 if (hi)
1576                 {
1577                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1578                     hi = 0;
1579                 }
1580                 break;
1581             case 1:
1582                 break; /* atom, nothing to do */
1583             case 3:
1584                 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
1585                 /* fall thru */
1586             case 2:
1587                 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1588                 break;
1589             }
1590             lParam = PackDDElParam( WM_DDE_ACK, lo, hi );
1591             ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1592         }
1593         break; /* FIXME don't know how to free allocated memory (handle) !! */
1594     case WM_DDE_EXECUTE:
1595         lParam = convert_handle_16_to_32( lParam, GMEM_DDESHARE );
1596         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1597         break; /* FIXME don't know how to free allocated memory (handle) !! */
1598     case WM_PAINTCLIPBOARD:
1599     case WM_SIZECLIPBOARD:
1600         FIXME_(msg)( "message %04x needs translation\n", msg );
1601         break;
1602     default:
1603         ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1604         break;
1605     }
1606     return ret;
1607 }
1608
1609
1610 /**********************************************************************
1611  *           __wine_call_wndproc   (USER.1010)
1612  */
1613 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
1614                                     WINDOWPROC *proc )
1615 {
1616     LRESULT result;
1617
1618     if (proc->procA)
1619         WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1620     else
1621         WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
1622     return result;
1623 }
1624
1625
1626 /**********************************************************************
1627  *           WINPROC_CallProc32ATo16
1628  *
1629  * Call a 16-bit window procedure, translating the 32-bit args.
1630  */
1631 LRESULT WINPROC_CallProc32ATo16( winproc_callback16_t callback, HWND hwnd, UINT msg,
1632                                  WPARAM wParam, LPARAM lParam, LRESULT *result, void *arg )
1633 {
1634     LRESULT ret = 0;
1635
1636     TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
1637                 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1638
1639     switch(msg)
1640     {
1641     case WM_NCCREATE:
1642     case WM_CREATE:
1643         {
1644             CREATESTRUCTA *cs32 = (CREATESTRUCTA *)lParam;
1645             CREATESTRUCT16 cs;
1646             MDICREATESTRUCT16 mdi_cs16;
1647             BOOL mdi_child = (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD);
1648
1649             CREATESTRUCT32Ato16( cs32, &cs );
1650             cs.lpszName  = MapLS( cs32->lpszName );
1651             cs.lpszClass = MapLS( cs32->lpszClass );
1652
1653             if (mdi_child)
1654             {
1655                 MDICREATESTRUCTA *mdi_cs = cs32->lpCreateParams;
1656                 MDICREATESTRUCT32Ato16( mdi_cs, &mdi_cs16 );
1657                 mdi_cs16.szTitle = MapLS( mdi_cs->szTitle );
1658                 mdi_cs16.szClass = MapLS( mdi_cs->szClass );
1659                 cs.lpCreateParams = MapLS( &mdi_cs16 );
1660             }
1661             lParam = MapLS( &cs );
1662             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1663             UnMapLS( lParam );
1664             UnMapLS( cs.lpszName );
1665             UnMapLS( cs.lpszClass );
1666             if (mdi_child)
1667             {
1668                 UnMapLS( cs.lpCreateParams );
1669                 UnMapLS( mdi_cs16.szTitle );
1670                 UnMapLS( mdi_cs16.szClass );
1671             }
1672         }
1673         break;
1674     case WM_MDICREATE:
1675         {
1676             MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)lParam;
1677             MDICREATESTRUCT16 cs;
1678
1679             MDICREATESTRUCT32Ato16( cs32, &cs );
1680             cs.szTitle = MapLS( cs32->szTitle );
1681             cs.szClass = MapLS( cs32->szClass );
1682             lParam = MapLS( &cs );
1683             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1684             UnMapLS( lParam );
1685             UnMapLS( cs.szTitle );
1686             UnMapLS( cs.szClass );
1687         }
1688         break;
1689     case WM_MDIACTIVATE:
1690         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
1691             ret = callback( HWND_16(hwnd), msg, ((HWND)lParam == hwnd),
1692                             MAKELPARAM( LOWORD(lParam), LOWORD(wParam) ), result, arg );
1693         else
1694             ret = callback( HWND_16(hwnd), msg, HWND_16( wParam ), 0, result, arg );
1695         break;
1696     case WM_MDIGETACTIVE:
1697         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1698         if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(*result);
1699         *result = (LRESULT)WIN_Handle32( LOWORD(*result) );
1700         break;
1701     case WM_MDISETMENU:
1702         ret = callback( HWND_16(hwnd), msg, (lParam == 0),
1703                         MAKELPARAM( LOWORD(wParam), LOWORD(lParam) ), result, arg );
1704         break;
1705     case WM_GETMINMAXINFO:
1706         {
1707             MINMAXINFO *mmi32 = (MINMAXINFO *)lParam;
1708             MINMAXINFO16 mmi;
1709
1710             MINMAXINFO32to16( mmi32, &mmi );
1711             lParam = MapLS( &mmi );
1712             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1713             UnMapLS( lParam );
1714             MINMAXINFO16to32( &mmi, mmi32 );
1715         }
1716         break;
1717     case WM_NCCALCSIZE:
1718         {
1719             NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)lParam;
1720             NCCALCSIZE_PARAMS16 nc;
1721             WINDOWPOS16 winpos;
1722
1723             RECT32to16( &nc32->rgrc[0], &nc.rgrc[0] );
1724             if (wParam)
1725             {
1726                 RECT32to16( &nc32->rgrc[1], &nc.rgrc[1] );
1727                 RECT32to16( &nc32->rgrc[2], &nc.rgrc[2] );
1728                 WINDOWPOS32to16( nc32->lppos, &winpos );
1729                 nc.lppos = MapLS( &winpos );
1730             }
1731             lParam = MapLS( &nc );
1732             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1733             UnMapLS( lParam );
1734             RECT16to32( &nc.rgrc[0], &nc32->rgrc[0] );
1735             if (wParam)
1736             {
1737                 RECT16to32( &nc.rgrc[1], &nc32->rgrc[1] );
1738                 RECT16to32( &nc.rgrc[2], &nc32->rgrc[2] );
1739                 WINDOWPOS16to32( &winpos, nc32->lppos );
1740                 UnMapLS( nc.lppos );
1741             }
1742         }
1743         break;
1744     case WM_WINDOWPOSCHANGING:
1745     case WM_WINDOWPOSCHANGED:
1746         {
1747             WINDOWPOS *winpos32 = (WINDOWPOS *)lParam;
1748             WINDOWPOS16 winpos;
1749
1750             WINDOWPOS32to16( winpos32, &winpos );
1751             lParam = MapLS( &winpos );
1752             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1753             UnMapLS( lParam );
1754             WINDOWPOS16to32( &winpos, winpos32 );
1755         }
1756         break;
1757     case WM_COMPAREITEM:
1758         {
1759             COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)lParam;
1760             COMPAREITEMSTRUCT16 cis;
1761             cis.CtlType    = cis32->CtlType;
1762             cis.CtlID      = cis32->CtlID;
1763             cis.hwndItem   = HWND_16( cis32->hwndItem );
1764             cis.itemID1    = cis32->itemID1;
1765             cis.itemData1  = cis32->itemData1;
1766             cis.itemID2    = cis32->itemID2;
1767             cis.itemData2  = cis32->itemData2;
1768             lParam = MapLS( &cis );
1769             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1770             UnMapLS( lParam );
1771         }
1772         break;
1773     case WM_DELETEITEM:
1774         {
1775             DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)lParam;
1776             DELETEITEMSTRUCT16 dis;
1777             dis.CtlType  = dis32->CtlType;
1778             dis.CtlID    = dis32->CtlID;
1779             dis.itemID   = dis32->itemID;
1780             dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
1781                                                      : HWND_16( dis32->hwndItem );
1782             dis.itemData = dis32->itemData;
1783             lParam = MapLS( &dis );
1784             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1785             UnMapLS( lParam );
1786         }
1787         break;
1788     case WM_DRAWITEM:
1789         {
1790             DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)lParam;
1791             DRAWITEMSTRUCT16 dis;
1792             dis.CtlType       = dis32->CtlType;
1793             dis.CtlID         = dis32->CtlID;
1794             dis.itemID        = dis32->itemID;
1795             dis.itemAction    = dis32->itemAction;
1796             dis.itemState     = dis32->itemState;
1797             dis.hwndItem      = HWND_16( dis32->hwndItem );
1798             dis.hDC           = HDC_16(dis32->hDC);
1799             dis.itemData      = dis32->itemData;
1800             dis.rcItem.left   = dis32->rcItem.left;
1801             dis.rcItem.top    = dis32->rcItem.top;
1802             dis.rcItem.right  = dis32->rcItem.right;
1803             dis.rcItem.bottom = dis32->rcItem.bottom;
1804             lParam = MapLS( &dis );
1805             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1806             UnMapLS( lParam );
1807         }
1808         break;
1809     case WM_MEASUREITEM:
1810         {
1811             MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)lParam;
1812             MEASUREITEMSTRUCT16 mis;
1813             mis.CtlType    = mis32->CtlType;
1814             mis.CtlID      = mis32->CtlID;
1815             mis.itemID     = mis32->itemID;
1816             mis.itemWidth  = mis32->itemWidth;
1817             mis.itemHeight = mis32->itemHeight;
1818             mis.itemData   = mis32->itemData;
1819             lParam = MapLS( &mis );
1820             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1821             UnMapLS( lParam );
1822             mis32->itemWidth  = mis.itemWidth;
1823             mis32->itemHeight = mis.itemHeight;
1824         }
1825         break;
1826     case WM_COPYDATA:
1827         {
1828             COPYDATASTRUCT *cds32 = (COPYDATASTRUCT *)lParam;
1829             COPYDATASTRUCT16 cds;
1830
1831             cds.dwData = cds32->dwData;
1832             cds.cbData = cds32->cbData;
1833             cds.lpData = MapLS( cds32->lpData );
1834             lParam = MapLS( &cds );
1835             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1836             UnMapLS( lParam );
1837             UnMapLS( cds.lpData );
1838         }
1839         break;
1840     case WM_GETDLGCODE:
1841         if (lParam)
1842         {
1843             MSG *msg32 = (MSG *)lParam;
1844             MSG16 msg16;
1845
1846             msg16.hwnd    = HWND_16( msg32->hwnd );
1847             msg16.message = msg32->message;
1848             msg16.wParam  = msg32->wParam;
1849             msg16.lParam  = msg32->lParam;
1850             msg16.time    = msg32->time;
1851             msg16.pt.x    = msg32->pt.x;
1852             msg16.pt.y    = msg32->pt.y;
1853             lParam = MapLS( &msg16 );
1854             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1855             UnMapLS( lParam );
1856         }
1857         else
1858             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1859         break;
1860     case WM_NEXTMENU:
1861         {
1862             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1863             ret = callback( HWND_16(hwnd), msg, wParam, (LPARAM)next->hmenuIn, result, arg );
1864             next->hmenuNext = HMENU_32( LOWORD(*result) );
1865             next->hwndNext  = WIN_Handle32( HIWORD(*result) );
1866             *result = 0;
1867         }
1868         break;
1869     case WM_GETTEXT:
1870     case WM_ASKCBFORMATNAME:
1871         wParam = min( wParam, 0xff80 ); /* Must be < 64K */
1872         /* fall through */
1873     case WM_NOTIFY:
1874     case WM_SETTEXT:
1875     case WM_WININICHANGE:
1876     case WM_DEVMODECHANGE:
1877         lParam = MapLS( (void *)lParam );
1878         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1879         UnMapLS( lParam );
1880         break;
1881     case WM_ACTIVATE:
1882     case WM_CHARTOITEM:
1883     case WM_COMMAND:
1884     case WM_VKEYTOITEM:
1885         ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ),
1886                         result, arg );
1887         break;
1888     case WM_HSCROLL:
1889     case WM_VSCROLL:
1890         ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( HIWORD(wParam), (HWND16)lParam ),
1891                         result, arg );
1892         break;
1893     case WM_CTLCOLORMSGBOX:
1894     case WM_CTLCOLOREDIT:
1895     case WM_CTLCOLORLISTBOX:
1896     case WM_CTLCOLORBTN:
1897     case WM_CTLCOLORDLG:
1898     case WM_CTLCOLORSCROLLBAR:
1899     case WM_CTLCOLORSTATIC:
1900         ret = callback( HWND_16(hwnd), WM_CTLCOLOR, wParam,
1901                         MAKELPARAM( (HWND16)lParam, msg - WM_CTLCOLORMSGBOX ), result, arg );
1902         break;
1903     case WM_MENUSELECT:
1904         if(HIWORD(wParam) & MF_POPUP)
1905         {
1906             HMENU hmenu;
1907             if ((HIWORD(wParam) != 0xffff) || lParam)
1908             {
1909                 if ((hmenu = GetSubMenu( (HMENU)lParam, LOWORD(wParam) )))
1910                 {
1911                     ret = callback( HWND_16(hwnd), msg, HMENU_16(hmenu),
1912                                     MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1913                     break;
1914                 }
1915             }
1916         }
1917         /* fall through */
1918     case WM_MENUCHAR:
1919         ret = callback( HWND_16(hwnd), msg, wParam,
1920                         MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1921         break;
1922     case WM_PARENTNOTIFY:
1923         if ((LOWORD(wParam) == WM_CREATE) || (LOWORD(wParam) == WM_DESTROY))
1924             ret = callback( HWND_16(hwnd), msg, wParam,
1925                             MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ), result, arg );
1926         else
1927             ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1928         break;
1929     case WM_ACTIVATEAPP:
1930         ret = callback( HWND_16(hwnd), msg, wParam, HTASK_16( lParam ), result, arg );
1931         break;
1932     case WM_PAINT:
1933         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
1934             ret = callback( HWND_16(hwnd), WM_PAINTICON, 1, lParam, result, arg );
1935         else
1936             ret = callback( HWND_16(hwnd), WM_PAINT, wParam, lParam, result, arg );
1937         break;
1938     case WM_ERASEBKGND:
1939         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON )) msg = WM_ICONERASEBKGND;
1940         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1941         break;
1942     case WM_DDE_INITIATE:
1943     case WM_DDE_TERMINATE:
1944     case WM_DDE_UNADVISE:
1945     case WM_DDE_REQUEST:
1946         ret = callback( HWND_16(hwnd), msg, HWND_16(wParam), lParam, result, arg );
1947         break;
1948     case WM_DDE_ADVISE:
1949     case WM_DDE_DATA:
1950     case WM_DDE_POKE:
1951         {
1952             UINT_PTR lo32, hi;
1953             HANDLE16 lo16 = 0;
1954
1955             UnpackDDElParam( msg, lParam, &lo32, &hi );
1956             if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE))) break;
1957             ret = callback( HWND_16(hwnd), msg, HWND_16(wParam),
1958                             MAKELPARAM(lo16, hi), result, arg );
1959         }
1960         break; /* FIXME don't know how to free allocated memory (handle)  !! */
1961     case WM_DDE_ACK:
1962         {
1963             UINT_PTR lo, hi;
1964             int flag = 0;
1965             char buf[2];
1966
1967             UnpackDDElParam( msg, lParam, &lo, &hi );
1968
1969             if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
1970             if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
1971             switch (flag)
1972             {
1973             case 0:
1974                 if (hi)
1975                 {
1976                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1977                     hi = 0;
1978                 }
1979                 break;
1980             case 1:
1981                 break; /* atom, nothing to do */
1982             case 3:
1983                 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
1984                 /* fall thru */
1985             case 2:
1986                 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
1987                 break;
1988             }
1989             ret = callback( HWND_16(hwnd), msg, HWND_16(wParam),
1990                             MAKELPARAM(lo, hi), result, arg );
1991         }
1992         break; /* FIXME don't know how to free allocated memory (handle) !! */
1993     case WM_DDE_EXECUTE:
1994         lParam = convert_handle_32_to_16(lParam, GMEM_DDESHARE);
1995         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1996         break; /* FIXME don't know how to free allocated memory (handle) !! */
1997     case SBM_SETRANGE:
1998         ret = callback( HWND_16(hwnd), SBM_SETRANGE16, 0, MAKELPARAM(wParam, lParam), result, arg );
1999         break;
2000     case SBM_GETRANGE:
2001         ret = callback( HWND_16(hwnd), SBM_GETRANGE16, wParam, lParam, result, arg );
2002         *(LPINT)wParam = LOWORD(*result);
2003         *(LPINT)lParam = HIWORD(*result);
2004         break;
2005     case BM_GETCHECK:
2006     case BM_SETCHECK:
2007     case BM_GETSTATE:
2008     case BM_SETSTATE:
2009     case BM_SETSTYLE:
2010         ret = callback( HWND_16(hwnd), msg + BM_GETCHECK16 - BM_GETCHECK, wParam, lParam, result, arg );
2011         break;
2012     case EM_GETSEL:
2013     case EM_GETRECT:
2014     case EM_SETRECT:
2015     case EM_SETRECTNP:
2016     case EM_SCROLL:
2017     case EM_LINESCROLL:
2018     case EM_SCROLLCARET:
2019     case EM_GETMODIFY:
2020     case EM_SETMODIFY:
2021     case EM_GETLINECOUNT:
2022     case EM_LINEINDEX:
2023     case EM_SETHANDLE:
2024     case EM_GETHANDLE:
2025     case EM_GETTHUMB:
2026     case EM_LINELENGTH:
2027     case EM_REPLACESEL:
2028     case EM_GETLINE:
2029     case EM_LIMITTEXT:
2030     case EM_CANUNDO:
2031     case EM_UNDO:
2032     case EM_FMTLINES:
2033     case EM_LINEFROMCHAR:
2034     case EM_SETTABSTOPS:
2035     case EM_SETPASSWORDCHAR:
2036     case EM_EMPTYUNDOBUFFER:
2037     case EM_GETFIRSTVISIBLELINE:
2038     case EM_SETREADONLY:
2039     case EM_SETWORDBREAKPROC:
2040     case EM_GETWORDBREAKPROC:
2041     case EM_GETPASSWORDCHAR:
2042         ret = callback( HWND_16(hwnd), msg + EM_GETSEL16 - EM_GETSEL, wParam, lParam, result, arg );
2043         break;
2044     case EM_SETSEL:
2045         ret = callback( HWND_16(hwnd), EM_SETSEL16, 0, MAKELPARAM( wParam, lParam ), result, arg );
2046         break;
2047     case LB_CARETOFF:
2048     case LB_CARETON:
2049     case LB_DELETESTRING:
2050     case LB_GETANCHORINDEX:
2051     case LB_GETCARETINDEX:
2052     case LB_GETCOUNT:
2053     case LB_GETCURSEL:
2054     case LB_GETHORIZONTALEXTENT:
2055     case LB_GETITEMDATA:
2056     case LB_GETITEMHEIGHT:
2057     case LB_GETSEL:
2058     case LB_GETSELCOUNT:
2059     case LB_GETTEXTLEN:
2060     case LB_GETTOPINDEX:
2061     case LB_RESETCONTENT:
2062     case LB_SELITEMRANGE:
2063     case LB_SELITEMRANGEEX:
2064     case LB_SETANCHORINDEX:
2065     case LB_SETCARETINDEX:
2066     case LB_SETCOLUMNWIDTH:
2067     case LB_SETCURSEL:
2068     case LB_SETHORIZONTALEXTENT:
2069     case LB_SETITEMDATA:
2070     case LB_SETITEMHEIGHT:
2071     case LB_SETSEL:
2072     case LB_SETTOPINDEX:
2073         ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2074         break;
2075     case LB_ADDSTRING:
2076     case LB_FINDSTRING:
2077     case LB_FINDSTRINGEXACT:
2078     case LB_INSERTSTRING:
2079     case LB_SELECTSTRING:
2080     case LB_GETTEXT:
2081     case LB_DIR:
2082     case LB_ADDFILE:
2083         lParam = MapLS( (LPSTR)lParam );
2084         ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2085         UnMapLS( lParam );
2086         break;
2087     case LB_GETSELITEMS:
2088         {
2089             INT *items32 = (INT *)lParam;
2090             INT16 *items, buffer[512];
2091             unsigned int i;
2092
2093             wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2094             if (!(items = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2095             lParam = MapLS( items );
2096             ret = callback( HWND_16(hwnd), LB_GETSELITEMS16, wParam, lParam, result, arg );
2097             UnMapLS( lParam );
2098             for (i = 0; i < wParam; i++) items32[i] = items[i];
2099             free_buffer( buffer, items );
2100         }
2101         break;
2102     case LB_SETTABSTOPS:
2103         if (wParam)
2104         {
2105             INT *stops32 = (INT *)lParam;
2106             INT16 *stops, buffer[512];
2107             unsigned int i;
2108
2109             wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2110             if (!(stops = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2111             for (i = 0; i < wParam; i++) stops[i] = stops32[i];
2112             lParam = MapLS( stops );
2113             ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2114             UnMapLS( lParam );
2115             free_buffer( buffer, stops );
2116         }
2117         else ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2118         break;
2119     case CB_DELETESTRING:
2120     case CB_GETCOUNT:
2121     case CB_GETLBTEXTLEN:
2122     case CB_LIMITTEXT:
2123     case CB_RESETCONTENT:
2124     case CB_SETEDITSEL:
2125     case CB_GETCURSEL:
2126     case CB_SETCURSEL:
2127     case CB_SHOWDROPDOWN:
2128     case CB_SETITEMDATA:
2129     case CB_SETITEMHEIGHT:
2130     case CB_GETITEMHEIGHT:
2131     case CB_SETEXTENDEDUI:
2132     case CB_GETEXTENDEDUI:
2133     case CB_GETDROPPEDSTATE:
2134         ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2135         break;
2136     case CB_GETEDITSEL:
2137         ret = callback( HWND_16(hwnd), CB_GETEDITSEL16, wParam, lParam, result, arg );
2138         if (wParam) *((PUINT)(wParam)) = LOWORD(*result);
2139         if (lParam) *((PUINT)(lParam)) = HIWORD(*result);  /* FIXME: substract 1? */
2140         break;
2141     case CB_ADDSTRING:
2142     case CB_FINDSTRING:
2143     case CB_FINDSTRINGEXACT:
2144     case CB_INSERTSTRING:
2145     case CB_SELECTSTRING:
2146     case CB_DIR:
2147     case CB_GETLBTEXT:
2148         lParam = MapLS( (LPSTR)lParam );
2149         ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2150         UnMapLS( lParam );
2151         break;
2152     case LB_GETITEMRECT:
2153     case CB_GETDROPPEDCONTROLRECT:
2154         {
2155             RECT *r32 = (RECT *)lParam;
2156             RECT16 rect;
2157             lParam = MapLS( &rect );
2158             ret = callback( HWND_16(hwnd),
2159                             (msg == LB_GETITEMRECT) ? LB_GETITEMRECT16 : CB_GETDROPPEDCONTROLRECT16,
2160                             wParam, lParam, result, arg );
2161             UnMapLS( lParam );
2162             RECT16to32( &rect, r32 );
2163         }
2164         break;
2165     case WM_PAINTCLIPBOARD:
2166     case WM_SIZECLIPBOARD:
2167         FIXME_(msg)( "message %04x needs translation\n", msg );
2168         break;
2169     /* the following messages should not be sent to 16-bit apps */
2170     case WM_SIZING:
2171     case WM_MOVING:
2172     case WM_CAPTURECHANGED:
2173     case WM_STYLECHANGING:
2174     case WM_STYLECHANGED:
2175         break;
2176     default:
2177         ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
2178         break;
2179     }
2180     return ret;
2181 }
2182
2183
2184 /**********************************************************************
2185  *              WINPROC_call_window
2186  *
2187  * Call the window procedure of the specified window.
2188  */
2189 BOOL WINPROC_call_window( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2190                           LRESULT *result, BOOL unicode, enum wm_char_mapping mapping )
2191 {
2192     struct user_thread_info *thread_info = get_user_thread_info();
2193     WND *wndPtr;
2194     WINDOWPROC *proc;
2195
2196     if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
2197     if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
2198     if (wndPtr->tid != GetCurrentThreadId())
2199     {
2200         WIN_ReleasePtr( wndPtr );
2201         return FALSE;
2202     }
2203     proc = handle_to_proc( wndPtr->winproc );
2204     WIN_ReleasePtr( wndPtr );
2205
2206     if (!proc) return TRUE;
2207
2208     if (thread_info->recursion_count > MAX_WINPROC_RECURSION) return FALSE;
2209     thread_info->recursion_count++;
2210
2211     if (unicode)
2212     {
2213         if (proc->procW)
2214             call_window_proc( hwnd, msg, wParam, lParam, result, proc->procW );
2215         else if (proc->procA)
2216             WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procA );
2217         else
2218             WINPROC_CallProcWtoA( call_window_proc_Ato16, hwnd, msg, wParam, lParam, result, proc->proc16 );
2219     }
2220     else
2221     {
2222         if (proc->procA)
2223             call_window_proc( hwnd, msg, wParam, lParam, result, proc->procA );
2224         else if (proc->procW)
2225             WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procW, mapping );
2226         else
2227             WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wParam, lParam, result, proc->proc16 );
2228     }
2229     thread_info->recursion_count--;
2230     return TRUE;
2231 }
2232
2233
2234 /**********************************************************************
2235  *              CallWindowProc (USER.122)
2236  */
2237 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
2238                                  WPARAM16 wParam, LPARAM lParam )
2239 {
2240     WINDOWPROC *proc;
2241     LRESULT result;
2242
2243     if (!func) return 0;
2244
2245     if (!(proc = handle16_to_proc( func )))
2246         call_window_proc16( hwnd, msg, wParam, lParam, &result, func );
2247     else if (proc->procA)
2248         WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2249     else if (proc->procW)
2250         WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
2251     else
2252         call_window_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2253
2254     return result;
2255 }
2256
2257
2258 /**********************************************************************
2259  *              CallWindowProcA (USER32.@)
2260  *
2261  * The CallWindowProc() function invokes the windows procedure _func_,
2262  * with _hwnd_ as the target window, the message specified by _msg_, and
2263  * the message parameters _wParam_ and _lParam_.
2264  *
2265  * Some kinds of argument conversion may be done, I'm not sure what.
2266  *
2267  * CallWindowProc() may be used for windows subclassing. Use
2268  * SetWindowLong() to set a new windows procedure for windows of the
2269  * subclass, and handle subclassed messages in the new windows
2270  * procedure. The new windows procedure may then use CallWindowProc()
2271  * with _func_ set to the parent class's windows procedure to dispatch
2272  * the message to the superclass.
2273  *
2274  * RETURNS
2275  *
2276  *    The return value is message dependent.
2277  *
2278  * CONFORMANCE
2279  *
2280  *   ECMA-234, Win32
2281  */
2282 LRESULT WINAPI CallWindowProcA(
2283     WNDPROC func,  /* [in] window procedure */
2284     HWND hwnd,     /* [in] target window */
2285     UINT msg,      /* [in] message */
2286     WPARAM wParam, /* [in] message dependent parameter */
2287     LPARAM lParam  /* [in] message dependent parameter */
2288 ) {
2289     WINDOWPROC *proc;
2290     LRESULT result;
2291
2292     if (!func) return 0;
2293
2294     if (!(proc = handle_to_proc( func )))
2295         call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2296     else if (proc->procA)
2297         call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2298     else if (proc->procW)
2299         WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result,
2300                               proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
2301     else
2302         WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2303     return result;
2304 }
2305
2306
2307 /**********************************************************************
2308  *              CallWindowProcW (USER32.@)
2309  *
2310  * See CallWindowProcA.
2311  */
2312 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
2313                                   WPARAM wParam, LPARAM lParam )
2314 {
2315     WINDOWPROC *proc;
2316     LRESULT result;
2317
2318     if (!func) return 0;
2319
2320     if (!(proc = handle_to_proc( func )))
2321         call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2322     else if (proc->procW)
2323         call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2324     else if (proc->procA)
2325         WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2326     else
2327         WINPROC_CallProcWtoA( call_window_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2328     return result;
2329 }
2330
2331
2332 /**********************************************************************
2333  *              WINPROC_CallDlgProc16
2334  */
2335 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
2336 {
2337     WINDOWPROC *proc;
2338     LRESULT result;
2339     INT_PTR ret;
2340
2341     if (!func) return 0;
2342
2343     if (!(proc = handle16_to_proc( (WNDPROC16)func )))
2344     {
2345         ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, func );
2346     }
2347     else if (proc->procA)
2348     {
2349         ret = WINPROC_CallProc16To32A( call_dialog_proc, hwnd, msg, wParam, lParam,
2350                                        &result, proc->procA );
2351         SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2352     }
2353     else if (proc->procW)
2354     {
2355         ret = WINPROC_CallProc16To32A( call_dialog_proc_AtoW, hwnd, msg, wParam, lParam,
2356                                        &result, proc->procW );
2357         SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2358     }
2359     else
2360     {
2361         ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2362     }
2363     return ret;
2364 }
2365
2366
2367 /**********************************************************************
2368  *              WINPROC_CallDlgProcA
2369  */
2370 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2371 {
2372     WINDOWPROC *proc;
2373     LRESULT result;
2374     INT_PTR ret;
2375
2376     if (!func) return 0;
2377
2378     if (!(proc = handle_to_proc( func )))
2379         ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2380     else if (proc->procA)
2381         ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2382     else if (proc->procW)
2383     {
2384         ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result,
2385                                     proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
2386         SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2387     }
2388     else
2389     {
2390         ret = WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2391         SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2392     }
2393     return ret;
2394 }
2395
2396
2397 /**********************************************************************
2398  *              WINPROC_CallDlgProcW
2399  */
2400 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2401 {
2402     WINDOWPROC *proc;
2403     LRESULT result;
2404     INT_PTR ret;
2405
2406     if (!func) return 0;
2407
2408     if (!(proc = handle_to_proc( func )))
2409         ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2410     else if (proc->procW)
2411         ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2412     else if (proc->procA)
2413     {
2414         ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2415         SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2416     }
2417     else
2418     {
2419         ret = WINPROC_CallProcWtoA( call_dialog_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2420         SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2421     }
2422     return ret;
2423 }