Release 20040914.
[wine/multimedia.git] / windows / winproc.c
blob9fbb4cc905641ea058092a473151d2ca05b27e34
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 "heap.h"
37 #include "win.h"
38 #include "winproc.h"
39 #include "message.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 #include "pshpack1.h"
52 /* Window procedure 16-to-32-bit thunk */
53 typedef struct
55 BYTE popl_eax; /* popl %eax (return address) */
56 BYTE pushl_func; /* pushl $proc */
57 WNDPROC proc;
58 BYTE pushl_eax; /* pushl %eax */
59 BYTE ljmp; /* ljmp relay*/
60 DWORD relay_offset; /* __wine_call_wndproc_32A/W */
61 WORD relay_sel;
62 } WINPROC_THUNK_FROM16;
64 /* Window procedure 32-to-16-bit thunk */
65 typedef struct
67 BYTE popl_eax; /* popl %eax (return address) */
68 BYTE pushl_func; /* pushl $proc */
69 WNDPROC16 proc;
70 BYTE pushl_eax; /* pushl %eax */
71 BYTE jmp; /* jmp relay (relative jump)*/
72 void (*relay)(); /* WINPROC_CallProc32ATo16() */
73 } WINPROC_THUNK_FROM32;
75 /* Simple jmp to call 32-bit procedure directly */
76 typedef struct
78 BYTE jmp; /* jmp proc (relative jump) */
79 WNDPROC proc;
80 } WINPROC_JUMP;
81 #include "poppack.h"
83 typedef union
85 WINPROC_THUNK_FROM16 t_from16;
86 WINPROC_THUNK_FROM32 t_from32;
87 } WINPROC_THUNK;
89 typedef struct tagWINDOWPROC
91 WINPROC_THUNK thunk; /* Thunk */
92 WINPROC_JUMP jmp; /* Jump */
93 struct tagWINDOWPROC *next; /* Next window proc */
94 UINT magic; /* Magic number */
95 WINDOWPROCTYPE type; /* Function type */
96 WINDOWPROCUSER user; /* Function user */
97 } WINDOWPROC;
99 #define WINPROC_MAGIC ('W' | ('P' << 8) | ('R' << 16) | ('C' << 24))
101 #define WINPROC_THUNKPROC(pproc) \
102 (((pproc)->type == WIN_PROC_16) ? \
103 (WNDPROC16)((pproc)->thunk.t_from32.proc) : \
104 (WNDPROC16)((pproc)->thunk.t_from16.proc))
106 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
107 UINT msg, WPARAM wParam,
108 LPARAM lParam );
109 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
110 UINT msg, WPARAM wParam,
111 LPARAM lParam );
113 #define MAX_WINPROCS (0x10000 / sizeof(WINDOWPROC))
115 static WINDOWPROC winproc_array[MAX_WINPROCS];
116 static WINDOWPROC *winproc_first_free;
117 static UINT winproc_used;
119 static CRITICAL_SECTION winproc_cs;
120 static CRITICAL_SECTION_DEBUG critsect_debug =
122 0, 0, &winproc_cs,
123 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124 0, 0, { 0, (DWORD)(__FILE__ ": winproc_cs") }
126 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
128 /* allocate a window procedure from the global array */
129 static WINDOWPROC *alloc_winproc(void)
131 WINDOWPROC *ret = NULL;
133 EnterCriticalSection( &winproc_cs );
134 if ((ret = winproc_first_free))
135 winproc_first_free = ret->next;
136 else if (winproc_used < MAX_WINPROCS)
137 ret = &winproc_array[winproc_used++];
138 LeaveCriticalSection( &winproc_cs );
139 return ret;
142 static void free_winproc( WINDOWPROC *proc )
144 EnterCriticalSection( &winproc_cs );
145 proc->magic = 0;
146 proc->next = winproc_first_free;
147 winproc_first_free = proc;
148 LeaveCriticalSection( &winproc_cs );
151 static BOOL is_valid_winproc( WINDOWPROC *proc )
153 if (proc < winproc_array || proc >= winproc_array + MAX_WINPROCS) return FALSE;
154 if (proc != winproc_array + (proc - winproc_array)) return FALSE;
155 return (proc->magic == WINPROC_MAGIC);
158 static WORD get_winproc_selector(void)
160 static LONG winproc_selector;
161 WORD ret;
163 if (!(ret = winproc_selector))
165 LDT_ENTRY entry;
166 WORD sel = wine_ldt_alloc_entries(1);
167 wine_ldt_set_base( &entry, winproc_array );
168 wine_ldt_set_limit( &entry, sizeof(winproc_array) - 1 );
169 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
170 wine_ldt_set_entry( sel, &entry );
171 if (!(ret = InterlockedCompareExchange( &winproc_selector, sel, 0 ))) ret = sel;
172 else wine_ldt_free_entries( sel, 1 ); /* somebody beat us to it */
174 return ret;
178 #ifdef __i386__
179 /* Some window procedures modify register they shouldn't, or are not
180 * properly declared stdcall; so we need a small assembly wrapper to
181 * call them. */
182 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
183 WPARAM wParam, LPARAM lParam );
184 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
185 "pushl %ebp\n\t"
186 "movl %esp,%ebp\n\t"
187 "pushl %edi\n\t"
188 "pushl %esi\n\t"
189 "pushl %ebx\n\t"
190 "pushl 24(%ebp)\n\t"
191 "pushl 20(%ebp)\n\t"
192 "pushl 16(%ebp)\n\t"
193 "pushl 12(%ebp)\n\t"
194 "movl 8(%ebp),%eax\n\t"
195 "call *%eax\n\t"
196 "leal -12(%ebp),%esp\n\t"
197 "popl %ebx\n\t"
198 "popl %esi\n\t"
199 "popl %edi\n\t"
200 "leave\n\t"
201 "ret" );
202 #else
203 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
204 WPARAM wParam, LPARAM lParam )
206 return proc( hwnd, msg, wParam, lParam );
208 #endif /* __i386__ */
211 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
213 to->ptReserved.x = from->ptReserved.x;
214 to->ptReserved.y = from->ptReserved.y;
215 to->ptMaxSize.x = from->ptMaxSize.x;
216 to->ptMaxSize.y = from->ptMaxSize.y;
217 to->ptMaxPosition.x = from->ptMaxPosition.x;
218 to->ptMaxPosition.y = from->ptMaxPosition.y;
219 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
220 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
221 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
222 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
225 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
227 to->ptReserved.x = from->ptReserved.x;
228 to->ptReserved.y = from->ptReserved.y;
229 to->ptMaxSize.x = from->ptMaxSize.x;
230 to->ptMaxSize.y = from->ptMaxSize.y;
231 to->ptMaxPosition.x = from->ptMaxPosition.x;
232 to->ptMaxPosition.y = from->ptMaxPosition.y;
233 to->ptMinTrackSize.x = from->ptMinTrackSize.x;
234 to->ptMinTrackSize.y = from->ptMinTrackSize.y;
235 to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
236 to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
239 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
241 to->hwnd = HWND_16(from->hwnd);
242 to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
243 to->x = from->x;
244 to->y = from->y;
245 to->cx = from->cx;
246 to->cy = from->cy;
247 to->flags = from->flags;
250 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
252 to->hwnd = WIN_Handle32(from->hwnd);
253 to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
254 HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
255 to->x = from->x;
256 to->y = from->y;
257 to->cx = from->cx;
258 to->cy = from->cy;
259 to->flags = from->flags;
262 /* The strings are not copied */
263 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
265 to->lpCreateParams = (SEGPTR)from->lpCreateParams;
266 to->hInstance = HINSTANCE_16(from->hInstance);
267 to->hMenu = HMENU_16(from->hMenu);
268 to->hwndParent = HWND_16(from->hwndParent);
269 to->cy = from->cy;
270 to->cx = from->cx;
271 to->y = from->y;
272 to->x = from->x;
273 to->style = from->style;
274 to->dwExStyle = from->dwExStyle;
277 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
280 to->lpCreateParams = (LPVOID)from->lpCreateParams;
281 to->hInstance = HINSTANCE_32(from->hInstance);
282 to->hMenu = HMENU_32(from->hMenu);
283 to->hwndParent = WIN_Handle32(from->hwndParent);
284 to->cy = from->cy;
285 to->cx = from->cx;
286 to->y = from->y;
287 to->x = from->x;
288 to->style = from->style;
289 to->dwExStyle = from->dwExStyle;
292 /* The strings are not copied */
293 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
295 to->hOwner = HINSTANCE_16(from->hOwner);
296 to->x = from->x;
297 to->y = from->y;
298 to->cx = from->cx;
299 to->cy = from->cy;
300 to->style = from->style;
301 to->lParam = from->lParam;
304 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
306 to->hOwner = HINSTANCE_32(from->hOwner);
307 to->x = from->x;
308 to->y = from->y;
309 to->cx = from->cx;
310 to->cy = from->cy;
311 to->style = from->style;
312 to->lParam = from->lParam;
315 /**********************************************************************
316 * WINPROC_CallWndProc32
318 * Call a 32-bit WndProc.
320 static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
321 WPARAM wParam, LPARAM lParam )
323 LRESULT retvalue;
324 int iWndsLocks;
326 hwnd = WIN_GetFullHandle( hwnd );
327 if (TRACE_ON(relay))
328 DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
329 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
330 /* To avoid any deadlocks, all the locks on the windows structures
331 must be suspended before the control is passed to the application */
332 iWndsLocks = WIN_SuspendWndsLock();
333 retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
334 WIN_RestoreWndsLock(iWndsLocks);
336 if (TRACE_ON(relay))
337 DPRINTF( "%04lx:Ret window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
338 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
339 return retvalue;
342 /***********************************************************************
343 * WINPROC_CallWndProc16
345 * Call a 16-bit window procedure
347 static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
348 UINT16 msg, WPARAM16 wParam,
349 LPARAM lParam )
351 CONTEXT86 context;
352 LRESULT ret;
353 WORD args[5];
354 DWORD offset = 0;
355 TEB *teb = NtCurrentTeb();
356 int iWndsLocks;
358 /* Window procedures want ax = hInstance, ds = es = ss */
360 memset(&context, 0, sizeof(context));
361 context.SegDs = context.SegEs = SELECTOROF(teb->cur_stack);
362 context.SegFs = wine_get_fs();
363 context.SegGs = wine_get_gs();
364 if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWL_HINSTANCE ))) context.Eax = context.SegDs;
365 context.SegCs = SELECTOROF(proc);
366 context.Eip = OFFSETOF(proc);
367 context.Ebp = OFFSETOF(teb->cur_stack)
368 + (WORD)&((STACK16FRAME*)0)->bp;
370 if (lParam)
372 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
373 work if structures passed in lParam are placed in the stack/data
374 segment. Programmers easily make the mistake of converting lParam
375 to a near rather than a far pointer, since Windows apparently
376 allows this. We copy the structures to the 16 bit stack; this is
377 ugly but makes these programs work. */
378 switch (msg)
380 case WM_CREATE:
381 case WM_NCCREATE:
382 offset = sizeof(CREATESTRUCT16); break;
383 case WM_DRAWITEM:
384 offset = sizeof(DRAWITEMSTRUCT16); break;
385 case WM_COMPAREITEM:
386 offset = sizeof(COMPAREITEMSTRUCT16); break;
388 if (offset)
390 void *s = MapSL(lParam);
391 lParam = stack16_push( offset );
392 memcpy( MapSL(lParam), s, offset );
396 iWndsLocks = WIN_SuspendWndsLock();
398 args[4] = hwnd;
399 args[3] = msg;
400 args[2] = wParam;
401 args[1] = HIWORD(lParam);
402 args[0] = LOWORD(lParam);
403 WOWCallback16Ex( 0, WCB16_REGS, sizeof(args), args, (DWORD *)&context );
404 ret = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
406 if (offset) stack16_pop( offset );
408 WIN_RestoreWndsLock(iWndsLocks);
410 return ret;
414 /**********************************************************************
415 * WINPROC_GetPtr
417 * Return a pointer to the win proc.
419 static WINDOWPROC *WINPROC_GetPtr( WNDPROC handle )
421 BYTE *ptr;
422 WINDOWPROC *proc;
424 /* ptr cannot be < 64K */
425 if (!HIWORD(handle)) return NULL;
427 /* Check for a linear pointer */
429 ptr = (BYTE *)handle;
430 /* First check if it is the jmp address */
431 proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->jmp);
432 if (is_valid_winproc(proc)) return proc;
434 /* Now it must be the thunk address */
435 proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->thunk);
436 if (is_valid_winproc(proc)) return proc;
438 /* Check for a segmented pointer */
440 if (!IsBadReadPtr16( (SEGPTR)handle, sizeof(proc->thunk) ))
442 ptr = MapSL( (SEGPTR)handle );
443 /* It must be the thunk address */
444 proc = (WINDOWPROC *)(ptr - (int)&((WINDOWPROC *)0)->thunk);
445 if (is_valid_winproc(proc)) return proc;
448 return NULL;
452 /**********************************************************************
453 * WINPROC_AllocWinProc
455 * Allocate a new window procedure.
457 static WINDOWPROC *WINPROC_AllocWinProc( WNDPROC func, WINDOWPROCTYPE type,
458 WINDOWPROCUSER user )
460 static FARPROC16 relay_32A, relay_32W;
462 WINDOWPROC *proc, *oldproc;
464 /* Allocate a window procedure */
466 if (!(proc = alloc_winproc())) return 0;
468 /* Check if the function is already a win proc */
470 if ((oldproc = WINPROC_GetPtr( func )))
472 *proc = *oldproc;
474 else
476 switch(type)
478 case WIN_PROC_16:
479 proc->thunk.t_from32.popl_eax = 0x58; /* popl %eax */
480 proc->thunk.t_from32.pushl_func = 0x68; /* pushl $proc */
481 proc->thunk.t_from32.proc = (WNDPROC16)func;
482 proc->thunk.t_from32.pushl_eax = 0x50; /* pushl %eax */
483 proc->thunk.t_from32.jmp = 0xe9; /* jmp relay*/
484 proc->thunk.t_from32.relay = /* relative jump */
485 (void(*)())((DWORD)WINPROC_CallProc32ATo16 -
486 (DWORD)(&proc->thunk.t_from32.relay + 1));
487 break;
488 case WIN_PROC_32A:
489 if (!relay_32A) relay_32A = GetProcAddress16( GetModuleHandle16("user"),
490 "__wine_call_wndproc_32A" );
491 proc->thunk.t_from16.popl_eax = 0x58; /* popl %eax */
492 proc->thunk.t_from16.pushl_func = 0x68; /* pushl $proc */
493 proc->thunk.t_from16.proc = func;
494 proc->thunk.t_from16.pushl_eax = 0x50; /* pushl %eax */
495 proc->thunk.t_from16.ljmp = 0xea; /* ljmp relay*/
496 proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32A);
497 proc->thunk.t_from16.relay_sel = SELECTOROF(relay_32A);
498 proc->jmp.jmp = 0xe9;
499 /* Fixup relative jump */
500 proc->jmp.proc = (WNDPROC)((DWORD)func - (DWORD)(&proc->jmp.proc + 1));
501 break;
502 case WIN_PROC_32W:
503 if (!relay_32W) relay_32W = GetProcAddress16( GetModuleHandle16("user"),
504 "__wine_call_wndproc_32W" );
505 proc->thunk.t_from16.popl_eax = 0x58; /* popl %eax */
506 proc->thunk.t_from16.pushl_func = 0x68; /* pushl $proc */
507 proc->thunk.t_from16.proc = func;
508 proc->thunk.t_from16.pushl_eax = 0x50; /* pushl %eax */
509 proc->thunk.t_from16.ljmp = 0xea; /* ljmp relay*/
510 proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32W);
511 proc->thunk.t_from16.relay_sel = SELECTOROF(relay_32W);
512 proc->jmp.jmp = 0xe9;
513 /* Fixup relative jump */
514 proc->jmp.proc = (WNDPROC)((char *)func - (char *)(&proc->jmp.proc + 1));
515 break;
516 default:
517 /* Should not happen */
518 break;
520 proc->magic = WINPROC_MAGIC;
521 proc->type = type;
522 proc->user = user;
524 proc->next = NULL;
525 TRACE("(%p,%d): returning %p\n", func, type, proc );
526 return proc;
530 /**********************************************************************
531 * WINPROC_GetProc
533 * Get a window procedure pointer that can be passed to the Windows program.
535 WNDPROC16 WINPROC_GetProc( WNDPROC proc, WINDOWPROCTYPE type )
537 WINDOWPROC *ptr = (WINDOWPROC *)proc;
539 if (!proc) return NULL;
540 if (type == WIN_PROC_16) /* We want a 16:16 address */
542 if (ptr->type == WIN_PROC_16)
543 return ptr->thunk.t_from32.proc;
544 else
545 return (WNDPROC16)MAKESEGPTR( get_winproc_selector(),
546 (char *)&ptr->thunk - (char *)winproc_array );
548 else /* We want a 32-bit address */
550 if (ptr->type == WIN_PROC_16)
551 return (WNDPROC16)&ptr->thunk;
552 else if (type != ptr->type)
553 /* Have to return the jmp address if types don't match */
554 return (WNDPROC16)&ptr->jmp;
555 else
556 /* Some Win16 programs want to get back the proc they set */
557 return (WNDPROC16)ptr->thunk.t_from16.proc;
562 /**********************************************************************
563 * WINPROC_SetProc
565 * Set the window procedure for a window or class. There are
566 * three tree classes of winproc callbacks:
568 * 1) class -> wp - not subclassed
569 * class -> wp -> wp -> wp -> wp - SetClassLong()
570 * / /
571 * 2) window -' / - not subclassed
572 * window -> wp -> wp ' - SetWindowLong()
574 * 3) timer -> wp - SetTimer()
576 * Initially, winproc of the window points to the current winproc
577 * thunk of its class. Subclassing prepends a new thunk to the
578 * window winproc chain at the head of the list. Thus, window thunk
579 * list includes class thunks and the latter are preserved when the
580 * window is destroyed.
583 BOOL WINPROC_SetProc( WNDPROC *pFirst, WNDPROC func,
584 WINDOWPROCTYPE type, WINDOWPROCUSER user )
586 BOOL bRecycle = FALSE;
587 WINDOWPROC *proc, **ppPrev;
589 /* Check if function is already in the list */
591 ppPrev = (WINDOWPROC **)pFirst;
592 proc = WINPROC_GetPtr( func );
593 while (*ppPrev)
595 if (proc)
597 if (*ppPrev == proc)
599 if ((*ppPrev)->user != user)
601 /* terminal thunk is being restored */
603 WINPROC_FreeProc( *pFirst, (*ppPrev)->user );
604 *(WINDOWPROC **)pFirst = *ppPrev;
605 return TRUE;
607 bRecycle = TRUE;
608 break;
611 else
613 if (((*ppPrev)->type == type) &&
614 (func == (WNDPROC)WINPROC_THUNKPROC(*ppPrev)))
616 if((*ppPrev)->user == user)
618 bRecycle = TRUE;
620 else
622 WINPROC_FreeProc( (WNDPROC)*ppPrev, user );
623 *ppPrev = NULL;
625 break;
629 /* WPF_CLASS thunk terminates window thunk list */
630 if ((*ppPrev)->user != user) break;
631 ppPrev = &(*ppPrev)->next;
634 if (bRecycle)
636 /* Extract this thunk from the list */
637 proc = *ppPrev;
638 *ppPrev = proc->next;
640 else /* Allocate a new one */
642 if (proc) /* Was already a win proc */
644 type = proc->type;
645 func = (WNDPROC)WINPROC_THUNKPROC(proc);
647 proc = WINPROC_AllocWinProc( func, type, user );
648 if (!proc) return FALSE;
651 /* Add the win proc at the head of the list */
653 TRACE("(%p,%p,%d): res=%p\n", *pFirst, func, type, proc );
654 proc->next = *(WINDOWPROC **)pFirst;
655 *(WINDOWPROC **)pFirst = proc;
656 return TRUE;
660 /**********************************************************************
661 * WINPROC_FreeProc
663 * Free a list of win procs.
665 void WINPROC_FreeProc( WNDPROC proc, WINDOWPROCUSER user )
667 WINDOWPROC *ptr = (WINDOWPROC *)proc;
668 while (ptr)
670 WINDOWPROC *next = ptr->next;
671 if (ptr->user != user) break;
672 TRACE("freeing %p (%d)\n", ptr, user);
673 free_winproc( ptr );
674 ptr = next;
679 /**********************************************************************
680 * WINPROC_GetProcType
682 * Return the window procedure type.
684 WINDOWPROCTYPE WINPROC_GetProcType( WNDPROC proc )
686 if (!proc ||
687 (((WINDOWPROC *)proc)->magic != WINPROC_MAGIC))
688 return WIN_PROC_INVALID;
689 return ((WINDOWPROC *)proc)->type;
691 /**********************************************************************
692 * WINPROC_TestCBForStr
694 * Return TRUE if the lparam is a string
696 inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
698 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
699 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
701 /**********************************************************************
702 * WINPROC_TestLBForStr
704 * Return TRUE if the lparam is a string
706 inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
708 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
709 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
712 /**********************************************************************
713 * WINPROC_MapMsg32ATo32W
715 * Map a message from Ansi to Unicode.
716 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
718 * FIXME:
719 * WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
720 * the first four bytes are the handle of the icon
721 * when the WM_SETTEXT message has been used to set the icon
723 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
725 switch(msg)
727 case WM_GETTEXT:
728 case WM_ASKCBFORMATNAME:
730 LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
731 *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
732 if (!ptr) return -1;
733 *ptr++ = *plparam; /* Store previous lParam */
734 *plparam = (LPARAM)ptr;
736 return 1;
737 /* lparam is string (0-terminated) */
738 case WM_SETTEXT:
739 case WM_WININICHANGE:
740 case WM_DEVMODECHANGE:
741 case CB_DIR:
742 case LB_DIR:
743 case LB_ADDFILE:
744 case EM_REPLACESEL:
746 DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
747 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
748 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
749 *plparam = (LPARAM)buf;
750 return (*plparam ? 1 : -1);
752 case WM_GETTEXTLENGTH:
753 case CB_GETLBTEXTLEN:
754 case LB_GETTEXTLEN:
755 return 1; /* need to map result */
756 case WM_NCCREATE:
757 case WM_CREATE:
759 UNICODE_STRING usBuffer;
760 struct s
761 { CREATESTRUCTW cs; /* new structure */
762 LPCWSTR lpszName; /* allocated Name */
763 LPCWSTR lpszClass; /* allocated Class */
766 struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
767 if (!xs) return -1;
768 xs->cs = *(CREATESTRUCTW *)*plparam;
769 if (HIWORD(xs->cs.lpszName))
771 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
772 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
774 if (HIWORD(xs->cs.lpszClass))
776 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
777 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
780 if (GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
782 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
783 sizeof(*mdi_cs));
784 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
785 if (HIWORD(mdi_cs->szTitle))
787 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
788 mdi_cs->szTitle = usBuffer.Buffer;
790 if (HIWORD(mdi_cs->szClass))
792 RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
793 mdi_cs->szClass = usBuffer.Buffer;
795 xs->cs.lpCreateParams = mdi_cs;
798 *plparam = (LPARAM)xs;
800 return 1;
801 case WM_MDICREATE:
803 MDICREATESTRUCTW *cs =
804 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
805 if (!cs) return -1;
806 *cs = *(MDICREATESTRUCTW *)*plparam;
807 if (HIWORD(cs->szClass))
809 UNICODE_STRING usBuffer;
810 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
811 cs->szClass = usBuffer.Buffer;
813 if (HIWORD(cs->szTitle))
815 UNICODE_STRING usBuffer;
816 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
817 cs->szTitle = usBuffer.Buffer;
819 *plparam = (LPARAM)cs;
821 return 1;
823 /* Listbox */
824 case LB_ADDSTRING:
825 case LB_INSERTSTRING:
826 case LB_FINDSTRING:
827 case LB_FINDSTRINGEXACT:
828 case LB_SELECTSTRING:
829 if(!*plparam) return 0;
830 if ( WINPROC_TestLBForStr( hwnd ))
832 UNICODE_STRING usBuffer;
833 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
834 *plparam = (LPARAM)usBuffer.Buffer;
836 return (*plparam ? 1 : -1);
838 case LB_GETTEXT: /* FIXME: fixed sized buffer */
839 { if ( WINPROC_TestLBForStr( hwnd ))
840 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
841 if (!ptr) return -1;
842 *ptr++ = *plparam; /* Store previous lParam */
843 *plparam = (LPARAM)ptr;
846 return 1;
848 /* Combobox */
849 case CB_ADDSTRING:
850 case CB_INSERTSTRING:
851 case CB_FINDSTRINGEXACT:
852 case CB_FINDSTRING:
853 case CB_SELECTSTRING:
854 if(!*plparam) return 0;
855 if ( WINPROC_TestCBForStr( hwnd ))
857 UNICODE_STRING usBuffer;
858 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
859 *plparam = (LPARAM)usBuffer.Buffer;
861 return (*plparam ? 1 : -1);
863 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
864 { if ( WINPROC_TestCBForStr( hwnd ))
865 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
866 if (!ptr) return -1;
867 *ptr++ = *plparam; /* Store previous lParam */
868 *plparam = (LPARAM)ptr;
871 return 1;
873 /* Multiline edit */
874 case EM_GETLINE:
875 { WORD len = (WORD)*plparam;
876 LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
877 if (!ptr) return -1;
878 *ptr++ = *plparam; /* Store previous lParam */
879 *((WORD *) ptr) = len; /* Store the length */
880 *plparam = (LPARAM)ptr;
882 return 1;
884 case WM_CHARTOITEM:
885 case WM_MENUCHAR:
886 case WM_CHAR:
887 case WM_DEADCHAR:
888 case WM_SYSCHAR:
889 case WM_SYSDEADCHAR:
890 case EM_SETPASSWORDCHAR:
892 BYTE ch = LOWORD(*pwparam);
893 WCHAR wch;
894 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
895 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
897 return 0;
899 case WM_IME_CHAR:
901 BYTE ch[2];
902 WCHAR wch;
903 ch[0] = (*pwparam >> 8);
904 ch[1] = *pwparam & 0xff;
905 if (ch[0])
906 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
907 else
908 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
909 *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
911 return 0;
913 case WM_PAINTCLIPBOARD:
914 case WM_SIZECLIPBOARD:
915 FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
916 return -1;
917 default: /* No translation needed */
918 return 0;
923 /**********************************************************************
924 * WINPROC_UnmapMsg32ATo32W
926 * Unmap a message that was mapped from Ansi to Unicode.
928 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
929 LRESULT result )
931 switch(msg)
933 case WM_GETTEXT:
934 case WM_ASKCBFORMATNAME:
936 LPARAM *ptr = (LPARAM *)lParam - 1;
937 if (!wParam) result = 0;
938 else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
939 (LPSTR)*ptr, wParam, NULL, NULL )))
941 ((LPSTR)*ptr)[wParam-1] = 0;
942 result = wParam - 1;
944 else result--; /* do not count terminating null */
945 HeapFree( GetProcessHeap(), 0, ptr );
947 break;
948 case WM_GETTEXTLENGTH:
949 case CB_GETLBTEXTLEN:
950 case LB_GETTEXTLEN:
951 /* there may be one DBCS char for each Unicode char */
952 return result * 2;
953 case WM_NCCREATE:
954 case WM_CREATE:
956 struct s
957 { CREATESTRUCTW cs; /* new structure */
958 LPWSTR lpszName; /* allocated Name */
959 LPWSTR lpszClass; /* allocated Class */
961 struct s *xs = (struct s *)lParam;
962 if (xs->lpszName) HeapFree( GetProcessHeap(), 0, xs->lpszName );
963 if (xs->lpszClass) HeapFree( GetProcessHeap(), 0, xs->lpszClass );
965 if (GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
967 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
968 if (HIWORD(mdi_cs->szTitle))
969 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
970 if (HIWORD(mdi_cs->szClass))
971 HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
972 HeapFree(GetProcessHeap(), 0, mdi_cs);
974 HeapFree( GetProcessHeap(), 0, xs );
976 break;
978 case WM_MDICREATE:
980 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
981 if (HIWORD(cs->szTitle))
982 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
983 if (HIWORD(cs->szClass))
984 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
985 HeapFree( GetProcessHeap(), 0, cs );
987 break;
989 case WM_SETTEXT:
990 case WM_WININICHANGE:
991 case WM_DEVMODECHANGE:
992 case CB_DIR:
993 case LB_DIR:
994 case LB_ADDFILE:
995 case EM_REPLACESEL:
996 HeapFree( GetProcessHeap(), 0, (void *)lParam );
997 break;
999 /* Listbox */
1000 case LB_ADDSTRING:
1001 case LB_INSERTSTRING:
1002 case LB_FINDSTRING:
1003 case LB_FINDSTRINGEXACT:
1004 case LB_SELECTSTRING:
1005 if ( WINPROC_TestLBForStr( hwnd ))
1006 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1007 break;
1009 case LB_GETTEXT:
1010 if ( WINPROC_TestLBForStr( hwnd ))
1012 LPARAM *ptr = (LPARAM *)lParam - 1;
1013 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
1014 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
1015 HeapFree( GetProcessHeap(), 0, ptr );
1017 break;
1019 /* Combobox */
1020 case CB_ADDSTRING:
1021 case CB_INSERTSTRING:
1022 case CB_FINDSTRING:
1023 case CB_FINDSTRINGEXACT:
1024 case CB_SELECTSTRING:
1025 if ( WINPROC_TestCBForStr( hwnd ))
1026 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1027 break;
1029 case CB_GETLBTEXT:
1030 if ( WINPROC_TestCBForStr( hwnd ))
1032 LPARAM *ptr = (LPARAM *)lParam - 1;
1033 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
1034 (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
1035 HeapFree( GetProcessHeap(), 0, ptr );
1037 break;
1039 /* Multiline edit */
1040 case EM_GETLINE:
1042 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lParam */
1043 WORD len = *(WORD *) lParam;
1044 result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
1045 (LPSTR)*ptr, len, NULL, NULL );
1046 if (result < len) ((LPSTR)*ptr)[result] = 0;
1047 HeapFree( GetProcessHeap(), 0, ptr );
1049 break;
1051 return result;
1055 /**********************************************************************
1056 * WINPROC_MapMsg32WTo32A
1058 * Map a message from Unicode to Ansi.
1059 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1061 static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
1063 switch(msg)
1065 case WM_GETTEXT:
1066 case WM_ASKCBFORMATNAME:
1068 LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
1069 *pwparam + sizeof(LPARAM) );
1070 if (!ptr) return -1;
1071 *ptr++ = *plparam; /* Store previous lParam */
1072 *plparam = (LPARAM)ptr;
1074 return 1;
1076 case WM_SETTEXT:
1077 case WM_WININICHANGE:
1078 case WM_DEVMODECHANGE:
1079 case CB_DIR:
1080 case LB_DIR:
1081 case LB_ADDFILE:
1082 case EM_REPLACESEL:
1083 if(!*plparam) return 0;
1084 *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1085 return (*plparam ? 1 : -1);
1087 case WM_MDICREATE:
1089 MDICREATESTRUCTA *cs =
1090 (MDICREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1091 if (!cs) return -1;
1092 *cs = *(MDICREATESTRUCTA *)*plparam;
1093 if (HIWORD(cs->szTitle))
1094 cs->szTitle = HEAP_strdupWtoA( GetProcessHeap(), 0,
1095 (LPCWSTR)cs->szTitle );
1096 if (HIWORD(cs->szClass))
1097 cs->szClass = HEAP_strdupWtoA( GetProcessHeap(), 0,
1098 (LPCWSTR)cs->szClass );
1099 *plparam = (LPARAM)cs;
1101 return 1;
1103 /* Listbox */
1104 case LB_ADDSTRING:
1105 case LB_INSERTSTRING:
1106 case LB_FINDSTRING:
1107 case LB_FINDSTRINGEXACT:
1108 case LB_SELECTSTRING:
1109 if(!*plparam) return 0;
1110 if ( WINPROC_TestLBForStr( hwnd ))
1111 *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1112 return (*plparam ? 1 : -1);
1114 case LB_GETTEXT: /* FIXME: fixed sized buffer */
1115 { if ( WINPROC_TestLBForStr( hwnd ))
1116 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1117 if (!ptr) return -1;
1118 *ptr++ = *plparam; /* Store previous lParam */
1119 *plparam = (LPARAM)ptr;
1122 return 1;
1124 /* Combobox */
1125 case CB_ADDSTRING:
1126 case CB_INSERTSTRING:
1127 case CB_FINDSTRING:
1128 case CB_FINDSTRINGEXACT:
1129 case CB_SELECTSTRING:
1130 if(!*plparam) return 0;
1131 if ( WINPROC_TestCBForStr( hwnd ))
1132 *plparam = (LPARAM)HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)*plparam );
1133 return (*plparam ? 1 : -1);
1135 case CB_GETLBTEXT: /* FIXME: fixed sized buffer */
1136 { if ( WINPROC_TestCBForStr( hwnd ))
1137 { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1138 if (!ptr) return -1;
1139 *ptr++ = *plparam; /* Store previous lParam */
1140 *plparam = (LPARAM)ptr;
1143 return 1;
1145 /* Multiline edit */
1146 case EM_GETLINE:
1147 { WORD len = (WORD)*plparam;
1148 LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1149 if (!ptr) return -1;
1150 *ptr++ = *plparam; /* Store previous lParam */
1151 *((WORD *) ptr) = len; /* Store the length */
1152 *plparam = (LPARAM)ptr;
1154 return 1;
1156 case WM_CHARTOITEM:
1157 case WM_MENUCHAR:
1158 case WM_CHAR:
1159 case WM_DEADCHAR:
1160 case WM_SYSCHAR:
1161 case WM_SYSDEADCHAR:
1162 case EM_SETPASSWORDCHAR:
1164 WCHAR wch = LOWORD(*pwparam);
1165 BYTE ch;
1166 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
1167 *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1169 return 0;
1171 case WM_IME_CHAR:
1173 WCHAR wch = LOWORD(*pwparam);
1174 BYTE ch[2];
1176 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
1177 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1178 else
1179 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1181 return 0;
1183 case WM_PAINTCLIPBOARD:
1184 case WM_SIZECLIPBOARD:
1185 FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1186 return -1;
1187 default: /* No translation needed */
1188 return 0;
1193 /**********************************************************************
1194 * WINPROC_UnmapMsg32WTo32A
1196 * Unmap a message that was mapped from Unicode to Ansi.
1198 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1200 switch(msg)
1202 case WM_GETTEXT:
1203 case WM_ASKCBFORMATNAME:
1205 LPARAM *ptr = (LPARAM *)lParam - 1;
1206 if (!wParam) result = 0;
1207 else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
1208 (LPWSTR)*ptr, wParam )))
1210 ((LPWSTR)*ptr)[wParam-1] = 0;
1211 result = wParam - 1;
1213 else result--; /* do not count terminating null */
1214 HeapFree( GetProcessHeap(), 0, ptr );
1216 break;
1218 case WM_SETTEXT:
1219 case WM_WININICHANGE:
1220 case WM_DEVMODECHANGE:
1221 case CB_DIR:
1222 case LB_DIR:
1223 case LB_ADDFILE:
1224 case EM_REPLACESEL:
1225 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1226 break;
1228 case WM_MDICREATE:
1230 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1231 if (HIWORD(cs->szTitle))
1232 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1233 if (HIWORD(cs->szClass))
1234 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1235 HeapFree( GetProcessHeap(), 0, cs );
1237 break;
1239 /* Listbox */
1240 case LB_ADDSTRING:
1241 case LB_INSERTSTRING:
1242 case LB_FINDSTRING:
1243 case LB_FINDSTRINGEXACT:
1244 case LB_SELECTSTRING:
1245 if ( WINPROC_TestLBForStr( hwnd ))
1246 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1247 break;
1249 case LB_GETTEXT:
1250 if ( WINPROC_TestLBForStr( hwnd ))
1252 LPARAM *ptr = (LPARAM *)lParam - 1;
1253 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1254 HeapFree( GetProcessHeap(), 0, ptr );
1256 break;
1258 /* Combobox */
1259 case CB_ADDSTRING:
1260 case CB_INSERTSTRING:
1261 case CB_FINDSTRING:
1262 case CB_FINDSTRINGEXACT:
1263 case CB_SELECTSTRING:
1264 if ( WINPROC_TestCBForStr( hwnd ))
1265 HeapFree( GetProcessHeap(), 0, (void *)lParam );
1266 break;
1268 case CB_GETLBTEXT:
1269 if ( WINPROC_TestCBForStr( hwnd ))
1271 LPARAM *ptr = (LPARAM *)lParam - 1;
1272 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1273 HeapFree( GetProcessHeap(), 0, ptr );
1275 break;
1277 /* Multiline edit */
1278 case EM_GETLINE:
1280 LPARAM * ptr = (LPARAM *)lParam - 1; /* get the old lparam */
1281 WORD len = *(WORD *)ptr;
1282 if (len)
1284 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1285 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1287 HeapFree( GetProcessHeap(), 0, ptr );
1289 break;
1291 return result;
1294 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1296 HANDLE dst;
1297 UINT sz = GlobalSize16(src);
1298 LPSTR ptr16, ptr32;
1300 if (!(dst = GlobalAlloc(flags, sz)))
1301 return 0;
1302 ptr16 = GlobalLock16(src);
1303 ptr32 = GlobalLock(dst);
1304 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1305 GlobalUnlock16(src);
1306 GlobalUnlock(dst);
1308 return (UINT)dst;
1311 /**********************************************************************
1312 * WINPROC_MapMsg16To32A
1314 * Map a message from 16- to 32-bit Ansi.
1315 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1317 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1318 WPARAM *pwparam32, LPARAM *plparam )
1320 *pmsg32 = (UINT)msg16;
1321 *pwparam32 = (WPARAM)wParam16;
1322 switch(msg16)
1324 case WM_ACTIVATE:
1325 case WM_CHARTOITEM:
1326 case WM_COMMAND:
1327 case WM_VKEYTOITEM:
1328 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1329 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1330 return 0;
1331 case WM_HSCROLL:
1332 case WM_VSCROLL:
1333 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1334 *plparam = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1335 return 0;
1336 case WM_CTLCOLOR:
1337 if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1338 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1339 *pwparam32 = (WPARAM)HDC_32(wParam16);
1340 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1341 return 0;
1342 case WM_COMPAREITEM:
1344 COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1345 COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)
1346 HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1347 if (!cis) return -1;
1348 cis->CtlType = cis16->CtlType;
1349 cis->CtlID = cis16->CtlID;
1350 cis->hwndItem = WIN_Handle32( cis16->hwndItem );
1351 cis->itemID1 = cis16->itemID1;
1352 cis->itemData1 = cis16->itemData1;
1353 cis->itemID2 = cis16->itemID2;
1354 cis->itemData2 = cis16->itemData2;
1355 cis->dwLocaleId = 0; /* FIXME */
1356 *plparam = (LPARAM)cis;
1358 return 1;
1359 case WM_DELETEITEM:
1361 DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1362 DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)
1363 HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1364 if (!dis) return -1;
1365 dis->CtlType = dis16->CtlType;
1366 dis->CtlID = dis16->CtlID;
1367 dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1368 dis->itemData = dis16->itemData;
1369 *plparam = (LPARAM)dis;
1371 return 1;
1372 case WM_MEASUREITEM:
1374 MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1375 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)
1376 HeapAlloc(GetProcessHeap(), 0,
1377 sizeof(*mis) + sizeof(LPARAM));
1378 if (!mis) return -1;
1379 mis->CtlType = mis16->CtlType;
1380 mis->CtlID = mis16->CtlID;
1381 mis->itemID = mis16->itemID;
1382 mis->itemWidth = mis16->itemWidth;
1383 mis->itemHeight = mis16->itemHeight;
1384 mis->itemData = mis16->itemData;
1385 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
1386 *plparam = (LPARAM)mis;
1388 return 1;
1389 case WM_DRAWITEM:
1391 DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1392 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)HeapAlloc(GetProcessHeap(), 0,
1393 sizeof(*dis));
1394 if (!dis) return -1;
1395 dis->CtlType = dis16->CtlType;
1396 dis->CtlID = dis16->CtlID;
1397 dis->itemID = dis16->itemID;
1398 dis->itemAction = dis16->itemAction;
1399 dis->itemState = dis16->itemState;
1400 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1401 : WIN_Handle32( dis16->hwndItem );
1402 dis->hDC = HDC_32(dis16->hDC);
1403 dis->itemData = dis16->itemData;
1404 dis->rcItem.left = dis16->rcItem.left;
1405 dis->rcItem.top = dis16->rcItem.top;
1406 dis->rcItem.right = dis16->rcItem.right;
1407 dis->rcItem.bottom = dis16->rcItem.bottom;
1408 *plparam = (LPARAM)dis;
1410 return 1;
1411 case WM_GETMINMAXINFO:
1413 MINMAXINFO *mmi = (MINMAXINFO *)HeapAlloc( GetProcessHeap(), 0,
1414 sizeof(*mmi) + sizeof(LPARAM));
1415 if (!mmi) return -1;
1416 MINMAXINFO16to32( MapSL(*plparam), mmi );
1417 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
1418 *plparam = (LPARAM)mmi;
1420 return 1;
1421 case WM_GETTEXT:
1422 case WM_SETTEXT:
1423 case WM_WININICHANGE:
1424 case WM_DEVMODECHANGE:
1425 case WM_ASKCBFORMATNAME:
1426 *plparam = (LPARAM)MapSL(*plparam);
1427 return 0;
1428 case WM_MDICREATE:
1430 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1431 MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1432 if (!cs) return -1;
1433 MDICREATESTRUCT16to32A( cs16, cs );
1434 cs->szTitle = MapSL(cs16->szTitle);
1435 cs->szClass = MapSL(cs16->szClass);
1436 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1437 *plparam = (LPARAM)cs;
1439 return 1;
1440 case WM_MDIGETACTIVE:
1441 *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1442 *(BOOL*)(*plparam) = 0;
1443 return 1;
1444 case WM_MDISETMENU:
1445 if(wParam16==TRUE)
1446 *pmsg32=WM_MDIREFRESHMENU;
1447 *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1448 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1449 return 0;
1450 case WM_MENUCHAR:
1451 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1452 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1453 return 0;
1454 case WM_MENUSELECT:
1455 if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1457 HMENU hmenu=HMENU_32(HIWORD(*plparam));
1458 UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1459 if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1460 *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1462 else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1463 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1464 return 0;
1465 case WM_MDIACTIVATE:
1466 if( *plparam )
1468 *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1469 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1471 else /* message sent to MDI client */
1472 *pwparam32 = wParam16;
1473 return 0;
1474 case WM_NCCALCSIZE:
1476 NCCALCSIZE_PARAMS16 *nc16;
1477 NCCALCSIZE_PARAMS *nc;
1479 nc = (NCCALCSIZE_PARAMS *)HeapAlloc( GetProcessHeap(), 0,
1480 sizeof(*nc) + sizeof(LPARAM) );
1481 if (!nc) return -1;
1482 nc16 = MapSL(*plparam);
1483 nc->rgrc[0].left = nc16->rgrc[0].left;
1484 nc->rgrc[0].top = nc16->rgrc[0].top;
1485 nc->rgrc[0].right = nc16->rgrc[0].right;
1486 nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1487 if (wParam16)
1489 nc->lppos = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1490 sizeof(*nc->lppos) );
1491 nc->rgrc[1].left = nc16->rgrc[1].left;
1492 nc->rgrc[1].top = nc16->rgrc[1].top;
1493 nc->rgrc[1].right = nc16->rgrc[1].right;
1494 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1495 nc->rgrc[2].left = nc16->rgrc[2].left;
1496 nc->rgrc[2].top = nc16->rgrc[2].top;
1497 nc->rgrc[2].right = nc16->rgrc[2].right;
1498 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1499 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1501 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1502 *plparam = (LPARAM)nc;
1504 return 1;
1505 case WM_NCCREATE:
1506 case WM_CREATE:
1508 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1509 CREATESTRUCTA *cs = (CREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0,
1510 sizeof(*cs) + sizeof(LPARAM) );
1511 if (!cs) return -1;
1512 CREATESTRUCT16to32A( cs16, cs );
1513 cs->lpszName = MapSL(cs16->lpszName);
1514 cs->lpszClass = MapSL(cs16->lpszClass);
1516 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1518 MDICREATESTRUCT16 *mdi_cs16;
1519 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)HeapAlloc(GetProcessHeap(), 0,
1520 sizeof(*mdi_cs));
1521 if (!mdi_cs)
1523 HeapFree(GetProcessHeap(), 0, cs);
1524 return -1;
1526 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1527 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1528 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1529 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1531 cs->lpCreateParams = mdi_cs;
1533 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1534 *plparam = (LPARAM)cs;
1536 return 1;
1537 case WM_PARENTNOTIFY:
1538 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1540 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1541 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1543 return 0;
1544 case WM_WINDOWPOSCHANGING:
1545 case WM_WINDOWPOSCHANGED:
1547 WINDOWPOS *wp = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1548 sizeof(*wp) + sizeof(LPARAM) );
1549 if (!wp) return -1;
1550 WINDOWPOS16to32( MapSL(*plparam), wp );
1551 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1552 *plparam = (LPARAM)wp;
1554 return 1;
1555 case WM_GETDLGCODE:
1556 if (*plparam)
1558 LPMSG16 msg16 = MapSL(*plparam);
1559 LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1561 if (!msg32) return -1;
1562 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1563 msg32->lParam = msg16->lParam;
1564 msg32->time = msg16->time;
1565 msg32->pt.x = msg16->pt.x;
1566 msg32->pt.y = msg16->pt.y;
1567 /* this is right, right? */
1568 if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1569 &msg32->message,&msg32->wParam,
1570 &msg32->lParam)<0) {
1571 HeapFree( GetProcessHeap(), 0, msg32 );
1572 return -1;
1574 *plparam = (LPARAM)msg32;
1575 return 1;
1577 else return 0;
1578 case WM_NOTIFY:
1579 *plparam = (LPARAM)MapSL(*plparam);
1580 return 0;
1581 case WM_ACTIVATEAPP:
1582 /* We need this when SetActiveWindow sends a Sendmessage16() to
1583 * a 32bit window. Might be superflous with 32bit interprocess
1584 * message queues. */
1585 if (*plparam) *plparam = HTASK_32( *plparam );
1586 return 0;
1587 case WM_NEXTMENU:
1589 MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1590 if (!next) return -1;
1591 next->hmenuIn = (HMENU)*plparam;
1592 next->hmenuNext = 0;
1593 next->hwndNext = 0;
1594 *plparam = (LPARAM)next;
1595 return 1;
1597 case WM_PAINTCLIPBOARD:
1598 case WM_SIZECLIPBOARD:
1599 FIXME_(msg)("message %04x needs translation\n",msg16 );
1600 return -1;
1601 case WM_DDE_INITIATE:
1602 case WM_DDE_TERMINATE:
1603 case WM_DDE_UNADVISE:
1604 case WM_DDE_REQUEST:
1605 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1606 return 0;
1607 case WM_DDE_ADVISE:
1608 case WM_DDE_DATA:
1609 case WM_DDE_POKE:
1611 HANDLE16 lo16;
1612 ATOM hi;
1613 UINT lo32 = 0;
1615 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1616 lo16 = LOWORD(*plparam);
1617 hi = HIWORD(*plparam);
1618 if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1619 return -1;
1620 *plparam = PackDDElParam(msg16, lo32, hi);
1622 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1623 case WM_DDE_ACK:
1625 UINT lo, hi;
1626 int flag = 0;
1627 char buf[2];
1629 *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1631 lo = LOWORD(*plparam);
1632 hi = HIWORD(*plparam);
1634 if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1635 if (GlobalSize16(hi) != 0) flag |= 2;
1636 switch (flag)
1638 case 0:
1639 if (hi)
1641 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1642 hi = 0;
1644 break;
1645 case 1:
1646 break; /* atom, nothing to do */
1647 case 3:
1648 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1649 /* fall thru */
1650 case 2:
1651 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1652 break;
1654 *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1656 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1657 case WM_DDE_EXECUTE:
1658 *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1659 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1660 default: /* No translation needed */
1661 return 0;
1666 /**********************************************************************
1667 * WINPROC_UnmapMsg16To32A
1669 * Unmap a message that was mapped from 16- to 32-bit Ansi.
1671 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1672 LRESULT result )
1674 switch(msg)
1676 case WM_COMPAREITEM:
1677 case WM_DELETEITEM:
1678 case WM_DRAWITEM:
1679 HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1680 break;
1681 case WM_MEASUREITEM:
1683 MEASUREITEMSTRUCT16 *mis16;
1684 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1685 lParam = *(LPARAM *)(mis + 1);
1686 mis16 = MapSL(lParam);
1687 mis16->itemWidth = (UINT16)mis->itemWidth;
1688 mis16->itemHeight = (UINT16)mis->itemHeight;
1689 HeapFree( GetProcessHeap(), 0, mis );
1691 break;
1692 case WM_GETMINMAXINFO:
1694 MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1695 lParam = *(LPARAM *)(mmi + 1);
1696 MINMAXINFO32to16( mmi, MapSL(lParam));
1697 HeapFree( GetProcessHeap(), 0, mmi );
1699 break;
1700 case WM_MDICREATE:
1702 MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1703 lParam = *(LPARAM *)(cs + 1);
1704 MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1705 HeapFree( GetProcessHeap(), 0, cs );
1707 break;
1708 case WM_MDIGETACTIVE:
1709 result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1710 HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1711 break;
1712 case WM_NCCALCSIZE:
1714 NCCALCSIZE_PARAMS16 *nc16;
1715 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1716 lParam = *(LPARAM *)(nc + 1);
1717 nc16 = MapSL(lParam);
1718 nc16->rgrc[0].left = nc->rgrc[0].left;
1719 nc16->rgrc[0].top = nc->rgrc[0].top;
1720 nc16->rgrc[0].right = nc->rgrc[0].right;
1721 nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1722 if (wParam)
1724 nc16->rgrc[1].left = nc->rgrc[1].left;
1725 nc16->rgrc[1].top = nc->rgrc[1].top;
1726 nc16->rgrc[1].right = nc->rgrc[1].right;
1727 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1728 nc16->rgrc[2].left = nc->rgrc[2].left;
1729 nc16->rgrc[2].top = nc->rgrc[2].top;
1730 nc16->rgrc[2].right = nc->rgrc[2].right;
1731 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1732 if (nc->lppos)
1734 WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1735 HeapFree( GetProcessHeap(), 0, nc->lppos );
1738 HeapFree( GetProcessHeap(), 0, nc );
1740 break;
1741 case WM_NCCREATE:
1742 case WM_CREATE:
1744 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1745 lParam = *(LPARAM *)(cs + 1);
1746 CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1748 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1749 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1751 HeapFree( GetProcessHeap(), 0, cs );
1753 break;
1754 case WM_WINDOWPOSCHANGING:
1755 case WM_WINDOWPOSCHANGED:
1757 WINDOWPOS *wp = (WINDOWPOS *)lParam;
1758 lParam = *(LPARAM *)(wp + 1);
1759 WINDOWPOS32to16(wp, MapSL(lParam));
1760 HeapFree( GetProcessHeap(), 0, wp );
1762 break;
1763 case WM_GETDLGCODE:
1764 if (lParam)
1766 LPMSG msg32 = (LPMSG)lParam;
1768 WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1769 result);
1770 HeapFree( GetProcessHeap(), 0, msg32 );
1772 break;
1773 case WM_NEXTMENU:
1775 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1776 result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1777 HeapFree( GetProcessHeap(), 0, next );
1779 break;
1781 return result;
1785 /**********************************************************************
1786 * WINPROC_MapMsg16To32W
1788 * Map a message from 16- to 32-bit Unicode.
1789 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1791 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1792 WPARAM *pwparam32, LPARAM *plparam )
1794 BYTE ch;
1795 WCHAR wch;
1797 *pmsg32=(UINT)msg16;
1798 *pwparam32 = (WPARAM)wParam16;
1799 switch(msg16)
1801 case WM_GETTEXT:
1802 case WM_SETTEXT:
1803 case WM_WININICHANGE:
1804 case WM_DEVMODECHANGE:
1805 case WM_ASKCBFORMATNAME:
1806 *plparam = (LPARAM)MapSL(*plparam);
1807 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1808 case WM_GETTEXTLENGTH:
1809 case CB_GETLBTEXTLEN:
1810 case LB_GETTEXTLEN:
1811 return 1; /* need to map result */
1812 case WM_NCCREATE:
1813 case WM_CREATE:
1815 CREATESTRUCT16 *cs16 = MapSL(*plparam);
1816 CREATESTRUCTW *cs = (CREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1817 sizeof(*cs) + sizeof(LPARAM) );
1818 if (!cs) return -1;
1819 CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1820 cs->lpszName = map_str_16_to_32W(cs16->lpszName);
1821 cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1823 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1825 MDICREATESTRUCT16 *mdi_cs16;
1826 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
1827 sizeof(*mdi_cs));
1828 if (!mdi_cs)
1830 HeapFree(GetProcessHeap(), 0, cs);
1831 return -1;
1833 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1834 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1835 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1836 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1838 cs->lpCreateParams = mdi_cs;
1840 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1841 *plparam = (LPARAM)cs;
1843 return 1;
1844 case WM_MDICREATE:
1846 MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1847 MDICREATESTRUCTW *cs =
1848 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1849 sizeof(*cs) + sizeof(LPARAM) );
1850 if (!cs) return -1;
1851 MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1852 cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1853 cs->szClass = map_str_16_to_32W(cs16->szClass);
1854 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
1855 *plparam = (LPARAM)cs;
1857 return 1;
1858 case WM_GETDLGCODE:
1859 if (*plparam)
1861 LPMSG16 msg16 = MapSL(*plparam);
1862 LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1864 if (!msg32) return -1;
1865 msg32->hwnd = WIN_Handle32( msg16->hwnd );
1866 msg32->lParam = msg16->lParam;
1867 msg32->time = msg16->time;
1868 msg32->pt.x = msg16->pt.x;
1869 msg32->pt.y = msg16->pt.y;
1870 /* this is right, right? */
1871 if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1872 &msg32->message,&msg32->wParam,
1873 &msg32->lParam)<0) {
1874 HeapFree( GetProcessHeap(), 0, msg32 );
1875 return -1;
1877 *plparam = (LPARAM)msg32;
1878 return 1;
1880 else return 0;
1882 case WM_CHARTOITEM:
1883 ch = wParam16;
1884 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1885 *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1886 *plparam = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1887 return 0;
1888 case WM_MENUCHAR:
1889 ch = wParam16;
1890 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1891 *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1892 *plparam = (LPARAM)HMENU_32(HIWORD(*plparam));
1893 return 0;
1894 case WM_CHAR:
1895 case WM_DEADCHAR:
1896 case WM_SYSCHAR:
1897 case WM_SYSDEADCHAR:
1898 ch = wParam16;
1899 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1900 *pwparam32 = wch;
1901 return 0;
1902 case WM_IME_CHAR:
1903 return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1905 default: /* No Unicode translation needed */
1906 return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1907 pwparam32, plparam );
1912 /**********************************************************************
1913 * WINPROC_UnmapMsg16To32W
1915 * Unmap a message that was mapped from 16- to 32-bit Unicode.
1917 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1918 LRESULT result )
1920 switch(msg)
1922 case WM_GETTEXT:
1923 case WM_SETTEXT:
1924 case WM_GETTEXTLENGTH:
1925 case CB_GETLBTEXTLEN:
1926 case LB_GETTEXTLEN:
1927 case WM_ASKCBFORMATNAME:
1928 return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
1929 case WM_NCCREATE:
1930 case WM_CREATE:
1932 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1933 lParam = *(LPARAM *)(cs + 1);
1934 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1935 unmap_str_16_to_32W( cs->lpszName );
1936 unmap_str_16_to_32W( cs->lpszClass );
1938 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1940 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1941 unmap_str_16_to_32W( mdi_cs->szTitle );
1942 unmap_str_16_to_32W( mdi_cs->szClass );
1943 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1945 HeapFree( GetProcessHeap(), 0, cs );
1947 break;
1948 case WM_MDICREATE:
1950 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1951 lParam = *(LPARAM *)(cs + 1);
1952 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1953 unmap_str_16_to_32W( cs->szTitle );
1954 unmap_str_16_to_32W( cs->szClass );
1955 HeapFree( GetProcessHeap(), 0, cs );
1957 break;
1958 case WM_GETDLGCODE:
1959 if (lParam)
1961 LPMSG msg32 = (LPMSG)lParam;
1963 WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1964 result);
1965 HeapFree( GetProcessHeap(), 0, msg32 );
1967 break;
1968 default:
1969 return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1971 return result;
1974 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1976 HANDLE16 dst;
1977 UINT sz = GlobalSize((HANDLE)src);
1978 LPSTR ptr16, ptr32;
1980 if (!(dst = GlobalAlloc16(flags, sz)))
1981 return 0;
1982 ptr32 = GlobalLock((HANDLE)src);
1983 ptr16 = GlobalLock16(dst);
1984 if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1985 GlobalUnlock((HANDLE)src);
1986 GlobalUnlock16(dst);
1988 return dst;
1992 /**********************************************************************
1993 * WINPROC_MapMsg32ATo16
1995 * Map a message from 32-bit Ansi to 16-bit.
1996 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1998 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1999 UINT16 *pmsg16, WPARAM16 *pwparam16,
2000 LPARAM *plparam )
2002 *pmsg16 = (UINT16)msg32;
2003 *pwparam16 = (WPARAM16)LOWORD(wParam32);
2004 switch(msg32)
2006 case SBM_SETRANGE:
2007 *pmsg16 = SBM_SETRANGE16;
2008 *plparam = MAKELPARAM(wParam32, *plparam);
2009 *pwparam16 = 0;
2010 return 0;
2012 case SBM_GETRANGE:
2013 *pmsg16 = SBM_GETRANGE16;
2014 return 1;
2016 case BM_GETCHECK:
2017 case BM_SETCHECK:
2018 case BM_GETSTATE:
2019 case BM_SETSTATE:
2020 case BM_SETSTYLE:
2021 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
2022 return 0;
2024 case EM_GETSEL:
2025 case EM_GETRECT:
2026 case EM_SETRECT:
2027 case EM_SETRECTNP:
2028 case EM_SCROLL:
2029 case EM_LINESCROLL:
2030 case EM_SCROLLCARET:
2031 case EM_GETMODIFY:
2032 case EM_SETMODIFY:
2033 case EM_GETLINECOUNT:
2034 case EM_LINEINDEX:
2035 case EM_SETHANDLE:
2036 case EM_GETHANDLE:
2037 case EM_GETTHUMB:
2038 case EM_LINELENGTH:
2039 case EM_REPLACESEL:
2040 case EM_GETLINE:
2041 case EM_LIMITTEXT:
2042 case EM_CANUNDO:
2043 case EM_UNDO:
2044 case EM_FMTLINES:
2045 case EM_LINEFROMCHAR:
2046 case EM_SETTABSTOPS:
2047 case EM_SETPASSWORDCHAR:
2048 case EM_EMPTYUNDOBUFFER:
2049 case EM_GETFIRSTVISIBLELINE:
2050 case EM_SETREADONLY:
2051 case EM_SETWORDBREAKPROC:
2052 case EM_GETWORDBREAKPROC:
2053 case EM_GETPASSWORDCHAR:
2054 *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
2055 return 0;
2057 case LB_CARETOFF:
2058 case LB_CARETON:
2059 case LB_DELETESTRING:
2060 case LB_GETANCHORINDEX:
2061 case LB_GETCARETINDEX:
2062 case LB_GETCOUNT:
2063 case LB_GETCURSEL:
2064 case LB_GETHORIZONTALEXTENT:
2065 case LB_GETITEMDATA:
2066 case LB_GETITEMHEIGHT:
2067 case LB_GETSEL:
2068 case LB_GETSELCOUNT:
2069 case LB_GETTEXTLEN:
2070 case LB_GETTOPINDEX:
2071 case LB_RESETCONTENT:
2072 case LB_SELITEMRANGE:
2073 case LB_SELITEMRANGEEX:
2074 case LB_SETANCHORINDEX:
2075 case LB_SETCARETINDEX:
2076 case LB_SETCOLUMNWIDTH:
2077 case LB_SETCURSEL:
2078 case LB_SETHORIZONTALEXTENT:
2079 case LB_SETITEMDATA:
2080 case LB_SETITEMHEIGHT:
2081 case LB_SETSEL:
2082 case LB_SETTOPINDEX:
2083 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2084 return 0;
2085 case CB_DELETESTRING:
2086 case CB_GETCOUNT:
2087 case CB_GETLBTEXTLEN:
2088 case CB_LIMITTEXT:
2089 case CB_RESETCONTENT:
2090 case CB_SETEDITSEL:
2091 case CB_GETCURSEL:
2092 case CB_SETCURSEL:
2093 case CB_SHOWDROPDOWN:
2094 case CB_SETITEMDATA:
2095 case CB_SETITEMHEIGHT:
2096 case CB_GETITEMHEIGHT:
2097 case CB_SETEXTENDEDUI:
2098 case CB_GETEXTENDEDUI:
2099 case CB_GETDROPPEDSTATE:
2100 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2101 return 0;
2102 case CB_GETEDITSEL:
2103 *pmsg16 = CB_GETEDITSEL16;
2104 return 1;
2106 case LB_ADDSTRING:
2107 case LB_FINDSTRING:
2108 case LB_FINDSTRINGEXACT:
2109 case LB_INSERTSTRING:
2110 case LB_SELECTSTRING:
2111 case LB_DIR:
2112 case LB_ADDFILE:
2113 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2114 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2115 return 1;
2117 case CB_ADDSTRING:
2118 case CB_FINDSTRING:
2119 case CB_FINDSTRINGEXACT:
2120 case CB_INSERTSTRING:
2121 case CB_SELECTSTRING:
2122 case CB_DIR:
2123 *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2124 *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2125 return 1;
2127 case LB_GETITEMRECT:
2129 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2130 if (!rect) return -1;
2131 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2132 *plparam = MapLS( rect );
2134 *pmsg16 = LB_GETITEMRECT16;
2135 return 1;
2136 case LB_GETSELITEMS:
2138 LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2140 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2141 if (!(items = HeapAlloc( GetProcessHeap(), 0,
2142 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2143 *items++ = *plparam; /* Store the previous lParam */
2144 *plparam = MapLS( items );
2146 *pmsg16 = LB_GETSELITEMS16;
2147 return 1;
2148 case LB_SETTABSTOPS:
2149 if (wParam32)
2151 INT i;
2152 LPINT16 stops;
2153 *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2154 if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2155 *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2156 for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2157 *plparam = MapLS( stops );
2158 return 1;
2160 *pmsg16 = LB_SETTABSTOPS16;
2161 return 0;
2163 case CB_GETDROPPEDCONTROLRECT:
2165 RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2166 if (!rect) return -1;
2167 *(LPARAM *)(rect + 1) = *plparam; /* Store the previous lParam */
2168 *plparam = (LPARAM)MapLS(rect);
2170 *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2171 return 1;
2173 case LB_GETTEXT:
2174 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2175 *pmsg16 = LB_GETTEXT16;
2176 return 1;
2178 case CB_GETLBTEXT:
2179 *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2180 *pmsg16 = CB_GETLBTEXT16;
2181 return 1;
2183 case EM_SETSEL:
2184 *pwparam16 = 0;
2185 *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2186 *pmsg16 = EM_SETSEL16;
2187 return 0;
2189 case WM_ACTIVATE:
2190 case WM_CHARTOITEM:
2191 case WM_COMMAND:
2192 case WM_VKEYTOITEM:
2193 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2194 return 0;
2195 case WM_HSCROLL:
2196 case WM_VSCROLL:
2197 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2198 return 0;
2199 case WM_CTLCOLORMSGBOX:
2200 case WM_CTLCOLOREDIT:
2201 case WM_CTLCOLORLISTBOX:
2202 case WM_CTLCOLORBTN:
2203 case WM_CTLCOLORDLG:
2204 case WM_CTLCOLORSCROLLBAR:
2205 case WM_CTLCOLORSTATIC:
2206 *pmsg16 = WM_CTLCOLOR;
2207 *plparam = MAKELPARAM( (HWND16)*plparam,
2208 (WORD)msg32 - WM_CTLCOLORMSGBOX );
2209 return 0;
2210 case WM_COMPAREITEM:
2212 COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2213 COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2214 if (!cis) return -1;
2215 cis->CtlType = (UINT16)cis32->CtlType;
2216 cis->CtlID = (UINT16)cis32->CtlID;
2217 cis->hwndItem = HWND_16( cis32->hwndItem );
2218 cis->itemID1 = (UINT16)cis32->itemID1;
2219 cis->itemData1 = cis32->itemData1;
2220 cis->itemID2 = (UINT16)cis32->itemID2;
2221 cis->itemData2 = cis32->itemData2;
2222 *plparam = MapLS( cis );
2224 return 1;
2225 case WM_DELETEITEM:
2227 DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2228 DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2229 if (!dis) return -1;
2230 dis->CtlType = (UINT16)dis32->CtlType;
2231 dis->CtlID = (UINT16)dis32->CtlID;
2232 dis->itemID = (UINT16)dis32->itemID;
2233 dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2234 : HWND_16( dis32->hwndItem );
2235 dis->itemData = dis32->itemData;
2236 *plparam = MapLS( dis );
2238 return 1;
2239 case WM_DRAWITEM:
2241 DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2242 DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2243 if (!dis) return -1;
2244 dis->CtlType = (UINT16)dis32->CtlType;
2245 dis->CtlID = (UINT16)dis32->CtlID;
2246 dis->itemID = (UINT16)dis32->itemID;
2247 dis->itemAction = (UINT16)dis32->itemAction;
2248 dis->itemState = (UINT16)dis32->itemState;
2249 dis->hwndItem = HWND_16( dis32->hwndItem );
2250 dis->hDC = HDC_16(dis32->hDC);
2251 dis->itemData = dis32->itemData;
2252 dis->rcItem.left = dis32->rcItem.left;
2253 dis->rcItem.top = dis32->rcItem.top;
2254 dis->rcItem.right = dis32->rcItem.right;
2255 dis->rcItem.bottom = dis32->rcItem.bottom;
2256 *plparam = MapLS( dis );
2258 return 1;
2259 case WM_MEASUREITEM:
2261 MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2262 MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2263 if (!mis) return -1;
2264 mis->CtlType = (UINT16)mis32->CtlType;
2265 mis->CtlID = (UINT16)mis32->CtlID;
2266 mis->itemID = (UINT16)mis32->itemID;
2267 mis->itemWidth = (UINT16)mis32->itemWidth;
2268 mis->itemHeight = (UINT16)mis32->itemHeight;
2269 mis->itemData = mis32->itemData;
2270 *(LPARAM *)(mis + 1) = *plparam; /* Store the previous lParam */
2271 *plparam = MapLS( mis );
2273 return 1;
2274 case WM_GETMINMAXINFO:
2276 MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2277 if (!mmi) return -1;
2278 MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2279 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
2280 *plparam = MapLS( mmi );
2282 return 1;
2283 case WM_GETTEXT:
2284 case WM_ASKCBFORMATNAME:
2286 LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2287 *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2288 if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2289 *str++ = *plparam; /* Store the previous lParam */
2290 *plparam = MapLS( str );
2292 return 1;
2293 case WM_MDICREATE:
2295 MDICREATESTRUCT16 *cs;
2296 MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2298 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2299 MDICREATESTRUCT32Ato16( cs32, cs );
2300 cs->szTitle = MapLS( cs32->szTitle );
2301 cs->szClass = MapLS( cs32->szClass );
2302 *plparam = MapLS( cs );
2304 return 1;
2305 case WM_MDIGETACTIVE:
2306 return 1;
2307 case WM_MDISETMENU:
2308 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2309 (HMENU16)LOWORD(*plparam) );
2310 *pwparam16 = (*plparam == 0);
2311 return 0;
2312 case WM_MENUSELECT:
2313 if(HIWORD(wParam32) & MF_POPUP)
2315 HMENU hmenu;
2316 if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2318 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2319 *pwparam16=HMENU_16(hmenu);
2322 /* fall through */
2323 case WM_MENUCHAR:
2324 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2325 return 0;
2326 case WM_MDIACTIVATE:
2327 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2329 *pwparam16 = ((HWND)*plparam == hwnd);
2330 *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2331 (HWND16)LOWORD(wParam32) );
2333 else
2335 *pwparam16 = HWND_16( (HWND)wParam32 );
2336 *plparam = 0;
2338 return 0;
2339 case WM_NCCALCSIZE:
2341 NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2342 NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2343 if (!nc) return -1;
2345 nc->rgrc[0].left = nc32->rgrc[0].left;
2346 nc->rgrc[0].top = nc32->rgrc[0].top;
2347 nc->rgrc[0].right = nc32->rgrc[0].right;
2348 nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2349 if (wParam32)
2351 WINDOWPOS16 *wp;
2352 nc->rgrc[1].left = nc32->rgrc[1].left;
2353 nc->rgrc[1].top = nc32->rgrc[1].top;
2354 nc->rgrc[1].right = nc32->rgrc[1].right;
2355 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2356 nc->rgrc[2].left = nc32->rgrc[2].left;
2357 nc->rgrc[2].top = nc32->rgrc[2].top;
2358 nc->rgrc[2].right = nc32->rgrc[2].right;
2359 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2360 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2362 HeapFree( GetProcessHeap(), 0, nc );
2363 return -1;
2365 WINDOWPOS32to16( nc32->lppos, wp );
2366 nc->lppos = MapLS( wp );
2368 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
2369 *plparam = MapLS( nc );
2371 return 1;
2372 case WM_NCCREATE:
2373 case WM_CREATE:
2375 CREATESTRUCT16 *cs;
2376 CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2378 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2379 CREATESTRUCT32Ato16( cs32, cs );
2380 cs->lpszName = MapLS( cs32->lpszName );
2381 cs->lpszClass = MapLS( cs32->lpszClass );
2383 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2385 MDICREATESTRUCT16 *mdi_cs16;
2386 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2387 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2388 if (!mdi_cs16)
2390 HeapFree(GetProcessHeap(), 0, cs);
2391 return -1;
2393 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2394 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2395 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2396 cs->lpCreateParams = MapLS( mdi_cs16 );
2398 *plparam = MapLS( cs );
2400 return 1;
2401 case WM_PARENTNOTIFY:
2402 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2403 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2404 /* else nothing to do */
2405 return 0;
2406 case WM_NOTIFY:
2407 *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2408 return 1;
2409 case WM_SETTEXT:
2410 case WM_WININICHANGE:
2411 case WM_DEVMODECHANGE:
2412 *plparam = MapLS( (LPSTR)*plparam );
2413 return 1;
2414 case WM_WINDOWPOSCHANGING:
2415 case WM_WINDOWPOSCHANGED:
2417 WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2418 if (!wp) return -1;
2419 WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2420 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
2421 *plparam = MapLS( wp );
2423 return 1;
2424 case WM_GETDLGCODE:
2425 if (*plparam) {
2426 LPMSG msg32 = (LPMSG) *plparam;
2427 LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2429 if (!msg16) return -1;
2430 msg16->hwnd = HWND_16( msg32->hwnd );
2431 msg16->lParam = msg32->lParam;
2432 msg16->time = msg32->time;
2433 msg16->pt.x = msg32->pt.x;
2434 msg16->pt.y = msg32->pt.y;
2435 /* this is right, right? */
2436 if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2437 &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2439 HeapFree( GetProcessHeap(), 0, msg16 );
2440 return -1;
2442 *plparam = MapLS( msg16 );
2443 return 1;
2445 return 0;
2447 case WM_ACTIVATEAPP:
2448 if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2449 return 0;
2450 case WM_NEXTMENU:
2452 MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2453 *plparam = (LPARAM)next->hmenuIn;
2454 return 1;
2456 case WM_PAINTCLIPBOARD:
2457 case WM_SIZECLIPBOARD:
2458 FIXME_(msg)("message %04x needs translation\n", msg32 );
2459 return -1;
2460 /* following messages should not be sent to 16-bit apps */
2461 case WM_SIZING:
2462 case WM_MOVING:
2463 case WM_CAPTURECHANGED:
2464 case WM_STYLECHANGING:
2465 case WM_STYLECHANGED:
2466 return -1;
2467 case WM_DDE_INITIATE:
2468 case WM_DDE_TERMINATE:
2469 case WM_DDE_UNADVISE:
2470 case WM_DDE_REQUEST:
2471 *pwparam16 = HWND_16((HWND)wParam32);
2472 return 0;
2473 case WM_DDE_ADVISE:
2474 case WM_DDE_DATA:
2475 case WM_DDE_POKE:
2477 UINT lo32, hi;
2478 HANDLE16 lo16 = 0;
2480 *pwparam16 = HWND_16((HWND)wParam32);
2481 UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2482 if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2483 return -1;
2484 *plparam = MAKELPARAM(lo16, hi);
2486 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2487 case WM_DDE_ACK:
2489 UINT lo, hi;
2490 int flag = 0;
2491 char buf[2];
2493 *pwparam16 = HWND_16((HWND)wParam32);
2495 UnpackDDElParam(msg32, *plparam, &lo, &hi);
2497 if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2498 if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2499 switch (flag)
2501 case 0:
2502 if (hi)
2504 MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2505 hi = 0;
2507 break;
2508 case 1:
2509 break; /* atom, nothing to do */
2510 case 3:
2511 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2512 /* fall thru */
2513 case 2:
2514 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2515 break;
2517 *plparam = MAKELPARAM(lo, hi);
2519 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2520 case WM_DDE_EXECUTE:
2521 *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2522 return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2523 default: /* No translation needed */
2524 return 0;
2529 /**********************************************************************
2530 * WINPROC_UnmapMsg32ATo16
2532 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2534 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2535 MSGPARAM16* p16 )
2537 switch(msg)
2539 case SBM_GETRANGE:
2540 *(LPINT)wParam = LOWORD(p16->lResult);
2541 *(LPINT)lParam = HIWORD(p16->lResult);
2542 break;
2544 case LB_ADDFILE:
2545 case LB_ADDSTRING:
2546 case LB_DIR:
2547 case LB_FINDSTRING:
2548 case LB_FINDSTRINGEXACT:
2549 case LB_INSERTSTRING:
2550 case LB_SELECTSTRING:
2551 case LB_GETTEXT:
2552 case CB_ADDSTRING:
2553 case CB_FINDSTRING:
2554 case CB_FINDSTRINGEXACT:
2555 case CB_INSERTSTRING:
2556 case CB_SELECTSTRING:
2557 case CB_DIR:
2558 case CB_GETLBTEXT:
2559 case WM_SETTEXT:
2560 case WM_WININICHANGE:
2561 case WM_DEVMODECHANGE:
2562 UnMapLS( (SEGPTR)p16->lParam );
2563 break;
2564 case LB_SETTABSTOPS:
2565 case WM_COMPAREITEM:
2566 case WM_DELETEITEM:
2567 case WM_DRAWITEM:
2569 void *ptr = MapSL( p16->lParam );
2570 UnMapLS( p16->lParam );
2571 HeapFree( GetProcessHeap(), 0, ptr );
2573 break;
2574 case CB_GETDROPPEDCONTROLRECT:
2575 case LB_GETITEMRECT:
2577 RECT *r32;
2578 RECT16 *rect = MapSL(p16->lParam);
2579 UnMapLS( p16->lParam );
2580 p16->lParam = *(LPARAM *)(rect + 1);
2581 r32 = (RECT *)p16->lParam;
2582 r32->left = rect->left;
2583 r32->top = rect->top;
2584 r32->right = rect->right;
2585 r32->bottom = rect->bottom;
2586 HeapFree( GetProcessHeap(), 0, rect );
2588 break;
2589 case LB_GETSELITEMS:
2591 INT i;
2592 LPINT16 items = MapSL(p16->lParam);
2593 UnMapLS( p16->lParam );
2594 p16->lParam = *((LPARAM *)items - 1);
2595 for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2596 HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2598 break;
2600 case CB_GETEDITSEL:
2601 if( wParam )
2602 *((PUINT)(wParam)) = LOWORD(p16->lResult);
2603 if( lParam )
2604 *((PUINT)(lParam)) = HIWORD(p16->lResult); /* FIXME: substract 1? */
2605 break;
2607 case WM_MEASUREITEM:
2609 MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2610 MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2611 mis32->itemWidth = mis->itemWidth;
2612 mis32->itemHeight = mis->itemHeight;
2613 UnMapLS( p16->lParam );
2614 HeapFree( GetProcessHeap(), 0, mis );
2616 break;
2617 case WM_GETMINMAXINFO:
2619 MINMAXINFO16 *mmi = MapSL(p16->lParam);
2620 UnMapLS( p16->lParam );
2621 p16->lParam = *(LPARAM *)(mmi + 1);
2622 MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2623 HeapFree( GetProcessHeap(), 0, mmi );
2625 break;
2626 case WM_GETTEXT:
2627 case WM_ASKCBFORMATNAME:
2629 LPSTR str = MapSL(p16->lParam);
2630 UnMapLS( p16->lParam );
2631 p16->lParam = *((LPARAM *)str - 1);
2632 lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2633 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2635 break;
2636 case WM_MDICREATE:
2638 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2639 UnMapLS( cs->szTitle );
2640 UnMapLS( cs->szClass );
2641 UnMapLS( p16->lParam );
2642 HeapFree( GetProcessHeap(), 0, cs );
2644 break;
2645 case WM_MDIGETACTIVE:
2646 if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2647 p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2648 break;
2649 case WM_NCCALCSIZE:
2651 NCCALCSIZE_PARAMS *nc32;
2652 NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2653 UnMapLS( p16->lParam );
2654 p16->lParam = *(LPARAM *)(nc + 1);
2655 nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2656 nc32->rgrc[0].left = nc->rgrc[0].left;
2657 nc32->rgrc[0].top = nc->rgrc[0].top;
2658 nc32->rgrc[0].right = nc->rgrc[0].right;
2659 nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2660 if (p16->wParam)
2662 WINDOWPOS16 *pos = MapSL(nc->lppos);
2663 UnMapLS( nc->lppos );
2664 nc32->rgrc[1].left = nc->rgrc[1].left;
2665 nc32->rgrc[1].top = nc->rgrc[1].top;
2666 nc32->rgrc[1].right = nc->rgrc[1].right;
2667 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2668 nc32->rgrc[2].left = nc->rgrc[2].left;
2669 nc32->rgrc[2].top = nc->rgrc[2].top;
2670 nc32->rgrc[2].right = nc->rgrc[2].right;
2671 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2672 WINDOWPOS16to32( pos, nc32->lppos );
2673 HeapFree( GetProcessHeap(), 0, pos );
2675 HeapFree( GetProcessHeap(), 0, nc );
2677 break;
2678 case WM_NCCREATE:
2679 case WM_CREATE:
2681 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2682 UnMapLS( p16->lParam );
2683 UnMapLS( cs->lpszName );
2684 UnMapLS( cs->lpszClass );
2685 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2687 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2688 UnMapLS( cs->lpCreateParams );
2689 UnMapLS( mdi_cs16->szTitle );
2690 UnMapLS( mdi_cs16->szClass );
2691 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2693 HeapFree( GetProcessHeap(), 0, cs );
2695 break;
2696 case WM_WINDOWPOSCHANGING:
2697 case WM_WINDOWPOSCHANGED:
2699 WINDOWPOS16 *wp = MapSL(p16->lParam);
2700 UnMapLS( p16->lParam );
2701 p16->lParam = *(LPARAM *)(wp + 1);
2702 WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2703 HeapFree( GetProcessHeap(), 0, wp );
2705 break;
2706 case WM_NOTIFY:
2707 UnMapLS(p16->lParam);
2708 break;
2709 case WM_GETDLGCODE:
2710 if (p16->lParam)
2712 LPMSG16 msg16 = MapSL(p16->lParam);
2713 MSGPARAM16 msgp16;
2714 UnMapLS( p16->lParam );
2715 msgp16.wParam=msg16->wParam;
2716 msgp16.lParam=msg16->lParam;
2717 WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2718 ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2719 &msgp16 );
2720 HeapFree( GetProcessHeap(), 0, msg16 );
2722 break;
2723 case WM_NEXTMENU:
2725 MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2726 next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2727 next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2728 p16->lResult = 0;
2730 break;
2735 /**********************************************************************
2736 * WINPROC_MapMsg32WTo16
2738 * Map a message from 32-bit Unicode to 16-bit.
2739 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2741 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2742 UINT16 *pmsg16, WPARAM16 *pwparam16,
2743 LPARAM *plparam )
2745 BYTE ch;
2746 WCHAR wch;
2748 *pmsg16 = LOWORD(msg32);
2749 *pwparam16 = LOWORD(wParam32);
2750 switch(msg32)
2752 case LB_ADDSTRING:
2753 case LB_FINDSTRING:
2754 case LB_FINDSTRINGEXACT:
2755 case LB_INSERTSTRING:
2756 case LB_SELECTSTRING:
2757 case LB_DIR:
2758 case LB_ADDFILE:
2759 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2760 *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2761 return 1;
2763 case CB_ADDSTRING:
2764 case CB_FINDSTRING:
2765 case CB_FINDSTRINGEXACT:
2766 case CB_INSERTSTRING:
2767 case CB_SELECTSTRING:
2768 case CB_DIR:
2769 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2770 *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2771 return 1;
2773 case WM_NCCREATE:
2774 case WM_CREATE:
2776 CREATESTRUCT16 *cs;
2777 CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2779 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2780 CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2781 cs->lpszName = map_str_32W_to_16( cs32->lpszName );
2782 cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2784 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2786 MDICREATESTRUCT16 *mdi_cs16;
2787 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2788 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2789 if (!mdi_cs16)
2791 HeapFree(GetProcessHeap(), 0, cs);
2792 return -1;
2794 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2795 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2796 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2797 cs->lpCreateParams = MapLS(mdi_cs16);
2799 *plparam = MapLS(cs);
2801 return 1;
2802 case WM_MDICREATE:
2804 MDICREATESTRUCT16 *cs;
2805 MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2807 if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2808 MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2809 cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2810 cs->szClass = map_str_32W_to_16( cs32->szClass );
2811 *plparam = MapLS(cs);
2813 return 1;
2814 case WM_SETTEXT:
2815 case WM_WININICHANGE:
2816 case WM_DEVMODECHANGE:
2817 *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2818 return 1;
2819 case LB_GETTEXT:
2820 if ( WINPROC_TestLBForStr( hwnd ))
2822 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2823 if (!str) return -1;
2824 *pmsg16 = LB_GETTEXT16;
2825 *plparam = (LPARAM)MapLS(str);
2827 return 1;
2828 case CB_GETLBTEXT:
2829 if ( WINPROC_TestCBForStr( hwnd ))
2831 LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2832 if (!str) return -1;
2833 *pmsg16 = CB_GETLBTEXT16;
2834 *plparam = (LPARAM)MapLS(str);
2836 return 1;
2838 case WM_CHARTOITEM:
2839 wch = LOWORD(wParam32);
2840 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2841 *pwparam16 = ch;
2842 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2843 return 0;
2844 case WM_MENUCHAR:
2845 wch = LOWORD(wParam32);
2846 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2847 *pwparam16 = ch;
2848 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2849 return 0;
2850 case WM_CHAR:
2851 case WM_DEADCHAR:
2852 case WM_SYSCHAR:
2853 case WM_SYSDEADCHAR:
2854 wch = wParam32;
2855 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2856 *pwparam16 = ch;
2857 return 0;
2858 case WM_IME_CHAR:
2860 BYTE ch[2];
2862 wch = wParam32;
2863 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
2864 *pwparam16 = (ch[0] << 8) | ch[1];
2865 else
2866 *pwparam16 = ch[0];
2868 return 0;
2870 default: /* No Unicode translation needed (?) */
2871 return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2872 pwparam16, plparam );
2877 /**********************************************************************
2878 * WINPROC_UnmapMsg32WTo16
2880 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2882 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2883 MSGPARAM16* p16 )
2885 switch(msg)
2887 case LB_ADDSTRING:
2888 case LB_FINDSTRING:
2889 case LB_FINDSTRINGEXACT:
2890 case LB_INSERTSTRING:
2891 case LB_SELECTSTRING:
2892 case LB_DIR:
2893 case LB_ADDFILE:
2894 case CB_ADDSTRING:
2895 case CB_FINDSTRING:
2896 case CB_FINDSTRINGEXACT:
2897 case CB_INSERTSTRING:
2898 case CB_SELECTSTRING:
2899 case CB_DIR:
2900 case WM_SETTEXT:
2901 case WM_WININICHANGE:
2902 case WM_DEVMODECHANGE:
2903 unmap_str_32W_to_16( p16->lParam );
2904 break;
2905 case WM_NCCREATE:
2906 case WM_CREATE:
2908 CREATESTRUCT16 *cs = MapSL(p16->lParam);
2909 UnMapLS( p16->lParam );
2910 unmap_str_32W_to_16( cs->lpszName );
2911 unmap_str_32W_to_16( cs->lpszClass );
2913 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2915 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2916 UnMapLS( cs->lpCreateParams );
2917 unmap_str_32W_to_16(mdi_cs16->szTitle);
2918 unmap_str_32W_to_16(mdi_cs16->szClass);
2919 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2921 HeapFree( GetProcessHeap(), 0, cs );
2923 break;
2924 case WM_MDICREATE:
2926 MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2927 UnMapLS( p16->lParam );
2928 unmap_str_32W_to_16( cs->szTitle );
2929 unmap_str_32W_to_16( cs->szClass );
2930 HeapFree( GetProcessHeap(), 0, cs );
2932 break;
2933 case WM_GETTEXT:
2934 case WM_ASKCBFORMATNAME:
2936 LPSTR str = MapSL(p16->lParam);
2937 UnMapLS( p16->lParam );
2938 p16->lParam = *((LPARAM *)str - 1);
2939 MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2940 p16->lResult = strlenW( (LPWSTR)p16->lParam );
2941 HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2943 break;
2944 case LB_GETTEXT:
2945 if ( WINPROC_TestLBForStr( hwnd ))
2947 LPSTR str = MapSL(p16->lParam);
2948 UnMapLS( p16->lParam );
2949 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2950 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2952 break;
2953 case CB_GETLBTEXT:
2954 if ( WINPROC_TestCBForStr( hwnd ))
2956 LPSTR str = MapSL(p16->lParam);
2957 UnMapLS( p16->lParam );
2958 p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2959 HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2961 break;
2962 default:
2963 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2964 break;
2969 /**********************************************************************
2970 * WINPROC_CallProc32ATo32W
2972 * Call a window procedure, translating args from Ansi to Unicode.
2974 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
2975 UINT msg, WPARAM wParam,
2976 LPARAM lParam )
2978 LRESULT result;
2979 int unmap;
2981 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2982 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2984 if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2985 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2986 SPY_GetMsgName(msg, hwnd), wParam, lParam );
2987 return 0;
2989 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2990 if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
2991 return result;
2995 /**********************************************************************
2996 * WINPROC_CallProc32WTo32A_fast
2999 static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
3000 UINT msg, WPARAM wParam,
3001 LPARAM lParam, LRESULT *result )
3003 switch(msg)
3005 case WM_NCCREATE:
3006 case WM_CREATE:
3007 { /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
3008 * at this point.
3010 char buffer[1024];
3011 char *cls = buffer, *name;
3012 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
3013 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
3014 DWORD name_lenA, name_lenW, class_lenA, class_lenW;
3016 class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
3017 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
3019 if (csW->lpszName)
3021 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
3022 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
3024 else
3025 name_lenW = name_lenA = 0;
3027 if (class_lenA + name_lenA + 2 > sizeof(buffer))
3029 cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
3030 if (!cls) return FALSE;
3033 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
3034 cls[class_lenA] = 0;
3035 csA.lpszClass = cls;
3037 if (csW->lpszName)
3039 name = cls + class_lenA + 1;
3040 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3041 name[name_lenA] = 0;
3042 csA.lpszName = name;
3045 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3047 MDICREATESTRUCTA mdi_cs;
3049 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3050 mdi_cs.szTitle = csA.lpszName;
3051 mdi_cs.szClass = csA.lpszClass;
3052 csA.lpCreateParams = &mdi_cs;
3055 lParam = (LPARAM)&csA;
3056 *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);
3058 if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
3060 return TRUE;
3062 default:
3063 return FALSE;
3067 /**********************************************************************
3068 * WINPROC_CallProc32WTo32A
3070 * Call a window procedure, translating args from Unicode to Ansi.
3072 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
3073 UINT msg, WPARAM wParam,
3074 LPARAM lParam )
3076 LRESULT result;
3077 int unmap;
3079 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3080 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3082 if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
3083 return result;
3085 if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3086 ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3087 SPY_GetMsgName(msg, hwnd), wParam, lParam );
3088 return 0;
3090 result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3091 if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3092 return result;
3096 /**********************************************************************
3097 * __wine_call_wndproc_32A (USER.1010)
3099 LRESULT WINAPI __wine_call_wndproc_32A( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3100 WNDPROC func )
3102 LRESULT result;
3103 UINT msg32;
3104 WPARAM wParam32;
3105 HWND hwnd32 = WIN_Handle32( hwnd );
3107 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3108 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3110 if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3111 return 0;
3112 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3113 return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3117 /**********************************************************************
3118 * __wine_call_wndproc_32W (USER.1011)
3120 LRESULT WINAPI __wine_call_wndproc_32W( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3121 WNDPROC func )
3123 LRESULT result;
3124 UINT msg32;
3125 WPARAM wParam32;
3126 HWND hwnd32 = WIN_Handle32( hwnd );
3128 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3129 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3131 if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3132 return 0;
3133 result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3134 return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result );
3138 /**********************************************************************
3139 * WINPROC_CallProc32ATo16
3141 * Call a 16-bit window procedure, translating the 32-bit args.
3143 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
3144 UINT msg, WPARAM wParam,
3145 LPARAM lParam )
3147 UINT16 msg16;
3148 MSGPARAM16 mp16;
3150 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3151 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3153 mp16.lParam = lParam;
3154 if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3155 return 0;
3156 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3157 mp16.wParam, mp16.lParam );
3158 WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3159 return mp16.lResult;
3163 /**********************************************************************
3164 * WINPROC_CallProc32WTo16
3166 * Call a 16-bit window procedure, translating the 32-bit args.
3168 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
3169 UINT msg, WPARAM wParam,
3170 LPARAM lParam )
3172 UINT16 msg16;
3173 MSGPARAM16 mp16;
3175 TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3176 func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3178 mp16.lParam = lParam;
3179 if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
3180 &mp16.lParam ) == -1)
3181 return 0;
3182 mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3183 mp16.wParam, mp16.lParam );
3184 WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3185 return mp16.lResult;
3189 /**********************************************************************
3190 * CallWindowProc (USER.122)
3192 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3193 WPARAM16 wParam, LPARAM lParam )
3195 WINDOWPROC *proc;
3197 if (!func) return 0;
3199 if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3200 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3202 #if testing
3203 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_16 );
3204 return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3205 #endif
3207 switch(proc->type)
3209 case WIN_PROC_16:
3210 if (!proc->thunk.t_from32.proc) return 0;
3211 return WINPROC_CallWndProc16( proc->thunk.t_from32.proc,
3212 hwnd, msg, wParam, lParam );
3213 case WIN_PROC_32A:
3214 if (!proc->thunk.t_from16.proc) return 0;
3215 return __wine_call_wndproc_32A( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3216 case WIN_PROC_32W:
3217 if (!proc->thunk.t_from16.proc) return 0;
3218 return __wine_call_wndproc_32W( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3219 default:
3220 WARN_(relay)("Invalid proc %p\n", proc );
3221 return 0;
3226 /**********************************************************************
3227 * CallWindowProcA (USER32.@)
3229 * The CallWindowProc() function invokes the windows procedure _func_,
3230 * with _hwnd_ as the target window, the message specified by _msg_, and
3231 * the message parameters _wParam_ and _lParam_.
3233 * Some kinds of argument conversion may be done, I'm not sure what.
3235 * CallWindowProc() may be used for windows subclassing. Use
3236 * SetWindowLong() to set a new windows procedure for windows of the
3237 * subclass, and handle subclassed messages in the new windows
3238 * procedure. The new windows procedure may then use CallWindowProc()
3239 * with _func_ set to the parent class's windows procedure to dispatch
3240 * the message to the superclass.
3242 * RETURNS
3244 * The return value is message dependent.
3246 * CONFORMANCE
3248 * ECMA-234, Win32
3250 LRESULT WINAPI CallWindowProcA(
3251 WNDPROC func, /* [in] window procedure */
3252 HWND hwnd, /* [in] target window */
3253 UINT msg, /* [in] message */
3254 WPARAM wParam, /* [in] message dependent parameter */
3255 LPARAM lParam /* [in] message dependent parameter */
3257 WINDOWPROC *proc;
3259 if (!func) return 0;
3261 if (!(proc = WINPROC_GetPtr( func )))
3262 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3264 #if testing
3265 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32A );
3266 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3267 #endif
3269 switch(proc->type)
3271 case WIN_PROC_16:
3272 if (!proc->thunk.t_from32.proc) return 0;
3273 return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
3274 hwnd, msg, wParam, lParam );
3275 case WIN_PROC_32A:
3276 if (!proc->thunk.t_from16.proc) return 0;
3277 return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3278 hwnd, msg, wParam, lParam );
3279 case WIN_PROC_32W:
3280 if (!proc->thunk.t_from16.proc) return 0;
3281 return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
3282 hwnd, msg, wParam, lParam );
3283 default:
3284 WARN_(relay)("Invalid proc %p\n", proc );
3285 return 0;
3290 /**********************************************************************
3291 * CallWindowProcW (USER32.@)
3293 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3294 WPARAM wParam, LPARAM lParam )
3296 WINDOWPROC *proc;
3298 if (!func) return 0;
3300 if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3301 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3303 #if testing
3304 func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32W );
3305 return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3306 #endif
3308 switch(proc->type)
3310 case WIN_PROC_16:
3311 if (!proc->thunk.t_from32.proc) return 0;
3312 return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
3313 hwnd, msg, wParam, lParam );
3314 case WIN_PROC_32A:
3315 if (!proc->thunk.t_from16.proc) return 0;
3316 return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
3317 hwnd, msg, wParam, lParam );
3318 case WIN_PROC_32W:
3319 if (!proc->thunk.t_from16.proc) return 0;
3320 return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3321 hwnd, msg, wParam, lParam );
3322 default:
3323 WARN_(relay)("Invalid proc %p\n", proc );
3324 return 0;