user: Simplify 16-bit mappings of the WM_GETDLGCODE message.
[wine/dcerpc.git] / dlls / user / winproc.c
blob26c84fe69f4139b6643d1e7245048e04611d08eb
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 /* find an existing winproc for a given 16-bit function and type */
71 /* FIXME: probably should do something more clever than a linear search */
72 static inline WINDOWPROC *find_winproc16( WNDPROC16 func )
74 unsigned int i;
76 for (i = 0; i < winproc_used; i++)
78 if (winproc_array[i].proc16 == func) return &winproc_array[i];
80 return NULL;
83 /* find an existing winproc for a given function and type */
84 /* FIXME: probably should do something more clever than a linear search */
85 static inline WINDOWPROC *find_winproc( WNDPROC funcA, WNDPROC funcW )
87 unsigned int i;
89 for (i = 0; i < winproc_used; i++)
91 if (funcA && winproc_array[i].procA != funcA) continue;
92 if (funcW && winproc_array[i].procW != funcW) continue;
93 return &winproc_array[i];
95 return NULL;
98 /* return the window proc for a given handle, or NULL for an invalid handle */
99 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
101 UINT index = LOWORD(handle);
102 if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
103 if (index >= winproc_used) return NULL;
104 return &winproc_array[index];
107 /* create a handle for a given window proc */
108 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
110 return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
113 /* allocate and initialize a new winproc */
114 static inline WINDOWPROC *alloc_winproc( WNDPROC funcA, WNDPROC funcW )
116 WINDOWPROC *proc;
118 /* check if the function is already a win proc */
119 if (funcA && (proc = handle_to_proc( funcA ))) return proc;
120 if (funcW && (proc = handle_to_proc( funcW ))) return proc;
121 if (!funcA && !funcW) return NULL;
123 EnterCriticalSection( &winproc_cs );
125 /* check if we already have a winproc for that function */
126 if (!(proc = find_winproc( funcA, funcW )))
128 if (winproc_used < MAX_WINPROCS)
130 proc = &winproc_array[winproc_used++];
131 proc->procA = funcA;
132 proc->procW = funcW;
133 TRACE( "allocated %p for %p/%p (%d/%d used)\n",
134 proc_to_handle(proc), funcA, funcW, winproc_used, MAX_WINPROCS );
136 else FIXME( "too many winprocs, cannot allocate one for %p/%p\n", funcA, funcW );
138 else TRACE( "reusing %p for %p/%p\n", proc_to_handle(proc), funcA, funcW );
140 LeaveCriticalSection( &winproc_cs );
141 return proc;
145 #ifdef __i386__
147 #include "pshpack1.h"
149 /* Window procedure 16-to-32-bit thunk */
150 typedef struct
152 BYTE popl_eax; /* popl %eax (return address) */
153 BYTE pushl_func; /* pushl $proc */
154 WINDOWPROC *proc;
155 BYTE pushl_eax; /* pushl %eax */
156 BYTE ljmp; /* ljmp relay*/
157 DWORD relay_offset; /* __wine_call_wndproc */
158 WORD relay_sel;
159 } WINPROC_THUNK;
161 #include "poppack.h"
163 #define MAX_THUNKS (0x10000 / sizeof(WINPROC_THUNK))
165 static WINPROC_THUNK *thunk_array;
166 static UINT thunk_selector;
167 static UINT thunk_used;
169 /* return the window proc for a given handle, or NULL for an invalid handle */
170 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
172 if (HIWORD(handle) == thunk_selector)
174 UINT index = LOWORD(handle) / sizeof(WINPROC_THUNK);
175 /* check alignment */
176 if (index * sizeof(WINPROC_THUNK) != LOWORD(handle)) return NULL;
177 /* check array limits */
178 if (index >= thunk_used) return NULL;
179 return thunk_array[index].proc;
181 return handle_to_proc( (WNDPROC)handle );
184 /* allocate a 16-bit thunk for an existing window proc */
185 static WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
187 static FARPROC16 relay;
188 UINT i;
190 if (proc->proc16) return proc->proc16;
192 EnterCriticalSection( &winproc_cs );
194 if (!thunk_array) /* allocate the array and its selector */
196 LDT_ENTRY entry;
198 if (!(thunk_selector = wine_ldt_alloc_entries(1))) goto done;
199 if (!(thunk_array = VirtualAlloc( NULL, MAX_THUNKS * sizeof(WINPROC_THUNK), MEM_COMMIT,
200 PAGE_EXECUTE_READWRITE ))) goto done;
201 wine_ldt_set_base( &entry, thunk_array );
202 wine_ldt_set_limit( &entry, MAX_THUNKS * sizeof(WINPROC_THUNK) - 1 );
203 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
204 wine_ldt_set_entry( thunk_selector, &entry );
205 relay = GetProcAddress16( GetModuleHandle16("user"), "__wine_call_wndproc" );
208 /* check if it already exists */
209 for (i = 0; i < thunk_used; i++) if (thunk_array[i].proc == proc) break;
211 if (i == thunk_used) /* create a new one */
213 WINPROC_THUNK *thunk;
215 if (thunk_used >= MAX_THUNKS) goto done;
216 thunk = &thunk_array[thunk_used++];
217 thunk->popl_eax = 0x58; /* popl %eax */
218 thunk->pushl_func = 0x68; /* pushl $proc */
219 thunk->proc = proc;
220 thunk->pushl_eax = 0x50; /* pushl %eax */
221 thunk->ljmp = 0xea; /* ljmp relay*/
222 thunk->relay_offset = OFFSETOF(relay);
223 thunk->relay_sel = SELECTOROF(relay);
225 proc->proc16 = (WNDPROC16)MAKESEGPTR( thunk_selector, i * sizeof(WINPROC_THUNK) );
226 done:
227 LeaveCriticalSection( &winproc_cs );
228 return proc->proc16;
231 #else /* __i386__ */
233 static inline WINDOWPROC *handle16_to_proc( WNDPROC16 handle )
235 return handle_to_proc( (WNDPROC)handle );
238 static inline WNDPROC16 alloc_win16_thunk( WINDOWPROC *proc )
240 return 0;
243 #endif /* __i386__ */
246 #ifdef __i386__
247 /* Some window procedures modify register they shouldn't, or are not
248 * properly declared stdcall; so we need a small assembly wrapper to
249 * call them. */
250 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
251 WPARAM wParam, LPARAM lParam );
252 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
253 "pushl %ebp\n\t"
254 "movl %esp,%ebp\n\t"
255 "pushl %edi\n\t"
256 "pushl %esi\n\t"
257 "pushl %ebx\n\t"
258 "subl $12,%esp\n\t"
259 "pushl 24(%ebp)\n\t"
260 "pushl 20(%ebp)\n\t"
261 "pushl 16(%ebp)\n\t"
262 "pushl 12(%ebp)\n\t"
263 "movl 8(%ebp),%eax\n\t"
264 "call *%eax\n\t"
265 "leal -12(%ebp),%esp\n\t"
266 "popl %ebx\n\t"
267 "popl %esi\n\t"
268 "popl %edi\n\t"
269 "leave\n\t"
270 "ret" );
271 #else
272 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
273 WPARAM wParam, LPARAM lParam )
275 return proc( hwnd, msg, wParam, lParam );
277 #endif /* __i386__ */
280 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
282 to->ptReserved.x = from->ptReserved.x;
283 to->ptReserved.y = from->ptReserved.y;
284 to->ptMaxSize.x = from->ptMaxSize.x;
285 to->ptMaxSize.y = from->ptMaxSize.y;
286 to->ptMaxPosition.x = from->ptMaxPosition.x;
287 to->ptMaxPosition.y = from->ptMaxPosition.y;
288 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
289 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
290 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
291 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
294 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
296 to->ptReserved.x = from->ptReserved.x;
297 to->ptReserved.y = from->ptReserved.y;
298 to->ptMaxSize.x = from->ptMaxSize.x;
299 to->ptMaxSize.y = from->ptMaxSize.y;
300 to->ptMaxPosition.x = from->ptMaxPosition.x;
301 to->ptMaxPosition.y = from->ptMaxPosition.y;
302 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
303 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
304 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
305 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
308 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
310 to->hwnd = HWND_16(from->hwnd);
311 to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
312 to->x = from->x;
313 to->y = from->y;
314 to->cx = from->cx;
315 to->cy = from->cy;
316 to->flags = from->flags;
319 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
321 to->hwnd = WIN_Handle32(from->hwnd);
322 to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
323 HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
324 to->x = from->x;
325 to->y = from->y;
326 to->cx = from->cx;
327 to->cy = from->cy;
328 to->flags = from->flags;
331 /* The strings are not copied */
332 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
334 to->lpCreateParams = (SEGPTR)from->lpCreateParams;
335 to->hInstance = HINSTANCE_16(from->hInstance);
336 to->hMenu = HMENU_16(from->hMenu);
337 to->hwndParent = HWND_16(from->hwndParent);
338 to->cy = from->cy;
339 to->cx = from->cx;
340 to->y = from->y;
341 to->x = from->x;
342 to->style = from->style;
343 to->dwExStyle = from->dwExStyle;
346 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
349 to->lpCreateParams = (LPVOID)from->lpCreateParams;
350 to->hInstance = HINSTANCE_32(from->hInstance);
351 to->hMenu = HMENU_32(from->hMenu);
352 to->hwndParent = WIN_Handle32(from->hwndParent);
353 to->cy = from->cy;
354 to->cx = from->cx;
355 to->y = from->y;
356 to->x = from->x;
357 to->style = from->style;
358 to->dwExStyle = from->dwExStyle;
361 /* The strings are not copied */
362 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
364 to->hOwner = HINSTANCE_16(from->hOwner);
365 to->x = from->x;
366 to->y = from->y;
367 to->cx = from->cx;
368 to->cy = from->cy;
369 to->style = from->style;
370 to->lParam = from->lParam;
373 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
375 to->hOwner = HINSTANCE_32(from->hOwner);
376 to->x = from->x;
377 to->y = from->y;
378 to->cx = from->cx;
379 to->cy = from->cy;
380 to->style = from->style;
381 to->lParam = from->lParam;
384 /**********************************************************************
385 * WINPROC_CallWndProc32
387 * Call a 32-bit WndProc.
389 static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
390 WPARAM wParam, LPARAM lParam )
392 LRESULT retvalue;
394 USER_CheckNotLock();
396 hwnd = WIN_GetFullHandle( hwnd );
397 if (TRACE_ON(relay))
398 DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
399 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
401 retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
403 if (TRACE_ON(relay))
404 DPRINTF( "%04lx:Ret window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
405 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
406 return retvalue;
409 /***********************************************************************
410 * WINPROC_CallWndProc16
412 * Call a 16-bit window procedure
414 static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
415 UINT16 msg, WPARAM16 wParam,
416 LPARAM lParam )
418 CONTEXT86 context;
419 size_t size = 0;
420 struct
422 WORD params[5];
423 union
425 CREATESTRUCT16 cs16;
426 DRAWITEMSTRUCT16 dis16;
427 COMPAREITEMSTRUCT16 cis16;
428 } u;
429 } args;
431 USER_CheckNotLock();
433 /* Window procedures want ax = hInstance, ds = es = ss */
435 memset(&context, 0, sizeof(context));
436 context.SegDs = context.SegEs = SELECTOROF(NtCurrentTeb()->WOW32Reserved);
437 context.SegFs = wine_get_fs();
438 context.SegGs = wine_get_gs();
439 if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
440 context.SegCs = SELECTOROF(proc);
441 context.Eip = OFFSETOF(proc);
442 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
444 if (lParam)
446 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
447 work if structures passed in lParam are placed in the stack/data
448 segment. Programmers easily make the mistake of converting lParam
449 to a near rather than a far pointer, since Windows apparently
450 allows this. We copy the structures to the 16 bit stack; this is
451 ugly but makes these programs work. */
452 switch (msg)
454 case WM_CREATE:
455 case WM_NCCREATE:
456 size = sizeof(CREATESTRUCT16); break;
457 case WM_DRAWITEM:
458 size = sizeof(DRAWITEMSTRUCT16); break;
459 case WM_COMPAREITEM:
460 size = sizeof(COMPAREITEMSTRUCT16); break;
462 if (size)
464 memcpy( &args.u, MapSL(lParam), size );
465 lParam = (SEGPTR)NtCurrentTeb()->WOW32Reserved - size;
469 args.params[4] = hwnd;
470 args.params[3] = msg;
471 args.params[2] = wParam;
472 args.params[1] = HIWORD(lParam);
473 args.params[0] = LOWORD(lParam);
474 WOWCallback16Ex( 0, WCB16_REGS, sizeof(args.params) + size, &args, (DWORD *)&context );
475 return MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
479 /**********************************************************************
480 * WINPROC_GetProc16
482 * Get a window procedure pointer that can be passed to the Windows program.
484 WNDPROC16 WINPROC_GetProc16( WNDPROC proc, BOOL unicode )
486 WINDOWPROC *ptr;
488 if (unicode) ptr = alloc_winproc( NULL, proc );
489 else ptr = alloc_winproc( proc, NULL );
491 if (!ptr) return 0;
492 return alloc_win16_thunk( ptr );
496 /**********************************************************************
497 * WINPROC_GetProc
499 * Get a window procedure pointer that can be passed to the Windows program.
501 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
503 WINDOWPROC *ptr = handle_to_proc( proc );
505 if (!ptr) return proc;
506 if (unicode)
508 if (ptr->procW) return ptr->procW;
509 return proc;
511 else
513 if (ptr->procA) return ptr->procA;
514 return proc;
519 /**********************************************************************
520 * WINPROC_AllocProc16
522 * Allocate a window procedure for a window or class.
524 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
525 * lot of windows, it will usually only have a limited number of window procedures, so the
526 * array won't grow too large, and this way we avoid the need to track allocations per window.
528 WNDPROC WINPROC_AllocProc16( WNDPROC16 func )
530 WINDOWPROC *proc;
532 if (!func) return NULL;
534 /* check if the function is already a win proc */
535 if (!(proc = handle16_to_proc( func )))
537 EnterCriticalSection( &winproc_cs );
539 /* then check if we already have a winproc for that function */
540 if (!(proc = find_winproc16( func )))
542 if (winproc_used < MAX_WINPROCS)
544 proc = &winproc_array[winproc_used++];
545 proc->proc16 = func;
546 TRACE( "allocated %p for %p/16-bit (%d/%d used)\n",
547 proc_to_handle(proc), func, winproc_used, MAX_WINPROCS );
549 else FIXME( "too many winprocs, cannot allocate one for 16-bit %p\n", func );
551 else TRACE( "reusing %p for %p/16-bit\n", proc_to_handle(proc), func );
553 LeaveCriticalSection( &winproc_cs );
555 return proc_to_handle( proc );
559 /**********************************************************************
560 * WINPROC_AllocProc
562 * Allocate a window procedure for a window or class.
564 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
565 * lot of windows, it will usually only have a limited number of window procedures, so the
566 * array won't grow too large, and this way we avoid the need to track allocations per window.
568 WNDPROC WINPROC_AllocProc( WNDPROC funcA, WNDPROC funcW )
570 WINDOWPROC *proc;
572 if (!(proc = alloc_winproc( funcA, funcW ))) return NULL;
573 return proc_to_handle( proc );
577 /**********************************************************************
578 * WINPROC_IsUnicode
580 * Return the window procedure type, or the default value if not a winproc handle.
582 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
584 WINDOWPROC *ptr = handle_to_proc( proc );
586 if (!ptr) return def_val;
587 if (ptr->procA && ptr->procW) return def_val; /* can be both */
588 return (ptr->procW != NULL);
592 /**********************************************************************
593 * WINPROC_TestLBForStr
595 * Return TRUE if the lparam is a string
597 inline static BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
599 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
600 if (msg <= CB_MSGMAX)
601 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
602 else
603 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
606 /**********************************************************************
607 * WINPROC_MapMsg32ATo32W
609 * Map a message from Ansi to Unicode.
610 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
612 * FIXME:
613 * WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
614 * the first four bytes are the handle of the icon
615 * when the WM_SETTEXT message has been used to set the icon
617 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
619 switch(msg)
621 case WM_GETTEXT:
622 case WM_ASKCBFORMATNAME:
624 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0,
625 *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
626 if (!ptr) return -1;
627 *ptr++ = *plparam; /* Store previous lParam */
628 *plparam = (LPARAM)ptr;
630 return 1;
631 /* lparam is string (0-terminated) */
632 case WM_SETTEXT:
633 case WM_WININICHANGE:
634 case WM_DEVMODECHANGE:
635 case CB_DIR:
636 case LB_DIR:
637 case LB_ADDFILE:
638 case EM_REPLACESEL:
639 if (!*plparam) return 0;
640 else
642 DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
643 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
644 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
645 *plparam = (LPARAM)buf;
646 return (*plparam ? 1 : -1);
648 case WM_GETTEXTLENGTH:
649 case CB_GETLBTEXTLEN:
650 case LB_GETTEXTLEN:
651 return 1; /* need to map result */
652 case WM_NCCREATE:
653 case WM_CREATE:
655 UNICODE_STRING usBuffer;
656 struct s
657 { CREATESTRUCTW cs; /* new structure */
658 LPCWSTR lpszName; /* allocated Name */
659 LPCWSTR lpszClass; /* allocated Class */
662 struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
663 if (!xs) return -1;
664 xs->cs = *(CREATESTRUCTW *)*plparam;
665 if (HIWORD(xs->cs.lpszName))
667 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
668 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
670 if (HIWORD(xs->cs.lpszClass))
672 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
673 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
676 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
678 MDICREATESTRUCTW *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
679 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
680 if (HIWORD(mdi_cs->szTitle))
682 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
683 mdi_cs->szTitle = usBuffer.Buffer;
685 if (HIWORD(mdi_cs->szClass))
687 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
688 mdi_cs->szClass = usBuffer.Buffer;
690 xs->cs.lpCreateParams = mdi_cs;
693 *plparam = (LPARAM)xs;
695 return 1;
696 case WM_MDICREATE:
698 MDICREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
699 if (!cs) return -1;
700 *cs = *(MDICREATESTRUCTW *)*plparam;
701 if (HIWORD(cs->szClass))
703 UNICODE_STRING usBuffer;
704 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
705 cs->szClass = usBuffer.Buffer;
707 if (HIWORD(cs->szTitle))
709 UNICODE_STRING usBuffer;
710 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
711 cs->szTitle = usBuffer.Buffer;
713 *plparam = (LPARAM)cs;
715 return 1;
717 /* Listbox / Combobox */
718 case LB_ADDSTRING:
719 case LB_INSERTSTRING:
720 case LB_FINDSTRING:
721 case LB_FINDSTRINGEXACT:
722 case LB_SELECTSTRING:
723 case CB_ADDSTRING:
724 case CB_INSERTSTRING:
725 case CB_FINDSTRINGEXACT:
726 case CB_FINDSTRING:
727 case CB_SELECTSTRING:
728 if(!*plparam) return 0;
729 if ( WINPROC_TestLBForStr( hwnd, msg ))
731 UNICODE_STRING usBuffer;
732 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
733 *plparam = (LPARAM)usBuffer.Buffer;
735 return (*plparam ? 1 : -1);
737 case LB_GETTEXT: /* FIXME: fixed sized buffer */
738 case CB_GETLBTEXT:
739 if ( WINPROC_TestLBForStr( hwnd, msg ))
741 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 * sizeof(WCHAR) + sizeof(LPARAM) );
742 if (!ptr) return -1;
743 *ptr++ = *plparam; /* Store previous lParam */
744 *plparam = (LPARAM)ptr;
746 return 1;
748 /* Multiline edit */
749 case EM_GETLINE:
750 { WORD len = (WORD)*plparam;
751 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
752 if (!ptr) return -1;
753 *ptr++ = *plparam; /* Store previous lParam */
754 *((WORD *) ptr) = len; /* Store the length */
755 *plparam = (LPARAM)ptr;
757 return 1;
759 case WM_CHARTOITEM:
760 case WM_MENUCHAR:
761 case WM_CHAR:
762 case WM_DEADCHAR:
763 case WM_SYSCHAR:
764 case WM_SYSDEADCHAR:
765 case EM_SETPASSWORDCHAR:
767 CHAR ch = LOWORD(*pwparam);
768 WCHAR wch;
769 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
770 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
772 return 0;
774 case WM_IME_CHAR:
776 CHAR ch[2];
777 WCHAR wch;
778 ch[0] = (*pwparam >> 8);
779 ch[1] = *pwparam & 0xff;
780 if (ch[0])
781 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
782 else
783 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
784 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
786 return 0;
788 case WM_PAINTCLIPBOARD:
789 case WM_SIZECLIPBOARD:
790 FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
791 return -1;
792 default: /* No translation needed */
793 return 0;
798 /**********************************************************************
799 * WINPROC_UnmapMsg32ATo32W
801 * Unmap a message that was mapped from Ansi to Unicode.
803 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
804 LRESULT result, WNDPROC dispatch )
806 switch(msg)
808 case WM_GETTEXT:
809 case WM_ASKCBFORMATNAME:
811 LPARAM *ptr = (LPARAM *)lParam - 1;
812 if (!wParam) result = 0;
813 else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
814 (LPSTR)*ptr, wParam, NULL, NULL )))
816 ((LPSTR)*ptr)[wParam-1] = 0;
817 result = wParam - 1;
819 else result--; /* do not count terminating null */
820 HeapFree( GetProcessHeap(), 0, ptr );
822 break;
823 case WM_GETTEXTLENGTH:
824 case CB_GETLBTEXTLEN:
825 case LB_GETTEXTLEN:
826 if (result >= 0)
828 /* Determine respective GETTEXT message */
829 UINT msgGetText =
830 (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
831 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
832 /* wParam differs between the messages */
833 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? (WPARAM)(result + 1) : wParam;
835 WCHAR* p = HeapAlloc (GetProcessHeap(), 0, (result + 1) * sizeof(WCHAR));
837 if (p)
839 LRESULT n;
841 if (dispatch)
842 n = WINPROC_CallWndProc(dispatch, hwnd, msgGetText, wp, (LPARAM)p);
843 else
844 n = SendMessageW (hwnd, msgGetText, wp, (LPARAM)p);
846 result = WideCharToMultiByte( CP_ACP, 0, p, n, NULL, 0, 0, NULL );
847 HeapFree (GetProcessHeap(), 0, p);
850 break;
851 case WM_NCCREATE:
852 case WM_CREATE:
854 struct s
855 { CREATESTRUCTW cs; /* new structure */
856 LPWSTR lpszName; /* allocated Name */
857 LPWSTR lpszClass; /* allocated Class */
859 struct s *xs = (struct s *)lParam;
860 HeapFree( GetProcessHeap(), 0, xs->lpszName );
861 HeapFree( GetProcessHeap(), 0, xs->lpszClass );
863 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
865 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
866 if (HIWORD(mdi_cs->szTitle))
867 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
868 if (HIWORD(mdi_cs->szClass))
869 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
870 HeapFree(GetProcessHeap(), 0, mdi_cs);
872 HeapFree( GetProcessHeap(), 0, xs );
874 break;
876 case WM_MDICREATE:
878 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
879 if (HIWORD(cs->szTitle))
880 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
881 if (HIWORD(cs->szClass))
882 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
883 HeapFree( GetProcessHeap(), 0, cs );
885 break;
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 HeapFree( GetProcessHeap(), 0, (void *)lParam );
895 break;
897 /* Listbox / Combobox */
898 case LB_ADDSTRING:
899 case LB_INSERTSTRING:
900 case LB_FINDSTRING:
901 case LB_FINDSTRINGEXACT:
902 case LB_SELECTSTRING:
903 case CB_ADDSTRING:
904 case CB_INSERTSTRING:
905 case CB_FINDSTRING:
906 case CB_FINDSTRINGEXACT:
907 case CB_SELECTSTRING:
908 if ( WINPROC_TestLBForStr( hwnd, msg ))
909 HeapFree( GetProcessHeap(), 0, (void *)lParam );
910 break;
912 case LB_GETTEXT:
913 case CB_GETLBTEXT:
914 if ( WINPROC_TestLBForStr( hwnd, msg ))
916 LPARAM *ptr = (LPARAM *)lParam - 1;
917 if (result >= 0)
918 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
919 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
920 HeapFree( GetProcessHeap(), 0, ptr );
922 break;
924 /* Multiline edit */
925 case EM_GETLINE:
927 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lParam */
928 WORD len = *(WORD *) lParam;
929 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
930 (LPSTR)*ptr, len, NULL, NULL );
931 if (result < len) ((LPSTR)*ptr)[result] = 0;
932 HeapFree( GetProcessHeap(), 0, ptr );
934 break;
936 return result;
940 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
942 HANDLE dst;
943 UINT sz = GlobalSize16(src);
944 LPSTR ptr16, ptr32;
946 if (!(dst = GlobalAlloc(flags, sz)))
947 return 0;
948 ptr16 = GlobalLock16(src);
949 ptr32 = GlobalLock(dst);
950 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
951 GlobalUnlock16(src);
952 GlobalUnlock(dst);
954 return (UINT)dst;
957 /**********************************************************************
958 * WINPROC_MapMsg16To32A
960 * Map a message from 16- to 32-bit Ansi.
961 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
963 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
964 WPARAM *pwparam32, LPARAM *plparam )
966 *pmsg32 = (UINT)msg16;
967 *pwparam32 = (WPARAM)wParam16;
968 switch(msg16)
970 case WM_ACTIVATE:
971 case WM_CHARTOITEM:
972 case WM_COMMAND:
973 case WM_VKEYTOITEM:
974 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
975 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
976 return 0;
977 case WM_HSCROLL:
978 case WM_VSCROLL:
979 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
980 *plparam = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
981 return 0;
982 case WM_CTLCOLOR:
983 if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
984 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
985 *pwparam32 = (WPARAM)HDC_32(wParam16);
986 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
987 return 0;
988 case WM_COMPAREITEM:
990 COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
991 COMPAREITEMSTRUCT *cis = HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
992 if (!cis) return -1;
993 cis->CtlType = cis16->CtlType;
994 cis->CtlID = cis16->CtlID;
995 cis->hwndItem = WIN_Handle32( cis16->hwndItem );
996 cis->itemID1 = cis16->itemID1;
997 cis->itemData1 = cis16->itemData1;
998 cis->itemID2 = cis16->itemID2;
999 cis->itemData2 = cis16->itemData2;
1000 cis->dwLocaleId = 0; /* FIXME */
1001 *plparam = (LPARAM)cis;
1003 return 1;
1004 case WM_COPYDATA:
1006 PCOPYDATASTRUCT16 pcds16 = MapSL(*plparam);
1007 PCOPYDATASTRUCT pcds = HeapAlloc ( GetProcessHeap(), 0, sizeof(*pcds));
1008 pcds->dwData = pcds16->dwData;
1009 pcds->cbData = pcds16->cbData;
1010 pcds->lpData = MapSL( pcds16->lpData);
1011 *plparam = (LPARAM)pcds;
1013 return 1;
1014 case WM_DELETEITEM:
1016 DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1017 DELETEITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1018 if (!dis) return -1;
1019 dis->CtlType = dis16->CtlType;
1020 dis->CtlID = dis16->CtlID;
1021 dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1022 dis->itemData = dis16->itemData;
1023 *plparam = (LPARAM)dis;
1025 return 1;
1026 case WM_MEASUREITEM:
1028 MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1029 MEASUREITEMSTRUCT *mis = HeapAlloc(GetProcessHeap(), 0,
1030 sizeof(*mis) + sizeof(LPARAM));
1031 if (!mis) return -1;
1032 mis->CtlType = mis16->CtlType;
1033 mis->CtlID = mis16->CtlID;
1034 mis->itemID = mis16->itemID;
1035 mis->itemWidth = mis16->itemWidth;
1036 mis->itemHeight = mis16->itemHeight;
1037 mis->itemData = mis16->itemData;
1038 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1039 *plparam = (LPARAM)mis;
1041 return 1;
1042 case WM_DRAWITEM:
1044 DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1045 DRAWITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1046 if (!dis) return -1;
1047 dis->CtlType = dis16->CtlType;
1048 dis->CtlID = dis16->CtlID;
1049 dis->itemID = dis16->itemID;
1050 dis->itemAction = dis16->itemAction;
1051 dis->itemState = dis16->itemState;
1052 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1053 : WIN_Handle32( dis16->hwndItem );
1054 dis->hDC = HDC_32(dis16->hDC);
1055 dis->itemData = dis16->itemData;
1056 dis->rcItem.left = dis16->rcItem.left;
1057 dis->rcItem.top = dis16->rcItem.top;
1058 dis->rcItem.right = dis16->rcItem.right;
1059 dis->rcItem.bottom = dis16->rcItem.bottom;
1060 *plparam = (LPARAM)dis;
1062 return 1;
1063 case WM_GETMINMAXINFO:
1065 MINMAXINFO *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM));
1066 if (!mmi) return -1;
1067 MINMAXINFO16to32( MapSL(*plparam), mmi );
1068 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1069 *plparam = (LPARAM)mmi;
1071 return 1;
1072 case WM_GETTEXT:
1073 case WM_SETTEXT:
1074 case WM_WININICHANGE:
1075 case WM_DEVMODECHANGE:
1076 case WM_ASKCBFORMATNAME:
1077 *plparam = (LPARAM)MapSL(*plparam);
1078 return 0;
1079 case WM_MDICREATE:
1081 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1082 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1083 if (!cs) return -1;
1084 MDICREATESTRUCT16to32A( cs16, cs );
1085 cs->szTitle = MapSL(cs16->szTitle);
1086 cs->szClass = MapSL(cs16->szClass);
1087 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1088 *plparam = (LPARAM)cs;
1090 return 1;
1091 case WM_MDIGETACTIVE:
1092 *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1093 *(BOOL*)(*plparam) = 0;
1094 return 1;
1095 case WM_MDISETMENU:
1096 if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1097 *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1098 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1099 return 0;
1100 case WM_MENUCHAR:
1101 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1102 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1103 return 0;
1104 case WM_MENUSELECT:
1105 if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1107 HMENU hmenu=HMENU_32(HIWORD(*plparam));
1108 UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1109 if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1110 *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1112 else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1113 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1114 return 0;
1115 case WM_MDIACTIVATE:
1116 if( *plparam )
1118 *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1119 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1121 else /* message sent to MDI client */
1122 *pwparam32 = wParam16;
1123 return 0;
1124 case WM_NCCALCSIZE:
1126 NCCALCSIZE_PARAMS16 *nc16;
1127 NCCALCSIZE_PARAMS *nc;
1129 nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM) );
1130 if (!nc) return -1;
1131 nc16 = MapSL(*plparam);
1132 nc->rgrc[0].left = nc16->rgrc[0].left;
1133 nc->rgrc[0].top = nc16->rgrc[0].top;
1134 nc->rgrc[0].right = nc16->rgrc[0].right;
1135 nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1136 if (wParam16)
1138 nc->lppos = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc->lppos) );
1139 nc->rgrc[1].left = nc16->rgrc[1].left;
1140 nc->rgrc[1].top = nc16->rgrc[1].top;
1141 nc->rgrc[1].right = nc16->rgrc[1].right;
1142 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1143 nc->rgrc[2].left = nc16->rgrc[2].left;
1144 nc->rgrc[2].top = nc16->rgrc[2].top;
1145 nc->rgrc[2].right = nc16->rgrc[2].right;
1146 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1147 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1149 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1150 *plparam = (LPARAM)nc;
1152 return 1;
1153 case WM_NCCREATE:
1154 case WM_CREATE:
1156 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1157 CREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1158 if (!cs) return -1;
1159 CREATESTRUCT16to32A( cs16, cs );
1160 cs->lpszName = MapSL(cs16->lpszName);
1161 cs->lpszClass = MapSL(cs16->lpszClass);
1163 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1165 MDICREATESTRUCT16 *mdi_cs16;
1166 MDICREATESTRUCTA *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1167 if (!mdi_cs)
1169 HeapFree(GetProcessHeap(), 0, cs);
1170 return -1;
1172 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1173 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1174 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1175 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1177 cs->lpCreateParams = mdi_cs;
1179 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1180 *plparam = (LPARAM)cs;
1182 return 1;
1183 case WM_PARENTNOTIFY:
1184 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1186 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1187 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1189 return 0;
1190 case WM_WINDOWPOSCHANGING:
1191 case WM_WINDOWPOSCHANGED:
1193 WINDOWPOS *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
1194 if (!wp) return -1;
1195 WINDOWPOS16to32( MapSL(*plparam), wp );
1196 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1197 *plparam = (LPARAM)wp;
1199 return 1;
1200 case WM_GETDLGCODE:
1201 if (*plparam)
1203 LPMSG16 msg16 = MapSL(*plparam);
1204 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1206 if (!msg32) return -1;
1207 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1208 msg32->message = msg16->message;
1209 msg32->wParam = msg16->wParam;
1210 msg32->lParam = msg16->lParam;
1211 msg32->time = msg16->time;
1212 msg32->pt.x = msg16->pt.x;
1213 msg32->pt.y = msg16->pt.y;
1214 *plparam = (LPARAM)msg32;
1215 return 1;
1217 else return 0;
1218 case WM_NOTIFY:
1219 *plparam = (LPARAM)MapSL(*plparam);
1220 return 0;
1221 case WM_ACTIVATEAPP:
1222 /* We need this when SetActiveWindow sends a Sendmessage16() to
1223 * a 32bit window. Might be superflous with 32bit interprocess
1224 * message queues. */
1225 if (*plparam) *plparam = HTASK_32( *plparam );
1226 return 0;
1227 case WM_NEXTMENU:
1229 MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1230 if (!next) return -1;
1231 next->hmenuIn = (HMENU)*plparam;
1232 next->hmenuNext = 0;
1233 next->hwndNext = 0;
1234 *plparam = (LPARAM)next;
1235 return 1;
1237 case WM_PAINTCLIPBOARD:
1238 case WM_SIZECLIPBOARD:
1239 FIXME_(msg)("message %04x needs translation\n",msg16 );
1240 return -1;
1241 case WM_DDE_INITIATE:
1242 case WM_DDE_TERMINATE:
1243 case WM_DDE_UNADVISE:
1244 case WM_DDE_REQUEST:
1245 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1246 return 0;
1247 case WM_DDE_ADVISE:
1248 case WM_DDE_DATA:
1249 case WM_DDE_POKE:
1251 HANDLE16 lo16;
1252 ATOM hi;
1253 UINT lo32 = 0;
1255 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1256 lo16 = LOWORD(*plparam);
1257 hi = HIWORD(*plparam);
1258 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1259 return -1;
1260 *plparam = PackDDElParam(msg16, lo32, hi);
1262 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1263 case WM_DDE_ACK:
1265 UINT lo, hi;
1266 int flag = 0;
1267 char buf[2];
1269 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1271 lo = LOWORD(*plparam);
1272 hi = HIWORD(*plparam);
1274 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1275 if (GlobalSize16(hi) != 0) flag |= 2;
1276 switch (flag)
1278 case 0:
1279 if (hi)
1281 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1282 hi = 0;
1284 break;
1285 case 1:
1286 break; /* atom, nothing to do */
1287 case 3:
1288 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1289 /* fall thru */
1290 case 2:
1291 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1292 break;
1294 *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1296 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1297 case WM_DDE_EXECUTE:
1298 *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1299 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1300 default: /* No translation needed */
1301 return 0;
1306 /**********************************************************************
1307 * WINPROC_UnmapMsg16To32A
1309 * Unmap a message that was mapped from 16- to 32-bit Ansi.
1311 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1312 LRESULT result )
1314 switch(msg)
1316 case WM_COMPAREITEM:
1317 case WM_DELETEITEM:
1318 case WM_DRAWITEM:
1319 case WM_COPYDATA:
1320 HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1321 break;
1322 case WM_MEASUREITEM:
1324 MEASUREITEMSTRUCT16 *mis16;
1325 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1326 lParam = *(LPARAM *)(mis + 1);
1327 mis16 = MapSL(lParam);
1328 mis16->itemWidth = (UINT16)mis->itemWidth;
1329 mis16->itemHeight = (UINT16)mis->itemHeight;
1330 HeapFree( GetProcessHeap(), 0, mis );
1332 break;
1333 case WM_GETMINMAXINFO:
1335 MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1336 lParam = *(LPARAM *)(mmi + 1);
1337 MINMAXINFO32to16( mmi, MapSL(lParam));
1338 HeapFree( GetProcessHeap(), 0, mmi );
1340 break;
1341 case WM_MDICREATE:
1343 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1344 lParam = *(LPARAM *)(cs + 1);
1345 MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1346 HeapFree( GetProcessHeap(), 0, cs );
1348 break;
1349 case WM_MDIGETACTIVE:
1350 result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1351 HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1352 break;
1353 case WM_NCCALCSIZE:
1355 NCCALCSIZE_PARAMS16 *nc16;
1356 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1357 lParam = *(LPARAM *)(nc + 1);
1358 nc16 = MapSL(lParam);
1359 nc16->rgrc[0].left = nc->rgrc[0].left;
1360 nc16->rgrc[0].top = nc->rgrc[0].top;
1361 nc16->rgrc[0].right = nc->rgrc[0].right;
1362 nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1363 if (wParam)
1365 nc16->rgrc[1].left = nc->rgrc[1].left;
1366 nc16->rgrc[1].top = nc->rgrc[1].top;
1367 nc16->rgrc[1].right = nc->rgrc[1].right;
1368 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1369 nc16->rgrc[2].left = nc->rgrc[2].left;
1370 nc16->rgrc[2].top = nc->rgrc[2].top;
1371 nc16->rgrc[2].right = nc->rgrc[2].right;
1372 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1373 if (nc->lppos)
1375 WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1376 HeapFree( GetProcessHeap(), 0, nc->lppos );
1379 HeapFree( GetProcessHeap(), 0, nc );
1381 break;
1382 case WM_NCCREATE:
1383 case WM_CREATE:
1385 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1386 lParam = *(LPARAM *)(cs + 1);
1387 CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1389 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1390 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1392 HeapFree( GetProcessHeap(), 0, cs );
1394 break;
1395 case WM_WINDOWPOSCHANGING:
1396 case WM_WINDOWPOSCHANGED:
1398 WINDOWPOS *wp = (WINDOWPOS *)lParam;
1399 lParam = *(LPARAM *)(wp + 1);
1400 WINDOWPOS32to16(wp, MapSL(lParam));
1401 HeapFree( GetProcessHeap(), 0, wp );
1403 break;
1404 case WM_GETDLGCODE:
1405 if (lParam)
1407 LPMSG msg32 = (LPMSG)lParam;
1408 HeapFree( GetProcessHeap(), 0, msg32 );
1410 break;
1411 case WM_NEXTMENU:
1413 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1414 result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1415 HeapFree( GetProcessHeap(), 0, next );
1417 break;
1419 return result;
1423 /**********************************************************************
1424 * WINPROC_MapMsg16To32W
1426 * Map a message from 16- to 32-bit Unicode.
1427 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1429 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1430 WPARAM *pwparam32, LPARAM *plparam )
1432 CHAR ch;
1433 WCHAR wch;
1435 *pmsg32=(UINT)msg16;
1436 *pwparam32 = (WPARAM)wParam16;
1437 switch(msg16)
1439 case WM_GETTEXT:
1440 case WM_SETTEXT:
1441 case WM_WININICHANGE:
1442 case WM_DEVMODECHANGE:
1443 case WM_ASKCBFORMATNAME:
1444 *plparam = (LPARAM)MapSL(*plparam);
1445 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1446 case WM_GETTEXTLENGTH:
1447 case CB_GETLBTEXTLEN:
1448 case LB_GETTEXTLEN:
1449 return 1; /* need to map result */
1450 case WM_NCCREATE:
1451 case WM_CREATE:
1453 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1454 CREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1455 if (!cs) return -1;
1456 CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1457 cs->lpszName = map_str_16_to_32W(cs16->lpszName);
1458 cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1460 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1462 MDICREATESTRUCT16 *mdi_cs16;
1463 MDICREATESTRUCTW *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1464 if (!mdi_cs)
1466 HeapFree(GetProcessHeap(), 0, cs);
1467 return -1;
1469 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1470 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1471 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1472 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1474 cs->lpCreateParams = mdi_cs;
1476 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1477 *plparam = (LPARAM)cs;
1479 return 1;
1480 case WM_MDICREATE:
1482 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1483 MDICREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1484 if (!cs) return -1;
1485 MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1486 cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1487 cs->szClass = map_str_16_to_32W(cs16->szClass);
1488 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1489 *plparam = (LPARAM)cs;
1491 return 1;
1492 case WM_GETDLGCODE:
1493 if (*plparam)
1495 LPMSG16 msg16 = MapSL(*plparam);
1496 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1498 if (!msg32) return -1;
1499 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1500 msg32->message = msg16->message;
1501 msg32->wParam = msg16->wParam;
1502 msg32->lParam = msg16->lParam;
1503 msg32->time = msg16->time;
1504 msg32->pt.x = msg16->pt.x;
1505 msg32->pt.y = msg16->pt.y;
1506 switch(msg32->message)
1508 case WM_CHAR:
1509 case WM_DEADCHAR:
1510 case WM_SYSCHAR:
1511 case WM_SYSDEADCHAR:
1512 ch = msg16->wParam;
1513 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1514 msg32->wParam = wch;
1515 break;
1517 *plparam = (LPARAM)msg32;
1518 return 1;
1520 else return 0;
1522 case WM_CHARTOITEM:
1523 ch = wParam16;
1524 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1525 *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1526 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1527 return 0;
1528 case WM_MENUCHAR:
1529 ch = wParam16;
1530 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1531 *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1532 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1533 return 0;
1534 case WM_CHAR:
1535 case WM_DEADCHAR:
1536 case WM_SYSCHAR:
1537 case WM_SYSDEADCHAR:
1538 ch = wParam16;
1539 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1540 *pwparam32 = wch;
1541 return 0;
1542 case WM_IME_CHAR:
1543 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1545 default: /* No Unicode translation needed */
1546 return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1547 pwparam32, plparam );
1552 /**********************************************************************
1553 * WINPROC_UnmapMsg16To32W
1555 * Unmap a message that was mapped from 16- to 32-bit Unicode.
1557 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1558 LRESULT result, WNDPROC dispatch )
1560 switch(msg)
1562 case WM_GETTEXT:
1563 case WM_SETTEXT:
1564 case WM_GETTEXTLENGTH:
1565 case CB_GETLBTEXTLEN:
1566 case LB_GETTEXTLEN:
1567 case WM_ASKCBFORMATNAME:
1568 return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, dispatch );
1569 case WM_NCCREATE:
1570 case WM_CREATE:
1572 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1573 lParam = *(LPARAM *)(cs + 1);
1574 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1575 unmap_str_16_to_32W( cs->lpszName );
1576 unmap_str_16_to_32W( cs->lpszClass );
1578 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1580 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1581 unmap_str_16_to_32W( mdi_cs->szTitle );
1582 unmap_str_16_to_32W( mdi_cs->szClass );
1583 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1585 HeapFree( GetProcessHeap(), 0, cs );
1587 break;
1588 case WM_MDICREATE:
1590 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1591 lParam = *(LPARAM *)(cs + 1);
1592 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1593 unmap_str_16_to_32W( cs->szTitle );
1594 unmap_str_16_to_32W( cs->szClass );
1595 HeapFree( GetProcessHeap(), 0, cs );
1597 break;
1598 case WM_GETDLGCODE:
1599 if (lParam)
1601 LPMSG msg32 = (LPMSG)lParam;
1602 HeapFree( GetProcessHeap(), 0, msg32 );
1604 break;
1605 default:
1606 return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1608 return result;
1611 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1613 HANDLE16 dst;
1614 UINT sz = GlobalSize((HANDLE)src);
1615 LPSTR ptr16, ptr32;
1617 if (!(dst = GlobalAlloc16(flags, sz)))
1618 return 0;
1619 ptr32 = GlobalLock((HANDLE)src);
1620 ptr16 = GlobalLock16(dst);
1621 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1622 GlobalUnlock((HANDLE)src);
1623 GlobalUnlock16(dst);
1625 return dst;
1629 /**********************************************************************
1630 * WINPROC_MapMsg32ATo16
1632 * Map a message from 32-bit Ansi to 16-bit.
1633 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1635 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1636 UINT16 *pmsg16, WPARAM16 *pwparam16,
1637 LPARAM *plparam )
1639 *pmsg16 = (UINT16)msg32;
1640 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1641 switch(msg32)
1643 case SBM_SETRANGE:
1644 *pmsg16 = SBM_SETRANGE16;
1645 *plparam = MAKELPARAM(wParam32, *plparam);
1646 *pwparam16 = 0;
1647 return 0;
1649 case SBM_GETRANGE:
1650 *pmsg16 = SBM_GETRANGE16;
1651 return 1;
1653 case BM_GETCHECK:
1654 case BM_SETCHECK:
1655 case BM_GETSTATE:
1656 case BM_SETSTATE:
1657 case BM_SETSTYLE:
1658 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
1659 return 0;
1661 case EM_GETSEL:
1662 case EM_GETRECT:
1663 case EM_SETRECT:
1664 case EM_SETRECTNP:
1665 case EM_SCROLL:
1666 case EM_LINESCROLL:
1667 case EM_SCROLLCARET:
1668 case EM_GETMODIFY:
1669 case EM_SETMODIFY:
1670 case EM_GETLINECOUNT:
1671 case EM_LINEINDEX:
1672 case EM_SETHANDLE:
1673 case EM_GETHANDLE:
1674 case EM_GETTHUMB:
1675 case EM_LINELENGTH:
1676 case EM_REPLACESEL:
1677 case EM_GETLINE:
1678 case EM_LIMITTEXT:
1679 case EM_CANUNDO:
1680 case EM_UNDO:
1681 case EM_FMTLINES:
1682 case EM_LINEFROMCHAR:
1683 case EM_SETTABSTOPS:
1684 case EM_SETPASSWORDCHAR:
1685 case EM_EMPTYUNDOBUFFER:
1686 case EM_GETFIRSTVISIBLELINE:
1687 case EM_SETREADONLY:
1688 case EM_SETWORDBREAKPROC:
1689 case EM_GETWORDBREAKPROC:
1690 case EM_GETPASSWORDCHAR:
1691 *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
1692 return 0;
1694 case LB_CARETOFF:
1695 case LB_CARETON:
1696 case LB_DELETESTRING:
1697 case LB_GETANCHORINDEX:
1698 case LB_GETCARETINDEX:
1699 case LB_GETCOUNT:
1700 case LB_GETCURSEL:
1701 case LB_GETHORIZONTALEXTENT:
1702 case LB_GETITEMDATA:
1703 case LB_GETITEMHEIGHT:
1704 case LB_GETSEL:
1705 case LB_GETSELCOUNT:
1706 case LB_GETTEXTLEN:
1707 case LB_GETTOPINDEX:
1708 case LB_RESETCONTENT:
1709 case LB_SELITEMRANGE:
1710 case LB_SELITEMRANGEEX:
1711 case LB_SETANCHORINDEX:
1712 case LB_SETCARETINDEX:
1713 case LB_SETCOLUMNWIDTH:
1714 case LB_SETCURSEL:
1715 case LB_SETHORIZONTALEXTENT:
1716 case LB_SETITEMDATA:
1717 case LB_SETITEMHEIGHT:
1718 case LB_SETSEL:
1719 case LB_SETTOPINDEX:
1720 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
1721 return 0;
1722 case CB_DELETESTRING:
1723 case CB_GETCOUNT:
1724 case CB_GETLBTEXTLEN:
1725 case CB_LIMITTEXT:
1726 case CB_RESETCONTENT:
1727 case CB_SETEDITSEL:
1728 case CB_GETCURSEL:
1729 case CB_SETCURSEL:
1730 case CB_SHOWDROPDOWN:
1731 case CB_SETITEMDATA:
1732 case CB_SETITEMHEIGHT:
1733 case CB_GETITEMHEIGHT:
1734 case CB_SETEXTENDEDUI:
1735 case CB_GETEXTENDEDUI:
1736 case CB_GETDROPPEDSTATE:
1737 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
1738 return 0;
1739 case CB_GETEDITSEL:
1740 *pmsg16 = CB_GETEDITSEL16;
1741 return 1;
1743 case LB_ADDSTRING:
1744 case LB_FINDSTRING:
1745 case LB_FINDSTRINGEXACT:
1746 case LB_INSERTSTRING:
1747 case LB_SELECTSTRING:
1748 case LB_DIR:
1749 case LB_ADDFILE:
1750 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
1751 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
1752 return 1;
1754 case CB_ADDSTRING:
1755 case CB_FINDSTRING:
1756 case CB_FINDSTRINGEXACT:
1757 case CB_INSERTSTRING:
1758 case CB_SELECTSTRING:
1759 case CB_DIR:
1760 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
1761 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
1762 return 1;
1764 case LB_GETITEMRECT:
1766 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
1767 if (!rect) return -1;
1768 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
1769 *plparam = MapLS( rect );
1771 *pmsg16 = LB_GETITEMRECT16;
1772 return 1;
1773 case LB_GETSELITEMS:
1775 LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
1777 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
1778 if (!(items = HeapAlloc( GetProcessHeap(), 0,
1779 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
1780 *items++ = *plparam; /* Store the previous lParam */
1781 *plparam = MapLS( items );
1783 *pmsg16 = LB_GETSELITEMS16;
1784 return 1;
1785 case LB_SETTABSTOPS:
1786 if (wParam32)
1788 INT i;
1789 LPINT16 stops;
1790 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
1791 if (!(stops = HeapAlloc( GetProcessHeap(), 0,
1792 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
1793 for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
1794 *plparam = MapLS( stops );
1795 return 1;
1797 *pmsg16 = LB_SETTABSTOPS16;
1798 return 0;
1800 case CB_GETDROPPEDCONTROLRECT:
1802 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
1803 if (!rect) return -1;
1804 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
1805 *plparam = (LPARAM)MapLS(rect);
1807 *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
1808 return 1;
1810 case LB_GETTEXT:
1811 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
1812 *pmsg16 = LB_GETTEXT16;
1813 return 1;
1815 case CB_GETLBTEXT:
1816 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
1817 *pmsg16 = CB_GETLBTEXT16;
1818 return 1;
1820 case EM_SETSEL:
1821 *pwparam16 = 0;
1822 *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
1823 *pmsg16 = EM_SETSEL16;
1824 return 0;
1826 case WM_ACTIVATE:
1827 case WM_CHARTOITEM:
1828 case WM_COMMAND:
1829 case WM_VKEYTOITEM:
1830 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
1831 return 0;
1832 case WM_HSCROLL:
1833 case WM_VSCROLL:
1834 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
1835 return 0;
1836 case WM_COPYDATA:
1838 PCOPYDATASTRUCT pcds32 = (PCOPYDATASTRUCT) *plparam;
1839 PCOPYDATASTRUCT16 pcds = HeapAlloc( GetProcessHeap(), 0, sizeof( *pcds));
1840 pcds->dwData = pcds32->dwData;
1841 pcds->cbData = pcds32->cbData;
1842 pcds->lpData = MapLS( pcds32->lpData);
1843 *plparam = MapLS( pcds );
1845 return 1;
1846 case WM_CTLCOLORMSGBOX:
1847 case WM_CTLCOLOREDIT:
1848 case WM_CTLCOLORLISTBOX:
1849 case WM_CTLCOLORBTN:
1850 case WM_CTLCOLORDLG:
1851 case WM_CTLCOLORSCROLLBAR:
1852 case WM_CTLCOLORSTATIC:
1853 *pmsg16 = WM_CTLCOLOR;
1854 *plparam = MAKELPARAM( (HWND16)*plparam,
1855 (WORD)msg32 - WM_CTLCOLORMSGBOX );
1856 return 0;
1857 case WM_COMPAREITEM:
1859 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
1860 COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
1861 if (!cis) return -1;
1862 cis->CtlType = (UINT16)cis32->CtlType;
1863 cis->CtlID = (UINT16)cis32->CtlID;
1864 cis->hwndItem = HWND_16( cis32->hwndItem );
1865 cis->itemID1 = (UINT16)cis32->itemID1;
1866 cis->itemData1 = cis32->itemData1;
1867 cis->itemID2 = (UINT16)cis32->itemID2;
1868 cis->itemData2 = cis32->itemData2;
1869 *plparam = MapLS( cis );
1871 return 1;
1872 case WM_DELETEITEM:
1874 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
1875 DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
1876 if (!dis) return -1;
1877 dis->CtlType = (UINT16)dis32->CtlType;
1878 dis->CtlID = (UINT16)dis32->CtlID;
1879 dis->itemID = (UINT16)dis32->itemID;
1880 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
1881 : HWND_16( dis32->hwndItem );
1882 dis->itemData = dis32->itemData;
1883 *plparam = MapLS( dis );
1885 return 1;
1886 case WM_DRAWITEM:
1888 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
1889 DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
1890 if (!dis) return -1;
1891 dis->CtlType = (UINT16)dis32->CtlType;
1892 dis->CtlID = (UINT16)dis32->CtlID;
1893 dis->itemID = (UINT16)dis32->itemID;
1894 dis->itemAction = (UINT16)dis32->itemAction;
1895 dis->itemState = (UINT16)dis32->itemState;
1896 dis->hwndItem = HWND_16( dis32->hwndItem );
1897 dis->hDC = HDC_16(dis32->hDC);
1898 dis->itemData = dis32->itemData;
1899 dis->rcItem.left = dis32->rcItem.left;
1900 dis->rcItem.top = dis32->rcItem.top;
1901 dis->rcItem.right = dis32->rcItem.right;
1902 dis->rcItem.bottom = dis32->rcItem.bottom;
1903 *plparam = MapLS( dis );
1905 return 1;
1906 case WM_MEASUREITEM:
1908 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
1909 MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
1910 if (!mis) return -1;
1911 mis->CtlType = (UINT16)mis32->CtlType;
1912 mis->CtlID = (UINT16)mis32->CtlID;
1913 mis->itemID = (UINT16)mis32->itemID;
1914 mis->itemWidth = (UINT16)mis32->itemWidth;
1915 mis->itemHeight = (UINT16)mis32->itemHeight;
1916 mis->itemData = mis32->itemData;
1917 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1918 *plparam = MapLS( mis );
1920 return 1;
1921 case WM_GETMINMAXINFO:
1923 MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
1924 if (!mmi) return -1;
1925 MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
1926 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1927 *plparam = MapLS( mmi );
1929 return 1;
1930 case WM_GETTEXT:
1931 case WM_ASKCBFORMATNAME:
1933 LPARAM *str; /* store LPARAM, then *pwparam16 char space */
1934 *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
1935 if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
1936 *str++ = *plparam; /* Store the previous lParam */
1937 *plparam = MapLS( str );
1939 return 1;
1940 case WM_MDICREATE:
1942 MDICREATESTRUCT16 *cs;
1943 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
1945 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
1946 MDICREATESTRUCT32Ato16( cs32, cs );
1947 cs->szTitle = MapLS( cs32->szTitle );
1948 cs->szClass = MapLS( cs32->szClass );
1949 *plparam = MapLS( cs );
1951 return 1;
1952 case WM_MDIGETACTIVE:
1953 return 1;
1954 case WM_MDISETMENU:
1955 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
1956 (HMENU16)LOWORD(*plparam) );
1957 *pwparam16 = (*plparam == 0);
1958 return 0;
1959 case WM_MENUSELECT:
1960 if(HIWORD(wParam32) & MF_POPUP)
1962 HMENU hmenu;
1963 if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
1965 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
1966 *pwparam16=HMENU_16(hmenu);
1969 /* fall through */
1970 case WM_MENUCHAR:
1971 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
1972 return 0;
1973 case WM_MDIACTIVATE:
1974 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
1976 *pwparam16 = ((HWND)*plparam == hwnd);
1977 *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
1978 (HWND16)LOWORD(wParam32) );
1980 else
1982 *pwparam16 = HWND_16( (HWND)wParam32 );
1983 *plparam = 0;
1985 return 0;
1986 case WM_NCCALCSIZE:
1988 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
1989 NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
1990 if (!nc) return -1;
1992 nc->rgrc[0].left = nc32->rgrc[0].left;
1993 nc->rgrc[0].top = nc32->rgrc[0].top;
1994 nc->rgrc[0].right = nc32->rgrc[0].right;
1995 nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
1996 if (wParam32)
1998 WINDOWPOS16 *wp;
1999 nc->rgrc[1].left = nc32->rgrc[1].left;
2000 nc->rgrc[1].top = nc32->rgrc[1].top;
2001 nc->rgrc[1].right = nc32->rgrc[1].right;
2002 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2003 nc->rgrc[2].left = nc32->rgrc[2].left;
2004 nc->rgrc[2].top = nc32->rgrc[2].top;
2005 nc->rgrc[2].right = nc32->rgrc[2].right;
2006 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2007 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2009 HeapFree( GetProcessHeap(), 0, nc );
2010 return -1;
2012 WINDOWPOS32to16( nc32->lppos, wp );
2013 nc->lppos = MapLS( wp );
2015 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
2016 *plparam = MapLS( nc );
2018 return 1;
2019 case WM_NCCREATE:
2020 case WM_CREATE:
2022 CREATESTRUCT16 *cs;
2023 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2025 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2026 CREATESTRUCT32Ato16( cs32, cs );
2027 cs->lpszName = MapLS( cs32->lpszName );
2028 cs->lpszClass = MapLS( cs32->lpszClass );
2030 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2032 MDICREATESTRUCT16 *mdi_cs16;
2033 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2034 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2035 if (!mdi_cs16)
2037 HeapFree(GetProcessHeap(), 0, cs);
2038 return -1;
2040 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2041 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2042 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2043 cs->lpCreateParams = MapLS( mdi_cs16 );
2045 *plparam = MapLS( cs );
2047 return 1;
2048 case WM_PARENTNOTIFY:
2049 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2050 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2051 /* else nothing to do */
2052 return 0;
2053 case WM_NOTIFY:
2054 *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2055 return 1;
2056 case WM_SETTEXT:
2057 case WM_WININICHANGE:
2058 case WM_DEVMODECHANGE:
2059 *plparam = MapLS( (LPSTR)*plparam );
2060 return 1;
2061 case WM_WINDOWPOSCHANGING:
2062 case WM_WINDOWPOSCHANGED:
2064 WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2065 if (!wp) return -1;
2066 WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2067 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
2068 *plparam = MapLS( wp );
2070 return 1;
2071 case WM_GETDLGCODE:
2072 if (*plparam) {
2073 LPMSG msg32 = (LPMSG) *plparam;
2074 LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2076 if (!msg16) return -1;
2077 msg16->hwnd = HWND_16( msg32->hwnd );
2078 msg16->message = msg32->message;
2079 msg16->wParam = msg32->wParam;
2080 msg16->lParam = msg32->lParam;
2081 msg16->time = msg32->time;
2082 msg16->pt.x = msg32->pt.x;
2083 msg16->pt.y = msg32->pt.y;
2084 *plparam = MapLS( msg16 );
2085 return 1;
2087 return 0;
2089 case WM_ACTIVATEAPP:
2090 if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2091 return 0;
2092 case WM_NEXTMENU:
2094 MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2095 *plparam = (LPARAM)next->hmenuIn;
2096 return 1;
2098 case WM_PAINT:
2099 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2101 *pmsg16 = WM_PAINTICON;
2102 *pwparam16 = 1;
2104 return 0;
2105 case WM_ERASEBKGND:
2106 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2107 *pmsg16 = WM_ICONERASEBKGND;
2108 return 0;
2109 case WM_PAINTCLIPBOARD:
2110 case WM_SIZECLIPBOARD:
2111 FIXME_(msg)("message %04x needs translation\n", msg32 );
2112 return -1;
2113 /* following messages should not be sent to 16-bit apps */
2114 case WM_SIZING:
2115 case WM_MOVING:
2116 case WM_CAPTURECHANGED:
2117 case WM_STYLECHANGING:
2118 case WM_STYLECHANGED:
2119 return -1;
2120 case WM_DDE_INITIATE:
2121 case WM_DDE_TERMINATE:
2122 case WM_DDE_UNADVISE:
2123 case WM_DDE_REQUEST:
2124 *pwparam16 = HWND_16((HWND)wParam32);
2125 return 0;
2126 case WM_DDE_ADVISE:
2127 case WM_DDE_DATA:
2128 case WM_DDE_POKE:
2130 UINT_PTR lo32, hi;
2131 HANDLE16 lo16 = 0;
2133 *pwparam16 = HWND_16((HWND)wParam32);
2134 UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2135 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2136 return -1;
2137 *plparam = MAKELPARAM(lo16, hi);
2139 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2140 case WM_DDE_ACK:
2142 UINT_PTR lo, hi;
2143 int flag = 0;
2144 char buf[2];
2146 *pwparam16 = HWND_16((HWND)wParam32);
2148 UnpackDDElParam(msg32, *plparam, &lo, &hi);
2150 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2151 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2152 switch (flag)
2154 case 0:
2155 if (hi)
2157 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2158 hi = 0;
2160 break;
2161 case 1:
2162 break; /* atom, nothing to do */
2163 case 3:
2164 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2165 /* fall thru */
2166 case 2:
2167 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2168 break;
2170 *plparam = MAKELPARAM(lo, hi);
2172 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2173 case WM_DDE_EXECUTE:
2174 *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2175 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2176 default: /* No translation needed */
2177 return 0;
2182 /**********************************************************************
2183 * WINPROC_UnmapMsg32ATo16
2185 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2187 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2188 MSGPARAM16* p16 )
2190 switch(msg)
2192 case SBM_GETRANGE:
2193 *(LPINT)wParam = LOWORD(p16->lResult);
2194 *(LPINT)lParam = HIWORD(p16->lResult);
2195 break;
2197 case LB_ADDFILE:
2198 case LB_ADDSTRING:
2199 case LB_DIR:
2200 case LB_FINDSTRING:
2201 case LB_FINDSTRINGEXACT:
2202 case LB_INSERTSTRING:
2203 case LB_SELECTSTRING:
2204 case LB_GETTEXT:
2205 case CB_ADDSTRING:
2206 case CB_FINDSTRING:
2207 case CB_FINDSTRINGEXACT:
2208 case CB_INSERTSTRING:
2209 case CB_SELECTSTRING:
2210 case CB_DIR:
2211 case CB_GETLBTEXT:
2212 case WM_SETTEXT:
2213 case WM_WININICHANGE:
2214 case WM_DEVMODECHANGE:
2215 UnMapLS( (SEGPTR)p16->lParam );
2216 break;
2217 case LB_SETTABSTOPS:
2218 case WM_COMPAREITEM:
2219 case WM_DELETEITEM:
2220 case WM_DRAWITEM:
2222 void *ptr = MapSL( p16->lParam );
2223 UnMapLS( p16->lParam );
2224 HeapFree( GetProcessHeap(), 0, ptr );
2226 break;
2227 case WM_COPYDATA:
2229 PCOPYDATASTRUCT16 pcds = MapSL( p16->lParam );
2230 UnMapLS( p16->lParam );
2231 UnMapLS( pcds->lpData );
2232 HeapFree( GetProcessHeap(), 0, pcds );
2234 break;
2235 case CB_GETDROPPEDCONTROLRECT:
2236 case LB_GETITEMRECT:
2238 RECT *r32;
2239 RECT16 *rect = MapSL(p16->lParam);
2240 UnMapLS( p16->lParam );
2241 p16->lParam = *(LPARAM *)(rect + 1);
2242 r32 = (RECT *)p16->lParam;
2243 r32->left = rect->left;
2244 r32->top = rect->top;
2245 r32->right = rect->right;
2246 r32->bottom = rect->bottom;
2247 HeapFree( GetProcessHeap(), 0, rect );
2249 break;
2250 case LB_GETSELITEMS:
2252 INT i;
2253 LPINT16 items = MapSL(p16->lParam);
2254 UnMapLS( p16->lParam );
2255 p16->lParam = *((LPARAM *)items - 1);
2256 for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2257 HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2259 break;
2261 case CB_GETEDITSEL:
2262 if( wParam )
2263 *((PUINT)(wParam)) = LOWORD(p16->lResult);
2264 if( lParam )
2265 *((PUINT)(lParam)) = HIWORD(p16->lResult); /* FIXME: substract 1? */
2266 break;
2268 case WM_MEASUREITEM:
2270 MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2271 MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2272 mis32->itemWidth = mis->itemWidth;
2273 mis32->itemHeight = mis->itemHeight;
2274 UnMapLS( p16->lParam );
2275 HeapFree( GetProcessHeap(), 0, mis );
2277 break;
2278 case WM_GETMINMAXINFO:
2280 MINMAXINFO16 *mmi = MapSL(p16->lParam);
2281 UnMapLS( p16->lParam );
2282 p16->lParam = *(LPARAM *)(mmi + 1);
2283 MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2284 HeapFree( GetProcessHeap(), 0, mmi );
2286 break;
2287 case WM_GETTEXT:
2288 case WM_ASKCBFORMATNAME:
2290 LPSTR str = MapSL(p16->lParam);
2291 UnMapLS( p16->lParam );
2292 p16->lParam = *((LPARAM *)str - 1);
2293 lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2294 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2296 break;
2297 case WM_MDICREATE:
2299 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2300 UnMapLS( cs->szTitle );
2301 UnMapLS( cs->szClass );
2302 UnMapLS( p16->lParam );
2303 HeapFree( GetProcessHeap(), 0, cs );
2305 break;
2306 case WM_MDIGETACTIVE:
2307 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2308 p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2309 break;
2310 case WM_NCCALCSIZE:
2312 NCCALCSIZE_PARAMS *nc32;
2313 NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2314 UnMapLS( p16->lParam );
2315 p16->lParam = *(LPARAM *)(nc + 1);
2316 nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2317 nc32->rgrc[0].left = nc->rgrc[0].left;
2318 nc32->rgrc[0].top = nc->rgrc[0].top;
2319 nc32->rgrc[0].right = nc->rgrc[0].right;
2320 nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2321 if (p16->wParam)
2323 WINDOWPOS16 *pos = MapSL(nc->lppos);
2324 UnMapLS( nc->lppos );
2325 nc32->rgrc[1].left = nc->rgrc[1].left;
2326 nc32->rgrc[1].top = nc->rgrc[1].top;
2327 nc32->rgrc[1].right = nc->rgrc[1].right;
2328 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2329 nc32->rgrc[2].left = nc->rgrc[2].left;
2330 nc32->rgrc[2].top = nc->rgrc[2].top;
2331 nc32->rgrc[2].right = nc->rgrc[2].right;
2332 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2333 WINDOWPOS16to32( pos, nc32->lppos );
2334 HeapFree( GetProcessHeap(), 0, pos );
2336 HeapFree( GetProcessHeap(), 0, nc );
2338 break;
2339 case WM_NCCREATE:
2340 case WM_CREATE:
2342 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2343 UnMapLS( p16->lParam );
2344 UnMapLS( cs->lpszName );
2345 UnMapLS( cs->lpszClass );
2346 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2348 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2349 UnMapLS( cs->lpCreateParams );
2350 UnMapLS( mdi_cs16->szTitle );
2351 UnMapLS( mdi_cs16->szClass );
2352 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2354 HeapFree( GetProcessHeap(), 0, cs );
2356 break;
2357 case WM_WINDOWPOSCHANGING:
2358 case WM_WINDOWPOSCHANGED:
2360 WINDOWPOS16 *wp = MapSL(p16->lParam);
2361 UnMapLS( p16->lParam );
2362 p16->lParam = *(LPARAM *)(wp + 1);
2363 WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2364 HeapFree( GetProcessHeap(), 0, wp );
2366 break;
2367 case WM_NOTIFY:
2368 UnMapLS(p16->lParam);
2369 break;
2370 case WM_GETDLGCODE:
2371 if (p16->lParam)
2373 LPMSG16 msg16 = MapSL(p16->lParam);
2374 UnMapLS( p16->lParam );
2375 HeapFree( GetProcessHeap(), 0, msg16 );
2377 break;
2378 case WM_NEXTMENU:
2380 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2381 next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2382 next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2383 p16->lResult = 0;
2385 break;
2390 /**********************************************************************
2391 * WINPROC_MapMsg32WTo16
2393 * Map a message from 32-bit Unicode to 16-bit.
2394 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2396 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2397 UINT16 *pmsg16, WPARAM16 *pwparam16,
2398 LPARAM *plparam )
2400 BYTE ch;
2401 WCHAR wch;
2403 *pmsg16 = LOWORD(msg32);
2404 *pwparam16 = LOWORD(wParam32);
2405 switch(msg32)
2407 case LB_ADDSTRING:
2408 case LB_FINDSTRING:
2409 case LB_FINDSTRINGEXACT:
2410 case LB_INSERTSTRING:
2411 case LB_SELECTSTRING:
2412 case LB_DIR:
2413 case LB_ADDFILE:
2414 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2415 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2416 return 1;
2418 case CB_ADDSTRING:
2419 case CB_FINDSTRING:
2420 case CB_FINDSTRINGEXACT:
2421 case CB_INSERTSTRING:
2422 case CB_SELECTSTRING:
2423 case CB_DIR:
2424 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2425 *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2426 return 1;
2428 case WM_NCCREATE:
2429 case WM_CREATE:
2431 CREATESTRUCT16 *cs;
2432 CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2434 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2435 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2436 cs->lpszName = map_str_32W_to_16( cs32->lpszName );
2437 cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2439 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2441 MDICREATESTRUCT16 *mdi_cs16;
2442 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2443 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2444 if (!mdi_cs16)
2446 HeapFree(GetProcessHeap(), 0, cs);
2447 return -1;
2449 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2450 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2451 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2452 cs->lpCreateParams = MapLS(mdi_cs16);
2454 *plparam = MapLS(cs);
2456 return 1;
2457 case WM_MDICREATE:
2459 MDICREATESTRUCT16 *cs;
2460 MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2462 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2463 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2464 cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2465 cs->szClass = map_str_32W_to_16( cs32->szClass );
2466 *plparam = MapLS(cs);
2468 return 1;
2469 case WM_SETTEXT:
2470 case WM_WININICHANGE:
2471 case WM_DEVMODECHANGE:
2472 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2473 return 1;
2474 case LB_GETTEXT:
2475 case CB_GETLBTEXT:
2476 if ( WINPROC_TestLBForStr( hwnd, msg32 ))
2478 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2479 if (!str) return -1;
2480 *pmsg16 = (msg32 == LB_GETTEXT) ? LB_GETTEXT16 : CB_GETLBTEXT16;
2481 *plparam = (LPARAM)MapLS(str);
2483 return 1;
2485 case WM_CHARTOITEM:
2486 wch = LOWORD(wParam32);
2487 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2488 *pwparam16 = ch;
2489 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2490 return 0;
2491 case WM_MENUCHAR:
2492 wch = LOWORD(wParam32);
2493 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2494 *pwparam16 = ch;
2495 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2496 return 0;
2497 case WM_CHAR:
2498 case WM_DEADCHAR:
2499 case WM_SYSCHAR:
2500 case WM_SYSDEADCHAR:
2501 wch = wParam32;
2502 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2503 *pwparam16 = ch;
2504 return 0;
2505 case WM_IME_CHAR:
2507 BYTE ch[2];
2509 wch = wParam32;
2510 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
2511 *pwparam16 = (ch[0] << 8) | ch[1];
2512 else
2513 *pwparam16 = ch[0];
2515 return 0;
2517 default: /* No Unicode translation needed (?) */
2518 return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2519 pwparam16, plparam );
2524 /**********************************************************************
2525 * WINPROC_UnmapMsg32WTo16
2527 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2529 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2530 MSGPARAM16* p16 )
2532 switch(msg)
2534 case LB_ADDSTRING:
2535 case LB_FINDSTRING:
2536 case LB_FINDSTRINGEXACT:
2537 case LB_INSERTSTRING:
2538 case LB_SELECTSTRING:
2539 case LB_DIR:
2540 case LB_ADDFILE:
2541 case CB_ADDSTRING:
2542 case CB_FINDSTRING:
2543 case CB_FINDSTRINGEXACT:
2544 case CB_INSERTSTRING:
2545 case CB_SELECTSTRING:
2546 case CB_DIR:
2547 case WM_SETTEXT:
2548 case WM_WININICHANGE:
2549 case WM_DEVMODECHANGE:
2550 unmap_str_32W_to_16( p16->lParam );
2551 break;
2552 case WM_NCCREATE:
2553 case WM_CREATE:
2555 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2556 UnMapLS( p16->lParam );
2557 unmap_str_32W_to_16( cs->lpszName );
2558 unmap_str_32W_to_16( cs->lpszClass );
2560 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2562 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2563 UnMapLS( cs->lpCreateParams );
2564 unmap_str_32W_to_16(mdi_cs16->szTitle);
2565 unmap_str_32W_to_16(mdi_cs16->szClass);
2566 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2568 HeapFree( GetProcessHeap(), 0, cs );
2570 break;
2571 case WM_MDICREATE:
2573 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2574 UnMapLS( p16->lParam );
2575 unmap_str_32W_to_16( cs->szTitle );
2576 unmap_str_32W_to_16( cs->szClass );
2577 HeapFree( GetProcessHeap(), 0, cs );
2579 break;
2580 case WM_GETTEXT:
2581 case WM_ASKCBFORMATNAME:
2583 LPSTR str = MapSL(p16->lParam);
2584 UnMapLS( p16->lParam );
2585 p16->lParam = *((LPARAM *)str - 1);
2586 MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2587 p16->lResult = strlenW( (LPWSTR)p16->lParam );
2588 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2590 break;
2591 case LB_GETTEXT:
2592 case CB_GETLBTEXT:
2593 if ( WINPROC_TestLBForStr( hwnd, msg ))
2595 LPSTR str = MapSL(p16->lParam);
2596 UnMapLS( p16->lParam );
2597 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2598 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2600 break;
2601 default:
2602 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2603 break;
2608 /**********************************************************************
2609 * WINPROC_CallProc32ATo32W
2611 * Call a window procedure, translating args from Ansi to Unicode.
2613 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd, UINT msg, WPARAM wParam,
2614 LPARAM lParam, BOOL dialog )
2616 LRESULT ret;
2617 int unmap;
2619 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2620 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2622 if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2623 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2624 SPY_GetMsgName(msg, hwnd), wParam, lParam );
2625 return 0;
2627 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2628 if (unmap)
2630 if (dialog)
2632 LRESULT result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
2633 result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, NULL );
2634 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2636 else ret = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, ret, func );
2638 return ret;
2642 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
2644 if (size >= need) return static_buffer;
2645 return HeapAlloc( GetProcessHeap(), 0, need );
2648 static inline void free_buffer( void *static_buffer, void *buffer )
2650 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
2653 /**********************************************************************
2654 * WINPROC_CallProc32WTo32A
2656 * Call a window procedure, translating args from Unicode to Ansi.
2658 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd, UINT msg, WPARAM wParam,
2659 LPARAM lParam, BOOL dialog )
2661 LRESULT ret = 0;
2663 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2664 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2666 switch(msg)
2668 case WM_NCCREATE:
2669 case WM_CREATE:
2670 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
2671 * at this point.
2673 char buffer[1024], *cls, *name;
2674 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
2675 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
2676 MDICREATESTRUCTA mdi_cs;
2677 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
2679 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
2680 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
2682 if (csW->lpszName)
2684 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
2685 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
2687 else
2688 name_lenW = name_lenA = 0;
2690 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA + 2 ))) break;
2692 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
2693 cls[class_lenA] = 0;
2694 csA.lpszClass = cls;
2696 if (csW->lpszName)
2698 name = cls + class_lenA + 1;
2699 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
2700 name[name_lenA] = 0;
2701 csA.lpszName = name;
2704 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2706 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
2707 mdi_cs.szTitle = csA.lpszName;
2708 mdi_cs.szClass = csA.lpszClass;
2709 csA.lpCreateParams = &mdi_cs;
2712 ret = WINPROC_CallWndProc(func, hwnd, msg, wParam, (LPARAM)&csA);
2713 free_buffer( buffer, cls );
2715 break;
2717 case WM_GETTEXT:
2718 case WM_ASKCBFORMATNAME:
2720 char *ptr, buffer[512];
2721 DWORD len = wParam * 2;
2723 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
2724 ret = WINPROC_CallWndProc( func, hwnd, msg, len, (LPARAM)ptr );
2725 if (ret && len)
2727 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
2728 ret = len/sizeof(WCHAR) - 1; /* do not count terminating null */
2729 ((LPWSTR)lParam)[ret] = 0;
2730 if (dialog)
2732 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, ret );
2733 ret = TRUE;
2736 free_buffer( buffer, ptr );
2738 break;
2740 case LB_ADDSTRING:
2741 case LB_INSERTSTRING:
2742 case LB_FINDSTRING:
2743 case LB_FINDSTRINGEXACT:
2744 case LB_SELECTSTRING:
2745 case CB_ADDSTRING:
2746 case CB_INSERTSTRING:
2747 case CB_FINDSTRING:
2748 case CB_FINDSTRINGEXACT:
2749 case CB_SELECTSTRING:
2750 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
2752 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2753 break;
2755 /* fall through */
2756 case WM_SETTEXT:
2757 case WM_WININICHANGE:
2758 case WM_DEVMODECHANGE:
2759 case CB_DIR:
2760 case LB_DIR:
2761 case LB_ADDFILE:
2762 case EM_REPLACESEL:
2763 if (!lParam) ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2764 else
2766 char *ptr, buffer[512];
2767 LPCWSTR strW = (LPCWSTR)lParam;
2768 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
2770 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
2771 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
2773 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
2774 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, (LPARAM)ptr );
2775 free_buffer( buffer, ptr );
2778 break;
2780 case WM_MDICREATE:
2782 char *ptr, buffer[1024];
2783 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
2784 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
2785 MDICREATESTRUCTA csA;
2787 memcpy( &csA, csW, sizeof(csA) );
2789 if (HIWORD(csW->szTitle))
2791 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
2792 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
2794 if (HIWORD(csW->szClass))
2796 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
2797 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
2800 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
2802 if (title_lenA)
2804 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
2805 csA.szTitle = ptr;
2807 if (class_lenA)
2809 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
2810 csA.szClass = ptr + title_lenA;
2812 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, (LPARAM)&csA );
2813 free_buffer( buffer, ptr );
2815 break;
2817 case LB_GETTEXT:
2818 case CB_GETLBTEXT:
2819 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
2821 char buffer[512]; /* FIXME: fixed sized buffer */
2822 LRESULT result;
2824 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, (LPARAM)buffer );
2825 result = dialog ? GetWindowLongPtrW( hwnd, DWLP_MSGRESULT ) : ret;
2826 if (result >= 0)
2828 DWORD len;
2829 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
2830 result = len / sizeof(WCHAR) - 1;
2831 if (dialog) SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2832 else ret = result;
2835 else ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2836 break;
2838 case EM_GETLINE:
2840 char *ptr, buffer[512];
2841 WORD len = *(WORD *)lParam;
2842 DWORD result;
2844 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
2845 *((WORD *)ptr) = len * 2; /* store the length */
2846 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, (LPARAM)ptr );
2847 result = dialog ? GetWindowLongPtrW( hwnd, DWLP_MSGRESULT ) : ret;
2848 if (result)
2850 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &result, buffer, result );
2851 result /= sizeof(WCHAR);
2852 if (result < len) ((LPWSTR)lParam)[result] = 0;
2853 if (dialog) SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2854 else ret = result;
2856 free_buffer( buffer, ptr );
2858 break;
2860 case WM_CHARTOITEM:
2861 case WM_MENUCHAR:
2862 case WM_CHAR:
2863 case WM_DEADCHAR:
2864 case WM_SYSCHAR:
2865 case WM_SYSDEADCHAR:
2866 case EM_SETPASSWORDCHAR:
2868 WCHAR wch = LOWORD(wParam);
2869 BYTE ch;
2870 RtlUnicodeToMultiByteN( (LPSTR)&ch, 1, NULL, &wch, sizeof(WCHAR) );
2871 wParam = MAKEWPARAM( ch, HIWORD(wParam) );
2872 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2874 break;
2876 case WM_IME_CHAR:
2878 WCHAR wch = LOWORD(wParam);
2879 BYTE ch[2];
2880 DWORD len;
2882 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, &wch, sizeof(WCHAR) );
2883 if (len == 2)
2884 wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
2885 else
2886 wParam = MAKEWPARAM( ch[0], HIWORD(wParam) );
2887 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2889 break;
2891 case WM_PAINTCLIPBOARD:
2892 case WM_SIZECLIPBOARD:
2893 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
2894 SPY_GetMsgName(msg, hwnd), msg );
2895 break;
2897 default:
2898 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2899 break;
2902 return ret;
2906 /**********************************************************************
2907 * WINPROC_CallProc16To32A
2909 static LRESULT WINPROC_CallProc16To32A( WNDPROC func, HWND16 hwnd, UINT16 msg,
2910 WPARAM16 wParam, LPARAM lParam, BOOL dialog )
2912 LRESULT ret;
2913 UINT msg32;
2914 WPARAM wParam32;
2915 HWND hwnd32 = WIN_Handle32( hwnd );
2917 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2918 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
2920 if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
2921 return 0;
2922 ret = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
2923 if (dialog)
2925 LRESULT result = GetWindowLongPtrW( hwnd32, DWLP_MSGRESULT );
2926 result = WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
2927 SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, result );
2929 else ret = WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, ret );
2931 return ret;
2935 /**********************************************************************
2936 * WINPROC_CallProc16To32W
2938 static LRESULT WINPROC_CallProc16To32W( WNDPROC func, HWND16 hwnd, UINT16 msg,
2939 WPARAM16 wParam, LPARAM lParam, BOOL dialog )
2941 LRESULT ret;
2942 UINT msg32;
2943 WPARAM wParam32;
2944 HWND hwnd32 = WIN_Handle32( hwnd );
2946 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2947 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
2949 if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
2950 return 0;
2952 ret = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
2953 if (dialog)
2955 LRESULT result = GetWindowLongPtrW( hwnd32, DWLP_MSGRESULT );
2956 result = WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result, NULL );
2957 SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, result );
2959 else ret = WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, ret, func );
2961 return ret;
2965 /**********************************************************************
2966 * __wine_call_wndproc (USER.1010)
2968 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
2969 WINDOWPROC *proc )
2971 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
2972 else return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
2976 /**********************************************************************
2977 * WINPROC_CallProc32ATo16
2979 * Call a 16-bit window procedure, translating the 32-bit args.
2981 static LRESULT WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd, UINT msg,
2982 WPARAM wParam, LPARAM lParam, BOOL dialog )
2984 LRESULT ret;
2985 UINT16 msg16;
2986 MSGPARAM16 mp16;
2988 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2989 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2991 mp16.lParam = lParam;
2992 if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
2993 return 0;
2994 ret = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16, mp16.wParam, mp16.lParam );
2995 if (dialog)
2997 mp16.lResult = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
2998 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
2999 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, mp16.lResult );
3000 ret = LOWORD(ret);
3002 else
3004 mp16.lResult = ret;
3005 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3006 ret = mp16.lResult;
3008 return ret;
3012 /**********************************************************************
3013 * WINPROC_CallProc32WTo16
3015 * Call a 16-bit window procedure, translating the 32-bit args.
3017 static LRESULT WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd, UINT msg,
3018 WPARAM wParam, LPARAM lParam, BOOL dialog )
3020 LRESULT ret;
3021 UINT16 msg16;
3022 MSGPARAM16 mp16;
3024 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3025 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3027 mp16.lParam = lParam;
3028 if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3029 return 0;
3030 ret = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16, mp16.wParam, mp16.lParam );
3031 if (dialog)
3033 mp16.lResult = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
3034 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3035 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, mp16.lResult );
3036 ret = LOWORD(ret);
3038 else
3040 mp16.lResult = ret;
3041 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3042 ret = mp16.lResult;
3044 return ret;
3048 /**********************************************************************
3049 * CallWindowProc (USER.122)
3051 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3052 WPARAM16 wParam, LPARAM lParam )
3054 WINDOWPROC *proc;
3056 if (!func) return 0;
3058 if (!(proc = handle16_to_proc( func )))
3059 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3061 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
3062 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
3063 return WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam );
3067 /**********************************************************************
3068 * CallWindowProcA (USER32.@)
3070 * The CallWindowProc() function invokes the windows procedure _func_,
3071 * with _hwnd_ as the target window, the message specified by _msg_, and
3072 * the message parameters _wParam_ and _lParam_.
3074 * Some kinds of argument conversion may be done, I'm not sure what.
3076 * CallWindowProc() may be used for windows subclassing. Use
3077 * SetWindowLong() to set a new windows procedure for windows of the
3078 * subclass, and handle subclassed messages in the new windows
3079 * procedure. The new windows procedure may then use CallWindowProc()
3080 * with _func_ set to the parent class's windows procedure to dispatch
3081 * the message to the superclass.
3083 * RETURNS
3085 * The return value is message dependent.
3087 * CONFORMANCE
3089 * ECMA-234, Win32
3091 LRESULT WINAPI CallWindowProcA(
3092 WNDPROC func, /* [in] window procedure */
3093 HWND hwnd, /* [in] target window */
3094 UINT msg, /* [in] message */
3095 WPARAM wParam, /* [in] message dependent parameter */
3096 LPARAM lParam /* [in] message dependent parameter */
3098 WINDOWPROC *proc;
3100 if (!func) return 0;
3102 if (!(proc = handle_to_proc( func )))
3103 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3105 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3106 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
3107 return WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam, FALSE );
3111 /**********************************************************************
3112 * CallWindowProcW (USER32.@)
3114 * See CallWindowProcA.
3116 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3117 WPARAM wParam, LPARAM lParam )
3119 WINDOWPROC *proc;
3121 if (!func) return 0;
3123 if (!(proc = handle_to_proc( func )))
3124 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3126 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3127 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
3128 return WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam, FALSE );
3132 /**********************************************************************
3133 * WINPROC_CallDlgProc16
3135 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
3137 WINDOWPROC *proc;
3139 if (!func) return 0;
3141 if (!(proc = handle16_to_proc( (WNDPROC16)func )))
3142 return LOWORD( WINPROC_CallWndProc16( (WNDPROC16)func, hwnd, msg, wParam, lParam ) );
3144 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, TRUE );
3145 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, TRUE );
3146 return LOWORD( WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam ) );
3150 /**********************************************************************
3151 * WINPROC_CallDlgProcA
3153 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3155 WINDOWPROC *proc;
3157 if (!func) return 0;
3159 if (!(proc = handle_to_proc( (WNDPROC)func )))
3160 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3162 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3163 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam, TRUE );
3164 return WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam, TRUE );
3168 /**********************************************************************
3169 * WINPROC_CallDlgProcW
3171 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3173 WINDOWPROC *proc;
3175 if (!func) return 0;
3177 if (!(proc = handle_to_proc( (WNDPROC)func )))
3178 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3180 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3181 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam, TRUE );
3182 return WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam, TRUE );