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