push 0c258732cb75565633c2f709ca58b227c9f8ae60
[wine/hacks.git] / dlls / user32 / winproc.c
blob27b31a12b3c0622aa9e724f171fa2716dff88e7e
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 "wownt32.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "controls.h"
35 #include "win.h"
36 #include "winproc.h"
37 #include "user_private.h"
38 #include "dde.h"
39 #include "winternl.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DECLARE_DEBUG_CHANNEL(msg);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45 WINE_DEFAULT_DEBUG_CHANNEL(win);
47 typedef struct tagWINDOWPROC
49 WNDPROC16 proc16; /* 16-bit window proc */
50 WNDPROC procA; /* ASCII window proc */
51 WNDPROC procW; /* Unicode window proc */
52 } WINDOWPROC;
54 #define WINPROC_HANDLE (~0UL >> 16)
55 #define MAX_WINPROCS 8192
56 #define BUILTIN_WINPROCS 8 /* first BUILTIN_WINPROCS entries are reserved for builtin procs */
58 static WINDOWPROC winproc_array[MAX_WINPROCS];
59 static UINT builtin_used;
60 static UINT winproc_used = BUILTIN_WINPROCS;
62 static CRITICAL_SECTION winproc_cs;
63 static CRITICAL_SECTION_DEBUG critsect_debug =
65 0, 0, &winproc_cs,
66 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": winproc_cs") }
69 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
71 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
73 if (size >= need) return static_buffer;
74 return HeapAlloc( GetProcessHeap(), 0, need );
77 static inline void free_buffer( void *static_buffer, void *buffer )
79 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
82 /* find an existing winproc for a given 16-bit function and type */
83 /* FIXME: probably should do something more clever than a linear search */
84 static inline WINDOWPROC *find_winproc16( WNDPROC16 func )
86 unsigned int i;
88 for (i = BUILTIN_WINPROCS; i < winproc_used; i++)
90 if (winproc_array[i].proc16 == func) return &winproc_array[i];
92 return NULL;
95 /* find an existing winproc for a given function and type */
96 /* FIXME: probably should do something more clever than a linear search */
97 static inline WINDOWPROC *find_winproc( WNDPROC funcA, WNDPROC funcW )
99 unsigned int i;
101 for (i = 0; i < builtin_used; i++)
103 /* match either proc, some apps confuse A and W */
104 if (funcA && winproc_array[i].procA != funcA && winproc_array[i].procW != funcA) continue;
105 if (funcW && winproc_array[i].procA != funcW && winproc_array[i].procW != funcW) continue;
106 return &winproc_array[i];
108 for (i = BUILTIN_WINPROCS; i < winproc_used; i++)
110 if (funcA && winproc_array[i].procA != funcA) continue;
111 if (funcW && winproc_array[i].procW != funcW) continue;
112 return &winproc_array[i];
114 return NULL;
117 /* find an existing builtin winproc */
118 static inline WINDOWPROC *find_builtin_proc( WNDPROC func )
120 unsigned int i;
122 for (i = 0; i < builtin_used; i++)
124 if (winproc_array[i].procA == func || winproc_array[i].procW == func)
125 return &winproc_array[i];
127 return NULL;
130 /* return the window proc for a given handle, or NULL for an invalid handle */
131 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
133 UINT index = LOWORD(handle);
134 if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
135 if (index >= winproc_used) return NULL;
136 return &winproc_array[index];
139 /* create a handle for a given window proc */
140 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
142 return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
145 /* allocate and initialize a new winproc */
146 static inline WINDOWPROC *alloc_winproc( WNDPROC funcA, WNDPROC funcW )
148 WINDOWPROC *proc;
150 /* check if the function is already a win proc */
151 if (funcA && (proc = handle_to_proc( funcA ))) return proc;
152 if (funcW && (proc = handle_to_proc( funcW ))) return proc;
153 if (!funcA && !funcW) return NULL;
155 EnterCriticalSection( &winproc_cs );
157 /* check if we already have a winproc for that function */
158 if (!(proc = find_winproc( funcA, funcW )))
160 if (funcA && funcW)
162 assert( builtin_used < BUILTIN_WINPROCS );
163 proc = &winproc_array[builtin_used++];
164 proc->procA = funcA;
165 proc->procW = funcW;
166 TRACE( "allocated %p for builtin %p/%p (%d/%d used)\n",
167 proc_to_handle(proc), funcA, funcW, builtin_used, BUILTIN_WINPROCS );
169 else if (winproc_used < MAX_WINPROCS)
171 proc = &winproc_array[winproc_used++];
172 proc->procA = funcA;
173 proc->procW = funcW;
174 TRACE( "allocated %p for %c %p (%d/%d used)\n",
175 proc_to_handle(proc), funcA ? 'A' : 'W', funcA ? funcA : funcW,
176 winproc_used, MAX_WINPROCS );
178 else FIXME( "too many winprocs, cannot allocate one for %p/%p\n", funcA, funcW );
180 else TRACE( "reusing %p for %p/%p\n", proc_to_handle(proc), funcA, funcW );
182 LeaveCriticalSection( &winproc_cs );
183 return proc;
187 #ifdef __i386__
189 #include "pshpack1.h"
191 /* Window procedure 16-to-32-bit thunk */
192 typedef struct
194 BYTE popl_eax; /* popl %eax (return address) */
195 BYTE pushl_func; /* pushl $proc */
196 WINDOWPROC *proc;
197 BYTE pushl_eax; /* pushl %eax */
198 BYTE ljmp; /* ljmp relay*/
199 DWORD relay_offset; /* __wine_call_wndproc */
200 WORD relay_sel;
201 } WINPROC_THUNK;
203 #include "poppack.h"
205 #define MAX_THUNKS (0x10000 / sizeof(WINPROC_THUNK))
207 static WINPROC_THUNK *thunk_array;
208 static UINT thunk_selector;
209 static UINT thunk_used;
211 /* return the window proc for a given handle, or NULL for an invalid handle */
212 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
214 if (HIWORD(handle) == thunk_selector)
216 UINT index = LOWORD(handle) / sizeof(WINPROC_THUNK);
217 /* check alignment */
218 if (index * sizeof(WINPROC_THUNK) != LOWORD(handle)) return NULL;
219 /* check array limits */
220 if (index >= thunk_used) return NULL;
221 return thunk_array[index].proc;
223 return handle_to_proc( (WNDPROC)handle );
226 /* allocate a 16-bit thunk for an existing window proc */
227 static WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
229 static FARPROC16 relay;
230 UINT i;
232 if (proc->proc16) return proc->proc16;
234 EnterCriticalSection( &winproc_cs );
236 if (!thunk_array) /* allocate the array and its selector */
238 LDT_ENTRY entry;
240 if (!(thunk_selector = wine_ldt_alloc_entries(1))) goto done;
241 if (!(thunk_array = VirtualAlloc( NULL, MAX_THUNKS * sizeof(WINPROC_THUNK), MEM_COMMIT,
242 PAGE_EXECUTE_READWRITE ))) goto done;
243 wine_ldt_set_base( &entry, thunk_array );
244 wine_ldt_set_limit( &entry, MAX_THUNKS * sizeof(WINPROC_THUNK) - 1 );
245 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
246 wine_ldt_set_entry( thunk_selector, &entry );
247 relay = GetProcAddress16( GetModuleHandle16("user"), "__wine_call_wndproc" );
250 /* check if it already exists */
251 for (i = 0; i < thunk_used; i++) if (thunk_array[i].proc == proc) break;
253 if (i == thunk_used) /* create a new one */
255 WINPROC_THUNK *thunk;
257 if (thunk_used >= MAX_THUNKS) goto done;
258 thunk = &thunk_array[thunk_used++];
259 thunk->popl_eax = 0x58; /* popl %eax */
260 thunk->pushl_func = 0x68; /* pushl $proc */
261 thunk->proc = proc;
262 thunk->pushl_eax = 0x50; /* pushl %eax */
263 thunk->ljmp = 0xea; /* ljmp relay*/
264 thunk->relay_offset = OFFSETOF(relay);
265 thunk->relay_sel = SELECTOROF(relay);
267 proc->proc16 = (WNDPROC16)MAKESEGPTR( thunk_selector, i * sizeof(WINPROC_THUNK) );
268 done:
269 LeaveCriticalSection( &winproc_cs );
270 return proc->proc16;
273 #else /* __i386__ */
275 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
277 return handle_to_proc( (WNDPROC)handle );
280 static inline WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
282 return 0;
285 #endif /* __i386__ */
288 #ifdef __i386__
289 /* Some window procedures modify register they shouldn't, or are not
290 * properly declared stdcall; so we need a small assembly wrapper to
291 * call them. */
292 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
293 WPARAM wParam, LPARAM lParam );
294 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
295 "pushl %ebp\n\t"
296 "movl %esp,%ebp\n\t"
297 "pushl %edi\n\t"
298 "pushl %esi\n\t"
299 "pushl %ebx\n\t"
300 "subl $12,%esp\n\t"
301 "pushl 24(%ebp)\n\t"
302 "pushl 20(%ebp)\n\t"
303 "pushl 16(%ebp)\n\t"
304 "pushl 12(%ebp)\n\t"
305 "movl 8(%ebp),%eax\n\t"
306 "call *%eax\n\t"
307 "leal -12(%ebp),%esp\n\t"
308 "popl %ebx\n\t"
309 "popl %esi\n\t"
310 "popl %edi\n\t"
311 "leave\n\t"
312 "ret" )
313 #else
314 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
315 WPARAM wParam, LPARAM lParam )
317 return proc( hwnd, msg, wParam, lParam );
319 #endif /* __i386__ */
321 static void RECT16to32( const RECT16 *from, RECT *to )
323 to->left = from->left;
324 to->top = from->top;
325 to->right = from->right;
326 to->bottom = from->bottom;
329 static void RECT32to16( const RECT *from, RECT16 *to )
331 to->left = from->left;
332 to->top = from->top;
333 to->right = from->right;
334 to->bottom = from->bottom;
337 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
339 to->ptReserved.x = from->ptReserved.x;
340 to->ptReserved.y = from->ptReserved.y;
341 to->ptMaxSize.x = from->ptMaxSize.x;
342 to->ptMaxSize.y = from->ptMaxSize.y;
343 to->ptMaxPosition.x = from->ptMaxPosition.x;
344 to->ptMaxPosition.y = from->ptMaxPosition.y;
345 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
346 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
347 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
348 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
351 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
353 to->ptReserved.x = from->ptReserved.x;
354 to->ptReserved.y = from->ptReserved.y;
355 to->ptMaxSize.x = from->ptMaxSize.x;
356 to->ptMaxSize.y = from->ptMaxSize.y;
357 to->ptMaxPosition.x = from->ptMaxPosition.x;
358 to->ptMaxPosition.y = from->ptMaxPosition.y;
359 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
360 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
361 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
362 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
365 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
367 to->hwnd = HWND_16(from->hwnd);
368 to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
369 to->x = from->x;
370 to->y = from->y;
371 to->cx = from->cx;
372 to->cy = from->cy;
373 to->flags = from->flags;
376 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
378 to->hwnd = WIN_Handle32(from->hwnd);
379 to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
380 HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
381 to->x = from->x;
382 to->y = from->y;
383 to->cx = from->cx;
384 to->cy = from->cy;
385 to->flags = from->flags;
388 /* The strings are not copied */
389 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
391 to->lpCreateParams = (SEGPTR)from->lpCreateParams;
392 to->hInstance = HINSTANCE_16(from->hInstance);
393 to->hMenu = HMENU_16(from->hMenu);
394 to->hwndParent = HWND_16(from->hwndParent);
395 to->cy = from->cy;
396 to->cx = from->cx;
397 to->y = from->y;
398 to->x = from->x;
399 to->style = from->style;
400 to->dwExStyle = from->dwExStyle;
403 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
406 to->lpCreateParams = (LPVOID)from->lpCreateParams;
407 to->hInstance = HINSTANCE_32(from->hInstance);
408 to->hMenu = HMENU_32(from->hMenu);
409 to->hwndParent = WIN_Handle32(from->hwndParent);
410 to->cy = from->cy;
411 to->cx = from->cx;
412 to->y = from->y;
413 to->x = from->x;
414 to->style = from->style;
415 to->dwExStyle = from->dwExStyle;
416 to->lpszName = MapSL(from->lpszName);
417 to->lpszClass = MapSL(from->lpszClass);
420 /* The strings are not copied */
421 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
423 to->hOwner = HINSTANCE_16(from->hOwner);
424 to->x = from->x;
425 to->y = from->y;
426 to->cx = from->cx;
427 to->cy = from->cy;
428 to->style = from->style;
429 to->lParam = from->lParam;
432 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
434 to->hOwner = HINSTANCE_32(from->hOwner);
435 to->x = from->x;
436 to->y = from->y;
437 to->cx = from->cx;
438 to->cy = from->cy;
439 to->style = from->style;
440 to->lParam = from->lParam;
441 to->szTitle = MapSL(from->szTitle);
442 to->szClass = MapSL(from->szClass);
445 static WPARAM map_wparam_char_AtoW( WPARAM wParam, DWORD len )
447 CHAR ch[2];
448 WCHAR wch;
450 ch[0] = (wParam >> 8);
451 ch[1] = wParam & 0xff;
452 if (len > 1 && ch[0])
453 RtlMultiByteToUnicodeN( &wch, sizeof(wch), NULL, ch, 2 );
454 else
455 RtlMultiByteToUnicodeN( &wch, sizeof(wch), NULL, ch + 1, 1 );
456 return MAKEWPARAM( wch, HIWORD(wParam) );
459 static WPARAM map_wparam_char_WtoA( WPARAM wParam, DWORD len )
461 WCHAR wch = wParam;
462 BYTE ch[2];
464 RtlUnicodeToMultiByteN( (LPSTR)ch, len, &len, &wch, sizeof(wch) );
465 if (len == 2)
466 return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
467 else
468 return MAKEWPARAM( ch[0], HIWORD(wParam) );
471 /* call a 32-bit window procedure */
472 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
474 WNDPROC proc = arg;
476 USER_CheckNotLock();
478 hwnd = WIN_GetFullHandle( hwnd );
479 if (TRACE_ON(relay))
480 DPRINTF( "%04x:Call window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
481 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
483 *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
485 if (TRACE_ON(relay))
486 DPRINTF( "%04x:Ret window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx\n",
487 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
488 return *result;
491 /* call a 32-bit dialog procedure */
492 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
494 WNDPROC proc = arg;
495 LRESULT ret;
497 USER_CheckNotLock();
499 hwnd = WIN_GetFullHandle( hwnd );
500 if (TRACE_ON(relay))
501 DPRINTF( "%04x:Call dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
502 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
504 ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
505 *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
507 if (TRACE_ON(relay))
508 DPRINTF( "%04x:Ret dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx result=%08lx\n",
509 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
510 return ret;
513 /* call a 16-bit window procedure */
514 static LRESULT call_window_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
515 LRESULT *result, void *arg )
517 WNDPROC16 proc = arg;
518 CONTEXT86 context;
519 size_t size = 0;
520 struct
522 WORD params[5];
523 union
525 CREATESTRUCT16 cs16;
526 DRAWITEMSTRUCT16 dis16;
527 COMPAREITEMSTRUCT16 cis16;
528 } u;
529 } args;
531 USER_CheckNotLock();
533 /* Window procedures want ax = hInstance, ds = es = ss */
535 memset(&context, 0, sizeof(context));
536 context.SegDs = context.SegEs = SELECTOROF(NtCurrentTeb()->WOW32Reserved);
537 context.SegFs = wine_get_fs();
538 context.SegGs = wine_get_gs();
539 if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
540 context.SegCs = SELECTOROF(proc);
541 context.Eip = OFFSETOF(proc);
542 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
544 if (lParam)
546 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
547 work if structures passed in lParam are placed in the stack/data
548 segment. Programmers easily make the mistake of converting lParam
549 to a near rather than a far pointer, since Windows apparently
550 allows this. We copy the structures to the 16 bit stack; this is
551 ugly but makes these programs work. */
552 switch (msg)
554 case WM_CREATE:
555 case WM_NCCREATE:
556 size = sizeof(CREATESTRUCT16); break;
557 case WM_DRAWITEM:
558 size = sizeof(DRAWITEMSTRUCT16); break;
559 case WM_COMPAREITEM:
560 size = sizeof(COMPAREITEMSTRUCT16); break;
562 if (size)
564 memcpy( &args.u, MapSL(lParam), size );
565 lParam = PtrToUlong(NtCurrentTeb()->WOW32Reserved) - size;
569 args.params[4] = hwnd;
570 args.params[3] = msg;
571 args.params[2] = wParam;
572 args.params[1] = HIWORD(lParam);
573 args.params[0] = LOWORD(lParam);
574 WOWCallback16Ex( 0, WCB16_REGS, sizeof(args.params) + size, &args, (DWORD *)&context );
575 *result = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
576 return *result;
579 /* call a 16-bit dialog procedure */
580 static LRESULT call_dialog_proc16( HWND16 hwnd, UINT16 msg, WPARAM16 wp, LPARAM lp,
581 LRESULT *result, void *arg )
583 LRESULT ret = call_window_proc16( hwnd, msg, wp, lp, result, arg );
584 *result = GetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT );
585 return LOWORD(ret);
588 /* helper callback for 32W->16 conversion */
589 static LRESULT call_window_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
590 LRESULT *result, void *arg )
592 return WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wp, lp, result, arg );
595 /* helper callback for 32W->16 conversion */
596 static LRESULT call_dialog_proc_Ato16( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
597 LRESULT *result, void *arg )
599 return WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wp, lp, result, arg );
602 /* helper callback for 16->32W conversion */
603 static LRESULT call_window_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
604 LRESULT *result, void *arg )
606 return WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wp, lp, result, arg );
609 /* helper callback for 16->32W conversion */
610 static LRESULT call_dialog_proc_AtoW( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
611 LRESULT *result, void *arg )
613 return WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wp, lp, result, arg );
617 /**********************************************************************
618 * WINPROC_GetProc16
620 * Get a window procedure pointer that can be passed to the Windows program.
622 WNDPROC16 WINPROC_GetProc16( WNDPROC proc, BOOL unicode )
624 WINDOWPROC *ptr;
626 if (unicode) ptr = alloc_winproc( NULL, proc );
627 else ptr = alloc_winproc( proc, NULL );
629 if (!ptr) return 0;
630 return alloc_win16_thunk( ptr );
634 /**********************************************************************
635 * WINPROC_GetProc
637 * Get a window procedure pointer that can be passed to the Windows program.
639 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
641 WINDOWPROC *ptr = handle_to_proc( proc );
643 if (!ptr) return proc;
644 if (unicode)
646 if (ptr->procW) return ptr->procW;
647 return proc;
649 else
651 if (ptr->procA) return ptr->procA;
652 return proc;
657 /**********************************************************************
658 * WINPROC_AllocProc16
660 * Allocate a window procedure for a window or class.
662 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
663 * lot of windows, it will usually only have a limited number of window procedures, so the
664 * array won't grow too large, and this way we avoid the need to track allocations per window.
666 WNDPROC WINPROC_AllocProc16( WNDPROC16 func )
668 WINDOWPROC *proc;
670 if (!func) return NULL;
672 /* check if the function is already a win proc */
673 if (!(proc = handle16_to_proc( func )))
675 EnterCriticalSection( &winproc_cs );
677 /* then check if we already have a winproc for that function */
678 if (!(proc = find_winproc16( func )))
680 if (winproc_used < MAX_WINPROCS)
682 proc = &winproc_array[winproc_used++];
683 proc->proc16 = func;
684 TRACE( "allocated %p for %p/16-bit (%d/%d used)\n",
685 proc_to_handle(proc), func, winproc_used, MAX_WINPROCS );
687 else FIXME( "too many winprocs, cannot allocate one for 16-bit %p\n", func );
689 else TRACE( "reusing %p for %p/16-bit\n", proc_to_handle(proc), func );
691 LeaveCriticalSection( &winproc_cs );
693 return proc_to_handle( proc );
697 /**********************************************************************
698 * WINPROC_AllocProc
700 * Allocate a window procedure for a window or class.
702 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
703 * lot of windows, it will usually only have a limited number of window procedures, so the
704 * array won't grow too large, and this way we avoid the need to track allocations per window.
706 WNDPROC WINPROC_AllocProc( WNDPROC funcA, WNDPROC funcW )
708 WINDOWPROC *proc;
710 if (!(proc = alloc_winproc( funcA, funcW ))) return NULL;
711 return proc_to_handle( proc );
715 /**********************************************************************
716 * WINPROC_IsUnicode
718 * Return the window procedure type, or the default value if not a winproc handle.
720 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
722 WINDOWPROC *ptr = handle_to_proc( proc );
724 if (!ptr) return def_val;
725 if (ptr->procA && ptr->procW) return def_val; /* can be both */
726 return (ptr->procW != NULL);
730 /**********************************************************************
731 * WINPROC_TestLBForStr
733 * Return TRUE if the lparam is a string
735 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
737 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
738 if (msg <= CB_MSGMAX)
739 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
740 else
741 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
746 static UINT_PTR convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
748 HANDLE dst;
749 UINT sz = GlobalSize16(src);
750 LPSTR ptr16, ptr32;
752 if (!(dst = GlobalAlloc(flags, sz)))
753 return 0;
754 ptr16 = GlobalLock16(src);
755 ptr32 = GlobalLock(dst);
756 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
757 GlobalUnlock16(src);
758 GlobalUnlock(dst);
760 return (UINT_PTR)dst;
763 static HANDLE16 convert_handle_32_to_16(UINT_PTR src, unsigned int flags)
765 HANDLE16 dst;
766 UINT sz = GlobalSize((HANDLE)src);
767 LPSTR ptr16, ptr32;
769 if (!(dst = GlobalAlloc16(flags, sz)))
770 return 0;
771 ptr32 = GlobalLock((HANDLE)src);
772 ptr16 = GlobalLock16(dst);
773 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
774 GlobalUnlock((HANDLE)src);
775 GlobalUnlock16(dst);
777 return dst;
781 /**********************************************************************
782 * WINPROC_CallProcAtoW
784 * Call a window procedure, translating args from Ansi to Unicode.
786 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
787 LPARAM lParam, LRESULT *result, void *arg )
789 LRESULT ret = 0;
791 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
792 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
794 switch(msg)
796 case WM_NCCREATE:
797 case WM_CREATE:
799 WCHAR *ptr, buffer[512];
800 CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
801 CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
802 MDICREATESTRUCTW mdi_cs;
803 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
805 if (HIWORD(csA->lpszClass))
807 class_lenA = strlen(csA->lpszClass) + 1;
808 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
810 if (HIWORD(csA->lpszName))
812 name_lenA = strlen(csA->lpszName) + 1;
813 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
816 if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
818 if (class_lenW)
820 csW.lpszClass = ptr;
821 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
823 if (name_lenW)
825 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
826 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
827 csA->lpszName, name_lenA );
830 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
832 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
833 mdi_cs.szTitle = csW.lpszName;
834 mdi_cs.szClass = csW.lpszClass;
835 csW.lpCreateParams = &mdi_cs;
838 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
839 free_buffer( buffer, ptr );
841 break;
843 case WM_MDICREATE:
845 WCHAR *ptr, buffer[512];
846 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
847 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
848 MDICREATESTRUCTW csW;
850 memcpy( &csW, csA, sizeof(csW) );
852 if (HIWORD(csA->szTitle))
854 title_lenA = strlen(csA->szTitle) + 1;
855 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
857 if (HIWORD(csA->szClass))
859 class_lenA = strlen(csA->szClass) + 1;
860 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
863 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
865 if (title_lenW)
867 csW.szTitle = ptr;
868 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
870 if (class_lenW)
872 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
873 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
874 csA->szClass, class_lenA );
876 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
877 free_buffer( buffer, ptr );
879 break;
881 case WM_GETTEXT:
882 case WM_ASKCBFORMATNAME:
884 WCHAR *ptr, buffer[512];
885 LPSTR str = (LPSTR)lParam;
886 DWORD len = wParam * sizeof(WCHAR);
888 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
889 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
890 if (wParam)
892 len = 0;
893 if (*result)
894 RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, strlenW(ptr) * sizeof(WCHAR) );
895 str[len] = 0;
896 *result = len;
898 free_buffer( buffer, ptr );
900 break;
902 case LB_ADDSTRING:
903 case LB_INSERTSTRING:
904 case LB_FINDSTRING:
905 case LB_FINDSTRINGEXACT:
906 case LB_SELECTSTRING:
907 case CB_ADDSTRING:
908 case CB_INSERTSTRING:
909 case CB_FINDSTRING:
910 case CB_FINDSTRINGEXACT:
911 case CB_SELECTSTRING:
912 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
914 ret = callback( hwnd, msg, wParam, lParam, result, arg );
915 break;
917 /* fall through */
918 case WM_SETTEXT:
919 case WM_WININICHANGE:
920 case WM_DEVMODECHANGE:
921 case CB_DIR:
922 case LB_DIR:
923 case LB_ADDFILE:
924 case EM_REPLACESEL:
925 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
926 else
928 WCHAR *ptr, buffer[512];
929 LPCSTR strA = (LPCSTR)lParam;
930 DWORD lenW, lenA = strlen(strA) + 1;
932 RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
933 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
935 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
936 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
937 free_buffer( buffer, ptr );
940 break;
942 case LB_GETTEXT:
943 case CB_GETLBTEXT:
944 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
946 WCHAR buffer[512]; /* FIXME: fixed sized buffer */
948 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
949 if (*result >= 0)
951 DWORD len;
952 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
953 buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
954 *result = len - 1;
957 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
958 break;
960 case EM_GETLINE:
962 WCHAR *ptr, buffer[512];
963 WORD len = *(WORD *)lParam;
965 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
966 *((WORD *)ptr) = len; /* store the length */
967 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
968 if (*result)
970 DWORD reslen;
971 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
972 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
973 *result = reslen;
975 free_buffer( buffer, ptr );
977 break;
979 case WM_GETDLGCODE:
980 if (lParam)
982 MSG newmsg = *(MSG *)lParam;
983 switch(newmsg.message)
985 case WM_CHAR:
986 case WM_DEADCHAR:
987 case WM_SYSCHAR:
988 case WM_SYSDEADCHAR:
989 newmsg.wParam = map_wparam_char_AtoW( newmsg.wParam, 1 );
990 break;
991 case WM_IME_CHAR:
992 newmsg.wParam = map_wparam_char_AtoW( newmsg.wParam, 2 );
993 break;
995 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
997 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
998 break;
1000 case WM_CHARTOITEM:
1001 case WM_MENUCHAR:
1002 case WM_CHAR:
1003 case WM_DEADCHAR:
1004 case WM_SYSCHAR:
1005 case WM_SYSDEADCHAR:
1006 case EM_SETPASSWORDCHAR:
1007 ret = callback( hwnd, msg, map_wparam_char_AtoW(wParam,1), lParam, result, arg );
1008 break;
1010 case WM_IME_CHAR:
1011 ret = callback( hwnd, msg, map_wparam_char_AtoW(wParam,2), lParam, result, arg );
1012 break;
1014 case WM_GETTEXTLENGTH:
1015 case CB_GETLBTEXTLEN:
1016 case LB_GETTEXTLEN:
1017 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1018 if (*result >= 0)
1020 WCHAR *ptr, buffer[512];
1021 LRESULT tmp;
1022 DWORD len = *result + 1;
1023 /* Determine respective GETTEXT message */
1024 UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
1025 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
1026 /* wParam differs between the messages */
1027 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
1029 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
1031 if (callback == call_window_proc) /* FIXME: hack */
1032 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
1033 else
1034 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
1035 RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
1036 *result = len;
1037 free_buffer( buffer, ptr );
1039 break;
1041 case WM_PAINTCLIPBOARD:
1042 case WM_SIZECLIPBOARD:
1043 FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
1044 SPY_GetMsgName(msg, hwnd), msg );
1045 break;
1047 default:
1048 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1049 break;
1051 return ret;
1055 /**********************************************************************
1056 * WINPROC_CallProcWtoA
1058 * Call a window procedure, translating args from Unicode to Ansi.
1060 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
1061 LPARAM lParam, LRESULT *result, void *arg )
1063 LRESULT ret = 0;
1065 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
1066 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1068 switch(msg)
1070 case WM_NCCREATE:
1071 case WM_CREATE:
1072 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
1073 * at this point.
1075 char buffer[1024], *cls, *name;
1076 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
1077 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
1078 MDICREATESTRUCTA mdi_cs;
1079 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
1081 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
1082 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
1084 if (csW->lpszName)
1086 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
1087 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
1089 else
1090 name_lenW = name_lenA = 0;
1092 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA + 2 ))) break;
1094 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
1095 cls[class_lenA] = 0;
1096 csA.lpszClass = cls;
1098 if (csW->lpszName)
1100 name = cls + class_lenA + 1;
1101 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
1102 name[name_lenA] = 0;
1103 csA.lpszName = name;
1106 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1108 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
1109 mdi_cs.szTitle = csA.lpszName;
1110 mdi_cs.szClass = csA.lpszClass;
1111 csA.lpCreateParams = &mdi_cs;
1114 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1115 free_buffer( buffer, cls );
1117 break;
1119 case WM_GETTEXT:
1120 case WM_ASKCBFORMATNAME:
1122 char *ptr, buffer[512];
1123 DWORD len = wParam * 2;
1125 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
1126 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1127 if (len)
1129 if (*result)
1131 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
1132 *result = len/sizeof(WCHAR) - 1; /* do not count terminating null */
1134 ((LPWSTR)lParam)[*result] = 0;
1136 free_buffer( buffer, ptr );
1138 break;
1140 case LB_ADDSTRING:
1141 case LB_INSERTSTRING:
1142 case LB_FINDSTRING:
1143 case LB_FINDSTRINGEXACT:
1144 case LB_SELECTSTRING:
1145 case CB_ADDSTRING:
1146 case CB_INSERTSTRING:
1147 case CB_FINDSTRING:
1148 case CB_FINDSTRINGEXACT:
1149 case CB_SELECTSTRING:
1150 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
1152 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1153 break;
1155 /* fall through */
1156 case WM_SETTEXT:
1157 case WM_WININICHANGE:
1158 case WM_DEVMODECHANGE:
1159 case CB_DIR:
1160 case LB_DIR:
1161 case LB_ADDFILE:
1162 case EM_REPLACESEL:
1163 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
1164 else
1166 char *ptr, buffer[512];
1167 LPCWSTR strW = (LPCWSTR)lParam;
1168 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
1170 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
1171 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
1173 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
1174 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1175 free_buffer( buffer, ptr );
1178 break;
1180 case WM_MDICREATE:
1182 char *ptr, buffer[1024];
1183 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
1184 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1185 MDICREATESTRUCTA csA;
1187 memcpy( &csA, csW, sizeof(csA) );
1189 if (HIWORD(csW->szTitle))
1191 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
1192 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
1194 if (HIWORD(csW->szClass))
1196 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
1197 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
1200 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
1202 if (title_lenA)
1204 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
1205 csA.szTitle = ptr;
1207 if (class_lenA)
1209 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
1210 csA.szClass = ptr + title_lenA;
1212 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
1213 free_buffer( buffer, ptr );
1215 break;
1217 case LB_GETTEXT:
1218 case CB_GETLBTEXT:
1219 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
1221 char buffer[512]; /* FIXME: fixed sized buffer */
1223 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
1224 if (*result >= 0)
1226 DWORD len;
1227 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
1228 *result = len / sizeof(WCHAR) - 1;
1231 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1232 break;
1234 case EM_GETLINE:
1236 char *ptr, buffer[512];
1237 WORD len = *(WORD *)lParam;
1239 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
1240 *((WORD *)ptr) = len * 2; /* store the length */
1241 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
1242 if (*result)
1244 DWORD reslen;
1245 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
1246 *result = reslen / sizeof(WCHAR);
1247 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
1249 free_buffer( buffer, ptr );
1251 break;
1253 case WM_GETDLGCODE:
1254 if (lParam)
1256 MSG newmsg = *(MSG *)lParam;
1257 switch(newmsg.message)
1259 case WM_CHAR:
1260 case WM_DEADCHAR:
1261 case WM_SYSCHAR:
1262 case WM_SYSDEADCHAR:
1263 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
1264 break;
1265 case WM_IME_CHAR:
1266 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
1267 break;
1269 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
1271 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
1272 break;
1274 case WM_CHARTOITEM:
1275 case WM_MENUCHAR:
1276 case WM_CHAR:
1277 case WM_DEADCHAR:
1278 case WM_SYSCHAR:
1279 case WM_SYSDEADCHAR:
1280 case EM_SETPASSWORDCHAR:
1281 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
1282 break;
1284 case WM_IME_CHAR:
1285 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
1286 break;
1288 case WM_PAINTCLIPBOARD:
1289 case WM_SIZECLIPBOARD:
1290 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
1291 SPY_GetMsgName(msg, hwnd), msg );
1292 break;
1294 default:
1295 ret = callback( hwnd, msg, wParam, lParam, result, arg );
1296 break;
1299 return ret;
1303 /**********************************************************************
1304 * WINPROC_CallProc16To32A
1306 LRESULT WINPROC_CallProc16To32A( winproc_callback_t callback, HWND16 hwnd, UINT16 msg,
1307 WPARAM16 wParam, LPARAM lParam, LRESULT *result, void *arg )
1309 LRESULT ret = 0;
1310 HWND hwnd32 = WIN_Handle32( hwnd );
1312 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
1313 hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
1315 switch(msg)
1317 case WM_NCCREATE:
1318 case WM_CREATE:
1320 CREATESTRUCT16 *cs16 = MapSL(lParam);
1321 CREATESTRUCTA cs;
1322 MDICREATESTRUCTA mdi_cs;
1324 CREATESTRUCT16to32A( cs16, &cs );
1325 if (GetWindowLongW(hwnd32, GWL_EXSTYLE) & WS_EX_MDICHILD)
1327 MDICREATESTRUCT16 *mdi_cs16 = MapSL(cs16->lpCreateParams);
1328 MDICREATESTRUCT16to32A(mdi_cs16, &mdi_cs);
1329 cs.lpCreateParams = &mdi_cs;
1331 ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1332 CREATESTRUCT32Ato16( &cs, cs16 );
1334 break;
1335 case WM_MDICREATE:
1337 MDICREATESTRUCT16 *cs16 = MapSL(lParam);
1338 MDICREATESTRUCTA cs;
1340 MDICREATESTRUCT16to32A( cs16, &cs );
1341 ret = callback( hwnd32, msg, wParam, (LPARAM)&cs, result, arg );
1342 MDICREATESTRUCT32Ato16( &cs, cs16 );
1344 break;
1345 case WM_MDIACTIVATE:
1346 if (lParam)
1347 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32( HIWORD(lParam) ),
1348 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1349 else /* message sent to MDI client */
1350 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1351 break;
1352 case WM_MDIGETACTIVE:
1354 BOOL maximized = FALSE;
1355 ret = callback( hwnd32, msg, wParam, (LPARAM)&maximized, result, arg );
1356 *result = MAKELRESULT( LOWORD(*result), maximized );
1358 break;
1359 case WM_MDISETMENU:
1360 ret = callback( hwnd32, wParam ? WM_MDIREFRESHMENU : WM_MDISETMENU,
1361 (WPARAM)HMENU_32(LOWORD(lParam)), (LPARAM)HMENU_32(HIWORD(lParam)),
1362 result, arg );
1363 break;
1364 case WM_GETMINMAXINFO:
1366 MINMAXINFO16 *mmi16 = MapSL(lParam);
1367 MINMAXINFO mmi;
1369 MINMAXINFO16to32( mmi16, &mmi );
1370 ret = callback( hwnd32, msg, wParam, (LPARAM)&mmi, result, arg );
1371 MINMAXINFO32to16( &mmi, mmi16 );
1373 break;
1374 case WM_WINDOWPOSCHANGING:
1375 case WM_WINDOWPOSCHANGED:
1377 WINDOWPOS16 *winpos16 = MapSL(lParam);
1378 WINDOWPOS winpos;
1380 WINDOWPOS16to32( winpos16, &winpos );
1381 ret = callback( hwnd32, msg, wParam, (LPARAM)&winpos, result, arg );
1382 WINDOWPOS32to16( &winpos, winpos16 );
1384 break;
1385 case WM_NCCALCSIZE:
1387 NCCALCSIZE_PARAMS16 *nc16 = MapSL(lParam);
1388 NCCALCSIZE_PARAMS nc;
1389 WINDOWPOS winpos;
1391 RECT16to32( &nc16->rgrc[0], &nc.rgrc[0] );
1392 if (wParam)
1394 RECT16to32( &nc16->rgrc[1], &nc.rgrc[1] );
1395 RECT16to32( &nc16->rgrc[2], &nc.rgrc[2] );
1396 WINDOWPOS16to32( MapSL(nc16->lppos), &winpos );
1397 nc.lppos = &winpos;
1399 ret = callback( hwnd32, msg, wParam, (LPARAM)&nc, result, arg );
1400 RECT32to16( &nc.rgrc[0], &nc16->rgrc[0] );
1401 if (wParam)
1403 RECT32to16( &nc.rgrc[1], &nc16->rgrc[1] );
1404 RECT32to16( &nc.rgrc[2], &nc16->rgrc[2] );
1405 WINDOWPOS32to16( &winpos, MapSL(nc16->lppos) );
1408 break;
1409 case WM_COMPAREITEM:
1411 COMPAREITEMSTRUCT16* cis16 = MapSL(lParam);
1412 COMPAREITEMSTRUCT cis;
1413 cis.CtlType = cis16->CtlType;
1414 cis.CtlID = cis16->CtlID;
1415 cis.hwndItem = WIN_Handle32( cis16->hwndItem );
1416 cis.itemID1 = cis16->itemID1;
1417 cis.itemData1 = cis16->itemData1;
1418 cis.itemID2 = cis16->itemID2;
1419 cis.itemData2 = cis16->itemData2;
1420 cis.dwLocaleId = 0; /* FIXME */
1421 ret = callback( hwnd32, msg, wParam, (LPARAM)&cis, result, arg );
1423 break;
1424 case WM_DELETEITEM:
1426 DELETEITEMSTRUCT16* dis16 = MapSL(lParam);
1427 DELETEITEMSTRUCT dis;
1428 dis.CtlType = dis16->CtlType;
1429 dis.CtlID = dis16->CtlID;
1430 dis.hwndItem = WIN_Handle32( dis16->hwndItem );
1431 dis.itemData = dis16->itemData;
1432 ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1434 break;
1435 case WM_MEASUREITEM:
1437 MEASUREITEMSTRUCT16* mis16 = MapSL(lParam);
1438 MEASUREITEMSTRUCT mis;
1439 mis.CtlType = mis16->CtlType;
1440 mis.CtlID = mis16->CtlID;
1441 mis.itemID = mis16->itemID;
1442 mis.itemWidth = mis16->itemWidth;
1443 mis.itemHeight = mis16->itemHeight;
1444 mis.itemData = mis16->itemData;
1445 ret = callback( hwnd32, msg, wParam, (LPARAM)&mis, result, arg );
1446 mis16->itemWidth = (UINT16)mis.itemWidth;
1447 mis16->itemHeight = (UINT16)mis.itemHeight;
1449 break;
1450 case WM_DRAWITEM:
1452 DRAWITEMSTRUCT16* dis16 = MapSL(lParam);
1453 DRAWITEMSTRUCT dis;
1454 dis.CtlType = dis16->CtlType;
1455 dis.CtlID = dis16->CtlID;
1456 dis.itemID = dis16->itemID;
1457 dis.itemAction = dis16->itemAction;
1458 dis.itemState = dis16->itemState;
1459 dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1460 : WIN_Handle32( dis16->hwndItem );
1461 dis.hDC = HDC_32(dis16->hDC);
1462 dis.itemData = dis16->itemData;
1463 dis.rcItem.left = dis16->rcItem.left;
1464 dis.rcItem.top = dis16->rcItem.top;
1465 dis.rcItem.right = dis16->rcItem.right;
1466 dis.rcItem.bottom = dis16->rcItem.bottom;
1467 ret = callback( hwnd32, msg, wParam, (LPARAM)&dis, result, arg );
1469 break;
1470 case WM_COPYDATA:
1472 COPYDATASTRUCT16 *cds16 = MapSL(lParam);
1473 COPYDATASTRUCT cds;
1474 cds.dwData = cds16->dwData;
1475 cds.cbData = cds16->cbData;
1476 cds.lpData = MapSL(cds16->lpData);
1477 ret = callback( hwnd32, msg, wParam, (LPARAM)&cds, result, arg );
1479 break;
1480 case WM_GETDLGCODE:
1481 if (lParam)
1483 MSG16 *msg16 = MapSL(lParam);
1484 MSG msg32;
1485 msg32.hwnd = WIN_Handle32( msg16->hwnd );
1486 msg32.message = msg16->message;
1487 msg32.wParam = msg16->wParam;
1488 msg32.lParam = msg16->lParam;
1489 msg32.time = msg16->time;
1490 msg32.pt.x = msg16->pt.x;
1491 msg32.pt.y = msg16->pt.y;
1492 ret = callback( hwnd32, msg, wParam, (LPARAM)&msg32, result, arg );
1494 else
1495 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1496 break;
1497 case WM_NEXTMENU:
1499 MDINEXTMENU next;
1500 next.hmenuIn = (HMENU)lParam;
1501 next.hmenuNext = 0;
1502 next.hwndNext = 0;
1503 ret = callback( hwnd32, msg, wParam, (LPARAM)&next, result, arg );
1504 *result = MAKELONG( HMENU_16(next.hmenuNext), HWND_16(next.hwndNext) );
1506 break;
1507 case WM_ACTIVATE:
1508 case WM_CHARTOITEM:
1509 case WM_COMMAND:
1510 case WM_VKEYTOITEM:
1511 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1512 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1513 break;
1514 case WM_HSCROLL:
1515 case WM_VSCROLL:
1516 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1517 (LPARAM)WIN_Handle32( HIWORD(lParam) ), result, arg );
1518 break;
1519 case WM_CTLCOLOR:
1520 if (HIWORD(lParam) <= CTLCOLOR_STATIC)
1521 ret = callback( hwnd32, WM_CTLCOLORMSGBOX + HIWORD(lParam),
1522 (WPARAM)HDC_32(wParam), (LPARAM)WIN_Handle32( LOWORD(lParam) ),
1523 result, arg );
1524 break;
1525 case WM_GETTEXT:
1526 case WM_SETTEXT:
1527 case WM_WININICHANGE:
1528 case WM_DEVMODECHANGE:
1529 case WM_ASKCBFORMATNAME:
1530 case WM_NOTIFY:
1531 ret = callback( hwnd32, msg, wParam, (LPARAM)MapSL(lParam), result, arg );
1532 break;
1533 case WM_MENUCHAR:
1534 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1535 (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1536 break;
1537 case WM_MENUSELECT:
1538 if((LOWORD(lParam) & MF_POPUP) && (LOWORD(lParam) != 0xFFFF))
1540 HMENU hmenu = HMENU_32(HIWORD(lParam));
1541 UINT pos = MENU_FindSubMenu( &hmenu, HMENU_32(wParam) );
1542 if (pos == 0xffff) pos = 0; /* NO_SELECTED_ITEM */
1543 wParam = pos;
1545 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, LOWORD(lParam) ),
1546 (LPARAM)HMENU_32(HIWORD(lParam)), result, arg );
1547 break;
1548 case WM_PARENTNOTIFY:
1549 if ((wParam == WM_CREATE) || (wParam == WM_DESTROY))
1550 ret = callback( hwnd32, msg, MAKEWPARAM( wParam, HIWORD(lParam) ),
1551 (LPARAM)WIN_Handle32( LOWORD(lParam) ), result, arg );
1552 else
1553 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1554 break;
1555 case WM_ACTIVATEAPP:
1556 /* We need this when SetActiveWindow sends a Sendmessage16() to
1557 * a 32bit window. Might be superflous with 32bit interprocess
1558 * message queues. */
1559 if (lParam) lParam = HTASK_32(lParam);
1560 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1561 break;
1562 case WM_DDE_INITIATE:
1563 case WM_DDE_TERMINATE:
1564 case WM_DDE_UNADVISE:
1565 case WM_DDE_REQUEST:
1566 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1567 break;
1568 case WM_DDE_ADVISE:
1569 case WM_DDE_DATA:
1570 case WM_DDE_POKE:
1572 HANDLE16 lo16 = LOWORD(lParam);
1573 UINT_PTR lo32 = 0;
1574 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE))) break;
1575 lParam = PackDDElParam( msg, lo32, HIWORD(lParam) );
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_ACK:
1581 UINT_PTR lo = LOWORD(lParam);
1582 UINT_PTR hi = HIWORD(lParam);
1583 int flag = 0;
1584 char buf[2];
1586 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1587 if (GlobalSize16(hi) != 0) flag |= 2;
1588 switch (flag)
1590 case 0:
1591 if (hi)
1593 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1594 hi = 0;
1596 break;
1597 case 1:
1598 break; /* atom, nothing to do */
1599 case 3:
1600 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
1601 /* fall thru */
1602 case 2:
1603 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1604 break;
1606 lParam = PackDDElParam( WM_DDE_ACK, lo, hi );
1607 ret = callback( hwnd32, msg, (WPARAM)WIN_Handle32(wParam), lParam, result, arg );
1609 break; /* FIXME don't know how to free allocated memory (handle) !! */
1610 case WM_DDE_EXECUTE:
1611 lParam = convert_handle_16_to_32( lParam, GMEM_DDESHARE );
1612 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1613 break; /* FIXME don't know how to free allocated memory (handle) !! */
1614 case WM_PAINTCLIPBOARD:
1615 case WM_SIZECLIPBOARD:
1616 FIXME_(msg)( "message %04x needs translation\n", msg );
1617 break;
1618 default:
1619 ret = callback( hwnd32, msg, wParam, lParam, result, arg );
1620 break;
1622 return ret;
1626 /**********************************************************************
1627 * __wine_call_wndproc (USER.1010)
1629 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
1630 WINDOWPROC *proc )
1632 LRESULT result;
1634 if (proc->procA)
1635 WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1636 else
1637 WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
1638 return result;
1642 /**********************************************************************
1643 * WINPROC_CallProc32ATo16
1645 * Call a 16-bit window procedure, translating the 32-bit args.
1647 LRESULT WINPROC_CallProc32ATo16( winproc_callback16_t callback, HWND hwnd, UINT msg,
1648 WPARAM wParam, LPARAM lParam, LRESULT *result, void *arg )
1650 LRESULT ret = 0;
1652 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
1653 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
1655 switch(msg)
1657 case WM_NCCREATE:
1658 case WM_CREATE:
1660 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)lParam;
1661 CREATESTRUCT16 cs;
1662 MDICREATESTRUCT16 mdi_cs16;
1663 BOOL mdi_child = (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD);
1665 CREATESTRUCT32Ato16( cs32, &cs );
1666 cs.lpszName = MapLS( cs32->lpszName );
1667 cs.lpszClass = MapLS( cs32->lpszClass );
1669 if (mdi_child)
1671 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
1672 MDICREATESTRUCT32Ato16( mdi_cs, &mdi_cs16 );
1673 mdi_cs16.szTitle = MapLS( mdi_cs->szTitle );
1674 mdi_cs16.szClass = MapLS( mdi_cs->szClass );
1675 cs.lpCreateParams = MapLS( &mdi_cs16 );
1677 lParam = MapLS( &cs );
1678 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1679 UnMapLS( lParam );
1680 UnMapLS( cs.lpszName );
1681 UnMapLS( cs.lpszClass );
1682 if (mdi_child)
1684 UnMapLS( cs.lpCreateParams );
1685 UnMapLS( mdi_cs16.szTitle );
1686 UnMapLS( mdi_cs16.szClass );
1689 break;
1690 case WM_MDICREATE:
1692 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)lParam;
1693 MDICREATESTRUCT16 cs;
1695 MDICREATESTRUCT32Ato16( cs32, &cs );
1696 cs.szTitle = MapLS( cs32->szTitle );
1697 cs.szClass = MapLS( cs32->szClass );
1698 lParam = MapLS( &cs );
1699 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1700 UnMapLS( lParam );
1701 UnMapLS( cs.szTitle );
1702 UnMapLS( cs.szClass );
1704 break;
1705 case WM_MDIACTIVATE:
1706 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
1707 ret = callback( HWND_16(hwnd), msg, ((HWND)lParam == hwnd),
1708 MAKELPARAM( LOWORD(lParam), LOWORD(wParam) ), result, arg );
1709 else
1710 ret = callback( HWND_16(hwnd), msg, HWND_16( (HWND)wParam ), 0, result, arg );
1711 break;
1712 case WM_MDIGETACTIVE:
1713 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1714 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(*result);
1715 *result = (LRESULT)WIN_Handle32( LOWORD(*result) );
1716 break;
1717 case WM_MDISETMENU:
1718 ret = callback( HWND_16(hwnd), msg, (lParam == 0),
1719 MAKELPARAM( LOWORD(wParam), LOWORD(lParam) ), result, arg );
1720 break;
1721 case WM_GETMINMAXINFO:
1723 MINMAXINFO *mmi32 = (MINMAXINFO *)lParam;
1724 MINMAXINFO16 mmi;
1726 MINMAXINFO32to16( mmi32, &mmi );
1727 lParam = MapLS( &mmi );
1728 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1729 UnMapLS( lParam );
1730 MINMAXINFO16to32( &mmi, mmi32 );
1732 break;
1733 case WM_NCCALCSIZE:
1735 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)lParam;
1736 NCCALCSIZE_PARAMS16 nc;
1737 WINDOWPOS16 winpos;
1739 RECT32to16( &nc32->rgrc[0], &nc.rgrc[0] );
1740 if (wParam)
1742 RECT32to16( &nc32->rgrc[1], &nc.rgrc[1] );
1743 RECT32to16( &nc32->rgrc[2], &nc.rgrc[2] );
1744 WINDOWPOS32to16( nc32->lppos, &winpos );
1745 nc.lppos = MapLS( &winpos );
1747 lParam = MapLS( &nc );
1748 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1749 UnMapLS( lParam );
1750 RECT16to32( &nc.rgrc[0], &nc32->rgrc[0] );
1751 if (wParam)
1753 RECT16to32( &nc.rgrc[1], &nc32->rgrc[1] );
1754 RECT16to32( &nc.rgrc[2], &nc32->rgrc[2] );
1755 WINDOWPOS16to32( &winpos, nc32->lppos );
1756 UnMapLS( nc.lppos );
1759 break;
1760 case WM_WINDOWPOSCHANGING:
1761 case WM_WINDOWPOSCHANGED:
1763 WINDOWPOS *winpos32 = (WINDOWPOS *)lParam;
1764 WINDOWPOS16 winpos;
1766 WINDOWPOS32to16( winpos32, &winpos );
1767 lParam = MapLS( &winpos );
1768 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1769 UnMapLS( lParam );
1770 WINDOWPOS16to32( &winpos, winpos32 );
1772 break;
1773 case WM_COMPAREITEM:
1775 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)lParam;
1776 COMPAREITEMSTRUCT16 cis;
1777 cis.CtlType = cis32->CtlType;
1778 cis.CtlID = cis32->CtlID;
1779 cis.hwndItem = HWND_16( cis32->hwndItem );
1780 cis.itemID1 = cis32->itemID1;
1781 cis.itemData1 = cis32->itemData1;
1782 cis.itemID2 = cis32->itemID2;
1783 cis.itemData2 = cis32->itemData2;
1784 lParam = MapLS( &cis );
1785 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1786 UnMapLS( lParam );
1788 break;
1789 case WM_DELETEITEM:
1791 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)lParam;
1792 DELETEITEMSTRUCT16 dis;
1793 dis.CtlType = dis32->CtlType;
1794 dis.CtlID = dis32->CtlID;
1795 dis.itemID = dis32->itemID;
1796 dis.hwndItem = (dis.CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
1797 : HWND_16( dis32->hwndItem );
1798 dis.itemData = dis32->itemData;
1799 lParam = MapLS( &dis );
1800 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1801 UnMapLS( lParam );
1803 break;
1804 case WM_DRAWITEM:
1806 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)lParam;
1807 DRAWITEMSTRUCT16 dis;
1808 dis.CtlType = dis32->CtlType;
1809 dis.CtlID = dis32->CtlID;
1810 dis.itemID = dis32->itemID;
1811 dis.itemAction = dis32->itemAction;
1812 dis.itemState = dis32->itemState;
1813 dis.hwndItem = HWND_16( dis32->hwndItem );
1814 dis.hDC = HDC_16(dis32->hDC);
1815 dis.itemData = dis32->itemData;
1816 dis.rcItem.left = dis32->rcItem.left;
1817 dis.rcItem.top = dis32->rcItem.top;
1818 dis.rcItem.right = dis32->rcItem.right;
1819 dis.rcItem.bottom = dis32->rcItem.bottom;
1820 lParam = MapLS( &dis );
1821 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1822 UnMapLS( lParam );
1824 break;
1825 case WM_MEASUREITEM:
1827 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)lParam;
1828 MEASUREITEMSTRUCT16 mis;
1829 mis.CtlType = mis32->CtlType;
1830 mis.CtlID = mis32->CtlID;
1831 mis.itemID = mis32->itemID;
1832 mis.itemWidth = mis32->itemWidth;
1833 mis.itemHeight = mis32->itemHeight;
1834 mis.itemData = mis32->itemData;
1835 lParam = MapLS( &mis );
1836 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1837 UnMapLS( lParam );
1838 mis32->itemWidth = mis.itemWidth;
1839 mis32->itemHeight = mis.itemHeight;
1841 break;
1842 case WM_COPYDATA:
1844 COPYDATASTRUCT *cds32 = (COPYDATASTRUCT *)lParam;
1845 COPYDATASTRUCT16 cds;
1847 cds.dwData = cds32->dwData;
1848 cds.cbData = cds32->cbData;
1849 cds.lpData = MapLS( cds32->lpData );
1850 lParam = MapLS( &cds );
1851 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1852 UnMapLS( lParam );
1853 UnMapLS( cds.lpData );
1855 break;
1856 case WM_GETDLGCODE:
1857 if (lParam)
1859 MSG *msg32 = (MSG *)lParam;
1860 MSG16 msg16;
1862 msg16.hwnd = HWND_16( msg32->hwnd );
1863 msg16.message = msg32->message;
1864 msg16.wParam = msg32->wParam;
1865 msg16.lParam = msg32->lParam;
1866 msg16.time = msg32->time;
1867 msg16.pt.x = msg32->pt.x;
1868 msg16.pt.y = msg32->pt.y;
1869 lParam = MapLS( &msg16 );
1870 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1871 UnMapLS( lParam );
1873 else
1874 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1875 break;
1876 case WM_NEXTMENU:
1878 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1879 ret = callback( HWND_16(hwnd), msg, wParam, (LPARAM)next->hmenuIn, result, arg );
1880 next->hmenuNext = HMENU_32( LOWORD(*result) );
1881 next->hwndNext = WIN_Handle32( HIWORD(*result) );
1882 *result = 0;
1884 break;
1885 case WM_GETTEXT:
1886 case WM_ASKCBFORMATNAME:
1887 wParam = min( wParam, 0xff80 ); /* Must be < 64K */
1888 /* fall through */
1889 case WM_NOTIFY:
1890 case WM_SETTEXT:
1891 case WM_WININICHANGE:
1892 case WM_DEVMODECHANGE:
1893 lParam = MapLS( (void *)lParam );
1894 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1895 UnMapLS( lParam );
1896 break;
1897 case WM_ACTIVATE:
1898 case WM_CHARTOITEM:
1899 case WM_COMMAND:
1900 case WM_VKEYTOITEM:
1901 ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ),
1902 result, arg );
1903 break;
1904 case WM_HSCROLL:
1905 case WM_VSCROLL:
1906 ret = callback( HWND_16(hwnd), msg, wParam, MAKELPARAM( HIWORD(wParam), (HWND16)lParam ),
1907 result, arg );
1908 break;
1909 case WM_CTLCOLORMSGBOX:
1910 case WM_CTLCOLOREDIT:
1911 case WM_CTLCOLORLISTBOX:
1912 case WM_CTLCOLORBTN:
1913 case WM_CTLCOLORDLG:
1914 case WM_CTLCOLORSCROLLBAR:
1915 case WM_CTLCOLORSTATIC:
1916 ret = callback( HWND_16(hwnd), WM_CTLCOLOR, wParam,
1917 MAKELPARAM( (HWND16)lParam, msg - WM_CTLCOLORMSGBOX ), result, arg );
1918 break;
1919 case WM_MENUSELECT:
1920 if(HIWORD(wParam) & MF_POPUP)
1922 HMENU hmenu;
1923 if ((HIWORD(wParam) != 0xffff) || lParam)
1925 if ((hmenu = GetSubMenu( (HMENU)lParam, LOWORD(wParam) )))
1927 ret = callback( HWND_16(hwnd), msg, HMENU_16(hmenu),
1928 MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1929 break;
1933 /* fall through */
1934 case WM_MENUCHAR:
1935 ret = callback( HWND_16(hwnd), msg, wParam,
1936 MAKELPARAM( HIWORD(wParam), (HMENU16)lParam ), result, arg );
1937 break;
1938 case WM_PARENTNOTIFY:
1939 if ((LOWORD(wParam) == WM_CREATE) || (LOWORD(wParam) == WM_DESTROY))
1940 ret = callback( HWND_16(hwnd), msg, wParam,
1941 MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ), result, arg );
1942 else
1943 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1944 break;
1945 case WM_ACTIVATEAPP:
1946 ret = callback( HWND_16(hwnd), msg, wParam, HTASK_16( (HANDLE)lParam ), result, arg );
1947 break;
1948 case WM_PAINT:
1949 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
1950 ret = callback( HWND_16(hwnd), WM_PAINTICON, 1, lParam, result, arg );
1951 else
1952 ret = callback( HWND_16(hwnd), WM_PAINT, wParam, lParam, result, arg );
1953 break;
1954 case WM_ERASEBKGND:
1955 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON )) msg = WM_ICONERASEBKGND;
1956 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
1957 break;
1958 case WM_DDE_INITIATE:
1959 case WM_DDE_TERMINATE:
1960 case WM_DDE_UNADVISE:
1961 case WM_DDE_REQUEST:
1962 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam), lParam, result, arg );
1963 break;
1964 case WM_DDE_ADVISE:
1965 case WM_DDE_DATA:
1966 case WM_DDE_POKE:
1968 UINT_PTR lo32, hi;
1969 HANDLE16 lo16 = 0;
1971 UnpackDDElParam( msg, lParam, &lo32, &hi );
1972 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE))) break;
1973 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam),
1974 MAKELPARAM(lo16, hi), result, arg );
1976 break; /* FIXME don't know how to free allocated memory (handle) !! */
1977 case WM_DDE_ACK:
1979 UINT_PTR lo, hi;
1980 int flag = 0;
1981 char buf[2];
1983 UnpackDDElParam( msg, lParam, &lo, &hi );
1985 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
1986 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
1987 switch (flag)
1989 case 0:
1990 if (hi)
1992 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1993 hi = 0;
1995 break;
1996 case 1:
1997 break; /* atom, nothing to do */
1998 case 3:
1999 MESSAGE("DDE_ACK: %lx both atom and handle... choosing handle\n", hi);
2000 /* fall thru */
2001 case 2:
2002 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2003 break;
2005 ret = callback( HWND_16(hwnd), msg, HWND_16((HWND)wParam),
2006 MAKELPARAM(lo, hi), result, arg );
2008 break; /* FIXME don't know how to free allocated memory (handle) !! */
2009 case WM_DDE_EXECUTE:
2010 lParam = convert_handle_32_to_16(lParam, GMEM_DDESHARE);
2011 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
2012 break; /* FIXME don't know how to free allocated memory (handle) !! */
2013 case SBM_SETRANGE:
2014 ret = callback( HWND_16(hwnd), SBM_SETRANGE16, 0, MAKELPARAM(wParam, lParam), result, arg );
2015 break;
2016 case SBM_GETRANGE:
2017 ret = callback( HWND_16(hwnd), SBM_GETRANGE16, wParam, lParam, result, arg );
2018 *(LPINT)wParam = LOWORD(*result);
2019 *(LPINT)lParam = HIWORD(*result);
2020 break;
2021 case BM_GETCHECK:
2022 case BM_SETCHECK:
2023 case BM_GETSTATE:
2024 case BM_SETSTATE:
2025 case BM_SETSTYLE:
2026 ret = callback( HWND_16(hwnd), msg + BM_GETCHECK16 - BM_GETCHECK, wParam, lParam, result, arg );
2027 break;
2028 case EM_GETSEL:
2029 case EM_GETRECT:
2030 case EM_SETRECT:
2031 case EM_SETRECTNP:
2032 case EM_SCROLL:
2033 case EM_LINESCROLL:
2034 case EM_SCROLLCARET:
2035 case EM_GETMODIFY:
2036 case EM_SETMODIFY:
2037 case EM_GETLINECOUNT:
2038 case EM_LINEINDEX:
2039 case EM_SETHANDLE:
2040 case EM_GETHANDLE:
2041 case EM_GETTHUMB:
2042 case EM_LINELENGTH:
2043 case EM_REPLACESEL:
2044 case EM_GETLINE:
2045 case EM_LIMITTEXT:
2046 case EM_CANUNDO:
2047 case EM_UNDO:
2048 case EM_FMTLINES:
2049 case EM_LINEFROMCHAR:
2050 case EM_SETTABSTOPS:
2051 case EM_SETPASSWORDCHAR:
2052 case EM_EMPTYUNDOBUFFER:
2053 case EM_GETFIRSTVISIBLELINE:
2054 case EM_SETREADONLY:
2055 case EM_SETWORDBREAKPROC:
2056 case EM_GETWORDBREAKPROC:
2057 case EM_GETPASSWORDCHAR:
2058 ret = callback( HWND_16(hwnd), msg + EM_GETSEL16 - EM_GETSEL, wParam, lParam, result, arg );
2059 break;
2060 case EM_SETSEL:
2061 ret = callback( HWND_16(hwnd), EM_SETSEL16, 0, MAKELPARAM( wParam, lParam ), result, arg );
2062 break;
2063 case LB_CARETOFF:
2064 case LB_CARETON:
2065 case LB_DELETESTRING:
2066 case LB_GETANCHORINDEX:
2067 case LB_GETCARETINDEX:
2068 case LB_GETCOUNT:
2069 case LB_GETCURSEL:
2070 case LB_GETHORIZONTALEXTENT:
2071 case LB_GETITEMDATA:
2072 case LB_GETITEMHEIGHT:
2073 case LB_GETSEL:
2074 case LB_GETSELCOUNT:
2075 case LB_GETTEXTLEN:
2076 case LB_GETTOPINDEX:
2077 case LB_RESETCONTENT:
2078 case LB_SELITEMRANGE:
2079 case LB_SELITEMRANGEEX:
2080 case LB_SETANCHORINDEX:
2081 case LB_SETCARETINDEX:
2082 case LB_SETCOLUMNWIDTH:
2083 case LB_SETCURSEL:
2084 case LB_SETHORIZONTALEXTENT:
2085 case LB_SETITEMDATA:
2086 case LB_SETITEMHEIGHT:
2087 case LB_SETSEL:
2088 case LB_SETTOPINDEX:
2089 ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2090 break;
2091 case LB_ADDSTRING:
2092 case LB_FINDSTRING:
2093 case LB_FINDSTRINGEXACT:
2094 case LB_INSERTSTRING:
2095 case LB_SELECTSTRING:
2096 case LB_GETTEXT:
2097 case LB_DIR:
2098 case LB_ADDFILE:
2099 lParam = MapLS( (LPSTR)lParam );
2100 ret = callback( HWND_16(hwnd), msg + LB_ADDSTRING16 - LB_ADDSTRING, wParam, lParam, result, arg );
2101 UnMapLS( lParam );
2102 break;
2103 case LB_GETSELITEMS:
2105 INT *items32 = (INT *)lParam;
2106 INT16 *items, buffer[512];
2107 unsigned int i;
2109 wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2110 if (!(items = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2111 lParam = MapLS( items );
2112 ret = callback( HWND_16(hwnd), LB_GETSELITEMS16, wParam, lParam, result, arg );
2113 UnMapLS( lParam );
2114 for (i = 0; i < wParam; i++) items32[i] = items[i];
2115 free_buffer( buffer, items );
2117 break;
2118 case LB_SETTABSTOPS:
2119 if (wParam)
2121 INT *stops32 = (INT *)lParam;
2122 INT16 *stops, buffer[512];
2123 unsigned int i;
2125 wParam = min( wParam, 0x7f80 ); /* Must be < 64K */
2126 if (!(stops = get_buffer( buffer, sizeof(buffer), wParam * sizeof(INT16) ))) break;
2127 for (i = 0; i < wParam; i++) stops[i] = stops32[i];
2128 lParam = MapLS( stops );
2129 ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2130 UnMapLS( lParam );
2131 free_buffer( buffer, stops );
2133 else ret = callback( HWND_16(hwnd), LB_SETTABSTOPS16, wParam, lParam, result, arg );
2134 break;
2135 case CB_DELETESTRING:
2136 case CB_GETCOUNT:
2137 case CB_GETLBTEXTLEN:
2138 case CB_LIMITTEXT:
2139 case CB_RESETCONTENT:
2140 case CB_SETEDITSEL:
2141 case CB_GETCURSEL:
2142 case CB_SETCURSEL:
2143 case CB_SHOWDROPDOWN:
2144 case CB_SETITEMDATA:
2145 case CB_SETITEMHEIGHT:
2146 case CB_GETITEMHEIGHT:
2147 case CB_SETEXTENDEDUI:
2148 case CB_GETEXTENDEDUI:
2149 case CB_GETDROPPEDSTATE:
2150 ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2151 break;
2152 case CB_GETEDITSEL:
2153 ret = callback( HWND_16(hwnd), CB_GETEDITSEL16, wParam, lParam, result, arg );
2154 if (wParam) *((PUINT)(wParam)) = LOWORD(*result);
2155 if (lParam) *((PUINT)(lParam)) = HIWORD(*result); /* FIXME: substract 1? */
2156 break;
2157 case CB_ADDSTRING:
2158 case CB_FINDSTRING:
2159 case CB_FINDSTRINGEXACT:
2160 case CB_INSERTSTRING:
2161 case CB_SELECTSTRING:
2162 case CB_DIR:
2163 case CB_GETLBTEXT:
2164 lParam = MapLS( (LPSTR)lParam );
2165 ret = callback( HWND_16(hwnd), msg + CB_GETEDITSEL16 - CB_GETEDITSEL, wParam, lParam, result, arg );
2166 UnMapLS( lParam );
2167 break;
2168 case LB_GETITEMRECT:
2169 case CB_GETDROPPEDCONTROLRECT:
2171 RECT *r32 = (RECT *)lParam;
2172 RECT16 rect;
2173 lParam = MapLS( &rect );
2174 ret = callback( HWND_16(hwnd),
2175 (msg == LB_GETITEMRECT) ? LB_GETITEMRECT16 : CB_GETDROPPEDCONTROLRECT16,
2176 wParam, lParam, result, arg );
2177 UnMapLS( lParam );
2178 RECT16to32( &rect, r32 );
2180 break;
2181 case WM_PAINTCLIPBOARD:
2182 case WM_SIZECLIPBOARD:
2183 FIXME_(msg)( "message %04x needs translation\n", msg );
2184 break;
2185 /* the following messages should not be sent to 16-bit apps */
2186 case WM_SIZING:
2187 case WM_MOVING:
2188 case WM_CAPTURECHANGED:
2189 case WM_STYLECHANGING:
2190 case WM_STYLECHANGED:
2191 break;
2192 default:
2193 ret = callback( HWND_16(hwnd), msg, wParam, lParam, result, arg );
2194 break;
2196 return ret;
2200 /**********************************************************************
2201 * CallWindowProc (USER.122)
2203 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
2204 WPARAM16 wParam, LPARAM lParam )
2206 WINDOWPROC *proc;
2207 LRESULT result;
2209 if (!func) return 0;
2211 if (!(proc = handle16_to_proc( func )))
2212 call_window_proc16( hwnd, msg, wParam, lParam, &result, func );
2213 else if (proc->procA)
2214 WINPROC_CallProc16To32A( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2215 else if (proc->procW)
2216 WINPROC_CallProc16To32A( call_window_proc_AtoW, hwnd, msg, wParam, lParam, &result, proc->procW );
2217 else
2218 call_window_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2220 return result;
2224 /**********************************************************************
2225 * CallWindowProcA (USER32.@)
2227 * The CallWindowProc() function invokes the windows procedure _func_,
2228 * with _hwnd_ as the target window, the message specified by _msg_, and
2229 * the message parameters _wParam_ and _lParam_.
2231 * Some kinds of argument conversion may be done, I'm not sure what.
2233 * CallWindowProc() may be used for windows subclassing. Use
2234 * SetWindowLong() to set a new windows procedure for windows of the
2235 * subclass, and handle subclassed messages in the new windows
2236 * procedure. The new windows procedure may then use CallWindowProc()
2237 * with _func_ set to the parent class's windows procedure to dispatch
2238 * the message to the superclass.
2240 * RETURNS
2242 * The return value is message dependent.
2244 * CONFORMANCE
2246 * ECMA-234, Win32
2248 LRESULT WINAPI CallWindowProcA(
2249 WNDPROC func, /* [in] window procedure */
2250 HWND hwnd, /* [in] target window */
2251 UINT msg, /* [in] message */
2252 WPARAM wParam, /* [in] message dependent parameter */
2253 LPARAM lParam /* [in] message dependent parameter */
2255 WINDOWPROC *proc;
2256 LRESULT result;
2258 if (!func) return 0;
2260 if (!(proc = handle_to_proc( func )) && !(proc = find_builtin_proc( func )))
2261 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2262 else if (proc->procA)
2263 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2264 else if (proc->procW)
2265 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procW );
2266 else
2267 WINPROC_CallProc32ATo16( call_window_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2268 return result;
2272 /**********************************************************************
2273 * CallWindowProcW (USER32.@)
2275 * See CallWindowProcA.
2277 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
2278 WPARAM wParam, LPARAM lParam )
2280 WINDOWPROC *proc;
2281 LRESULT result;
2283 if (!func) return 0;
2285 if (!(proc = handle_to_proc( func )) && !(proc = find_builtin_proc( func )))
2286 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
2287 else if (proc->procW)
2288 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2289 else if (proc->procA)
2290 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2291 else
2292 WINPROC_CallProcWtoA( call_window_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2293 return result;
2297 /**********************************************************************
2298 * WINPROC_CallDlgProc16
2300 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
2302 WINDOWPROC *proc;
2303 LRESULT result;
2304 INT_PTR ret;
2306 if (!func) return 0;
2308 if (!(proc = handle16_to_proc( (WNDPROC16)func )))
2310 ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, func );
2312 else if (proc->procA)
2314 ret = WINPROC_CallProc16To32A( call_dialog_proc, hwnd, msg, wParam, lParam,
2315 &result, proc->procA );
2316 SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2318 else if (proc->procW)
2320 ret = WINPROC_CallProc16To32A( call_dialog_proc_AtoW, hwnd, msg, wParam, lParam,
2321 &result, proc->procW );
2322 SetWindowLongPtrW( WIN_Handle32(hwnd), DWLP_MSGRESULT, result );
2324 else
2326 ret = call_dialog_proc16( hwnd, msg, wParam, lParam, &result, proc->proc16 );
2328 return ret;
2332 /**********************************************************************
2333 * WINPROC_CallDlgProcA
2335 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2337 WINDOWPROC *proc;
2338 LRESULT result;
2339 INT_PTR ret;
2341 if (!func) return 0;
2343 if (!(proc = handle_to_proc( func )) && !(proc = find_builtin_proc( func )))
2344 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2345 else if (proc->procA)
2346 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
2347 else if (proc->procW)
2349 ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procW );
2350 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2352 else
2354 ret = WINPROC_CallProc32ATo16( call_dialog_proc16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2355 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2357 return ret;
2361 /**********************************************************************
2362 * WINPROC_CallDlgProcW
2364 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
2366 WINDOWPROC *proc;
2367 LRESULT result;
2368 INT_PTR ret;
2370 if (!func) return 0;
2372 if (!(proc = handle_to_proc( func )) && !(proc = find_builtin_proc( func )))
2373 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
2374 else if (proc->procW)
2375 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
2376 else if (proc->procA)
2378 ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
2379 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2381 else
2383 ret = WINPROC_CallProcWtoA( call_dialog_proc_Ato16, hwnd, msg, wParam, lParam, &result, proc->proc16 );
2384 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2386 return ret;