Release 960818
[wine/multimedia.git] / windows / winproc.c
blobd1c89d7eeec551560042121c91e6d4487a6e1e78
1 /*
2 * Window procedure callbacks
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <stdio.h>
9 #include "windows.h"
10 #include "callback.h"
11 #include "heap.h"
12 #include "ldt.h"
13 #include "registers.h"
14 #include "stackframe.h"
15 #include "string32.h"
16 #include "struct32.h"
17 #include "win.h"
18 #include "winproc.h"
19 #include "stddebug.h"
20 #include "debug.h"
22 /* Window procedure 16-bit thunk; see BuildSpec16Files() in tools/build.c */
23 typedef struct
25 BYTE popl_eax; /* popl %eax (return address) */
26 BYTE pushl_func; /* pushl $proc */
27 WNDPROC32 proc WINE_PACKED;
28 BYTE pushl_eax; /* pushl %eax */
29 WORD pushw_bp WINE_PACKED; /* pushw %bp */
30 BYTE pushl_thunk; /* pushl $thunkfrom16 */
31 void (*thunk32)() WINE_PACKED;
32 BYTE lcall; /* lcall cs:relay */
33 void (*relay)() WINE_PACKED;
34 WORD cs WINE_PACKED;
35 } WINPROC_THUNK_FROM16;
37 /* Window procedure 32-bit thunk; see BuildSpec32Files() in tools/build.c */
38 typedef struct
40 BYTE popl_eax; /* popl %eax (return address) */
41 BYTE pushl_func; /* pushl $proc */
42 WNDPROC16 proc WINE_PACKED;
43 BYTE pushl_eax; /* pushl %eax */
44 BYTE pushl_ebp; /* pushl %ebp */
45 BYTE pushl_name; /* pushl $name */
46 LPCSTR name WINE_PACKED;
47 BYTE pushl_thunk; /* pushl $thunkfrom32 */
48 void (*thunk32)() WINE_PACKED;
49 BYTE jmp; /* jmp relay (relative jump)*/
50 void (*relay)() WINE_PACKED;
51 } WINPROC_THUNK_FROM32;
53 /* Simple jmp to call 32-bit procedure directly */
54 typedef struct
56 BYTE jmp; /* jmp proc (relative jump) */
57 WNDPROC32 proc WINE_PACKED;
58 } WINPROC_JUMP;
60 typedef union
62 WINPROC_THUNK_FROM16 t_from16;
63 WINPROC_THUNK_FROM32 t_from32;
64 } WINPROC_THUNK;
66 typedef struct tagWINDOWPROC
68 WINPROC_THUNK thunk; /* Thunk */
69 WINPROC_JUMP jmp; /* Jump */
70 struct tagWINDOWPROC *next; /* Next window proc */
71 UINT32 magic; /* Magic number */
72 WINDOWPROCTYPE type; /* Function type */
73 } WINDOWPROC;
75 #define WINPROC_MAGIC ('W' | ('P' << 8) | ('R' << 16) | ('C' << 24))
77 #define WINPROC_THUNKPROC(pproc) \
78 (((pproc)->type == WIN_PROC_16) ? \
79 (WNDPROC16)((pproc)->thunk.t_from32.proc) : \
80 (WNDPROC16)((pproc)->thunk.t_from16.proc))
82 LRESULT WINPROC_CallProc16To32A( HWND16 hwnd, UINT16 msg,
83 WPARAM16 wParam, LPARAM lParam,
84 WNDPROC32 func );
85 LRESULT WINPROC_CallProc16To32W( HWND16 hwnd, UINT16 msg,
86 WPARAM16 wParam, LPARAM lParam,
87 WNDPROC32 func );
88 static LRESULT WINPROC_CallProc32ATo16( WNDPROC16 func, HWND32 hwnd,
89 UINT32 msg, WPARAM32 wParam,
90 LPARAM lParam );
91 static LRESULT WINPROC_CallProc32WTo16( WNDPROC16 func, HWND32 hwnd,
92 UINT32 msg, WPARAM32 wParam,
93 LPARAM lParam );
95 #ifndef WINELIB
96 extern void CallFrom16_long_wwwll(void);
97 extern void CallFrom32_stdcall_5(void);
98 #else
99 static void CallFrom16_long_wwwll(void) {}
100 static void CallFrom32_stdcall_5(void) {}
101 #endif /* WINELIB */
103 static HANDLE32 WinProcHeap;
105 /**********************************************************************
106 * WINPROC_Init
108 BOOL32 WINPROC_Init(void)
110 WinProcHeap = HeapCreate( HEAP_WINE_SEGPTR | HEAP_WINE_CODESEG, 0, 0 );
111 if (!WinProcHeap)
113 fprintf( stderr, "Unable to create winproc heap\n" );
114 return FALSE;
116 return TRUE;
120 /**********************************************************************
121 * WINPROC_GetPtr
123 * Return a pointer to the win proc.
125 static WINDOWPROC *WINPROC_GetPtr( WNDPROC16 handle )
127 BYTE *ptr;
128 WINDOWPROC *proc;
130 /* Check for a linear pointer */
132 if (HEAP_IsInsideHeap( WinProcHeap, 0, (LPVOID)handle ))
134 ptr = (BYTE *)handle;
135 /* First check if it is the jmp address */
136 if (*ptr == 0xe9 /* jmp */) ptr -= (int)&((WINDOWPROC *)0)->jmp -
137 (int)&((WINDOWPROC *)0)->thunk;
138 /* Now it must be the thunk address */
139 if (*ptr == 0x58 /* popl eax */) ptr -= (int)&((WINDOWPROC *)0)->thunk;
140 /* Now we have a pointer to the WINDOWPROC struct */
141 if (((WINDOWPROC *)ptr)->magic == WINPROC_MAGIC)
142 return (WINDOWPROC *)ptr;
145 /* Check for a segmented pointer */
147 if (!IsBadReadPtr( (SEGPTR)handle, sizeof(WINDOWPROC)-sizeof(proc->thunk)))
149 ptr = (BYTE *)PTR_SEG_TO_LIN(handle);
150 if (!HEAP_IsInsideHeap( WinProcHeap, 0, ptr )) return NULL;
151 /* It must be the thunk address */
152 if (*ptr == 0x58 /* popl eax */) ptr -= (int)&((WINDOWPROC *)0)->thunk;
153 /* Now we have a pointer to the WINDOWPROC struct */
154 if (((WINDOWPROC *)ptr)->magic == WINPROC_MAGIC)
155 return (WINDOWPROC *)ptr;
158 return NULL;
162 /**********************************************************************
163 * WINPROC_AllocWinProc
165 * Allocate a new window procedure.
167 static WINDOWPROC *WINPROC_AllocWinProc( WNDPROC16 func, WINDOWPROCTYPE type )
169 WINDOWPROC *proc, *oldproc;
171 /* Allocate a window procedure */
173 if (!(proc = HeapAlloc( WinProcHeap, 0, sizeof(WINDOWPROC) ))) return 0;
175 /* Check if the function is already a win proc */
177 if ((oldproc = WINPROC_GetPtr( func )))
179 *proc = *oldproc;
181 else
183 switch(type)
185 case WIN_PROC_16:
186 proc->thunk.t_from32.popl_eax = 0x58; /* popl %eax */
187 proc->thunk.t_from32.pushl_func = 0x68; /* pushl $proc */
188 proc->thunk.t_from32.proc = func;
189 proc->thunk.t_from32.pushl_eax = 0x50; /* pushl %eax */
190 proc->thunk.t_from32.pushl_ebp = 0x55; /* pushl %ebp */
191 proc->thunk.t_from32.pushl_name = 0x68; /* pushl $name */
192 proc->thunk.t_from32.name = "WINPROC_CallProc32ATo16";
193 proc->thunk.t_from32.pushl_thunk = 0x68; /* pushl $thunkfrom32 */
194 proc->thunk.t_from32.thunk32 = (void(*)())WINPROC_CallProc32ATo16;
195 proc->thunk.t_from32.jmp = 0xe9; /* jmp relay*/
196 proc->thunk.t_from32.relay = /* relative jump */
197 (void (*)())((DWORD)CallFrom32_stdcall_5 -
198 (DWORD)(&proc->thunk.t_from32.relay + 1));
199 break;
200 case WIN_PROC_32A:
201 case WIN_PROC_32W:
202 proc->thunk.t_from16.popl_eax = 0x58; /* popl %eax */
203 proc->thunk.t_from16.pushl_func = 0x68; /* pushl $proc */
204 proc->thunk.t_from16.proc = (FARPROC32)func;
205 proc->thunk.t_from16.pushl_eax = 0x50; /* pushl %eax */
206 proc->thunk.t_from16.pushw_bp = 0x5566; /* pushw %bp */
207 proc->thunk.t_from16.pushl_thunk = 0x68; /* pushl $thunkfrom16 */
208 proc->thunk.t_from16.thunk32 = (type == WIN_PROC_32A) ?
209 (void(*)())WINPROC_CallProc16To32A :
210 (void(*)())WINPROC_CallProc16To32W;
211 proc->thunk.t_from16.lcall = 0x9a; /* lcall cs:relay */
212 proc->thunk.t_from16.relay = CallFrom16_long_wwwll;
213 proc->thunk.t_from16.cs = WINE_CODE_SELECTOR;
214 proc->jmp.jmp = 0xe9;
215 /* Fixup relative jump */
216 proc->jmp.proc = (WNDPROC32)((DWORD)func -
217 (DWORD)(&proc->jmp.proc + 1));
218 break;
219 default:
220 /* Should not happen */
221 break;
223 proc->magic = WINPROC_MAGIC;
224 proc->type = type;
226 proc->next = NULL;
227 dprintf_win( stddeb, "WINPROC_AllocWinProc(%08x,%d): returning %08x\n",
228 (UINT32)func, type, (UINT32)proc );
229 return proc;
233 /**********************************************************************
234 * WINPROC_GetProc
236 * Get a window procedure pointer that can be passed to the Windows program.
238 WNDPROC16 WINPROC_GetProc( HWINDOWPROC proc, WINDOWPROCTYPE type )
240 if (type == WIN_PROC_16) /* We want a 16:16 address */
242 if (((WINDOWPROC *)proc)->type == WIN_PROC_16)
243 return ((WINDOWPROC *)proc)->thunk.t_from32.proc;
244 else
245 return (WNDPROC16)HEAP_GetSegptr( WinProcHeap, 0,
246 &((WINDOWPROC *)proc)->thunk );
248 else /* We want a 32-bit address */
250 if (((WINDOWPROC *)proc)->type == WIN_PROC_16)
251 return (WNDPROC16)&((WINDOWPROC *)proc)->thunk;
252 else
253 return (WNDPROC16)&((WINDOWPROC *)proc)->jmp;
258 /**********************************************************************
259 * WINPROC_SetProc
261 * Set the window procedure for a window or class.
263 BOOL32 WINPROC_SetProc( HWINDOWPROC *pFirst, WNDPROC16 func,
264 WINDOWPROCTYPE type )
266 WINDOWPROC *proc, **ppPrev;
268 /* Check if function is already in the list */
270 ppPrev = (WINDOWPROC **)pFirst;
271 proc = WINPROC_GetPtr( func );
272 while (*ppPrev)
274 if (proc)
276 if (*ppPrev == proc) break;
278 else
280 if (((*ppPrev)->type == type) &&
281 (func == WINPROC_THUNKPROC(*ppPrev))) break;
283 ppPrev = &(*ppPrev)->next;
286 if (*ppPrev) /* Remove it from the list */
288 proc = *ppPrev;
289 *ppPrev = proc->next;
291 else /* Allocate a new one */
293 if (proc) /* Was already a win proc */
295 type = proc->type;
296 func = WINPROC_THUNKPROC(proc);
298 proc = WINPROC_AllocWinProc( func, type );
299 if (!proc) return FALSE;
302 /* Add the win proc at the head of the list */
304 dprintf_win( stddeb, "WINPROC_SetProc(%08x,%08x,%d): res=%08x\n",
305 (UINT32)*pFirst, (UINT32)func, type, (UINT32)proc );
306 proc->next = *(WINDOWPROC **)pFirst;
307 *(WINDOWPROC **)pFirst = proc;
308 return TRUE;
312 /**********************************************************************
313 * WINPROC_FreeProc
315 * Free a list of win procs.
317 void WINPROC_FreeProc( HWINDOWPROC proc )
319 while (proc)
321 WINDOWPROC *next = ((WINDOWPROC *)proc)->next;
322 dprintf_win( stddeb, "WINPROC_FreeProc: freeing %08x\n", (UINT32)proc);
323 HeapFree( WinProcHeap, 0, proc );
324 proc = next;
329 /**********************************************************************
330 * WINPROC_GetProcType
332 * Return the window procedure type.
334 WINDOWPROCTYPE WINPROC_GetProcType( HWINDOWPROC proc )
336 if (!proc ||
337 (((WINDOWPROC *)proc)->magic != WINPROC_MAGIC))
338 return WIN_PROC_INVALID;
339 return ((WINDOWPROC *)proc)->type;
343 /**********************************************************************
344 * WINPROC_MapMsg32ATo32W
346 * Map a message from Ansi to Unicode.
347 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
349 INT32 WINPROC_MapMsg32ATo32W( UINT32 msg, WPARAM32 wParam, LPARAM *plparam )
351 switch(msg)
353 case WM_GETTEXT:
355 LPARAM *ptr = (LPARAM *)HeapAlloc( SystemHeap, 0,
356 wParam * sizeof(WCHAR) + sizeof(LPARAM) );
357 if (!ptr) return -1;
358 *ptr++ = *plparam; /* Store previous lParam */
359 *plparam = (LPARAM)ptr;
361 return 1;
362 case WM_SETTEXT:
363 *plparam = (LPARAM)STRING32_DupAnsiToUni( (LPCSTR)*plparam );
364 return (*plparam ? 1 : -1);
365 case WM_NCCREATE:
366 case WM_CREATE:
368 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)HeapAlloc( SystemHeap, 0,
369 sizeof(*cs) );
370 if (!cs) return -1;
371 *cs = *(CREATESTRUCT32W *)*plparam;
372 if (HIWORD(cs->lpszName))
373 cs->lpszName = STRING32_DupAnsiToUni( (LPCSTR)cs->lpszName );
374 if (HIWORD(cs->lpszClass))
375 cs->lpszClass = STRING32_DupAnsiToUni( (LPCSTR)cs->lpszClass );
376 *plparam = (LPARAM)cs;
378 return 1;
379 case WM_MDICREATE:
381 MDICREATESTRUCT32W *cs =
382 (MDICREATESTRUCT32W *)HeapAlloc( SystemHeap, 0, sizeof(*cs) );
383 if (!cs) return -1;
384 *cs = *(MDICREATESTRUCT32W *)*plparam;
385 if (HIWORD(cs->szClass))
386 cs->szClass = STRING32_DupAnsiToUni( (LPCSTR)cs->szClass );
387 if (HIWORD(cs->szTitle))
388 cs->szTitle = STRING32_DupAnsiToUni( (LPCSTR)cs->szTitle );
389 *plparam = (LPARAM)cs;
391 return 1;
392 case WM_ASKCBFORMATNAME:
393 case WM_COMPAREITEM:
394 case WM_DELETEITEM:
395 case WM_DEVMODECHANGE:
396 case WM_MDIACTIVATE:
397 case WM_MEASUREITEM:
398 case WM_PAINTCLIPBOARD:
399 case WM_SIZECLIPBOARD:
400 case WM_WININICHANGE:
401 fprintf( stderr, "MapMsg32ATo32W: message %04x needs translation\n",
402 msg );
403 return -1;
404 default: /* No translation needed */
405 return 0;
410 /**********************************************************************
411 * WINPROC_UnmapMsg32ATo32W
413 * Unmap a message that was mapped from Ansi to Unicode.
415 void WINPROC_UnmapMsg32ATo32W( UINT32 msg, WPARAM32 wParam, LPARAM lParam )
417 switch(msg)
419 case WM_GETTEXT:
421 LPARAM *ptr = (LPARAM *)lParam - 1;
422 lstrcpynWtoA( (LPSTR)*ptr, (LPWSTR)(ptr + 1), wParam );
423 HeapFree( SystemHeap, 0, ptr );
425 break;
426 case WM_SETTEXT:
427 free( (void *)lParam );
428 break;
429 case WM_NCCREATE:
430 case WM_CREATE:
432 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
433 if (HIWORD(cs->lpszName)) free( (LPVOID)cs->lpszName );
434 if (HIWORD(cs->lpszClass)) free( (LPVOID)cs->lpszClass );
435 HeapFree( SystemHeap, 0, cs );
437 break;
438 case WM_MDICREATE:
440 MDICREATESTRUCT32W *cs = (MDICREATESTRUCT32W *)lParam;
441 if (HIWORD(cs->szTitle)) free( (LPVOID)cs->szTitle );
442 if (HIWORD(cs->szClass)) free( (LPVOID)cs->szClass );
443 HeapFree( SystemHeap, 0, cs );
445 break;
450 /**********************************************************************
451 * WINPROC_MapMsg32WTo32A
453 * Map a message from Unicode to Ansi.
454 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
456 INT32 WINPROC_MapMsg32WTo32A( UINT32 msg, WPARAM32 wParam, LPARAM *plparam )
458 switch(msg)
460 case WM_GETTEXT:
462 LPARAM *ptr = (LPARAM *)HeapAlloc( SystemHeap, 0,
463 wParam + sizeof(LPARAM) );
464 if (!ptr) return -1;
465 *ptr++ = *plparam; /* Store previous lParam */
466 *plparam = (LPARAM)ptr;
468 return 1;
469 case WM_SETTEXT:
470 *plparam = (LPARAM)STRING32_DupUniToAnsi( (LPCWSTR)*plparam );
471 return (*plparam ? 1 : -1);
472 case WM_NCCREATE:
473 case WM_CREATE:
475 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)HeapAlloc( SystemHeap, 0,
476 sizeof(*cs) );
477 if (!cs) return -1;
478 *cs = *(CREATESTRUCT32A *)*plparam;
479 if (HIWORD(cs->lpszName))
480 cs->lpszName = STRING32_DupUniToAnsi( (LPCWSTR)cs->lpszName );
481 if (HIWORD(cs->lpszClass))
482 cs->lpszClass = STRING32_DupUniToAnsi( (LPCWSTR)cs->lpszClass);
483 *plparam = (LPARAM)cs;
485 return 1;
486 case WM_MDICREATE:
488 MDICREATESTRUCT32A *cs =
489 (MDICREATESTRUCT32A *)HeapAlloc( SystemHeap, 0, sizeof(*cs) );
490 if (!cs) return -1;
491 *cs = *(MDICREATESTRUCT32A *)*plparam;
492 if (HIWORD(cs->szTitle))
493 cs->szTitle = STRING32_DupUniToAnsi( (LPCWSTR)cs->szTitle );
494 if (HIWORD(cs->szClass))
495 cs->szClass = STRING32_DupUniToAnsi( (LPCWSTR)cs->szClass );
496 *plparam = (LPARAM)cs;
498 return 1;
499 case WM_ASKCBFORMATNAME:
500 case WM_COMPAREITEM:
501 case WM_DELETEITEM:
502 case WM_DEVMODECHANGE:
503 case WM_MDIACTIVATE:
504 case WM_MEASUREITEM:
505 case WM_PAINTCLIPBOARD:
506 case WM_SIZECLIPBOARD:
507 case WM_WININICHANGE:
508 fprintf( stderr, "MapMsg32WTo32A: message %04x needs translation\n",
509 msg );
510 return -1;
511 default: /* No translation needed */
512 return 0;
517 /**********************************************************************
518 * WINPROC_UnmapMsg32WTo32A
520 * Unmap a message that was mapped from Unicode to Ansi.
522 void WINPROC_UnmapMsg32WTo32A( UINT32 msg, WPARAM32 wParam, LPARAM lParam )
524 switch(msg)
526 case WM_GETTEXT:
528 LPARAM *ptr = (LPARAM *)lParam - 1;
529 lstrcpynAtoW( (LPWSTR)*ptr, (LPSTR)(ptr + 1), wParam );
530 HeapFree( SystemHeap, 0, ptr );
532 break;
533 case WM_SETTEXT:
534 free( (void *)lParam );
535 break;
536 case WM_NCCREATE:
537 case WM_CREATE:
539 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
540 if (HIWORD(cs->lpszName)) free( (LPVOID)cs->lpszName );
541 if (HIWORD(cs->lpszClass)) free( (LPVOID)cs->lpszClass );
542 HeapFree( SystemHeap, 0, cs );
544 break;
545 case WM_MDICREATE:
547 MDICREATESTRUCT32A *cs = (MDICREATESTRUCT32A *)lParam;
548 if (HIWORD(cs->szTitle)) free( (LPVOID)cs->szTitle );
549 if (HIWORD(cs->szClass)) free( (LPVOID)cs->szClass );
550 HeapFree( SystemHeap, 0, cs );
552 break;
557 /**********************************************************************
558 * WINPROC_MapMsg16To32A
560 * Map a message from 16- to 32-bit Ansi.
561 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
563 INT32 WINPROC_MapMsg16To32A( UINT16 msg16, WPARAM16 wParam16, UINT32 *pmsg32,
564 WPARAM32 *pwparam32, LPARAM *plparam )
566 *pmsg32 = (UINT32)msg16;
567 *pwparam32 = (WPARAM32)wParam16;
568 switch(msg16)
570 case WM_ACTIVATE:
571 case WM_CHARTOITEM:
572 case WM_COMMAND:
573 case WM_HSCROLL:
574 case WM_VKEYTOITEM:
575 case WM_VSCROLL:
576 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
577 *plparam = (LPARAM)(HWND32)LOWORD(*plparam);
578 return 0;
579 case WM_CTLCOLOR:
580 *pmsg32 = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
581 *pwparam32 = (WPARAM32)(HDC32)wParam16;
582 *plparam = (LPARAM)(HWND32)LOWORD(*plparam);
583 return 0;
584 case WM_DRAWITEM:
586 DRAWITEMSTRUCT16* dis16 = (DRAWITEMSTRUCT16 *)PTR_SEG_TO_LIN(*plparam);
587 DRAWITEMSTRUCT32 *dis = (DRAWITEMSTRUCT32*)HeapAlloc(SystemHeap, 0,
588 sizeof(*dis));
589 if (!dis) return -1;
590 dis->CtlType = dis16->CtlType;
591 dis->CtlID = dis16->CtlID;
592 dis->itemID = dis16->itemID;
593 dis->itemAction = dis16->itemAction;
594 dis->itemState = dis16->itemState;
595 dis->hwndItem = dis16->hwndItem;
596 dis->hDC = dis16->hDC;
597 dis->itemData = dis16->itemData;
598 CONV_RECT16TO32( &dis16->rcItem, &dis->rcItem );
599 *plparam = (LPARAM)dis;
601 return 1;
602 case WM_GETMINMAXINFO:
604 MINMAXINFO32 *mmi = (MINMAXINFO32 *)HeapAlloc( SystemHeap, 0,
605 sizeof(*mmi) + sizeof(LPARAM));
606 if (!mmi) return -1;
607 STRUCT32_MINMAXINFO16to32( (MINMAXINFO16*)PTR_SEG_TO_LIN(*plparam),
608 mmi );
609 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
610 *plparam = (LPARAM)mmi;
612 return 1;
613 case WM_GETTEXT:
614 *plparam = (LPARAM)PTR_SEG_TO_LIN(*plparam);
615 return 0;
616 case WM_MDICREATE:
618 MDICREATESTRUCT16 *cs16 =
619 (MDICREATESTRUCT16 *)PTR_SEG_TO_LIN(*plparam);
620 MDICREATESTRUCT32A *cs =
621 (MDICREATESTRUCT32A *)HeapAlloc( SystemHeap, 0,
622 sizeof(*cs) + sizeof(LPARAM) );
623 if (!cs) return -1;
624 STRUCT32_MDICREATESTRUCT16to32A( cs16, cs );
625 cs->szTitle = (LPCSTR)PTR_SEG_TO_LIN(cs16->szTitle);
626 cs->szClass = (LPCSTR)PTR_SEG_TO_LIN(cs16->szClass);
627 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
628 *plparam = (LPARAM)cs;
630 return 1;
631 case WM_MDISETMENU:
632 *pwparam32 = (WPARAM32)(HMENU32)LOWORD(*plparam);
633 *plparam = (LPARAM)(HMENU32)HIWORD(*plparam);
634 return 0;
635 case WM_MENUCHAR:
636 case WM_MENUSELECT:
637 *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
638 *plparam = (LPARAM)(HMENU32)HIWORD(*plparam);
639 return 0;
640 case WM_NCCALCSIZE:
642 NCCALCSIZE_PARAMS16 *nc16;
643 NCCALCSIZE_PARAMS32 *nc;
645 nc = (NCCALCSIZE_PARAMS32 *)HeapAlloc( SystemHeap, 0,
646 sizeof(*nc) + sizeof(LPARAM) );
647 if (!nc) return -1;
648 nc16 = (NCCALCSIZE_PARAMS16 *)PTR_SEG_TO_LIN(*plparam);
649 CONV_RECT16TO32( &nc16->rgrc[0], &nc->rgrc[0] );
650 if (wParam16)
652 nc->lppos = (WINDOWPOS32 *)HeapAlloc( SystemHeap, 0,
653 sizeof(*nc->lppos) );
654 CONV_RECT16TO32( &nc16->rgrc[1], &nc->rgrc[1] );
655 CONV_RECT16TO32( &nc16->rgrc[2], &nc->rgrc[2] );
656 if (nc->lppos) STRUCT32_WINDOWPOS16to32( (WINDOWPOS16 *)PTR_SEG_TO_LIN(nc16->lppos), nc->lppos );
658 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
659 *plparam = (LPARAM)nc;
661 return 1;
662 case WM_NCCREATE:
663 case WM_CREATE:
665 CREATESTRUCT16 *cs16 = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(*plparam);
666 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)HeapAlloc( SystemHeap, 0,
667 sizeof(*cs) + sizeof(LPARAM) );
668 if (!cs) return -1;
669 STRUCT32_CREATESTRUCT16to32A( cs16, cs );
670 cs->lpszName = (LPCSTR)PTR_SEG_TO_LIN(cs16->lpszName);
671 cs->lpszClass = (LPCSTR)PTR_SEG_TO_LIN(cs16->lpszClass);
672 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
673 *plparam = (LPARAM)cs;
675 return 1;
676 case WM_PARENTNOTIFY:
677 if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
679 *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
680 *plparam = (LPARAM)(HWND32)LOWORD(*plparam);
682 return 0;
683 case WM_SETTEXT:
684 *plparam = (LPARAM)PTR_SEG_TO_LIN(*plparam);
685 return 0;
686 case WM_WINDOWPOSCHANGING:
687 case WM_WINDOWPOSCHANGED:
689 WINDOWPOS32 *wp = (WINDOWPOS32 *)HeapAlloc( SystemHeap, 0,
690 sizeof(*wp) + sizeof(LPARAM) );
691 if (!wp) return -1;
692 STRUCT32_WINDOWPOS16to32( (WINDOWPOS16 *)PTR_SEG_TO_LIN(*plparam),
693 wp );
694 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
695 *plparam = (LPARAM)wp;
697 return 1;
698 case WM_ASKCBFORMATNAME:
699 case WM_COMPAREITEM:
700 case WM_DELETEITEM:
701 case WM_DEVMODECHANGE:
702 case WM_MDIACTIVATE:
703 case WM_MEASUREITEM:
704 case WM_PAINTCLIPBOARD:
705 case WM_SIZECLIPBOARD:
706 case WM_WININICHANGE:
707 fprintf( stderr, "MapMsg16To32A: message %04x needs translation\n",
708 msg16 );
709 return -1;
711 default: /* No translation needed */
712 return 0;
717 /**********************************************************************
718 * WINPROC_UnmapMsg16To32A
720 * Unmap a message that was mapped from 16- to 32-bit Ansi.
722 void WINPROC_UnmapMsg16To32A( UINT32 msg, WPARAM32 wParam, LPARAM lParam )
724 switch(msg)
726 case WM_DRAWITEM:
727 HeapFree( SystemHeap, 0, (LPVOID)lParam );
728 break;
729 case WM_GETMINMAXINFO:
731 MINMAXINFO32 *mmi = (MINMAXINFO32 *)lParam;
732 lParam = *(LPARAM *)(mmi + 1);
733 STRUCT32_MINMAXINFO32to16( mmi,
734 (MINMAXINFO16 *)PTR_SEG_TO_LIN(lParam));
735 HeapFree( SystemHeap, 0, mmi );
737 break;
738 case WM_MDICREATE:
740 MDICREATESTRUCT32A *cs = (MDICREATESTRUCT32A *)lParam;
741 lParam = *(LPARAM *)(cs + 1);
742 STRUCT32_MDICREATESTRUCT32Ato16( cs,
743 (MDICREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam) );
744 HeapFree( SystemHeap, 0, cs );
746 break;
747 case WM_NCCALCSIZE:
749 NCCALCSIZE_PARAMS16 *nc16;
750 NCCALCSIZE_PARAMS32 *nc = (NCCALCSIZE_PARAMS32 *)lParam;
751 lParam = *(LPARAM *)(nc + 1);
752 nc16 = (NCCALCSIZE_PARAMS16 *)PTR_SEG_TO_LIN(lParam);
753 CONV_RECT32TO16( &nc->rgrc[0], &nc16->rgrc[0] );
754 if (wParam)
756 CONV_RECT32TO16( &nc->rgrc[1], &nc16->rgrc[1] );
757 CONV_RECT32TO16( &nc->rgrc[2], &nc16->rgrc[2] );
758 if (nc->lppos)
760 STRUCT32_WINDOWPOS32to16( nc->lppos,
761 (WINDOWPOS16 *)PTR_SEG_TO_LIN(nc16->lppos));
762 HeapFree( SystemHeap, 0, nc->lppos );
765 HeapFree( SystemHeap, 0, nc );
767 break;
768 case WM_NCCREATE:
769 case WM_CREATE:
771 CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
772 lParam = *(LPARAM *)(cs + 1);
773 STRUCT32_CREATESTRUCT32Ato16( cs,
774 (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam) );
775 HeapFree( SystemHeap, 0, cs );
777 break;
778 case WM_WINDOWPOSCHANGING:
779 case WM_WINDOWPOSCHANGED:
781 WINDOWPOS32 *wp = (WINDOWPOS32 *)lParam;
782 lParam = *(LPARAM *)(wp + 1);
783 STRUCT32_WINDOWPOS32to16(wp,(WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam));
784 HeapFree( SystemHeap, 0, wp );
786 break;
791 /**********************************************************************
792 * WINPROC_MapMsg16To32W
794 * Map a message from 16- to 32-bit Unicode.
795 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
797 INT32 WINPROC_MapMsg16To32W( UINT16 msg16, WPARAM16 wParam16, UINT32 *pmsg32,
798 WPARAM32 *pwparam32, LPARAM *plparam )
800 switch(msg16)
802 case WM_GETTEXT:
803 case WM_SETTEXT:
804 *plparam = (LPARAM)PTR_SEG_TO_LIN(*plparam);
805 return WINPROC_MapMsg32ATo32W( *pmsg32, *pwparam32, plparam );
806 case WM_NCCREATE:
807 case WM_CREATE:
809 CREATESTRUCT16 *cs16 = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(*plparam);
810 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)HeapAlloc( SystemHeap, 0,
811 sizeof(*cs) + sizeof(LPARAM) );
812 if (!cs) return -1;
813 STRUCT32_CREATESTRUCT16to32A( cs16, (CREATESTRUCT32A *)cs );
814 cs->lpszName = (LPCWSTR)PTR_SEG_TO_LIN(cs16->lpszName);
815 cs->lpszClass = (LPCWSTR)PTR_SEG_TO_LIN(cs16->lpszClass);
816 if (HIWORD(cs->lpszName))
817 cs->lpszName = STRING32_DupAnsiToUni( (LPCSTR)cs->lpszName );
818 if (HIWORD(cs->lpszClass))
819 cs->lpszClass = STRING32_DupAnsiToUni( (LPCSTR)cs->lpszClass );
820 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
821 *plparam = (LPARAM)cs;
823 return 1;
824 case WM_MDICREATE:
826 MDICREATESTRUCT16 *cs16 =
827 (MDICREATESTRUCT16 *)PTR_SEG_TO_LIN(*plparam);
828 MDICREATESTRUCT32W *cs =
829 (MDICREATESTRUCT32W *)HeapAlloc( SystemHeap, 0,
830 sizeof(*cs) + sizeof(LPARAM) );
831 if (!cs) return -1;
832 STRUCT32_MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCT32A *)cs );
833 cs->szTitle = (LPCWSTR)PTR_SEG_TO_LIN(cs16->szTitle);
834 cs->szClass = (LPCWSTR)PTR_SEG_TO_LIN(cs16->szClass);
835 if (HIWORD(cs->szTitle))
836 cs->szTitle = STRING32_DupAnsiToUni( (LPCSTR)cs->szTitle );
837 if (HIWORD(cs->szClass))
838 cs->szClass = STRING32_DupAnsiToUni( (LPCSTR)cs->szClass );
839 *(LPARAM *)(cs + 1) = *plparam; /* Store the previous lParam */
840 *plparam = (LPARAM)cs;
842 return 1;
843 default: /* No Unicode translation needed */
844 return WINPROC_MapMsg16To32A( msg16, wParam16, pmsg32,
845 pwparam32, plparam );
850 /**********************************************************************
851 * WINPROC_UnmapMsg16To32W
853 * Unmap a message that was mapped from 16- to 32-bit Unicode.
855 void WINPROC_UnmapMsg16To32W( UINT32 msg, WPARAM32 wParam, LPARAM lParam )
857 switch(msg)
859 case WM_GETTEXT:
860 case WM_SETTEXT:
861 WINPROC_UnmapMsg32ATo32W( msg, wParam, lParam );
862 break;
863 case WM_NCCREATE:
864 case WM_CREATE:
866 CREATESTRUCT32W *cs = (CREATESTRUCT32W *)lParam;
867 lParam = *(LPARAM *)(cs + 1);
868 STRUCT32_CREATESTRUCT32Ato16( (CREATESTRUCT32A *)cs,
869 (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam) );
870 if (HIWORD(cs->lpszName)) free( (LPVOID)cs->lpszName );
871 if (HIWORD(cs->lpszClass)) free( (LPVOID)cs->lpszClass );
872 HeapFree( SystemHeap, 0, cs );
874 break;
875 case WM_MDICREATE:
877 MDICREATESTRUCT32W *cs = (MDICREATESTRUCT32W *)lParam;
878 lParam = *(LPARAM *)(cs + 1);
879 STRUCT32_MDICREATESTRUCT32Ato16( (MDICREATESTRUCT32A *)cs,
880 (MDICREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam) );
881 if (HIWORD(cs->szTitle)) free( (LPVOID)cs->szTitle );
882 if (HIWORD(cs->szClass)) free( (LPVOID)cs->szClass );
883 HeapFree( SystemHeap, 0, cs );
885 break;
886 default:
887 WINPROC_UnmapMsg16To32A( msg, wParam, lParam );
888 break;
893 /**********************************************************************
894 * WINPROC_MapMsg32ATo16
896 * Map a message from 32-bit Ansi to 16-bit.
897 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
899 INT32 WINPROC_MapMsg32ATo16( UINT32 msg32, WPARAM32 wParam32, UINT16 *pmsg16,
900 WPARAM16 *pwparam16, LPARAM *plparam )
902 *pmsg16 = (UINT16)msg32;
903 *pwparam16 = (WPARAM16)LOWORD(wParam32);
904 switch(msg32)
906 case BM_GETCHECK32:
907 case BM_SETCHECK32:
908 case BM_GETSTATE32:
909 case BM_SETSTATE32:
910 case BM_SETSTYLE32:
911 *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK32);
912 return 0;
913 case WM_ACTIVATE:
914 case WM_CHARTOITEM:
915 case WM_COMMAND:
916 case WM_VKEYTOITEM:
917 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
918 return 0;
919 case WM_HSCROLL:
920 case WM_VSCROLL:
921 *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
922 return 0;
923 case WM_CTLCOLORMSGBOX:
924 case WM_CTLCOLOREDIT:
925 case WM_CTLCOLORLISTBOX:
926 case WM_CTLCOLORBTN:
927 case WM_CTLCOLORDLG:
928 case WM_CTLCOLORSCROLLBAR:
929 case WM_CTLCOLORSTATIC:
930 *pmsg16 = WM_CTLCOLOR;
931 *plparam = MAKELPARAM( (HWND16)*plparam,
932 (WORD)msg32 - WM_CTLCOLORMSGBOX );
933 return 0;
934 case WM_DRAWITEM:
936 DRAWITEMSTRUCT32 *dis32 = (DRAWITEMSTRUCT32 *)*plparam;
937 DRAWITEMSTRUCT16 *dis = SEGPTR_NEW(DRAWITEMSTRUCT16);
938 if (!dis) return -1;
939 dis->CtlType = (UINT16)dis32->CtlType;
940 dis->CtlID = (UINT16)dis32->CtlID;
941 dis->itemID = (UINT16)dis32->itemID;
942 dis->itemAction = (UINT16)dis32->itemAction;
943 dis->itemState = (UINT16)dis32->itemState;
944 dis->hwndItem = (HWND16)dis32->hwndItem;
945 dis->hDC = (HDC16)dis32->hDC;
946 dis->itemData = dis32->itemData;
947 CONV_RECT32TO16( &dis32->rcItem, &dis->rcItem );
948 *plparam = (LPARAM)SEGPTR_GET(dis);
950 return 1;
951 case WM_GETMINMAXINFO:
953 MINMAXINFO16 *mmi = (MINMAXINFO16 *)SEGPTR_ALLOC( sizeof(*mmi) +
954 sizeof(LPARAM) );
955 if (!mmi) return -1;
956 STRUCT32_MINMAXINFO32to16( (MINMAXINFO32 *)*plparam, mmi );
957 *(LPARAM *)(mmi + 1) = *plparam; /* Store the previous lParam */
958 *plparam = (LPARAM)SEGPTR_GET(mmi);
960 return 1;
961 case WM_GETTEXT:
963 LPSTR str;
964 *pwparam16 = (WPARAM16)MIN( wParam32, 0xff80 ); /* Must be < 64K */
965 if (!(str = SEGPTR_ALLOC(*pwparam16 + sizeof(LPARAM)))) return -1;
966 *((LPARAM *)str)++ = *plparam; /* Store the previous lParam */
967 *plparam = (LPARAM)SEGPTR_GET(str);
969 return 1;
970 case WM_MDICREATE:
972 MDICREATESTRUCT16 *cs;
973 MDICREATESTRUCT32A *cs32 = (MDICREATESTRUCT32A *)*plparam;
974 LPSTR name, cls;
976 if (!(cs = SEGPTR_NEW(MDICREATESTRUCT16))) return -1;
977 STRUCT32_MDICREATESTRUCT32Ato16( cs32, cs );
978 name = SEGPTR_STRDUP( cs32->szTitle );
979 cls = SEGPTR_STRDUP( cs32->szClass );
980 cs->szTitle = SEGPTR_GET(name);
981 cs->szClass = SEGPTR_GET(cls);
982 *plparam = (LPARAM)SEGPTR_GET(cs);
984 return 1;
985 case WM_MDISETMENU:
986 *pwparam16 = TRUE; /* FIXME? */
987 *plparam = MAKELPARAM( (HMENU16)LOWORD(wParam32),
988 (HMENU16)LOWORD(*plparam) );
989 return 0;
990 case WM_MENUCHAR:
991 case WM_MENUSELECT:
992 *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
993 return 0;
994 case WM_NCCALCSIZE:
996 NCCALCSIZE_PARAMS32 *nc32 = (NCCALCSIZE_PARAMS32 *)*plparam;
997 NCCALCSIZE_PARAMS16 *nc = (NCCALCSIZE_PARAMS16 *)SEGPTR_ALLOC( sizeof(*nc) + sizeof(LPARAM) );
998 if (!nc) return -1;
1000 CONV_RECT32TO16( &nc32->rgrc[0], &nc->rgrc[0] );
1001 if (wParam32)
1003 WINDOWPOS16 *wp;
1004 CONV_RECT32TO16( &nc32->rgrc[1], &nc->rgrc[1] );
1005 CONV_RECT32TO16( &nc32->rgrc[2], &nc->rgrc[2] );
1006 if (!(wp = SEGPTR_NEW(WINDOWPOS16)))
1008 SEGPTR_FREE(nc);
1009 return -1;
1011 STRUCT32_WINDOWPOS32to16( nc32->lppos, wp );
1012 nc->lppos = SEGPTR_GET(wp);
1014 *(LPARAM *)(nc + 1) = *plparam; /* Store the previous lParam */
1015 *plparam = (LPARAM)SEGPTR_GET(nc);
1017 return 1;
1018 case WM_NCCREATE:
1019 case WM_CREATE:
1021 CREATESTRUCT16 *cs;
1022 CREATESTRUCT32A *cs32 = (CREATESTRUCT32A *)*plparam;
1023 LPSTR name, cls;
1025 if (!(cs = SEGPTR_NEW(CREATESTRUCT16))) return -1;
1026 STRUCT32_CREATESTRUCT32Ato16( cs32, cs );
1027 name = SEGPTR_STRDUP( cs32->lpszName );
1028 cls = SEGPTR_STRDUP( cs32->lpszClass );
1029 cs->lpszName = SEGPTR_GET(name);
1030 cs->lpszClass = SEGPTR_GET(cls);
1031 *plparam = (LPARAM)SEGPTR_GET(cs);
1033 return 1;
1034 case WM_PARENTNOTIFY:
1035 if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
1036 *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
1037 /* else nothing to do */
1038 return 0;
1039 case WM_SETTEXT:
1041 LPSTR str = SEGPTR_STRDUP( (LPSTR)*plparam );
1042 if (!str) return -1;
1043 *plparam = (LPARAM)SEGPTR_GET(str);
1045 return 1;
1046 case WM_WINDOWPOSCHANGING:
1047 case WM_WINDOWPOSCHANGED:
1049 WINDOWPOS16 *wp = (WINDOWPOS16 *)SEGPTR_ALLOC( sizeof(*wp) +
1050 sizeof(LPARAM) );
1051 if (!wp) return -1;
1052 STRUCT32_WINDOWPOS32to16( (WINDOWPOS32 *)*plparam, wp );
1053 *(LPARAM *)(wp + 1) = *plparam; /* Store the previous lParam */
1054 *plparam = (LPARAM)SEGPTR_GET(wp);
1056 return 1;
1057 case WM_ASKCBFORMATNAME:
1058 case WM_COMPAREITEM:
1059 case WM_DELETEITEM:
1060 case WM_DEVMODECHANGE:
1061 case WM_MDIACTIVATE:
1062 case WM_MEASUREITEM:
1063 case WM_PAINTCLIPBOARD:
1064 case WM_SIZECLIPBOARD:
1065 case WM_WININICHANGE:
1066 fprintf( stderr, "MapMsg32ATo16: message %04x needs translation\n",
1067 msg32 );
1068 return -1;
1070 default: /* No translation needed */
1071 return 0;
1076 /**********************************************************************
1077 * WINPROC_UnmapMsg32ATo16
1079 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
1081 void WINPROC_UnmapMsg32ATo16( UINT16 msg, WPARAM16 wParam, LPARAM lParam )
1083 switch(msg)
1085 case WM_DRAWITEM:
1086 SEGPTR_FREE( PTR_SEG_TO_LIN(lParam) );
1087 break;
1088 case WM_GETMINMAXINFO:
1090 MINMAXINFO16 *mmi = (MINMAXINFO16 *)PTR_SEG_TO_LIN(lParam);
1091 lParam = *(LPARAM *)(mmi + 1);
1092 STRUCT32_MINMAXINFO16to32( mmi, (MINMAXINFO32 *)lParam );
1093 SEGPTR_FREE(mmi);
1095 break;
1096 case WM_GETTEXT:
1098 LPSTR str = (LPSTR)PTR_SEG_TO_LIN(lParam);
1099 lParam = *((LPARAM *)str - 1);
1100 strcpy( (LPSTR)lParam, str );
1101 SEGPTR_FREE( (LPARAM *)str - 1 );
1103 break;
1104 case WM_MDICREATE:
1106 MDICREATESTRUCT16 *cs = (MDICREATESTRUCT16*)PTR_SEG_TO_LIN(lParam);
1107 SEGPTR_FREE( PTR_SEG_TO_LIN(cs->szTitle) );
1108 SEGPTR_FREE( PTR_SEG_TO_LIN(cs->szClass) );
1109 SEGPTR_FREE( cs );
1111 break;
1112 case WM_NCCALCSIZE:
1114 NCCALCSIZE_PARAMS32 *nc32;
1115 NCCALCSIZE_PARAMS16 *nc = (NCCALCSIZE_PARAMS16 *)PTR_SEG_TO_LIN(lParam);
1116 lParam = *(LPARAM *)(nc + 1);
1117 nc32 = (NCCALCSIZE_PARAMS32 *)lParam;
1118 CONV_RECT16TO32( &nc->rgrc[0], &nc32->rgrc[0] );
1119 if (wParam)
1121 CONV_RECT16TO32( &nc->rgrc[1], &nc32->rgrc[1] );
1122 CONV_RECT16TO32( &nc->rgrc[2], &nc32->rgrc[2] );
1123 STRUCT32_WINDOWPOS16to32( (WINDOWPOS16 *)PTR_SEG_TO_LIN(nc->lppos),
1124 nc32->lppos );
1125 SEGPTR_FREE( PTR_SEG_TO_LIN(nc->lppos) );
1127 SEGPTR_FREE(nc);
1129 break;
1130 case WM_NCCREATE:
1131 case WM_CREATE:
1133 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
1134 SEGPTR_FREE( PTR_SEG_TO_LIN(cs->lpszName) );
1135 SEGPTR_FREE( PTR_SEG_TO_LIN(cs->lpszClass) );
1136 SEGPTR_FREE( cs );
1138 break;
1139 case WM_SETTEXT:
1140 SEGPTR_FREE( PTR_SEG_TO_LIN(lParam) );
1141 break;
1142 case WM_WINDOWPOSCHANGING:
1143 case WM_WINDOWPOSCHANGED:
1145 WINDOWPOS16 *wp = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
1146 lParam = *(LPARAM *)(wp + 1);
1147 STRUCT32_WINDOWPOS16to32( wp, (WINDOWPOS32 *)lParam );
1148 SEGPTR_FREE(wp);
1150 break;
1155 /**********************************************************************
1156 * WINPROC_MapMsg32WTo16
1158 * Map a message from 32-bit Unicode to 16-bit.
1159 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1161 INT32 WINPROC_MapMsg32WTo16( UINT32 msg32, WPARAM32 wParam32, UINT16 *pmsg16,
1162 WPARAM16 *pwparam16, LPARAM *plparam )
1164 switch(msg32)
1166 case WM_NCCREATE:
1167 case WM_CREATE:
1169 CREATESTRUCT16 *cs;
1170 CREATESTRUCT32W *cs32 = (CREATESTRUCT32W *)*plparam;
1172 if (!(cs = SEGPTR_NEW(CREATESTRUCT16))) return -1;
1173 STRUCT32_CREATESTRUCT32Ato16( (CREATESTRUCT32A *)cs32, cs );
1174 if (HIWORD(cs32->lpszName))
1176 LPSTR name = SEGPTR_ALLOC( lstrlen32W(cs32->lpszName) + 1 );
1177 STRING32_UniToAnsi( name, cs32->lpszName );
1178 cs->lpszName = SEGPTR_GET(name);
1180 else cs->lpszName = (SEGPTR)cs32->lpszName;
1181 if (HIWORD(cs32->lpszClass))
1183 LPSTR name = SEGPTR_ALLOC( lstrlen32W(cs32->lpszClass) + 1 );
1184 STRING32_UniToAnsi( name, cs32->lpszClass );
1185 cs->lpszClass = SEGPTR_GET(name);
1187 else cs->lpszClass = (SEGPTR)cs32->lpszClass;
1188 *pmsg16 = (UINT16)msg32;
1189 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1190 *plparam = (LPARAM)SEGPTR_GET(cs);
1192 return 1;
1193 case WM_MDICREATE:
1195 MDICREATESTRUCT16 *cs;
1196 MDICREATESTRUCT32W *cs32 = (MDICREATESTRUCT32W *)*plparam;
1198 if (!(cs = SEGPTR_NEW(MDICREATESTRUCT16))) return -1;
1199 STRUCT32_MDICREATESTRUCT32Ato16( (MDICREATESTRUCT32A *)cs32, cs );
1200 if (HIWORD(cs32->szTitle))
1202 LPSTR name = SEGPTR_ALLOC( lstrlen32W(cs32->szTitle) + 1 );
1203 STRING32_UniToAnsi( name, cs32->szTitle );
1204 cs->szTitle = SEGPTR_GET(name);
1206 else cs->szTitle = (SEGPTR)cs32->szTitle;
1207 if (HIWORD(cs32->szClass))
1209 LPSTR name = SEGPTR_ALLOC( lstrlen32W(cs32->szClass) + 1 );
1210 STRING32_UniToAnsi( name, cs32->szClass );
1211 cs->szClass = SEGPTR_GET(name);
1213 else cs->szClass = (SEGPTR)cs32->szClass;
1214 *pmsg16 = (UINT16)msg32;
1215 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1216 *plparam = (LPARAM)SEGPTR_GET(cs);
1218 return 1;
1219 case WM_SETTEXT:
1221 LPSTR str = SEGPTR_ALLOC( lstrlen32W((LPWSTR)*plparam) + 1 );
1222 if (!str) return -1;
1223 STRING32_UniToAnsi( str, (LPWSTR)*plparam );
1224 *pmsg16 = (UINT16)msg32;
1225 *pwparam16 = (WPARAM16)LOWORD(wParam32);
1226 *plparam = (LPARAM)SEGPTR_GET(str);
1228 return 1;
1229 default: /* No Unicode translation needed */
1230 return WINPROC_MapMsg32ATo16( msg32, wParam32, pmsg16,
1231 pwparam16, plparam );
1236 /**********************************************************************
1237 * WINPROC_UnmapMsg32WTo16
1239 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
1241 void WINPROC_UnmapMsg32WTo16( UINT16 msg, WPARAM16 wParam, LPARAM lParam )
1243 switch(msg)
1245 case WM_GETTEXT:
1247 LPSTR str = (LPSTR)PTR_SEG_TO_LIN(lParam);
1248 lParam = *((LPARAM *)str - 1);
1249 STRING32_AnsiToUni( (LPWSTR)lParam, str );
1250 SEGPTR_FREE( (LPARAM *)str - 1 );
1252 break;
1253 default:
1254 WINPROC_UnmapMsg32ATo16( msg, wParam, lParam );
1255 break;
1260 /**********************************************************************
1261 * WINPROC_CallProc32ATo32W
1263 * Call a window procedure, translating args from Ansi to Unicode.
1265 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC32 func, HWND32 hwnd,
1266 UINT32 msg, WPARAM32 wParam,
1267 LPARAM lParam )
1269 LRESULT result;
1271 if (WINPROC_MapMsg32ATo32W( msg, wParam, &lParam ) == -1) return 0;
1272 result = CallWndProc32( func, hwnd, msg, wParam, lParam );
1273 WINPROC_UnmapMsg32ATo32W( msg, wParam, lParam );
1274 return result;
1278 /**********************************************************************
1279 * WINPROC_CallProc32WTo32A
1281 * Call a window procedure, translating args from Unicode to Ansi.
1283 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC32 func, HWND32 hwnd,
1284 UINT32 msg, WPARAM32 wParam,
1285 LPARAM lParam )
1287 LRESULT result;
1289 if (WINPROC_MapMsg32WTo32A( msg, wParam, &lParam ) == -1) return 0;
1290 result = CallWndProc32( func, hwnd, msg, wParam, lParam );
1291 WINPROC_UnmapMsg32WTo32A( msg, wParam, lParam );
1292 return result;
1296 /**********************************************************************
1297 * WINPROC_CallProc16To32A
1299 * Call a 32-bit window procedure, translating the 16-bit args.
1301 LRESULT WINPROC_CallProc16To32A( HWND16 hwnd, UINT16 msg,
1302 WPARAM16 wParam, LPARAM lParam,
1303 WNDPROC32 func )
1305 LRESULT result;
1306 UINT32 msg32;
1307 WPARAM32 wParam32;
1309 if (WINPROC_MapMsg16To32A( msg, wParam, &msg32, &wParam32, &lParam ) == -1)
1310 return 0;
1311 result = CallWndProc32( func, hwnd, msg32, wParam32, lParam );
1312 WINPROC_UnmapMsg16To32A( msg32, wParam32, lParam );
1313 return result;
1317 /**********************************************************************
1318 * WINPROC_CallProc16To32W
1320 * Call a 32-bit window procedure, translating the 16-bit args.
1322 LRESULT WINPROC_CallProc16To32W( HWND16 hwnd, UINT16 msg,
1323 WPARAM16 wParam, LPARAM lParam,
1324 WNDPROC32 func )
1326 LRESULT result;
1327 UINT32 msg32;
1328 WPARAM32 wParam32;
1330 if (WINPROC_MapMsg16To32W( msg, wParam, &msg32, &wParam32, &lParam ) == -1)
1331 return 0;
1332 result = CallWndProc32( func, hwnd, msg32, wParam32, lParam );
1333 WINPROC_UnmapMsg16To32W( msg32, wParam32, lParam );
1334 return result;
1338 /**********************************************************************
1339 * WINPROC_CallProc32ATo16
1341 * Call a 16-bit window procedure, translating the 32-bit args.
1343 static LRESULT WINPROC_CallProc32ATo16( WNDPROC16 func, HWND32 hwnd,
1344 UINT32 msg, WPARAM32 wParam,
1345 LPARAM lParam )
1347 LRESULT result;
1348 UINT16 msg16;
1349 WPARAM16 wParam16;
1350 WND *wndPtr = WIN_FindWndPtr( hwnd );
1351 WORD ds = CURRENT_DS;
1353 if (WINPROC_MapMsg32ATo16( msg, wParam, &msg16, &wParam16, &lParam ) == -1)
1354 return 0;
1355 if (wndPtr) CURRENT_DS = wndPtr->hInstance;
1356 result = CallWndProc16( func, hwnd, msg16, wParam16, lParam );
1357 CURRENT_DS = ds;
1358 WINPROC_UnmapMsg32ATo16( msg16, wParam16, lParam );
1359 return result;
1363 /**********************************************************************
1364 * WINPROC_CallProc32WTo16
1366 * Call a 16-bit window procedure, translating the 32-bit args.
1368 static LRESULT WINPROC_CallProc32WTo16( WNDPROC16 func, HWND32 hwnd,
1369 UINT32 msg, WPARAM32 wParam,
1370 LPARAM lParam )
1372 LRESULT result;
1373 UINT16 msg16;
1374 WPARAM16 wParam16;
1375 WND *wndPtr = WIN_FindWndPtr( hwnd );
1376 WORD ds = CURRENT_DS;
1378 if (WINPROC_MapMsg32WTo16( msg, wParam, &msg16, &wParam16, &lParam ) == -1)
1379 return 0;
1380 if (wndPtr) CURRENT_DS = wndPtr->hInstance;
1381 result = CallWndProc16( func, hwnd, msg16, wParam16, lParam );
1382 CURRENT_DS = ds;
1383 WINPROC_UnmapMsg32WTo16( msg16, wParam16, lParam );
1384 return result;
1388 /**********************************************************************
1389 * CallWindowProc16 (USER.122)
1391 LRESULT CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
1392 WPARAM16 wParam, LPARAM lParam )
1394 LRESULT result;
1395 WND *wndPtr;
1396 WINDOWPROC *proc = WINPROC_GetPtr( func );
1397 WORD ds = CURRENT_DS;
1399 if (!proc)
1401 wndPtr = WIN_FindWndPtr( hwnd );
1402 if (wndPtr) CURRENT_DS = wndPtr->hInstance;
1403 result = CallWndProc16( (FARPROC16)func, hwnd, msg, wParam, lParam );
1404 CURRENT_DS = ds;
1405 return result;
1407 #if testing
1408 wndPtr = WIN_FindWndPtr( hwnd );
1409 if (wndPtr) CURRENT_DS = wndPtr->hInstance;
1410 result = CallWndProc16( WINPROC_GetProc( (HWINDOWPROC)proc, WIN_PROC_16),
1411 hwnd, msg, wParam, lParam );
1412 CURRENT_DS = ds;
1413 return result;
1414 #endif
1416 switch(proc->type)
1418 case WIN_PROC_16:
1419 if (!proc->thunk.t_from32.proc) return 0;
1420 wndPtr = WIN_FindWndPtr( hwnd );
1421 if (wndPtr) CURRENT_DS = wndPtr->hInstance;
1422 #ifndef WINELIB
1423 if ((msg == WM_CREATE) || (msg == WM_NCCREATE))
1425 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
1426 /* Build the CREATESTRUCT on the 16-bit stack. */
1427 /* This is really ugly, but some programs (notably the */
1428 /* "Undocumented Windows" examples) want it that way. */
1429 result = CallWndProcNCCREATE16( proc->thunk.t_from32.proc,
1430 cs->dwExStyle, cs->lpszClass, cs->lpszName, cs->style,
1431 cs->x, cs->y, cs->cx, cs->cy, cs->hwndParent, cs->hMenu,
1432 cs->hInstance, (LONG)cs->lpCreateParams, hwnd, msg, wParam,
1433 MAKELONG( IF1632_Saved16_sp-sizeof(CREATESTRUCT16),
1434 IF1632_Saved16_ss ) );
1435 CURRENT_DS = ds;
1436 return result;
1438 #endif
1439 result = CallWndProc16( proc->thunk.t_from32.proc,
1440 hwnd, msg, wParam, lParam );
1441 CURRENT_DS = ds;
1442 return result;
1444 case WIN_PROC_32A:
1445 if (!proc->thunk.t_from16.proc) return 0;
1446 return WINPROC_CallProc16To32A( hwnd, msg, wParam, lParam,
1447 proc->thunk.t_from16.proc );
1448 case WIN_PROC_32W:
1449 if (!proc->thunk.t_from16.proc) return 0;
1450 return WINPROC_CallProc16To32W( hwnd, msg, wParam, lParam,
1451 proc->thunk.t_from16.proc );
1452 default:
1453 fprintf( stderr, "CallWindowProc16: invalid proc %p\n", proc );
1454 return 0;
1459 /**********************************************************************
1460 * CallWindowProc32A (USER32.17)
1462 LRESULT CallWindowProc32A( WNDPROC32 func, HWND32 hwnd, UINT32 msg,
1463 WPARAM32 wParam, LPARAM lParam )
1465 WINDOWPROC *proc = WINPROC_GetPtr( (WNDPROC16)func );
1467 if (!proc) return CallWndProc32( func, hwnd, msg, wParam, lParam );
1469 #if testing
1470 func = WINPROC_GetProc( (HWINDOWPROC)proc, WIN_PROC_32A );
1471 return CallWndProc32( func, hwnd, msg, wParam, lParam );
1472 #endif
1474 switch(proc->type)
1476 case WIN_PROC_16:
1477 if (!proc->thunk.t_from32.proc) return 0;
1478 return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
1479 hwnd, msg, wParam, lParam );
1480 case WIN_PROC_32A:
1481 if (!proc->thunk.t_from16.proc) return 0;
1482 return CallWndProc32( proc->thunk.t_from16.proc,
1483 hwnd, msg, wParam, lParam );
1484 case WIN_PROC_32W:
1485 if (!proc->thunk.t_from16.proc) return 0;
1486 return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
1487 hwnd, msg, wParam, lParam );
1488 default:
1489 fprintf( stderr, "CallWindowProc32A: invalid proc %p\n", proc );
1490 return 0;
1495 /**********************************************************************
1496 * CallWindowProc32W (USER32.18)
1498 LRESULT CallWindowProc32W( WNDPROC32 func, HWND32 hwnd, UINT32 msg,
1499 WPARAM32 wParam, LPARAM lParam )
1501 WINDOWPROC *proc = WINPROC_GetPtr( (WNDPROC16)func );
1503 if (!proc) return CallWndProc32( func, hwnd, msg, wParam, lParam );
1505 #if testing
1506 func = WINPROC_GetProc( (HWINDOWPROC)proc, WIN_PROC_32W );
1507 return CallWndProc32( func, hwnd, msg, wParam, lParam );
1508 #endif
1510 switch(proc->type)
1512 case WIN_PROC_16:
1513 if (!proc->thunk.t_from32.proc) return 0;
1514 return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
1515 hwnd, msg, wParam, lParam );
1516 case WIN_PROC_32A:
1517 if (!proc->thunk.t_from16.proc) return 0;
1518 return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
1519 hwnd, msg, wParam, lParam );
1520 case WIN_PROC_32W:
1521 if (!proc->thunk.t_from16.proc) return 0;
1522 return CallWndProc32( proc->thunk.t_from16.proc,
1523 hwnd, msg, wParam, lParam );
1524 default:
1525 fprintf( stderr, "CallWindowProc32W: invalid proc %p\n", proc );
1526 return 0;