winproc.c 110 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * Window procedure callbacks
 *
 * Copyright 1995 Martin von Loewis
 * Copyright 1996 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

22
#include "config.h"
23
#include "wine/port.h"
24

25
#include <stdarg.h>
26
#include <string.h>
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
27

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"
Alexandre Julliard's avatar
Alexandre Julliard committed
36
#include "win.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
37
#include "winproc.h"
38
#include "message.h"
39
#include "user_private.h"
40
#include "thread.h"
41
#include "dde.h"
42
#include "winternl.h"
43 44
#include "wine/unicode.h"
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
45

46 47
WINE_DECLARE_DEBUG_CHANNEL(msg);
WINE_DECLARE_DEBUG_CHANNEL(relay);
48
WINE_DEFAULT_DEBUG_CHANNEL(win);
49

50 51
#ifdef __i386__

52
#include "pshpack1.h"
53 54

/* Window procedure 16-to-32-bit thunk */
Alexandre Julliard's avatar
Alexandre Julliard committed
55 56
typedef struct
{
57
    BYTE       popl_eax;             /* popl  %eax (return address) */
Alexandre Julliard's avatar
Alexandre Julliard committed
58
    BYTE       pushl_func;           /* pushl $proc */
59
    WNDPROC    proc;
60 61 62 63
    BYTE       pushl_eax;            /* pushl %eax */
    BYTE       ljmp;                 /* ljmp relay*/
    DWORD      relay_offset;         /* __wine_call_wndproc_32A/W */
    WORD       relay_sel;
Alexandre Julliard's avatar
Alexandre Julliard committed
64
} WINPROC_THUNK_FROM16;
Alexandre Julliard's avatar
Alexandre Julliard committed
65

66
/* Window procedure 32-to-16-bit thunk */
Alexandre Julliard's avatar
Alexandre Julliard committed
67 68
typedef struct
{
Alexandre Julliard's avatar
Alexandre Julliard committed
69
    BYTE       popl_eax;             /* popl  %eax (return address) */
Alexandre Julliard's avatar
Alexandre Julliard committed
70
    BYTE       pushl_func;           /* pushl $proc */
71
    WNDPROC16  proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
72
    BYTE       pushl_eax;            /* pushl %eax */
Alexandre Julliard's avatar
Alexandre Julliard committed
73
    BYTE       jmp;                  /* jmp   relay (relative jump)*/
74
    void     (*relay)();             /* WINPROC_CallProc32ATo16() */
Alexandre Julliard's avatar
Alexandre Julliard committed
75 76
} WINPROC_THUNK_FROM32;

Alexandre Julliard's avatar
Alexandre Julliard committed
77 78 79 80
/* Simple jmp to call 32-bit procedure directly */
typedef struct
{
    BYTE       jmp;                  /* jmp  proc (relative jump) */
81
    WNDPROC    proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
82
} WINPROC_JUMP;
83

84
#include "poppack.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#else /* __i386__ */

typedef struct
{
    WNDPROC    proc;
} WINPROC_THUNK_FROM16;

typedef struct
{
    WNDPROC16  proc;
} WINPROC_THUNK_FROM32;

typedef struct
{
    WNDPROC    proc;
} WINPROC_JUMP;

#endif /* __i386__ */

Alexandre Julliard's avatar
Alexandre Julliard committed
105 106 107 108 109 110 111 112
typedef union
{
    WINPROC_THUNK_FROM16  t_from16;
    WINPROC_THUNK_FROM32  t_from32;
} WINPROC_THUNK;

typedef struct tagWINDOWPROC
{
113 114 115
    WINPROC_THUNK  thunk;    /* Thunk */
    WINPROC_JUMP   jmp;      /* Jump */
    BYTE           type;     /* Function type */
Alexandre Julliard's avatar
Alexandre Julliard committed
116 117
} WINDOWPROC;

118 119
static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
                                               UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
120
                                               LPARAM lParam );
121 122
static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
                                               UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
123
                                               LPARAM lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
124

125
#define MAX_WINPROCS  (0x10000 / sizeof(WINDOWPROC))
Alexandre Julliard's avatar
Alexandre Julliard committed
126

127 128
static WINDOWPROC winproc_array[MAX_WINPROCS];
static UINT winproc_used;
129 130 131 132 133 134 135 136 137

static CRITICAL_SECTION winproc_cs;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &winproc_cs,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { 0, (DWORD)(__FILE__ ": winproc_cs") }
};
static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
Alexandre Julliard's avatar
Alexandre Julliard committed
138

139
static BOOL is_valid_winproc( WINDOWPROC *proc )
140
{
141 142
    /* check alignment */
    if (((BYTE *)proc - (BYTE *)winproc_array) % sizeof(*proc)) return FALSE;
143 144 145
    /* check array limits */
    if (proc < winproc_array || proc >= winproc_array + winproc_used) return FALSE;
    return (proc->type != WIN_PROC_INVALID);
146 147
}

148 149 150
/* find an existing winproc for a given function and type */
/* FIXME: probably should do something more clever than a linear search */
static inline WINDOWPROC *find_winproc( WNDPROC func, WINDOWPROCTYPE type )
151
{
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    unsigned int i;

    if (type == WIN_PROC_16)
    {
        for (i = 0; i < winproc_used; i++)
        {
            if (winproc_array[i].type == type &&
                winproc_array[i].thunk.t_from32.proc == (WNDPROC16)func)
                return &winproc_array[i];
        }
    }
    else
    {
        for (i = 0; i < winproc_used; i++)
        {
            if (winproc_array[i].type == type &&
                winproc_array[i].thunk.t_from16.proc == func)
                return &winproc_array[i];
        }
    }
    return NULL;
173 174
}

175 176
/* initialize a new winproc */
static inline void set_winproc( WINDOWPROC *proc, WNDPROC func, WINDOWPROCTYPE type )
177
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#ifdef __i386__
    static FARPROC16 relay_32A, relay_32W;

    switch(type)
    {
    case WIN_PROC_16:
        proc->thunk.t_from32.popl_eax    = 0x58;   /* popl  %eax */
        proc->thunk.t_from32.pushl_func  = 0x68;   /* pushl $proc */
        proc->thunk.t_from32.proc        = (WNDPROC16)func;
        proc->thunk.t_from32.pushl_eax   = 0x50;   /* pushl %eax */
        proc->thunk.t_from32.jmp         = 0xe9;   /* jmp   relay*/
        proc->thunk.t_from32.relay =  /* relative jump */
            (void(*)())((DWORD)WINPROC_CallProc32ATo16 -
                        (DWORD)(&proc->thunk.t_from32.relay + 1));
        break;
    case WIN_PROC_32A:
        if (!relay_32A) relay_32A = GetProcAddress16( GetModuleHandle16("user"),
                                                      "__wine_call_wndproc_32A" );
        proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
        proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
        proc->thunk.t_from16.proc         = func;
        proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
        proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
        proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32A);
        proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32A);
        proc->jmp.jmp  = 0xe9;
        /* Fixup relative jump */
        proc->jmp.proc = (WNDPROC)((DWORD)func - (DWORD)(&proc->jmp.proc + 1));
        break;
    case WIN_PROC_32W:
        if (!relay_32W) relay_32W = GetProcAddress16( GetModuleHandle16("user"),
                                                      "__wine_call_wndproc_32W" );
        proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
        proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
        proc->thunk.t_from16.proc         = func;
        proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
        proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
        proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32W);
        proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32W);
        proc->jmp.jmp  = 0xe9;
        /* Fixup relative jump */
        proc->jmp.proc = (WNDPROC)((char *)func - (char *)(&proc->jmp.proc + 1));
        break;
    default:
        /* Should not happen */
        break;
    }
#else /* __i386__ */
    switch(type)
    {
    case WIN_PROC_16:
        proc->thunk.t_from32.proc = (WNDPROC16)func;
        break;
    case WIN_PROC_32A:
    case WIN_PROC_32W:
        proc->thunk.t_from16.proc = func;
        break;
    default:
        /* Should not happen */
        break;
    }
#endif /* __i386__ */

    proc->type = type;
242 243 244
}

static WORD get_winproc_selector(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
245
{
246 247 248 249
    static LONG winproc_selector;
    WORD ret;

    if (!(ret = winproc_selector))
Alexandre Julliard's avatar
Alexandre Julliard committed
250
    {
251 252 253 254 255 256 257 258
        LDT_ENTRY entry;
        WORD sel = wine_ldt_alloc_entries(1);
        wine_ldt_set_base( &entry, winproc_array );
        wine_ldt_set_limit( &entry, sizeof(winproc_array) - 1 );
        wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
        wine_ldt_set_entry( sel, &entry );
        if (!(ret = InterlockedCompareExchange( &winproc_selector, sel, 0 ))) ret = sel;
        else wine_ldt_free_entries( sel, 1 );  /* somebody beat us to it */
Alexandre Julliard's avatar
Alexandre Julliard committed
259
    }
260
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
261
}
Alexandre Julliard's avatar
Alexandre Julliard committed
262

Alexandre Julliard's avatar
Alexandre Julliard committed
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
#ifdef __i386__
/* Some window procedures modify register they shouldn't, or are not
 * properly declared stdcall; so we need a small assembly wrapper to
 * call them. */
extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
                                WPARAM wParam, LPARAM lParam );
__ASM_GLOBAL_FUNC( WINPROC_wrapper,
                   "pushl %ebp\n\t"
                   "movl %esp,%ebp\n\t"
                   "pushl %edi\n\t"
                   "pushl %esi\n\t"
                   "pushl %ebx\n\t"
                   "pushl 24(%ebp)\n\t"
                   "pushl 20(%ebp)\n\t"
                   "pushl 16(%ebp)\n\t"
                   "pushl 12(%ebp)\n\t"
                   "movl 8(%ebp),%eax\n\t"
                   "call *%eax\n\t"
                   "leal -12(%ebp),%esp\n\t"
                   "popl %ebx\n\t"
                   "popl %esi\n\t"
                   "popl %edi\n\t"
                   "leave\n\t"
                   "ret" );
#else
static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
                                       WPARAM wParam, LPARAM lParam )
{
    return proc( hwnd, msg, wParam, lParam );
}
#endif  /* __i386__ */

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
{
    to->ptReserved.x     = from->ptReserved.x;
    to->ptReserved.y     = from->ptReserved.y;
    to->ptMaxSize.x      = from->ptMaxSize.x;
    to->ptMaxSize.y      = from->ptMaxSize.y;
    to->ptMaxPosition.x  = from->ptMaxPosition.x;
    to->ptMaxPosition.y  = from->ptMaxPosition.y;
    to->ptMinTrackSize.x = from->ptMinTrackSize.x;
    to->ptMinTrackSize.y = from->ptMinTrackSize.y;
    to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
    to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
}

static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
{
    to->ptReserved.x     = from->ptReserved.x;
    to->ptReserved.y     = from->ptReserved.y;
    to->ptMaxSize.x      = from->ptMaxSize.x;
    to->ptMaxSize.y      = from->ptMaxSize.y;
    to->ptMaxPosition.x  = from->ptMaxPosition.x;
    to->ptMaxPosition.y  = from->ptMaxPosition.y;
    to->ptMinTrackSize.x = from->ptMinTrackSize.x;
    to->ptMinTrackSize.y = from->ptMinTrackSize.y;
    to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
    to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
}

static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
{
    to->hwnd            = HWND_16(from->hwnd);
    to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
    to->x               = from->x;
    to->y               = from->y;
    to->cx              = from->cx;
    to->cy              = from->cy;
    to->flags           = from->flags;
}

static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
{
    to->hwnd            = WIN_Handle32(from->hwnd);
    to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
                           HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
    to->x               = from->x;
    to->y               = from->y;
    to->cx              = from->cx;
    to->cy              = from->cy;
    to->flags           = from->flags;
}

/* The strings are not copied */
static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
{
    to->lpCreateParams = (SEGPTR)from->lpCreateParams;
    to->hInstance      = HINSTANCE_16(from->hInstance);
    to->hMenu          = HMENU_16(from->hMenu);
    to->hwndParent     = HWND_16(from->hwndParent);
    to->cy             = from->cy;
    to->cx             = from->cx;
    to->y              = from->y;
    to->x              = from->x;
    to->style          = from->style;
    to->dwExStyle      = from->dwExStyle;
}

static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )

{
    to->lpCreateParams = (LPVOID)from->lpCreateParams;
    to->hInstance      = HINSTANCE_32(from->hInstance);
    to->hMenu          = HMENU_32(from->hMenu);
    to->hwndParent     = WIN_Handle32(from->hwndParent);
    to->cy             = from->cy;
    to->cx             = from->cx;
    to->y              = from->y;
    to->x              = from->x;
    to->style          = from->style;
    to->dwExStyle      = from->dwExStyle;
}

/* The strings are not copied */
static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
{
    to->hOwner = HINSTANCE_16(from->hOwner);
    to->x      = from->x;
    to->y      = from->y;
    to->cx     = from->cx;
    to->cy     = from->cy;
    to->style  = from->style;
    to->lParam = from->lParam;
}

static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
{
    to->hOwner = HINSTANCE_32(from->hOwner);
    to->x      = from->x;
    to->y      = from->y;
    to->cx     = from->cx;
    to->cy     = from->cy;
    to->style  = from->style;
    to->lParam = from->lParam;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
401 402 403 404 405
/**********************************************************************
 *	     WINPROC_CallWndProc32
 *
 * Call a 32-bit WndProc.
 */
406 407
static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
                                      WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
408
{
409 410
    LRESULT retvalue;
    int iWndsLocks;
411

412
    hwnd = WIN_GetFullHandle( hwnd );
413
    if (TRACE_ON(relay))
414
        DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
415
                 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
416 417 418
    /* To avoid any deadlocks, all the locks on the windows structures
       must be suspended before the control is passed to the application */
    iWndsLocks = WIN_SuspendWndsLock();
419
    retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
420
    WIN_RestoreWndsLock(iWndsLocks);
421 422

    if (TRACE_ON(relay))
423
        DPRINTF( "%04lx:Ret  window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
424
                 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
425
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
426 427
}

428 429 430 431 432 433 434 435 436 437 438
/***********************************************************************
 *           WINPROC_CallWndProc16
 *
 * Call a 16-bit window procedure
 */
static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
                                             UINT16 msg, WPARAM16 wParam,
                                             LPARAM lParam )
{
    CONTEXT86 context;
    LRESULT ret;
439
    WORD args[5];
440 441 442 443 444 445
    DWORD offset = 0;
    TEB *teb = NtCurrentTeb();
    int iWndsLocks;

    /* Window procedures want ax = hInstance, ds = es = ss */

446
    memset(&context, 0, sizeof(context));
447
    context.SegDs = context.SegEs = SELECTOROF(teb->cur_stack);
448 449
    context.SegFs = wine_get_fs();
    context.SegGs = wine_get_gs();
450
    if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
451 452 453
    context.SegCs = SELECTOROF(proc);
    context.Eip   = OFFSETOF(proc);
    context.Ebp   = OFFSETOF(teb->cur_stack)
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
                        + (WORD)&((STACK16FRAME*)0)->bp;

    if (lParam)
    {
        /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
           work if structures passed in lParam are placed in the stack/data
           segment. Programmers easily make the mistake of converting lParam
           to a near rather than a far pointer, since Windows apparently
           allows this. We copy the structures to the 16 bit stack; this is
           ugly but makes these programs work. */
        switch (msg)
        {
          case WM_CREATE:
          case WM_NCCREATE:
            offset = sizeof(CREATESTRUCT16); break;
          case WM_DRAWITEM:
            offset = sizeof(DRAWITEMSTRUCT16); break;
          case WM_COMPAREITEM:
            offset = sizeof(COMPAREITEMSTRUCT16); break;
        }
        if (offset)
        {
476
            void *s = MapSL(lParam);
477
            lParam = stack16_push( offset );
478
            memcpy( MapSL(lParam), s, offset );
479 480 481 482 483 484
        }
    }

    iWndsLocks = WIN_SuspendWndsLock();

    args[4] = hwnd;
485 486 487 488 489
    args[3] = msg;
    args[2] = wParam;
    args[1] = HIWORD(lParam);
    args[0] = LOWORD(lParam);
    WOWCallback16Ex( 0, WCB16_REGS, sizeof(args), args, (DWORD *)&context );
490
    ret = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
491

492 493 494 495 496 497 498
    if (offset) stack16_pop( offset );

    WIN_RestoreWndsLock(iWndsLocks);

    return ret;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
499

Alexandre Julliard's avatar
Alexandre Julliard committed
500
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
501
 *	     WINPROC_GetPtr
Alexandre Julliard's avatar
Alexandre Julliard committed
502
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
503
 * Return a pointer to the win proc.
Alexandre Julliard's avatar
Alexandre Julliard committed
504
 */
505
static WINDOWPROC *WINPROC_GetPtr( WNDPROC handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
506
{
Alexandre Julliard's avatar
Alexandre Julliard committed
507
    BYTE *ptr;
Alexandre Julliard's avatar
Alexandre Julliard committed
508
    WINDOWPROC *proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
509

510 511 512
    /* ptr cannot be < 64K */
    if (!HIWORD(handle)) return NULL;

Alexandre Julliard's avatar
Alexandre Julliard committed
513 514
    /* Check for a linear pointer */

515 516
    ptr = (BYTE *)handle;
    /* First check if it is the jmp address */
517
    proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,jmp));
518 519
    if (is_valid_winproc(proc)) return proc;

520
    /* Now it must be the thunk address */
521
    proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
522
    if (is_valid_winproc(proc)) return proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
523 524 525

    /* Check for a segmented pointer */

526 527 528 529 530 531 532
    if (HIWORD(handle) == get_winproc_selector())
    {
        ptr = (BYTE *)winproc_array + LOWORD(handle);
        /* It must be the thunk address */
        proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
        if (is_valid_winproc(proc)) return proc;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
533
    return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
534 535 536 537
}


/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
538
 *	     WINPROC_GetProc
Alexandre Julliard's avatar
Alexandre Julliard committed
539
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
540
 * Get a window procedure pointer that can be passed to the Windows program.
Alexandre Julliard's avatar
Alexandre Julliard committed
541
 */
542
WNDPROC16 WINPROC_GetProc( WNDPROC proc, WINDOWPROCTYPE type )
Alexandre Julliard's avatar
Alexandre Julliard committed
543
{
544 545
    WINDOWPROC *ptr = (WINDOWPROC *)proc;

Alexandre Julliard's avatar
Alexandre Julliard committed
546
    if (!proc) return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
547
    if (type == WIN_PROC_16)  /* We want a 16:16 address */
Alexandre Julliard's avatar
Alexandre Julliard committed
548
    {
549 550
        if (ptr->type == WIN_PROC_16)
            return ptr->thunk.t_from32.proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
551
        else
552 553
            return (WNDPROC16)MAKESEGPTR( get_winproc_selector(),
                                          (char *)&ptr->thunk - (char *)winproc_array );
Alexandre Julliard's avatar
Alexandre Julliard committed
554 555 556
    }
    else  /* We want a 32-bit address */
    {
557 558 559
        if (ptr->type == WIN_PROC_16)
            return (WNDPROC16)&ptr->thunk;
        else if (type != ptr->type)
Alexandre Julliard's avatar
Alexandre Julliard committed
560
            /* Have to return the jmp address if types don't match */
561
            return (WNDPROC16)&ptr->jmp;
Alexandre Julliard's avatar
Alexandre Julliard committed
562
        else
Alexandre Julliard's avatar
Alexandre Julliard committed
563
            /* Some Win16 programs want to get back the proc they set */
564
            return (WNDPROC16)ptr->thunk.t_from16.proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
565
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
566 567 568 569
}


/**********************************************************************
570
 *	     WINPROC_AllocProc
Alexandre Julliard's avatar
Alexandre Julliard committed
571
 *
572
 * Allocate a window procedure for a window or class.
Alexandre Julliard's avatar
Alexandre Julliard committed
573
 *
574 575 576
 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
 * lot of windows, it will usually only have a limited number of window procedures, so the
 * array won't grow too large, and this way we avoid the need to track allocations per window.
Alexandre Julliard's avatar
Alexandre Julliard committed
577
 */
578
WNDPROC WINPROC_AllocProc( WNDPROC func, WINDOWPROCTYPE type )
Alexandre Julliard's avatar
Alexandre Julliard committed
579
{
580
    WINDOWPROC *proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
581

582
    if (!func) return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
583

584 585 586 587
    EnterCriticalSection( &winproc_cs );

    /* check if the function is already a win proc */
    if (!(proc = WINPROC_GetPtr( func )))
Alexandre Julliard's avatar
Alexandre Julliard committed
588
    {
589 590
        /* then check if we already have a winproc for that function */
        if (!(proc = find_winproc( func, type )))
Alexandre Julliard's avatar
Alexandre Julliard committed
591
        {
592 593 594
            if (winproc_used >= MAX_WINPROCS)
                FIXME( "too many winprocs, cannot allocate one for %p/%d\n", func, type );
            else
Alexandre Julliard's avatar
Alexandre Julliard committed
595
            {
596 597 598 599
                proc = &winproc_array[winproc_used++];
                set_winproc( proc, func, type );
                TRACE( "allocated %p for %p/%d (%d/%d used)\n",
                       proc, func, type, winproc_used, MAX_WINPROCS );
Alexandre Julliard's avatar
Alexandre Julliard committed
600
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
601
        }
602
        else TRACE( "reusing %p for %p/%d\n", proc, func, type );
Alexandre Julliard's avatar
Alexandre Julliard committed
603 604
    }

605 606
    LeaveCriticalSection( &winproc_cs );
    return (WNDPROC)proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
607 608 609 610
}


/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
611
 *	     WINPROC_GetProcType
Alexandre Julliard's avatar
Alexandre Julliard committed
612
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
613
 * Return the window procedure type.
Alexandre Julliard's avatar
Alexandre Julliard committed
614
 */
615
WINDOWPROCTYPE WINPROC_GetProcType( WNDPROC proc )
Alexandre Julliard's avatar
Alexandre Julliard committed
616
{
617
    if (!proc) return WIN_PROC_INVALID;
Alexandre Julliard's avatar
Alexandre Julliard committed
618
    return ((WINDOWPROC *)proc)->type;
Alexandre Julliard's avatar
Alexandre Julliard committed
619
}
620 621 622 623 624
/**********************************************************************
 *	     WINPROC_TestCBForStr
 *
 * Return TRUE if the lparam is a string
 */
625
inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
626
{
627 628
    DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
    return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
629 630 631 632 633 634
}
/**********************************************************************
 *	     WINPROC_TestLBForStr
 *
 * Return TRUE if the lparam is a string
 */
635
inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
636
{
637 638
    DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
    return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
639

640
}
Alexandre Julliard's avatar
Alexandre Julliard committed
641
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
642
 *	     WINPROC_MapMsg32ATo32W
Alexandre Julliard's avatar
Alexandre Julliard committed
643
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
644 645
 * Map a message from Ansi to Unicode.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
Juergen Schmied's avatar
Juergen Schmied committed
646 647 648
 *
 * FIXME:
 *  WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
649
 *  the first four bytes are the handle of the icon
Juergen Schmied's avatar
Juergen Schmied committed
650
 *  when the WM_SETTEXT message has been used to set the icon
Alexandre Julliard's avatar
Alexandre Julliard committed
651
 */
652
INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
Juergen Schmied's avatar
Juergen Schmied committed
653 654
{
    switch(msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
655 656
    {
    case WM_GETTEXT:
657
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
658
        {
659
            LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
660
                                     *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
661 662 663 664 665
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
        }
        return 1;
666
    /* lparam is string (0-terminated) */
Alexandre Julliard's avatar
Alexandre Julliard committed
667
    case WM_SETTEXT:
668
    case WM_WININICHANGE:
669
    case WM_DEVMODECHANGE:
670 671 672 673
    case CB_DIR:
    case LB_DIR:
    case LB_ADDFILE:
    case EM_REPLACESEL:
674 675 676 677 678 679 680
        {
            DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
            WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
            len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
            *plparam = (LPARAM)buf;
            return (*plparam ? 1 : -1);
        }
681 682 683 684
    case WM_GETTEXTLENGTH:
    case CB_GETLBTEXTLEN:
    case LB_GETTEXTLEN:
        return 1;  /* need to map result */
Alexandre Julliard's avatar
Alexandre Julliard committed
685 686 687
    case WM_NCCREATE:
    case WM_CREATE:
        {
688
            UNICODE_STRING usBuffer;
689
	    struct s
690 691 692 693 694
	    { CREATESTRUCTW cs;		/* new structure */
	      LPCWSTR lpszName;		/* allocated Name */
	      LPCWSTR lpszClass;	/* allocated Class */
	    };

695
	    struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
696 697 698
            if (!xs) return -1;
            xs->cs = *(CREATESTRUCTW *)*plparam;
            if (HIWORD(xs->cs.lpszName))
699 700 701 702
            {
                RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
                xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
            }
703
            if (HIWORD(xs->cs.lpszClass))
704 705 706 707
            {
                RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
                xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
            }
708

709
            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
            {
                MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
                                                                         sizeof(*mdi_cs));
                *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
                if (HIWORD(mdi_cs->szTitle))
                {
                    RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
                    mdi_cs->szTitle = usBuffer.Buffer;
                }
                if (HIWORD(mdi_cs->szClass))
                {
                    RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
                    mdi_cs->szClass = usBuffer.Buffer;
                }
                xs->cs.lpCreateParams = mdi_cs;
            }

727
            *plparam = (LPARAM)xs;
Alexandre Julliard's avatar
Alexandre Julliard committed
728 729
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
730 731
    case WM_MDICREATE:
        {
732
            MDICREATESTRUCTW *cs =
733
                (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
Alexandre Julliard's avatar
Alexandre Julliard committed
734
            if (!cs) return -1;
735
            *cs = *(MDICREATESTRUCTW *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
736
            if (HIWORD(cs->szClass))
737 738 739 740 741
            {
                UNICODE_STRING usBuffer;
                RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
                cs->szClass = usBuffer.Buffer;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
742
            if (HIWORD(cs->szTitle))
743 744 745 746 747
            {
                UNICODE_STRING usBuffer;
                RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
                cs->szTitle = usBuffer.Buffer;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
748 749 750
            *plparam = (LPARAM)cs;
        }
        return 1;
751 752

/* Listbox */
753 754
    case LB_ADDSTRING:
    case LB_INSERTSTRING:
755 756 757
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_SELECTSTRING:
758
        if(!*plparam) return 0;
759
	if ( WINPROC_TestLBForStr( hwnd ))
760 761 762 763 764
        {
            UNICODE_STRING usBuffer;
            RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
            *plparam = (LPARAM)usBuffer.Buffer;
        }
765 766
        return (*plparam ? 1 : -1);

767
    case LB_GETTEXT:		    /* FIXME: fixed sized buffer */
768
        { if ( WINPROC_TestLBForStr( hwnd ))
769
	  { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
770 771 772 773 774 775 776 777
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
	  }
        }
        return 1;

/* Combobox */
778 779
    case CB_ADDSTRING:
    case CB_INSERTSTRING:
780 781 782
    case CB_FINDSTRINGEXACT:
    case CB_FINDSTRING:
    case CB_SELECTSTRING:
783
        if(!*plparam) return 0;
784
	if ( WINPROC_TestCBForStr( hwnd ))
785 786 787 788 789
        {
            UNICODE_STRING usBuffer;
            RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
            *plparam = (LPARAM)usBuffer.Buffer;
        }
790 791
        return (*plparam ? 1 : -1);

792
    case CB_GETLBTEXT:    /* FIXME: fixed sized buffer */
793
        { if ( WINPROC_TestCBForStr( hwnd ))
794
          { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
795 796 797 798 799 800 801 802
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
	  }
        }
        return 1;

/* Multiline edit */
803
    case EM_GETLINE:
804
        { WORD len = (WORD)*plparam;
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
805
	  LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
806 807
          if (!ptr) return -1;
          *ptr++ = *plparam;  /* Store previous lParam */
808
	  *((WORD *) ptr) = len;   /* Store the length */
809 810 811 812
          *plparam = (LPARAM)ptr;
	}
        return 1;

813 814
    case WM_CHARTOITEM:
    case WM_MENUCHAR:
815 816 817 818
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
819
    case EM_SETPASSWORDCHAR:
820
        {
821
            BYTE ch = LOWORD(*pwparam);
822 823
            WCHAR wch;
            MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
824
            *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
825 826 827
        }
        return 0;

828 829 830 831 832 833
    case WM_IME_CHAR:
        {
            BYTE ch[2];
            WCHAR wch;
            ch[0] = (*pwparam >> 8);
            ch[1] = *pwparam & 0xff;
834 835 836 837
            if (ch[0])
                MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
            else
                MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
838 839 840 841
            *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
        }
        return 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
842 843
    case WM_PAINTCLIPBOARD:
    case WM_SIZECLIPBOARD:
844
        FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
Alexandre Julliard's avatar
Alexandre Julliard committed
845
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
846 847 848
    default:  /* No translation needed */
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
849 850 851
}


Alexandre Julliard's avatar
Alexandre Julliard committed
852
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
853
 *	     WINPROC_UnmapMsg32ATo32W
Alexandre Julliard's avatar
Alexandre Julliard committed
854
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
855
 * Unmap a message that was mapped from Ansi to Unicode.
Alexandre Julliard's avatar
Alexandre Julliard committed
856
 */
857 858
LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
                                  LRESULT result )
Juergen Schmied's avatar
Juergen Schmied committed
859 860
{
    switch(msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
861 862
    {
    case WM_GETTEXT:
863
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
864
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
865
            LPARAM *ptr = (LPARAM *)lParam - 1;
866 867 868 869
            if (!wParam) result = 0;
            else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
                                                    (LPSTR)*ptr, wParam, NULL, NULL )))
            {
870
                ((LPSTR)*ptr)[wParam-1] = 0;
871 872 873
                result = wParam - 1;
            }
            else result--;  /* do not count terminating null */
874
            HeapFree( GetProcessHeap(), 0, ptr );
875 876
        }
        break;
877 878 879 880 881
    case WM_GETTEXTLENGTH:
    case CB_GETLBTEXTLEN:
    case LB_GETTEXTLEN:
        /* there may be one DBCS char for each Unicode char */
        return result * 2;
Alexandre Julliard's avatar
Alexandre Julliard committed
882 883
    case WM_NCCREATE:
    case WM_CREATE:
Alexandre Julliard's avatar
Alexandre Julliard committed
884
        {
885
	    struct s
886 887 888 889 890
	    { CREATESTRUCTW cs;		/* new structure */
	      LPWSTR lpszName;		/* allocated Name */
	      LPWSTR lpszClass;		/* allocated Class */
	    };
            struct s *xs = (struct s *)lParam;
891 892
            HeapFree( GetProcessHeap(), 0, xs->lpszName );
            HeapFree( GetProcessHeap(), 0, xs->lpszClass );
893

894
            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
895 896 897 898 899 900 901 902
            {
                MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
                if (HIWORD(mdi_cs->szTitle))
                    HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
                if (HIWORD(mdi_cs->szClass))
                    HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
                HeapFree(GetProcessHeap(), 0, mdi_cs);
            }
903
            HeapFree( GetProcessHeap(), 0, xs );
Alexandre Julliard's avatar
Alexandre Julliard committed
904
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
905
        break;
906

Alexandre Julliard's avatar
Alexandre Julliard committed
907 908
    case WM_MDICREATE:
        {
909
            MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
910
            if (HIWORD(cs->szTitle))
911
                HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
Alexandre Julliard's avatar
Alexandre Julliard committed
912
            if (HIWORD(cs->szClass))
913 914
                HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
915 916
        }
        break;
917 918

    case WM_SETTEXT:
919
    case WM_WININICHANGE:
920
    case WM_DEVMODECHANGE:
921 922 923 924
    case CB_DIR:
    case LB_DIR:
    case LB_ADDFILE:
    case EM_REPLACESEL:
925
        HeapFree( GetProcessHeap(), 0, (void *)lParam );
926 927 928
        break;

/* Listbox */
929 930
    case LB_ADDSTRING:
    case LB_INSERTSTRING:
931 932 933
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_SELECTSTRING:
934
	if ( WINPROC_TestLBForStr( hwnd ))
935
          HeapFree( GetProcessHeap(), 0, (void *)lParam );
936 937
        break;

938
    case LB_GETTEXT:
939 940 941 942 943
        if ( WINPROC_TestLBForStr( hwnd ))
        {
            LPARAM *ptr = (LPARAM *)lParam - 1;
            result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
                                          (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
944
            HeapFree( GetProcessHeap(), 0, ptr );
945 946 947 948
        }
        break;

/* Combobox */
949 950
    case CB_ADDSTRING:
    case CB_INSERTSTRING:
951 952 953
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_SELECTSTRING:
954
	if ( WINPROC_TestCBForStr( hwnd ))
955
          HeapFree( GetProcessHeap(), 0, (void *)lParam );
956 957
        break;

958
    case CB_GETLBTEXT:
959 960 961 962 963
        if ( WINPROC_TestCBForStr( hwnd ))
        {
            LPARAM *ptr = (LPARAM *)lParam - 1;
            result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
                                          (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
964
            HeapFree( GetProcessHeap(), 0, ptr );
965 966 967 968
        }
        break;

/* Multiline edit */
969
    case EM_GETLINE:
970 971 972 973 974 975 976
        {
            LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lParam */
            WORD len = *(WORD *) lParam;
            result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
                                          (LPSTR)*ptr, len, NULL, NULL );
            if (result < len) ((LPSTR)*ptr)[result] = 0;
            HeapFree( GetProcessHeap(), 0, ptr );
977 978
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
979
    }
980
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
981
}
Alexandre Julliard's avatar
Alexandre Julliard committed
982

Alexandre Julliard's avatar
Alexandre Julliard committed
983 984 985 986 987 988 989

/**********************************************************************
 *	     WINPROC_MapMsg32WTo32A
 *
 * Map a message from Unicode to Ansi.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
 */
990
static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
991 992
{
    switch(msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
993 994
    {
    case WM_GETTEXT:
995
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
996
        {
997
            LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
998
                                               *pwparam + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
999 1000 1001 1002 1003
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
        }
        return 1;
1004

1005
    case WM_SETTEXT:
1006
    case WM_WININICHANGE:
1007
    case WM_DEVMODECHANGE:
1008 1009 1010 1011
    case CB_DIR:
    case LB_DIR:
    case LB_ADDFILE:
    case EM_REPLACESEL:
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        if (*plparam)
        {
            LPCWSTR str = (LPCWSTR)*plparam;
            int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, 0, 0);
            *plparam = (LPARAM)HeapAlloc(GetProcessHeap(), 0, len);
            if (!*plparam) return -1;
            WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)*plparam, len, 0, 0);
            return 1;
        }
        return 0;
1022

Alexandre Julliard's avatar
Alexandre Julliard committed
1023 1024
    case WM_MDICREATE:
        {
1025
            MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1026
            if (!cs) return -1;
1027
            *cs = *(MDICREATESTRUCTA *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1028
            if (HIWORD(cs->szTitle))
1029 1030 1031 1032 1033 1034
            {
                int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, NULL, 0, 0, 0);
                LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
                if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, buf, len, 0, 0);
                cs->szTitle = buf;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1035
            if (HIWORD(cs->szClass))
1036 1037 1038 1039 1040 1041
            {
                int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, NULL, 0, 0, 0);
                LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
                if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, buf, len, 0, 0);
                cs->szClass = buf;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1042 1043 1044
            *plparam = (LPARAM)cs;
        }
        return 1;
1045 1046

/* Listbox */
1047 1048
    case LB_ADDSTRING:
    case LB_INSERTSTRING:
1049 1050 1051
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_SELECTSTRING:
1052
        if(!*plparam) return 0;
1053 1054 1055 1056 1057 1058 1059
        if ( WINPROC_TestLBForStr( hwnd ))
        {
            int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
            LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
            if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
            *plparam = (LPARAM)buf;
        }
1060 1061
        return (*plparam ? 1 : -1);

1062
    case LB_GETTEXT:			/* FIXME: fixed sized buffer */
1063
        { if ( WINPROC_TestLBForStr( hwnd ))
1064
	  { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1065 1066 1067 1068 1069 1070 1071 1072
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
	  }
        }
        return 1;

/* Combobox */
1073 1074
    case CB_ADDSTRING:
    case CB_INSERTSTRING:
1075 1076 1077
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_SELECTSTRING:
1078
        if(!*plparam) return 0;
1079 1080 1081 1082 1083 1084 1085
        if ( WINPROC_TestCBForStr( hwnd ))
        {
            int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
            LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
            if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
            *plparam = (LPARAM)buf;
        }
1086 1087
        return (*plparam ? 1 : -1);

1088
    case CB_GETLBTEXT:		/* FIXME: fixed sized buffer */
1089
        { if ( WINPROC_TestCBForStr( hwnd ))
1090
	  { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1091 1092 1093 1094 1095 1096 1097 1098
            if (!ptr) return -1;
            *ptr++ = *plparam;  /* Store previous lParam */
            *plparam = (LPARAM)ptr;
	  }
        }
        return 1;

/* Multiline edit */
1099
    case EM_GETLINE:
1100
        { WORD len = (WORD)*plparam;
Dimitrie O. Paun's avatar
Dimitrie O. Paun committed
1101
	  LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1102 1103
          if (!ptr) return -1;
          *ptr++ = *plparam;  /* Store previous lParam */
1104
	  *((WORD *) ptr) = len;   /* Store the length */
1105 1106 1107 1108
          *plparam = (LPARAM)ptr;
	}
        return 1;

1109 1110
    case WM_CHARTOITEM:
    case WM_MENUCHAR:
1111 1112 1113 1114
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
1115
    case EM_SETPASSWORDCHAR:
1116
        {
1117
            WCHAR wch = LOWORD(*pwparam);
1118
            BYTE ch;
1119
            WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
1120
            *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1121 1122 1123
        }
        return 0;

1124 1125 1126 1127 1128
    case WM_IME_CHAR:
        {
            WCHAR wch = LOWORD(*pwparam);
            BYTE ch[2];

1129 1130 1131 1132
            if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
                *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
            else
                *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1133 1134 1135
        }
        return 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
1136 1137
    case WM_PAINTCLIPBOARD:
    case WM_SIZECLIPBOARD:
1138
        FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
Alexandre Julliard's avatar
Alexandre Julliard committed
1139
        return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1140
    default:  /* No translation needed */
Alexandre Julliard's avatar
Alexandre Julliard committed
1141
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1142 1143 1144 1145 1146
    }
}


/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1147
 *	     WINPROC_UnmapMsg32WTo32A
Alexandre Julliard's avatar
Alexandre Julliard committed
1148
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1149
 * Unmap a message that was mapped from Unicode to Ansi.
Alexandre Julliard's avatar
Alexandre Julliard committed
1150
 */
1151
static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
Juergen Schmied's avatar
Juergen Schmied committed
1152 1153
{
    switch(msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1154 1155
    {
    case WM_GETTEXT:
1156
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
1157
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
1158
            LPARAM *ptr = (LPARAM *)lParam - 1;
1159 1160 1161
            if (!wParam) result = 0;
            else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
                                                     (LPWSTR)*ptr, wParam )))
1162
            {
1163 1164
                ((LPWSTR)*ptr)[wParam-1] = 0;
                result = wParam - 1;
1165
            }
1166
            else result--;  /* do not count terminating null */
1167
            HeapFree( GetProcessHeap(), 0, ptr );
1168 1169
        }
        break;
1170 1171

    case WM_SETTEXT:
1172
    case WM_WININICHANGE:
1173
    case WM_DEVMODECHANGE:
1174 1175 1176 1177
    case CB_DIR:
    case LB_DIR:
    case LB_ADDFILE:
    case EM_REPLACESEL:
1178
        HeapFree( GetProcessHeap(), 0, (void *)lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1179
        break;
1180

Alexandre Julliard's avatar
Alexandre Julliard committed
1181 1182
    case WM_MDICREATE:
        {
1183
            MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1184
            if (HIWORD(cs->szTitle))
1185
                HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
Alexandre Julliard's avatar
Alexandre Julliard committed
1186
            if (HIWORD(cs->szClass))
1187 1188
                HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
1189 1190
        }
        break;
1191 1192

/* Listbox */
1193 1194
    case LB_ADDSTRING:
    case LB_INSERTSTRING:
1195 1196 1197
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_SELECTSTRING:
1198
	if ( WINPROC_TestLBForStr( hwnd ))
1199
          HeapFree( GetProcessHeap(), 0, (void *)lParam );
1200 1201
        break;

1202
    case LB_GETTEXT:
1203 1204 1205
        if ( WINPROC_TestLBForStr( hwnd ))
        {
            LPARAM *ptr = (LPARAM *)lParam - 1;
1206
            result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1207
            HeapFree( GetProcessHeap(), 0, ptr );
1208 1209 1210 1211
        }
        break;

/* Combobox */
1212 1213
    case CB_ADDSTRING:
    case CB_INSERTSTRING:
1214 1215 1216
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_SELECTSTRING:
1217
	if ( WINPROC_TestCBForStr( hwnd ))
1218
          HeapFree( GetProcessHeap(), 0, (void *)lParam );
1219 1220
        break;

1221
    case CB_GETLBTEXT:
1222 1223 1224
        if ( WINPROC_TestCBForStr( hwnd ))
        {
            LPARAM *ptr = (LPARAM *)lParam - 1;
1225
            result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1226
            HeapFree( GetProcessHeap(), 0, ptr );
1227 1228 1229 1230
        }
        break;

/* Multiline edit */
1231
    case EM_GETLINE:
1232 1233 1234 1235 1236 1237 1238 1239 1240
        {
            LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lparam */
            WORD len = *(WORD *)ptr;
            if (len)
            {
                result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
                if (result < len) ((LPWSTR)*ptr)[result] = 0;
            }
            HeapFree( GetProcessHeap(), 0, ptr );
1241 1242
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1243
    }
1244
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1245
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1246

1247
static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
{
    HANDLE	dst;
    UINT	sz = GlobalSize16(src);
    LPSTR	ptr16, ptr32;

    if (!(dst = GlobalAlloc(flags, sz)))
	return 0;
    ptr16 = GlobalLock16(src);
    ptr32 = GlobalLock(dst);
    if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
    GlobalUnlock16(src);
    GlobalUnlock(dst);

1261
    return (UINT)dst;
1262
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1263 1264

/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1265
 *	     WINPROC_MapMsg16To32A
Alexandre Julliard's avatar
Alexandre Julliard committed
1266
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1267 1268
 * Map a message from 16- to 32-bit Ansi.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
Alexandre Julliard's avatar
Alexandre Julliard committed
1269
 */
1270
INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1271
                             WPARAM *pwparam32, LPARAM *plparam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1272
{
1273 1274
    *pmsg32 = (UINT)msg16;
    *pwparam32 = (WPARAM)wParam16;
Alexandre Julliard's avatar
Alexandre Julliard committed
1275
    switch(msg16)
Alexandre Julliard's avatar
Alexandre Julliard committed
1276 1277 1278 1279 1280
    {
    case WM_ACTIVATE:
    case WM_CHARTOITEM:
    case WM_COMMAND:
    case WM_VKEYTOITEM:
Alexandre Julliard's avatar
Alexandre Julliard committed
1281
        *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1282
        *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1283
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1284 1285 1286
    case WM_HSCROLL:
    case WM_VSCROLL:
        *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1287
        *plparam   = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1288
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1289
    case WM_CTLCOLOR:
1290
    	if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1291
        *pmsg32    = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1292
        *pwparam32 = (WPARAM)HDC_32(wParam16);
1293
        *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1294
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1295 1296
    case WM_COMPAREITEM:
        {
1297
            COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1298
            COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)
1299
                                        HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
Alexandre Julliard's avatar
Alexandre Julliard committed
1300 1301 1302
            if (!cis) return -1;
            cis->CtlType    = cis16->CtlType;
            cis->CtlID      = cis16->CtlID;
1303
            cis->hwndItem   = WIN_Handle32( cis16->hwndItem );
Alexandre Julliard's avatar
Alexandre Julliard committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
            cis->itemID1    = cis16->itemID1;
            cis->itemData1  = cis16->itemData1;
            cis->itemID2    = cis16->itemID2;
            cis->itemData2  = cis16->itemData2;
            cis->dwLocaleId = 0;  /* FIXME */
            *plparam = (LPARAM)cis;
        }
        return 1;
    case WM_DELETEITEM:
        {
1314
            DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1315
            DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)
1316
                                        HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
Alexandre Julliard's avatar
Alexandre Julliard committed
1317 1318 1319
            if (!dis) return -1;
            dis->CtlType  = dis16->CtlType;
            dis->CtlID    = dis16->CtlID;
1320
            dis->hwndItem = WIN_Handle32( dis16->hwndItem );
Alexandre Julliard's avatar
Alexandre Julliard committed
1321 1322 1323 1324 1325 1326
            dis->itemData = dis16->itemData;
            *plparam = (LPARAM)dis;
        }
        return 1;
    case WM_MEASUREITEM:
        {
1327
            MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1328
            MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)
1329
                                        HeapAlloc(GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
                                                sizeof(*mis) + sizeof(LPARAM));
            if (!mis) return -1;
            mis->CtlType    = mis16->CtlType;
            mis->CtlID      = mis16->CtlID;
            mis->itemID     = mis16->itemID;
            mis->itemWidth  = mis16->itemWidth;
            mis->itemHeight = mis16->itemHeight;
            mis->itemData   = mis16->itemData;
            *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)mis;
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1342 1343
    case WM_DRAWITEM:
        {
1344
            DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1345
            DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)HeapAlloc(GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1346 1347
                                                                 sizeof(*dis));
            if (!dis) return -1;
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
            dis->CtlType       = dis16->CtlType;
            dis->CtlID         = dis16->CtlID;
            dis->itemID        = dis16->itemID;
            dis->itemAction    = dis16->itemAction;
            dis->itemState     = dis16->itemState;
            dis->hwndItem      = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
                                                            : WIN_Handle32( dis16->hwndItem );
            dis->hDC           = HDC_32(dis16->hDC);
            dis->itemData      = dis16->itemData;
            dis->rcItem.left   = dis16->rcItem.left;
            dis->rcItem.top    = dis16->rcItem.top;
            dis->rcItem.right  = dis16->rcItem.right;
            dis->rcItem.bottom = dis16->rcItem.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
1361 1362 1363
            *plparam = (LPARAM)dis;
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1364 1365
    case WM_GETMINMAXINFO:
        {
1366
            MINMAXINFO *mmi = (MINMAXINFO *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1367 1368
                                                sizeof(*mmi) + sizeof(LPARAM));
            if (!mmi) return -1;
1369
            MINMAXINFO16to32( MapSL(*plparam), mmi );
Alexandre Julliard's avatar
Alexandre Julliard committed
1370 1371
            *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)mmi;
Alexandre Julliard's avatar
Alexandre Julliard committed
1372
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1373
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1374
    case WM_GETTEXT:
Ove Kaaven's avatar
Ove Kaaven committed
1375
    case WM_SETTEXT:
1376 1377 1378
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
    case WM_ASKCBFORMATNAME:
1379
        *plparam = (LPARAM)MapSL(*plparam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1380
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1381 1382
    case WM_MDICREATE:
        {
1383 1384
            MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
            MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1385
            if (!cs) return -1;
1386
            MDICREATESTRUCT16to32A( cs16, cs );
1387 1388
            cs->szTitle = MapSL(cs16->szTitle);
            cs->szClass = MapSL(cs16->szClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
1389 1390 1391 1392
            *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)cs;
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1393
    case WM_MDIGETACTIVE:
1394
        *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1395
        *(BOOL*)(*plparam) = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1396
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1397
    case WM_MDISETMENU:
1398
        if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1399 1400
        *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
        *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
Alexandre Julliard's avatar
Alexandre Julliard committed
1401
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1402
    case WM_MENUCHAR:
Alexandre Julliard's avatar
Alexandre Julliard committed
1403
        *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1404
        *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
Alexandre Julliard's avatar
Alexandre Julliard committed
1405
        return 0;
1406 1407 1408
    case WM_MENUSELECT:
        if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
        {
1409
            HMENU hmenu=HMENU_32(HIWORD(*plparam));
1410
            UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1411 1412 1413 1414
            if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
            *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
        }
        else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1415
        *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1416
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1417
    case WM_MDIACTIVATE:
Alexandre Julliard's avatar
Alexandre Julliard committed
1418 1419
	if( *plparam )
	{
1420 1421
            *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
            *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1422 1423 1424
	}
	else /* message sent to MDI client */
	    *pwparam32 = wParam16;
Alexandre Julliard's avatar
Alexandre Julliard committed
1425
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1426 1427
    case WM_NCCALCSIZE:
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
1428
            NCCALCSIZE_PARAMS16 *nc16;
1429
            NCCALCSIZE_PARAMS *nc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1430

1431
            nc = (NCCALCSIZE_PARAMS *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1432 1433
                                                sizeof(*nc) + sizeof(LPARAM) );
            if (!nc) return -1;
1434
            nc16 = MapSL(*plparam);
1435 1436 1437 1438
            nc->rgrc[0].left   = nc16->rgrc[0].left;
            nc->rgrc[0].top    = nc16->rgrc[0].top;
            nc->rgrc[0].right  = nc16->rgrc[0].right;
            nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
1439
            if (wParam16)
Alexandre Julliard's avatar
Alexandre Julliard committed
1440
            {
1441
                nc->lppos = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1442
                                                      sizeof(*nc->lppos) );
1443 1444 1445 1446 1447 1448 1449 1450
                nc->rgrc[1].left   = nc16->rgrc[1].left;
                nc->rgrc[1].top    = nc16->rgrc[1].top;
                nc->rgrc[1].right  = nc16->rgrc[1].right;
                nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
                nc->rgrc[2].left   = nc16->rgrc[2].left;
                nc->rgrc[2].top    = nc16->rgrc[2].top;
                nc->rgrc[2].right  = nc16->rgrc[2].right;
                nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1451
                if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
Alexandre Julliard's avatar
Alexandre Julliard committed
1452
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1453 1454
            *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)nc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1455
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1456
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1457 1458 1459
    case WM_NCCREATE:
    case WM_CREATE:
        {
1460
            CREATESTRUCT16 *cs16 = MapSL(*plparam);
1461
            CREATESTRUCTA *cs = (CREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1462 1463
                                                sizeof(*cs) + sizeof(LPARAM) );
            if (!cs) return -1;
1464
            CREATESTRUCT16to32A( cs16, cs );
1465 1466
            cs->lpszName  = MapSL(cs16->lpszName);
            cs->lpszClass = MapSL(cs16->lpszClass);
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16;
                MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)HeapAlloc(GetProcessHeap(), 0,
                                                                           sizeof(*mdi_cs));
                if (!mdi_cs)
                {
                    HeapFree(GetProcessHeap(), 0, cs);
                    return -1;
                }
                mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1479
                MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1480 1481 1482 1483 1484
                mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
                mdi_cs->szClass = MapSL(mdi_cs16->szClass);

                cs->lpCreateParams = mdi_cs;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1485 1486
            *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)cs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1487
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1488
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1489
    case WM_PARENTNOTIFY:
Alexandre Julliard's avatar
Alexandre Julliard committed
1490 1491 1492
        if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
        {
            *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1493
            *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1494 1495
        }
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1496 1497 1498
    case WM_WINDOWPOSCHANGING:
    case WM_WINDOWPOSCHANGED:
        {
1499
            WINDOWPOS *wp = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1500 1501
                                                sizeof(*wp) + sizeof(LPARAM) );
            if (!wp) return -1;
1502
            WINDOWPOS16to32( MapSL(*plparam), wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
1503 1504
            *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)wp;
Alexandre Julliard's avatar
Alexandre Julliard committed
1505
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1506
        return 1;
Ove Kaaven's avatar
Ove Kaaven committed
1507
    case WM_GETDLGCODE:
1508
        if (*plparam)
Ove Kaaven's avatar
Ove Kaaven committed
1509
        {
1510
            LPMSG16 msg16 = MapSL(*plparam);
1511
            LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
Ove Kaaven's avatar
Ove Kaaven committed
1512 1513

            if (!msg32) return -1;
1514
            msg32->hwnd = WIN_Handle32( msg16->hwnd );
Ove Kaaven's avatar
Ove Kaaven committed
1515 1516
            msg32->lParam = msg16->lParam;
            msg32->time = msg16->time;
1517 1518
            msg32->pt.x = msg16->pt.x;
            msg32->pt.y = msg16->pt.y;
Ove Kaaven's avatar
Ove Kaaven committed
1519
            /* this is right, right? */
1520
            if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
Ove Kaaven's avatar
Ove Kaaven committed
1521 1522
                                     &msg32->message,&msg32->wParam,
                                     &msg32->lParam)<0) {
1523
                HeapFree( GetProcessHeap(), 0, msg32 );
Ove Kaaven's avatar
Ove Kaaven committed
1524 1525 1526
                return -1;
            }
            *plparam = (LPARAM)msg32;
1527
            return 1;
Ove Kaaven's avatar
Ove Kaaven committed
1528
        }
1529
        else return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1530
    case WM_NOTIFY:
1531
    	*plparam = (LPARAM)MapSL(*plparam);
1532
        return 0;
1533
    case WM_ACTIVATEAPP:
1534 1535 1536 1537
        /* We need this when SetActiveWindow sends a Sendmessage16() to
         * a 32bit window. Might be superflous with 32bit interprocess
         * message queues. */
        if (*plparam) *plparam = HTASK_32( *plparam );
1538
        return 0;
1539 1540 1541 1542
    case WM_NEXTMENU:
        {
            MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
            if (!next) return -1;
1543
            next->hmenuIn = (HMENU)*plparam;
1544 1545 1546 1547 1548
            next->hmenuNext = 0;
            next->hwndNext = 0;
            *plparam = (LPARAM)next;
            return 1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1549 1550
    case WM_PAINTCLIPBOARD:
    case WM_SIZECLIPBOARD:
1551
        FIXME_(msg)("message %04x needs translation\n",msg16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1552
        return -1;
1553
    case WM_DDE_INITIATE:
1554 1555 1556
    case WM_DDE_TERMINATE:
    case WM_DDE_UNADVISE:
    case WM_DDE_REQUEST:
1557
	*pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1558 1559 1560 1561 1562 1563 1564
	return 0;
    case WM_DDE_ADVISE:
    case WM_DDE_DATA:
    case WM_DDE_POKE:
        {
	    HANDLE16	lo16;
	    ATOM	hi;
1565
	    UINT lo32 = 0;
1566

1567
	    *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	    lo16 = LOWORD(*plparam);
	    hi = HIWORD(*plparam);
	    if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
		return -1;
	    *plparam = PackDDElParam(msg16, lo32, hi);
	}
	return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
    case WM_DDE_ACK:
        {
	    UINT	lo, hi;
	    int		flag = 0;
	    char	buf[2];

1581
	    *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
Alexandre Julliard's avatar
Alexandre Julliard committed
1582

1583 1584 1585 1586 1587 1588 1589 1590
	    lo = LOWORD(*plparam);
	    hi = HIWORD(*plparam);

	    if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
	    if (GlobalSize16(hi) != 0) flag |= 2;
	    switch (flag)
	    {
	    case 0:
1591
		if (hi)
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
		{
		    MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
		    hi = 0;
		}
		break;
	    case 1:
		break; /* atom, nothing to do */
	    case 3:
		MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
		/* fall thru */
	    case 2:
		hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
		break;
	    }
	    *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
	}
	return 0; /* FIXME don't know how to free allocated memory (handle) !! */
    case WM_DDE_EXECUTE:
	*plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
        return 0; /* FIXME don't know how to free allocated memory (handle) !! */
Alexandre Julliard's avatar
Alexandre Julliard committed
1612
    default:  /* No translation needed */
Alexandre Julliard's avatar
Alexandre Julliard committed
1613
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1614 1615 1616 1617 1618
    }
}


/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1619
 *	     WINPROC_UnmapMsg16To32A
Alexandre Julliard's avatar
Alexandre Julliard committed
1620
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1621
 * Unmap a message that was mapped from 16- to 32-bit Ansi.
Alexandre Julliard's avatar
Alexandre Julliard committed
1622
 */
1623
LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
1624
                                 LRESULT result )
Alexandre Julliard's avatar
Alexandre Julliard committed
1625 1626 1627
{
    switch(msg)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1628 1629
    case WM_COMPAREITEM:
    case WM_DELETEITEM:
Alexandre Julliard's avatar
Alexandre Julliard committed
1630
    case WM_DRAWITEM:
1631
        HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1632
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1633 1634 1635
    case WM_MEASUREITEM:
        {
            MEASUREITEMSTRUCT16 *mis16;
1636
            MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1637
            lParam = *(LPARAM *)(mis + 1);
1638
            mis16 = MapSL(lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1639 1640
            mis16->itemWidth  = (UINT16)mis->itemWidth;
            mis16->itemHeight = (UINT16)mis->itemHeight;
1641
            HeapFree( GetProcessHeap(), 0, mis );
Alexandre Julliard's avatar
Alexandre Julliard committed
1642 1643
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1644 1645
    case WM_GETMINMAXINFO:
        {
1646
            MINMAXINFO *mmi = (MINMAXINFO *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1647
            lParam = *(LPARAM *)(mmi + 1);
1648
            MINMAXINFO32to16( mmi, MapSL(lParam));
1649
            HeapFree( GetProcessHeap(), 0, mmi );
Alexandre Julliard's avatar
Alexandre Julliard committed
1650 1651
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1652 1653
    case WM_MDICREATE:
        {
1654
            MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1655
            lParam = *(LPARAM *)(cs + 1);
1656
            MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1657
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
1658 1659
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1660
    case WM_MDIGETACTIVE:
1661
        result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1662
        HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1663
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1664 1665 1666
    case WM_NCCALCSIZE:
        {
            NCCALCSIZE_PARAMS16 *nc16;
1667
            NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1668
            lParam = *(LPARAM *)(nc + 1);
1669
            nc16 = MapSL(lParam);
1670 1671 1672 1673
            nc16->rgrc[0].left   = nc->rgrc[0].left;
            nc16->rgrc[0].top    = nc->rgrc[0].top;
            nc16->rgrc[0].right  = nc->rgrc[0].right;
            nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
1674 1675
            if (wParam)
            {
1676 1677 1678 1679 1680 1681 1682 1683
                nc16->rgrc[1].left   = nc->rgrc[1].left;
                nc16->rgrc[1].top    = nc->rgrc[1].top;
                nc16->rgrc[1].right  = nc->rgrc[1].right;
                nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
                nc16->rgrc[2].left   = nc->rgrc[2].left;
                nc16->rgrc[2].top    = nc->rgrc[2].top;
                nc16->rgrc[2].right  = nc->rgrc[2].right;
                nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
1684 1685
                if (nc->lppos)
                {
1686
                    WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1687
                    HeapFree( GetProcessHeap(), 0, nc->lppos );
Alexandre Julliard's avatar
Alexandre Julliard committed
1688 1689
                }
            }
1690
            HeapFree( GetProcessHeap(), 0, nc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1691 1692 1693 1694 1695
        }
	break;
    case WM_NCCREATE:
    case WM_CREATE:
        {
1696
            CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1697
            lParam = *(LPARAM *)(cs + 1);
1698
            CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1699 1700 1701 1702

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
                HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);

1703
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
1704 1705 1706 1707 1708
        }
        break;
    case WM_WINDOWPOSCHANGING:
    case WM_WINDOWPOSCHANGED:
        {
1709
            WINDOWPOS *wp = (WINDOWPOS *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1710
            lParam = *(LPARAM *)(wp + 1);
1711
            WINDOWPOS32to16(wp, MapSL(lParam));
1712
            HeapFree( GetProcessHeap(), 0, wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
1713 1714
        }
        break;
Ove Kaaven's avatar
Ove Kaaven committed
1715
    case WM_GETDLGCODE:
1716
        if (lParam)
Ove Kaaven's avatar
Ove Kaaven committed
1717
        {
1718
            LPMSG msg32 = (LPMSG)lParam;
Ove Kaaven's avatar
Ove Kaaven committed
1719

1720
            WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
Ove Kaaven's avatar
Ove Kaaven committed
1721
                                     result);
1722
            HeapFree( GetProcessHeap(), 0, msg32 );
Ove Kaaven's avatar
Ove Kaaven committed
1723 1724
        }
        break;
1725 1726 1727
    case WM_NEXTMENU:
        {
            MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1728
            result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1729 1730 1731
            HeapFree( GetProcessHeap(), 0, next );
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1732
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1733
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1734 1735 1736 1737 1738 1739 1740 1741 1742
}


/**********************************************************************
 *	     WINPROC_MapMsg16To32W
 *
 * Map a message from 16- to 32-bit Unicode.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
 */
1743 1744
INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
                           WPARAM *pwparam32, LPARAM *plparam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1745
{
1746
    BYTE ch;
1747 1748
    WCHAR wch;

1749 1750
    *pmsg32=(UINT)msg16;
    *pwparam32 = (WPARAM)wParam16;
Alexandre Julliard's avatar
Alexandre Julliard committed
1751 1752
    switch(msg16)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1753 1754
    case WM_GETTEXT:
    case WM_SETTEXT:
1755 1756 1757
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
    case WM_ASKCBFORMATNAME:
1758
        *plparam = (LPARAM)MapSL(*plparam);
1759
        return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1760 1761 1762 1763
    case WM_GETTEXTLENGTH:
    case CB_GETLBTEXTLEN:
    case LB_GETTEXTLEN:
        return 1;  /* need to map result */
Alexandre Julliard's avatar
Alexandre Julliard committed
1764 1765 1766
    case WM_NCCREATE:
    case WM_CREATE:
        {
1767
            CREATESTRUCT16 *cs16 = MapSL(*plparam);
1768
            CREATESTRUCTW *cs = (CREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1769 1770
                                                sizeof(*cs) + sizeof(LPARAM) );
            if (!cs) return -1;
1771
            CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1772 1773
            cs->lpszName  = map_str_16_to_32W(cs16->lpszName);
            cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16;
                MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
                                                                           sizeof(*mdi_cs));
                if (!mdi_cs)
                {
                    HeapFree(GetProcessHeap(), 0, cs);
                    return -1;
                }
                mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1786
                MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1787 1788 1789 1790 1791
                mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
                mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);

                cs->lpCreateParams = mdi_cs;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1792 1793
            *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)cs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1794
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1795
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1796 1797
    case WM_MDICREATE:
        {
1798
            MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1799
            MDICREATESTRUCTW *cs =
1800
                (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
1801 1802
                                                sizeof(*cs) + sizeof(LPARAM) );
            if (!cs) return -1;
1803
            MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1804 1805
            cs->szTitle = map_str_16_to_32W(cs16->szTitle);
            cs->szClass = map_str_16_to_32W(cs16->szClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
1806 1807 1808 1809
            *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
            *plparam = (LPARAM)cs;
        }
        return 1;
Ove Kaaven's avatar
Ove Kaaven committed
1810
    case WM_GETDLGCODE:
1811
        if (*plparam)
Ove Kaaven's avatar
Ove Kaaven committed
1812
        {
1813
            LPMSG16 msg16 = MapSL(*plparam);
1814
            LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
Ove Kaaven's avatar
Ove Kaaven committed
1815 1816

            if (!msg32) return -1;
1817
            msg32->hwnd = WIN_Handle32( msg16->hwnd );
Ove Kaaven's avatar
Ove Kaaven committed
1818 1819
            msg32->lParam = msg16->lParam;
            msg32->time = msg16->time;
1820 1821
            msg32->pt.x = msg16->pt.x;
            msg32->pt.y = msg16->pt.y;
Ove Kaaven's avatar
Ove Kaaven committed
1822
            /* this is right, right? */
1823
            if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
Ove Kaaven's avatar
Ove Kaaven committed
1824 1825
                                     &msg32->message,&msg32->wParam,
                                     &msg32->lParam)<0) {
1826
                HeapFree( GetProcessHeap(), 0, msg32 );
Ove Kaaven's avatar
Ove Kaaven committed
1827 1828 1829
                return -1;
            }
            *plparam = (LPARAM)msg32;
1830
            return 1;
Ove Kaaven's avatar
Ove Kaaven committed
1831
        }
1832
        else return 0;
1833

1834 1835 1836 1837
    case WM_CHARTOITEM:
        ch = wParam16;
        MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
        *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1838
        *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1839 1840 1841 1842 1843
        return 0;
    case WM_MENUCHAR:
        ch = wParam16;
        MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
        *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1844
        *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1845
        return 0;
1846 1847 1848 1849
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
1850 1851 1852
        ch = wParam16;
        MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
        *pwparam32 = wch;
1853
        return 0;
1854
    case WM_IME_CHAR:
1855
        return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1856

Alexandre Julliard's avatar
Alexandre Julliard committed
1857
    default:  /* No Unicode translation needed */
1858
        return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
Alexandre Julliard's avatar
Alexandre Julliard committed
1859
                                      pwparam32, plparam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1860 1861 1862 1863 1864
    }
}


/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1865
 *	     WINPROC_UnmapMsg16To32W
Alexandre Julliard's avatar
Alexandre Julliard committed
1866
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1867
 * Unmap a message that was mapped from 16- to 32-bit Unicode.
Alexandre Julliard's avatar
Alexandre Julliard committed
1868
 */
1869
LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
1870
                                 LRESULT result )
Alexandre Julliard's avatar
Alexandre Julliard committed
1871 1872 1873
{
    switch(msg)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1874 1875
    case WM_GETTEXT:
    case WM_SETTEXT:
1876 1877 1878 1879 1880
    case WM_GETTEXTLENGTH:
    case CB_GETLBTEXTLEN:
    case LB_GETTEXTLEN:
    case WM_ASKCBFORMATNAME:
        return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
1881 1882 1883
    case WM_NCCREATE:
    case WM_CREATE:
        {
1884
            CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1885
            lParam = *(LPARAM *)(cs + 1);
1886
            CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1887 1888
            unmap_str_16_to_32W( cs->lpszName );
            unmap_str_16_to_32W( cs->lpszClass );
1889 1890 1891 1892 1893 1894 1895 1896

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
                unmap_str_16_to_32W( mdi_cs->szTitle );
                unmap_str_16_to_32W( mdi_cs->szClass );
                HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
            }
1897
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
1898 1899
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1900 1901
    case WM_MDICREATE:
        {
1902
            MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
1903
            lParam = *(LPARAM *)(cs + 1);
1904
            MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1905 1906
            unmap_str_16_to_32W( cs->szTitle );
            unmap_str_16_to_32W( cs->szClass );
1907
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
1908 1909
        }
        break;
Ove Kaaven's avatar
Ove Kaaven committed
1910
    case WM_GETDLGCODE:
1911
        if (lParam)
Ove Kaaven's avatar
Ove Kaaven committed
1912
        {
1913
            LPMSG msg32 = (LPMSG)lParam;
Ove Kaaven's avatar
Ove Kaaven committed
1914

1915
            WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
Ove Kaaven's avatar
Ove Kaaven committed
1916
                                     result);
1917
            HeapFree( GetProcessHeap(), 0, msg32 );
Ove Kaaven's avatar
Ove Kaaven committed
1918 1919
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1920
    default:
1921
        return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
1922
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1923
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1924 1925
}

1926
static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1927 1928
{
    HANDLE16	dst;
1929
    UINT	sz = GlobalSize((HANDLE)src);
1930 1931 1932 1933
    LPSTR	ptr16, ptr32;

    if (!(dst = GlobalAlloc16(flags, sz)))
	return 0;
1934
    ptr32 = GlobalLock((HANDLE)src);
1935 1936
    ptr16 = GlobalLock16(dst);
    if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1937
    GlobalUnlock((HANDLE)src);
1938 1939 1940 1941 1942
    GlobalUnlock16(dst);

    return dst;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1943 1944 1945 1946 1947 1948 1949

/**********************************************************************
 *	     WINPROC_MapMsg32ATo16
 *
 * Map a message from 32-bit Ansi to 16-bit.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
 */
1950
INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
Alexandre Julliard's avatar
Alexandre Julliard committed
1951 1952
                             UINT16 *pmsg16, WPARAM16 *pwparam16,
                             LPARAM *plparam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1953 1954 1955 1956 1957
{
    *pmsg16 = (UINT16)msg32;
    *pwparam16 = (WPARAM16)LOWORD(wParam32);
    switch(msg32)
    {
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
    case SBM_SETRANGE:
        *pmsg16 = SBM_SETRANGE16;
        *plparam = MAKELPARAM(wParam32, *plparam);
        *pwparam16 = 0;
        return 0;

    case SBM_GETRANGE:
        *pmsg16 = SBM_GETRANGE16;
        return 1;

1968 1969 1970 1971 1972 1973
    case BM_GETCHECK:
    case BM_SETCHECK:
    case BM_GETSTATE:
    case BM_SETSTATE:
    case BM_SETSTYLE:
        *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
Alexandre Julliard's avatar
Alexandre Julliard committed
1974
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1975

1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
    case EM_GETSEL:
    case EM_GETRECT:
    case EM_SETRECT:
    case EM_SETRECTNP:
    case EM_SCROLL:
    case EM_LINESCROLL:
    case EM_SCROLLCARET:
    case EM_GETMODIFY:
    case EM_SETMODIFY:
    case EM_GETLINECOUNT:
    case EM_LINEINDEX:
    case EM_SETHANDLE:
    case EM_GETHANDLE:
    case EM_GETTHUMB:
    case EM_LINELENGTH:
    case EM_REPLACESEL:
    case EM_GETLINE:
    case EM_LIMITTEXT:
    case EM_CANUNDO:
    case EM_UNDO:
    case EM_FMTLINES:
    case EM_LINEFROMCHAR:
    case EM_SETTABSTOPS:
    case EM_SETPASSWORDCHAR:
    case EM_EMPTYUNDOBUFFER:
    case EM_GETFIRSTVISIBLELINE:
    case EM_SETREADONLY:
    case EM_SETWORDBREAKPROC:
    case EM_GETWORDBREAKPROC:
    case EM_GETPASSWORDCHAR:
        *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
Alexandre Julliard's avatar
Alexandre Julliard committed
2007 2008
        return 0;

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
    case LB_CARETOFF:
    case LB_CARETON:
    case LB_DELETESTRING:
    case LB_GETANCHORINDEX:
    case LB_GETCARETINDEX:
    case LB_GETCOUNT:
    case LB_GETCURSEL:
    case LB_GETHORIZONTALEXTENT:
    case LB_GETITEMDATA:
    case LB_GETITEMHEIGHT:
    case LB_GETSEL:
    case LB_GETSELCOUNT:
    case LB_GETTEXTLEN:
    case LB_GETTOPINDEX:
    case LB_RESETCONTENT:
    case LB_SELITEMRANGE:
    case LB_SELITEMRANGEEX:
    case LB_SETANCHORINDEX:
    case LB_SETCARETINDEX:
    case LB_SETCOLUMNWIDTH:
    case LB_SETCURSEL:
    case LB_SETHORIZONTALEXTENT:
    case LB_SETITEMDATA:
    case LB_SETITEMHEIGHT:
    case LB_SETSEL:
    case LB_SETTOPINDEX:
        *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
Alexandre Julliard's avatar
Alexandre Julliard committed
2036
        return 0;
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
    case CB_DELETESTRING:
    case CB_GETCOUNT:
    case CB_GETLBTEXTLEN:
    case CB_LIMITTEXT:
    case CB_RESETCONTENT:
    case CB_SETEDITSEL:
    case CB_GETCURSEL:
    case CB_SETCURSEL:
    case CB_SHOWDROPDOWN:
    case CB_SETITEMDATA:
    case CB_SETITEMHEIGHT:
    case CB_GETITEMHEIGHT:
    case CB_SETEXTENDEDUI:
    case CB_GETEXTENDEDUI:
    case CB_GETDROPPEDSTATE:
	*pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
Alexandre Julliard's avatar
Alexandre Julliard committed
2053
	return 0;
2054
    case CB_GETEDITSEL:
Alexandre Julliard's avatar
Alexandre Julliard committed
2055 2056 2057
	*pmsg16 = CB_GETEDITSEL16;
	return 1;

2058 2059 2060 2061 2062 2063 2064
    case LB_ADDSTRING:
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_INSERTSTRING:
    case LB_SELECTSTRING:
    case LB_DIR:
    case LB_ADDFILE:
2065
        *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2066
        *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
Alexandre Julliard's avatar
Alexandre Julliard committed
2067
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2068

2069 2070 2071 2072 2073 2074
    case CB_ADDSTRING:
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_INSERTSTRING:
    case CB_SELECTSTRING:
    case CB_DIR:
2075
        *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2076
	*pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
Alexandre Julliard's avatar
Alexandre Julliard committed
2077 2078
	return 1;

2079
    case LB_GETITEMRECT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2080
        {
2081
            RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2082 2083
            if (!rect) return -1;
            *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2084
            *plparam = MapLS( rect );
Alexandre Julliard's avatar
Alexandre Julliard committed
2085
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2086
	*pmsg16 = LB_GETITEMRECT16;
Alexandre Julliard's avatar
Alexandre Julliard committed
2087
        return 1;
2088
    case LB_GETSELITEMS:
Alexandre Julliard's avatar
Alexandre Julliard committed
2089
        {
2090 2091
            LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */

2092
            *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2093 2094
            if (!(items = HeapAlloc( GetProcessHeap(), 0,
                                     *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2095
            *items++ = *plparam;  /* Store the previous lParam */
2096
            *plparam = MapLS( items );
Alexandre Julliard's avatar
Alexandre Julliard committed
2097
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2098
	*pmsg16 = LB_GETSELITEMS16;
Alexandre Julliard's avatar
Alexandre Julliard committed
2099
        return 1;
2100
    case LB_SETTABSTOPS:
Alexandre Julliard's avatar
Alexandre Julliard committed
2101 2102
        if (wParam32)
        {
2103
            INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
2104
            LPINT16 stops;
2105
            *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2106 2107
            if (!(stops = HeapAlloc( GetProcessHeap(), 0,
                                     *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2108
            for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2109
            *plparam = MapLS( stops );
Alexandre Julliard's avatar
Alexandre Julliard committed
2110 2111 2112 2113
            return 1;
        }
        *pmsg16 = LB_SETTABSTOPS16;
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2114

2115
    case CB_GETDROPPEDCONTROLRECT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2116
        {
2117
	    RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2118 2119
	    if (!rect) return -1;
	    *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2120
	    *plparam = (LPARAM)MapLS(rect);
Alexandre Julliard's avatar
Alexandre Julliard committed
2121 2122 2123 2124
        }
	*pmsg16 = CB_GETDROPPEDCONTROLRECT16;
        return 1;

2125
    case LB_GETTEXT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2126 2127 2128 2129
	*plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
	*pmsg16 = LB_GETTEXT16;
	return 1;

2130
    case CB_GETLBTEXT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2131 2132 2133 2134
	*plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
	*pmsg16 = CB_GETLBTEXT16;
	return 1;

2135
    case EM_SETSEL:
Alexandre Julliard's avatar
Alexandre Julliard committed
2136
	*pwparam16 = 0;
2137
	*plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2138 2139 2140
	*pmsg16 = EM_SETSEL16;
	return 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
2141 2142 2143 2144
    case WM_ACTIVATE:
    case WM_CHARTOITEM:
    case WM_COMMAND:
    case WM_VKEYTOITEM:
Alexandre Julliard's avatar
Alexandre Julliard committed
2145 2146
        *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2147 2148 2149 2150
    case WM_HSCROLL:
    case WM_VSCROLL:
        *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2151 2152 2153 2154 2155 2156 2157
    case WM_CTLCOLORMSGBOX:
    case WM_CTLCOLOREDIT:
    case WM_CTLCOLORLISTBOX:
    case WM_CTLCOLORBTN:
    case WM_CTLCOLORDLG:
    case WM_CTLCOLORSCROLLBAR:
    case WM_CTLCOLORSTATIC:
Alexandre Julliard's avatar
Alexandre Julliard committed
2158 2159 2160 2161
        *pmsg16  = WM_CTLCOLOR;
        *plparam = MAKELPARAM( (HWND16)*plparam,
                               (WORD)msg32 - WM_CTLCOLORMSGBOX );
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2162 2163
    case WM_COMPAREITEM:
        {
2164
            COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2165
            COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
Alexandre Julliard's avatar
Alexandre Julliard committed
2166 2167 2168
            if (!cis) return -1;
            cis->CtlType    = (UINT16)cis32->CtlType;
            cis->CtlID      = (UINT16)cis32->CtlID;
2169
            cis->hwndItem   = HWND_16( cis32->hwndItem );
Alexandre Julliard's avatar
Alexandre Julliard committed
2170 2171 2172 2173
            cis->itemID1    = (UINT16)cis32->itemID1;
            cis->itemData1  = cis32->itemData1;
            cis->itemID2    = (UINT16)cis32->itemID2;
            cis->itemData2  = cis32->itemData2;
2174
            *plparam = MapLS( cis );
Alexandre Julliard's avatar
Alexandre Julliard committed
2175 2176 2177 2178
        }
        return 1;
    case WM_DELETEITEM:
        {
2179
            DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2180
            DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2181 2182 2183 2184
            if (!dis) return -1;
            dis->CtlType  = (UINT16)dis32->CtlType;
            dis->CtlID    = (UINT16)dis32->CtlID;
            dis->itemID   = (UINT16)dis32->itemID;
2185
            dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2186
                                                       : HWND_16( dis32->hwndItem );
Alexandre Julliard's avatar
Alexandre Julliard committed
2187
            dis->itemData = dis32->itemData;
2188
            *plparam = MapLS( dis );
Alexandre Julliard's avatar
Alexandre Julliard committed
2189 2190
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2191 2192
    case WM_DRAWITEM:
        {
2193
            DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2194
            DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2195
            if (!dis) return -1;
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
            dis->CtlType       = (UINT16)dis32->CtlType;
            dis->CtlID         = (UINT16)dis32->CtlID;
            dis->itemID        = (UINT16)dis32->itemID;
            dis->itemAction    = (UINT16)dis32->itemAction;
            dis->itemState     = (UINT16)dis32->itemState;
            dis->hwndItem      = HWND_16( dis32->hwndItem );
            dis->hDC           = HDC_16(dis32->hDC);
            dis->itemData      = dis32->itemData;
            dis->rcItem.left   = dis32->rcItem.left;
            dis->rcItem.top    = dis32->rcItem.top;
            dis->rcItem.right  = dis32->rcItem.right;
            dis->rcItem.bottom = dis32->rcItem.bottom;
2208
            *plparam = MapLS( dis );
Alexandre Julliard's avatar
Alexandre Julliard committed
2209
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2210
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2211 2212
    case WM_MEASUREITEM:
        {
2213
            MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2214
            MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
Alexandre Julliard's avatar
Alexandre Julliard committed
2215 2216 2217 2218 2219 2220 2221 2222
            if (!mis) return -1;
            mis->CtlType    = (UINT16)mis32->CtlType;
            mis->CtlID      = (UINT16)mis32->CtlID;
            mis->itemID     = (UINT16)mis32->itemID;
            mis->itemWidth  = (UINT16)mis32->itemWidth;
            mis->itemHeight = (UINT16)mis32->itemHeight;
            mis->itemData   = mis32->itemData;
            *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
2223
            *plparam = MapLS( mis );
Alexandre Julliard's avatar
Alexandre Julliard committed
2224 2225
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2226 2227
    case WM_GETMINMAXINFO:
        {
2228
            MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2229
            if (!mmi) return -1;
2230
            MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
Alexandre Julliard's avatar
Alexandre Julliard committed
2231
            *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
2232
            *plparam = MapLS( mmi );
Alexandre Julliard's avatar
Alexandre Julliard committed
2233
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2234
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2235
    case WM_GETTEXT:
2236
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
2237
        {
2238
            LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2239
            *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2240
            if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2241
            *str++ = *plparam;  /* Store the previous lParam */
2242
            *plparam = MapLS( str );
Alexandre Julliard's avatar
Alexandre Julliard committed
2243
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2244
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2245 2246 2247
    case WM_MDICREATE:
        {
            MDICREATESTRUCT16 *cs;
2248
            MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
2249

2250
            if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2251
            MDICREATESTRUCT32Ato16( cs32, cs );
2252 2253 2254
            cs->szTitle = MapLS( cs32->szTitle );
            cs->szClass = MapLS( cs32->szClass );
            *plparam = MapLS( cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
2255 2256
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2257 2258
    case WM_MDIGETACTIVE:
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2259
    case WM_MDISETMENU:
Alexandre Julliard's avatar
Alexandre Julliard committed
2260 2261
        *plparam   = MAKELPARAM( (HMENU16)LOWORD(wParam32),
                                 (HMENU16)LOWORD(*plparam) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2262
        *pwparam16 = (*plparam == 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
2263
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2264
    case WM_MENUSELECT:
2265 2266
        if(HIWORD(wParam32) & MF_POPUP)
        {
2267
            HMENU hmenu;
2268 2269
            if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
            {
2270 2271
                if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
                    *pwparam16=HMENU_16(hmenu);
2272
            }
2273 2274 2275
        }
        /* fall through */
    case WM_MENUCHAR:
Alexandre Julliard's avatar
Alexandre Julliard committed
2276 2277
        *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2278
    case WM_MDIACTIVATE:
2279
        if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2280 2281 2282 2283 2284 2285 2286
        {
            *pwparam16 = ((HWND)*plparam == hwnd);
            *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
                                   (HWND16)LOWORD(wParam32) );
        }
        else
        {
2287
            *pwparam16 = HWND_16( (HWND)wParam32 );
2288
            *plparam = 0;
2289
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2290
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2291 2292
    case WM_NCCALCSIZE:
        {
2293
            NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2294
            NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
Alexandre Julliard's avatar
Alexandre Julliard committed
2295
            if (!nc) return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2296

2297 2298 2299 2300
            nc->rgrc[0].left   = nc32->rgrc[0].left;
            nc->rgrc[0].top    = nc32->rgrc[0].top;
            nc->rgrc[0].right  = nc32->rgrc[0].right;
            nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
2301
            if (wParam32)
Alexandre Julliard's avatar
Alexandre Julliard committed
2302
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
2303
                WINDOWPOS16 *wp;
2304 2305 2306 2307 2308 2309 2310 2311
                nc->rgrc[1].left   = nc32->rgrc[1].left;
                nc->rgrc[1].top    = nc32->rgrc[1].top;
                nc->rgrc[1].right  = nc32->rgrc[1].right;
                nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
                nc->rgrc[2].left   = nc32->rgrc[2].left;
                nc->rgrc[2].top    = nc32->rgrc[2].top;
                nc->rgrc[2].right  = nc32->rgrc[2].right;
                nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2312
                if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
Alexandre Julliard's avatar
Alexandre Julliard committed
2313
                {
2314
                    HeapFree( GetProcessHeap(), 0, nc );
Alexandre Julliard's avatar
Alexandre Julliard committed
2315
                    return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2316
                }
2317
                WINDOWPOS32to16( nc32->lppos, wp );
2318
                nc->lppos = MapLS( wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2319
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
2320
            *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
2321
            *plparam = MapLS( nc );
Alexandre Julliard's avatar
Alexandre Julliard committed
2322
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2323
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2324 2325 2326 2327
    case WM_NCCREATE:
    case WM_CREATE:
        {
            CREATESTRUCT16 *cs;
2328
            CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
2329

2330
            if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2331
            CREATESTRUCT32Ato16( cs32, cs );
2332 2333
            cs->lpszName  = MapLS( cs32->lpszName );
            cs->lpszClass = MapLS( cs32->lpszClass );
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16;
                MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
                mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
                if (!mdi_cs16)
                {
                    HeapFree(GetProcessHeap(), 0, cs);
                    return -1;
                }
2345
                MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2346 2347 2348 2349
                mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
                mdi_cs16->szClass = MapLS( mdi_cs->szClass );
                cs->lpCreateParams = MapLS( mdi_cs16 );
            }
2350
            *plparam = MapLS( cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
2351
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2352
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2353
    case WM_PARENTNOTIFY:
Alexandre Julliard's avatar
Alexandre Julliard committed
2354 2355 2356 2357
        if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
            *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
        /* else nothing to do */
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2358 2359 2360
    case WM_NOTIFY:
	*plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
	return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2361
    case WM_SETTEXT:
2362 2363
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
2364
        *plparam = MapLS( (LPSTR)*plparam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2365
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2366 2367 2368
    case WM_WINDOWPOSCHANGING:
    case WM_WINDOWPOSCHANGED:
        {
2369
            WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2370
            if (!wp) return -1;
2371
            WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2372
            *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
2373
            *plparam = MapLS( wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2374
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2375
        return 1;
2376 2377
    case WM_GETDLGCODE:
         if (*plparam) {
2378
            LPMSG msg32 = (LPMSG) *plparam;
2379
            LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2380 2381

            if (!msg16) return -1;
2382
            msg16->hwnd = HWND_16( msg32->hwnd );
2383 2384
            msg16->lParam = msg32->lParam;
            msg16->time = msg32->time;
2385 2386
            msg16->pt.x = msg32->pt.x;
            msg16->pt.y = msg32->pt.y;
2387 2388
            /* this is right, right? */
            if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2389 2390 2391
                         &msg16->message,&msg16->wParam, &msg16->lParam)<0)
            {
                HeapFree( GetProcessHeap(), 0, msg16 );
2392 2393
                return -1;
            }
2394
            *plparam = MapLS( msg16 );
2395 2396 2397 2398
            return 1;
        }
        return 0;

2399
    case WM_ACTIVATEAPP:
2400
        if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2401
        return 0;
2402 2403 2404
    case WM_NEXTMENU:
        {
            MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2405
            *plparam = (LPARAM)next->hmenuIn;
2406 2407
            return 1;
        }
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
    case WM_PAINT:
        if (IsIconic( hwnd ) && GetClassLongW( hwnd, GCL_HICON ))
        {
            *pmsg16 = WM_PAINTICON;
            *pwparam16 = 1;
        }
        return 0;
    case WM_ERASEBKGND:
        if (IsIconic( hwnd ) && GetClassLongW( hwnd, GCL_HICON ))
            *pmsg16 = WM_ICONERASEBKGND;
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2419 2420
    case WM_PAINTCLIPBOARD:
    case WM_SIZECLIPBOARD:
2421
        FIXME_(msg)("message %04x needs translation\n", msg32 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2422
        return -1;
2423 2424
    /* following messages should not be sent to 16-bit apps */
    case WM_SIZING:
2425
    case WM_MOVING:
2426 2427 2428
    case WM_CAPTURECHANGED:
    case WM_STYLECHANGING:
    case WM_STYLECHANGED:
2429
        return -1;
2430
    case WM_DDE_INITIATE:
2431 2432 2433
    case WM_DDE_TERMINATE:
    case WM_DDE_UNADVISE:
    case WM_DDE_REQUEST:
2434
	*pwparam16 = HWND_16((HWND)wParam32);
2435 2436 2437 2438 2439
	return 0;
    case WM_DDE_ADVISE:
    case WM_DDE_DATA:
    case WM_DDE_POKE:
        {
2440
	    UINT lo32, hi;
2441 2442
	    HANDLE16	lo16 = 0;

2443
	    *pwparam16 = HWND_16((HWND)wParam32);
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
            UnpackDDElParam(msg32, *plparam, &lo32, &hi);
	    if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
		return -1;
	    *plparam = MAKELPARAM(lo16, hi);
	}
	return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
    case WM_DDE_ACK:
        {
	    UINT	lo, hi;
	    int		flag = 0;
	    char	buf[2];

2456
	    *pwparam16 = HWND_16((HWND)wParam32);
2457 2458 2459 2460

            UnpackDDElParam(msg32, *plparam, &lo, &hi);

	    if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2461
	    if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2462 2463 2464
	    switch (flag)
	    {
	    case 0:
2465
		if (hi)
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
		{
		    MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
		    hi = 0;
		}
		break;
	    case 1:
		break; /* atom, nothing to do */
	    case 3:
		MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
		/* fall thru */
	    case 2:
		hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
		break;
	    }
	    *plparam = MAKELPARAM(lo, hi);
	}
	return 0; /* FIXME don't know how to free allocated memory (handle) !! */
    case WM_DDE_EXECUTE:
	*plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
        return 0; /* FIXME don't know how to free allocated memory (handle) !! */
Alexandre Julliard's avatar
Alexandre Julliard committed
2486
    default:  /* No translation needed */
Alexandre Julliard's avatar
Alexandre Julliard committed
2487
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2488 2489 2490 2491
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2492
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
2493
 *	     WINPROC_UnmapMsg32ATo16
Alexandre Julliard's avatar
Alexandre Julliard committed
2494
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
2495
 * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
Alexandre Julliard's avatar
Alexandre Julliard committed
2496
 */
2497
void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2498
                              MSGPARAM16* p16 )
Alexandre Julliard's avatar
Alexandre Julliard committed
2499 2500 2501
{
    switch(msg)
    {
2502 2503 2504 2505 2506
    case SBM_GETRANGE:
        *(LPINT)wParam = LOWORD(p16->lResult);
        *(LPINT)lParam = HIWORD(p16->lResult);
        break;

2507 2508 2509 2510 2511 2512 2513
    case LB_ADDFILE:
    case LB_ADDSTRING:
    case LB_DIR:
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_INSERTSTRING:
    case LB_SELECTSTRING:
2514
    case LB_GETTEXT:
2515 2516 2517 2518 2519 2520
    case CB_ADDSTRING:
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_INSERTSTRING:
    case CB_SELECTSTRING:
    case CB_DIR:
2521
    case CB_GETLBTEXT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2522
    case WM_SETTEXT:
2523 2524
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
        UnMapLS( (SEGPTR)p16->lParam );
        break;
    case LB_SETTABSTOPS:
    case WM_COMPAREITEM:
    case WM_DELETEITEM:
    case WM_DRAWITEM:
        {
            void *ptr = MapSL( p16->lParam );
            UnMapLS( p16->lParam );
            HeapFree( GetProcessHeap(), 0, ptr );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2536
        break;
2537 2538
    case CB_GETDROPPEDCONTROLRECT:
    case LB_GETITEMRECT:
Alexandre Julliard's avatar
Alexandre Julliard committed
2539
        {
2540
            RECT *r32;
2541
            RECT16 *rect = MapSL(p16->lParam);
2542
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2543
            p16->lParam = *(LPARAM *)(rect + 1);
2544 2545 2546 2547 2548
            r32 = (RECT *)p16->lParam;
            r32->left   = rect->left;
            r32->top    = rect->top;
            r32->right  = rect->right;
            r32->bottom = rect->bottom;
2549
            HeapFree( GetProcessHeap(), 0, rect );
Alexandre Julliard's avatar
Alexandre Julliard committed
2550 2551
        }
        break;
2552
    case LB_GETSELITEMS:
Alexandre Julliard's avatar
Alexandre Julliard committed
2553
        {
2554
            INT i;
2555 2556
            LPINT16 items = MapSL(p16->lParam);
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2557
            p16->lParam = *((LPARAM *)items - 1);
2558
            for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2559
            HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2560 2561
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2562

2563
    case CB_GETEDITSEL:
Alexandre Julliard's avatar
Alexandre Julliard committed
2564
	if( wParam )
2565
	    *((PUINT)(wParam)) = LOWORD(p16->lResult);
Alexandre Julliard's avatar
Alexandre Julliard committed
2566
	if( lParam )
2567
	    *((PUINT)(lParam)) = HIWORD(p16->lResult);	/* FIXME: substract 1? */
Alexandre Julliard's avatar
Alexandre Julliard committed
2568 2569
	break;

Alexandre Julliard's avatar
Alexandre Julliard committed
2570 2571
    case WM_MEASUREITEM:
        {
2572
            MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2573
            MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
2574 2575
            mis32->itemWidth  = mis->itemWidth;
            mis32->itemHeight = mis->itemHeight;
2576 2577
            UnMapLS( p16->lParam );
            HeapFree( GetProcessHeap(), 0, mis );
Alexandre Julliard's avatar
Alexandre Julliard committed
2578 2579
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2580 2581
    case WM_GETMINMAXINFO:
        {
2582
            MINMAXINFO16 *mmi = MapSL(p16->lParam);
2583
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2584
            p16->lParam = *(LPARAM *)(mmi + 1);
2585
            MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2586
            HeapFree( GetProcessHeap(), 0, mmi );
Alexandre Julliard's avatar
Alexandre Julliard committed
2587 2588
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2589
    case WM_GETTEXT:
2590
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
2591
        {
2592
            LPSTR str = MapSL(p16->lParam);
2593
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2594
            p16->lParam = *((LPARAM *)str - 1);
2595
            lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2596
            HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2597
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2598
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2599 2600
    case WM_MDICREATE:
        {
2601
            MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2602 2603 2604 2605
            UnMapLS( cs->szTitle );
            UnMapLS( cs->szClass );
            UnMapLS( p16->lParam );
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
2606 2607
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2608
    case WM_MDIGETACTIVE:
2609
        if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2610
        p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2611
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2612 2613
    case WM_NCCALCSIZE:
        {
2614
            NCCALCSIZE_PARAMS *nc32;
2615
            NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2616
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2617
            p16->lParam = *(LPARAM *)(nc + 1);
2618
            nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2619 2620 2621 2622
            nc32->rgrc[0].left   = nc->rgrc[0].left;
            nc32->rgrc[0].top    = nc->rgrc[0].top;
            nc32->rgrc[0].right  = nc->rgrc[0].right;
            nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
2623
            if (p16->wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
2624
            {
2625 2626
                WINDOWPOS16 *pos = MapSL(nc->lppos);
                UnMapLS( nc->lppos );
2627 2628 2629 2630 2631 2632 2633 2634
                nc32->rgrc[1].left   = nc->rgrc[1].left;
                nc32->rgrc[1].top    = nc->rgrc[1].top;
                nc32->rgrc[1].right  = nc->rgrc[1].right;
                nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
                nc32->rgrc[2].left   = nc->rgrc[2].left;
                nc32->rgrc[2].top    = nc->rgrc[2].top;
                nc32->rgrc[2].right  = nc->rgrc[2].right;
                nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2635
                WINDOWPOS16to32( pos, nc32->lppos );
2636
                HeapFree( GetProcessHeap(), 0, pos );
Alexandre Julliard's avatar
Alexandre Julliard committed
2637
            }
2638
            HeapFree( GetProcessHeap(), 0, nc );
Alexandre Julliard's avatar
Alexandre Julliard committed
2639 2640
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2641 2642 2643
    case WM_NCCREATE:
    case WM_CREATE:
        {
2644
            CREATESTRUCT16 *cs = MapSL(p16->lParam);
2645 2646 2647
            UnMapLS( p16->lParam );
            UnMapLS( cs->lpszName );
            UnMapLS( cs->lpszClass );
2648 2649 2650 2651 2652 2653 2654 2655
            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
                UnMapLS( cs->lpCreateParams );
                UnMapLS( mdi_cs16->szTitle );
                UnMapLS( mdi_cs16->szClass );
                HeapFree(GetProcessHeap(), 0, mdi_cs16);
            }
2656
            HeapFree( GetProcessHeap(), 0, cs );
Alexandre Julliard's avatar
Alexandre Julliard committed
2657 2658 2659 2660
        }
        break;
    case WM_WINDOWPOSCHANGING:
    case WM_WINDOWPOSCHANGED:
Alexandre Julliard's avatar
Alexandre Julliard committed
2661
        {
2662
            WINDOWPOS16 *wp = MapSL(p16->lParam);
2663
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2664
            p16->lParam = *(LPARAM *)(wp + 1);
2665
            WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2666
            HeapFree( GetProcessHeap(), 0, wp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2667
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2668
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2669 2670 2671
    case WM_NOTIFY:
	UnMapLS(p16->lParam);
	break;
2672 2673 2674
    case WM_GETDLGCODE:
        if (p16->lParam)
        {
2675
            LPMSG16 msg16 = MapSL(p16->lParam);
2676
            MSGPARAM16 msgp16;
2677
            UnMapLS( p16->lParam );
2678 2679
            msgp16.wParam=msg16->wParam;
            msgp16.lParam=msg16->lParam;
2680 2681
            WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
                    ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2682
                    &msgp16 );
2683
            HeapFree( GetProcessHeap(), 0, msg16 );
2684 2685
        }
        break;
2686 2687 2688
    case WM_NEXTMENU:
        {
            MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2689
            next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2690
            next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2691 2692 2693
            p16->lResult = 0;
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
    }
}


/**********************************************************************
 *	     WINPROC_MapMsg32WTo16
 *
 * Map a message from 32-bit Unicode to 16-bit.
 * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
 */
2704
INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
Alexandre Julliard's avatar
Alexandre Julliard committed
2705 2706
                             UINT16 *pmsg16, WPARAM16 *pwparam16,
                             LPARAM *plparam )
Alexandre Julliard's avatar
Alexandre Julliard committed
2707
{
2708
    BYTE ch;
2709 2710
    WCHAR wch;

2711 2712
    *pmsg16    = LOWORD(msg32);
    *pwparam16 = LOWORD(wParam32);
Alexandre Julliard's avatar
Alexandre Julliard committed
2713 2714
    switch(msg32)
    {
2715 2716 2717 2718 2719 2720 2721
    case LB_ADDSTRING:
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_INSERTSTRING:
    case LB_SELECTSTRING:
    case LB_DIR:
    case LB_ADDFILE:
2722
        *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2723
        *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
Alexandre Julliard's avatar
Alexandre Julliard committed
2724
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2725

2726 2727 2728 2729 2730 2731
    case CB_ADDSTRING:
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_INSERTSTRING:
    case CB_SELECTSTRING:
    case CB_DIR:
2732
        *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2733
        *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
Alexandre Julliard's avatar
Alexandre Julliard committed
2734 2735
        return 1;

Alexandre Julliard's avatar
Alexandre Julliard committed
2736 2737 2738 2739
    case WM_NCCREATE:
    case WM_CREATE:
        {
            CREATESTRUCT16 *cs;
2740
            CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
2741

2742
            if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2743
            CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2744 2745
            cs->lpszName  = map_str_32W_to_16( cs32->lpszName );
            cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16;
                MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
                mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
                if (!mdi_cs16)
                {
                    HeapFree(GetProcessHeap(), 0, cs);
                    return -1;
                }
2757
                MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2758 2759 2760 2761
                mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
                mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
                cs->lpCreateParams = MapLS(mdi_cs16);
            }
2762
            *plparam   = MapLS(cs);
Alexandre Julliard's avatar
Alexandre Julliard committed
2763 2764
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2765 2766 2767
    case WM_MDICREATE:
        {
            MDICREATESTRUCT16 *cs;
2768
            MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
Alexandre Julliard's avatar
Alexandre Julliard committed
2769

2770
            if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2771
            MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2772 2773 2774
            cs->szTitle = map_str_32W_to_16( cs32->szTitle );
            cs->szClass = map_str_32W_to_16( cs32->szClass );
            *plparam   = MapLS(cs);
Alexandre Julliard's avatar
Alexandre Julliard committed
2775 2776
        }
        return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
2777
    case WM_SETTEXT:
2778 2779 2780
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
        *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2781
        return 1;
2782 2783 2784
    case LB_GETTEXT:
        if ( WINPROC_TestLBForStr( hwnd ))
        {
2785
            LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2786
            if (!str) return -1;
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
            *pmsg16    = LB_GETTEXT16;
            *plparam   = (LPARAM)MapLS(str);
        }
        return 1;
    case CB_GETLBTEXT:
        if ( WINPROC_TestCBForStr( hwnd ))
        {
            LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
            if (!str) return -1;
            *pmsg16    = CB_GETLBTEXT16;
2797
            *plparam   = (LPARAM)MapLS(str);
2798 2799
        }
        return 1;
2800

2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
    case WM_CHARTOITEM:
        wch = LOWORD(wParam32);
        WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
        *pwparam16 = ch;
        *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
        return 0;
    case WM_MENUCHAR:
        wch = LOWORD(wParam32);
        WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
        *pwparam16 = ch;
        *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
        return 0;
2813 2814 2815 2816
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
2817 2818 2819
        wch = wParam32;
        WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
        *pwparam16 = ch;
2820
        return 0;
2821 2822 2823 2824 2825
    case WM_IME_CHAR:
        {
            BYTE ch[2];

            wch = wParam32;
2826 2827 2828 2829
            if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
                *pwparam16 = (ch[0] << 8) | ch[1];
            else
                *pwparam16 = ch[0];
2830 2831 2832
        }
        return 0;

2833
    default:  /* No Unicode translation needed (?) */
Alexandre Julliard's avatar
Alexandre Julliard committed
2834
        return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
Alexandre Julliard's avatar
Alexandre Julliard committed
2835
                                      pwparam16, plparam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2836 2837 2838 2839
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2840 2841 2842 2843 2844
/**********************************************************************
 *	     WINPROC_UnmapMsg32WTo16
 *
 * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
 */
2845
void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
2846
                              MSGPARAM16* p16 )
Alexandre Julliard's avatar
Alexandre Julliard committed
2847 2848 2849
{
    switch(msg)
    {
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
    case LB_ADDSTRING:
    case LB_FINDSTRING:
    case LB_FINDSTRINGEXACT:
    case LB_INSERTSTRING:
    case LB_SELECTSTRING:
    case LB_DIR:
    case LB_ADDFILE:
    case CB_ADDSTRING:
    case CB_FINDSTRING:
    case CB_FINDSTRINGEXACT:
    case CB_INSERTSTRING:
    case CB_SELECTSTRING:
    case CB_DIR:
    case WM_SETTEXT:
    case WM_WININICHANGE:
    case WM_DEVMODECHANGE:
        unmap_str_32W_to_16( p16->lParam );
        break;
    case WM_NCCREATE:
    case WM_CREATE:
        {
            CREATESTRUCT16 *cs = MapSL(p16->lParam);
            UnMapLS( p16->lParam );
            unmap_str_32W_to_16( cs->lpszName );
            unmap_str_32W_to_16( cs->lpszClass );
2875 2876 2877 2878 2879 2880 2881 2882 2883

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
                UnMapLS( cs->lpCreateParams );
                unmap_str_32W_to_16(mdi_cs16->szTitle);
                unmap_str_32W_to_16(mdi_cs16->szClass);
                HeapFree(GetProcessHeap(), 0, mdi_cs16);
            }
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
            HeapFree( GetProcessHeap(), 0, cs );
        }
        break;
    case WM_MDICREATE:
        {
            MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
            UnMapLS( p16->lParam );
            unmap_str_32W_to_16( cs->szTitle );
            unmap_str_32W_to_16( cs->szClass );
            HeapFree( GetProcessHeap(), 0, cs );
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2896
    case WM_GETTEXT:
2897
    case WM_ASKCBFORMATNAME:
Alexandre Julliard's avatar
Alexandre Julliard committed
2898
        {
2899
            LPSTR str = MapSL(p16->lParam);
2900
            UnMapLS( p16->lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
2901
            p16->lParam = *((LPARAM *)str - 1);
2902
            MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2903
            p16->lResult = strlenW( (LPWSTR)p16->lParam );
2904
            HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2905 2906
        }
        break;
2907 2908 2909
    case LB_GETTEXT:
        if ( WINPROC_TestLBForStr( hwnd ))
        {
2910
            LPSTR str = MapSL(p16->lParam);
2911
            UnMapLS( p16->lParam );
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
            p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
            HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
        }
        break;
    case CB_GETLBTEXT:
        if ( WINPROC_TestCBForStr( hwnd ))
        {
            LPSTR str = MapSL(p16->lParam);
            UnMapLS( p16->lParam );
            p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2922
            HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2923 2924
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2925
    default:
2926
        WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
        break;
    }
}


/**********************************************************************
 *	     WINPROC_CallProc32ATo32W
 *
 * Call a window procedure, translating args from Ansi to Unicode.
 */
2937 2938
static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
                                         UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
2939 2940 2941
                                         LPARAM lParam )
{
    LRESULT result;
2942
    int unmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
2943

2944
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2945 2946
	func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);

2947 2948
    if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
        ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2949
                       SPY_GetMsgName(msg, hwnd), wParam, lParam );
2950 2951
        return 0;
    }
2952
    result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2953
    if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
2954 2955 2956 2957
    return result;
}


2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
/**********************************************************************
 *	     WINPROC_CallProc32WTo32A_fast
 *
 */
static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
                                           UINT msg, WPARAM wParam,
                                           LPARAM lParam, LRESULT *result )
{
    switch(msg)
    {
    case WM_NCCREATE:
    case WM_CREATE:
        {   /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
             * at this point.
             */
            char buffer[1024];
            char *cls = buffer, *name;
            CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
            CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
            DWORD name_lenA, name_lenW, class_lenA, class_lenW;

            class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
            RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);

            if (csW->lpszName)
            {
                name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
                RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
            }
            else
                name_lenW = name_lenA = 0;

            if (class_lenA + name_lenA + 2 > sizeof(buffer))
            {
                cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
                if (!cls) return FALSE;
            }

            RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
            cls[class_lenA] = 0;
            csA.lpszClass = cls;

            if (csW->lpszName)
            {
                name = cls + class_lenA + 1;
                RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
                name[name_lenA] = 0;
                csA.lpszName = name;
            }

            if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
            {
                MDICREATESTRUCTA mdi_cs;

                mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
                mdi_cs.szTitle = csA.lpszName;
                mdi_cs.szClass = csA.lpszClass;
                csA.lpCreateParams = &mdi_cs;
            }

            lParam = (LPARAM)&csA;
            *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);

            if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
        }
        return TRUE;

    default:
        return FALSE;
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
3030 3031 3032 3033 3034
/**********************************************************************
 *	     WINPROC_CallProc32WTo32A
 *
 * Call a window procedure, translating args from Unicode to Ansi.
 */
3035 3036
static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
                                         UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
3037 3038 3039
                                         LPARAM lParam )
{
    LRESULT result;
3040
    int unmap;
Alexandre Julliard's avatar
Alexandre Julliard committed
3041

3042
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3043 3044
	func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);

3045 3046 3047
    if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
        return result;

3048 3049
    if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
        ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3050
                       SPY_GetMsgName(msg, hwnd), wParam, lParam );
3051 3052
        return 0;
    }
3053
    result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3054
    if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
3055 3056 3057 3058 3059
    return result;
}


/**********************************************************************
3060
 *	     __wine_call_wndproc_32A   (USER.1010)
Alexandre Julliard's avatar
Alexandre Julliard committed
3061
 */
3062 3063
LRESULT WINAPI __wine_call_wndproc_32A( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
                                        WNDPROC func )
Alexandre Julliard's avatar
Alexandre Julliard committed
3064 3065
{
    LRESULT result;
3066 3067
    UINT msg32;
    WPARAM wParam32;
3068
    HWND hwnd32 = WIN_Handle32( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
3069

3070 3071 3072
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
                 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);

3073
    if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3074
        return 0;
3075 3076
    result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
    return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
3077 3078 3079 3080
}


/**********************************************************************
3081
 *	     __wine_call_wndproc_32W   (USER.1011)
Alexandre Julliard's avatar
Alexandre Julliard committed
3082
 */
3083 3084
LRESULT WINAPI  __wine_call_wndproc_32W( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
                                         WNDPROC func )
Alexandre Julliard's avatar
Alexandre Julliard committed
3085 3086
{
    LRESULT result;
3087 3088
    UINT msg32;
    WPARAM wParam32;
3089
    HWND hwnd32 = WIN_Handle32( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
3090

3091 3092 3093
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
                 func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);

3094
    if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3095
        return 0;
3096 3097
    result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
    return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result );
Alexandre Julliard's avatar
Alexandre Julliard committed
3098 3099 3100 3101 3102 3103 3104 3105
}


/**********************************************************************
 *	     WINPROC_CallProc32ATo16
 *
 * Call a 16-bit window procedure, translating the 32-bit args.
 */
3106 3107
static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
                                               UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
3108
                                               LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3109 3110
{
    UINT16 msg16;
Alexandre Julliard's avatar
Alexandre Julliard committed
3111
    MSGPARAM16 mp16;
Alexandre Julliard's avatar
Alexandre Julliard committed
3112

3113
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3114 3115
	func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);

Alexandre Julliard's avatar
Alexandre Julliard committed
3116
    mp16.lParam = lParam;
3117
    if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3118
        return 0;
3119
    mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3120
                                          mp16.wParam, mp16.lParam );
3121
    WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
3122
    return mp16.lResult;
Alexandre Julliard's avatar
Alexandre Julliard committed
3123 3124 3125 3126 3127 3128 3129 3130
}


/**********************************************************************
 *	     WINPROC_CallProc32WTo16
 *
 * Call a 16-bit window procedure, translating the 32-bit args.
 */
3131 3132
static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
                                               UINT msg, WPARAM wParam,
Alexandre Julliard's avatar
Alexandre Julliard committed
3133
                                               LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3134 3135
{
    UINT16 msg16;
Alexandre Julliard's avatar
Alexandre Julliard committed
3136
    MSGPARAM16 mp16;
Alexandre Julliard's avatar
Alexandre Julliard committed
3137

3138
    TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3139 3140
	func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);

Alexandre Julliard's avatar
Alexandre Julliard committed
3141
    mp16.lParam = lParam;
Alexandre Julliard's avatar
Alexandre Julliard committed
3142 3143
    if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
                               &mp16.lParam ) == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
3144
        return 0;
3145
    mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3146
                                          mp16.wParam, mp16.lParam );
3147
    WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
Alexandre Julliard's avatar
Alexandre Julliard committed
3148
    return mp16.lResult;
Alexandre Julliard's avatar
Alexandre Julliard committed
3149 3150 3151
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3152
/**********************************************************************
3153
 *		CallWindowProc (USER.122)
Alexandre Julliard's avatar
Alexandre Julliard committed
3154
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
3155 3156
LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
                                 WPARAM16 wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3157
{
3158 3159 3160
    WINDOWPROC *proc;

    if (!func) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3161

3162
    if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3163
        return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3164

Alexandre Julliard's avatar
Alexandre Julliard committed
3165
#if testing
3166
    func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_16 );
3167
    return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3168
#endif
3169

Alexandre Julliard's avatar
Alexandre Julliard committed
3170 3171 3172
    switch(proc->type)
    {
    case WIN_PROC_16:
Alexandre Julliard's avatar
Alexandre Julliard committed
3173
        if (!proc->thunk.t_from32.proc) return 0;
3174 3175
        return WINPROC_CallWndProc16( proc->thunk.t_from32.proc,
                                      hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3176
    case WIN_PROC_32A:
Alexandre Julliard's avatar
Alexandre Julliard committed
3177
        if (!proc->thunk.t_from16.proc) return 0;
3178
        return __wine_call_wndproc_32A( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
Alexandre Julliard's avatar
Alexandre Julliard committed
3179
    case WIN_PROC_32W:
Alexandre Julliard's avatar
Alexandre Julliard committed
3180
        if (!proc->thunk.t_from16.proc) return 0;
3181
        return __wine_call_wndproc_32W( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
Alexandre Julliard's avatar
Alexandre Julliard committed
3182
    default:
3183
        WARN_(relay)("Invalid proc %p\n", proc );
Alexandre Julliard's avatar
Alexandre Julliard committed
3184
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3185 3186 3187 3188 3189
    }
}


/**********************************************************************
3190
 *		CallWindowProcA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
 *
 * The CallWindowProc() function invokes the windows procedure _func_,
 * with _hwnd_ as the target window, the message specified by _msg_, and
 * the message parameters _wParam_ and _lParam_.
 *
 * Some kinds of argument conversion may be done, I'm not sure what.
 *
 * CallWindowProc() may be used for windows subclassing. Use
 * SetWindowLong() to set a new windows procedure for windows of the
 * subclass, and handle subclassed messages in the new windows
 * procedure. The new windows procedure may then use CallWindowProc()
 * with _func_ set to the parent class's windows procedure to dispatch
 * the message to the superclass.
 *
 * RETURNS
 *
 *    The return value is message dependent.
 *
 * CONFORMANCE
 *
3211
 *   ECMA-234, Win32
Alexandre Julliard's avatar
Alexandre Julliard committed
3212
 */
3213
LRESULT WINAPI CallWindowProcA(
3214 3215 3216 3217 3218
    WNDPROC func,  /* [in] window procedure */
    HWND hwnd,     /* [in] target window */
    UINT msg,      /* [in] message */
    WPARAM wParam, /* [in] message dependent parameter */
    LPARAM lParam  /* [in] message dependent parameter */
Alexandre Julliard's avatar
Alexandre Julliard committed
3219
) {
3220 3221 3222
    WINDOWPROC *proc;

    if (!func) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3223

3224 3225
    if (!(proc = WINPROC_GetPtr( func )))
        return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3226

Alexandre Julliard's avatar
Alexandre Julliard committed
3227
#if testing
3228
    func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32A );
3229
    return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3230 3231
#endif

Alexandre Julliard's avatar
Alexandre Julliard committed
3232
    switch(proc->type)
Alexandre Julliard's avatar
Alexandre Julliard committed
3233
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
3234
    case WIN_PROC_16:
Alexandre Julliard's avatar
Alexandre Julliard committed
3235 3236 3237
        if (!proc->thunk.t_from32.proc) return 0;
        return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
                                        hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3238
    case WIN_PROC_32A:
Alexandre Julliard's avatar
Alexandre Julliard committed
3239
        if (!proc->thunk.t_from16.proc) return 0;
3240
        return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
Alexandre Julliard's avatar
Alexandre Julliard committed
3241
                                      hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3242
    case WIN_PROC_32W:
Alexandre Julliard's avatar
Alexandre Julliard committed
3243 3244
        if (!proc->thunk.t_from16.proc) return 0;
        return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
Alexandre Julliard's avatar
Alexandre Julliard committed
3245
                                         hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3246
    default:
3247
        WARN_(relay)("Invalid proc %p\n", proc );
Alexandre Julliard's avatar
Alexandre Julliard committed
3248 3249 3250 3251 3252 3253
        return 0;
    }
}


/**********************************************************************
3254
 *		CallWindowProcW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3255
 */
3256 3257
LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
                                  WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3258
{
3259
    WINDOWPROC *proc;
Alexandre Julliard's avatar
Alexandre Julliard committed
3260

3261 3262 3263 3264
    if (!func) return 0;

    if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
        return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3265

Alexandre Julliard's avatar
Alexandre Julliard committed
3266
#if testing
3267
    func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32W );
3268
    return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3269 3270
#endif

Alexandre Julliard's avatar
Alexandre Julliard committed
3271
    switch(proc->type)
Alexandre Julliard's avatar
Alexandre Julliard committed
3272 3273
    {
    case WIN_PROC_16:
Alexandre Julliard's avatar
Alexandre Julliard committed
3274 3275 3276
        if (!proc->thunk.t_from32.proc) return 0;
        return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
                                        hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3277
    case WIN_PROC_32A:
Alexandre Julliard's avatar
Alexandre Julliard committed
3278 3279
        if (!proc->thunk.t_from16.proc) return 0;
        return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
Alexandre Julliard's avatar
Alexandre Julliard committed
3280
                                         hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3281
    case WIN_PROC_32W:
Alexandre Julliard's avatar
Alexandre Julliard committed
3282
        if (!proc->thunk.t_from16.proc) return 0;
3283
        return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
Alexandre Julliard's avatar
Alexandre Julliard committed
3284
                                      hwnd, msg, wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
3285
    default:
3286
        WARN_(relay)("Invalid proc %p\n", proc );
Alexandre Julliard's avatar
Alexandre Julliard committed
3287 3288
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3289
}