Fall back to default prepare/unprepare header functions.
[wine/hacks.git] / windows / winproc.c
blobd7c5253f3616d6906c49a2ac4f63783f506b8286
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 <stdarg.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "wownt32.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "stackframe.h"
35 #include "controls.h"
36 #include "win.h"
37 #include "winproc.h"
38 #include "message.h"
39 #include "user_private.h"
40 #include "thread.h"
41 #include "dde.h"
42 #include "winternl.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DECLARE_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DEFAULT_DEBUG_CHANNEL(win);
50 #ifdef __i386__
52 #include "pshpack1.h"
54 /* Window procedure 16-to-32-bit thunk */
55 typedef struct
57 BYTE popl_eax; /* popl %eax (return address) */
58 BYTE pushl_func; /* pushl $proc */
59 WNDPROC proc;
60 BYTE pushl_eax; /* pushl %eax */
61 BYTE ljmp; /* ljmp relay*/
62 DWORD relay_offset; /* __wine_call_wndproc_32A/W */
63 WORD relay_sel;
64 } WINPROC_THUNK_FROM16;
66 /* Window procedure 32-to-16-bit thunk */
67 typedef struct
69 BYTE popl_eax; /* popl %eax (return address) */
70 BYTE pushl_func; /* pushl $proc */
71 WNDPROC16 proc;
72 BYTE pushl_eax; /* pushl %eax */
73 BYTE jmp; /* jmp relay (relative jump)*/
74 void (*relay)(); /* WINPROC_CallProc32ATo16() */
75 } WINPROC_THUNK_FROM32;
77 /* Simple jmp to call 32-bit procedure directly */
78 typedef struct
80 BYTE jmp; /* jmp proc (relative jump) */
81 WNDPROC proc;
82 } WINPROC_JUMP;
84 #include "poppack.h"
86 #else /* __i386__ */
88 typedef struct
90 WNDPROC proc;
91 } WINPROC_THUNK_FROM16;
93 typedef struct
95 WNDPROC16 proc;
96 } WINPROC_THUNK_FROM32;
98 typedef struct
100 WNDPROC proc;
101 } WINPROC_JUMP;
103 #endif /* __i386__ */
105 typedef union
107 WINPROC_THUNK_FROM16 t_from16;
108 WINPROC_THUNK_FROM32 t_from32;
109 } WINPROC_THUNK;
111 typedef struct tagWINDOWPROC
113 WINPROC_THUNK thunk; /* Thunk */
114 WINPROC_JUMP jmp; /* Jump */
115 BYTE type; /* Function type */
116 } WINDOWPROC;
118 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
119 UINT msg, WPARAM wParam,
120 LPARAM lParam );
121 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
122 UINT msg, WPARAM wParam,
123 LPARAM lParam );
125 #define MAX_WINPROCS (0x10000 / sizeof(WINDOWPROC))
127 static WINDOWPROC winproc_array[MAX_WINPROCS];
128 static UINT winproc_used;
130 static CRITICAL_SECTION winproc_cs;
131 static CRITICAL_SECTION_DEBUG critsect_debug =
133 0, 0, &winproc_cs,
134 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
135 0, 0, { 0, (DWORD)(__FILE__ ": winproc_cs") }
137 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
139 static BOOL is_valid_winproc( WINDOWPROC *proc )
141 /* check alignment */
142 if (((BYTE *)proc - (BYTE *)winproc_array) % sizeof(*proc)) return FALSE;
143 /* check array limits */
144 if (proc < winproc_array || proc >= winproc_array + winproc_used) return FALSE;
145 return (proc->type != WIN_PROC_INVALID);
148 /* find an existing winproc for a given function and type */
149 /* FIXME: probably should do something more clever than a linear search */
150 static inline WINDOWPROC *find_winproc( WNDPROC func, WINDOWPROCTYPE type )
152 unsigned int i;
154 if (type == WIN_PROC_16)
156 for (i = 0; i < winproc_used; i++)
158 if (winproc_array[i].type == type &&
159 winproc_array[i].thunk.t_from32.proc == (WNDPROC16)func)
160 return &winproc_array[i];
163 else
165 for (i = 0; i < winproc_used; i++)
167 if (winproc_array[i].type == type &&
168 winproc_array[i].thunk.t_from16.proc == func)
169 return &winproc_array[i];
172 return NULL;
175 /* initialize a new winproc */
176 static inline void set_winproc( WINDOWPROC *proc, WNDPROC func, WINDOWPROCTYPE type )
178 #ifdef __i386__
179 static FARPROC16 relay_32A, relay_32W;
181 switch(type)
183 case WIN_PROC_16:
184 proc->thunk.t_from32.popl_eax = 0x58; /* popl %eax */
185 proc->thunk.t_from32.pushl_func = 0x68; /* pushl $proc */
186 proc->thunk.t_from32.proc = (WNDPROC16)func;
187 proc->thunk.t_from32.pushl_eax = 0x50; /* pushl %eax */
188 proc->thunk.t_from32.jmp = 0xe9; /* jmp relay*/
189 proc->thunk.t_from32.relay = /* relative jump */
190 (void(*)())((DWORD)WINPROC_CallProc32ATo16 -
191 (DWORD)(&proc->thunk.t_from32.relay + 1));
192 break;
193 case WIN_PROC_32A:
194 if (!relay_32A) relay_32A = GetProcAddress16( GetModuleHandle16("user"),
195 "__wine_call_wndproc_32A" );
196 proc->thunk.t_from16.popl_eax = 0x58; /* popl %eax */
197 proc->thunk.t_from16.pushl_func = 0x68; /* pushl $proc */
198 proc->thunk.t_from16.proc = func;
199 proc->thunk.t_from16.pushl_eax = 0x50; /* pushl %eax */
200 proc->thunk.t_from16.ljmp = 0xea; /* ljmp relay*/
201 proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32A);
202 proc->thunk.t_from16.relay_sel = SELECTOROF(relay_32A);
203 proc->jmp.jmp = 0xe9;
204 /* Fixup relative jump */
205 proc->jmp.proc = (WNDPROC)((DWORD)func - (DWORD)(&proc->jmp.proc + 1));
206 break;
207 case WIN_PROC_32W:
208 if (!relay_32W) relay_32W = GetProcAddress16( GetModuleHandle16("user"),
209 "__wine_call_wndproc_32W" );
210 proc->thunk.t_from16.popl_eax = 0x58; /* popl %eax */
211 proc->thunk.t_from16.pushl_func = 0x68; /* pushl $proc */
212 proc->thunk.t_from16.proc = func;
213 proc->thunk.t_from16.pushl_eax = 0x50; /* pushl %eax */
214 proc->thunk.t_from16.ljmp = 0xea; /* ljmp relay*/
215 proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32W);
216 proc->thunk.t_from16.relay_sel = SELECTOROF(relay_32W);
217 proc->jmp.jmp = 0xe9;
218 /* Fixup relative jump */
219 proc->jmp.proc = (WNDPROC)((char *)func - (char *)(&proc->jmp.proc + 1));
220 break;
221 default:
222 /* Should not happen */
223 break;
225 #else /* __i386__ */
226 switch(type)
228 case WIN_PROC_16:
229 proc->thunk.t_from32.proc = (WNDPROC16)func;
230 break;
231 case WIN_PROC_32A:
232 case WIN_PROC_32W:
233 proc->thunk.t_from16.proc = func;
234 break;
235 default:
236 /* Should not happen */
237 break;
239 #endif /* __i386__ */
241 proc->type = type;
244 static WORD get_winproc_selector(void)
246 static LONG winproc_selector;
247 WORD ret;
249 if (!(ret = winproc_selector))
251 LDT_ENTRY entry;
252 WORD sel = wine_ldt_alloc_entries(1);
253 wine_ldt_set_base( &entry, winproc_array );
254 wine_ldt_set_limit( &entry, sizeof(winproc_array) - 1 );
255 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
256 wine_ldt_set_entry( sel, &entry );
257 if (!(ret = InterlockedCompareExchange( &winproc_selector, sel, 0 ))) ret = sel;
258 else wine_ldt_free_entries( sel, 1 ); /* somebody beat us to it */
260 return ret;
264 #ifdef __i386__
265 /* Some window procedures modify register they shouldn't, or are not
266 * properly declared stdcall; so we need a small assembly wrapper to
267 * call them. */
268 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
269 WPARAM wParam, LPARAM lParam );
270 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
271 "pushl %ebp\n\t"
272 "movl %esp,%ebp\n\t"
273 "pushl %edi\n\t"
274 "pushl %esi\n\t"
275 "pushl %ebx\n\t"
276 "pushl 24(%ebp)\n\t"
277 "pushl 20(%ebp)\n\t"
278 "pushl 16(%ebp)\n\t"
279 "pushl 12(%ebp)\n\t"
280 "movl 8(%ebp),%eax\n\t"
281 "call *%eax\n\t"
282 "leal -12(%ebp),%esp\n\t"
283 "popl %ebx\n\t"
284 "popl %esi\n\t"
285 "popl %edi\n\t"
286 "leave\n\t"
287 "ret" );
288 #else
289 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
290 WPARAM wParam, LPARAM lParam )
292 return proc( hwnd, msg, wParam, lParam );
294 #endif /* __i386__ */
297 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
299 to->ptReserved.x = from->ptReserved.x;
300 to->ptReserved.y = from->ptReserved.y;
301 to->ptMaxSize.x = from->ptMaxSize.x;
302 to->ptMaxSize.y = from->ptMaxSize.y;
303 to->ptMaxPosition.x = from->ptMaxPosition.x;
304 to->ptMaxPosition.y = from->ptMaxPosition.y;
305 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
306 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
307 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
308 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
311 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
313 to->ptReserved.x = from->ptReserved.x;
314 to->ptReserved.y = from->ptReserved.y;
315 to->ptMaxSize.x = from->ptMaxSize.x;
316 to->ptMaxSize.y = from->ptMaxSize.y;
317 to->ptMaxPosition.x = from->ptMaxPosition.x;
318 to->ptMaxPosition.y = from->ptMaxPosition.y;
319 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
320 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
321 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
322 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
325 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
327 to->hwnd = HWND_16(from->hwnd);
328 to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
329 to->x = from->x;
330 to->y = from->y;
331 to->cx = from->cx;
332 to->cy = from->cy;
333 to->flags = from->flags;
336 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
338 to->hwnd = WIN_Handle32(from->hwnd);
339 to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
340 HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
341 to->x = from->x;
342 to->y = from->y;
343 to->cx = from->cx;
344 to->cy = from->cy;
345 to->flags = from->flags;
348 /* The strings are not copied */
349 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
351 to->lpCreateParams = (SEGPTR)from->lpCreateParams;
352 to->hInstance = HINSTANCE_16(from->hInstance);
353 to->hMenu = HMENU_16(from->hMenu);
354 to->hwndParent = HWND_16(from->hwndParent);
355 to->cy = from->cy;
356 to->cx = from->cx;
357 to->y = from->y;
358 to->x = from->x;
359 to->style = from->style;
360 to->dwExStyle = from->dwExStyle;
363 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
366 to->lpCreateParams = (LPVOID)from->lpCreateParams;
367 to->hInstance = HINSTANCE_32(from->hInstance);
368 to->hMenu = HMENU_32(from->hMenu);
369 to->hwndParent = WIN_Handle32(from->hwndParent);
370 to->cy = from->cy;
371 to->cx = from->cx;
372 to->y = from->y;
373 to->x = from->x;
374 to->style = from->style;
375 to->dwExStyle = from->dwExStyle;
378 /* The strings are not copied */
379 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
381 to->hOwner = HINSTANCE_16(from->hOwner);
382 to->x = from->x;
383 to->y = from->y;
384 to->cx = from->cx;
385 to->cy = from->cy;
386 to->style = from->style;
387 to->lParam = from->lParam;
390 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
392 to->hOwner = HINSTANCE_32(from->hOwner);
393 to->x = from->x;
394 to->y = from->y;
395 to->cx = from->cx;
396 to->cy = from->cy;
397 to->style = from->style;
398 to->lParam = from->lParam;
401 /**********************************************************************
402 * WINPROC_CallWndProc32
404 * Call a 32-bit WndProc.
406 static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
407 WPARAM wParam, LPARAM lParam )
409 LRESULT retvalue;
410 int iWndsLocks;
412 hwnd = WIN_GetFullHandle( hwnd );
413 if (TRACE_ON(relay))
414 DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
415 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
416 /* To avoid any deadlocks, all the locks on the windows structures
417 must be suspended before the control is passed to the application */
418 iWndsLocks = WIN_SuspendWndsLock();
419 retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
420 WIN_RestoreWndsLock(iWndsLocks);
422 if (TRACE_ON(relay))
423 DPRINTF( "%04lx:Ret window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
424 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
425 return retvalue;
428 /***********************************************************************
429 * WINPROC_CallWndProc16
431 * Call a 16-bit window procedure
433 static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
434 UINT16 msg, WPARAM16 wParam,
435 LPARAM lParam )
437 CONTEXT86 context;
438 LRESULT ret;
439 WORD args[5];
440 DWORD offset = 0;
441 TEB *teb = NtCurrentTeb();
442 int iWndsLocks;
444 /* Window procedures want ax = hInstance, ds = es = ss */
446 memset(&context, 0, sizeof(context));
447 context.SegDs = context.SegEs = SELECTOROF(teb->cur_stack);
448 context.SegFs = wine_get_fs();
449 context.SegGs = wine_get_gs();
450 if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
451 context.SegCs = SELECTOROF(proc);
452 context.Eip = OFFSETOF(proc);
453 context.Ebp = OFFSETOF(teb->cur_stack)
454 + (WORD)&((STACK16FRAME*)0)->bp;
456 if (lParam)
458 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
459 work if structures passed in lParam are placed in the stack/data
460 segment. Programmers easily make the mistake of converting lParam
461 to a near rather than a far pointer, since Windows apparently
462 allows this. We copy the structures to the 16 bit stack; this is
463 ugly but makes these programs work. */
464 switch (msg)
466 case WM_CREATE:
467 case WM_NCCREATE:
468 offset = sizeof(CREATESTRUCT16); break;
469 case WM_DRAWITEM:
470 offset = sizeof(DRAWITEMSTRUCT16); break;
471 case WM_COMPAREITEM:
472 offset = sizeof(COMPAREITEMSTRUCT16); break;
474 if (offset)
476 void *s = MapSL(lParam);
477 lParam = stack16_push( offset );
478 memcpy( MapSL(lParam), s, offset );
482 iWndsLocks = WIN_SuspendWndsLock();
484 args[4] = hwnd;
485 args[3] = msg;
486 args[2] = wParam;
487 args[1] = HIWORD(lParam);
488 args[0] = LOWORD(lParam);
489 WOWCallback16Ex( 0, WCB16_REGS, sizeof(args), args, (DWORD *)&context );
490 ret = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
492 if (offset) stack16_pop( offset );
494 WIN_RestoreWndsLock(iWndsLocks);
496 return ret;
500 /**********************************************************************
501 * WINPROC_GetPtr
503 * Return a pointer to the win proc.
505 static WINDOWPROC *WINPROC_GetPtr( WNDPROC handle )
507 BYTE *ptr;
508 WINDOWPROC *proc;
510 /* ptr cannot be < 64K */
511 if (!HIWORD(handle)) return NULL;
513 /* Check for a linear pointer */
515 ptr = (BYTE *)handle;
516 /* First check if it is the jmp address */
517 proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,jmp));
518 if (is_valid_winproc(proc)) return proc;
520 /* Now it must be the thunk address */
521 proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
522 if (is_valid_winproc(proc)) return proc;
524 /* Check for a segmented pointer */
526 if (HIWORD(handle) == get_winproc_selector())
528 ptr = (BYTE *)winproc_array + LOWORD(handle);
529 /* It must be the thunk address */
530 proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
531 if (is_valid_winproc(proc)) return proc;
533 return NULL;
537 /**********************************************************************
538 * WINPROC_GetProc
540 * Get a window procedure pointer that can be passed to the Windows program.
542 WNDPROC16 WINPROC_GetProc( WNDPROC proc, WINDOWPROCTYPE type )
544 WINDOWPROC *ptr = (WINDOWPROC *)proc;
546 if (!proc) return NULL;
547 if (type == WIN_PROC_16) /* We want a 16:16 address */
549 if (ptr->type == WIN_PROC_16)
550 return ptr->thunk.t_from32.proc;
551 else
552 return (WNDPROC16)MAKESEGPTR( get_winproc_selector(),
553 (char *)&ptr->thunk - (char *)winproc_array );
555 else /* We want a 32-bit address */
557 if (ptr->type == WIN_PROC_16)
558 return (WNDPROC16)&ptr->thunk;
559 else if (type != ptr->type)
560 /* Have to return the jmp address if types don't match */
561 return (WNDPROC16)&ptr->jmp;
562 else
563 /* Some Win16 programs want to get back the proc they set */
564 return (WNDPROC16)ptr->thunk.t_from16.proc;
569 /**********************************************************************
570 * WINPROC_AllocProc
572 * Allocate a window procedure for a window or class.
574 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
575 * lot of windows, it will usually only have a limited number of window procedures, so the
576 * array won't grow too large, and this way we avoid the need to track allocations per window.
578 WNDPROC WINPROC_AllocProc( WNDPROC func, WINDOWPROCTYPE type )
580 WINDOWPROC *proc;
582 if (!func) return NULL;
584 EnterCriticalSection( &winproc_cs );
586 /* check if the function is already a win proc */
587 if (!(proc = WINPROC_GetPtr( func )))
589 /* then check if we already have a winproc for that function */
590 if (!(proc = find_winproc( func, type )))
592 if (winproc_used >= MAX_WINPROCS)
593 FIXME( "too many winprocs, cannot allocate one for %p/%d\n", func, type );
594 else
596 proc = &winproc_array[winproc_used++];
597 set_winproc( proc, func, type );
598 TRACE( "allocated %p for %p/%d (%d/%d used)\n",
599 proc, func, type, winproc_used, MAX_WINPROCS );
602 else TRACE( "reusing %p for %p/%d\n", proc, func, type );
605 LeaveCriticalSection( &winproc_cs );
606 return (WNDPROC)proc;
610 /**********************************************************************
611 * WINPROC_GetProcType
613 * Return the window procedure type.
615 WINDOWPROCTYPE WINPROC_GetProcType( WNDPROC proc )
617 if (!proc) return WIN_PROC_INVALID;
618 return ((WINDOWPROC *)proc)->type;
620 /**********************************************************************
621 * WINPROC_TestCBForStr
623 * Return TRUE if the lparam is a string
625 inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
627 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
628 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
630 /**********************************************************************
631 * WINPROC_TestLBForStr
633 * Return TRUE if the lparam is a string
635 inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
637 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
638 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
641 /**********************************************************************
642 * WINPROC_MapMsg32ATo32W
644 * Map a message from Ansi to Unicode.
645 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
647 * FIXME:
648 * WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
649 * the first four bytes are the handle of the icon
650 * when the WM_SETTEXT message has been used to set the icon
652 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
654 switch(msg)
656 case WM_GETTEXT:
657 case WM_ASKCBFORMATNAME:
659 LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
660 *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
661 if (!ptr) return -1;
662 *ptr++ = *plparam; /* Store previous lParam */
663 *plparam = (LPARAM)ptr;
665 return 1;
666 /* lparam is string (0-terminated) */
667 case WM_SETTEXT:
668 case WM_WININICHANGE:
669 case WM_DEVMODECHANGE:
670 case CB_DIR:
671 case LB_DIR:
672 case LB_ADDFILE:
673 case EM_REPLACESEL:
675 DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
676 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
677 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
678 *plparam = (LPARAM)buf;
679 return (*plparam ? 1 : -1);
681 case WM_GETTEXTLENGTH:
682 case CB_GETLBTEXTLEN:
683 case LB_GETTEXTLEN:
684 return 1; /* need to map result */
685 case WM_NCCREATE:
686 case WM_CREATE:
688 UNICODE_STRING usBuffer;
689 struct s
690 { CREATESTRUCTW cs; /* new structure */
691 LPCWSTR lpszName; /* allocated Name */
692 LPCWSTR lpszClass; /* allocated Class */
695 struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
696 if (!xs) return -1;
697 xs->cs = *(CREATESTRUCTW *)*plparam;
698 if (HIWORD(xs->cs.lpszName))
700 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
701 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
703 if (HIWORD(xs->cs.lpszClass))
705 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
706 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
709 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
711 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
712 sizeof(*mdi_cs));
713 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
714 if (HIWORD(mdi_cs->szTitle))
716 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
717 mdi_cs->szTitle = usBuffer.Buffer;
719 if (HIWORD(mdi_cs->szClass))
721 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
722 mdi_cs->szClass = usBuffer.Buffer;
724 xs->cs.lpCreateParams = mdi_cs;
727 *plparam = (LPARAM)xs;
729 return 1;
730 case WM_MDICREATE:
732 MDICREATESTRUCTW *cs =
733 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
734 if (!cs) return -1;
735 *cs = *(MDICREATESTRUCTW *)*plparam;
736 if (HIWORD(cs->szClass))
738 UNICODE_STRING usBuffer;
739 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
740 cs->szClass = usBuffer.Buffer;
742 if (HIWORD(cs->szTitle))
744 UNICODE_STRING usBuffer;
745 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
746 cs->szTitle = usBuffer.Buffer;
748 *plparam = (LPARAM)cs;
750 return 1;
752 /* Listbox */
753 case LB_ADDSTRING:
754 case LB_INSERTSTRING:
755 case LB_FINDSTRING:
756 case LB_FINDSTRINGEXACT:
757 case LB_SELECTSTRING:
758 if(!*plparam) return 0;
759 if ( WINPROC_TestLBForStr( hwnd ))
761 UNICODE_STRING usBuffer;
762 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
763 *plparam = (LPARAM)usBuffer.Buffer;
765 return (*plparam ? 1 : -1);
767 case LB_GETTEXT: /* FIXME: fixed sized buffer */
768 { if ( WINPROC_TestLBForStr( hwnd ))
769 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 512 * sizeof(WCHAR) + sizeof(LPARAM) );
770 if (!ptr) return -1;
771 *ptr++ = *plparam; /* Store previous lParam */
772 *plparam = (LPARAM)ptr;
775 return 1;
777 /* Combobox */
778 case CB_ADDSTRING:
779 case CB_INSERTSTRING:
780 case CB_FINDSTRINGEXACT:
781 case CB_FINDSTRING:
782 case CB_SELECTSTRING:
783 if(!*plparam) return 0;
784 if ( WINPROC_TestCBForStr( hwnd ))
786 UNICODE_STRING usBuffer;
787 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
788 *plparam = (LPARAM)usBuffer.Buffer;
790 return (*plparam ? 1 : -1);
792 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
793 { if ( WINPROC_TestCBForStr( hwnd ))
794 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 512 * sizeof(WCHAR) + sizeof(LPARAM) );
795 if (!ptr) return -1;
796 *ptr++ = *plparam; /* Store previous lParam */
797 *plparam = (LPARAM)ptr;
800 return 1;
802 /* Multiline edit */
803 case EM_GETLINE:
804 { WORD len = (WORD)*plparam;
805 LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
806 if (!ptr) return -1;
807 *ptr++ = *plparam; /* Store previous lParam */
808 *((WORD *) ptr) = len; /* Store the length */
809 *plparam = (LPARAM)ptr;
811 return 1;
813 case WM_CHARTOITEM:
814 case WM_MENUCHAR:
815 case WM_CHAR:
816 case WM_DEADCHAR:
817 case WM_SYSCHAR:
818 case WM_SYSDEADCHAR:
819 case EM_SETPASSWORDCHAR:
821 BYTE ch = LOWORD(*pwparam);
822 WCHAR wch;
823 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
824 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
826 return 0;
828 case WM_IME_CHAR:
830 BYTE ch[2];
831 WCHAR wch;
832 ch[0] = (*pwparam >> 8);
833 ch[1] = *pwparam & 0xff;
834 if (ch[0])
835 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
836 else
837 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
838 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
840 return 0;
842 case WM_PAINTCLIPBOARD:
843 case WM_SIZECLIPBOARD:
844 FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
845 return -1;
846 default: /* No translation needed */
847 return 0;
852 /**********************************************************************
853 * WINPROC_UnmapMsg32ATo32W
855 * Unmap a message that was mapped from Ansi to Unicode.
857 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
858 LRESULT result )
860 switch(msg)
862 case WM_GETTEXT:
863 case WM_ASKCBFORMATNAME:
865 LPARAM *ptr = (LPARAM *)lParam - 1;
866 if (!wParam) result = 0;
867 else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
868 (LPSTR)*ptr, wParam, NULL, NULL )))
870 ((LPSTR)*ptr)[wParam-1] = 0;
871 result = wParam - 1;
873 else result--; /* do not count terminating null */
874 HeapFree( GetProcessHeap(), 0, ptr );
876 break;
877 case WM_GETTEXTLENGTH:
878 case CB_GETLBTEXTLEN:
879 case LB_GETTEXTLEN:
880 /* there may be one DBCS char for each Unicode char */
881 return result * 2;
882 case WM_NCCREATE:
883 case WM_CREATE:
885 struct s
886 { CREATESTRUCTW cs; /* new structure */
887 LPWSTR lpszName; /* allocated Name */
888 LPWSTR lpszClass; /* allocated Class */
890 struct s *xs = (struct s *)lParam;
891 HeapFree( GetProcessHeap(), 0, xs->lpszName );
892 HeapFree( GetProcessHeap(), 0, xs->lpszClass );
894 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
896 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
897 if (HIWORD(mdi_cs->szTitle))
898 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
899 if (HIWORD(mdi_cs->szClass))
900 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
901 HeapFree(GetProcessHeap(), 0, mdi_cs);
903 HeapFree( GetProcessHeap(), 0, xs );
905 break;
907 case WM_MDICREATE:
909 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
910 if (HIWORD(cs->szTitle))
911 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
912 if (HIWORD(cs->szClass))
913 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
914 HeapFree( GetProcessHeap(), 0, cs );
916 break;
918 case WM_SETTEXT:
919 case WM_WININICHANGE:
920 case WM_DEVMODECHANGE:
921 case CB_DIR:
922 case LB_DIR:
923 case LB_ADDFILE:
924 case EM_REPLACESEL:
925 HeapFree( GetProcessHeap(), 0, (void *)lParam );
926 break;
928 /* Listbox */
929 case LB_ADDSTRING:
930 case LB_INSERTSTRING:
931 case LB_FINDSTRING:
932 case LB_FINDSTRINGEXACT:
933 case LB_SELECTSTRING:
934 if ( WINPROC_TestLBForStr( hwnd ))
935 HeapFree( GetProcessHeap(), 0, (void *)lParam );
936 break;
938 case LB_GETTEXT:
939 if ( WINPROC_TestLBForStr( hwnd ))
941 LPARAM *ptr = (LPARAM *)lParam - 1;
942 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
943 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
944 HeapFree( GetProcessHeap(), 0, ptr );
946 break;
948 /* Combobox */
949 case CB_ADDSTRING:
950 case CB_INSERTSTRING:
951 case CB_FINDSTRING:
952 case CB_FINDSTRINGEXACT:
953 case CB_SELECTSTRING:
954 if ( WINPROC_TestCBForStr( hwnd ))
955 HeapFree( GetProcessHeap(), 0, (void *)lParam );
956 break;
958 case CB_GETLBTEXT:
959 if ( WINPROC_TestCBForStr( hwnd ))
961 LPARAM *ptr = (LPARAM *)lParam - 1;
962 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
963 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
964 HeapFree( GetProcessHeap(), 0, ptr );
966 break;
968 /* Multiline edit */
969 case EM_GETLINE:
971 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lParam */
972 WORD len = *(WORD *) lParam;
973 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
974 (LPSTR)*ptr, len, NULL, NULL );
975 if (result < len) ((LPSTR)*ptr)[result] = 0;
976 HeapFree( GetProcessHeap(), 0, ptr );
978 break;
980 return result;
984 /**********************************************************************
985 * WINPROC_MapMsg32WTo32A
987 * Map a message from Unicode to Ansi.
988 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
990 static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
992 switch(msg)
994 case WM_GETTEXT:
995 case WM_ASKCBFORMATNAME:
997 LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
998 *pwparam + sizeof(LPARAM) );
999 if (!ptr) return -1;
1000 *ptr++ = *plparam; /* Store previous lParam */
1001 *plparam = (LPARAM)ptr;
1003 return 1;
1005 case WM_SETTEXT:
1006 case WM_WININICHANGE:
1007 case WM_DEVMODECHANGE:
1008 case CB_DIR:
1009 case LB_DIR:
1010 case LB_ADDFILE:
1011 case EM_REPLACESEL:
1012 if (*plparam)
1014 LPCWSTR str = (LPCWSTR)*plparam;
1015 int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, 0, 0);
1016 *plparam = (LPARAM)HeapAlloc(GetProcessHeap(), 0, len);
1017 if (!*plparam) return -1;
1018 WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)*plparam, len, 0, 0);
1019 return 1;
1021 return 0;
1023 case WM_MDICREATE:
1025 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1026 if (!cs) return -1;
1027 *cs = *(MDICREATESTRUCTA *)*plparam;
1028 if (HIWORD(cs->szTitle))
1030 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, NULL, 0, 0, 0);
1031 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1032 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, buf, len, 0, 0);
1033 cs->szTitle = buf;
1035 if (HIWORD(cs->szClass))
1037 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, NULL, 0, 0, 0);
1038 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1039 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, buf, len, 0, 0);
1040 cs->szClass = buf;
1042 *plparam = (LPARAM)cs;
1044 return 1;
1046 /* Listbox */
1047 case LB_ADDSTRING:
1048 case LB_INSERTSTRING:
1049 case LB_FINDSTRING:
1050 case LB_FINDSTRINGEXACT:
1051 case LB_SELECTSTRING:
1052 if(!*plparam) return 0;
1053 if ( WINPROC_TestLBForStr( hwnd ))
1055 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1056 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1057 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1058 *plparam = (LPARAM)buf;
1060 return (*plparam ? 1 : -1);
1062 case LB_GETTEXT: /* FIXME: fixed sized buffer */
1063 { if ( WINPROC_TestLBForStr( hwnd ))
1064 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1065 if (!ptr) return -1;
1066 *ptr++ = *plparam; /* Store previous lParam */
1067 *plparam = (LPARAM)ptr;
1070 return 1;
1072 /* Combobox */
1073 case CB_ADDSTRING:
1074 case CB_INSERTSTRING:
1075 case CB_FINDSTRING:
1076 case CB_FINDSTRINGEXACT:
1077 case CB_SELECTSTRING:
1078 if(!*plparam) return 0;
1079 if ( WINPROC_TestCBForStr( hwnd ))
1081 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1082 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1083 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1084 *plparam = (LPARAM)buf;
1086 return (*plparam ? 1 : -1);
1088 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
1089 { if ( WINPROC_TestCBForStr( hwnd ))
1090 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 512 + sizeof(LPARAM) );
1091 if (!ptr) return -1;
1092 *ptr++ = *plparam; /* Store previous lParam */
1093 *plparam = (LPARAM)ptr;
1096 return 1;
1098 /* Multiline edit */
1099 case EM_GETLINE:
1100 { WORD len = (WORD)*plparam;
1101 LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1102 if (!ptr) return -1;
1103 *ptr++ = *plparam; /* Store previous lParam */
1104 *((WORD *) ptr) = len; /* Store the length */
1105 *plparam = (LPARAM)ptr;
1107 return 1;
1109 case WM_CHARTOITEM:
1110 case WM_MENUCHAR:
1111 case WM_CHAR:
1112 case WM_DEADCHAR:
1113 case WM_SYSCHAR:
1114 case WM_SYSDEADCHAR:
1115 case EM_SETPASSWORDCHAR:
1117 WCHAR wch = LOWORD(*pwparam);
1118 BYTE ch;
1119 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
1120 *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1122 return 0;
1124 case WM_IME_CHAR:
1126 WCHAR wch = LOWORD(*pwparam);
1127 BYTE ch[2];
1129 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
1130 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1131 else
1132 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1134 return 0;
1136 case WM_PAINTCLIPBOARD:
1137 case WM_SIZECLIPBOARD:
1138 FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1139 return -1;
1140 default: /* No translation needed */
1141 return 0;
1146 /**********************************************************************
1147 * WINPROC_UnmapMsg32WTo32A
1149 * Unmap a message that was mapped from Unicode to Ansi.
1151 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1153 switch(msg)
1155 case WM_GETTEXT:
1156 case WM_ASKCBFORMATNAME:
1158 LPARAM *ptr = (LPARAM *)lParam - 1;
1159 if (!wParam) result = 0;
1160 else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
1161 (LPWSTR)*ptr, wParam )))
1163 ((LPWSTR)*ptr)[wParam-1] = 0;
1164 result = wParam - 1;
1166 else result--; /* do not count terminating null */
1167 HeapFree( GetProcessHeap(), 0, ptr );
1169 break;
1171 case WM_SETTEXT:
1172 case WM_WININICHANGE:
1173 case WM_DEVMODECHANGE:
1174 case CB_DIR:
1175 case LB_DIR:
1176 case LB_ADDFILE:
1177 case EM_REPLACESEL:
1178 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1179 break;
1181 case WM_MDICREATE:
1183 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1184 if (HIWORD(cs->szTitle))
1185 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1186 if (HIWORD(cs->szClass))
1187 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1188 HeapFree( GetProcessHeap(), 0, cs );
1190 break;
1192 /* Listbox */
1193 case LB_ADDSTRING:
1194 case LB_INSERTSTRING:
1195 case LB_FINDSTRING:
1196 case LB_FINDSTRINGEXACT:
1197 case LB_SELECTSTRING:
1198 if ( WINPROC_TestLBForStr( hwnd ))
1199 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1200 break;
1202 case LB_GETTEXT:
1203 if ( WINPROC_TestLBForStr( hwnd ))
1205 LPARAM *ptr = (LPARAM *)lParam - 1;
1206 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1207 HeapFree( GetProcessHeap(), 0, ptr );
1209 break;
1211 /* Combobox */
1212 case CB_ADDSTRING:
1213 case CB_INSERTSTRING:
1214 case CB_FINDSTRING:
1215 case CB_FINDSTRINGEXACT:
1216 case CB_SELECTSTRING:
1217 if ( WINPROC_TestCBForStr( hwnd ))
1218 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1219 break;
1221 case CB_GETLBTEXT:
1222 if ( WINPROC_TestCBForStr( hwnd ))
1224 LPARAM *ptr = (LPARAM *)lParam - 1;
1225 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1226 HeapFree( GetProcessHeap(), 0, ptr );
1228 break;
1230 /* Multiline edit */
1231 case EM_GETLINE:
1233 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lparam */
1234 WORD len = *(WORD *)ptr;
1235 if (len)
1237 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1238 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1240 HeapFree( GetProcessHeap(), 0, ptr );
1242 break;
1244 return result;
1247 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1249 HANDLE dst;
1250 UINT sz = GlobalSize16(src);
1251 LPSTR ptr16, ptr32;
1253 if (!(dst = GlobalAlloc(flags, sz)))
1254 return 0;
1255 ptr16 = GlobalLock16(src);
1256 ptr32 = GlobalLock(dst);
1257 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1258 GlobalUnlock16(src);
1259 GlobalUnlock(dst);
1261 return (UINT)dst;
1264 /**********************************************************************
1265 * WINPROC_MapMsg16To32A
1267 * Map a message from 16- to 32-bit Ansi.
1268 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1270 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1271 WPARAM *pwparam32, LPARAM *plparam )
1273 *pmsg32 = (UINT)msg16;
1274 *pwparam32 = (WPARAM)wParam16;
1275 switch(msg16)
1277 case WM_ACTIVATE:
1278 case WM_CHARTOITEM:
1279 case WM_COMMAND:
1280 case WM_VKEYTOITEM:
1281 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1282 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1283 return 0;
1284 case WM_HSCROLL:
1285 case WM_VSCROLL:
1286 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1287 *plparam = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1288 return 0;
1289 case WM_CTLCOLOR:
1290 if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1291 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1292 *pwparam32 = (WPARAM)HDC_32(wParam16);
1293 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1294 return 0;
1295 case WM_COMPAREITEM:
1297 COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1298 COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)
1299 HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1300 if (!cis) return -1;
1301 cis->CtlType = cis16->CtlType;
1302 cis->CtlID = cis16->CtlID;
1303 cis->hwndItem = WIN_Handle32( cis16->hwndItem );
1304 cis->itemID1 = cis16->itemID1;
1305 cis->itemData1 = cis16->itemData1;
1306 cis->itemID2 = cis16->itemID2;
1307 cis->itemData2 = cis16->itemData2;
1308 cis->dwLocaleId = 0; /* FIXME */
1309 *plparam = (LPARAM)cis;
1311 return 1;
1312 case WM_DELETEITEM:
1314 DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1315 DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)
1316 HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1317 if (!dis) return -1;
1318 dis->CtlType = dis16->CtlType;
1319 dis->CtlID = dis16->CtlID;
1320 dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1321 dis->itemData = dis16->itemData;
1322 *plparam = (LPARAM)dis;
1324 return 1;
1325 case WM_MEASUREITEM:
1327 MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1328 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)
1329 HeapAlloc(GetProcessHeap(), 0,
1330 sizeof(*mis) + sizeof(LPARAM));
1331 if (!mis) return -1;
1332 mis->CtlType = mis16->CtlType;
1333 mis->CtlID = mis16->CtlID;
1334 mis->itemID = mis16->itemID;
1335 mis->itemWidth = mis16->itemWidth;
1336 mis->itemHeight = mis16->itemHeight;
1337 mis->itemData = mis16->itemData;
1338 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1339 *plparam = (LPARAM)mis;
1341 return 1;
1342 case WM_DRAWITEM:
1344 DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1345 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)HeapAlloc(GetProcessHeap(), 0,
1346 sizeof(*dis));
1347 if (!dis) return -1;
1348 dis->CtlType = dis16->CtlType;
1349 dis->CtlID = dis16->CtlID;
1350 dis->itemID = dis16->itemID;
1351 dis->itemAction = dis16->itemAction;
1352 dis->itemState = dis16->itemState;
1353 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1354 : WIN_Handle32( dis16->hwndItem );
1355 dis->hDC = HDC_32(dis16->hDC);
1356 dis->itemData = dis16->itemData;
1357 dis->rcItem.left = dis16->rcItem.left;
1358 dis->rcItem.top = dis16->rcItem.top;
1359 dis->rcItem.right = dis16->rcItem.right;
1360 dis->rcItem.bottom = dis16->rcItem.bottom;
1361 *plparam = (LPARAM)dis;
1363 return 1;
1364 case WM_GETMINMAXINFO:
1366 MINMAXINFO *mmi = (MINMAXINFO *)HeapAlloc( GetProcessHeap(), 0,
1367 sizeof(*mmi) + sizeof(LPARAM));
1368 if (!mmi) return -1;
1369 MINMAXINFO16to32( MapSL(*plparam), mmi );
1370 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1371 *plparam = (LPARAM)mmi;
1373 return 1;
1374 case WM_GETTEXT:
1375 case WM_SETTEXT:
1376 case WM_WININICHANGE:
1377 case WM_DEVMODECHANGE:
1378 case WM_ASKCBFORMATNAME:
1379 *plparam = (LPARAM)MapSL(*plparam);
1380 return 0;
1381 case WM_MDICREATE:
1383 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1384 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1385 if (!cs) return -1;
1386 MDICREATESTRUCT16to32A( cs16, cs );
1387 cs->szTitle = MapSL(cs16->szTitle);
1388 cs->szClass = MapSL(cs16->szClass);
1389 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1390 *plparam = (LPARAM)cs;
1392 return 1;
1393 case WM_MDIGETACTIVE:
1394 *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1395 *(BOOL*)(*plparam) = 0;
1396 return 1;
1397 case WM_MDISETMENU:
1398 if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1399 *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1400 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1401 return 0;
1402 case WM_MENUCHAR:
1403 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1404 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1405 return 0;
1406 case WM_MENUSELECT:
1407 if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1409 HMENU hmenu=HMENU_32(HIWORD(*plparam));
1410 UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1411 if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1412 *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1414 else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1415 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1416 return 0;
1417 case WM_MDIACTIVATE:
1418 if( *plparam )
1420 *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1421 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1423 else /* message sent to MDI client */
1424 *pwparam32 = wParam16;
1425 return 0;
1426 case WM_NCCALCSIZE:
1428 NCCALCSIZE_PARAMS16 *nc16;
1429 NCCALCSIZE_PARAMS *nc;
1431 nc = (NCCALCSIZE_PARAMS *)HeapAlloc( GetProcessHeap(), 0,
1432 sizeof(*nc) + sizeof(LPARAM) );
1433 if (!nc) return -1;
1434 nc16 = MapSL(*plparam);
1435 nc->rgrc[0].left = nc16->rgrc[0].left;
1436 nc->rgrc[0].top = nc16->rgrc[0].top;
1437 nc->rgrc[0].right = nc16->rgrc[0].right;
1438 nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1439 if (wParam16)
1441 nc->lppos = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1442 sizeof(*nc->lppos) );
1443 nc->rgrc[1].left = nc16->rgrc[1].left;
1444 nc->rgrc[1].top = nc16->rgrc[1].top;
1445 nc->rgrc[1].right = nc16->rgrc[1].right;
1446 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1447 nc->rgrc[2].left = nc16->rgrc[2].left;
1448 nc->rgrc[2].top = nc16->rgrc[2].top;
1449 nc->rgrc[2].right = nc16->rgrc[2].right;
1450 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1451 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1453 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1454 *plparam = (LPARAM)nc;
1456 return 1;
1457 case WM_NCCREATE:
1458 case WM_CREATE:
1460 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1461 CREATESTRUCTA *cs = (CREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0,
1462 sizeof(*cs) + sizeof(LPARAM) );
1463 if (!cs) return -1;
1464 CREATESTRUCT16to32A( cs16, cs );
1465 cs->lpszName = MapSL(cs16->lpszName);
1466 cs->lpszClass = MapSL(cs16->lpszClass);
1468 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1470 MDICREATESTRUCT16 *mdi_cs16;
1471 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)HeapAlloc(GetProcessHeap(), 0,
1472 sizeof(*mdi_cs));
1473 if (!mdi_cs)
1475 HeapFree(GetProcessHeap(), 0, cs);
1476 return -1;
1478 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1479 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1480 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1481 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1483 cs->lpCreateParams = mdi_cs;
1485 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1486 *plparam = (LPARAM)cs;
1488 return 1;
1489 case WM_PARENTNOTIFY:
1490 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1492 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1493 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1495 return 0;
1496 case WM_WINDOWPOSCHANGING:
1497 case WM_WINDOWPOSCHANGED:
1499 WINDOWPOS *wp = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1500 sizeof(*wp) + sizeof(LPARAM) );
1501 if (!wp) return -1;
1502 WINDOWPOS16to32( MapSL(*plparam), wp );
1503 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1504 *plparam = (LPARAM)wp;
1506 return 1;
1507 case WM_GETDLGCODE:
1508 if (*plparam)
1510 LPMSG16 msg16 = MapSL(*plparam);
1511 LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1513 if (!msg32) return -1;
1514 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1515 msg32->lParam = msg16->lParam;
1516 msg32->time = msg16->time;
1517 msg32->pt.x = msg16->pt.x;
1518 msg32->pt.y = msg16->pt.y;
1519 /* this is right, right? */
1520 if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1521 &msg32->message,&msg32->wParam,
1522 &msg32->lParam)<0) {
1523 HeapFree( GetProcessHeap(), 0, msg32 );
1524 return -1;
1526 *plparam = (LPARAM)msg32;
1527 return 1;
1529 else return 0;
1530 case WM_NOTIFY:
1531 *plparam = (LPARAM)MapSL(*plparam);
1532 return 0;
1533 case WM_ACTIVATEAPP:
1534 /* We need this when SetActiveWindow sends a Sendmessage16() to
1535 * a 32bit window. Might be superflous with 32bit interprocess
1536 * message queues. */
1537 if (*plparam) *plparam = HTASK_32( *plparam );
1538 return 0;
1539 case WM_NEXTMENU:
1541 MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1542 if (!next) return -1;
1543 next->hmenuIn = (HMENU)*plparam;
1544 next->hmenuNext = 0;
1545 next->hwndNext = 0;
1546 *plparam = (LPARAM)next;
1547 return 1;
1549 case WM_PAINTCLIPBOARD:
1550 case WM_SIZECLIPBOARD:
1551 FIXME_(msg)("message %04x needs translation\n",msg16 );
1552 return -1;
1553 case WM_DDE_INITIATE:
1554 case WM_DDE_TERMINATE:
1555 case WM_DDE_UNADVISE:
1556 case WM_DDE_REQUEST:
1557 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1558 return 0;
1559 case WM_DDE_ADVISE:
1560 case WM_DDE_DATA:
1561 case WM_DDE_POKE:
1563 HANDLE16 lo16;
1564 ATOM hi;
1565 UINT lo32 = 0;
1567 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1568 lo16 = LOWORD(*plparam);
1569 hi = HIWORD(*plparam);
1570 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1571 return -1;
1572 *plparam = PackDDElParam(msg16, lo32, hi);
1574 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1575 case WM_DDE_ACK:
1577 UINT lo, hi;
1578 int flag = 0;
1579 char buf[2];
1581 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1583 lo = LOWORD(*plparam);
1584 hi = HIWORD(*plparam);
1586 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1587 if (GlobalSize16(hi) != 0) flag |= 2;
1588 switch (flag)
1590 case 0:
1591 if (hi)
1593 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1594 hi = 0;
1596 break;
1597 case 1:
1598 break; /* atom, nothing to do */
1599 case 3:
1600 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1601 /* fall thru */
1602 case 2:
1603 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1604 break;
1606 *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1608 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1609 case WM_DDE_EXECUTE:
1610 *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1611 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1612 default: /* No translation needed */
1613 return 0;
1618 /**********************************************************************
1619 * WINPROC_UnmapMsg16To32A
1621 * Unmap a message that was mapped from 16- to 32-bit Ansi.
1623 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1624 LRESULT result )
1626 switch(msg)
1628 case WM_COMPAREITEM:
1629 case WM_DELETEITEM:
1630 case WM_DRAWITEM:
1631 HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1632 break;
1633 case WM_MEASUREITEM:
1635 MEASUREITEMSTRUCT16 *mis16;
1636 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1637 lParam = *(LPARAM *)(mis + 1);
1638 mis16 = MapSL(lParam);
1639 mis16->itemWidth = (UINT16)mis->itemWidth;
1640 mis16->itemHeight = (UINT16)mis->itemHeight;
1641 HeapFree( GetProcessHeap(), 0, mis );
1643 break;
1644 case WM_GETMINMAXINFO:
1646 MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1647 lParam = *(LPARAM *)(mmi + 1);
1648 MINMAXINFO32to16( mmi, MapSL(lParam));
1649 HeapFree( GetProcessHeap(), 0, mmi );
1651 break;
1652 case WM_MDICREATE:
1654 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1655 lParam = *(LPARAM *)(cs + 1);
1656 MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1657 HeapFree( GetProcessHeap(), 0, cs );
1659 break;
1660 case WM_MDIGETACTIVE:
1661 result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1662 HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1663 break;
1664 case WM_NCCALCSIZE:
1666 NCCALCSIZE_PARAMS16 *nc16;
1667 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1668 lParam = *(LPARAM *)(nc + 1);
1669 nc16 = MapSL(lParam);
1670 nc16->rgrc[0].left = nc->rgrc[0].left;
1671 nc16->rgrc[0].top = nc->rgrc[0].top;
1672 nc16->rgrc[0].right = nc->rgrc[0].right;
1673 nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1674 if (wParam)
1676 nc16->rgrc[1].left = nc->rgrc[1].left;
1677 nc16->rgrc[1].top = nc->rgrc[1].top;
1678 nc16->rgrc[1].right = nc->rgrc[1].right;
1679 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1680 nc16->rgrc[2].left = nc->rgrc[2].left;
1681 nc16->rgrc[2].top = nc->rgrc[2].top;
1682 nc16->rgrc[2].right = nc->rgrc[2].right;
1683 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1684 if (nc->lppos)
1686 WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1687 HeapFree( GetProcessHeap(), 0, nc->lppos );
1690 HeapFree( GetProcessHeap(), 0, nc );
1692 break;
1693 case WM_NCCREATE:
1694 case WM_CREATE:
1696 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1697 lParam = *(LPARAM *)(cs + 1);
1698 CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1700 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1701 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1703 HeapFree( GetProcessHeap(), 0, cs );
1705 break;
1706 case WM_WINDOWPOSCHANGING:
1707 case WM_WINDOWPOSCHANGED:
1709 WINDOWPOS *wp = (WINDOWPOS *)lParam;
1710 lParam = *(LPARAM *)(wp + 1);
1711 WINDOWPOS32to16(wp, MapSL(lParam));
1712 HeapFree( GetProcessHeap(), 0, wp );
1714 break;
1715 case WM_GETDLGCODE:
1716 if (lParam)
1718 LPMSG msg32 = (LPMSG)lParam;
1720 WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1721 result);
1722 HeapFree( GetProcessHeap(), 0, msg32 );
1724 break;
1725 case WM_NEXTMENU:
1727 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1728 result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1729 HeapFree( GetProcessHeap(), 0, next );
1731 break;
1733 return result;
1737 /**********************************************************************
1738 * WINPROC_MapMsg16To32W
1740 * Map a message from 16- to 32-bit Unicode.
1741 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1743 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1744 WPARAM *pwparam32, LPARAM *plparam )
1746 BYTE ch;
1747 WCHAR wch;
1749 *pmsg32=(UINT)msg16;
1750 *pwparam32 = (WPARAM)wParam16;
1751 switch(msg16)
1753 case WM_GETTEXT:
1754 case WM_SETTEXT:
1755 case WM_WININICHANGE:
1756 case WM_DEVMODECHANGE:
1757 case WM_ASKCBFORMATNAME:
1758 *plparam = (LPARAM)MapSL(*plparam);
1759 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1760 case WM_GETTEXTLENGTH:
1761 case CB_GETLBTEXTLEN:
1762 case LB_GETTEXTLEN:
1763 return 1; /* need to map result */
1764 case WM_NCCREATE:
1765 case WM_CREATE:
1767 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1768 CREATESTRUCTW *cs = (CREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1769 sizeof(*cs) + sizeof(LPARAM) );
1770 if (!cs) return -1;
1771 CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1772 cs->lpszName = map_str_16_to_32W(cs16->lpszName);
1773 cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1775 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1777 MDICREATESTRUCT16 *mdi_cs16;
1778 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
1779 sizeof(*mdi_cs));
1780 if (!mdi_cs)
1782 HeapFree(GetProcessHeap(), 0, cs);
1783 return -1;
1785 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1786 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1787 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1788 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1790 cs->lpCreateParams = mdi_cs;
1792 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1793 *plparam = (LPARAM)cs;
1795 return 1;
1796 case WM_MDICREATE:
1798 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1799 MDICREATESTRUCTW *cs =
1800 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1801 sizeof(*cs) + sizeof(LPARAM) );
1802 if (!cs) return -1;
1803 MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1804 cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1805 cs->szClass = map_str_16_to_32W(cs16->szClass);
1806 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1807 *plparam = (LPARAM)cs;
1809 return 1;
1810 case WM_GETDLGCODE:
1811 if (*plparam)
1813 LPMSG16 msg16 = MapSL(*plparam);
1814 LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1816 if (!msg32) return -1;
1817 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1818 msg32->lParam = msg16->lParam;
1819 msg32->time = msg16->time;
1820 msg32->pt.x = msg16->pt.x;
1821 msg32->pt.y = msg16->pt.y;
1822 /* this is right, right? */
1823 if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1824 &msg32->message,&msg32->wParam,
1825 &msg32->lParam)<0) {
1826 HeapFree( GetProcessHeap(), 0, msg32 );
1827 return -1;
1829 *plparam = (LPARAM)msg32;
1830 return 1;
1832 else return 0;
1834 case WM_CHARTOITEM:
1835 ch = wParam16;
1836 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1837 *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1838 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1839 return 0;
1840 case WM_MENUCHAR:
1841 ch = wParam16;
1842 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1843 *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1844 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1845 return 0;
1846 case WM_CHAR:
1847 case WM_DEADCHAR:
1848 case WM_SYSCHAR:
1849 case WM_SYSDEADCHAR:
1850 ch = wParam16;
1851 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1852 *pwparam32 = wch;
1853 return 0;
1854 case WM_IME_CHAR:
1855 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1857 default: /* No Unicode translation needed */
1858 return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1859 pwparam32, plparam );
1864 /**********************************************************************
1865 * WINPROC_UnmapMsg16To32W
1867 * Unmap a message that was mapped from 16- to 32-bit Unicode.
1869 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1870 LRESULT result )
1872 switch(msg)
1874 case WM_GETTEXT:
1875 case WM_SETTEXT:
1876 case WM_GETTEXTLENGTH:
1877 case CB_GETLBTEXTLEN:
1878 case LB_GETTEXTLEN:
1879 case WM_ASKCBFORMATNAME:
1880 return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
1881 case WM_NCCREATE:
1882 case WM_CREATE:
1884 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1885 lParam = *(LPARAM *)(cs + 1);
1886 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1887 unmap_str_16_to_32W( cs->lpszName );
1888 unmap_str_16_to_32W( cs->lpszClass );
1890 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1892 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1893 unmap_str_16_to_32W( mdi_cs->szTitle );
1894 unmap_str_16_to_32W( mdi_cs->szClass );
1895 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1897 HeapFree( GetProcessHeap(), 0, cs );
1899 break;
1900 case WM_MDICREATE:
1902 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1903 lParam = *(LPARAM *)(cs + 1);
1904 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1905 unmap_str_16_to_32W( cs->szTitle );
1906 unmap_str_16_to_32W( cs->szClass );
1907 HeapFree( GetProcessHeap(), 0, cs );
1909 break;
1910 case WM_GETDLGCODE:
1911 if (lParam)
1913 LPMSG msg32 = (LPMSG)lParam;
1915 WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1916 result);
1917 HeapFree( GetProcessHeap(), 0, msg32 );
1919 break;
1920 default:
1921 return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1923 return result;
1926 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1928 HANDLE16 dst;
1929 UINT sz = GlobalSize((HANDLE)src);
1930 LPSTR ptr16, ptr32;
1932 if (!(dst = GlobalAlloc16(flags, sz)))
1933 return 0;
1934 ptr32 = GlobalLock((HANDLE)src);
1935 ptr16 = GlobalLock16(dst);
1936 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1937 GlobalUnlock((HANDLE)src);
1938 GlobalUnlock16(dst);
1940 return dst;
1944 /**********************************************************************
1945 * WINPROC_MapMsg32ATo16
1947 * Map a message from 32-bit Ansi to 16-bit.
1948 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1950 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1951 UINT16 *pmsg16, WPARAM16 *pwparam16,
1952 LPARAM *plparam )
1954 *pmsg16 = (UINT16)msg32;
1955 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1956 switch(msg32)
1958 case SBM_SETRANGE:
1959 *pmsg16 = SBM_SETRANGE16;
1960 *plparam = MAKELPARAM(wParam32, *plparam);
1961 *pwparam16 = 0;
1962 return 0;
1964 case SBM_GETRANGE:
1965 *pmsg16 = SBM_GETRANGE16;
1966 return 1;
1968 case BM_GETCHECK:
1969 case BM_SETCHECK:
1970 case BM_GETSTATE:
1971 case BM_SETSTATE:
1972 case BM_SETSTYLE:
1973 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
1974 return 0;
1976 case EM_GETSEL:
1977 case EM_GETRECT:
1978 case EM_SETRECT:
1979 case EM_SETRECTNP:
1980 case EM_SCROLL:
1981 case EM_LINESCROLL:
1982 case EM_SCROLLCARET:
1983 case EM_GETMODIFY:
1984 case EM_SETMODIFY:
1985 case EM_GETLINECOUNT:
1986 case EM_LINEINDEX:
1987 case EM_SETHANDLE:
1988 case EM_GETHANDLE:
1989 case EM_GETTHUMB:
1990 case EM_LINELENGTH:
1991 case EM_REPLACESEL:
1992 case EM_GETLINE:
1993 case EM_LIMITTEXT:
1994 case EM_CANUNDO:
1995 case EM_UNDO:
1996 case EM_FMTLINES:
1997 case EM_LINEFROMCHAR:
1998 case EM_SETTABSTOPS:
1999 case EM_SETPASSWORDCHAR:
2000 case EM_EMPTYUNDOBUFFER:
2001 case EM_GETFIRSTVISIBLELINE:
2002 case EM_SETREADONLY:
2003 case EM_SETWORDBREAKPROC:
2004 case EM_GETWORDBREAKPROC:
2005 case EM_GETPASSWORDCHAR:
2006 *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
2007 return 0;
2009 case LB_CARETOFF:
2010 case LB_CARETON:
2011 case LB_DELETESTRING:
2012 case LB_GETANCHORINDEX:
2013 case LB_GETCARETINDEX:
2014 case LB_GETCOUNT:
2015 case LB_GETCURSEL:
2016 case LB_GETHORIZONTALEXTENT:
2017 case LB_GETITEMDATA:
2018 case LB_GETITEMHEIGHT:
2019 case LB_GETSEL:
2020 case LB_GETSELCOUNT:
2021 case LB_GETTEXTLEN:
2022 case LB_GETTOPINDEX:
2023 case LB_RESETCONTENT:
2024 case LB_SELITEMRANGE:
2025 case LB_SELITEMRANGEEX:
2026 case LB_SETANCHORINDEX:
2027 case LB_SETCARETINDEX:
2028 case LB_SETCOLUMNWIDTH:
2029 case LB_SETCURSEL:
2030 case LB_SETHORIZONTALEXTENT:
2031 case LB_SETITEMDATA:
2032 case LB_SETITEMHEIGHT:
2033 case LB_SETSEL:
2034 case LB_SETTOPINDEX:
2035 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2036 return 0;
2037 case CB_DELETESTRING:
2038 case CB_GETCOUNT:
2039 case CB_GETLBTEXTLEN:
2040 case CB_LIMITTEXT:
2041 case CB_RESETCONTENT:
2042 case CB_SETEDITSEL:
2043 case CB_GETCURSEL:
2044 case CB_SETCURSEL:
2045 case CB_SHOWDROPDOWN:
2046 case CB_SETITEMDATA:
2047 case CB_SETITEMHEIGHT:
2048 case CB_GETITEMHEIGHT:
2049 case CB_SETEXTENDEDUI:
2050 case CB_GETEXTENDEDUI:
2051 case CB_GETDROPPEDSTATE:
2052 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2053 return 0;
2054 case CB_GETEDITSEL:
2055 *pmsg16 = CB_GETEDITSEL16;
2056 return 1;
2058 case LB_ADDSTRING:
2059 case LB_FINDSTRING:
2060 case LB_FINDSTRINGEXACT:
2061 case LB_INSERTSTRING:
2062 case LB_SELECTSTRING:
2063 case LB_DIR:
2064 case LB_ADDFILE:
2065 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2066 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2067 return 1;
2069 case CB_ADDSTRING:
2070 case CB_FINDSTRING:
2071 case CB_FINDSTRINGEXACT:
2072 case CB_INSERTSTRING:
2073 case CB_SELECTSTRING:
2074 case CB_DIR:
2075 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2076 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2077 return 1;
2079 case LB_GETITEMRECT:
2081 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2082 if (!rect) return -1;
2083 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2084 *plparam = MapLS( rect );
2086 *pmsg16 = LB_GETITEMRECT16;
2087 return 1;
2088 case LB_GETSELITEMS:
2090 LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2092 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2093 if (!(items = HeapAlloc( GetProcessHeap(), 0,
2094 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2095 *items++ = *plparam; /* Store the previous lParam */
2096 *plparam = MapLS( items );
2098 *pmsg16 = LB_GETSELITEMS16;
2099 return 1;
2100 case LB_SETTABSTOPS:
2101 if (wParam32)
2103 INT i;
2104 LPINT16 stops;
2105 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2106 if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2107 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2108 for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2109 *plparam = MapLS( stops );
2110 return 1;
2112 *pmsg16 = LB_SETTABSTOPS16;
2113 return 0;
2115 case CB_GETDROPPEDCONTROLRECT:
2117 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2118 if (!rect) return -1;
2119 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2120 *plparam = (LPARAM)MapLS(rect);
2122 *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2123 return 1;
2125 case LB_GETTEXT:
2126 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2127 *pmsg16 = LB_GETTEXT16;
2128 return 1;
2130 case CB_GETLBTEXT:
2131 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2132 *pmsg16 = CB_GETLBTEXT16;
2133 return 1;
2135 case EM_SETSEL:
2136 *pwparam16 = 0;
2137 *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2138 *pmsg16 = EM_SETSEL16;
2139 return 0;
2141 case WM_ACTIVATE:
2142 case WM_CHARTOITEM:
2143 case WM_COMMAND:
2144 case WM_VKEYTOITEM:
2145 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2146 return 0;
2147 case WM_HSCROLL:
2148 case WM_VSCROLL:
2149 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2150 return 0;
2151 case WM_CTLCOLORMSGBOX:
2152 case WM_CTLCOLOREDIT:
2153 case WM_CTLCOLORLISTBOX:
2154 case WM_CTLCOLORBTN:
2155 case WM_CTLCOLORDLG:
2156 case WM_CTLCOLORSCROLLBAR:
2157 case WM_CTLCOLORSTATIC:
2158 *pmsg16 = WM_CTLCOLOR;
2159 *plparam = MAKELPARAM( (HWND16)*plparam,
2160 (WORD)msg32 - WM_CTLCOLORMSGBOX );
2161 return 0;
2162 case WM_COMPAREITEM:
2164 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2165 COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2166 if (!cis) return -1;
2167 cis->CtlType = (UINT16)cis32->CtlType;
2168 cis->CtlID = (UINT16)cis32->CtlID;
2169 cis->hwndItem = HWND_16( cis32->hwndItem );
2170 cis->itemID1 = (UINT16)cis32->itemID1;
2171 cis->itemData1 = cis32->itemData1;
2172 cis->itemID2 = (UINT16)cis32->itemID2;
2173 cis->itemData2 = cis32->itemData2;
2174 *plparam = MapLS( cis );
2176 return 1;
2177 case WM_DELETEITEM:
2179 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2180 DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2181 if (!dis) return -1;
2182 dis->CtlType = (UINT16)dis32->CtlType;
2183 dis->CtlID = (UINT16)dis32->CtlID;
2184 dis->itemID = (UINT16)dis32->itemID;
2185 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2186 : HWND_16( dis32->hwndItem );
2187 dis->itemData = dis32->itemData;
2188 *plparam = MapLS( dis );
2190 return 1;
2191 case WM_DRAWITEM:
2193 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2194 DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2195 if (!dis) return -1;
2196 dis->CtlType = (UINT16)dis32->CtlType;
2197 dis->CtlID = (UINT16)dis32->CtlID;
2198 dis->itemID = (UINT16)dis32->itemID;
2199 dis->itemAction = (UINT16)dis32->itemAction;
2200 dis->itemState = (UINT16)dis32->itemState;
2201 dis->hwndItem = HWND_16( dis32->hwndItem );
2202 dis->hDC = HDC_16(dis32->hDC);
2203 dis->itemData = dis32->itemData;
2204 dis->rcItem.left = dis32->rcItem.left;
2205 dis->rcItem.top = dis32->rcItem.top;
2206 dis->rcItem.right = dis32->rcItem.right;
2207 dis->rcItem.bottom = dis32->rcItem.bottom;
2208 *plparam = MapLS( dis );
2210 return 1;
2211 case WM_MEASUREITEM:
2213 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2214 MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2215 if (!mis) return -1;
2216 mis->CtlType = (UINT16)mis32->CtlType;
2217 mis->CtlID = (UINT16)mis32->CtlID;
2218 mis->itemID = (UINT16)mis32->itemID;
2219 mis->itemWidth = (UINT16)mis32->itemWidth;
2220 mis->itemHeight = (UINT16)mis32->itemHeight;
2221 mis->itemData = mis32->itemData;
2222 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
2223 *plparam = MapLS( mis );
2225 return 1;
2226 case WM_GETMINMAXINFO:
2228 MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2229 if (!mmi) return -1;
2230 MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2231 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
2232 *plparam = MapLS( mmi );
2234 return 1;
2235 case WM_GETTEXT:
2236 case WM_ASKCBFORMATNAME:
2238 LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2239 *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2240 if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2241 *str++ = *plparam; /* Store the previous lParam */
2242 *plparam = MapLS( str );
2244 return 1;
2245 case WM_MDICREATE:
2247 MDICREATESTRUCT16 *cs;
2248 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2250 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2251 MDICREATESTRUCT32Ato16( cs32, cs );
2252 cs->szTitle = MapLS( cs32->szTitle );
2253 cs->szClass = MapLS( cs32->szClass );
2254 *plparam = MapLS( cs );
2256 return 1;
2257 case WM_MDIGETACTIVE:
2258 return 1;
2259 case WM_MDISETMENU:
2260 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2261 (HMENU16)LOWORD(*plparam) );
2262 *pwparam16 = (*plparam == 0);
2263 return 0;
2264 case WM_MENUSELECT:
2265 if(HIWORD(wParam32) & MF_POPUP)
2267 HMENU hmenu;
2268 if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2270 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2271 *pwparam16=HMENU_16(hmenu);
2274 /* fall through */
2275 case WM_MENUCHAR:
2276 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2277 return 0;
2278 case WM_MDIACTIVATE:
2279 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2281 *pwparam16 = ((HWND)*plparam == hwnd);
2282 *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2283 (HWND16)LOWORD(wParam32) );
2285 else
2287 *pwparam16 = HWND_16( (HWND)wParam32 );
2288 *plparam = 0;
2290 return 0;
2291 case WM_NCCALCSIZE:
2293 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2294 NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2295 if (!nc) return -1;
2297 nc->rgrc[0].left = nc32->rgrc[0].left;
2298 nc->rgrc[0].top = nc32->rgrc[0].top;
2299 nc->rgrc[0].right = nc32->rgrc[0].right;
2300 nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2301 if (wParam32)
2303 WINDOWPOS16 *wp;
2304 nc->rgrc[1].left = nc32->rgrc[1].left;
2305 nc->rgrc[1].top = nc32->rgrc[1].top;
2306 nc->rgrc[1].right = nc32->rgrc[1].right;
2307 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2308 nc->rgrc[2].left = nc32->rgrc[2].left;
2309 nc->rgrc[2].top = nc32->rgrc[2].top;
2310 nc->rgrc[2].right = nc32->rgrc[2].right;
2311 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2312 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2314 HeapFree( GetProcessHeap(), 0, nc );
2315 return -1;
2317 WINDOWPOS32to16( nc32->lppos, wp );
2318 nc->lppos = MapLS( wp );
2320 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
2321 *plparam = MapLS( nc );
2323 return 1;
2324 case WM_NCCREATE:
2325 case WM_CREATE:
2327 CREATESTRUCT16 *cs;
2328 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2330 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2331 CREATESTRUCT32Ato16( cs32, cs );
2332 cs->lpszName = MapLS( cs32->lpszName );
2333 cs->lpszClass = MapLS( cs32->lpszClass );
2335 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2337 MDICREATESTRUCT16 *mdi_cs16;
2338 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2339 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2340 if (!mdi_cs16)
2342 HeapFree(GetProcessHeap(), 0, cs);
2343 return -1;
2345 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2346 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2347 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2348 cs->lpCreateParams = MapLS( mdi_cs16 );
2350 *plparam = MapLS( cs );
2352 return 1;
2353 case WM_PARENTNOTIFY:
2354 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2355 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2356 /* else nothing to do */
2357 return 0;
2358 case WM_NOTIFY:
2359 *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2360 return 1;
2361 case WM_SETTEXT:
2362 case WM_WININICHANGE:
2363 case WM_DEVMODECHANGE:
2364 *plparam = MapLS( (LPSTR)*plparam );
2365 return 1;
2366 case WM_WINDOWPOSCHANGING:
2367 case WM_WINDOWPOSCHANGED:
2369 WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2370 if (!wp) return -1;
2371 WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2372 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
2373 *plparam = MapLS( wp );
2375 return 1;
2376 case WM_GETDLGCODE:
2377 if (*plparam) {
2378 LPMSG msg32 = (LPMSG) *plparam;
2379 LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2381 if (!msg16) return -1;
2382 msg16->hwnd = HWND_16( msg32->hwnd );
2383 msg16->lParam = msg32->lParam;
2384 msg16->time = msg32->time;
2385 msg16->pt.x = msg32->pt.x;
2386 msg16->pt.y = msg32->pt.y;
2387 /* this is right, right? */
2388 if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2389 &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2391 HeapFree( GetProcessHeap(), 0, msg16 );
2392 return -1;
2394 *plparam = MapLS( msg16 );
2395 return 1;
2397 return 0;
2399 case WM_ACTIVATEAPP:
2400 if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2401 return 0;
2402 case WM_NEXTMENU:
2404 MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2405 *plparam = (LPARAM)next->hmenuIn;
2406 return 1;
2408 case WM_PAINT:
2409 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2411 *pmsg16 = WM_PAINTICON;
2412 *pwparam16 = 1;
2414 return 0;
2415 case WM_ERASEBKGND:
2416 if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2417 *pmsg16 = WM_ICONERASEBKGND;
2418 return 0;
2419 case WM_PAINTCLIPBOARD:
2420 case WM_SIZECLIPBOARD:
2421 FIXME_(msg)("message %04x needs translation\n", msg32 );
2422 return -1;
2423 /* following messages should not be sent to 16-bit apps */
2424 case WM_SIZING:
2425 case WM_MOVING:
2426 case WM_CAPTURECHANGED:
2427 case WM_STYLECHANGING:
2428 case WM_STYLECHANGED:
2429 return -1;
2430 case WM_DDE_INITIATE:
2431 case WM_DDE_TERMINATE:
2432 case WM_DDE_UNADVISE:
2433 case WM_DDE_REQUEST:
2434 *pwparam16 = HWND_16((HWND)wParam32);
2435 return 0;
2436 case WM_DDE_ADVISE:
2437 case WM_DDE_DATA:
2438 case WM_DDE_POKE:
2440 UINT lo32, hi;
2441 HANDLE16 lo16 = 0;
2443 *pwparam16 = HWND_16((HWND)wParam32);
2444 UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2445 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2446 return -1;
2447 *plparam = MAKELPARAM(lo16, hi);
2449 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2450 case WM_DDE_ACK:
2452 UINT lo, hi;
2453 int flag = 0;
2454 char buf[2];
2456 *pwparam16 = HWND_16((HWND)wParam32);
2458 UnpackDDElParam(msg32, *plparam, &lo, &hi);
2460 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2461 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2462 switch (flag)
2464 case 0:
2465 if (hi)
2467 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2468 hi = 0;
2470 break;
2471 case 1:
2472 break; /* atom, nothing to do */
2473 case 3:
2474 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2475 /* fall thru */
2476 case 2:
2477 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2478 break;
2480 *plparam = MAKELPARAM(lo, hi);
2482 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2483 case WM_DDE_EXECUTE:
2484 *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2485 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2486 default: /* No translation needed */
2487 return 0;
2492 /**********************************************************************
2493 * WINPROC_UnmapMsg32ATo16
2495 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2497 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2498 MSGPARAM16* p16 )
2500 switch(msg)
2502 case SBM_GETRANGE:
2503 *(LPINT)wParam = LOWORD(p16->lResult);
2504 *(LPINT)lParam = HIWORD(p16->lResult);
2505 break;
2507 case LB_ADDFILE:
2508 case LB_ADDSTRING:
2509 case LB_DIR:
2510 case LB_FINDSTRING:
2511 case LB_FINDSTRINGEXACT:
2512 case LB_INSERTSTRING:
2513 case LB_SELECTSTRING:
2514 case LB_GETTEXT:
2515 case CB_ADDSTRING:
2516 case CB_FINDSTRING:
2517 case CB_FINDSTRINGEXACT:
2518 case CB_INSERTSTRING:
2519 case CB_SELECTSTRING:
2520 case CB_DIR:
2521 case CB_GETLBTEXT:
2522 case WM_SETTEXT:
2523 case WM_WININICHANGE:
2524 case WM_DEVMODECHANGE:
2525 UnMapLS( (SEGPTR)p16->lParam );
2526 break;
2527 case LB_SETTABSTOPS:
2528 case WM_COMPAREITEM:
2529 case WM_DELETEITEM:
2530 case WM_DRAWITEM:
2532 void *ptr = MapSL( p16->lParam );
2533 UnMapLS( p16->lParam );
2534 HeapFree( GetProcessHeap(), 0, ptr );
2536 break;
2537 case CB_GETDROPPEDCONTROLRECT:
2538 case LB_GETITEMRECT:
2540 RECT *r32;
2541 RECT16 *rect = MapSL(p16->lParam);
2542 UnMapLS( p16->lParam );
2543 p16->lParam = *(LPARAM *)(rect + 1);
2544 r32 = (RECT *)p16->lParam;
2545 r32->left = rect->left;
2546 r32->top = rect->top;
2547 r32->right = rect->right;
2548 r32->bottom = rect->bottom;
2549 HeapFree( GetProcessHeap(), 0, rect );
2551 break;
2552 case LB_GETSELITEMS:
2554 INT i;
2555 LPINT16 items = MapSL(p16->lParam);
2556 UnMapLS( p16->lParam );
2557 p16->lParam = *((LPARAM *)items - 1);
2558 for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2559 HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2561 break;
2563 case CB_GETEDITSEL:
2564 if( wParam )
2565 *((PUINT)(wParam)) = LOWORD(p16->lResult);
2566 if( lParam )
2567 *((PUINT)(lParam)) = HIWORD(p16->lResult); /* FIXME: substract 1? */
2568 break;
2570 case WM_MEASUREITEM:
2572 MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2573 MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2574 mis32->itemWidth = mis->itemWidth;
2575 mis32->itemHeight = mis->itemHeight;
2576 UnMapLS( p16->lParam );
2577 HeapFree( GetProcessHeap(), 0, mis );
2579 break;
2580 case WM_GETMINMAXINFO:
2582 MINMAXINFO16 *mmi = MapSL(p16->lParam);
2583 UnMapLS( p16->lParam );
2584 p16->lParam = *(LPARAM *)(mmi + 1);
2585 MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2586 HeapFree( GetProcessHeap(), 0, mmi );
2588 break;
2589 case WM_GETTEXT:
2590 case WM_ASKCBFORMATNAME:
2592 LPSTR str = MapSL(p16->lParam);
2593 UnMapLS( p16->lParam );
2594 p16->lParam = *((LPARAM *)str - 1);
2595 lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2596 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2598 break;
2599 case WM_MDICREATE:
2601 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2602 UnMapLS( cs->szTitle );
2603 UnMapLS( cs->szClass );
2604 UnMapLS( p16->lParam );
2605 HeapFree( GetProcessHeap(), 0, cs );
2607 break;
2608 case WM_MDIGETACTIVE:
2609 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2610 p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2611 break;
2612 case WM_NCCALCSIZE:
2614 NCCALCSIZE_PARAMS *nc32;
2615 NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2616 UnMapLS( p16->lParam );
2617 p16->lParam = *(LPARAM *)(nc + 1);
2618 nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2619 nc32->rgrc[0].left = nc->rgrc[0].left;
2620 nc32->rgrc[0].top = nc->rgrc[0].top;
2621 nc32->rgrc[0].right = nc->rgrc[0].right;
2622 nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2623 if (p16->wParam)
2625 WINDOWPOS16 *pos = MapSL(nc->lppos);
2626 UnMapLS( nc->lppos );
2627 nc32->rgrc[1].left = nc->rgrc[1].left;
2628 nc32->rgrc[1].top = nc->rgrc[1].top;
2629 nc32->rgrc[1].right = nc->rgrc[1].right;
2630 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2631 nc32->rgrc[2].left = nc->rgrc[2].left;
2632 nc32->rgrc[2].top = nc->rgrc[2].top;
2633 nc32->rgrc[2].right = nc->rgrc[2].right;
2634 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2635 WINDOWPOS16to32( pos, nc32->lppos );
2636 HeapFree( GetProcessHeap(), 0, pos );
2638 HeapFree( GetProcessHeap(), 0, nc );
2640 break;
2641 case WM_NCCREATE:
2642 case WM_CREATE:
2644 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2645 UnMapLS( p16->lParam );
2646 UnMapLS( cs->lpszName );
2647 UnMapLS( cs->lpszClass );
2648 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2650 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2651 UnMapLS( cs->lpCreateParams );
2652 UnMapLS( mdi_cs16->szTitle );
2653 UnMapLS( mdi_cs16->szClass );
2654 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2656 HeapFree( GetProcessHeap(), 0, cs );
2658 break;
2659 case WM_WINDOWPOSCHANGING:
2660 case WM_WINDOWPOSCHANGED:
2662 WINDOWPOS16 *wp = MapSL(p16->lParam);
2663 UnMapLS( p16->lParam );
2664 p16->lParam = *(LPARAM *)(wp + 1);
2665 WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2666 HeapFree( GetProcessHeap(), 0, wp );
2668 break;
2669 case WM_NOTIFY:
2670 UnMapLS(p16->lParam);
2671 break;
2672 case WM_GETDLGCODE:
2673 if (p16->lParam)
2675 LPMSG16 msg16 = MapSL(p16->lParam);
2676 MSGPARAM16 msgp16;
2677 UnMapLS( p16->lParam );
2678 msgp16.wParam=msg16->wParam;
2679 msgp16.lParam=msg16->lParam;
2680 WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2681 ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2682 &msgp16 );
2683 HeapFree( GetProcessHeap(), 0, msg16 );
2685 break;
2686 case WM_NEXTMENU:
2688 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2689 next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2690 next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2691 p16->lResult = 0;
2693 break;
2698 /**********************************************************************
2699 * WINPROC_MapMsg32WTo16
2701 * Map a message from 32-bit Unicode to 16-bit.
2702 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2704 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2705 UINT16 *pmsg16, WPARAM16 *pwparam16,
2706 LPARAM *plparam )
2708 BYTE ch;
2709 WCHAR wch;
2711 *pmsg16 = LOWORD(msg32);
2712 *pwparam16 = LOWORD(wParam32);
2713 switch(msg32)
2715 case LB_ADDSTRING:
2716 case LB_FINDSTRING:
2717 case LB_FINDSTRINGEXACT:
2718 case LB_INSERTSTRING:
2719 case LB_SELECTSTRING:
2720 case LB_DIR:
2721 case LB_ADDFILE:
2722 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2723 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2724 return 1;
2726 case CB_ADDSTRING:
2727 case CB_FINDSTRING:
2728 case CB_FINDSTRINGEXACT:
2729 case CB_INSERTSTRING:
2730 case CB_SELECTSTRING:
2731 case CB_DIR:
2732 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2733 *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2734 return 1;
2736 case WM_NCCREATE:
2737 case WM_CREATE:
2739 CREATESTRUCT16 *cs;
2740 CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2742 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2743 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2744 cs->lpszName = map_str_32W_to_16( cs32->lpszName );
2745 cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2747 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2749 MDICREATESTRUCT16 *mdi_cs16;
2750 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2751 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2752 if (!mdi_cs16)
2754 HeapFree(GetProcessHeap(), 0, cs);
2755 return -1;
2757 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2758 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2759 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2760 cs->lpCreateParams = MapLS(mdi_cs16);
2762 *plparam = MapLS(cs);
2764 return 1;
2765 case WM_MDICREATE:
2767 MDICREATESTRUCT16 *cs;
2768 MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2770 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2771 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2772 cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2773 cs->szClass = map_str_32W_to_16( cs32->szClass );
2774 *plparam = MapLS(cs);
2776 return 1;
2777 case WM_SETTEXT:
2778 case WM_WININICHANGE:
2779 case WM_DEVMODECHANGE:
2780 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2781 return 1;
2782 case LB_GETTEXT:
2783 if ( WINPROC_TestLBForStr( hwnd ))
2785 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2786 if (!str) return -1;
2787 *pmsg16 = LB_GETTEXT16;
2788 *plparam = (LPARAM)MapLS(str);
2790 return 1;
2791 case CB_GETLBTEXT:
2792 if ( WINPROC_TestCBForStr( hwnd ))
2794 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 512 ); /* FIXME: fixed sized buffer */
2795 if (!str) return -1;
2796 *pmsg16 = CB_GETLBTEXT16;
2797 *plparam = (LPARAM)MapLS(str);
2799 return 1;
2801 case WM_CHARTOITEM:
2802 wch = LOWORD(wParam32);
2803 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2804 *pwparam16 = ch;
2805 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2806 return 0;
2807 case WM_MENUCHAR:
2808 wch = LOWORD(wParam32);
2809 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2810 *pwparam16 = ch;
2811 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2812 return 0;
2813 case WM_CHAR:
2814 case WM_DEADCHAR:
2815 case WM_SYSCHAR:
2816 case WM_SYSDEADCHAR:
2817 wch = wParam32;
2818 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2819 *pwparam16 = ch;
2820 return 0;
2821 case WM_IME_CHAR:
2823 BYTE ch[2];
2825 wch = wParam32;
2826 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
2827 *pwparam16 = (ch[0] << 8) | ch[1];
2828 else
2829 *pwparam16 = ch[0];
2831 return 0;
2833 default: /* No Unicode translation needed (?) */
2834 return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2835 pwparam16, plparam );
2840 /**********************************************************************
2841 * WINPROC_UnmapMsg32WTo16
2843 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2845 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2846 MSGPARAM16* p16 )
2848 switch(msg)
2850 case LB_ADDSTRING:
2851 case LB_FINDSTRING:
2852 case LB_FINDSTRINGEXACT:
2853 case LB_INSERTSTRING:
2854 case LB_SELECTSTRING:
2855 case LB_DIR:
2856 case LB_ADDFILE:
2857 case CB_ADDSTRING:
2858 case CB_FINDSTRING:
2859 case CB_FINDSTRINGEXACT:
2860 case CB_INSERTSTRING:
2861 case CB_SELECTSTRING:
2862 case CB_DIR:
2863 case WM_SETTEXT:
2864 case WM_WININICHANGE:
2865 case WM_DEVMODECHANGE:
2866 unmap_str_32W_to_16( p16->lParam );
2867 break;
2868 case WM_NCCREATE:
2869 case WM_CREATE:
2871 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2872 UnMapLS( p16->lParam );
2873 unmap_str_32W_to_16( cs->lpszName );
2874 unmap_str_32W_to_16( cs->lpszClass );
2876 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2878 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2879 UnMapLS( cs->lpCreateParams );
2880 unmap_str_32W_to_16(mdi_cs16->szTitle);
2881 unmap_str_32W_to_16(mdi_cs16->szClass);
2882 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2884 HeapFree( GetProcessHeap(), 0, cs );
2886 break;
2887 case WM_MDICREATE:
2889 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2890 UnMapLS( p16->lParam );
2891 unmap_str_32W_to_16( cs->szTitle );
2892 unmap_str_32W_to_16( cs->szClass );
2893 HeapFree( GetProcessHeap(), 0, cs );
2895 break;
2896 case WM_GETTEXT:
2897 case WM_ASKCBFORMATNAME:
2899 LPSTR str = MapSL(p16->lParam);
2900 UnMapLS( p16->lParam );
2901 p16->lParam = *((LPARAM *)str - 1);
2902 MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2903 p16->lResult = strlenW( (LPWSTR)p16->lParam );
2904 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2906 break;
2907 case LB_GETTEXT:
2908 if ( WINPROC_TestLBForStr( hwnd ))
2910 LPSTR str = MapSL(p16->lParam);
2911 UnMapLS( p16->lParam );
2912 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2913 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2915 break;
2916 case CB_GETLBTEXT:
2917 if ( WINPROC_TestCBForStr( hwnd ))
2919 LPSTR str = MapSL(p16->lParam);
2920 UnMapLS( p16->lParam );
2921 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2922 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2924 break;
2925 default:
2926 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2927 break;
2932 /**********************************************************************
2933 * WINPROC_CallProc32ATo32W
2935 * Call a window procedure, translating args from Ansi to Unicode.
2937 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
2938 UINT msg, WPARAM wParam,
2939 LPARAM lParam )
2941 LRESULT result;
2942 int unmap;
2944 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2945 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2947 if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2948 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2949 SPY_GetMsgName(msg, hwnd), wParam, lParam );
2950 return 0;
2952 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2953 if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
2954 return result;
2958 /**********************************************************************
2959 * WINPROC_CallProc32WTo32A_fast
2962 static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
2963 UINT msg, WPARAM wParam,
2964 LPARAM lParam, LRESULT *result )
2966 switch(msg)
2968 case WM_NCCREATE:
2969 case WM_CREATE:
2970 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
2971 * at this point.
2973 char buffer[1024];
2974 char *cls = buffer, *name;
2975 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
2976 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
2977 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
2979 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
2980 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
2982 if (csW->lpszName)
2984 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
2985 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
2987 else
2988 name_lenW = name_lenA = 0;
2990 if (class_lenA + name_lenA + 2 > sizeof(buffer))
2992 cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
2993 if (!cls) return FALSE;
2996 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
2997 cls[class_lenA] = 0;
2998 csA.lpszClass = cls;
3000 if (csW->lpszName)
3002 name = cls + class_lenA + 1;
3003 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3004 name[name_lenA] = 0;
3005 csA.lpszName = name;
3008 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3010 MDICREATESTRUCTA mdi_cs;
3012 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3013 mdi_cs.szTitle = csA.lpszName;
3014 mdi_cs.szClass = csA.lpszClass;
3015 csA.lpCreateParams = &mdi_cs;
3018 lParam = (LPARAM)&csA;
3019 *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);
3021 if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
3023 return TRUE;
3025 default:
3026 return FALSE;
3030 /**********************************************************************
3031 * WINPROC_CallProc32WTo32A
3033 * Call a window procedure, translating args from Unicode to Ansi.
3035 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
3036 UINT msg, WPARAM wParam,
3037 LPARAM lParam )
3039 LRESULT result;
3040 int unmap;
3042 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3043 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3045 if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
3046 return result;
3048 if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3049 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3050 SPY_GetMsgName(msg, hwnd), wParam, lParam );
3051 return 0;
3053 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3054 if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3055 return result;
3059 /**********************************************************************
3060 * __wine_call_wndproc_32A (USER.1010)
3062 LRESULT WINAPI __wine_call_wndproc_32A( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3063 WNDPROC func )
3065 LRESULT result;
3066 UINT msg32;
3067 WPARAM wParam32;
3068 HWND hwnd32 = WIN_Handle32( hwnd );
3070 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3071 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3073 if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3074 return 0;
3075 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3076 return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3080 /**********************************************************************
3081 * __wine_call_wndproc_32W (USER.1011)
3083 LRESULT WINAPI __wine_call_wndproc_32W( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3084 WNDPROC func )
3086 LRESULT result;
3087 UINT msg32;
3088 WPARAM wParam32;
3089 HWND hwnd32 = WIN_Handle32( hwnd );
3091 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3092 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3094 if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3095 return 0;
3096 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3097 return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result );
3101 /**********************************************************************
3102 * WINPROC_CallProc32ATo16
3104 * Call a 16-bit window procedure, translating the 32-bit args.
3106 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
3107 UINT msg, WPARAM wParam,
3108 LPARAM lParam )
3110 UINT16 msg16;
3111 MSGPARAM16 mp16;
3113 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3114 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3116 mp16.lParam = lParam;
3117 if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3118 return 0;
3119 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3120 mp16.wParam, mp16.lParam );
3121 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3122 return mp16.lResult;
3126 /**********************************************************************
3127 * WINPROC_CallProc32WTo16
3129 * Call a 16-bit window procedure, translating the 32-bit args.
3131 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
3132 UINT msg, WPARAM wParam,
3133 LPARAM lParam )
3135 UINT16 msg16;
3136 MSGPARAM16 mp16;
3138 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3139 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3141 mp16.lParam = lParam;
3142 if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
3143 &mp16.lParam ) == -1)
3144 return 0;
3145 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3146 mp16.wParam, mp16.lParam );
3147 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3148 return mp16.lResult;
3152 /**********************************************************************
3153 * CallWindowProc (USER.122)
3155 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3156 WPARAM16 wParam, LPARAM lParam )
3158 WINDOWPROC *proc;
3160 if (!func) return 0;
3162 if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3163 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3165 #if testing
3166 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_16 );
3167 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3168 #endif
3170 switch(proc->type)
3172 case WIN_PROC_16:
3173 if (!proc->thunk.t_from32.proc) return 0;
3174 return WINPROC_CallWndProc16( proc->thunk.t_from32.proc,
3175 hwnd, msg, wParam, lParam );
3176 case WIN_PROC_32A:
3177 if (!proc->thunk.t_from16.proc) return 0;
3178 return __wine_call_wndproc_32A( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3179 case WIN_PROC_32W:
3180 if (!proc->thunk.t_from16.proc) return 0;
3181 return __wine_call_wndproc_32W( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3182 default:
3183 WARN_(relay)("Invalid proc %p\n", proc );
3184 return 0;
3189 /**********************************************************************
3190 * CallWindowProcA (USER32.@)
3192 * The CallWindowProc() function invokes the windows procedure _func_,
3193 * with _hwnd_ as the target window, the message specified by _msg_, and
3194 * the message parameters _wParam_ and _lParam_.
3196 * Some kinds of argument conversion may be done, I'm not sure what.
3198 * CallWindowProc() may be used for windows subclassing. Use
3199 * SetWindowLong() to set a new windows procedure for windows of the
3200 * subclass, and handle subclassed messages in the new windows
3201 * procedure. The new windows procedure may then use CallWindowProc()
3202 * with _func_ set to the parent class's windows procedure to dispatch
3203 * the message to the superclass.
3205 * RETURNS
3207 * The return value is message dependent.
3209 * CONFORMANCE
3211 * ECMA-234, Win32
3213 LRESULT WINAPI CallWindowProcA(
3214 WNDPROC func, /* [in] window procedure */
3215 HWND hwnd, /* [in] target window */
3216 UINT msg, /* [in] message */
3217 WPARAM wParam, /* [in] message dependent parameter */
3218 LPARAM lParam /* [in] message dependent parameter */
3220 WINDOWPROC *proc;
3222 if (!func) return 0;
3224 if (!(proc = WINPROC_GetPtr( func )))
3225 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3227 #if testing
3228 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32A );
3229 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3230 #endif
3232 switch(proc->type)
3234 case WIN_PROC_16:
3235 if (!proc->thunk.t_from32.proc) return 0;
3236 return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
3237 hwnd, msg, wParam, lParam );
3238 case WIN_PROC_32A:
3239 if (!proc->thunk.t_from16.proc) return 0;
3240 return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3241 hwnd, msg, wParam, lParam );
3242 case WIN_PROC_32W:
3243 if (!proc->thunk.t_from16.proc) return 0;
3244 return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
3245 hwnd, msg, wParam, lParam );
3246 default:
3247 WARN_(relay)("Invalid proc %p\n", proc );
3248 return 0;
3253 /**********************************************************************
3254 * CallWindowProcW (USER32.@)
3256 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3257 WPARAM wParam, LPARAM lParam )
3259 WINDOWPROC *proc;
3261 if (!func) return 0;
3263 if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3264 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3266 #if testing
3267 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32W );
3268 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3269 #endif
3271 switch(proc->type)
3273 case WIN_PROC_16:
3274 if (!proc->thunk.t_from32.proc) return 0;
3275 return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
3276 hwnd, msg, wParam, lParam );
3277 case WIN_PROC_32A:
3278 if (!proc->thunk.t_from16.proc) return 0;
3279 return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
3280 hwnd, msg, wParam, lParam );
3281 case WIN_PROC_32W:
3282 if (!proc->thunk.t_from16.proc) return 0;
3283 return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3284 hwnd, msg, wParam, lParam );
3285 default:
3286 WARN_(relay)("Invalid proc %p\n", proc );
3287 return 0;