user: Support storing multiple winprocs in a single winproc handle.
[wine/multimedia.git] / dlls / user / winproc.c
blob6e012cc5c755df2c60ebed6ce95fbbd7c39cf3d6
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_TestCBForStr
595 * Return TRUE if the lparam is a string
597 inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
599 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
600 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
602 /**********************************************************************
603 * WINPROC_TestLBForStr
605 * Return TRUE if the lparam is a string
607 inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
609 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
610 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
613 /**********************************************************************
614 * WINPROC_MapMsg32ATo32W
616 * Map a message from Ansi to Unicode.
617 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
619 * FIXME:
620 * WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
621 * the first four bytes are the handle of the icon
622 * when the WM_SETTEXT message has been used to set the icon
624 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
626 switch(msg)
628 case WM_GETTEXT:
629 case WM_ASKCBFORMATNAME:
631 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0,
632 *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
633 if (!ptr) return -1;
634 *ptr++ = *plparam; /* Store previous lParam */
635 *plparam = (LPARAM)ptr;
637 return 1;
638 /* lparam is string (0-terminated) */
639 case WM_SETTEXT:
640 case WM_WININICHANGE:
641 case WM_DEVMODECHANGE:
642 case CB_DIR:
643 case LB_DIR:
644 case LB_ADDFILE:
645 case EM_REPLACESEL:
646 if (!*plparam) return 0;
647 else
649 DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
650 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
651 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
652 *plparam = (LPARAM)buf;
653 return (*plparam ? 1 : -1);
655 case WM_GETTEXTLENGTH:
656 case CB_GETLBTEXTLEN:
657 case LB_GETTEXTLEN:
658 return 1; /* need to map result */
659 case WM_NCCREATE:
660 case WM_CREATE:
662 UNICODE_STRING usBuffer;
663 struct s
664 { CREATESTRUCTW cs; /* new structure */
665 LPCWSTR lpszName; /* allocated Name */
666 LPCWSTR lpszClass; /* allocated Class */
669 struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
670 if (!xs) return -1;
671 xs->cs = *(CREATESTRUCTW *)*plparam;
672 if (HIWORD(xs->cs.lpszName))
674 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
675 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
677 if (HIWORD(xs->cs.lpszClass))
679 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
680 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
683 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
685 MDICREATESTRUCTW *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
686 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
687 if (HIWORD(mdi_cs->szTitle))
689 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
690 mdi_cs->szTitle = usBuffer.Buffer;
692 if (HIWORD(mdi_cs->szClass))
694 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
695 mdi_cs->szClass = usBuffer.Buffer;
697 xs->cs.lpCreateParams = mdi_cs;
700 *plparam = (LPARAM)xs;
702 return 1;
703 case WM_MDICREATE:
705 MDICREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
706 if (!cs) return -1;
707 *cs = *(MDICREATESTRUCTW *)*plparam;
708 if (HIWORD(cs->szClass))
710 UNICODE_STRING usBuffer;
711 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
712 cs->szClass = usBuffer.Buffer;
714 if (HIWORD(cs->szTitle))
716 UNICODE_STRING usBuffer;
717 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
718 cs->szTitle = usBuffer.Buffer;
720 *plparam = (LPARAM)cs;
722 return 1;
724 /* Listbox */
725 case LB_ADDSTRING:
726 case LB_INSERTSTRING:
727 case LB_FINDSTRING:
728 case LB_FINDSTRINGEXACT:
729 case LB_SELECTSTRING:
730 if(!*plparam) return 0;
731 if ( WINPROC_TestLBForStr( hwnd ))
733 UNICODE_STRING usBuffer;
734 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
735 *plparam = (LPARAM)usBuffer.Buffer;
737 return (*plparam ? 1 : -1);
739 case LB_GETTEXT: /* FIXME: fixed sized buffer */
740 { if ( WINPROC_TestLBForStr( hwnd ))
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;
747 return 1;
749 /* Combobox */
750 case CB_ADDSTRING:
751 case CB_INSERTSTRING:
752 case CB_FINDSTRINGEXACT:
753 case CB_FINDSTRING:
754 case CB_SELECTSTRING:
755 if(!*plparam) return 0;
756 if ( WINPROC_TestCBForStr( hwnd ))
758 UNICODE_STRING usBuffer;
759 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
760 *plparam = (LPARAM)usBuffer.Buffer;
762 return (*plparam ? 1 : -1);
764 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
765 { if ( WINPROC_TestCBForStr( hwnd ))
766 { LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 * sizeof(WCHAR) + sizeof(LPARAM) );
767 if (!ptr) return -1;
768 *ptr++ = *plparam; /* Store previous lParam */
769 *plparam = (LPARAM)ptr;
772 return 1;
774 /* Multiline edit */
775 case EM_GETLINE:
776 { WORD len = (WORD)*plparam;
777 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
778 if (!ptr) return -1;
779 *ptr++ = *plparam; /* Store previous lParam */
780 *((WORD *) ptr) = len; /* Store the length */
781 *plparam = (LPARAM)ptr;
783 return 1;
785 case WM_CHARTOITEM:
786 case WM_MENUCHAR:
787 case WM_CHAR:
788 case WM_DEADCHAR:
789 case WM_SYSCHAR:
790 case WM_SYSDEADCHAR:
791 case EM_SETPASSWORDCHAR:
793 CHAR ch = LOWORD(*pwparam);
794 WCHAR wch;
795 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
796 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
798 return 0;
800 case WM_IME_CHAR:
802 CHAR ch[2];
803 WCHAR wch;
804 ch[0] = (*pwparam >> 8);
805 ch[1] = *pwparam & 0xff;
806 if (ch[0])
807 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
808 else
809 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
810 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
812 return 0;
814 case WM_PAINTCLIPBOARD:
815 case WM_SIZECLIPBOARD:
816 FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
817 return -1;
818 default: /* No translation needed */
819 return 0;
824 /**********************************************************************
825 * WINPROC_UnmapMsg32ATo32W
827 * Unmap a message that was mapped from Ansi to Unicode.
829 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
830 LRESULT result, WNDPROC dispatch )
832 switch(msg)
834 case WM_GETTEXT:
835 case WM_ASKCBFORMATNAME:
837 LPARAM *ptr = (LPARAM *)lParam - 1;
838 if (!wParam) result = 0;
839 else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
840 (LPSTR)*ptr, wParam, NULL, NULL )))
842 ((LPSTR)*ptr)[wParam-1] = 0;
843 result = wParam - 1;
845 else result--; /* do not count terminating null */
846 HeapFree( GetProcessHeap(), 0, ptr );
848 break;
849 case WM_GETTEXTLENGTH:
850 case CB_GETLBTEXTLEN:
851 case LB_GETTEXTLEN:
852 if (result >= 0)
854 /* Determine respective GETTEXT message */
855 UINT msgGetText =
856 (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
857 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
858 /* wParam differs between the messages */
859 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? (WPARAM)(result + 1) : wParam;
861 WCHAR* p = HeapAlloc (GetProcessHeap(), 0, (result + 1) * sizeof(WCHAR));
863 if (p)
865 LRESULT n;
867 if (dispatch)
868 n = WINPROC_CallWndProc(dispatch, hwnd, msgGetText, wp, (LPARAM)p);
869 else
870 n = SendMessageW (hwnd, msgGetText, wp, (LPARAM)p);
872 result = WideCharToMultiByte( CP_ACP, 0, p, n, NULL, 0, 0, NULL );
873 HeapFree (GetProcessHeap(), 0, p);
876 break;
877 case WM_NCCREATE:
878 case WM_CREATE:
880 struct s
881 { CREATESTRUCTW cs; /* new structure */
882 LPWSTR lpszName; /* allocated Name */
883 LPWSTR lpszClass; /* allocated Class */
885 struct s *xs = (struct s *)lParam;
886 HeapFree( GetProcessHeap(), 0, xs->lpszName );
887 HeapFree( GetProcessHeap(), 0, xs->lpszClass );
889 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
891 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
892 if (HIWORD(mdi_cs->szTitle))
893 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
894 if (HIWORD(mdi_cs->szClass))
895 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
896 HeapFree(GetProcessHeap(), 0, mdi_cs);
898 HeapFree( GetProcessHeap(), 0, xs );
900 break;
902 case WM_MDICREATE:
904 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
905 if (HIWORD(cs->szTitle))
906 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
907 if (HIWORD(cs->szClass))
908 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
909 HeapFree( GetProcessHeap(), 0, cs );
911 break;
913 case WM_SETTEXT:
914 case WM_WININICHANGE:
915 case WM_DEVMODECHANGE:
916 case CB_DIR:
917 case LB_DIR:
918 case LB_ADDFILE:
919 case EM_REPLACESEL:
920 HeapFree( GetProcessHeap(), 0, (void *)lParam );
921 break;
923 /* Listbox */
924 case LB_ADDSTRING:
925 case LB_INSERTSTRING:
926 case LB_FINDSTRING:
927 case LB_FINDSTRINGEXACT:
928 case LB_SELECTSTRING:
929 if ( WINPROC_TestLBForStr( hwnd ))
930 HeapFree( GetProcessHeap(), 0, (void *)lParam );
931 break;
933 case LB_GETTEXT:
934 if ( WINPROC_TestLBForStr( hwnd ))
936 LPARAM *ptr = (LPARAM *)lParam - 1;
937 if (result >= 0)
938 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
939 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
940 HeapFree( GetProcessHeap(), 0, ptr );
942 break;
944 /* Combobox */
945 case CB_ADDSTRING:
946 case CB_INSERTSTRING:
947 case CB_FINDSTRING:
948 case CB_FINDSTRINGEXACT:
949 case CB_SELECTSTRING:
950 if ( WINPROC_TestCBForStr( hwnd ))
951 HeapFree( GetProcessHeap(), 0, (void *)lParam );
952 break;
954 case CB_GETLBTEXT:
955 if ( result < 0) /* CB_ERR and CB_ERRSPACE */
957 LPARAM *ptr = (LPARAM *)lParam - 1;
958 HeapFree( GetProcessHeap(), 0, ptr );
960 else if ( WINPROC_TestCBForStr( hwnd ))
962 LPARAM *ptr = (LPARAM *)lParam - 1;
963 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
964 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
965 HeapFree( GetProcessHeap(), 0, ptr );
967 break;
969 /* Multiline edit */
970 case EM_GETLINE:
972 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lParam */
973 WORD len = *(WORD *) lParam;
974 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
975 (LPSTR)*ptr, len, NULL, NULL );
976 if (result < len) ((LPSTR)*ptr)[result] = 0;
977 HeapFree( GetProcessHeap(), 0, ptr );
979 break;
981 return result;
985 /**********************************************************************
986 * WINPROC_MapMsg32WTo32A
988 * Map a message from Unicode to Ansi.
989 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
991 static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
993 switch(msg)
995 case WM_GETTEXT:
996 case WM_ASKCBFORMATNAME:
998 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, *pwparam + sizeof(LPARAM) );
999 if (!ptr) return -1;
1000 *ptr++ = *plparam; /* Store previous lParam */
1001 *plparam = (LPARAM)ptr;
1003 return 1;
1005 case WM_SETTEXT:
1006 case WM_WININICHANGE:
1007 case WM_DEVMODECHANGE:
1008 case CB_DIR:
1009 case LB_DIR:
1010 case LB_ADDFILE:
1011 case EM_REPLACESEL:
1012 if (*plparam)
1014 LPCWSTR str = (LPCWSTR)*plparam;
1015 int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, 0, 0);
1016 *plparam = (LPARAM)HeapAlloc(GetProcessHeap(), 0, len);
1017 if (!*plparam) return -1;
1018 WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)*plparam, len, 0, 0);
1019 return 1;
1021 return 0;
1023 case WM_MDICREATE:
1025 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1026 if (!cs) return -1;
1027 *cs = *(MDICREATESTRUCTA *)*plparam;
1028 if (HIWORD(cs->szTitle))
1030 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, NULL, 0, 0, 0);
1031 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1032 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, buf, len, 0, 0);
1033 cs->szTitle = buf;
1035 if (HIWORD(cs->szClass))
1037 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, NULL, 0, 0, 0);
1038 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1039 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, buf, len, 0, 0);
1040 cs->szClass = buf;
1042 *plparam = (LPARAM)cs;
1044 return 1;
1046 /* Listbox */
1047 case LB_ADDSTRING:
1048 case LB_INSERTSTRING:
1049 case LB_FINDSTRING:
1050 case LB_FINDSTRINGEXACT:
1051 case LB_SELECTSTRING:
1052 if(!*plparam) return 0;
1053 if ( WINPROC_TestLBForStr( hwnd ))
1055 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1056 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1057 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1058 *plparam = (LPARAM)buf;
1060 return (*plparam ? 1 : -1);
1062 case LB_GETTEXT: /* FIXME: fixed sized buffer */
1063 { if ( WINPROC_TestLBForStr( hwnd ))
1064 { LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1065 if (!ptr) return -1;
1066 *ptr++ = *plparam; /* Store previous lParam */
1067 *plparam = (LPARAM)ptr;
1070 return 1;
1072 /* Combobox */
1073 case CB_ADDSTRING:
1074 case CB_INSERTSTRING:
1075 case CB_FINDSTRING:
1076 case CB_FINDSTRINGEXACT:
1077 case CB_SELECTSTRING:
1078 if(!*plparam) return 0;
1079 if ( WINPROC_TestCBForStr( hwnd ))
1081 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1082 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1083 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1084 *plparam = (LPARAM)buf;
1086 return (*plparam ? 1 : -1);
1088 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
1089 { if ( WINPROC_TestCBForStr( hwnd ))
1090 { LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1091 if (!ptr) return -1;
1092 *ptr++ = *plparam; /* Store previous lParam */
1093 *plparam = (LPARAM)ptr;
1096 return 1;
1098 /* Multiline edit */
1099 case EM_GETLINE:
1100 { WORD len = (WORD)*plparam;
1101 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1102 if (!ptr) return -1;
1103 *ptr++ = *plparam; /* Store previous lParam */
1104 *((WORD *) ptr) = len; /* Store the length */
1105 *plparam = (LPARAM)ptr;
1107 return 1;
1109 case WM_CHARTOITEM:
1110 case WM_MENUCHAR:
1111 case WM_CHAR:
1112 case WM_DEADCHAR:
1113 case WM_SYSCHAR:
1114 case WM_SYSDEADCHAR:
1115 case EM_SETPASSWORDCHAR:
1117 WCHAR wch = LOWORD(*pwparam);
1118 BYTE ch;
1119 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
1120 *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1122 return 0;
1124 case WM_IME_CHAR:
1126 WCHAR wch = LOWORD(*pwparam);
1127 BYTE ch[2];
1129 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
1130 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1131 else
1132 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1134 return 0;
1136 case WM_PAINTCLIPBOARD:
1137 case WM_SIZECLIPBOARD:
1138 FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1139 return -1;
1140 default: /* No translation needed */
1141 return 0;
1146 /**********************************************************************
1147 * WINPROC_UnmapMsg32WTo32A
1149 * Unmap a message that was mapped from Unicode to Ansi.
1151 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1153 switch(msg)
1155 case WM_GETTEXT:
1156 case WM_ASKCBFORMATNAME:
1158 LPARAM *ptr = (LPARAM *)lParam - 1;
1159 if (!wParam) result = 0;
1160 else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
1161 (LPWSTR)*ptr, wParam )))
1163 ((LPWSTR)*ptr)[wParam-1] = 0;
1164 result = wParam - 1;
1166 else result--; /* do not count terminating null */
1167 HeapFree( GetProcessHeap(), 0, ptr );
1169 break;
1171 case WM_SETTEXT:
1172 case WM_WININICHANGE:
1173 case WM_DEVMODECHANGE:
1174 case CB_DIR:
1175 case LB_DIR:
1176 case LB_ADDFILE:
1177 case EM_REPLACESEL:
1178 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1179 break;
1181 case WM_MDICREATE:
1183 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1184 if (HIWORD(cs->szTitle))
1185 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1186 if (HIWORD(cs->szClass))
1187 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1188 HeapFree( GetProcessHeap(), 0, cs );
1190 break;
1192 /* Listbox */
1193 case LB_ADDSTRING:
1194 case LB_INSERTSTRING:
1195 case LB_FINDSTRING:
1196 case LB_FINDSTRINGEXACT:
1197 case LB_SELECTSTRING:
1198 if ( WINPROC_TestLBForStr( hwnd ))
1199 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1200 break;
1202 case LB_GETTEXT:
1203 if ( WINPROC_TestLBForStr( hwnd ))
1205 LPARAM *ptr = (LPARAM *)lParam - 1;
1206 if (result >= 0)
1207 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1208 HeapFree( GetProcessHeap(), 0, ptr );
1210 break;
1212 /* Combobox */
1213 case CB_ADDSTRING:
1214 case CB_INSERTSTRING:
1215 case CB_FINDSTRING:
1216 case CB_FINDSTRINGEXACT:
1217 case CB_SELECTSTRING:
1218 if ( WINPROC_TestCBForStr( hwnd ))
1219 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1220 break;
1222 case CB_GETLBTEXT:
1223 if ( result < 0) /* CB_ERR and CB_ERRSPACE */
1225 LPARAM *ptr = (LPARAM *)lParam - 1;
1226 HeapFree( GetProcessHeap(), 0, ptr );
1228 else if ( WINPROC_TestCBForStr( hwnd ))
1230 LPARAM *ptr = (LPARAM *)lParam - 1;
1231 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1232 HeapFree( GetProcessHeap(), 0, ptr );
1234 break;
1236 /* Multiline edit */
1237 case EM_GETLINE:
1239 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lparam */
1240 WORD len = *(WORD *)ptr;
1241 if (len)
1243 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1244 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1246 HeapFree( GetProcessHeap(), 0, ptr );
1248 break;
1250 return result;
1253 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1255 HANDLE dst;
1256 UINT sz = GlobalSize16(src);
1257 LPSTR ptr16, ptr32;
1259 if (!(dst = GlobalAlloc(flags, sz)))
1260 return 0;
1261 ptr16 = GlobalLock16(src);
1262 ptr32 = GlobalLock(dst);
1263 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1264 GlobalUnlock16(src);
1265 GlobalUnlock(dst);
1267 return (UINT)dst;
1270 /**********************************************************************
1271 * WINPROC_MapMsg16To32A
1273 * Map a message from 16- to 32-bit Ansi.
1274 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1276 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1277 WPARAM *pwparam32, LPARAM *plparam )
1279 *pmsg32 = (UINT)msg16;
1280 *pwparam32 = (WPARAM)wParam16;
1281 switch(msg16)
1283 case WM_ACTIVATE:
1284 case WM_CHARTOITEM:
1285 case WM_COMMAND:
1286 case WM_VKEYTOITEM:
1287 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1288 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1289 return 0;
1290 case WM_HSCROLL:
1291 case WM_VSCROLL:
1292 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1293 *plparam = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1294 return 0;
1295 case WM_CTLCOLOR:
1296 if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1297 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1298 *pwparam32 = (WPARAM)HDC_32(wParam16);
1299 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1300 return 0;
1301 case WM_COMPAREITEM:
1303 COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1304 COMPAREITEMSTRUCT *cis = HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1305 if (!cis) return -1;
1306 cis->CtlType = cis16->CtlType;
1307 cis->CtlID = cis16->CtlID;
1308 cis->hwndItem = WIN_Handle32( cis16->hwndItem );
1309 cis->itemID1 = cis16->itemID1;
1310 cis->itemData1 = cis16->itemData1;
1311 cis->itemID2 = cis16->itemID2;
1312 cis->itemData2 = cis16->itemData2;
1313 cis->dwLocaleId = 0; /* FIXME */
1314 *plparam = (LPARAM)cis;
1316 return 1;
1317 case WM_COPYDATA:
1319 PCOPYDATASTRUCT16 pcds16 = MapSL(*plparam);
1320 PCOPYDATASTRUCT pcds = HeapAlloc ( GetProcessHeap(), 0, sizeof(*pcds));
1321 pcds->dwData = pcds16->dwData;
1322 pcds->cbData = pcds16->cbData;
1323 pcds->lpData = MapSL( pcds16->lpData);
1324 *plparam = (LPARAM)pcds;
1326 return 1;
1327 case WM_DELETEITEM:
1329 DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1330 DELETEITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1331 if (!dis) return -1;
1332 dis->CtlType = dis16->CtlType;
1333 dis->CtlID = dis16->CtlID;
1334 dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1335 dis->itemData = dis16->itemData;
1336 *plparam = (LPARAM)dis;
1338 return 1;
1339 case WM_MEASUREITEM:
1341 MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1342 MEASUREITEMSTRUCT *mis = HeapAlloc(GetProcessHeap(), 0,
1343 sizeof(*mis) + sizeof(LPARAM));
1344 if (!mis) return -1;
1345 mis->CtlType = mis16->CtlType;
1346 mis->CtlID = mis16->CtlID;
1347 mis->itemID = mis16->itemID;
1348 mis->itemWidth = mis16->itemWidth;
1349 mis->itemHeight = mis16->itemHeight;
1350 mis->itemData = mis16->itemData;
1351 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1352 *plparam = (LPARAM)mis;
1354 return 1;
1355 case WM_DRAWITEM:
1357 DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1358 DRAWITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1359 if (!dis) return -1;
1360 dis->CtlType = dis16->CtlType;
1361 dis->CtlID = dis16->CtlID;
1362 dis->itemID = dis16->itemID;
1363 dis->itemAction = dis16->itemAction;
1364 dis->itemState = dis16->itemState;
1365 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1366 : WIN_Handle32( dis16->hwndItem );
1367 dis->hDC = HDC_32(dis16->hDC);
1368 dis->itemData = dis16->itemData;
1369 dis->rcItem.left = dis16->rcItem.left;
1370 dis->rcItem.top = dis16->rcItem.top;
1371 dis->rcItem.right = dis16->rcItem.right;
1372 dis->rcItem.bottom = dis16->rcItem.bottom;
1373 *plparam = (LPARAM)dis;
1375 return 1;
1376 case WM_GETMINMAXINFO:
1378 MINMAXINFO *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM));
1379 if (!mmi) return -1;
1380 MINMAXINFO16to32( MapSL(*plparam), mmi );
1381 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1382 *plparam = (LPARAM)mmi;
1384 return 1;
1385 case WM_GETTEXT:
1386 case WM_SETTEXT:
1387 case WM_WININICHANGE:
1388 case WM_DEVMODECHANGE:
1389 case WM_ASKCBFORMATNAME:
1390 *plparam = (LPARAM)MapSL(*plparam);
1391 return 0;
1392 case WM_MDICREATE:
1394 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1395 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1396 if (!cs) return -1;
1397 MDICREATESTRUCT16to32A( cs16, cs );
1398 cs->szTitle = MapSL(cs16->szTitle);
1399 cs->szClass = MapSL(cs16->szClass);
1400 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1401 *plparam = (LPARAM)cs;
1403 return 1;
1404 case WM_MDIGETACTIVE:
1405 *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1406 *(BOOL*)(*plparam) = 0;
1407 return 1;
1408 case WM_MDISETMENU:
1409 if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1410 *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1411 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1412 return 0;
1413 case WM_MENUCHAR:
1414 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1415 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1416 return 0;
1417 case WM_MENUSELECT:
1418 if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1420 HMENU hmenu=HMENU_32(HIWORD(*plparam));
1421 UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1422 if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1423 *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1425 else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1426 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1427 return 0;
1428 case WM_MDIACTIVATE:
1429 if( *plparam )
1431 *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1432 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1434 else /* message sent to MDI client */
1435 *pwparam32 = wParam16;
1436 return 0;
1437 case WM_NCCALCSIZE:
1439 NCCALCSIZE_PARAMS16 *nc16;
1440 NCCALCSIZE_PARAMS *nc;
1442 nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM) );
1443 if (!nc) return -1;
1444 nc16 = MapSL(*plparam);
1445 nc->rgrc[0].left = nc16->rgrc[0].left;
1446 nc->rgrc[0].top = nc16->rgrc[0].top;
1447 nc->rgrc[0].right = nc16->rgrc[0].right;
1448 nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1449 if (wParam16)
1451 nc->lppos = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc->lppos) );
1452 nc->rgrc[1].left = nc16->rgrc[1].left;
1453 nc->rgrc[1].top = nc16->rgrc[1].top;
1454 nc->rgrc[1].right = nc16->rgrc[1].right;
1455 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1456 nc->rgrc[2].left = nc16->rgrc[2].left;
1457 nc->rgrc[2].top = nc16->rgrc[2].top;
1458 nc->rgrc[2].right = nc16->rgrc[2].right;
1459 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1460 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1462 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1463 *plparam = (LPARAM)nc;
1465 return 1;
1466 case WM_NCCREATE:
1467 case WM_CREATE:
1469 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1470 CREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1471 if (!cs) return -1;
1472 CREATESTRUCT16to32A( cs16, cs );
1473 cs->lpszName = MapSL(cs16->lpszName);
1474 cs->lpszClass = MapSL(cs16->lpszClass);
1476 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1478 MDICREATESTRUCT16 *mdi_cs16;
1479 MDICREATESTRUCTA *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1480 if (!mdi_cs)
1482 HeapFree(GetProcessHeap(), 0, cs);
1483 return -1;
1485 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1486 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1487 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1488 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1490 cs->lpCreateParams = mdi_cs;
1492 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1493 *plparam = (LPARAM)cs;
1495 return 1;
1496 case WM_PARENTNOTIFY:
1497 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1499 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1500 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1502 return 0;
1503 case WM_WINDOWPOSCHANGING:
1504 case WM_WINDOWPOSCHANGED:
1506 WINDOWPOS *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
1507 if (!wp) return -1;
1508 WINDOWPOS16to32( MapSL(*plparam), wp );
1509 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1510 *plparam = (LPARAM)wp;
1512 return 1;
1513 case WM_GETDLGCODE:
1514 if (*plparam)
1516 LPMSG16 msg16 = MapSL(*plparam);
1517 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1519 if (!msg32) return -1;
1520 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1521 msg32->lParam = msg16->lParam;
1522 msg32->time = msg16->time;
1523 msg32->pt.x = msg16->pt.x;
1524 msg32->pt.y = msg16->pt.y;
1525 /* this is right, right? */
1526 if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1527 &msg32->message,&msg32->wParam,
1528 &msg32->lParam)<0) {
1529 HeapFree( GetProcessHeap(), 0, msg32 );
1530 return -1;
1532 *plparam = (LPARAM)msg32;
1533 return 1;
1535 else return 0;
1536 case WM_NOTIFY:
1537 *plparam = (LPARAM)MapSL(*plparam);
1538 return 0;
1539 case WM_ACTIVATEAPP:
1540 /* We need this when SetActiveWindow sends a Sendmessage16() to
1541 * a 32bit window. Might be superflous with 32bit interprocess
1542 * message queues. */
1543 if (*plparam) *plparam = HTASK_32( *plparam );
1544 return 0;
1545 case WM_NEXTMENU:
1547 MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1548 if (!next) return -1;
1549 next->hmenuIn = (HMENU)*plparam;
1550 next->hmenuNext = 0;
1551 next->hwndNext = 0;
1552 *plparam = (LPARAM)next;
1553 return 1;
1555 case WM_PAINTCLIPBOARD:
1556 case WM_SIZECLIPBOARD:
1557 FIXME_(msg)("message %04x needs translation\n",msg16 );
1558 return -1;
1559 case WM_DDE_INITIATE:
1560 case WM_DDE_TERMINATE:
1561 case WM_DDE_UNADVISE:
1562 case WM_DDE_REQUEST:
1563 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1564 return 0;
1565 case WM_DDE_ADVISE:
1566 case WM_DDE_DATA:
1567 case WM_DDE_POKE:
1569 HANDLE16 lo16;
1570 ATOM hi;
1571 UINT lo32 = 0;
1573 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1574 lo16 = LOWORD(*plparam);
1575 hi = HIWORD(*plparam);
1576 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1577 return -1;
1578 *plparam = PackDDElParam(msg16, lo32, hi);
1580 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1581 case WM_DDE_ACK:
1583 UINT lo, hi;
1584 int flag = 0;
1585 char buf[2];
1587 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1589 lo = LOWORD(*plparam);
1590 hi = HIWORD(*plparam);
1592 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1593 if (GlobalSize16(hi) != 0) flag |= 2;
1594 switch (flag)
1596 case 0:
1597 if (hi)
1599 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1600 hi = 0;
1602 break;
1603 case 1:
1604 break; /* atom, nothing to do */
1605 case 3:
1606 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1607 /* fall thru */
1608 case 2:
1609 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1610 break;
1612 *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1614 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1615 case WM_DDE_EXECUTE:
1616 *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1617 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1618 default: /* No translation needed */
1619 return 0;
1624 /**********************************************************************
1625 * WINPROC_UnmapMsg16To32A
1627 * Unmap a message that was mapped from 16- to 32-bit Ansi.
1629 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1630 LRESULT result )
1632 switch(msg)
1634 case WM_COMPAREITEM:
1635 case WM_DELETEITEM:
1636 case WM_DRAWITEM:
1637 case WM_COPYDATA:
1638 HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1639 break;
1640 case WM_MEASUREITEM:
1642 MEASUREITEMSTRUCT16 *mis16;
1643 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1644 lParam = *(LPARAM *)(mis + 1);
1645 mis16 = MapSL(lParam);
1646 mis16->itemWidth = (UINT16)mis->itemWidth;
1647 mis16->itemHeight = (UINT16)mis->itemHeight;
1648 HeapFree( GetProcessHeap(), 0, mis );
1650 break;
1651 case WM_GETMINMAXINFO:
1653 MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1654 lParam = *(LPARAM *)(mmi + 1);
1655 MINMAXINFO32to16( mmi, MapSL(lParam));
1656 HeapFree( GetProcessHeap(), 0, mmi );
1658 break;
1659 case WM_MDICREATE:
1661 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1662 lParam = *(LPARAM *)(cs + 1);
1663 MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1664 HeapFree( GetProcessHeap(), 0, cs );
1666 break;
1667 case WM_MDIGETACTIVE:
1668 result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1669 HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1670 break;
1671 case WM_NCCALCSIZE:
1673 NCCALCSIZE_PARAMS16 *nc16;
1674 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1675 lParam = *(LPARAM *)(nc + 1);
1676 nc16 = MapSL(lParam);
1677 nc16->rgrc[0].left = nc->rgrc[0].left;
1678 nc16->rgrc[0].top = nc->rgrc[0].top;
1679 nc16->rgrc[0].right = nc->rgrc[0].right;
1680 nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1681 if (wParam)
1683 nc16->rgrc[1].left = nc->rgrc[1].left;
1684 nc16->rgrc[1].top = nc->rgrc[1].top;
1685 nc16->rgrc[1].right = nc->rgrc[1].right;
1686 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1687 nc16->rgrc[2].left = nc->rgrc[2].left;
1688 nc16->rgrc[2].top = nc->rgrc[2].top;
1689 nc16->rgrc[2].right = nc->rgrc[2].right;
1690 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1691 if (nc->lppos)
1693 WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1694 HeapFree( GetProcessHeap(), 0, nc->lppos );
1697 HeapFree( GetProcessHeap(), 0, nc );
1699 break;
1700 case WM_NCCREATE:
1701 case WM_CREATE:
1703 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1704 lParam = *(LPARAM *)(cs + 1);
1705 CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1707 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1708 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1710 HeapFree( GetProcessHeap(), 0, cs );
1712 break;
1713 case WM_WINDOWPOSCHANGING:
1714 case WM_WINDOWPOSCHANGED:
1716 WINDOWPOS *wp = (WINDOWPOS *)lParam;
1717 lParam = *(LPARAM *)(wp + 1);
1718 WINDOWPOS32to16(wp, MapSL(lParam));
1719 HeapFree( GetProcessHeap(), 0, wp );
1721 break;
1722 case WM_GETDLGCODE:
1723 if (lParam)
1725 LPMSG msg32 = (LPMSG)lParam;
1727 WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1728 result);
1729 HeapFree( GetProcessHeap(), 0, msg32 );
1731 break;
1732 case WM_NEXTMENU:
1734 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1735 result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1736 HeapFree( GetProcessHeap(), 0, next );
1738 break;
1740 return result;
1744 /**********************************************************************
1745 * WINPROC_MapMsg16To32W
1747 * Map a message from 16- to 32-bit Unicode.
1748 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1750 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1751 WPARAM *pwparam32, LPARAM *plparam )
1753 CHAR ch;
1754 WCHAR wch;
1756 *pmsg32=(UINT)msg16;
1757 *pwparam32 = (WPARAM)wParam16;
1758 switch(msg16)
1760 case WM_GETTEXT:
1761 case WM_SETTEXT:
1762 case WM_WININICHANGE:
1763 case WM_DEVMODECHANGE:
1764 case WM_ASKCBFORMATNAME:
1765 *plparam = (LPARAM)MapSL(*plparam);
1766 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1767 case WM_GETTEXTLENGTH:
1768 case CB_GETLBTEXTLEN:
1769 case LB_GETTEXTLEN:
1770 return 1; /* need to map result */
1771 case WM_NCCREATE:
1772 case WM_CREATE:
1774 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1775 CREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1776 if (!cs) return -1;
1777 CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1778 cs->lpszName = map_str_16_to_32W(cs16->lpszName);
1779 cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1781 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1783 MDICREATESTRUCT16 *mdi_cs16;
1784 MDICREATESTRUCTW *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1785 if (!mdi_cs)
1787 HeapFree(GetProcessHeap(), 0, cs);
1788 return -1;
1790 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1791 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1792 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1793 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1795 cs->lpCreateParams = mdi_cs;
1797 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1798 *plparam = (LPARAM)cs;
1800 return 1;
1801 case WM_MDICREATE:
1803 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1804 MDICREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1805 if (!cs) return -1;
1806 MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1807 cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1808 cs->szClass = map_str_16_to_32W(cs16->szClass);
1809 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1810 *plparam = (LPARAM)cs;
1812 return 1;
1813 case WM_GETDLGCODE:
1814 if (*plparam)
1816 LPMSG16 msg16 = MapSL(*plparam);
1817 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1819 if (!msg32) return -1;
1820 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1821 msg32->lParam = msg16->lParam;
1822 msg32->time = msg16->time;
1823 msg32->pt.x = msg16->pt.x;
1824 msg32->pt.y = msg16->pt.y;
1825 /* this is right, right? */
1826 if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1827 &msg32->message,&msg32->wParam,
1828 &msg32->lParam)<0) {
1829 HeapFree( GetProcessHeap(), 0, msg32 );
1830 return -1;
1832 *plparam = (LPARAM)msg32;
1833 return 1;
1835 else return 0;
1837 case WM_CHARTOITEM:
1838 ch = wParam16;
1839 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1840 *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1841 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1842 return 0;
1843 case WM_MENUCHAR:
1844 ch = wParam16;
1845 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1846 *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1847 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1848 return 0;
1849 case WM_CHAR:
1850 case WM_DEADCHAR:
1851 case WM_SYSCHAR:
1852 case WM_SYSDEADCHAR:
1853 ch = wParam16;
1854 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1855 *pwparam32 = wch;
1856 return 0;
1857 case WM_IME_CHAR:
1858 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1860 default: /* No Unicode translation needed */
1861 return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1862 pwparam32, plparam );
1867 /**********************************************************************
1868 * WINPROC_UnmapMsg16To32W
1870 * Unmap a message that was mapped from 16- to 32-bit Unicode.
1872 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1873 LRESULT result, WNDPROC dispatch )
1875 switch(msg)
1877 case WM_GETTEXT:
1878 case WM_SETTEXT:
1879 case WM_GETTEXTLENGTH:
1880 case CB_GETLBTEXTLEN:
1881 case LB_GETTEXTLEN:
1882 case WM_ASKCBFORMATNAME:
1883 return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, dispatch );
1884 case WM_NCCREATE:
1885 case WM_CREATE:
1887 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1888 lParam = *(LPARAM *)(cs + 1);
1889 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1890 unmap_str_16_to_32W( cs->lpszName );
1891 unmap_str_16_to_32W( cs->lpszClass );
1893 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1895 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1896 unmap_str_16_to_32W( mdi_cs->szTitle );
1897 unmap_str_16_to_32W( mdi_cs->szClass );
1898 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1900 HeapFree( GetProcessHeap(), 0, cs );
1902 break;
1903 case WM_MDICREATE:
1905 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1906 lParam = *(LPARAM *)(cs + 1);
1907 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1908 unmap_str_16_to_32W( cs->szTitle );
1909 unmap_str_16_to_32W( cs->szClass );
1910 HeapFree( GetProcessHeap(), 0, cs );
1912 break;
1913 case WM_GETDLGCODE:
1914 if (lParam)
1916 LPMSG msg32 = (LPMSG)lParam;
1918 WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1919 result, dispatch );
1920 HeapFree( GetProcessHeap(), 0, msg32 );
1922 break;
1923 default:
1924 return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1926 return result;
1929 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1931 HANDLE16 dst;
1932 UINT sz = GlobalSize((HANDLE)src);
1933 LPSTR ptr16, ptr32;
1935 if (!(dst = GlobalAlloc16(flags, sz)))
1936 return 0;
1937 ptr32 = GlobalLock((HANDLE)src);
1938 ptr16 = GlobalLock16(dst);
1939 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1940 GlobalUnlock((HANDLE)src);
1941 GlobalUnlock16(dst);
1943 return dst;
1947 /**********************************************************************
1948 * WINPROC_MapMsg32ATo16
1950 * Map a message from 32-bit Ansi to 16-bit.
1951 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1953 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1954 UINT16 *pmsg16, WPARAM16 *pwparam16,
1955 LPARAM *plparam )
1957 *pmsg16 = (UINT16)msg32;
1958 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1959 switch(msg32)
1961 case SBM_SETRANGE:
1962 *pmsg16 = SBM_SETRANGE16;
1963 *plparam = MAKELPARAM(wParam32, *plparam);
1964 *pwparam16 = 0;
1965 return 0;
1967 case SBM_GETRANGE:
1968 *pmsg16 = SBM_GETRANGE16;
1969 return 1;
1971 case BM_GETCHECK:
1972 case BM_SETCHECK:
1973 case BM_GETSTATE:
1974 case BM_SETSTATE:
1975 case BM_SETSTYLE:
1976 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
1977 return 0;
1979 case EM_GETSEL:
1980 case EM_GETRECT:
1981 case EM_SETRECT:
1982 case EM_SETRECTNP:
1983 case EM_SCROLL:
1984 case EM_LINESCROLL:
1985 case EM_SCROLLCARET:
1986 case EM_GETMODIFY:
1987 case EM_SETMODIFY:
1988 case EM_GETLINECOUNT:
1989 case EM_LINEINDEX:
1990 case EM_SETHANDLE:
1991 case EM_GETHANDLE:
1992 case EM_GETTHUMB:
1993 case EM_LINELENGTH:
1994 case EM_REPLACESEL:
1995 case EM_GETLINE:
1996 case EM_LIMITTEXT:
1997 case EM_CANUNDO:
1998 case EM_UNDO:
1999 case EM_FMTLINES:
2000 case EM_LINEFROMCHAR:
2001 case EM_SETTABSTOPS:
2002 case EM_SETPASSWORDCHAR:
2003 case EM_EMPTYUNDOBUFFER:
2004 case EM_GETFIRSTVISIBLELINE:
2005 case EM_SETREADONLY:
2006 case EM_SETWORDBREAKPROC:
2007 case EM_GETWORDBREAKPROC:
2008 case EM_GETPASSWORDCHAR:
2009 *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
2010 return 0;
2012 case LB_CARETOFF:
2013 case LB_CARETON:
2014 case LB_DELETESTRING:
2015 case LB_GETANCHORINDEX:
2016 case LB_GETCARETINDEX:
2017 case LB_GETCOUNT:
2018 case LB_GETCURSEL:
2019 case LB_GETHORIZONTALEXTENT:
2020 case LB_GETITEMDATA:
2021 case LB_GETITEMHEIGHT:
2022 case LB_GETSEL:
2023 case LB_GETSELCOUNT:
2024 case LB_GETTEXTLEN:
2025 case LB_GETTOPINDEX:
2026 case LB_RESETCONTENT:
2027 case LB_SELITEMRANGE:
2028 case LB_SELITEMRANGEEX:
2029 case LB_SETANCHORINDEX:
2030 case LB_SETCARETINDEX:
2031 case LB_SETCOLUMNWIDTH:
2032 case LB_SETCURSEL:
2033 case LB_SETHORIZONTALEXTENT:
2034 case LB_SETITEMDATA:
2035 case LB_SETITEMHEIGHT:
2036 case LB_SETSEL:
2037 case LB_SETTOPINDEX:
2038 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2039 return 0;
2040 case CB_DELETESTRING:
2041 case CB_GETCOUNT:
2042 case CB_GETLBTEXTLEN:
2043 case CB_LIMITTEXT:
2044 case CB_RESETCONTENT:
2045 case CB_SETEDITSEL:
2046 case CB_GETCURSEL:
2047 case CB_SETCURSEL:
2048 case CB_SHOWDROPDOWN:
2049 case CB_SETITEMDATA:
2050 case CB_SETITEMHEIGHT:
2051 case CB_GETITEMHEIGHT:
2052 case CB_SETEXTENDEDUI:
2053 case CB_GETEXTENDEDUI:
2054 case CB_GETDROPPEDSTATE:
2055 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2056 return 0;
2057 case CB_GETEDITSEL:
2058 *pmsg16 = CB_GETEDITSEL16;
2059 return 1;
2061 case LB_ADDSTRING:
2062 case LB_FINDSTRING:
2063 case LB_FINDSTRINGEXACT:
2064 case LB_INSERTSTRING:
2065 case LB_SELECTSTRING:
2066 case LB_DIR:
2067 case LB_ADDFILE:
2068 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2069 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2070 return 1;
2072 case CB_ADDSTRING:
2073 case CB_FINDSTRING:
2074 case CB_FINDSTRINGEXACT:
2075 case CB_INSERTSTRING:
2076 case CB_SELECTSTRING:
2077 case CB_DIR:
2078 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2079 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2080 return 1;
2082 case LB_GETITEMRECT:
2084 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2085 if (!rect) return -1;
2086 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2087 *plparam = MapLS( rect );
2089 *pmsg16 = LB_GETITEMRECT16;
2090 return 1;
2091 case LB_GETSELITEMS:
2093 LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2095 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2096 if (!(items = HeapAlloc( GetProcessHeap(), 0,
2097 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2098 *items++ = *plparam; /* Store the previous lParam */
2099 *plparam = MapLS( items );
2101 *pmsg16 = LB_GETSELITEMS16;
2102 return 1;
2103 case LB_SETTABSTOPS:
2104 if (wParam32)
2106 INT i;
2107 LPINT16 stops;
2108 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2109 if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2110 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2111 for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2112 *plparam = MapLS( stops );
2113 return 1;
2115 *pmsg16 = LB_SETTABSTOPS16;
2116 return 0;
2118 case CB_GETDROPPEDCONTROLRECT:
2120 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2121 if (!rect) return -1;
2122 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2123 *plparam = (LPARAM)MapLS(rect);
2125 *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2126 return 1;
2128 case LB_GETTEXT:
2129 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2130 *pmsg16 = LB_GETTEXT16;
2131 return 1;
2133 case CB_GETLBTEXT:
2134 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2135 *pmsg16 = CB_GETLBTEXT16;
2136 return 1;
2138 case EM_SETSEL:
2139 *pwparam16 = 0;
2140 *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2141 *pmsg16 = EM_SETSEL16;
2142 return 0;
2144 case WM_ACTIVATE:
2145 case WM_CHARTOITEM:
2146 case WM_COMMAND:
2147 case WM_VKEYTOITEM:
2148 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2149 return 0;
2150 case WM_HSCROLL:
2151 case WM_VSCROLL:
2152 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2153 return 0;
2154 case WM_COPYDATA:
2156 PCOPYDATASTRUCT pcds32 = (PCOPYDATASTRUCT) *plparam;
2157 PCOPYDATASTRUCT16 pcds = HeapAlloc( GetProcessHeap(), 0, sizeof( *pcds));
2158 pcds->dwData = pcds32->dwData;
2159 pcds->cbData = pcds32->cbData;
2160 pcds->lpData = MapLS( pcds32->lpData);
2161 *plparam = MapLS( pcds );
2163 return 1;
2164 case WM_CTLCOLORMSGBOX:
2165 case WM_CTLCOLOREDIT:
2166 case WM_CTLCOLORLISTBOX:
2167 case WM_CTLCOLORBTN:
2168 case WM_CTLCOLORDLG:
2169 case WM_CTLCOLORSCROLLBAR:
2170 case WM_CTLCOLORSTATIC:
2171 *pmsg16 = WM_CTLCOLOR;
2172 *plparam = MAKELPARAM( (HWND16)*plparam,
2173 (WORD)msg32 - WM_CTLCOLORMSGBOX );
2174 return 0;
2175 case WM_COMPAREITEM:
2177 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2178 COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2179 if (!cis) return -1;
2180 cis->CtlType = (UINT16)cis32->CtlType;
2181 cis->CtlID = (UINT16)cis32->CtlID;
2182 cis->hwndItem = HWND_16( cis32->hwndItem );
2183 cis->itemID1 = (UINT16)cis32->itemID1;
2184 cis->itemData1 = cis32->itemData1;
2185 cis->itemID2 = (UINT16)cis32->itemID2;
2186 cis->itemData2 = cis32->itemData2;
2187 *plparam = MapLS( cis );
2189 return 1;
2190 case WM_DELETEITEM:
2192 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2193 DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2194 if (!dis) return -1;
2195 dis->CtlType = (UINT16)dis32->CtlType;
2196 dis->CtlID = (UINT16)dis32->CtlID;
2197 dis->itemID = (UINT16)dis32->itemID;
2198 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2199 : HWND_16( dis32->hwndItem );
2200 dis->itemData = dis32->itemData;
2201 *plparam = MapLS( dis );
2203 return 1;
2204 case WM_DRAWITEM:
2206 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2207 DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2208 if (!dis) return -1;
2209 dis->CtlType = (UINT16)dis32->CtlType;
2210 dis->CtlID = (UINT16)dis32->CtlID;
2211 dis->itemID = (UINT16)dis32->itemID;
2212 dis->itemAction = (UINT16)dis32->itemAction;
2213 dis->itemState = (UINT16)dis32->itemState;
2214 dis->hwndItem = HWND_16( dis32->hwndItem );
2215 dis->hDC = HDC_16(dis32->hDC);
2216 dis->itemData = dis32->itemData;
2217 dis->rcItem.left = dis32->rcItem.left;
2218 dis->rcItem.top = dis32->rcItem.top;
2219 dis->rcItem.right = dis32->rcItem.right;
2220 dis->rcItem.bottom = dis32->rcItem.bottom;
2221 *plparam = MapLS( dis );
2223 return 1;
2224 case WM_MEASUREITEM:
2226 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2227 MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2228 if (!mis) return -1;
2229 mis->CtlType = (UINT16)mis32->CtlType;
2230 mis->CtlID = (UINT16)mis32->CtlID;
2231 mis->itemID = (UINT16)mis32->itemID;
2232 mis->itemWidth = (UINT16)mis32->itemWidth;
2233 mis->itemHeight = (UINT16)mis32->itemHeight;
2234 mis->itemData = mis32->itemData;
2235 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
2236 *plparam = MapLS( mis );
2238 return 1;
2239 case WM_GETMINMAXINFO:
2241 MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2242 if (!mmi) return -1;
2243 MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2244 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
2245 *plparam = MapLS( mmi );
2247 return 1;
2248 case WM_GETTEXT:
2249 case WM_ASKCBFORMATNAME:
2251 LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2252 *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2253 if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2254 *str++ = *plparam; /* Store the previous lParam */
2255 *plparam = MapLS( str );
2257 return 1;
2258 case WM_MDICREATE:
2260 MDICREATESTRUCT16 *cs;
2261 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2263 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2264 MDICREATESTRUCT32Ato16( cs32, cs );
2265 cs->szTitle = MapLS( cs32->szTitle );
2266 cs->szClass = MapLS( cs32->szClass );
2267 *plparam = MapLS( cs );
2269 return 1;
2270 case WM_MDIGETACTIVE:
2271 return 1;
2272 case WM_MDISETMENU:
2273 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2274 (HMENU16)LOWORD(*plparam) );
2275 *pwparam16 = (*plparam == 0);
2276 return 0;
2277 case WM_MENUSELECT:
2278 if(HIWORD(wParam32) & MF_POPUP)
2280 HMENU hmenu;
2281 if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2283 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2284 *pwparam16=HMENU_16(hmenu);
2287 /* fall through */
2288 case WM_MENUCHAR:
2289 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2290 return 0;
2291 case WM_MDIACTIVATE:
2292 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2294 *pwparam16 = ((HWND)*plparam == hwnd);
2295 *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2296 (HWND16)LOWORD(wParam32) );
2298 else
2300 *pwparam16 = HWND_16( (HWND)wParam32 );
2301 *plparam = 0;
2303 return 0;
2304 case WM_NCCALCSIZE:
2306 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2307 NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2308 if (!nc) return -1;
2310 nc->rgrc[0].left = nc32->rgrc[0].left;
2311 nc->rgrc[0].top = nc32->rgrc[0].top;
2312 nc->rgrc[0].right = nc32->rgrc[0].right;
2313 nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2314 if (wParam32)
2316 WINDOWPOS16 *wp;
2317 nc->rgrc[1].left = nc32->rgrc[1].left;
2318 nc->rgrc[1].top = nc32->rgrc[1].top;
2319 nc->rgrc[1].right = nc32->rgrc[1].right;
2320 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2321 nc->rgrc[2].left = nc32->rgrc[2].left;
2322 nc->rgrc[2].top = nc32->rgrc[2].top;
2323 nc->rgrc[2].right = nc32->rgrc[2].right;
2324 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2325 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2327 HeapFree( GetProcessHeap(), 0, nc );
2328 return -1;
2330 WINDOWPOS32to16( nc32->lppos, wp );
2331 nc->lppos = MapLS( wp );
2333 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
2334 *plparam = MapLS( nc );
2336 return 1;
2337 case WM_NCCREATE:
2338 case WM_CREATE:
2340 CREATESTRUCT16 *cs;
2341 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2343 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2344 CREATESTRUCT32Ato16( cs32, cs );
2345 cs->lpszName = MapLS( cs32->lpszName );
2346 cs->lpszClass = MapLS( cs32->lpszClass );
2348 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2350 MDICREATESTRUCT16 *mdi_cs16;
2351 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2352 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2353 if (!mdi_cs16)
2355 HeapFree(GetProcessHeap(), 0, cs);
2356 return -1;
2358 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2359 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2360 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2361 cs->lpCreateParams = MapLS( mdi_cs16 );
2363 *plparam = MapLS( cs );
2365 return 1;
2366 case WM_PARENTNOTIFY:
2367 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2368 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2369 /* else nothing to do */
2370 return 0;
2371 case WM_NOTIFY:
2372 *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2373 return 1;
2374 case WM_SETTEXT:
2375 case WM_WININICHANGE:
2376 case WM_DEVMODECHANGE:
2377 *plparam = MapLS( (LPSTR)*plparam );
2378 return 1;
2379 case WM_WINDOWPOSCHANGING:
2380 case WM_WINDOWPOSCHANGED:
2382 WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2383 if (!wp) return -1;
2384 WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2385 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
2386 *plparam = MapLS( wp );
2388 return 1;
2389 case WM_GETDLGCODE:
2390 if (*plparam) {
2391 LPMSG msg32 = (LPMSG) *plparam;
2392 LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2394 if (!msg16) return -1;
2395 msg16->hwnd = HWND_16( msg32->hwnd );
2396 msg16->lParam = msg32->lParam;
2397 msg16->time = msg32->time;
2398 msg16->pt.x = msg32->pt.x;
2399 msg16->pt.y = msg32->pt.y;
2400 /* this is right, right? */
2401 if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2402 &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2404 HeapFree( GetProcessHeap(), 0, msg16 );
2405 return -1;
2407 *plparam = MapLS( msg16 );
2408 return 1;
2410 return 0;
2412 case WM_ACTIVATEAPP:
2413 if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2414 return 0;
2415 case WM_NEXTMENU:
2417 MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2418 *plparam = (LPARAM)next->hmenuIn;
2419 return 1;
2421 case WM_PAINT:
2422 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2424 *pmsg16 = WM_PAINTICON;
2425 *pwparam16 = 1;
2427 return 0;
2428 case WM_ERASEBKGND:
2429 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2430 *pmsg16 = WM_ICONERASEBKGND;
2431 return 0;
2432 case WM_PAINTCLIPBOARD:
2433 case WM_SIZECLIPBOARD:
2434 FIXME_(msg)("message %04x needs translation\n", msg32 );
2435 return -1;
2436 /* following messages should not be sent to 16-bit apps */
2437 case WM_SIZING:
2438 case WM_MOVING:
2439 case WM_CAPTURECHANGED:
2440 case WM_STYLECHANGING:
2441 case WM_STYLECHANGED:
2442 return -1;
2443 case WM_DDE_INITIATE:
2444 case WM_DDE_TERMINATE:
2445 case WM_DDE_UNADVISE:
2446 case WM_DDE_REQUEST:
2447 *pwparam16 = HWND_16((HWND)wParam32);
2448 return 0;
2449 case WM_DDE_ADVISE:
2450 case WM_DDE_DATA:
2451 case WM_DDE_POKE:
2453 UINT_PTR lo32, hi;
2454 HANDLE16 lo16 = 0;
2456 *pwparam16 = HWND_16((HWND)wParam32);
2457 UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2458 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2459 return -1;
2460 *plparam = MAKELPARAM(lo16, hi);
2462 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2463 case WM_DDE_ACK:
2465 UINT_PTR lo, hi;
2466 int flag = 0;
2467 char buf[2];
2469 *pwparam16 = HWND_16((HWND)wParam32);
2471 UnpackDDElParam(msg32, *plparam, &lo, &hi);
2473 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2474 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2475 switch (flag)
2477 case 0:
2478 if (hi)
2480 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2481 hi = 0;
2483 break;
2484 case 1:
2485 break; /* atom, nothing to do */
2486 case 3:
2487 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2488 /* fall thru */
2489 case 2:
2490 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2491 break;
2493 *plparam = MAKELPARAM(lo, hi);
2495 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2496 case WM_DDE_EXECUTE:
2497 *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2498 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2499 default: /* No translation needed */
2500 return 0;
2505 /**********************************************************************
2506 * WINPROC_UnmapMsg32ATo16
2508 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2510 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2511 MSGPARAM16* p16 )
2513 switch(msg)
2515 case SBM_GETRANGE:
2516 *(LPINT)wParam = LOWORD(p16->lResult);
2517 *(LPINT)lParam = HIWORD(p16->lResult);
2518 break;
2520 case LB_ADDFILE:
2521 case LB_ADDSTRING:
2522 case LB_DIR:
2523 case LB_FINDSTRING:
2524 case LB_FINDSTRINGEXACT:
2525 case LB_INSERTSTRING:
2526 case LB_SELECTSTRING:
2527 case LB_GETTEXT:
2528 case CB_ADDSTRING:
2529 case CB_FINDSTRING:
2530 case CB_FINDSTRINGEXACT:
2531 case CB_INSERTSTRING:
2532 case CB_SELECTSTRING:
2533 case CB_DIR:
2534 case CB_GETLBTEXT:
2535 case WM_SETTEXT:
2536 case WM_WININICHANGE:
2537 case WM_DEVMODECHANGE:
2538 UnMapLS( (SEGPTR)p16->lParam );
2539 break;
2540 case LB_SETTABSTOPS:
2541 case WM_COMPAREITEM:
2542 case WM_DELETEITEM:
2543 case WM_DRAWITEM:
2545 void *ptr = MapSL( p16->lParam );
2546 UnMapLS( p16->lParam );
2547 HeapFree( GetProcessHeap(), 0, ptr );
2549 break;
2550 case WM_COPYDATA:
2552 PCOPYDATASTRUCT16 pcds = MapSL( p16->lParam );
2553 UnMapLS( p16->lParam );
2554 UnMapLS( pcds->lpData );
2555 HeapFree( GetProcessHeap(), 0, pcds );
2557 break;
2558 case CB_GETDROPPEDCONTROLRECT:
2559 case LB_GETITEMRECT:
2561 RECT *r32;
2562 RECT16 *rect = MapSL(p16->lParam);
2563 UnMapLS( p16->lParam );
2564 p16->lParam = *(LPARAM *)(rect + 1);
2565 r32 = (RECT *)p16->lParam;
2566 r32->left = rect->left;
2567 r32->top = rect->top;
2568 r32->right = rect->right;
2569 r32->bottom = rect->bottom;
2570 HeapFree( GetProcessHeap(), 0, rect );
2572 break;
2573 case LB_GETSELITEMS:
2575 INT i;
2576 LPINT16 items = MapSL(p16->lParam);
2577 UnMapLS( p16->lParam );
2578 p16->lParam = *((LPARAM *)items - 1);
2579 for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2580 HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2582 break;
2584 case CB_GETEDITSEL:
2585 if( wParam )
2586 *((PUINT)(wParam)) = LOWORD(p16->lResult);
2587 if( lParam )
2588 *((PUINT)(lParam)) = HIWORD(p16->lResult); /* FIXME: substract 1? */
2589 break;
2591 case WM_MEASUREITEM:
2593 MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2594 MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2595 mis32->itemWidth = mis->itemWidth;
2596 mis32->itemHeight = mis->itemHeight;
2597 UnMapLS( p16->lParam );
2598 HeapFree( GetProcessHeap(), 0, mis );
2600 break;
2601 case WM_GETMINMAXINFO:
2603 MINMAXINFO16 *mmi = MapSL(p16->lParam);
2604 UnMapLS( p16->lParam );
2605 p16->lParam = *(LPARAM *)(mmi + 1);
2606 MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2607 HeapFree( GetProcessHeap(), 0, mmi );
2609 break;
2610 case WM_GETTEXT:
2611 case WM_ASKCBFORMATNAME:
2613 LPSTR str = MapSL(p16->lParam);
2614 UnMapLS( p16->lParam );
2615 p16->lParam = *((LPARAM *)str - 1);
2616 lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2617 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2619 break;
2620 case WM_MDICREATE:
2622 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2623 UnMapLS( cs->szTitle );
2624 UnMapLS( cs->szClass );
2625 UnMapLS( p16->lParam );
2626 HeapFree( GetProcessHeap(), 0, cs );
2628 break;
2629 case WM_MDIGETACTIVE:
2630 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2631 p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2632 break;
2633 case WM_NCCALCSIZE:
2635 NCCALCSIZE_PARAMS *nc32;
2636 NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2637 UnMapLS( p16->lParam );
2638 p16->lParam = *(LPARAM *)(nc + 1);
2639 nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2640 nc32->rgrc[0].left = nc->rgrc[0].left;
2641 nc32->rgrc[0].top = nc->rgrc[0].top;
2642 nc32->rgrc[0].right = nc->rgrc[0].right;
2643 nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2644 if (p16->wParam)
2646 WINDOWPOS16 *pos = MapSL(nc->lppos);
2647 UnMapLS( nc->lppos );
2648 nc32->rgrc[1].left = nc->rgrc[1].left;
2649 nc32->rgrc[1].top = nc->rgrc[1].top;
2650 nc32->rgrc[1].right = nc->rgrc[1].right;
2651 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2652 nc32->rgrc[2].left = nc->rgrc[2].left;
2653 nc32->rgrc[2].top = nc->rgrc[2].top;
2654 nc32->rgrc[2].right = nc->rgrc[2].right;
2655 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2656 WINDOWPOS16to32( pos, nc32->lppos );
2657 HeapFree( GetProcessHeap(), 0, pos );
2659 HeapFree( GetProcessHeap(), 0, nc );
2661 break;
2662 case WM_NCCREATE:
2663 case WM_CREATE:
2665 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2666 UnMapLS( p16->lParam );
2667 UnMapLS( cs->lpszName );
2668 UnMapLS( cs->lpszClass );
2669 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2671 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2672 UnMapLS( cs->lpCreateParams );
2673 UnMapLS( mdi_cs16->szTitle );
2674 UnMapLS( mdi_cs16->szClass );
2675 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2677 HeapFree( GetProcessHeap(), 0, cs );
2679 break;
2680 case WM_WINDOWPOSCHANGING:
2681 case WM_WINDOWPOSCHANGED:
2683 WINDOWPOS16 *wp = MapSL(p16->lParam);
2684 UnMapLS( p16->lParam );
2685 p16->lParam = *(LPARAM *)(wp + 1);
2686 WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2687 HeapFree( GetProcessHeap(), 0, wp );
2689 break;
2690 case WM_NOTIFY:
2691 UnMapLS(p16->lParam);
2692 break;
2693 case WM_GETDLGCODE:
2694 if (p16->lParam)
2696 LPMSG16 msg16 = MapSL(p16->lParam);
2697 MSGPARAM16 msgp16;
2698 UnMapLS( p16->lParam );
2699 msgp16.wParam=msg16->wParam;
2700 msgp16.lParam=msg16->lParam;
2701 WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2702 ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2703 &msgp16 );
2704 HeapFree( GetProcessHeap(), 0, msg16 );
2706 break;
2707 case WM_NEXTMENU:
2709 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2710 next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2711 next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2712 p16->lResult = 0;
2714 break;
2719 /**********************************************************************
2720 * WINPROC_MapMsg32WTo16
2722 * Map a message from 32-bit Unicode to 16-bit.
2723 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2725 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2726 UINT16 *pmsg16, WPARAM16 *pwparam16,
2727 LPARAM *plparam )
2729 BYTE ch;
2730 WCHAR wch;
2732 *pmsg16 = LOWORD(msg32);
2733 *pwparam16 = LOWORD(wParam32);
2734 switch(msg32)
2736 case LB_ADDSTRING:
2737 case LB_FINDSTRING:
2738 case LB_FINDSTRINGEXACT:
2739 case LB_INSERTSTRING:
2740 case LB_SELECTSTRING:
2741 case LB_DIR:
2742 case LB_ADDFILE:
2743 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2744 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2745 return 1;
2747 case CB_ADDSTRING:
2748 case CB_FINDSTRING:
2749 case CB_FINDSTRINGEXACT:
2750 case CB_INSERTSTRING:
2751 case CB_SELECTSTRING:
2752 case CB_DIR:
2753 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2754 *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2755 return 1;
2757 case WM_NCCREATE:
2758 case WM_CREATE:
2760 CREATESTRUCT16 *cs;
2761 CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2763 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2764 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2765 cs->lpszName = map_str_32W_to_16( cs32->lpszName );
2766 cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2768 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2770 MDICREATESTRUCT16 *mdi_cs16;
2771 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2772 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2773 if (!mdi_cs16)
2775 HeapFree(GetProcessHeap(), 0, cs);
2776 return -1;
2778 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2779 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2780 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2781 cs->lpCreateParams = MapLS(mdi_cs16);
2783 *plparam = MapLS(cs);
2785 return 1;
2786 case WM_MDICREATE:
2788 MDICREATESTRUCT16 *cs;
2789 MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2791 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2792 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2793 cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2794 cs->szClass = map_str_32W_to_16( cs32->szClass );
2795 *plparam = MapLS(cs);
2797 return 1;
2798 case WM_SETTEXT:
2799 case WM_WININICHANGE:
2800 case WM_DEVMODECHANGE:
2801 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2802 return 1;
2803 case LB_GETTEXT:
2804 if ( WINPROC_TestLBForStr( hwnd ))
2806 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2807 if (!str) return -1;
2808 *pmsg16 = LB_GETTEXT16;
2809 *plparam = (LPARAM)MapLS(str);
2811 return 1;
2812 case CB_GETLBTEXT:
2813 if ( WINPROC_TestCBForStr( hwnd ))
2815 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2816 if (!str) return -1;
2817 *pmsg16 = CB_GETLBTEXT16;
2818 *plparam = (LPARAM)MapLS(str);
2820 return 1;
2822 case WM_CHARTOITEM:
2823 wch = LOWORD(wParam32);
2824 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2825 *pwparam16 = ch;
2826 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2827 return 0;
2828 case WM_MENUCHAR:
2829 wch = LOWORD(wParam32);
2830 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2831 *pwparam16 = ch;
2832 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2833 return 0;
2834 case WM_CHAR:
2835 case WM_DEADCHAR:
2836 case WM_SYSCHAR:
2837 case WM_SYSDEADCHAR:
2838 wch = wParam32;
2839 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2840 *pwparam16 = ch;
2841 return 0;
2842 case WM_IME_CHAR:
2844 BYTE ch[2];
2846 wch = wParam32;
2847 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
2848 *pwparam16 = (ch[0] << 8) | ch[1];
2849 else
2850 *pwparam16 = ch[0];
2852 return 0;
2854 default: /* No Unicode translation needed (?) */
2855 return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2856 pwparam16, plparam );
2861 /**********************************************************************
2862 * WINPROC_UnmapMsg32WTo16
2864 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2866 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2867 MSGPARAM16* p16 )
2869 switch(msg)
2871 case LB_ADDSTRING:
2872 case LB_FINDSTRING:
2873 case LB_FINDSTRINGEXACT:
2874 case LB_INSERTSTRING:
2875 case LB_SELECTSTRING:
2876 case LB_DIR:
2877 case LB_ADDFILE:
2878 case CB_ADDSTRING:
2879 case CB_FINDSTRING:
2880 case CB_FINDSTRINGEXACT:
2881 case CB_INSERTSTRING:
2882 case CB_SELECTSTRING:
2883 case CB_DIR:
2884 case WM_SETTEXT:
2885 case WM_WININICHANGE:
2886 case WM_DEVMODECHANGE:
2887 unmap_str_32W_to_16( p16->lParam );
2888 break;
2889 case WM_NCCREATE:
2890 case WM_CREATE:
2892 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2893 UnMapLS( p16->lParam );
2894 unmap_str_32W_to_16( cs->lpszName );
2895 unmap_str_32W_to_16( cs->lpszClass );
2897 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2899 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2900 UnMapLS( cs->lpCreateParams );
2901 unmap_str_32W_to_16(mdi_cs16->szTitle);
2902 unmap_str_32W_to_16(mdi_cs16->szClass);
2903 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2905 HeapFree( GetProcessHeap(), 0, cs );
2907 break;
2908 case WM_MDICREATE:
2910 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2911 UnMapLS( p16->lParam );
2912 unmap_str_32W_to_16( cs->szTitle );
2913 unmap_str_32W_to_16( cs->szClass );
2914 HeapFree( GetProcessHeap(), 0, cs );
2916 break;
2917 case WM_GETTEXT:
2918 case WM_ASKCBFORMATNAME:
2920 LPSTR str = MapSL(p16->lParam);
2921 UnMapLS( p16->lParam );
2922 p16->lParam = *((LPARAM *)str - 1);
2923 MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2924 p16->lResult = strlenW( (LPWSTR)p16->lParam );
2925 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2927 break;
2928 case LB_GETTEXT:
2929 if ( WINPROC_TestLBForStr( hwnd ))
2931 LPSTR str = MapSL(p16->lParam);
2932 UnMapLS( p16->lParam );
2933 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2934 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2936 break;
2937 case CB_GETLBTEXT:
2938 if ( WINPROC_TestCBForStr( hwnd ))
2940 LPSTR str = MapSL(p16->lParam);
2941 UnMapLS( p16->lParam );
2942 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2943 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2945 break;
2946 default:
2947 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2948 break;
2953 /**********************************************************************
2954 * WINPROC_CallProc32ATo32W
2956 * Call a window procedure, translating args from Ansi to Unicode.
2958 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
2959 UINT msg, WPARAM wParam,
2960 LPARAM lParam )
2962 LRESULT result;
2963 int unmap;
2965 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2966 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2968 if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2969 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2970 SPY_GetMsgName(msg, hwnd), wParam, lParam );
2971 return 0;
2973 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2974 if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, func );
2975 return result;
2979 /**********************************************************************
2980 * WINPROC_CallProc32WTo32A_fast
2983 static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
2984 UINT msg, WPARAM wParam,
2985 LPARAM lParam, LRESULT *result )
2987 switch(msg)
2989 case WM_NCCREATE:
2990 case WM_CREATE:
2991 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
2992 * at this point.
2994 char buffer[1024];
2995 char *cls = buffer, *name;
2996 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
2997 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
2998 MDICREATESTRUCTA mdi_cs;
2999 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
3001 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
3002 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
3004 if (csW->lpszName)
3006 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
3007 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
3009 else
3010 name_lenW = name_lenA = 0;
3012 if (class_lenA + name_lenA + 2 > sizeof(buffer))
3014 cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
3015 if (!cls) return FALSE;
3018 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
3019 cls[class_lenA] = 0;
3020 csA.lpszClass = cls;
3022 if (csW->lpszName)
3024 name = cls + class_lenA + 1;
3025 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3026 name[name_lenA] = 0;
3027 csA.lpszName = name;
3030 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3032 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3033 mdi_cs.szTitle = csA.lpszName;
3034 mdi_cs.szClass = csA.lpszClass;
3035 csA.lpCreateParams = &mdi_cs;
3038 lParam = (LPARAM)&csA;
3039 *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);
3041 if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
3043 return TRUE;
3045 default:
3046 return FALSE;
3050 /**********************************************************************
3051 * WINPROC_CallProc32WTo32A
3053 * Call a window procedure, translating args from Unicode to Ansi.
3055 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
3056 UINT msg, WPARAM wParam,
3057 LPARAM lParam )
3059 LRESULT result;
3060 int unmap;
3062 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3063 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3065 if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
3066 return result;
3068 if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3069 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3070 SPY_GetMsgName(msg, hwnd), wParam, lParam );
3071 return 0;
3073 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3074 if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3075 return result;
3079 /**********************************************************************
3080 * WINPROC_CallProc16To32A
3082 static LRESULT WINPROC_CallProc16To32A( WNDPROC func, HWND16 hwnd, UINT16 msg,
3083 WPARAM16 wParam, LPARAM lParam )
3085 LRESULT result;
3086 UINT msg32;
3087 WPARAM wParam32;
3088 HWND hwnd32 = WIN_Handle32( hwnd );
3090 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3091 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3093 if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3094 return 0;
3095 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3096 return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3100 /**********************************************************************
3101 * WINPROC_CallProc16To32W
3103 static LRESULT WINPROC_CallProc16To32W( WNDPROC func, HWND16 hwnd, UINT16 msg,
3104 WPARAM16 wParam, LPARAM lParam )
3106 LRESULT result;
3107 UINT msg32;
3108 WPARAM wParam32;
3109 HWND hwnd32 = WIN_Handle32( hwnd );
3111 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3112 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3114 if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3115 return 0;
3116 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3117 return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result,
3118 func );
3122 /**********************************************************************
3123 * __wine_call_wndproc (USER.1010)
3125 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3126 WINDOWPROC *proc )
3128 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam );
3129 else return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam );
3133 /**********************************************************************
3134 * WINPROC_CallProc32ATo16
3136 * Call a 16-bit window procedure, translating the 32-bit args.
3138 static LRESULT WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
3139 UINT msg, WPARAM wParam, LPARAM lParam )
3141 UINT16 msg16;
3142 MSGPARAM16 mp16;
3144 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3145 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3147 mp16.lParam = lParam;
3148 if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3149 return 0;
3150 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3151 mp16.wParam, mp16.lParam );
3152 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3153 return mp16.lResult;
3157 /**********************************************************************
3158 * WINPROC_CallProc32WTo16
3160 * Call a 16-bit window procedure, translating the 32-bit args.
3162 static LRESULT WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
3163 UINT msg, WPARAM wParam, LPARAM lParam )
3165 UINT16 msg16;
3166 MSGPARAM16 mp16;
3168 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3169 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3171 mp16.lParam = lParam;
3172 if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
3173 &mp16.lParam ) == -1)
3174 return 0;
3175 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3176 mp16.wParam, mp16.lParam );
3177 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3178 return mp16.lResult;
3182 /**********************************************************************
3183 * CallWindowProc (USER.122)
3185 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3186 WPARAM16 wParam, LPARAM lParam )
3188 WINDOWPROC *proc;
3190 if (!func) return 0;
3192 if (!(proc = handle16_to_proc( func )))
3193 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3195 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam );
3196 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam );
3197 return WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam );
3201 /**********************************************************************
3202 * CallWindowProcA (USER32.@)
3204 * The CallWindowProc() function invokes the windows procedure _func_,
3205 * with _hwnd_ as the target window, the message specified by _msg_, and
3206 * the message parameters _wParam_ and _lParam_.
3208 * Some kinds of argument conversion may be done, I'm not sure what.
3210 * CallWindowProc() may be used for windows subclassing. Use
3211 * SetWindowLong() to set a new windows procedure for windows of the
3212 * subclass, and handle subclassed messages in the new windows
3213 * procedure. The new windows procedure may then use CallWindowProc()
3214 * with _func_ set to the parent class's windows procedure to dispatch
3215 * the message to the superclass.
3217 * RETURNS
3219 * The return value is message dependent.
3221 * CONFORMANCE
3223 * ECMA-234, Win32
3225 LRESULT WINAPI CallWindowProcA(
3226 WNDPROC func, /* [in] window procedure */
3227 HWND hwnd, /* [in] target window */
3228 UINT msg, /* [in] message */
3229 WPARAM wParam, /* [in] message dependent parameter */
3230 LPARAM lParam /* [in] message dependent parameter */
3232 WINDOWPROC *proc;
3234 if (!func) return 0;
3236 if (!(proc = handle_to_proc( func )))
3237 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3239 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3240 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam );
3241 return WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam );
3245 /**********************************************************************
3246 * CallWindowProcW (USER32.@)
3248 * See CallWindowProcA.
3250 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3251 WPARAM wParam, LPARAM lParam )
3253 WINDOWPROC *proc;
3255 if (!func) return 0;
3257 if (!(proc = handle_to_proc( func )))
3258 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3260 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3261 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam );
3262 return WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam );
3266 /**********************************************************************
3267 * WINPROC_CallDlgProc16
3269 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
3271 WINDOWPROC *proc;
3273 if (!func) return 0;
3275 if (!(proc = handle16_to_proc( (WNDPROC16)func )))
3276 return LOWORD( WINPROC_CallWndProc16( (WNDPROC16)func, hwnd, msg, wParam, lParam ) );
3278 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam );
3279 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam );
3280 return LOWORD( WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam ) );
3284 /**********************************************************************
3285 * WINPROC_CallDlgProcA
3287 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3289 WINDOWPROC *proc;
3291 if (!func) return 0;
3293 if (!(proc = handle_to_proc( (WNDPROC)func )))
3294 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3296 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3297 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam );
3298 return LOWORD( WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam ) );
3302 /**********************************************************************
3303 * WINPROC_CallDlgProcW
3305 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3307 WINDOWPROC *proc;
3309 if (!func) return 0;
3311 if (!(proc = handle_to_proc( (WNDPROC)func )))
3312 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3314 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3315 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam );
3316 return LOWORD( WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam ));