Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / user32 / winproc.c
blobef5cc77907104e50df0ed52c81fa8d2134992108
1 /*
2 * Window procedure callbacks
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1996 Alexandre Julliard
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "wownt32.h"
33 #include "wine/winbase16.h"
34 #include "wine/winuser16.h"
35 #include "controls.h"
36 #include "win.h"
37 #include "winproc.h"
38 #include "user_private.h"
39 #include "dde.h"
40 #include "winternl.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DECLARE_DEBUG_CHANNEL(msg);
45 WINE_DECLARE_DEBUG_CHANNEL(relay);
46 WINE_DEFAULT_DEBUG_CHANNEL(win);
48 typedef struct tagWINDOWPROC
50 WNDPROC16 proc16; /* 16-bit window proc */
51 WNDPROC procA; /* ASCII window proc */
52 WNDPROC procW; /* Unicode window proc */
53 } WINDOWPROC;
55 #define WINPROC_HANDLE (~0UL >> 16)
56 #define MAX_WINPROCS 8192
58 static WINDOWPROC winproc_array[MAX_WINPROCS];
59 static UINT winproc_used;
61 static CRITICAL_SECTION winproc_cs;
62 static CRITICAL_SECTION_DEBUG critsect_debug =
64 0, 0, &winproc_cs,
65 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": winproc_cs") }
68 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
70 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
72 if (size >= need) return static_buffer;
73 return HeapAlloc( GetProcessHeap(), 0, need );
76 static inline void free_buffer( void *static_buffer, void *buffer )
78 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
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 )
85 unsigned int i;
87 for (i = 0; i < winproc_used; i++)
89 if (winproc_array[i].proc16 == func) return &winproc_array[i];
91 return NULL;
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 )
98 unsigned int i;
100 for (i = 0; i < winproc_used; i++)
102 if (funcA && winproc_array[i].procA != funcA) continue;
103 if (funcW && winproc_array[i].procW != funcW) continue;
104 return &winproc_array[i];
106 return NULL;
109 /* return the window proc for a given handle, or NULL for an invalid handle */
110 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
112 UINT index = LOWORD(handle);
113 if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
114 if (index >= winproc_used) return NULL;
115 return &winproc_array[index];
118 /* create a handle for a given window proc */
119 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
121 return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
124 /* allocate and initialize a new winproc */
125 static inline WINDOWPROC *alloc_winproc( WNDPROC funcA, WNDPROC funcW )
127 WINDOWPROC *proc;
129 /* check if the function is already a win proc */
130 if (funcA && (proc = handle_to_proc( funcA ))) return proc;
131 if (funcW && (proc = handle_to_proc( funcW ))) return proc;
132 if (!funcA && !funcW) return NULL;
134 EnterCriticalSection( &winproc_cs );
136 /* check if we already have a winproc for that function */
137 if (!(proc = find_winproc( funcA, funcW )))
139 if (winproc_used < MAX_WINPROCS)
141 proc = &winproc_array[winproc_used++];
142 proc->procA = funcA;
143 proc->procW = funcW;
144 TRACE( "allocated %p for %p/%p (%d/%d used)\n",
145 proc_to_handle(proc), funcA, funcW, winproc_used, MAX_WINPROCS );
147 else FIXME( "too many winprocs, cannot allocate one for %p/%p\n", funcA, funcW );
149 else TRACE( "reusing %p for %p/%p\n", proc_to_handle(proc), funcA, funcW );
151 LeaveCriticalSection( &winproc_cs );
152 return proc;
156 #ifdef __i386__
158 #include "pshpack1.h"
160 /* Window procedure 16-to-32-bit thunk */
161 typedef struct
163 BYTE popl_eax; /* popl %eax (return address) */
164 BYTE pushl_func; /* pushl $proc */
165 WINDOWPROC *proc;
166 BYTE pushl_eax; /* pushl %eax */
167 BYTE ljmp; /* ljmp relay*/
168 DWORD relay_offset; /* __wine_call_wndproc */
169 WORD relay_sel;
170 } WINPROC_THUNK;
172 #include "poppack.h"
174 #define MAX_THUNKS (0x10000 / sizeof(WINPROC_THUNK))
176 static WINPROC_THUNK *thunk_array;
177 static UINT thunk_selector;
178 static UINT thunk_used;
180 /* return the window proc for a given handle, or NULL for an invalid handle */
181 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
183 if (HIWORD(handle) == thunk_selector)
185 UINT index = LOWORD(handle) / sizeof(WINPROC_THUNK);
186 /* check alignment */
187 if (index * sizeof(WINPROC_THUNK) != LOWORD(handle)) return NULL;
188 /* check array limits */
189 if (index >= thunk_used) return NULL;
190 return thunk_array[index].proc;
192 return handle_to_proc( (WNDPROC)handle );
195 /* allocate a 16-bit thunk for an existing window proc */
196 static WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
198 static FARPROC16 relay;
199 UINT i;
201 if (proc->proc16) return proc->proc16;
203 EnterCriticalSection( &winproc_cs );
205 if (!thunk_array) /* allocate the array and its selector */
207 LDT_ENTRY entry;
209 if (!(thunk_selector = wine_ldt_alloc_entries(1))) goto done;
210 if (!(thunk_array = VirtualAlloc( NULL, MAX_THUNKS * sizeof(WINPROC_THUNK), MEM_COMMIT,
211 PAGE_EXECUTE_READWRITE ))) goto done;
212 wine_ldt_set_base( &entry, thunk_array );
213 wine_ldt_set_limit( &entry, MAX_THUNKS * sizeof(WINPROC_THUNK) - 1 );
214 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
215 wine_ldt_set_entry( thunk_selector, &entry );
216 relay = GetProcAddress16( GetModuleHandle16("user"), "__wine_call_wndproc" );
219 /* check if it already exists */
220 for (i = 0; i < thunk_used; i++) if (thunk_array[i].proc == proc) break;
222 if (i == thunk_used) /* create a new one */
224 WINPROC_THUNK *thunk;
226 if (thunk_used >= MAX_THUNKS) goto done;
227 thunk = &thunk_array[thunk_used++];
228 thunk->popl_eax = 0x58; /* popl %eax */
229 thunk->pushl_func = 0x68; /* pushl $proc */
230 thunk->proc = proc;
231 thunk->pushl_eax = 0x50; /* pushl %eax */
232 thunk->ljmp = 0xea; /* ljmp relay*/
233 thunk->relay_offset = OFFSETOF(relay);
234 thunk->relay_sel = SELECTOROF(relay);
236 proc->proc16 = (WNDPROC16)MAKESEGPTR( thunk_selector, i * sizeof(WINPROC_THUNK) );
237 done:
238 LeaveCriticalSection( &winproc_cs );
239 return proc->proc16;
242 #else /* __i386__ */
244 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
246 return handle_to_proc( (WNDPROC)handle );
249 static inline WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
251 return 0;
254 #endif /* __i386__ */
257 #ifdef __i386__
258 /* Some window procedures modify register they shouldn't, or are not
259 * properly declared stdcall; so we need a small assembly wrapper to
260 * call them. */
261 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
262 WPARAM wParam, LPARAM lParam );
263 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
264 "pushl %ebp\n\t"
265 "movl %esp,%ebp\n\t"
266 "pushl %edi\n\t"
267 "pushl %esi\n\t"
268 "pushl %ebx\n\t"
269 "subl $12,%esp\n\t"
270 "pushl 24(%ebp)\n\t"
271 "pushl 20(%ebp)\n\t"
272 "pushl 16(%ebp)\n\t"
273 "pushl 12(%ebp)\n\t"
274 "movl 8(%ebp),%eax\n\t"
275 "call *%eax\n\t"
276 "leal -12(%ebp),%esp\n\t"
277 "popl %ebx\n\t"
278 "popl %esi\n\t"
279 "popl %edi\n\t"
280 "leave\n\t"
281 "ret" )
282 #else
283 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
284 WPARAM wParam, LPARAM lParam )
286 return proc( hwnd, msg, wParam, lParam );
288 #endif /* __i386__ */
290 static void RECT16to32( const RECT16 *from, RECT *to )
292 to->left = from->left;
293 to->top = from->top;
294 to->right = from->right;
295 to->bottom = from->bottom;
298 static void RECT32to16( const RECT *from, RECT16 *to )
300 to->left = from->left;
301 to->top = from->top;
302 to->right = from->right;
303 to->bottom = from->bottom;
306 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
308 to->ptReserved.x = from->ptReserved.x;
309 to->ptReserved.y = from->ptReserved.y;
310 to->ptMaxSize.x = from->ptMaxSize.x;
311 to->ptMaxSize.y = from->ptMaxSize.y;
312 to->ptMaxPosition.x = from->ptMaxPosition.x;
313 to->ptMaxPosition.y = from->ptMaxPosition.y;
314 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
315 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
316 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
317 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
320 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
322 to->ptReserved.x = from->ptReserved.x;
323 to->ptReserved.y = from->ptReserved.y;
324 to->ptMaxSize.x = from->ptMaxSize.x;
325 to->ptMaxSize.y = from->ptMaxSize.y;
326 to->ptMaxPosition.x = from->ptMaxPosition.x;
327 to->ptMaxPosition.y = from->ptMaxPosition.y;
328 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
329 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
330 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
331 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
334 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
336 to->hwnd = HWND_16(from->hwnd);
337 to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
338 to->x = from->x;
339 to->y = from->y;
340 to->cx = from->cx;
341 to->cy = from->cy;
342 to->flags = from->flags;
345 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
347 to->hwnd = WIN_Handle32(from->hwnd);
348 to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
349 HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
350 to->x = from->x;
351 to->y = from->y;
352 to->cx = from->cx;
353 to->cy = from->cy;
354 to->flags = from->flags;
357 /* The strings are not copied */
358 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
360 to->lpCreateParams = (SEGPTR)from->lpCreateParams;
361 to->hInstance = HINSTANCE_16(from->hInstance);
362 to->hMenu = HMENU_16(from->hMenu);
363 to->hwndParent = HWND_16(from->hwndParent);
364 to->cy = from->cy;
365 to->cx = from->cx;
366 to->y = from->y;
367 to->x = from->x;
368 to->style = from->style;
369 to->dwExStyle = from->dwExStyle;
372 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
375 to->lpCreateParams = (LPVOID)from->lpCreateParams;
376 to->hInstance = HINSTANCE_32(from->hInstance);
377 to->hMenu = HMENU_32(from->hMenu);
378 to->hwndParent = WIN_Handle32(from->hwndParent);
379 to->cy = from->cy;
380 to->cx = from->cx;
381 to->y = from->y;
382 to->x = from->x;
383 to->style = from->style;
384 to->dwExStyle = from->dwExStyle;
385 to->lpszName = MapSL(from->lpszName);
386 to->lpszClass = MapSL(from->lpszClass);
389 /* The strings are not copied */
390 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
392 to->hOwner = HINSTANCE_16(from->hOwner);
393 to->x = from->x;
394 to->y = from->y;
395 to->cx = from->cx;
396 to->cy = from->cy;
397 to->style = from->style;
398 to->lParam = from->lParam;
401 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
403 to->hOwner = HINSTANCE_32(from->hOwner);
404 to->x = from->x;
405 to->y = from->y;
406 to->cx = from->cx;
407 to->cy = from->cy;
408 to->style = from->style;
409 to->lParam = from->lParam;
410 to->szTitle = MapSL(from->szTitle);
411 to->szClass = MapSL(from->szClass);
414 static WPARAM map_wparam_char_AtoW( WPARAM wParam, DWORD len )
416 CHAR ch[2];
417 WCHAR wch;
419 ch[0] = (wParam >> 8);
420 ch[1] = wParam & 0xff;
421 if (len > 1 && ch[0])
422 RtlMultiByteToUnicodeN( &wch, sizeof(wch), NULL, ch, 2 );
423 else
424 RtlMultiByteToUnicodeN( &wch, sizeof(wch), NULL, ch + 1, 1 );
425 return MAKEWPARAM( wch, HIWORD(wParam) );
428 static WPARAM map_wparam_char_WtoA( WPARAM wParam, DWORD len )
430 WCHAR wch = wParam;
431 BYTE ch[2];
433 RtlUnicodeToMultiByteN( (LPSTR)ch, len, &len, &wch, sizeof(wch) );
434 if (len == 2)
435 return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
436 else
437 return MAKEWPARAM( ch[0], HIWORD(wParam) );
440 /* call a 32-bit window procedure */
441 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
443 WNDPROC proc = arg;
445 USER_CheckNotLock();
447 hwnd = WIN_GetFullHandle( hwnd );
448 if (TRACE_ON(relay))
449 DPRINTF( "%04x:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
450 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
452 *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
454 if (TRACE_ON(relay))
455 DPRINTF( "%04x:Ret window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
456 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
457 return *result;
460 /* call a 32-bit dialog procedure */
461 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
463 WNDPROC proc = arg;
464 LRESULT ret;
466 USER_CheckNotLock();
468 hwnd = WIN_GetFullHandle( hwnd );
469 if (TRACE_ON(relay))
470 DPRINTF( "%04x:Call dialog proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
471 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
473 ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
474 *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
476 if (TRACE_ON(relay))
477 DPRINTF( "%04x:Ret dialog proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx result=%08lx\n",
478 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
479 return ret;
482 /* call a 16-bit window procedure */
483 static LRESULT call_window_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
484 LRESULT *result, void *arg )
486 WNDPROC16 proc = arg;
487 CONTEXT86 context;
488 size_t size = 0;
489 struct
491 WORD params[5];
492 union
494 CREATESTRUCT16 cs16;
495 DRAWITEMSTRUCT16 dis16;
496 COMPAREITEMSTRUCT16 cis16;
497 } u;
498 } args;
500 USER_CheckNotLock();
502 /* Window procedures want ax = hInstance, ds = es = ss */
504 memset(&context, 0, sizeof(context));
505 context.SegDs = context.SegEs = SELECTOROF(NtCurrentTeb()->WOW32Reserved);
506 context.SegFs = wine_get_fs();
507 context.SegGs = wine_get_gs();
508 if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
509 context.SegCs = SELECTOROF(proc);
510 context.Eip = OFFSETOF(proc);
511 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
513 if (lParam)
515 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
516 work if structures passed in lParam are placed in the stack/data
517 segment. Programmers easily make the mistake of converting lParam
518 to a near rather than a far pointer, since Windows apparently
519 allows this. We copy the structures to the 16 bit stack; this is
520 ugly but makes these programs work. */
521 switch (msg)
523 case WM_CREATE:
524 case WM_NCCREATE:
525 size = sizeof(CREATESTRUCT16); break;
526 case WM_DRAWITEM:
527 size = sizeof(DRAWITEMSTRUCT16); break;
528 case WM_COMPAREITEM:
529 size = sizeof(COMPAREITEMSTRUCT16); break;
531 if (size)
533 memcpy( &args.u, MapSL(lParam), size );
534 lParam = (SEGPTR)NtCurrentTeb()->WOW32Reserved - size;
538 args.params[4] = hwnd;
539 args.params[3] = msg;
540 args.params[2] = wParam;
541 args.params[1] = HIWORD(lParam);
542 args.params[0] = LOWORD(lParam);
543 WOWCallback16Ex( 0, WCB16_REGS, sizeof(args.params) + size, &args, (DWORD *)&context );
544 *result = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
545 return *result;
548 /* call a 16-bit dialog procedure */
549 static LRESULT call_dialog_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wp, LPARAM lp,
550 LRESULT *result, void *arg )
552 LRESULT ret = call_window_proc16( hwnd, msg, wp, lp, result, arg );
553 *result = GetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT );
554 return LOWORD(ret);
557 /* helper callback for 32W->16 conversion */
558 static LRESULT call_window_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
559 LRESULT *result, void *arg )
561 return WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wp, lp, result, arg );
564 /* helper callback for 32W->16 conversion */
565 static LRESULT call_dialog_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
566 LRESULT *result, void *arg )
568 return WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wp, lp, result, arg );
571 /* helper callback for 16->32W conversion */
572 static LRESULT call_window_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
573 LRESULT *result, void *arg )
575 return WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wp, lp, result, arg );
578 /* helper callback for 16->32W conversion */
579 static LRESULT call_dialog_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
580 LRESULT *result, void *arg )
582 return WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wp, lp, result, arg );
586 /**********************************************************************
587 * WINPROC_GetProc16
589 * Get a window procedure pointer that can be passed to the Windows program.
591 WNDPROC16 WINPROC_GetProc16( WNDPROC proc, BOOL unicode )
593 WINDOWPROC *ptr;
595 if (unicode) ptr = alloc_winproc( NULL, proc );
596 else ptr = alloc_winproc( proc, NULL );
598 if (!ptr) return 0;
599 return alloc_win16_thunk( ptr );
603 /**********************************************************************
604 * WINPROC_GetProc
606 * Get a window procedure pointer that can be passed to the Windows program.
608 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
610 WINDOWPROC *ptr = handle_to_proc( proc );
612 if (!ptr) return proc;
613 if (unicode)
615 if (ptr->procW) return ptr->procW;
616 return proc;
618 else
620 if (ptr->procA) return ptr->procA;
621 return proc;
626 /**********************************************************************
627 * WINPROC_AllocProc16
629 * Allocate a window procedure for a window or class.
631 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
632 * lot of windows, it will usually only have a limited number of window procedures, so the
633 * array won't grow too large, and this way we avoid the need to track allocations per window.
635 WNDPROC WINPROC_AllocProc16( WNDPROC16 func )
637 WINDOWPROC *proc;
639 if (!func) return NULL;
641 /* check if the function is already a win proc */
642 if (!(proc = handle16_to_proc( func )))
644 EnterCriticalSection( &winproc_cs );
646 /* then check if we already have a winproc for that function */
647 if (!(proc = find_winproc16( func )))
649 if (winproc_used < MAX_WINPROCS)
651 proc = &winproc_array[winproc_used++];
652 proc->proc16 = func;
653 TRACE( "allocated %p for %p/16-bit (%d/%d used)\n",
654 proc_to_handle(proc), func, winproc_used, MAX_WINPROCS );
656 else FIXME( "too many winprocs, cannot allocate one for 16-bit %p\n", func );
658 else TRACE( "reusing %p for %p/16-bit\n", proc_to_handle(proc), func );
660 LeaveCriticalSection( &winproc_cs );
662 return proc_to_handle( proc );
666 /**********************************************************************
667 * WINPROC_AllocProc
669 * Allocate a window procedure for a window or class.
671 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
672 * lot of windows, it will usually only have a limited number of window procedures, so the
673 * array won't grow too large, and this way we avoid the need to track allocations per window.
675 WNDPROC WINPROC_AllocProc( WNDPROC funcA, WNDPROC funcW )
677 WINDOWPROC *proc;
679 if (!(proc = alloc_winproc( funcA, funcW ))) return NULL;
680 return proc_to_handle( proc );
684 /**********************************************************************
685 * WINPROC_IsUnicode
687 * Return the window procedure type, or the default value if not a winproc handle.
689 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
691 WINDOWPROC *ptr = handle_to_proc( proc );
693 if (!ptr) return def_val;
694 if (ptr->procA && ptr->procW) return def_val; /* can be both */
695 return (ptr->procW != NULL);
699 /**********************************************************************
700 * WINPROC_TestLBForStr
702 * Return TRUE if the lparam is a string
704 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
706 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
707 if (msg <= CB_MSGMAX)
708 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
709 else
710 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
715 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
717 HANDLE dst;
718 UINT sz = GlobalSize16(src);
719 LPSTR ptr16, ptr32;
721 if (!(dst = GlobalAlloc(flags, sz)))
722 return 0;
723 ptr16 = GlobalLock16(src);
724 ptr32 = GlobalLock(dst);
725 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
726 GlobalUnlock16(src);
727 GlobalUnlock(dst);
729 return (UINT)dst;
732 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
734 HANDLE16 dst;
735 UINT sz = GlobalSize((HANDLE)src);
736 LPSTR ptr16, ptr32;
738 if (!(dst = GlobalAlloc16(flags, sz)))
739 return 0;
740 ptr32 = GlobalLock((HANDLE)src);
741 ptr16 = GlobalLock16(dst);
742 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
743 GlobalUnlock((HANDLE)src);
744 GlobalUnlock16(dst);
746 return dst;
750 /**********************************************************************
751 * WINPROC_CallProcAtoW
753 * Call a window procedure, translating args from Ansi to Unicode.
755 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
756 LPARAM lParam, LRESULT *result, void *arg )
758 LRESULT ret = 0;
760 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
761 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
763 switch(msg)
765 case WM_NCCREATE:
766 case WM_CREATE:
768 WCHAR *ptr, buffer[512];
769 CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
770 CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
771 MDICREATESTRUCTW mdi_cs;
772 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
774 if (HIWORD(csA->lpszClass))
776 class_lenA = strlen(csA->lpszClass) + 1;
777 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
779 if (HIWORD(csA->lpszName))
781 name_lenA = strlen(csA->lpszName) + 1;
782 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
785 if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
787 if (class_lenW)
789 csW.lpszClass = ptr;
790 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
792 if (name_lenW)
794 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
795 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
796 csA->lpszName, name_lenA );
799 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
801 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
802 mdi_cs.szTitle = csW.lpszName;
803 mdi_cs.szClass = csW.lpszClass;
804 csW.lpCreateParams = &mdi_cs;
807 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
808 free_buffer( buffer, ptr );
810 break;
812 case WM_MDICREATE:
814 WCHAR *ptr, buffer[512];
815 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
816 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
817 MDICREATESTRUCTW csW;
819 memcpy( &csW, csA, sizeof(csW) );
821 if (HIWORD(csA->szTitle))
823 title_lenA = strlen(csA->szTitle) + 1;
824 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
826 if (HIWORD(csA->szClass))
828 class_lenA = strlen(csA->szClass) + 1;
829 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
832 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
834 if (title_lenW)
836 csW.szTitle = ptr;
837 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
839 if (class_lenW)
841 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
842 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
843 csA->szClass, class_lenA );
845 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
846 free_buffer( buffer, ptr );
848 break;
850 case WM_GETTEXT:
851 case WM_ASKCBFORMATNAME:
853 WCHAR *ptr, buffer[512];
854 LPSTR str = (LPSTR)lParam;
855 DWORD len = wParam * sizeof(WCHAR);
857 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
858 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
859 if (wParam)
861 len = 0;
862 if (*result)
863 RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, strlenW(ptr) * sizeof(WCHAR) );
864 str[len] = 0;
865 *result = len;
867 free_buffer( buffer, ptr );
869 break;
871 case LB_ADDSTRING:
872 case LB_INSERTSTRING:
873 case LB_FINDSTRING:
874 case LB_FINDSTRINGEXACT:
875 case LB_SELECTSTRING:
876 case CB_ADDSTRING:
877 case CB_INSERTSTRING:
878 case CB_FINDSTRING:
879 case CB_FINDSTRINGEXACT:
880 case CB_SELECTSTRING:
881 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
883 ret = callback( hwnd, msg, wParam, lParam, result, arg );
884 break;
886 /* fall through */
887 case WM_SETTEXT:
888 case WM_WININICHANGE:
889 case WM_DEVMODECHANGE:
890 case CB_DIR:
891 case LB_DIR:
892 case LB_ADDFILE:
893 case EM_REPLACESEL:
894 if (!HIWORD(lParam)) ret = callback( hwnd, msg, wParam, lParam, result, arg );
895 else
897 WCHAR *ptr, buffer[512];
898 LPCSTR strA = (LPCSTR)lParam;
899 DWORD lenW, lenA = strlen(strA) + 1;
901 RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
902 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
904 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
905 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
906 free_buffer( buffer, ptr );
909 break;
911 case LB_GETTEXT:
912 case CB_GETLBTEXT:
913 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
915 WCHAR buffer[512]; /* FIXME: fixed sized buffer */
917 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
918 if (*result >= 0)
920 DWORD len;
921 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
922 buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
923 *result = len - 1;
926 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
927 break;
929 case EM_GETLINE:
931 WCHAR *ptr, buffer[512];
932 WORD len = *(WORD *)lParam;
934 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
935 *((WORD *)ptr) = len; /* store the length */
936 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
937 if (*result)
939 DWORD reslen;
940 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
941 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
942 *result = reslen;
944 free_buffer( buffer, ptr );
946 break;
948 case WM_GETDLGCODE:
949 if (lParam)
951 MSG newmsg = *(MSG *)lParam;
952 switch(newmsg.message)
954 case WM_CHAR:
955 case WM_DEADCHAR:
956 case WM_SYSCHAR:
957 case WM_SYSDEADCHAR:
958 newmsg.wParam = map_wparam_char_AtoW( newmsg.wParam, 1 );
959 break;
960 case WM_IME_CHAR:
961 newmsg.wParam = map_wparam_char_AtoW( newmsg.wParam, 2 );
962 break;
964 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
966 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
967 break;
969 case WM_CHARTOITEM:
970 case WM_MENUCHAR:
971 case WM_CHAR:
972 case WM_DEADCHAR:
973 case WM_SYSCHAR:
974 case WM_SYSDEADCHAR:
975 case EM_SETPASSWORDCHAR:
976 ret = callback( hwnd, msg, map_wparam_char_AtoW(wParam,1), lParam, result, arg );
977 break;
979 case WM_IME_CHAR:
980 ret = callback( hwnd, msg, map_wparam_char_AtoW(wParam,2), lParam, result, arg );
981 break;
983 case WM_GETTEXTLENGTH:
984 case CB_GETLBTEXTLEN:
985 case LB_GETTEXTLEN:
986 ret = callback( hwnd, msg, wParam, lParam, result, arg );
987 if (*result >= 0)
989 WCHAR *ptr, buffer[512];
990 LRESULT tmp;
991 DWORD len = *result + 1;
992 /* Determine respective GETTEXT message */
993 UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
994 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
995 /* wParam differs between the messages */
996 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
998 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
1000 if (callback == call_window_proc) /* FIXME: hack */
1001 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
1002 else
1003 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
1004 RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
1005 *result = len;
1006 free_buffer( buffer, ptr );
1008 break;
1010 case WM_PAINTCLIPBOARD:
1011 case WM_SIZECLIPBOARD:
1012 FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
1013 SPY_GetMsgName(msg, hwnd), msg );
1014 break;
1016 default:
1017 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1018 break;
1020 return ret;
1024 /**********************************************************************
1025 * WINPROC_CallProcWtoA
1027 * Call a window procedure, translating args from Unicode to Ansi.
1029 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
1030 LPARAM lParam, LRESULT *result, void *arg )
1032 LRESULT ret = 0;
1034 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
1035 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1037 switch(msg)
1039 case WM_NCCREATE:
1040 case WM_CREATE:
1041 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
1042 * at this point.
1044 char buffer[1024], *cls, *name;
1045 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
1046 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
1047 MDICREATESTRUCTA mdi_cs;
1048 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
1050 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
1051 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
1053 if (csW->lpszName)
1055 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
1056 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
1058 else
1059 name_lenW = name_lenA = 0;
1061 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA + 2 ))) break;
1063 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
1064 cls[class_lenA] = 0;
1065 csA.lpszClass = cls;
1067 if (csW->lpszName)
1069 name = cls + class_lenA + 1;
1070 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
1071 name[name_lenA] = 0;
1072 csA.lpszName = name;
1075 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1077 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
1078 mdi_cs.szTitle = csA.lpszName;
1079 mdi_cs.szClass = csA.lpszClass;
1080 csA.lpCreateParams = &mdi_cs;
1083 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1084 free_buffer( buffer, cls );
1086 break;
1088 case WM_GETTEXT:
1089 case WM_ASKCBFORMATNAME:
1091 char *ptr, buffer[512];
1092 DWORD len = wParam * 2;
1094 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
1095 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1096 if (len)
1098 if (*result)
1100 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
1101 *result = len/sizeof(WCHAR) - 1; /* do not count terminating null */
1103 ((LPWSTR)lParam)[*result] = 0;
1105 free_buffer( buffer, ptr );
1107 break;
1109 case LB_ADDSTRING:
1110 case LB_INSERTSTRING:
1111 case LB_FINDSTRING:
1112 case LB_FINDSTRINGEXACT:
1113 case LB_SELECTSTRING:
1114 case CB_ADDSTRING:
1115 case CB_INSERTSTRING:
1116 case CB_FINDSTRING:
1117 case CB_FINDSTRINGEXACT:
1118 case CB_SELECTSTRING:
1119 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
1121 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1122 break;
1124 /* fall through */
1125 case WM_SETTEXT:
1126 case WM_WININICHANGE:
1127 case WM_DEVMODECHANGE:
1128 case CB_DIR:
1129 case LB_DIR:
1130 case LB_ADDFILE:
1131 case EM_REPLACESEL:
1132 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
1133 else
1135 char *ptr, buffer[512];
1136 LPCWSTR strW = (LPCWSTR)lParam;
1137 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
1139 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
1140 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
1142 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
1143 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1144 free_buffer( buffer, ptr );
1147 break;
1149 case WM_MDICREATE:
1151 char *ptr, buffer[1024];
1152 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
1153 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1154 MDICREATESTRUCTA csA;
1156 memcpy( &csA, csW, sizeof(csA) );
1158 if (HIWORD(csW->szTitle))
1160 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
1161 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
1163 if (HIWORD(csW->szClass))
1165 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
1166 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
1169 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
1171 if (title_lenA)
1173 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
1174 csA.szTitle = ptr;
1176 if (class_lenA)
1178 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
1179 csA.szClass = ptr + title_lenA;
1181 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1182 free_buffer( buffer, ptr );
1184 break;
1186 case LB_GETTEXT:
1187 case CB_GETLBTEXT:
1188 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
1190 char buffer[512]; /* FIXME: fixed sized buffer */
1192 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
1193 if (*result >= 0)
1195 DWORD len;
1196 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
1197 *result = len / sizeof(WCHAR) - 1;
1200 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1201 break;
1203 case EM_GETLINE:
1205 char *ptr, buffer[512];
1206 WORD len = *(WORD *)lParam;
1208 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
1209 *((WORD *)ptr) = len * 2; /* store the length */
1210 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1211 if (*result)
1213 DWORD reslen;
1214 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
1215 *result = reslen / sizeof(WCHAR);
1216 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
1218 free_buffer( buffer, ptr );
1220 break;
1222 case WM_GETDLGCODE:
1223 if (lParam)
1225 MSG newmsg = *(MSG *)lParam;
1226 switch(newmsg.message)
1228 case WM_CHAR:
1229 case WM_DEADCHAR:
1230 case WM_SYSCHAR:
1231 case WM_SYSDEADCHAR:
1232 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
1233 break;
1234 case WM_IME_CHAR:
1235 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
1236 break;
1238 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
1240 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1241 break;
1243 case WM_CHARTOITEM:
1244 case WM_MENUCHAR:
1245 case WM_CHAR:
1246 case WM_DEADCHAR:
1247 case WM_SYSCHAR:
1248 case WM_SYSDEADCHAR:
1249 case EM_SETPASSWORDCHAR:
1250 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
1251 break;
1253 case WM_IME_CHAR:
1254 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
1255 break;
1257 case WM_PAINTCLIPBOARD:
1258 case WM_SIZECLIPBOARD:
1259 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
1260 SPY_GetMsgName(msg, hwnd), msg );
1261 break;
1263 default:
1264 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1265 break;
1268 return ret;
1272 /**********************************************************************
1273 * WINPROC_CallProc16To32A
1275 LRESULT WINPROC_CallProc16To32A( winproc_callback_t callback, HWND16 hwnd, UINT16 msg,
1276 WPARAM16 wParam, LPARAM lParam, LRESULT *result, void *arg )
1278 LRESULT ret = 0;
1279 HWND hwnd32 = WIN_Handle32( hwnd );
1281 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
1282 hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
1284 switch(msg)
1286 case WM_NCCREATE:
1287 case WM_CREATE:
1289 CREATESTRUCT16 *cs16 = MapSL(lParam);
1290 CREATESTRUCTA cs;
1291 MDICREATESTRUCTA mdi_cs;
1293 CREATESTRUCT16to32A( cs16, &cs );
1294 if (GetWindowLongW(hwnd32, GWL_EXSTYLE) & WS_EX_MDICHILD)
1296 MDICREATESTRUCT16 *mdi_cs16 = MapSL(cs16->lpCreateParams);
1297 MDICREATESTRUCT16to32A(mdi_cs16, &mdi_cs);
1298 cs.lpCreateParams = &mdi_cs;
1300 ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1301 CREATESTRUCT32Ato16( &cs, cs16 );
1303 break;
1304 case WM_MDICREATE:
1306 MDICREATESTRUCT16 *cs16 = MapSL(lParam);
1307 MDICREATESTRUCTA cs;
1309 MDICREATESTRUCT16to32A( cs16, &cs );
1310 ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1311 MDICREATESTRUCT32Ato16( &cs, cs16 );
1313 break;
1314 case WM_MDIACTIVATE:
1315 if (lParam)
1316 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32( HIWORD(lParam) ),
1317 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1318 else /* message sent to MDI client */
1319 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1320 break;
1321 case WM_MDIGETACTIVE:
1323 BOOL maximized = FALSE;
1324 ret = callback( hwnd32, msg, wParam, (LPARAM)&maximized, result, arg );
1325 *result = MAKELRESULT( LOWORD(*result), maximized );
1327 break;
1328 case WM_MDISETMENU:
1329 ret = callback( hwnd32, wParam ? WM_MDIREFRESHMENU : WM_MDISETMENU,
1330 (WPARAM)HMENU_32(LOWORD(lParam)), (LPARAM)HMENU_32(HIWORD(lParam)),
1331 result, arg );
1332 break;
1333 case WM_GETMINMAXINFO:
1335 MINMAXINFO16 *mmi16 = MapSL(lParam);
1336 MINMAXINFO mmi;
1338 MINMAXINFO16to32( mmi16, &mmi );
1339 ret = callback( hwnd32, msg, wParam, (LPARAM)&mmi, result, arg );
1340 MINMAXINFO32to16( &mmi, mmi16 );
1342 break;
1343 case WM_WINDOWPOSCHANGING:
1344 case WM_WINDOWPOSCHANGED:
1346 WINDOWPOS16 *winpos16 = MapSL(lParam);
1347 WINDOWPOS winpos;
1349 WINDOWPOS16to32( winpos16, &winpos );
1350 ret = callback( hwnd32, msg, wParam, (LPARAM)&winpos, result, arg );
1351 WINDOWPOS32to16( &winpos, winpos16 );
1353 break;
1354 case WM_NCCALCSIZE:
1356 NCCALCSIZE_PARAMS16 *nc16 = MapSL(lParam);
1357 NCCALCSIZE_PARAMS nc;
1358 WINDOWPOS winpos;
1360 RECT16to32( &nc16->rgrc[0], &nc.rgrc[0] );
1361 if (wParam)
1363 RECT16to32( &nc16->rgrc[1], &nc.rgrc[1] );
1364 RECT16to32( &nc16->rgrc[2], &nc.rgrc[2] );
1365 WINDOWPOS16to32( MapSL(nc16->lppos), &winpos );
1366 nc.lppos = &winpos;
1368 ret = callback( hwnd32, msg, wParam, (LPARAM)&nc, result, arg );
1369 RECT32to16( &nc.rgrc[0], &nc16->rgrc[0] );
1370 if (wParam)
1372 RECT32to16( &nc.rgrc[1], &nc16->rgrc[1] );
1373 RECT32to16( &nc.rgrc[2], &nc16->rgrc[2] );
1374 WINDOWPOS32to16( &winpos, MapSL(nc16->lppos) );
1377 break;
1378 case WM_COMPAREITEM:
1380 COMPAREITEMSTRUCT16* cis16 = MapSL(lParam);
1381 COMPAREITEMSTRUCT cis;
1382 cis.CtlType = cis16->CtlType;
1383 cis.CtlID = cis16->CtlID;
1384 cis.hwndItem = WIN_Handle32( cis16->hwndItem );
1385 cis.itemID1 = cis16->itemID1;
1386 cis.itemData1 = cis16->itemData1;
1387 cis.itemID2 = cis16->itemID2;
1388 cis.itemData2 = cis16->itemData2;
1389 cis.dwLocaleId = 0; /* FIXME */
1390 ret = callback( hwnd32, msg, wParam, (LPARAM)&cis, result, arg );
1392 break;
1393 case WM_DELETEITEM:
1395 DELETEITEMSTRUCT16* dis16 = MapSL(lParam);
1396 DELETEITEMSTRUCT dis;
1397 dis.CtlType = dis16->CtlType;
1398 dis.CtlID = dis16->CtlID;
1399 dis.hwndItem = WIN_Handle32( dis16->hwndItem );
1400 dis.itemData = dis16->itemData;
1401 ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1403 break;
1404 case WM_MEASUREITEM:
1406 MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
1407 MEASUREITEMSTRUCT mis;
1408 mis.CtlType = mis16->CtlType;
1409 mis.CtlID = mis16->CtlID;
1410 mis.itemID = mis16->itemID;
1411 mis.itemWidth = mis16->itemWidth;
1412 mis.itemHeight = mis16->itemHeight;
1413 mis.itemData = mis16->itemData;
1414 ret = callback( hwnd32, msg, wParam, (LPARAM)&mis, result, arg );
1415 mis16->itemWidth = (UINT16)mis.itemWidth;
1416 mis16->itemHeight = (UINT16)mis.itemHeight;
1418 break;
1419 case WM_DRAWITEM:
1421 DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
1422 DRAWITEMSTRUCT dis;
1423 dis.CtlType = dis16->CtlType;
1424 dis.CtlID = dis16->CtlID;
1425 dis.itemID = dis16->itemID;
1426 dis.itemAction = dis16->itemAction;
1427 dis.itemState = dis16->itemState;
1428 dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1429 : WIN_Handle32( dis16->hwndItem );
1430 dis.hDC = HDC_32(dis16->hDC);
1431 dis.itemData = dis16->itemData;
1432 dis.rcItem.left = dis16->rcItem.left;
1433 dis.rcItem.top = dis16->rcItem.top;
1434 dis.rcItem.right = dis16->rcItem.right;
1435 dis.rcItem.bottom = dis16->rcItem.bottom;
1436 ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1438 break;
1439 case WM_COPYDATA:
1441 COPYDATASTRUCT16 *cds16 = MapSL(lParam);
1442 COPYDATASTRUCT cds;
1443 cds.dwData = cds16->dwData;
1444 cds.cbData = cds16->cbData;
1445 cds.lpData = MapSL(cds16->lpData);
1446 ret = callback( hwnd32, msg, wParam, (LPARAM)&cds, result, arg );
1448 break;
1449 case WM_GETDLGCODE:
1450 if (lParam)
1452 MSG16 *msg16 = MapSL(lParam);
1453 MSG msg32;
1454 msg32.hwnd = WIN_Handle32( msg16->hwnd );
1455 msg32.message = msg16->message;
1456 msg32.wParam = msg16->wParam;
1457 msg32.lParam = msg16->lParam;
1458 msg32.time = msg16->time;
1459 msg32.pt.x = msg16->pt.x;
1460 msg32.pt.y = msg16->pt.y;
1461 ret = callback( hwnd32, msg, wParam, (LPARAM)&msg32, result, arg );
1463 else
1464 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1465 break;
1466 case WM_NEXTMENU:
1468 MDINEXTMENU next;
1469 next.hmenuIn = (HMENU)lParam;
1470 next.hmenuNext = 0;
1471 next.hwndNext = 0;
1472 ret = callback( hwnd32, msg, wParam, (LPARAM)&next, result, arg );
1473 *result = MAKELONG( HMENU_16(next.hmenuNext), HWND_16(next.hwndNext) );
1475 break;
1476 case WM_ACTIVATE:
1477 case WM_CHARTOITEM:
1478 case WM_COMMAND:
1479 case WM_VKEYTOITEM:
1480 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1481 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1482 break;
1483 case WM_HSCROLL:
1484 case WM_VSCROLL:
1485 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1486 (LPARAM)WIN_Handle32( HIWORD(lParam) ), result, arg );
1487 break;
1488 case WM_CTLCOLOR:
1489 if (HIWORD(lParam) <= CTLCOLOR_STATIC)
1490 ret = callback( hwnd32, WM_CTLCOLORMSGBOX + HIWORD(lParam),
1491 (WPARAM)HDC_32(wParam), (LPARAM)WIN_Handle32( LOWORD(lParam) ),
1492 result, arg );
1493 break;
1494 case WM_GETTEXT:
1495 case WM_SETTEXT:
1496 case WM_WININICHANGE:
1497 case WM_DEVMODECHANGE:
1498 case WM_ASKCBFORMATNAME:
1499 case WM_NOTIFY:
1500 ret = callback( hwnd32, msg, wParam, (LPARAM)MapSL(lParam), result, arg );
1501 break;
1502 case WM_MENUCHAR:
1503 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1504 (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1505 break;
1506 case WM_MENUSELECT:
1507 if((LOWORD(lParam) & MF_POPUP) && (LOWORD(lParam) != 0xFFFF))
1509 HMENU hmenu = HMENU_32(HIWORD(lParam));
1510 UINT pos = MENU_FindSubMenu( &hmenu, HMENU_32(wParam) );
1511 if (pos == 0xffff) pos = 0; /* NO_SELECTED_ITEM */
1512 wParam = pos;
1514 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1515 (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1516 break;
1517 case WM_PARENTNOTIFY:
1518 if ((wParam == WM_CREATE) || (wParam == WM_DESTROY))
1519 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1520 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1521 else
1522 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1523 break;
1524 case WM_ACTIVATEAPP:
1525 /* We need this when SetActiveWindow sends a Sendmessage16() to
1526 * a 32bit window. Might be superflous with 32bit interprocess
1527 * message queues. */
1528 if (lParam) lParam = HTASK_32(lParam);
1529 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1530 break;
1531 case WM_DDE_INITIATE:
1532 case WM_DDE_TERMINATE:
1533 case WM_DDE_UNADVISE:
1534 case WM_DDE_REQUEST:
1535 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1536 break;
1537 case WM_DDE_ADVISE:
1538 case WM_DDE_DATA:
1539 case WM_DDE_POKE:
1541 HANDLE16 lo16 = LOWORD(lParam);
1542 UINT lo32 = 0;
1543 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE))) break;
1544 lParam = PackDDElParam( msg, lo32, HIWORD(lParam) );
1545 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1547 break; /* FIXME don't know how to free allocated memory (handle) !! */
1548 case WM_DDE_ACK:
1550 UINT lo = LOWORD(lParam);
1551 UINT hi = HIWORD(lParam);
1552 int flag = 0;
1553 char buf[2];
1555 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1556 if (GlobalSize16(hi) != 0) flag |= 2;
1557 switch (flag)
1559 case 0:
1560 if (hi)
1562 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1563 hi = 0;
1565 break;
1566 case 1:
1567 break; /* atom, nothing to do */
1568 case 3:
1569 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1570 /* fall thru */
1571 case 2:
1572 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1573 break;
1575 lParam = PackDDElParam( WM_DDE_ACK, lo, hi );
1576 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1578 break; /* FIXME don't know how to free allocated memory (handle) !! */
1579 case WM_DDE_EXECUTE:
1580 lParam = convert_handle_16_to_32( lParam, GMEM_DDESHARE );
1581 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1582 break; /* FIXME don't know how to free allocated memory (handle) !! */
1583 case WM_PAINTCLIPBOARD:
1584 case WM_SIZECLIPBOARD:
1585 FIXME_(msg)( "message %04x needs translation\n", msg );
1586 break;
1587 default:
1588 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1589 break;
1591 return ret;
1595 /**********************************************************************
1596 * __wine_call_wndproc (USER.1010)
1598 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
1599 WINDOWPROC *proc )
1601 LRESULT result;
1603 if (proc->procA)
1604 WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1605 else
1606 WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
1607 return result;
1611 /**********************************************************************
1612 * WINPROC_CallProc32ATo16
1614 * Call a 16-bit window procedure, translating the 32-bit args.
1616 LRESULT WINPROC_CallProc32ATo16( winproc_callback16_t callback, HWND hwnd, UINT msg,
1617 WPARAM wParam, LPARAM lParam, LRESULT *result, void *arg )
1619 LRESULT ret = 0;
1621 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
1622 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1624 switch(msg)
1626 case WM_NCCREATE:
1627 case WM_CREATE:
1629 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)lParam;
1630 CREATESTRUCT16 cs;
1631 MDICREATESTRUCT16 mdi_cs16;
1632 BOOL mdi_child = (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD);
1634 CREATESTRUCT32Ato16( cs32, &cs );
1635 cs.lpszName = MapLS( cs32->lpszName );
1636 cs.lpszClass = MapLS( cs32->lpszClass );
1638 if (mdi_child)
1640 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
1641 MDICREATESTRUCT32Ato16( mdi_cs, &mdi_cs16 );
1642 mdi_cs16.szTitle = MapLS( mdi_cs->szTitle );
1643 mdi_cs16.szClass = MapLS( mdi_cs->szClass );
1644 cs.lpCreateParams = MapLS( &mdi_cs16 );
1646 lParam = MapLS( &cs );
1647 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1648 UnMapLS( lParam );
1649 UnMapLS( cs.lpszName );
1650 UnMapLS( cs.lpszClass );
1651 if (mdi_child)
1653 UnMapLS( cs.lpCreateParams );
1654 UnMapLS( mdi_cs16.szTitle );
1655 UnMapLS( mdi_cs16.szClass );
1658 break;
1659 case WM_MDICREATE:
1661 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)lParam;
1662 MDICREATESTRUCT16 cs;
1664 MDICREATESTRUCT32Ato16( cs32, &cs );
1665 cs.szTitle = MapLS( cs32->szTitle );
1666 cs.szClass = MapLS( cs32->szClass );
1667 lParam = MapLS( &cs );
1668 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1669 UnMapLS( lParam );
1670 UnMapLS( cs.szTitle );
1671 UnMapLS( cs.szClass );
1673 break;
1674 case WM_MDIACTIVATE:
1675 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
1676 ret = callback( HWND_16(hwnd), msg, ((HWND)lParam == hwnd),
1677 MAKELPARAM( LOWORD(lParam), LOWORD(wParam) ), result, arg );
1678 else
1679 ret = callback( HWND_16(hwnd), msg, HWND_16( (HWND)wParam ), 0, result, arg );
1680 break;
1681 case WM_MDIGETACTIVE:
1682 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1683 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(*result);
1684 *result = (LRESULT)WIN_Handle32( LOWORD(*result) );
1685 break;
1686 case WM_MDISETMENU:
1687 ret = callback( HWND_16(hwnd), msg, (lParam == 0),
1688 MAKELPARAM( LOWORD(wParam), LOWORD(lParam) ), result, arg );
1689 break;
1690 case WM_GETMINMAXINFO:
1692 MINMAXINFO *mmi32 = (MINMAXINFO *)lParam;
1693 MINMAXINFO16 mmi;
1695 MINMAXINFO32to16( mmi32, &mmi );
1696 lParam = MapLS( &mmi );
1697 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1698 UnMapLS( lParam );
1699 MINMAXINFO16to32( &mmi, mmi32 );
1701 break;
1702 case WM_NCCALCSIZE:
1704 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)lParam;
1705 NCCALCSIZE_PARAMS16 nc;
1706 WINDOWPOS16 winpos;
1708 RECT32to16( &nc32->rgrc[0], &nc.rgrc[0] );
1709 if (wParam)
1711 RECT32to16( &nc32->rgrc[1], &nc.rgrc[1] );
1712 RECT32to16( &nc32->rgrc[2], &nc.rgrc[2] );
1713 WINDOWPOS32to16( nc32->lppos, &winpos );
1714 nc.lppos = MapLS( &winpos );
1716 lParam = MapLS( &nc );
1717 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1718 UnMapLS( lParam );
1719 RECT16to32( &nc.rgrc[0], &nc32->rgrc[0] );
1720 if (wParam)
1722 RECT16to32( &nc.rgrc[1], &nc32->rgrc[1] );
1723 RECT16to32( &nc.rgrc[2], &nc32->rgrc[2] );
1724 WINDOWPOS16to32( &winpos, nc32->lppos );
1725 UnMapLS( nc.lppos );
1728 break;
1729 case WM_WINDOWPOSCHANGING:
1730 case WM_WINDOWPOSCHANGED:
1732 WINDOWPOS *winpos32 = (WINDOWPOS *)lParam;
1733 WINDOWPOS16 winpos;
1735 WINDOWPOS32to16( winpos32, &winpos );
1736 lParam = MapLS( &winpos );
1737 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1738 UnMapLS( lParam );
1739 WINDOWPOS16to32( &winpos, winpos32 );
1741 break;
1742 case WM_COMPAREITEM:
1744 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)lParam;
1745 COMPAREITEMSTRUCT16 cis;
1746 cis.CtlType = cis32->CtlType;
1747 cis.CtlID = cis32->CtlID;
1748 cis.hwndItem = HWND_16( cis32->hwndItem );
1749 cis.itemID1 = cis32->itemID1;
1750 cis.itemData1 = cis32->itemData1;
1751 cis.itemID2 = cis32->itemID2;
1752 cis.itemData2 = cis32->itemData2;
1753 lParam = MapLS( &cis );
1754 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1755 UnMapLS( lParam );
1757 break;
1758 case WM_DELETEITEM:
1760 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)lParam;
1761 DELETEITEMSTRUCT16 dis;
1762 dis.CtlType = dis32->CtlType;
1763 dis.CtlID = dis32->CtlID;
1764 dis.itemID = dis32->itemID;
1765 dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
1766 : HWND_16( dis32->hwndItem );
1767 dis.itemData = dis32->itemData;
1768 lParam = MapLS( &dis );
1769 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1770 UnMapLS( lParam );
1772 break;
1773 case WM_DRAWITEM:
1775 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)lParam;
1776 DRAWITEMSTRUCT16 dis;
1777 dis.CtlType = dis32->CtlType;
1778 dis.CtlID = dis32->CtlID;
1779 dis.itemID = dis32->itemID;
1780 dis.itemAction = dis32->itemAction;
1781 dis.itemState = dis32->itemState;
1782 dis.hwndItem = HWND_16( dis32->hwndItem );
1783 dis.hDC = HDC_16(dis32->hDC);
1784 dis.itemData = dis32->itemData;
1785 dis.rcItem.left = dis32->rcItem.left;
1786 dis.rcItem.top = dis32->rcItem.top;
1787 dis.rcItem.right = dis32->rcItem.right;
1788 dis.rcItem.bottom = dis32->rcItem.bottom;
1789 lParam = MapLS( &dis );
1790 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1791 UnMapLS( lParam );
1793 break;
1794 case WM_MEASUREITEM:
1796 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)lParam;
1797 MEASUREITEMSTRUCT16 mis;
1798 mis.CtlType = mis32->CtlType;
1799 mis.CtlID = mis32->CtlID;
1800 mis.itemID = mis32->itemID;
1801 mis.itemWidth = mis32->itemWidth;
1802 mis.itemHeight = mis32->itemHeight;
1803 mis.itemData = mis32->itemData;
1804 lParam = MapLS( &mis );
1805 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1806 UnMapLS( lParam );
1807 mis32->itemWidth = mis.itemWidth;
1808 mis32->itemHeight = mis.itemHeight;
1810 break;
1811 case WM_COPYDATA:
1813 COPYDATASTRUCT *cds32 = (COPYDATASTRUCT *)lParam;
1814 COPYDATASTRUCT16 cds;
1816 cds.dwData = cds32->dwData;
1817 cds.cbData = cds32->cbData;
1818 cds.lpData = MapLS( cds32->lpData );
1819 lParam = MapLS( &cds );
1820 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1821 UnMapLS( lParam );
1822 UnMapLS( cds.lpData );
1824 break;
1825 case WM_GETDLGCODE:
1826 if (lParam)
1828 MSG *msg32 = (MSG *)lParam;
1829 MSG16 msg16;
1831 msg16.hwnd = HWND_16( msg32->hwnd );
1832 msg16.message = msg32->message;
1833 msg16.wParam = msg32->wParam;
1834 msg16.lParam = msg32->lParam;
1835 msg16.time = msg32->time;
1836 msg16.pt.x = msg32->pt.x;
1837 msg16.pt.y = msg32->pt.y;
1838 lParam = MapLS( &msg16 );
1839 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1840 UnMapLS( lParam );
1842 else
1843 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1844 break;
1845 case WM_NEXTMENU:
1847 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1848 ret = callback( HWND_16(hwnd), msg, wParam, (LPARAM)next->hmenuIn, result, arg );
1849 next->hmenuNext = HMENU_32( LOWORD(*result) );
1850 next->hwndNext = WIN_Handle32( HIWORD(*result) );
1851 *result = 0;
1853 break;
1854 case WM_GETTEXT:
1855 case WM_ASKCBFORMATNAME:
1856 wParam = min( wParam, 0xff80 ); /* Must be < 64K */
1857 /* fall through */
1858 case WM_NOTIFY:
1859 case WM_SETTEXT:
1860 case WM_WININICHANGE:
1861 case WM_DEVMODECHANGE:
1862 lParam = MapLS( (void *)lParam );
1863 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1864 UnMapLS( lParam );
1865 break;
1866 case WM_ACTIVATE:
1867 case WM_CHARTOITEM:
1868 case WM_COMMAND:
1869 case WM_VKEYTOITEM:
1870 ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ),
1871 result, arg );
1872 break;
1873 case WM_HSCROLL:
1874 case WM_VSCROLL:
1875 ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( HIWORD(wParam), (HWND16)lParam ),
1876 result, arg );
1877 break;
1878 case WM_CTLCOLORMSGBOX:
1879 case WM_CTLCOLOREDIT:
1880 case WM_CTLCOLORLISTBOX:
1881 case WM_CTLCOLORBTN:
1882 case WM_CTLCOLORDLG:
1883 case WM_CTLCOLORSCROLLBAR:
1884 case WM_CTLCOLORSTATIC:
1885 ret = callback( HWND_16(hwnd), WM_CTLCOLOR, wParam,
1886 MAKELPARAM( (HWND16)lParam, msg - WM_CTLCOLORMSGBOX ), result, arg );
1887 break;
1888 case WM_MENUSELECT:
1889 if(HIWORD(wParam) & MF_POPUP)
1891 HMENU hmenu;
1892 if ((HIWORD(wParam) != 0xffff) || lParam)
1894 if ((hmenu = GetSubMenu( (HMENU)lParam, LOWORD(wParam) )))
1896 ret = callback( HWND_16(hwnd), msg, HMENU_16(hmenu),
1897 MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1898 break;
1902 /* fall through */
1903 case WM_MENUCHAR:
1904 ret = callback( HWND_16(hwnd), msg, wParam,
1905 MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1906 break;
1907 case WM_PARENTNOTIFY:
1908 if ((LOWORD(wParam) == WM_CREATE) || (LOWORD(wParam) == WM_DESTROY))
1909 ret = callback( HWND_16(hwnd), msg, wParam,
1910 MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ), result, arg );
1911 else
1912 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1913 break;
1914 case WM_ACTIVATEAPP:
1915 ret = callback( HWND_16(hwnd), msg, wParam, HTASK_16( (HANDLE)lParam ), result, arg );
1916 break;
1917 case WM_PAINT:
1918 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
1919 ret = callback( HWND_16(hwnd), WM_PAINTICON, 1, lParam, result, arg );
1920 else
1921 ret = callback( HWND_16(hwnd), WM_PAINT, wParam, lParam, result, arg );
1922 break;
1923 case WM_ERASEBKGND:
1924 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON )) msg = WM_ICONERASEBKGND;
1925 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1926 break;
1927 case WM_DDE_INITIATE:
1928 case WM_DDE_TERMINATE:
1929 case WM_DDE_UNADVISE:
1930 case WM_DDE_REQUEST:
1931 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam), lParam, result, arg );
1932 break;
1933 case WM_DDE_ADVISE:
1934 case WM_DDE_DATA:
1935 case WM_DDE_POKE:
1937 UINT_PTR lo32, hi;
1938 HANDLE16 lo16 = 0;
1940 UnpackDDElParam( msg, lParam, &lo32, &hi );
1941 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE))) break;
1942 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam),
1943 MAKELPARAM(lo16, hi), result, arg );
1945 break; /* FIXME don't know how to free allocated memory (handle) !! */
1946 case WM_DDE_ACK:
1948 UINT_PTR lo, hi;
1949 int flag = 0;
1950 char buf[2];
1952 UnpackDDElParam( msg, lParam, &lo, &hi );
1954 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
1955 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
1956 switch (flag)
1958 case 0:
1959 if (hi)
1961 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1962 hi = 0;
1964 break;
1965 case 1:
1966 break; /* atom, nothing to do */
1967 case 3:
1968 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1969 /* fall thru */
1970 case 2:
1971 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
1972 break;
1974 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam),
1975 MAKELPARAM(lo, hi), result, arg );
1977 break; /* FIXME don't know how to free allocated memory (handle) !! */
1978 case WM_DDE_EXECUTE:
1979 lParam = convert_handle_32_to_16(lParam, GMEM_DDESHARE);
1980 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1981 break; /* FIXME don't know how to free allocated memory (handle) !! */
1982 case SBM_SETRANGE:
1983 ret = callback( HWND_16(hwnd), SBM_SETRANGE16, 0, MAKELPARAM(wParam, lParam), result, arg );
1984 break;
1985 case SBM_GETRANGE:
1986 ret = callback( HWND_16(hwnd), SBM_GETRANGE16, wParam, lParam, result, arg );
1987 *(LPINT)wParam = LOWORD(*result);
1988 *(LPINT)lParam = HIWORD(*result);
1989 break;
1990 case BM_GETCHECK:
1991 case BM_SETCHECK:
1992 case BM_GETSTATE:
1993 case BM_SETSTATE:
1994 case BM_SETSTYLE:
1995 ret = callback( HWND_16(hwnd), msg + BM_GETCHECK16 - BM_GETCHECK, wParam, lParam, result, arg );
1996 break;
1997 case EM_GETSEL:
1998 case EM_GETRECT:
1999 case EM_SETRECT:
2000 case EM_SETRECTNP:
2001 case EM_SCROLL:
2002 case EM_LINESCROLL:
2003 case EM_SCROLLCARET:
2004 case EM_GETMODIFY:
2005 case EM_SETMODIFY:
2006 case EM_GETLINECOUNT:
2007 case EM_LINEINDEX:
2008 case EM_SETHANDLE:
2009 case EM_GETHANDLE:
2010 case EM_GETTHUMB:
2011 case EM_LINELENGTH:
2012 case EM_REPLACESEL:
2013 case EM_GETLINE:
2014 case EM_LIMITTEXT:
2015 case EM_CANUNDO:
2016 case EM_UNDO:
2017 case EM_FMTLINES:
2018 case EM_LINEFROMCHAR:
2019 case EM_SETTABSTOPS:
2020 case EM_SETPASSWORDCHAR:
2021 case EM_EMPTYUNDOBUFFER:
2022 case EM_GETFIRSTVISIBLELINE:
2023 case EM_SETREADONLY:
2024 case EM_SETWORDBREAKPROC:
2025 case EM_GETWORDBREAKPROC:
2026 case EM_GETPASSWORDCHAR:
2027 ret = callback( HWND_16(hwnd), msg + EM_GETSEL16 - EM_GETSEL, wParam, lParam, result, arg );
2028 break;
2029 case EM_SETSEL:
2030 ret = callback( HWND_16(hwnd), EM_SETSEL16, 0, MAKELPARAM( wParam, lParam ), result, arg );
2031 break;
2032 case LB_CARETOFF:
2033 case LB_CARETON:
2034 case LB_DELETESTRING:
2035 case LB_GETANCHORINDEX:
2036 case LB_GETCARETINDEX:
2037 case LB_GETCOUNT:
2038 case LB_GETCURSEL:
2039 case LB_GETHORIZONTALEXTENT:
2040 case LB_GETITEMDATA:
2041 case LB_GETITEMHEIGHT:
2042 case LB_GETSEL:
2043 case LB_GETSELCOUNT:
2044 case LB_GETTEXTLEN:
2045 case LB_GETTOPINDEX:
2046 case LB_RESETCONTENT:
2047 case LB_SELITEMRANGE:
2048 case LB_SELITEMRANGEEX:
2049 case LB_SETANCHORINDEX:
2050 case LB_SETCARETINDEX:
2051 case LB_SETCOLUMNWIDTH:
2052 case LB_SETCURSEL:
2053 case LB_SETHORIZONTALEXTENT:
2054 case LB_SETITEMDATA:
2055 case LB_SETITEMHEIGHT:
2056 case LB_SETSEL:
2057 case LB_SETTOPINDEX:
2058 ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2059 break;
2060 case LB_ADDSTRING:
2061 case LB_FINDSTRING:
2062 case LB_FINDSTRINGEXACT:
2063 case LB_INSERTSTRING:
2064 case LB_SELECTSTRING:
2065 case LB_GETTEXT:
2066 case LB_DIR:
2067 case LB_ADDFILE:
2068 lParam = MapLS( (LPSTR)lParam );
2069 ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2070 UnMapLS( lParam );
2071 break;
2072 case LB_GETSELITEMS:
2074 INT *items32 = (INT *)lParam;
2075 INT16 *items, buffer[512];
2076 unsigned int i;
2078 wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2079 if (!(items = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2080 lParam = MapLS( items );
2081 ret = callback( HWND_16(hwnd), LB_GETSELITEMS16, wParam, lParam, result, arg );
2082 UnMapLS( lParam );
2083 for (i = 0; i < wParam; i++) items32[i] = items[i];
2084 free_buffer( buffer, items );
2086 break;
2087 case LB_SETTABSTOPS:
2088 if (wParam)
2090 INT *stops32 = (INT *)lParam;
2091 INT16 *stops, buffer[512];
2092 unsigned int i;
2094 wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2095 if (!(stops = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2096 for (i = 0; i < wParam; i++) stops[i] = stops32[i];
2097 lParam = MapLS( stops );
2098 ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2099 UnMapLS( lParam );
2100 free_buffer( buffer, stops );
2102 else ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2103 break;
2104 case CB_DELETESTRING:
2105 case CB_GETCOUNT:
2106 case CB_GETLBTEXTLEN:
2107 case CB_LIMITTEXT:
2108 case CB_RESETCONTENT:
2109 case CB_SETEDITSEL:
2110 case CB_GETCURSEL:
2111 case CB_SETCURSEL:
2112 case CB_SHOWDROPDOWN:
2113 case CB_SETITEMDATA:
2114 case CB_SETITEMHEIGHT:
2115 case CB_GETITEMHEIGHT:
2116 case CB_SETEXTENDEDUI:
2117 case CB_GETEXTENDEDUI:
2118 case CB_GETDROPPEDSTATE:
2119 ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2120 break;
2121 case CB_GETEDITSEL:
2122 ret = callback( HWND_16(hwnd), CB_GETEDITSEL16, wParam, lParam, result, arg );
2123 if (wParam) *((PUINT)(wParam)) = LOWORD(*result);
2124 if (lParam) *((PUINT)(lParam)) = HIWORD(*result); /* FIXME: substract 1? */
2125 break;
2126 case CB_ADDSTRING:
2127 case CB_FINDSTRING:
2128 case CB_FINDSTRINGEXACT:
2129 case CB_INSERTSTRING:
2130 case CB_SELECTSTRING:
2131 case CB_DIR:
2132 case CB_GETLBTEXT:
2133 lParam = MapLS( (LPSTR)lParam );
2134 ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2135 UnMapLS( lParam );
2136 break;
2137 case LB_GETITEMRECT:
2138 case CB_GETDROPPEDCONTROLRECT:
2140 RECT *r32 = (RECT *)lParam;
2141 RECT16 rect;
2142 lParam = MapLS( &rect );
2143 ret = callback( HWND_16(hwnd),
2144 (msg == LB_GETITEMRECT) ? LB_GETITEMRECT16 : CB_GETDROPPEDCONTROLRECT16,
2145 wParam, lParam, result, arg );
2146 UnMapLS( lParam );
2147 RECT16to32( &rect, r32 );
2149 break;
2150 case WM_PAINTCLIPBOARD:
2151 case WM_SIZECLIPBOARD:
2152 FIXME_(msg)( "message %04x needs translation\n", msg );
2153 break;
2154 /* the following messages should not be sent to 16-bit apps */
2155 case WM_SIZING:
2156 case WM_MOVING:
2157 case WM_CAPTURECHANGED:
2158 case WM_STYLECHANGING:
2159 case WM_STYLECHANGED:
2160 break;
2161 default:
2162 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
2163 break;
2165 return ret;
2169 /**********************************************************************
2170 * CallWindowProc (USER.122)
2172 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
2173 WPARAM16 wParam, LPARAM lParam )
2175 WINDOWPROC *proc;
2176 LRESULT result;
2178 if (!func) return 0;
2180 if (!(proc = handle16_to_proc( func )))
2181 call_window_proc16( hwnd, msg, wParam, lParam, &result, func );
2182 else if (proc->procA)
2183 WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2184 else if (proc->procW)
2185 WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
2186 else
2187 call_window_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2189 return result;
2193 /**********************************************************************
2194 * CallWindowProcA (USER32.@)
2196 * The CallWindowProc() function invokes the windows procedure _func_,
2197 * with _hwnd_ as the target window, the message specified by _msg_, and
2198 * the message parameters _wParam_ and _lParam_.
2200 * Some kinds of argument conversion may be done, I'm not sure what.
2202 * CallWindowProc() may be used for windows subclassing. Use
2203 * SetWindowLong() to set a new windows procedure for windows of the
2204 * subclass, and handle subclassed messages in the new windows
2205 * procedure. The new windows procedure may then use CallWindowProc()
2206 * with _func_ set to the parent class's windows procedure to dispatch
2207 * the message to the superclass.
2209 * RETURNS
2211 * The return value is message dependent.
2213 * CONFORMANCE
2215 * ECMA-234, Win32
2217 LRESULT WINAPI CallWindowProcA(
2218 WNDPROC func, /* [in] window procedure */
2219 HWND hwnd, /* [in] target window */
2220 UINT msg, /* [in] message */
2221 WPARAM wParam, /* [in] message dependent parameter */
2222 LPARAM lParam /* [in] message dependent parameter */
2224 WINDOWPROC *proc;
2225 LRESULT result;
2227 if (!func) return 0;
2229 if (!(proc = handle_to_proc( func )))
2230 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2231 else if (proc->procA)
2232 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2233 else if (proc->procW)
2234 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procW );
2235 else
2236 WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2237 return result;
2241 /**********************************************************************
2242 * CallWindowProcW (USER32.@)
2244 * See CallWindowProcA.
2246 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
2247 WPARAM wParam, LPARAM lParam )
2249 WINDOWPROC *proc;
2250 LRESULT result;
2252 if (!func) return 0;
2254 if (!(proc = handle_to_proc( func )))
2255 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2256 else if (proc->procW)
2257 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2258 else if (proc->procA)
2259 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2260 else
2261 WINPROC_CallProcWtoA( call_window_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2262 return result;
2266 /**********************************************************************
2267 * WINPROC_CallDlgProc16
2269 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
2271 WINDOWPROC *proc;
2272 LRESULT result;
2273 INT_PTR ret;
2275 if (!func) return 0;
2277 if (!(proc = handle16_to_proc( (WNDPROC16)func )))
2279 ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, func );
2281 else if (proc->procA)
2283 ret = WINPROC_CallProc16To32A( call_dialog_proc, hwnd, msg, wParam, lParam,
2284 &result, proc->procA );
2285 SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2287 else if (proc->procW)
2289 ret = WINPROC_CallProc16To32A( call_dialog_proc_AtoW, hwnd, msg, wParam, lParam,
2290 &result, proc->procW );
2291 SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2293 else
2295 ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2297 return ret;
2301 /**********************************************************************
2302 * WINPROC_CallDlgProcA
2304 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2306 WINDOWPROC *proc;
2307 LRESULT result;
2308 INT_PTR ret;
2310 if (!func) return 0;
2312 if (!(proc = handle_to_proc( (WNDPROC)func )))
2313 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2314 else if (proc->procA)
2315 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2316 else if (proc->procW)
2318 ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procW );
2319 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2321 else
2323 ret = WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2324 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2326 return ret;
2330 /**********************************************************************
2331 * WINPROC_CallDlgProcW
2333 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2335 WINDOWPROC *proc;
2336 LRESULT result;
2337 INT_PTR ret;
2339 if (!func) return 0;
2341 if (!(proc = handle_to_proc( (WNDPROC)func )))
2342 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2343 else if (proc->procW)
2344 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2345 else if (proc->procA)
2347 ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2348 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2350 else
2352 ret = WINPROC_CallProcWtoA( call_dialog_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2353 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2355 return ret;