user: Added fast W->A mapping for WM_GETTEXT and WM_ASKCBFORNAME.
[wine/multimedia.git] / dlls / user / winproc.c
blobbe91e0f362852ec97aa886ab86038ff6201ebca3
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_SETTEXT:
996 case WM_WININICHANGE:
997 case WM_DEVMODECHANGE:
998 case CB_DIR:
999 case LB_DIR:
1000 case LB_ADDFILE:
1001 case EM_REPLACESEL:
1002 if (*plparam)
1004 LPCWSTR str = (LPCWSTR)*plparam;
1005 int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, 0, 0);
1006 *plparam = (LPARAM)HeapAlloc(GetProcessHeap(), 0, len);
1007 if (!*plparam) return -1;
1008 WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)*plparam, len, 0, 0);
1009 return 1;
1011 return 0;
1013 case WM_MDICREATE:
1015 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1016 if (!cs) return -1;
1017 *cs = *(MDICREATESTRUCTA *)*plparam;
1018 if (HIWORD(cs->szTitle))
1020 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, NULL, 0, 0, 0);
1021 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1022 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, buf, len, 0, 0);
1023 cs->szTitle = buf;
1025 if (HIWORD(cs->szClass))
1027 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, NULL, 0, 0, 0);
1028 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1029 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, buf, len, 0, 0);
1030 cs->szClass = buf;
1032 *plparam = (LPARAM)cs;
1034 return 1;
1036 /* Listbox */
1037 case LB_ADDSTRING:
1038 case LB_INSERTSTRING:
1039 case LB_FINDSTRING:
1040 case LB_FINDSTRINGEXACT:
1041 case LB_SELECTSTRING:
1042 if(!*plparam) return 0;
1043 if ( WINPROC_TestLBForStr( hwnd ))
1045 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1046 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1047 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1048 *plparam = (LPARAM)buf;
1050 return (*plparam ? 1 : -1);
1052 case LB_GETTEXT: /* FIXME: fixed sized buffer */
1053 { if ( WINPROC_TestLBForStr( hwnd ))
1054 { LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1055 if (!ptr) return -1;
1056 *ptr++ = *plparam; /* Store previous lParam */
1057 *plparam = (LPARAM)ptr;
1060 return 1;
1062 /* Combobox */
1063 case CB_ADDSTRING:
1064 case CB_INSERTSTRING:
1065 case CB_FINDSTRING:
1066 case CB_FINDSTRINGEXACT:
1067 case CB_SELECTSTRING:
1068 if(!*plparam) return 0;
1069 if ( WINPROC_TestCBForStr( hwnd ))
1071 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1072 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1073 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1074 *plparam = (LPARAM)buf;
1076 return (*plparam ? 1 : -1);
1078 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
1079 { if ( WINPROC_TestCBForStr( hwnd ))
1080 { LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1081 if (!ptr) return -1;
1082 *ptr++ = *plparam; /* Store previous lParam */
1083 *plparam = (LPARAM)ptr;
1086 return 1;
1088 /* Multiline edit */
1089 case EM_GETLINE:
1090 { WORD len = (WORD)*plparam;
1091 LPARAM *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1092 if (!ptr) return -1;
1093 *ptr++ = *plparam; /* Store previous lParam */
1094 *((WORD *) ptr) = len; /* Store the length */
1095 *plparam = (LPARAM)ptr;
1097 return 1;
1099 case WM_CHARTOITEM:
1100 case WM_MENUCHAR:
1101 case WM_CHAR:
1102 case WM_DEADCHAR:
1103 case WM_SYSCHAR:
1104 case WM_SYSDEADCHAR:
1105 case EM_SETPASSWORDCHAR:
1107 WCHAR wch = LOWORD(*pwparam);
1108 BYTE ch;
1109 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
1110 *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1112 return 0;
1114 case WM_IME_CHAR:
1116 WCHAR wch = LOWORD(*pwparam);
1117 BYTE ch[2];
1119 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
1120 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1121 else
1122 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1124 return 0;
1126 case WM_PAINTCLIPBOARD:
1127 case WM_SIZECLIPBOARD:
1128 FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1129 return -1;
1130 default: /* No translation needed */
1131 return 0;
1136 /**********************************************************************
1137 * WINPROC_UnmapMsg32WTo32A
1139 * Unmap a message that was mapped from Unicode to Ansi.
1141 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1143 switch(msg)
1145 case WM_SETTEXT:
1146 case WM_WININICHANGE:
1147 case WM_DEVMODECHANGE:
1148 case CB_DIR:
1149 case LB_DIR:
1150 case LB_ADDFILE:
1151 case EM_REPLACESEL:
1152 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1153 break;
1155 case WM_MDICREATE:
1157 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1158 if (HIWORD(cs->szTitle))
1159 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1160 if (HIWORD(cs->szClass))
1161 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1162 HeapFree( GetProcessHeap(), 0, cs );
1164 break;
1166 /* Listbox */
1167 case LB_ADDSTRING:
1168 case LB_INSERTSTRING:
1169 case LB_FINDSTRING:
1170 case LB_FINDSTRINGEXACT:
1171 case LB_SELECTSTRING:
1172 if ( WINPROC_TestLBForStr( hwnd ))
1173 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1174 break;
1176 case LB_GETTEXT:
1177 if ( WINPROC_TestLBForStr( hwnd ))
1179 LPARAM *ptr = (LPARAM *)lParam - 1;
1180 if (result >= 0)
1181 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1182 HeapFree( GetProcessHeap(), 0, ptr );
1184 break;
1186 /* Combobox */
1187 case CB_ADDSTRING:
1188 case CB_INSERTSTRING:
1189 case CB_FINDSTRING:
1190 case CB_FINDSTRINGEXACT:
1191 case CB_SELECTSTRING:
1192 if ( WINPROC_TestCBForStr( hwnd ))
1193 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1194 break;
1196 case CB_GETLBTEXT:
1197 if ( result < 0) /* CB_ERR and CB_ERRSPACE */
1199 LPARAM *ptr = (LPARAM *)lParam - 1;
1200 HeapFree( GetProcessHeap(), 0, ptr );
1202 else if ( WINPROC_TestCBForStr( hwnd ))
1204 LPARAM *ptr = (LPARAM *)lParam - 1;
1205 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1206 HeapFree( GetProcessHeap(), 0, ptr );
1208 break;
1210 /* Multiline edit */
1211 case EM_GETLINE:
1213 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lparam */
1214 WORD len = *(WORD *)ptr;
1215 if (len)
1217 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1218 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1220 HeapFree( GetProcessHeap(), 0, ptr );
1222 break;
1224 return result;
1227 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1229 HANDLE dst;
1230 UINT sz = GlobalSize16(src);
1231 LPSTR ptr16, ptr32;
1233 if (!(dst = GlobalAlloc(flags, sz)))
1234 return 0;
1235 ptr16 = GlobalLock16(src);
1236 ptr32 = GlobalLock(dst);
1237 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1238 GlobalUnlock16(src);
1239 GlobalUnlock(dst);
1241 return (UINT)dst;
1244 /**********************************************************************
1245 * WINPROC_MapMsg16To32A
1247 * Map a message from 16- to 32-bit Ansi.
1248 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1250 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1251 WPARAM *pwparam32, LPARAM *plparam )
1253 *pmsg32 = (UINT)msg16;
1254 *pwparam32 = (WPARAM)wParam16;
1255 switch(msg16)
1257 case WM_ACTIVATE:
1258 case WM_CHARTOITEM:
1259 case WM_COMMAND:
1260 case WM_VKEYTOITEM:
1261 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1262 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1263 return 0;
1264 case WM_HSCROLL:
1265 case WM_VSCROLL:
1266 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1267 *plparam = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1268 return 0;
1269 case WM_CTLCOLOR:
1270 if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1271 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1272 *pwparam32 = (WPARAM)HDC_32(wParam16);
1273 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1274 return 0;
1275 case WM_COMPAREITEM:
1277 COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1278 COMPAREITEMSTRUCT *cis = HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1279 if (!cis) return -1;
1280 cis->CtlType = cis16->CtlType;
1281 cis->CtlID = cis16->CtlID;
1282 cis->hwndItem = WIN_Handle32( cis16->hwndItem );
1283 cis->itemID1 = cis16->itemID1;
1284 cis->itemData1 = cis16->itemData1;
1285 cis->itemID2 = cis16->itemID2;
1286 cis->itemData2 = cis16->itemData2;
1287 cis->dwLocaleId = 0; /* FIXME */
1288 *plparam = (LPARAM)cis;
1290 return 1;
1291 case WM_COPYDATA:
1293 PCOPYDATASTRUCT16 pcds16 = MapSL(*plparam);
1294 PCOPYDATASTRUCT pcds = HeapAlloc ( GetProcessHeap(), 0, sizeof(*pcds));
1295 pcds->dwData = pcds16->dwData;
1296 pcds->cbData = pcds16->cbData;
1297 pcds->lpData = MapSL( pcds16->lpData);
1298 *plparam = (LPARAM)pcds;
1300 return 1;
1301 case WM_DELETEITEM:
1303 DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1304 DELETEITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1305 if (!dis) return -1;
1306 dis->CtlType = dis16->CtlType;
1307 dis->CtlID = dis16->CtlID;
1308 dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1309 dis->itemData = dis16->itemData;
1310 *plparam = (LPARAM)dis;
1312 return 1;
1313 case WM_MEASUREITEM:
1315 MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1316 MEASUREITEMSTRUCT *mis = HeapAlloc(GetProcessHeap(), 0,
1317 sizeof(*mis) + sizeof(LPARAM));
1318 if (!mis) return -1;
1319 mis->CtlType = mis16->CtlType;
1320 mis->CtlID = mis16->CtlID;
1321 mis->itemID = mis16->itemID;
1322 mis->itemWidth = mis16->itemWidth;
1323 mis->itemHeight = mis16->itemHeight;
1324 mis->itemData = mis16->itemData;
1325 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1326 *plparam = (LPARAM)mis;
1328 return 1;
1329 case WM_DRAWITEM:
1331 DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1332 DRAWITEMSTRUCT *dis = HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1333 if (!dis) return -1;
1334 dis->CtlType = dis16->CtlType;
1335 dis->CtlID = dis16->CtlID;
1336 dis->itemID = dis16->itemID;
1337 dis->itemAction = dis16->itemAction;
1338 dis->itemState = dis16->itemState;
1339 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1340 : WIN_Handle32( dis16->hwndItem );
1341 dis->hDC = HDC_32(dis16->hDC);
1342 dis->itemData = dis16->itemData;
1343 dis->rcItem.left = dis16->rcItem.left;
1344 dis->rcItem.top = dis16->rcItem.top;
1345 dis->rcItem.right = dis16->rcItem.right;
1346 dis->rcItem.bottom = dis16->rcItem.bottom;
1347 *plparam = (LPARAM)dis;
1349 return 1;
1350 case WM_GETMINMAXINFO:
1352 MINMAXINFO *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM));
1353 if (!mmi) return -1;
1354 MINMAXINFO16to32( MapSL(*plparam), mmi );
1355 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1356 *plparam = (LPARAM)mmi;
1358 return 1;
1359 case WM_GETTEXT:
1360 case WM_SETTEXT:
1361 case WM_WININICHANGE:
1362 case WM_DEVMODECHANGE:
1363 case WM_ASKCBFORMATNAME:
1364 *plparam = (LPARAM)MapSL(*plparam);
1365 return 0;
1366 case WM_MDICREATE:
1368 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1369 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1370 if (!cs) return -1;
1371 MDICREATESTRUCT16to32A( cs16, cs );
1372 cs->szTitle = MapSL(cs16->szTitle);
1373 cs->szClass = MapSL(cs16->szClass);
1374 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1375 *plparam = (LPARAM)cs;
1377 return 1;
1378 case WM_MDIGETACTIVE:
1379 *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1380 *(BOOL*)(*plparam) = 0;
1381 return 1;
1382 case WM_MDISETMENU:
1383 if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1384 *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1385 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1386 return 0;
1387 case WM_MENUCHAR:
1388 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1389 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1390 return 0;
1391 case WM_MENUSELECT:
1392 if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1394 HMENU hmenu=HMENU_32(HIWORD(*plparam));
1395 UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1396 if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1397 *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1399 else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1400 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1401 return 0;
1402 case WM_MDIACTIVATE:
1403 if( *plparam )
1405 *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1406 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1408 else /* message sent to MDI client */
1409 *pwparam32 = wParam16;
1410 return 0;
1411 case WM_NCCALCSIZE:
1413 NCCALCSIZE_PARAMS16 *nc16;
1414 NCCALCSIZE_PARAMS *nc;
1416 nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM) );
1417 if (!nc) return -1;
1418 nc16 = MapSL(*plparam);
1419 nc->rgrc[0].left = nc16->rgrc[0].left;
1420 nc->rgrc[0].top = nc16->rgrc[0].top;
1421 nc->rgrc[0].right = nc16->rgrc[0].right;
1422 nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1423 if (wParam16)
1425 nc->lppos = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc->lppos) );
1426 nc->rgrc[1].left = nc16->rgrc[1].left;
1427 nc->rgrc[1].top = nc16->rgrc[1].top;
1428 nc->rgrc[1].right = nc16->rgrc[1].right;
1429 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1430 nc->rgrc[2].left = nc16->rgrc[2].left;
1431 nc->rgrc[2].top = nc16->rgrc[2].top;
1432 nc->rgrc[2].right = nc16->rgrc[2].right;
1433 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1434 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1436 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1437 *plparam = (LPARAM)nc;
1439 return 1;
1440 case WM_NCCREATE:
1441 case WM_CREATE:
1443 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1444 CREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1445 if (!cs) return -1;
1446 CREATESTRUCT16to32A( cs16, cs );
1447 cs->lpszName = MapSL(cs16->lpszName);
1448 cs->lpszClass = MapSL(cs16->lpszClass);
1450 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1452 MDICREATESTRUCT16 *mdi_cs16;
1453 MDICREATESTRUCTA *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1454 if (!mdi_cs)
1456 HeapFree(GetProcessHeap(), 0, cs);
1457 return -1;
1459 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1460 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1461 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1462 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1464 cs->lpCreateParams = mdi_cs;
1466 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1467 *plparam = (LPARAM)cs;
1469 return 1;
1470 case WM_PARENTNOTIFY:
1471 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1473 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1474 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1476 return 0;
1477 case WM_WINDOWPOSCHANGING:
1478 case WM_WINDOWPOSCHANGED:
1480 WINDOWPOS *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
1481 if (!wp) return -1;
1482 WINDOWPOS16to32( MapSL(*plparam), wp );
1483 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1484 *plparam = (LPARAM)wp;
1486 return 1;
1487 case WM_GETDLGCODE:
1488 if (*plparam)
1490 LPMSG16 msg16 = MapSL(*plparam);
1491 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1493 if (!msg32) return -1;
1494 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1495 msg32->lParam = msg16->lParam;
1496 msg32->time = msg16->time;
1497 msg32->pt.x = msg16->pt.x;
1498 msg32->pt.y = msg16->pt.y;
1499 /* this is right, right? */
1500 if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1501 &msg32->message,&msg32->wParam,
1502 &msg32->lParam)<0) {
1503 HeapFree( GetProcessHeap(), 0, msg32 );
1504 return -1;
1506 *plparam = (LPARAM)msg32;
1507 return 1;
1509 else return 0;
1510 case WM_NOTIFY:
1511 *plparam = (LPARAM)MapSL(*plparam);
1512 return 0;
1513 case WM_ACTIVATEAPP:
1514 /* We need this when SetActiveWindow sends a Sendmessage16() to
1515 * a 32bit window. Might be superflous with 32bit interprocess
1516 * message queues. */
1517 if (*plparam) *plparam = HTASK_32( *plparam );
1518 return 0;
1519 case WM_NEXTMENU:
1521 MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1522 if (!next) return -1;
1523 next->hmenuIn = (HMENU)*plparam;
1524 next->hmenuNext = 0;
1525 next->hwndNext = 0;
1526 *plparam = (LPARAM)next;
1527 return 1;
1529 case WM_PAINTCLIPBOARD:
1530 case WM_SIZECLIPBOARD:
1531 FIXME_(msg)("message %04x needs translation\n",msg16 );
1532 return -1;
1533 case WM_DDE_INITIATE:
1534 case WM_DDE_TERMINATE:
1535 case WM_DDE_UNADVISE:
1536 case WM_DDE_REQUEST:
1537 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1538 return 0;
1539 case WM_DDE_ADVISE:
1540 case WM_DDE_DATA:
1541 case WM_DDE_POKE:
1543 HANDLE16 lo16;
1544 ATOM hi;
1545 UINT lo32 = 0;
1547 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1548 lo16 = LOWORD(*plparam);
1549 hi = HIWORD(*plparam);
1550 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1551 return -1;
1552 *plparam = PackDDElParam(msg16, lo32, hi);
1554 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1555 case WM_DDE_ACK:
1557 UINT lo, hi;
1558 int flag = 0;
1559 char buf[2];
1561 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1563 lo = LOWORD(*plparam);
1564 hi = HIWORD(*plparam);
1566 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1567 if (GlobalSize16(hi) != 0) flag |= 2;
1568 switch (flag)
1570 case 0:
1571 if (hi)
1573 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1574 hi = 0;
1576 break;
1577 case 1:
1578 break; /* atom, nothing to do */
1579 case 3:
1580 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1581 /* fall thru */
1582 case 2:
1583 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1584 break;
1586 *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1588 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1589 case WM_DDE_EXECUTE:
1590 *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1591 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1592 default: /* No translation needed */
1593 return 0;
1598 /**********************************************************************
1599 * WINPROC_UnmapMsg16To32A
1601 * Unmap a message that was mapped from 16- to 32-bit Ansi.
1603 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1604 LRESULT result )
1606 switch(msg)
1608 case WM_COMPAREITEM:
1609 case WM_DELETEITEM:
1610 case WM_DRAWITEM:
1611 case WM_COPYDATA:
1612 HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1613 break;
1614 case WM_MEASUREITEM:
1616 MEASUREITEMSTRUCT16 *mis16;
1617 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1618 lParam = *(LPARAM *)(mis + 1);
1619 mis16 = MapSL(lParam);
1620 mis16->itemWidth = (UINT16)mis->itemWidth;
1621 mis16->itemHeight = (UINT16)mis->itemHeight;
1622 HeapFree( GetProcessHeap(), 0, mis );
1624 break;
1625 case WM_GETMINMAXINFO:
1627 MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1628 lParam = *(LPARAM *)(mmi + 1);
1629 MINMAXINFO32to16( mmi, MapSL(lParam));
1630 HeapFree( GetProcessHeap(), 0, mmi );
1632 break;
1633 case WM_MDICREATE:
1635 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1636 lParam = *(LPARAM *)(cs + 1);
1637 MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1638 HeapFree( GetProcessHeap(), 0, cs );
1640 break;
1641 case WM_MDIGETACTIVE:
1642 result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1643 HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1644 break;
1645 case WM_NCCALCSIZE:
1647 NCCALCSIZE_PARAMS16 *nc16;
1648 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1649 lParam = *(LPARAM *)(nc + 1);
1650 nc16 = MapSL(lParam);
1651 nc16->rgrc[0].left = nc->rgrc[0].left;
1652 nc16->rgrc[0].top = nc->rgrc[0].top;
1653 nc16->rgrc[0].right = nc->rgrc[0].right;
1654 nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1655 if (wParam)
1657 nc16->rgrc[1].left = nc->rgrc[1].left;
1658 nc16->rgrc[1].top = nc->rgrc[1].top;
1659 nc16->rgrc[1].right = nc->rgrc[1].right;
1660 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1661 nc16->rgrc[2].left = nc->rgrc[2].left;
1662 nc16->rgrc[2].top = nc->rgrc[2].top;
1663 nc16->rgrc[2].right = nc->rgrc[2].right;
1664 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1665 if (nc->lppos)
1667 WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1668 HeapFree( GetProcessHeap(), 0, nc->lppos );
1671 HeapFree( GetProcessHeap(), 0, nc );
1673 break;
1674 case WM_NCCREATE:
1675 case WM_CREATE:
1677 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1678 lParam = *(LPARAM *)(cs + 1);
1679 CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1681 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1682 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1684 HeapFree( GetProcessHeap(), 0, cs );
1686 break;
1687 case WM_WINDOWPOSCHANGING:
1688 case WM_WINDOWPOSCHANGED:
1690 WINDOWPOS *wp = (WINDOWPOS *)lParam;
1691 lParam = *(LPARAM *)(wp + 1);
1692 WINDOWPOS32to16(wp, MapSL(lParam));
1693 HeapFree( GetProcessHeap(), 0, wp );
1695 break;
1696 case WM_GETDLGCODE:
1697 if (lParam)
1699 LPMSG msg32 = (LPMSG)lParam;
1701 WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1702 result);
1703 HeapFree( GetProcessHeap(), 0, msg32 );
1705 break;
1706 case WM_NEXTMENU:
1708 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1709 result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1710 HeapFree( GetProcessHeap(), 0, next );
1712 break;
1714 return result;
1718 /**********************************************************************
1719 * WINPROC_MapMsg16To32W
1721 * Map a message from 16- to 32-bit Unicode.
1722 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1724 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1725 WPARAM *pwparam32, LPARAM *plparam )
1727 CHAR ch;
1728 WCHAR wch;
1730 *pmsg32=(UINT)msg16;
1731 *pwparam32 = (WPARAM)wParam16;
1732 switch(msg16)
1734 case WM_GETTEXT:
1735 case WM_SETTEXT:
1736 case WM_WININICHANGE:
1737 case WM_DEVMODECHANGE:
1738 case WM_ASKCBFORMATNAME:
1739 *plparam = (LPARAM)MapSL(*plparam);
1740 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1741 case WM_GETTEXTLENGTH:
1742 case CB_GETLBTEXTLEN:
1743 case LB_GETTEXTLEN:
1744 return 1; /* need to map result */
1745 case WM_NCCREATE:
1746 case WM_CREATE:
1748 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1749 CREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1750 if (!cs) return -1;
1751 CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1752 cs->lpszName = map_str_16_to_32W(cs16->lpszName);
1753 cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1755 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1757 MDICREATESTRUCT16 *mdi_cs16;
1758 MDICREATESTRUCTW *mdi_cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs));
1759 if (!mdi_cs)
1761 HeapFree(GetProcessHeap(), 0, cs);
1762 return -1;
1764 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1765 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1766 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1767 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1769 cs->lpCreateParams = mdi_cs;
1771 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1772 *plparam = (LPARAM)cs;
1774 return 1;
1775 case WM_MDICREATE:
1777 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1778 MDICREATESTRUCTW *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1779 if (!cs) return -1;
1780 MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1781 cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1782 cs->szClass = map_str_16_to_32W(cs16->szClass);
1783 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1784 *plparam = (LPARAM)cs;
1786 return 1;
1787 case WM_GETDLGCODE:
1788 if (*plparam)
1790 LPMSG16 msg16 = MapSL(*plparam);
1791 LPMSG msg32 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1793 if (!msg32) return -1;
1794 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1795 msg32->lParam = msg16->lParam;
1796 msg32->time = msg16->time;
1797 msg32->pt.x = msg16->pt.x;
1798 msg32->pt.y = msg16->pt.y;
1799 /* this is right, right? */
1800 if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1801 &msg32->message,&msg32->wParam,
1802 &msg32->lParam)<0) {
1803 HeapFree( GetProcessHeap(), 0, msg32 );
1804 return -1;
1806 *plparam = (LPARAM)msg32;
1807 return 1;
1809 else return 0;
1811 case WM_CHARTOITEM:
1812 ch = wParam16;
1813 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1814 *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1815 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1816 return 0;
1817 case WM_MENUCHAR:
1818 ch = wParam16;
1819 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1820 *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1821 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1822 return 0;
1823 case WM_CHAR:
1824 case WM_DEADCHAR:
1825 case WM_SYSCHAR:
1826 case WM_SYSDEADCHAR:
1827 ch = wParam16;
1828 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1829 *pwparam32 = wch;
1830 return 0;
1831 case WM_IME_CHAR:
1832 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1834 default: /* No Unicode translation needed */
1835 return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1836 pwparam32, plparam );
1841 /**********************************************************************
1842 * WINPROC_UnmapMsg16To32W
1844 * Unmap a message that was mapped from 16- to 32-bit Unicode.
1846 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1847 LRESULT result, WNDPROC dispatch )
1849 switch(msg)
1851 case WM_GETTEXT:
1852 case WM_SETTEXT:
1853 case WM_GETTEXTLENGTH:
1854 case CB_GETLBTEXTLEN:
1855 case LB_GETTEXTLEN:
1856 case WM_ASKCBFORMATNAME:
1857 return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, dispatch );
1858 case WM_NCCREATE:
1859 case WM_CREATE:
1861 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1862 lParam = *(LPARAM *)(cs + 1);
1863 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1864 unmap_str_16_to_32W( cs->lpszName );
1865 unmap_str_16_to_32W( cs->lpszClass );
1867 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1869 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1870 unmap_str_16_to_32W( mdi_cs->szTitle );
1871 unmap_str_16_to_32W( mdi_cs->szClass );
1872 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1874 HeapFree( GetProcessHeap(), 0, cs );
1876 break;
1877 case WM_MDICREATE:
1879 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1880 lParam = *(LPARAM *)(cs + 1);
1881 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1882 unmap_str_16_to_32W( cs->szTitle );
1883 unmap_str_16_to_32W( cs->szClass );
1884 HeapFree( GetProcessHeap(), 0, cs );
1886 break;
1887 case WM_GETDLGCODE:
1888 if (lParam)
1890 LPMSG msg32 = (LPMSG)lParam;
1892 WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1893 result, dispatch );
1894 HeapFree( GetProcessHeap(), 0, msg32 );
1896 break;
1897 default:
1898 return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1900 return result;
1903 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1905 HANDLE16 dst;
1906 UINT sz = GlobalSize((HANDLE)src);
1907 LPSTR ptr16, ptr32;
1909 if (!(dst = GlobalAlloc16(flags, sz)))
1910 return 0;
1911 ptr32 = GlobalLock((HANDLE)src);
1912 ptr16 = GlobalLock16(dst);
1913 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1914 GlobalUnlock((HANDLE)src);
1915 GlobalUnlock16(dst);
1917 return dst;
1921 /**********************************************************************
1922 * WINPROC_MapMsg32ATo16
1924 * Map a message from 32-bit Ansi to 16-bit.
1925 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1927 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1928 UINT16 *pmsg16, WPARAM16 *pwparam16,
1929 LPARAM *plparam )
1931 *pmsg16 = (UINT16)msg32;
1932 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1933 switch(msg32)
1935 case SBM_SETRANGE:
1936 *pmsg16 = SBM_SETRANGE16;
1937 *plparam = MAKELPARAM(wParam32, *plparam);
1938 *pwparam16 = 0;
1939 return 0;
1941 case SBM_GETRANGE:
1942 *pmsg16 = SBM_GETRANGE16;
1943 return 1;
1945 case BM_GETCHECK:
1946 case BM_SETCHECK:
1947 case BM_GETSTATE:
1948 case BM_SETSTATE:
1949 case BM_SETSTYLE:
1950 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
1951 return 0;
1953 case EM_GETSEL:
1954 case EM_GETRECT:
1955 case EM_SETRECT:
1956 case EM_SETRECTNP:
1957 case EM_SCROLL:
1958 case EM_LINESCROLL:
1959 case EM_SCROLLCARET:
1960 case EM_GETMODIFY:
1961 case EM_SETMODIFY:
1962 case EM_GETLINECOUNT:
1963 case EM_LINEINDEX:
1964 case EM_SETHANDLE:
1965 case EM_GETHANDLE:
1966 case EM_GETTHUMB:
1967 case EM_LINELENGTH:
1968 case EM_REPLACESEL:
1969 case EM_GETLINE:
1970 case EM_LIMITTEXT:
1971 case EM_CANUNDO:
1972 case EM_UNDO:
1973 case EM_FMTLINES:
1974 case EM_LINEFROMCHAR:
1975 case EM_SETTABSTOPS:
1976 case EM_SETPASSWORDCHAR:
1977 case EM_EMPTYUNDOBUFFER:
1978 case EM_GETFIRSTVISIBLELINE:
1979 case EM_SETREADONLY:
1980 case EM_SETWORDBREAKPROC:
1981 case EM_GETWORDBREAKPROC:
1982 case EM_GETPASSWORDCHAR:
1983 *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
1984 return 0;
1986 case LB_CARETOFF:
1987 case LB_CARETON:
1988 case LB_DELETESTRING:
1989 case LB_GETANCHORINDEX:
1990 case LB_GETCARETINDEX:
1991 case LB_GETCOUNT:
1992 case LB_GETCURSEL:
1993 case LB_GETHORIZONTALEXTENT:
1994 case LB_GETITEMDATA:
1995 case LB_GETITEMHEIGHT:
1996 case LB_GETSEL:
1997 case LB_GETSELCOUNT:
1998 case LB_GETTEXTLEN:
1999 case LB_GETTOPINDEX:
2000 case LB_RESETCONTENT:
2001 case LB_SELITEMRANGE:
2002 case LB_SELITEMRANGEEX:
2003 case LB_SETANCHORINDEX:
2004 case LB_SETCARETINDEX:
2005 case LB_SETCOLUMNWIDTH:
2006 case LB_SETCURSEL:
2007 case LB_SETHORIZONTALEXTENT:
2008 case LB_SETITEMDATA:
2009 case LB_SETITEMHEIGHT:
2010 case LB_SETSEL:
2011 case LB_SETTOPINDEX:
2012 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2013 return 0;
2014 case CB_DELETESTRING:
2015 case CB_GETCOUNT:
2016 case CB_GETLBTEXTLEN:
2017 case CB_LIMITTEXT:
2018 case CB_RESETCONTENT:
2019 case CB_SETEDITSEL:
2020 case CB_GETCURSEL:
2021 case CB_SETCURSEL:
2022 case CB_SHOWDROPDOWN:
2023 case CB_SETITEMDATA:
2024 case CB_SETITEMHEIGHT:
2025 case CB_GETITEMHEIGHT:
2026 case CB_SETEXTENDEDUI:
2027 case CB_GETEXTENDEDUI:
2028 case CB_GETDROPPEDSTATE:
2029 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2030 return 0;
2031 case CB_GETEDITSEL:
2032 *pmsg16 = CB_GETEDITSEL16;
2033 return 1;
2035 case LB_ADDSTRING:
2036 case LB_FINDSTRING:
2037 case LB_FINDSTRINGEXACT:
2038 case LB_INSERTSTRING:
2039 case LB_SELECTSTRING:
2040 case LB_DIR:
2041 case LB_ADDFILE:
2042 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2043 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2044 return 1;
2046 case CB_ADDSTRING:
2047 case CB_FINDSTRING:
2048 case CB_FINDSTRINGEXACT:
2049 case CB_INSERTSTRING:
2050 case CB_SELECTSTRING:
2051 case CB_DIR:
2052 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2053 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2054 return 1;
2056 case LB_GETITEMRECT:
2058 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2059 if (!rect) return -1;
2060 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2061 *plparam = MapLS( rect );
2063 *pmsg16 = LB_GETITEMRECT16;
2064 return 1;
2065 case LB_GETSELITEMS:
2067 LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2069 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2070 if (!(items = HeapAlloc( GetProcessHeap(), 0,
2071 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2072 *items++ = *plparam; /* Store the previous lParam */
2073 *plparam = MapLS( items );
2075 *pmsg16 = LB_GETSELITEMS16;
2076 return 1;
2077 case LB_SETTABSTOPS:
2078 if (wParam32)
2080 INT i;
2081 LPINT16 stops;
2082 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2083 if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2084 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2085 for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2086 *plparam = MapLS( stops );
2087 return 1;
2089 *pmsg16 = LB_SETTABSTOPS16;
2090 return 0;
2092 case CB_GETDROPPEDCONTROLRECT:
2094 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2095 if (!rect) return -1;
2096 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2097 *plparam = (LPARAM)MapLS(rect);
2099 *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2100 return 1;
2102 case LB_GETTEXT:
2103 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2104 *pmsg16 = LB_GETTEXT16;
2105 return 1;
2107 case CB_GETLBTEXT:
2108 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2109 *pmsg16 = CB_GETLBTEXT16;
2110 return 1;
2112 case EM_SETSEL:
2113 *pwparam16 = 0;
2114 *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2115 *pmsg16 = EM_SETSEL16;
2116 return 0;
2118 case WM_ACTIVATE:
2119 case WM_CHARTOITEM:
2120 case WM_COMMAND:
2121 case WM_VKEYTOITEM:
2122 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2123 return 0;
2124 case WM_HSCROLL:
2125 case WM_VSCROLL:
2126 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2127 return 0;
2128 case WM_COPYDATA:
2130 PCOPYDATASTRUCT pcds32 = (PCOPYDATASTRUCT) *plparam;
2131 PCOPYDATASTRUCT16 pcds = HeapAlloc( GetProcessHeap(), 0, sizeof( *pcds));
2132 pcds->dwData = pcds32->dwData;
2133 pcds->cbData = pcds32->cbData;
2134 pcds->lpData = MapLS( pcds32->lpData);
2135 *plparam = MapLS( pcds );
2137 return 1;
2138 case WM_CTLCOLORMSGBOX:
2139 case WM_CTLCOLOREDIT:
2140 case WM_CTLCOLORLISTBOX:
2141 case WM_CTLCOLORBTN:
2142 case WM_CTLCOLORDLG:
2143 case WM_CTLCOLORSCROLLBAR:
2144 case WM_CTLCOLORSTATIC:
2145 *pmsg16 = WM_CTLCOLOR;
2146 *plparam = MAKELPARAM( (HWND16)*plparam,
2147 (WORD)msg32 - WM_CTLCOLORMSGBOX );
2148 return 0;
2149 case WM_COMPAREITEM:
2151 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2152 COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2153 if (!cis) return -1;
2154 cis->CtlType = (UINT16)cis32->CtlType;
2155 cis->CtlID = (UINT16)cis32->CtlID;
2156 cis->hwndItem = HWND_16( cis32->hwndItem );
2157 cis->itemID1 = (UINT16)cis32->itemID1;
2158 cis->itemData1 = cis32->itemData1;
2159 cis->itemID2 = (UINT16)cis32->itemID2;
2160 cis->itemData2 = cis32->itemData2;
2161 *plparam = MapLS( cis );
2163 return 1;
2164 case WM_DELETEITEM:
2166 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2167 DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2168 if (!dis) return -1;
2169 dis->CtlType = (UINT16)dis32->CtlType;
2170 dis->CtlID = (UINT16)dis32->CtlID;
2171 dis->itemID = (UINT16)dis32->itemID;
2172 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2173 : HWND_16( dis32->hwndItem );
2174 dis->itemData = dis32->itemData;
2175 *plparam = MapLS( dis );
2177 return 1;
2178 case WM_DRAWITEM:
2180 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2181 DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2182 if (!dis) return -1;
2183 dis->CtlType = (UINT16)dis32->CtlType;
2184 dis->CtlID = (UINT16)dis32->CtlID;
2185 dis->itemID = (UINT16)dis32->itemID;
2186 dis->itemAction = (UINT16)dis32->itemAction;
2187 dis->itemState = (UINT16)dis32->itemState;
2188 dis->hwndItem = HWND_16( dis32->hwndItem );
2189 dis->hDC = HDC_16(dis32->hDC);
2190 dis->itemData = dis32->itemData;
2191 dis->rcItem.left = dis32->rcItem.left;
2192 dis->rcItem.top = dis32->rcItem.top;
2193 dis->rcItem.right = dis32->rcItem.right;
2194 dis->rcItem.bottom = dis32->rcItem.bottom;
2195 *plparam = MapLS( dis );
2197 return 1;
2198 case WM_MEASUREITEM:
2200 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2201 MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2202 if (!mis) return -1;
2203 mis->CtlType = (UINT16)mis32->CtlType;
2204 mis->CtlID = (UINT16)mis32->CtlID;
2205 mis->itemID = (UINT16)mis32->itemID;
2206 mis->itemWidth = (UINT16)mis32->itemWidth;
2207 mis->itemHeight = (UINT16)mis32->itemHeight;
2208 mis->itemData = mis32->itemData;
2209 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
2210 *plparam = MapLS( mis );
2212 return 1;
2213 case WM_GETMINMAXINFO:
2215 MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2216 if (!mmi) return -1;
2217 MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2218 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
2219 *plparam = MapLS( mmi );
2221 return 1;
2222 case WM_GETTEXT:
2223 case WM_ASKCBFORMATNAME:
2225 LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2226 *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2227 if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2228 *str++ = *plparam; /* Store the previous lParam */
2229 *plparam = MapLS( str );
2231 return 1;
2232 case WM_MDICREATE:
2234 MDICREATESTRUCT16 *cs;
2235 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2237 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2238 MDICREATESTRUCT32Ato16( cs32, cs );
2239 cs->szTitle = MapLS( cs32->szTitle );
2240 cs->szClass = MapLS( cs32->szClass );
2241 *plparam = MapLS( cs );
2243 return 1;
2244 case WM_MDIGETACTIVE:
2245 return 1;
2246 case WM_MDISETMENU:
2247 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2248 (HMENU16)LOWORD(*plparam) );
2249 *pwparam16 = (*plparam == 0);
2250 return 0;
2251 case WM_MENUSELECT:
2252 if(HIWORD(wParam32) & MF_POPUP)
2254 HMENU hmenu;
2255 if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2257 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2258 *pwparam16=HMENU_16(hmenu);
2261 /* fall through */
2262 case WM_MENUCHAR:
2263 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2264 return 0;
2265 case WM_MDIACTIVATE:
2266 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2268 *pwparam16 = ((HWND)*plparam == hwnd);
2269 *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2270 (HWND16)LOWORD(wParam32) );
2272 else
2274 *pwparam16 = HWND_16( (HWND)wParam32 );
2275 *plparam = 0;
2277 return 0;
2278 case WM_NCCALCSIZE:
2280 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2281 NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2282 if (!nc) return -1;
2284 nc->rgrc[0].left = nc32->rgrc[0].left;
2285 nc->rgrc[0].top = nc32->rgrc[0].top;
2286 nc->rgrc[0].right = nc32->rgrc[0].right;
2287 nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2288 if (wParam32)
2290 WINDOWPOS16 *wp;
2291 nc->rgrc[1].left = nc32->rgrc[1].left;
2292 nc->rgrc[1].top = nc32->rgrc[1].top;
2293 nc->rgrc[1].right = nc32->rgrc[1].right;
2294 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2295 nc->rgrc[2].left = nc32->rgrc[2].left;
2296 nc->rgrc[2].top = nc32->rgrc[2].top;
2297 nc->rgrc[2].right = nc32->rgrc[2].right;
2298 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2299 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2301 HeapFree( GetProcessHeap(), 0, nc );
2302 return -1;
2304 WINDOWPOS32to16( nc32->lppos, wp );
2305 nc->lppos = MapLS( wp );
2307 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
2308 *plparam = MapLS( nc );
2310 return 1;
2311 case WM_NCCREATE:
2312 case WM_CREATE:
2314 CREATESTRUCT16 *cs;
2315 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2317 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2318 CREATESTRUCT32Ato16( cs32, cs );
2319 cs->lpszName = MapLS( cs32->lpszName );
2320 cs->lpszClass = MapLS( cs32->lpszClass );
2322 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2324 MDICREATESTRUCT16 *mdi_cs16;
2325 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2326 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2327 if (!mdi_cs16)
2329 HeapFree(GetProcessHeap(), 0, cs);
2330 return -1;
2332 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2333 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2334 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2335 cs->lpCreateParams = MapLS( mdi_cs16 );
2337 *plparam = MapLS( cs );
2339 return 1;
2340 case WM_PARENTNOTIFY:
2341 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2342 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2343 /* else nothing to do */
2344 return 0;
2345 case WM_NOTIFY:
2346 *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2347 return 1;
2348 case WM_SETTEXT:
2349 case WM_WININICHANGE:
2350 case WM_DEVMODECHANGE:
2351 *plparam = MapLS( (LPSTR)*plparam );
2352 return 1;
2353 case WM_WINDOWPOSCHANGING:
2354 case WM_WINDOWPOSCHANGED:
2356 WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2357 if (!wp) return -1;
2358 WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2359 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
2360 *plparam = MapLS( wp );
2362 return 1;
2363 case WM_GETDLGCODE:
2364 if (*plparam) {
2365 LPMSG msg32 = (LPMSG) *plparam;
2366 LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2368 if (!msg16) return -1;
2369 msg16->hwnd = HWND_16( msg32->hwnd );
2370 msg16->lParam = msg32->lParam;
2371 msg16->time = msg32->time;
2372 msg16->pt.x = msg32->pt.x;
2373 msg16->pt.y = msg32->pt.y;
2374 /* this is right, right? */
2375 if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2376 &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2378 HeapFree( GetProcessHeap(), 0, msg16 );
2379 return -1;
2381 *plparam = MapLS( msg16 );
2382 return 1;
2384 return 0;
2386 case WM_ACTIVATEAPP:
2387 if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2388 return 0;
2389 case WM_NEXTMENU:
2391 MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2392 *plparam = (LPARAM)next->hmenuIn;
2393 return 1;
2395 case WM_PAINT:
2396 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2398 *pmsg16 = WM_PAINTICON;
2399 *pwparam16 = 1;
2401 return 0;
2402 case WM_ERASEBKGND:
2403 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2404 *pmsg16 = WM_ICONERASEBKGND;
2405 return 0;
2406 case WM_PAINTCLIPBOARD:
2407 case WM_SIZECLIPBOARD:
2408 FIXME_(msg)("message %04x needs translation\n", msg32 );
2409 return -1;
2410 /* following messages should not be sent to 16-bit apps */
2411 case WM_SIZING:
2412 case WM_MOVING:
2413 case WM_CAPTURECHANGED:
2414 case WM_STYLECHANGING:
2415 case WM_STYLECHANGED:
2416 return -1;
2417 case WM_DDE_INITIATE:
2418 case WM_DDE_TERMINATE:
2419 case WM_DDE_UNADVISE:
2420 case WM_DDE_REQUEST:
2421 *pwparam16 = HWND_16((HWND)wParam32);
2422 return 0;
2423 case WM_DDE_ADVISE:
2424 case WM_DDE_DATA:
2425 case WM_DDE_POKE:
2427 UINT_PTR lo32, hi;
2428 HANDLE16 lo16 = 0;
2430 *pwparam16 = HWND_16((HWND)wParam32);
2431 UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2432 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2433 return -1;
2434 *plparam = MAKELPARAM(lo16, hi);
2436 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2437 case WM_DDE_ACK:
2439 UINT_PTR lo, hi;
2440 int flag = 0;
2441 char buf[2];
2443 *pwparam16 = HWND_16((HWND)wParam32);
2445 UnpackDDElParam(msg32, *plparam, &lo, &hi);
2447 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2448 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2449 switch (flag)
2451 case 0:
2452 if (hi)
2454 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2455 hi = 0;
2457 break;
2458 case 1:
2459 break; /* atom, nothing to do */
2460 case 3:
2461 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2462 /* fall thru */
2463 case 2:
2464 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2465 break;
2467 *plparam = MAKELPARAM(lo, hi);
2469 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2470 case WM_DDE_EXECUTE:
2471 *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2472 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2473 default: /* No translation needed */
2474 return 0;
2479 /**********************************************************************
2480 * WINPROC_UnmapMsg32ATo16
2482 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2484 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2485 MSGPARAM16* p16 )
2487 switch(msg)
2489 case SBM_GETRANGE:
2490 *(LPINT)wParam = LOWORD(p16->lResult);
2491 *(LPINT)lParam = HIWORD(p16->lResult);
2492 break;
2494 case LB_ADDFILE:
2495 case LB_ADDSTRING:
2496 case LB_DIR:
2497 case LB_FINDSTRING:
2498 case LB_FINDSTRINGEXACT:
2499 case LB_INSERTSTRING:
2500 case LB_SELECTSTRING:
2501 case LB_GETTEXT:
2502 case CB_ADDSTRING:
2503 case CB_FINDSTRING:
2504 case CB_FINDSTRINGEXACT:
2505 case CB_INSERTSTRING:
2506 case CB_SELECTSTRING:
2507 case CB_DIR:
2508 case CB_GETLBTEXT:
2509 case WM_SETTEXT:
2510 case WM_WININICHANGE:
2511 case WM_DEVMODECHANGE:
2512 UnMapLS( (SEGPTR)p16->lParam );
2513 break;
2514 case LB_SETTABSTOPS:
2515 case WM_COMPAREITEM:
2516 case WM_DELETEITEM:
2517 case WM_DRAWITEM:
2519 void *ptr = MapSL( p16->lParam );
2520 UnMapLS( p16->lParam );
2521 HeapFree( GetProcessHeap(), 0, ptr );
2523 break;
2524 case WM_COPYDATA:
2526 PCOPYDATASTRUCT16 pcds = MapSL( p16->lParam );
2527 UnMapLS( p16->lParam );
2528 UnMapLS( pcds->lpData );
2529 HeapFree( GetProcessHeap(), 0, pcds );
2531 break;
2532 case CB_GETDROPPEDCONTROLRECT:
2533 case LB_GETITEMRECT:
2535 RECT *r32;
2536 RECT16 *rect = MapSL(p16->lParam);
2537 UnMapLS( p16->lParam );
2538 p16->lParam = *(LPARAM *)(rect + 1);
2539 r32 = (RECT *)p16->lParam;
2540 r32->left = rect->left;
2541 r32->top = rect->top;
2542 r32->right = rect->right;
2543 r32->bottom = rect->bottom;
2544 HeapFree( GetProcessHeap(), 0, rect );
2546 break;
2547 case LB_GETSELITEMS:
2549 INT i;
2550 LPINT16 items = MapSL(p16->lParam);
2551 UnMapLS( p16->lParam );
2552 p16->lParam = *((LPARAM *)items - 1);
2553 for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2554 HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2556 break;
2558 case CB_GETEDITSEL:
2559 if( wParam )
2560 *((PUINT)(wParam)) = LOWORD(p16->lResult);
2561 if( lParam )
2562 *((PUINT)(lParam)) = HIWORD(p16->lResult); /* FIXME: substract 1? */
2563 break;
2565 case WM_MEASUREITEM:
2567 MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2568 MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2569 mis32->itemWidth = mis->itemWidth;
2570 mis32->itemHeight = mis->itemHeight;
2571 UnMapLS( p16->lParam );
2572 HeapFree( GetProcessHeap(), 0, mis );
2574 break;
2575 case WM_GETMINMAXINFO:
2577 MINMAXINFO16 *mmi = MapSL(p16->lParam);
2578 UnMapLS( p16->lParam );
2579 p16->lParam = *(LPARAM *)(mmi + 1);
2580 MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2581 HeapFree( GetProcessHeap(), 0, mmi );
2583 break;
2584 case WM_GETTEXT:
2585 case WM_ASKCBFORMATNAME:
2587 LPSTR str = MapSL(p16->lParam);
2588 UnMapLS( p16->lParam );
2589 p16->lParam = *((LPARAM *)str - 1);
2590 lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2591 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2593 break;
2594 case WM_MDICREATE:
2596 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2597 UnMapLS( cs->szTitle );
2598 UnMapLS( cs->szClass );
2599 UnMapLS( p16->lParam );
2600 HeapFree( GetProcessHeap(), 0, cs );
2602 break;
2603 case WM_MDIGETACTIVE:
2604 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2605 p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2606 break;
2607 case WM_NCCALCSIZE:
2609 NCCALCSIZE_PARAMS *nc32;
2610 NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2611 UnMapLS( p16->lParam );
2612 p16->lParam = *(LPARAM *)(nc + 1);
2613 nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2614 nc32->rgrc[0].left = nc->rgrc[0].left;
2615 nc32->rgrc[0].top = nc->rgrc[0].top;
2616 nc32->rgrc[0].right = nc->rgrc[0].right;
2617 nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2618 if (p16->wParam)
2620 WINDOWPOS16 *pos = MapSL(nc->lppos);
2621 UnMapLS( nc->lppos );
2622 nc32->rgrc[1].left = nc->rgrc[1].left;
2623 nc32->rgrc[1].top = nc->rgrc[1].top;
2624 nc32->rgrc[1].right = nc->rgrc[1].right;
2625 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2626 nc32->rgrc[2].left = nc->rgrc[2].left;
2627 nc32->rgrc[2].top = nc->rgrc[2].top;
2628 nc32->rgrc[2].right = nc->rgrc[2].right;
2629 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2630 WINDOWPOS16to32( pos, nc32->lppos );
2631 HeapFree( GetProcessHeap(), 0, pos );
2633 HeapFree( GetProcessHeap(), 0, nc );
2635 break;
2636 case WM_NCCREATE:
2637 case WM_CREATE:
2639 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2640 UnMapLS( p16->lParam );
2641 UnMapLS( cs->lpszName );
2642 UnMapLS( cs->lpszClass );
2643 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2645 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2646 UnMapLS( cs->lpCreateParams );
2647 UnMapLS( mdi_cs16->szTitle );
2648 UnMapLS( mdi_cs16->szClass );
2649 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2651 HeapFree( GetProcessHeap(), 0, cs );
2653 break;
2654 case WM_WINDOWPOSCHANGING:
2655 case WM_WINDOWPOSCHANGED:
2657 WINDOWPOS16 *wp = MapSL(p16->lParam);
2658 UnMapLS( p16->lParam );
2659 p16->lParam = *(LPARAM *)(wp + 1);
2660 WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2661 HeapFree( GetProcessHeap(), 0, wp );
2663 break;
2664 case WM_NOTIFY:
2665 UnMapLS(p16->lParam);
2666 break;
2667 case WM_GETDLGCODE:
2668 if (p16->lParam)
2670 LPMSG16 msg16 = MapSL(p16->lParam);
2671 MSGPARAM16 msgp16;
2672 UnMapLS( p16->lParam );
2673 msgp16.wParam=msg16->wParam;
2674 msgp16.lParam=msg16->lParam;
2675 WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2676 ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2677 &msgp16 );
2678 HeapFree( GetProcessHeap(), 0, msg16 );
2680 break;
2681 case WM_NEXTMENU:
2683 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2684 next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2685 next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2686 p16->lResult = 0;
2688 break;
2693 /**********************************************************************
2694 * WINPROC_MapMsg32WTo16
2696 * Map a message from 32-bit Unicode to 16-bit.
2697 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2699 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2700 UINT16 *pmsg16, WPARAM16 *pwparam16,
2701 LPARAM *plparam )
2703 BYTE ch;
2704 WCHAR wch;
2706 *pmsg16 = LOWORD(msg32);
2707 *pwparam16 = LOWORD(wParam32);
2708 switch(msg32)
2710 case LB_ADDSTRING:
2711 case LB_FINDSTRING:
2712 case LB_FINDSTRINGEXACT:
2713 case LB_INSERTSTRING:
2714 case LB_SELECTSTRING:
2715 case LB_DIR:
2716 case LB_ADDFILE:
2717 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2718 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2719 return 1;
2721 case CB_ADDSTRING:
2722 case CB_FINDSTRING:
2723 case CB_FINDSTRINGEXACT:
2724 case CB_INSERTSTRING:
2725 case CB_SELECTSTRING:
2726 case CB_DIR:
2727 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2728 *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2729 return 1;
2731 case WM_NCCREATE:
2732 case WM_CREATE:
2734 CREATESTRUCT16 *cs;
2735 CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2737 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2738 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2739 cs->lpszName = map_str_32W_to_16( cs32->lpszName );
2740 cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2742 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2744 MDICREATESTRUCT16 *mdi_cs16;
2745 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2746 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2747 if (!mdi_cs16)
2749 HeapFree(GetProcessHeap(), 0, cs);
2750 return -1;
2752 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2753 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2754 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2755 cs->lpCreateParams = MapLS(mdi_cs16);
2757 *plparam = MapLS(cs);
2759 return 1;
2760 case WM_MDICREATE:
2762 MDICREATESTRUCT16 *cs;
2763 MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2765 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2766 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2767 cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2768 cs->szClass = map_str_32W_to_16( cs32->szClass );
2769 *plparam = MapLS(cs);
2771 return 1;
2772 case WM_SETTEXT:
2773 case WM_WININICHANGE:
2774 case WM_DEVMODECHANGE:
2775 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2776 return 1;
2777 case LB_GETTEXT:
2778 if ( WINPROC_TestLBForStr( hwnd ))
2780 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2781 if (!str) return -1;
2782 *pmsg16 = LB_GETTEXT16;
2783 *plparam = (LPARAM)MapLS(str);
2785 return 1;
2786 case CB_GETLBTEXT:
2787 if ( WINPROC_TestCBForStr( hwnd ))
2789 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2790 if (!str) return -1;
2791 *pmsg16 = CB_GETLBTEXT16;
2792 *plparam = (LPARAM)MapLS(str);
2794 return 1;
2796 case WM_CHARTOITEM:
2797 wch = LOWORD(wParam32);
2798 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2799 *pwparam16 = ch;
2800 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2801 return 0;
2802 case WM_MENUCHAR:
2803 wch = LOWORD(wParam32);
2804 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2805 *pwparam16 = ch;
2806 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2807 return 0;
2808 case WM_CHAR:
2809 case WM_DEADCHAR:
2810 case WM_SYSCHAR:
2811 case WM_SYSDEADCHAR:
2812 wch = wParam32;
2813 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL);
2814 *pwparam16 = ch;
2815 return 0;
2816 case WM_IME_CHAR:
2818 BYTE ch[2];
2820 wch = wParam32;
2821 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
2822 *pwparam16 = (ch[0] << 8) | ch[1];
2823 else
2824 *pwparam16 = ch[0];
2826 return 0;
2828 default: /* No Unicode translation needed (?) */
2829 return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2830 pwparam16, plparam );
2835 /**********************************************************************
2836 * WINPROC_UnmapMsg32WTo16
2838 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2840 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2841 MSGPARAM16* p16 )
2843 switch(msg)
2845 case LB_ADDSTRING:
2846 case LB_FINDSTRING:
2847 case LB_FINDSTRINGEXACT:
2848 case LB_INSERTSTRING:
2849 case LB_SELECTSTRING:
2850 case LB_DIR:
2851 case LB_ADDFILE:
2852 case CB_ADDSTRING:
2853 case CB_FINDSTRING:
2854 case CB_FINDSTRINGEXACT:
2855 case CB_INSERTSTRING:
2856 case CB_SELECTSTRING:
2857 case CB_DIR:
2858 case WM_SETTEXT:
2859 case WM_WININICHANGE:
2860 case WM_DEVMODECHANGE:
2861 unmap_str_32W_to_16( p16->lParam );
2862 break;
2863 case WM_NCCREATE:
2864 case WM_CREATE:
2866 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2867 UnMapLS( p16->lParam );
2868 unmap_str_32W_to_16( cs->lpszName );
2869 unmap_str_32W_to_16( cs->lpszClass );
2871 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2873 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2874 UnMapLS( cs->lpCreateParams );
2875 unmap_str_32W_to_16(mdi_cs16->szTitle);
2876 unmap_str_32W_to_16(mdi_cs16->szClass);
2877 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2879 HeapFree( GetProcessHeap(), 0, cs );
2881 break;
2882 case WM_MDICREATE:
2884 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2885 UnMapLS( p16->lParam );
2886 unmap_str_32W_to_16( cs->szTitle );
2887 unmap_str_32W_to_16( cs->szClass );
2888 HeapFree( GetProcessHeap(), 0, cs );
2890 break;
2891 case WM_GETTEXT:
2892 case WM_ASKCBFORMATNAME:
2894 LPSTR str = MapSL(p16->lParam);
2895 UnMapLS( p16->lParam );
2896 p16->lParam = *((LPARAM *)str - 1);
2897 MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2898 p16->lResult = strlenW( (LPWSTR)p16->lParam );
2899 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2901 break;
2902 case LB_GETTEXT:
2903 if ( WINPROC_TestLBForStr( hwnd ))
2905 LPSTR str = MapSL(p16->lParam);
2906 UnMapLS( p16->lParam );
2907 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2908 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2910 break;
2911 case CB_GETLBTEXT:
2912 if ( WINPROC_TestCBForStr( hwnd ))
2914 LPSTR str = MapSL(p16->lParam);
2915 UnMapLS( p16->lParam );
2916 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2917 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2919 break;
2920 default:
2921 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2922 break;
2927 /**********************************************************************
2928 * WINPROC_CallProc32ATo32W
2930 * Call a window procedure, translating args from Ansi to Unicode.
2932 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd, UINT msg, WPARAM wParam,
2933 LPARAM lParam, BOOL dialog )
2935 LRESULT ret;
2936 int unmap;
2938 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2939 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2941 if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2942 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2943 SPY_GetMsgName(msg, hwnd), wParam, lParam );
2944 return 0;
2946 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2947 if (unmap)
2949 if (dialog)
2951 LRESULT result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
2952 result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result, NULL );
2953 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
2955 else ret = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, ret, func );
2957 return ret;
2961 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
2963 if (size >= need) return static_buffer;
2964 return HeapAlloc( GetProcessHeap(), 0, need );
2967 static inline void free_buffer( void *static_buffer, void *buffer )
2969 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
2972 /**********************************************************************
2973 * WINPROC_CallProc32WTo32A
2975 * Call a window procedure, translating args from Unicode to Ansi.
2977 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd, UINT msg, WPARAM wParam,
2978 LPARAM lParam, BOOL dialog )
2980 LRESULT ret = 0;
2981 int unmap;
2983 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2984 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2986 switch(msg)
2988 case WM_NCCREATE:
2989 case WM_CREATE:
2990 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
2991 * at this point.
2993 char buffer[1024], *cls, *name;
2994 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
2995 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
2996 MDICREATESTRUCTA mdi_cs;
2997 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
2999 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
3000 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
3002 if (csW->lpszName)
3004 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
3005 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
3007 else
3008 name_lenW = name_lenA = 0;
3010 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA + 2 ))) break;
3012 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
3013 cls[class_lenA] = 0;
3014 csA.lpszClass = cls;
3016 if (csW->lpszName)
3018 name = cls + class_lenA + 1;
3019 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3020 name[name_lenA] = 0;
3021 csA.lpszName = name;
3024 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3026 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3027 mdi_cs.szTitle = csA.lpszName;
3028 mdi_cs.szClass = csA.lpszClass;
3029 csA.lpCreateParams = &mdi_cs;
3032 ret = WINPROC_CallWndProc(func, hwnd, msg, wParam, (LPARAM)&csA);
3033 free_buffer( buffer, cls );
3035 break;
3037 case WM_GETTEXT:
3038 case WM_ASKCBFORMATNAME:
3040 char *ptr, buffer[512];
3041 DWORD len = wParam * 2;
3043 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
3044 ret = WINPROC_CallWndProc( func, hwnd, msg, len, (LPARAM)ptr );
3045 if (ret && len)
3047 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
3048 ret = len/sizeof(WCHAR) - 1; /* do not count terminating null */
3049 ((LPWSTR)lParam)[ret] = 0;
3050 if (dialog)
3052 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, ret );
3053 ret = TRUE;
3056 free_buffer( buffer, ptr );
3058 break;
3060 default:
3061 if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3062 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3063 SPY_GetMsgName(msg, hwnd), wParam, lParam );
3064 return 0;
3066 ret = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3067 if (!unmap) break;
3068 if (dialog)
3070 LRESULT result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
3071 result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3072 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
3074 else ret = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, ret );
3075 break;
3078 return ret;
3082 /**********************************************************************
3083 * WINPROC_CallProc16To32A
3085 static LRESULT WINPROC_CallProc16To32A( WNDPROC func, HWND16 hwnd, UINT16 msg,
3086 WPARAM16 wParam, LPARAM lParam, BOOL dialog )
3088 LRESULT ret;
3089 UINT msg32;
3090 WPARAM wParam32;
3091 HWND hwnd32 = WIN_Handle32( hwnd );
3093 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3094 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3096 if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3097 return 0;
3098 ret = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3099 if (dialog)
3101 LRESULT result = GetWindowLongPtrW( hwnd32, DWLP_MSGRESULT );
3102 result = WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3103 SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, result );
3105 else ret = WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, ret );
3107 return ret;
3111 /**********************************************************************
3112 * WINPROC_CallProc16To32W
3114 static LRESULT WINPROC_CallProc16To32W( WNDPROC func, HWND16 hwnd, UINT16 msg,
3115 WPARAM16 wParam, LPARAM lParam, BOOL dialog )
3117 LRESULT ret;
3118 UINT msg32;
3119 WPARAM wParam32;
3120 HWND hwnd32 = WIN_Handle32( hwnd );
3122 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3123 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3125 if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3126 return 0;
3128 ret = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3129 if (dialog)
3131 LRESULT result = GetWindowLongPtrW( hwnd32, DWLP_MSGRESULT );
3132 result = WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result, NULL );
3133 SetWindowLongPtrW( hwnd32, DWLP_MSGRESULT, result );
3135 else ret = WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, ret, func );
3137 return ret;
3141 /**********************************************************************
3142 * __wine_call_wndproc (USER.1010)
3144 LRESULT WINAPI __wine_call_wndproc( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3145 WINDOWPROC *proc )
3147 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
3148 else return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
3152 /**********************************************************************
3153 * WINPROC_CallProc32ATo16
3155 * Call a 16-bit window procedure, translating the 32-bit args.
3157 static LRESULT WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd, UINT msg,
3158 WPARAM wParam, LPARAM lParam, BOOL dialog )
3160 LRESULT ret;
3161 UINT16 msg16;
3162 MSGPARAM16 mp16;
3164 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3165 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3167 mp16.lParam = lParam;
3168 if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3169 return 0;
3170 ret = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16, mp16.wParam, mp16.lParam );
3171 if (dialog)
3173 mp16.lResult = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
3174 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3175 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, mp16.lResult );
3176 ret = LOWORD(ret);
3178 else
3180 mp16.lResult = ret;
3181 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3182 ret = mp16.lResult;
3184 return ret;
3188 /**********************************************************************
3189 * WINPROC_CallProc32WTo16
3191 * Call a 16-bit window procedure, translating the 32-bit args.
3193 static LRESULT WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd, UINT msg,
3194 WPARAM wParam, LPARAM lParam, BOOL dialog )
3196 LRESULT ret;
3197 UINT16 msg16;
3198 MSGPARAM16 mp16;
3200 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3201 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3203 mp16.lParam = lParam;
3204 if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3205 return 0;
3206 ret = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16, mp16.wParam, mp16.lParam );
3207 if (dialog)
3209 mp16.lResult = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
3210 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3211 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, mp16.lResult );
3212 ret = LOWORD(ret);
3214 else
3216 mp16.lResult = ret;
3217 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3218 ret = mp16.lResult;
3220 return ret;
3224 /**********************************************************************
3225 * CallWindowProc (USER.122)
3227 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3228 WPARAM16 wParam, LPARAM lParam )
3230 WINDOWPROC *proc;
3232 if (!func) return 0;
3234 if (!(proc = handle16_to_proc( func )))
3235 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3237 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
3238 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
3239 return WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam );
3243 /**********************************************************************
3244 * CallWindowProcA (USER32.@)
3246 * The CallWindowProc() function invokes the windows procedure _func_,
3247 * with _hwnd_ as the target window, the message specified by _msg_, and
3248 * the message parameters _wParam_ and _lParam_.
3250 * Some kinds of argument conversion may be done, I'm not sure what.
3252 * CallWindowProc() may be used for windows subclassing. Use
3253 * SetWindowLong() to set a new windows procedure for windows of the
3254 * subclass, and handle subclassed messages in the new windows
3255 * procedure. The new windows procedure may then use CallWindowProc()
3256 * with _func_ set to the parent class's windows procedure to dispatch
3257 * the message to the superclass.
3259 * RETURNS
3261 * The return value is message dependent.
3263 * CONFORMANCE
3265 * ECMA-234, Win32
3267 LRESULT WINAPI CallWindowProcA(
3268 WNDPROC func, /* [in] window procedure */
3269 HWND hwnd, /* [in] target window */
3270 UINT msg, /* [in] message */
3271 WPARAM wParam, /* [in] message dependent parameter */
3272 LPARAM lParam /* [in] message dependent parameter */
3274 WINDOWPROC *proc;
3276 if (!func) return 0;
3278 if (!(proc = handle_to_proc( func )))
3279 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3281 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3282 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam, FALSE );
3283 return WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam, FALSE );
3287 /**********************************************************************
3288 * CallWindowProcW (USER32.@)
3290 * See CallWindowProcA.
3292 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3293 WPARAM wParam, LPARAM lParam )
3295 WINDOWPROC *proc;
3297 if (!func) return 0;
3299 if (!(proc = handle_to_proc( func )))
3300 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3302 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3303 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam, FALSE );
3304 return WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam, FALSE );
3308 /**********************************************************************
3309 * WINPROC_CallDlgProc16
3311 INT_PTR WINPROC_CallDlgProc16( DLGPROC16 func, HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam )
3313 WINDOWPROC *proc;
3315 if (!func) return 0;
3317 if (!(proc = handle16_to_proc( (WNDPROC16)func )))
3318 return LOWORD( WINPROC_CallWndProc16( (WNDPROC16)func, hwnd, msg, wParam, lParam ) );
3320 if (proc->procA) return WINPROC_CallProc16To32A( proc->procA, hwnd, msg, wParam, lParam, TRUE );
3321 if (proc->procW) return WINPROC_CallProc16To32W( proc->procW, hwnd, msg, wParam, lParam, TRUE );
3322 return LOWORD( WINPROC_CallWndProc16( proc->proc16, hwnd, msg, wParam, lParam ) );
3326 /**********************************************************************
3327 * WINPROC_CallDlgProcA
3329 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3331 WINDOWPROC *proc;
3333 if (!func) return 0;
3335 if (!(proc = handle_to_proc( (WNDPROC)func )))
3336 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3338 if (proc->procA) return WINPROC_CallWndProc( proc->procA, hwnd, msg, wParam, lParam );
3339 if (proc->procW) return WINPROC_CallProc32ATo32W( proc->procW, hwnd, msg, wParam, lParam, TRUE );
3340 return WINPROC_CallProc32ATo16( proc->proc16, hwnd, msg, wParam, lParam, TRUE );
3344 /**********************************************************************
3345 * WINPROC_CallDlgProcW
3347 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
3349 WINDOWPROC *proc;
3351 if (!func) return 0;
3353 if (!(proc = handle_to_proc( (WNDPROC)func )))
3354 return WINPROC_CallWndProc( (WNDPROC)func, hwnd, msg, wParam, lParam );
3356 if (proc->procW) return WINPROC_CallWndProc( proc->procW, hwnd, msg, wParam, lParam );
3357 if (proc->procA) return WINPROC_CallProc32WTo32A( proc->procA, hwnd, msg, wParam, lParam, TRUE );
3358 return WINPROC_CallProc32WTo16( proc->proc16, hwnd, msg, wParam, lParam, TRUE );