ntdll: Add stub for RtlSetHeapInformation.
[wine.git] / dlls / user32 / winproc.c
blob364210984b3fdeae7faf44e1e83486205dc8e65b
1 /*
2 * Window procedure callbacks
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1996 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "controls.h"
33 #include "win.h"
34 #include "user_private.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DECLARE_DEBUG_CHANNEL(msg);
39 WINE_DECLARE_DEBUG_CHANNEL(relay);
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 typedef struct tagWINDOWPROC
44 WNDPROC procA; /* ASCII window proc */
45 WNDPROC procW; /* Unicode window proc */
46 } WINDOWPROC;
48 #define MAX_WINPROCS 4096
49 #define MAX_WINPROC_RECURSION 64
50 #define WINPROC_PROC16 ((WINDOWPROC *)1) /* placeholder for 16-bit window procs */
52 static LRESULT WINAPI ButtonWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
53 static LRESULT WINAPI ButtonWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
54 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
55 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
56 LRESULT WINAPI EditWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
57 static LRESULT WINAPI EditWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
58 static LRESULT WINAPI ListBoxWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
59 static LRESULT WINAPI ListBoxWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
60 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
61 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
62 static LRESULT WINAPI ScrollBarWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
63 static LRESULT WINAPI ScrollBarWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
64 static LRESULT WINAPI StaticWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
65 static LRESULT WINAPI StaticWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
67 static WINDOWPROC winproc_array[MAX_WINPROCS] =
69 { ButtonWndProcA, ButtonWndProcW }, /* WINPROC_BUTTON */
70 { ComboWndProcA, ComboWndProcW }, /* WINPROC_COMBO */
71 { DefWindowProcA, DefWindowProcW }, /* WINPROC_DEFWND */
72 { DefDlgProcA, DefDlgProcW }, /* WINPROC_DIALOG */
73 { EditWndProcA, EditWndProcW }, /* WINPROC_EDIT */
74 { ListBoxWndProcA, ListBoxWndProcW }, /* WINPROC_LISTBOX */
75 { MDIClientWndProcA, MDIClientWndProcW }, /* WINPROC_MDICLIENT */
76 { ScrollBarWndProcA, ScrollBarWndProcW }, /* WINPROC_SCROLLBAR */
77 { StaticWndProcA, StaticWndProcW }, /* WINPROC_STATIC */
78 { ImeWndProcA, ImeWndProcW }, /* WINPROC_IME */
79 { NULL, DesktopWndProc }, /* WINPROC_DESKTOP */
80 { NULL, IconTitleWndProc }, /* WINPROC_ICONTITLE */
81 { NULL, PopupMenuWndProc }, /* WINPROC_MENU */
82 { NULL, MessageWndProc }, /* WINPROC_MESSAGE */
85 static UINT winproc_used = NB_BUILTIN_WINPROCS;
87 static CRITICAL_SECTION winproc_cs;
88 static CRITICAL_SECTION_DEBUG critsect_debug =
90 0, 0, &winproc_cs,
91 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
92 0, 0, { (DWORD_PTR)(__FILE__ ": winproc_cs") }
94 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
96 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
98 if (size >= need) return static_buffer;
99 return HeapAlloc( GetProcessHeap(), 0, need );
102 static inline void free_buffer( void *static_buffer, void *buffer )
104 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
107 /* find an existing winproc for a given function and type */
108 /* FIXME: probably should do something more clever than a linear search */
109 static inline WINDOWPROC *find_winproc( WNDPROC func, BOOL unicode )
111 unsigned int i;
113 for (i = 0; i < NB_BUILTIN_AW_WINPROCS; i++)
115 /* match either proc, some apps confuse A and W */
116 if (winproc_array[i].procA != func && winproc_array[i].procW != func) continue;
117 return &winproc_array[i];
119 for (i = NB_BUILTIN_AW_WINPROCS; i < winproc_used; i++)
121 if (!unicode && winproc_array[i].procA != func) continue;
122 if (unicode && winproc_array[i].procW != func) continue;
123 return &winproc_array[i];
125 return NULL;
128 /* return the window proc for a given handle, or NULL for an invalid handle,
129 * or WINPROC_PROC16 for a handle to a 16-bit proc. */
130 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
132 UINT index = LOWORD(handle);
133 if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
134 if (index >= MAX_WINPROCS) return WINPROC_PROC16;
135 if (index >= winproc_used) return NULL;
136 return &winproc_array[index];
139 /* create a handle for a given window proc */
140 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
142 return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
145 /* allocate and initialize a new winproc */
146 static inline WINDOWPROC *alloc_winproc( WNDPROC func, BOOL unicode )
148 WINDOWPROC *proc;
150 /* check if the function is already a win proc */
151 if (!func) return NULL;
152 if ((proc = handle_to_proc( func ))) return proc;
154 EnterCriticalSection( &winproc_cs );
156 /* check if we already have a winproc for that function */
157 if (!(proc = find_winproc( func, unicode )))
159 if (winproc_used < MAX_WINPROCS)
161 proc = &winproc_array[winproc_used++];
162 if (unicode) proc->procW = func;
163 else proc->procA = func;
164 TRACE( "allocated %p for %c %p (%d/%d used)\n",
165 proc_to_handle(proc), unicode ? 'W' : 'A', func,
166 winproc_used, MAX_WINPROCS );
168 else FIXME( "too many winprocs, cannot allocate one for %p\n", func );
170 else TRACE( "reusing %p for %p\n", proc_to_handle(proc), func );
172 LeaveCriticalSection( &winproc_cs );
173 return proc;
176 #ifdef __i386__
177 /* Some window procedures modify register they shouldn't, or are not
178 * properly declared stdcall; so we need a small assembly wrapper to
179 * call them. */
180 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
181 WPARAM wParam, LPARAM lParam );
182 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
183 "pushl %ebp\n\t"
184 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
185 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
186 "movl %esp,%ebp\n\t"
187 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
188 "pushl %edi\n\t"
189 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
190 "pushl %esi\n\t"
191 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
192 "pushl %ebx\n\t"
193 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
194 "subl $12,%esp\n\t"
195 "pushl 24(%ebp)\n\t"
196 "pushl 20(%ebp)\n\t"
197 "pushl 16(%ebp)\n\t"
198 "pushl 12(%ebp)\n\t"
199 "movl 8(%ebp),%eax\n\t"
200 "call *%eax\n\t"
201 "leal -12(%ebp),%esp\n\t"
202 "popl %ebx\n\t"
203 __ASM_CFI(".cfi_same_value %ebx\n\t")
204 "popl %esi\n\t"
205 __ASM_CFI(".cfi_same_value %esi\n\t")
206 "popl %edi\n\t"
207 __ASM_CFI(".cfi_same_value %edi\n\t")
208 "leave\n\t"
209 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
210 __ASM_CFI(".cfi_same_value %ebp\n\t")
211 "ret" )
212 #else
213 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
214 WPARAM wParam, LPARAM lParam )
216 return proc( hwnd, msg, wParam, lParam );
218 #endif /* __i386__ */
220 static WPARAM map_wparam_char_WtoA( WPARAM wParam, DWORD len )
222 WCHAR wch = wParam;
223 BYTE ch[2];
224 DWORD cp = get_input_codepage();
226 len = WideCharToMultiByte( cp, 0, &wch, 1, (LPSTR)ch, len, NULL, NULL );
227 if (len == 2)
228 return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
229 else
230 return MAKEWPARAM( ch[0], HIWORD(wParam) );
233 /* call a 32-bit window procedure */
234 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
236 WNDPROC proc = arg;
238 USER_CheckNotLock();
240 hwnd = WIN_GetFullHandle( hwnd );
241 if (TRACE_ON(relay))
242 DPRINTF( "%04x:Call window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
243 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
245 *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
247 if (TRACE_ON(relay))
248 DPRINTF( "%04x:Ret window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx\n",
249 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
250 return *result;
253 /* call a 32-bit dialog procedure */
254 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
256 WNDPROC proc = arg;
257 LRESULT ret;
259 USER_CheckNotLock();
261 hwnd = WIN_GetFullHandle( hwnd );
262 if (TRACE_ON(relay))
263 DPRINTF( "%04x:Call dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
264 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
266 ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
267 *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
269 if (TRACE_ON(relay))
270 DPRINTF( "%04x:Ret dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx result=%08lx\n",
271 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
272 return ret;
276 /**********************************************************************
277 * WINPROC_GetProc
279 * Get a window procedure pointer that can be passed to the Windows program.
281 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
283 WINDOWPROC *ptr = handle_to_proc( proc );
285 if (!ptr || ptr == WINPROC_PROC16) return proc;
286 if (unicode)
288 if (ptr->procW) return ptr->procW;
289 return proc;
291 else
293 if (ptr->procA) return ptr->procA;
294 return proc;
299 /**********************************************************************
300 * WINPROC_AllocProc
302 * Allocate a window procedure for a window or class.
304 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
305 * lot of windows, it will usually only have a limited number of window procedures, so the
306 * array won't grow too large, and this way we avoid the need to track allocations per window.
308 WNDPROC WINPROC_AllocProc( WNDPROC func, BOOL unicode )
310 WINDOWPROC *proc;
312 if (!(proc = alloc_winproc( func, unicode ))) return NULL;
313 if (proc == WINPROC_PROC16) return func;
314 return proc_to_handle( proc );
318 /**********************************************************************
319 * WINPROC_IsUnicode
321 * Return the window procedure type, or the default value if not a winproc handle.
323 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
325 WINDOWPROC *ptr = handle_to_proc( proc );
327 if (!ptr) return def_val;
328 if (ptr == WINPROC_PROC16) return FALSE; /* 16-bit is always A */
329 if (ptr->procA && ptr->procW) return def_val; /* can be both */
330 return (ptr->procW != NULL);
334 /**********************************************************************
335 * WINPROC_TestLBForStr
337 * Return TRUE if the lparam is a string
339 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
341 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
342 if (msg <= CB_MSGMAX)
343 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
344 else
345 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
350 /**********************************************************************
351 * WINPROC_CallProcAtoW
353 * Call a window procedure, translating args from Ansi to Unicode.
355 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
356 LPARAM lParam, LRESULT *result, void *arg, enum wm_char_mapping mapping )
358 LRESULT ret = 0;
360 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
361 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
363 switch(msg)
365 case WM_NCCREATE:
366 case WM_CREATE:
368 WCHAR *ptr, buffer[512];
369 CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
370 CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
371 MDICREATESTRUCTW mdi_cs;
372 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
374 if (!IS_INTRESOURCE(csA->lpszClass))
376 class_lenA = strlen(csA->lpszClass) + 1;
377 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
379 if (!IS_INTRESOURCE(csA->lpszName))
381 name_lenA = strlen(csA->lpszName) + 1;
382 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
385 if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
387 if (class_lenW)
389 csW.lpszClass = ptr;
390 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
392 if (name_lenW)
394 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
395 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
396 csA->lpszName, name_lenA );
399 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
401 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
402 mdi_cs.szTitle = csW.lpszName;
403 mdi_cs.szClass = csW.lpszClass;
404 csW.lpCreateParams = &mdi_cs;
407 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
408 free_buffer( buffer, ptr );
410 break;
412 case WM_MDICREATE:
414 WCHAR *ptr, buffer[512];
415 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
416 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
417 MDICREATESTRUCTW csW;
419 memcpy( &csW, csA, sizeof(csW) );
421 if (!IS_INTRESOURCE(csA->szTitle))
423 title_lenA = strlen(csA->szTitle) + 1;
424 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
426 if (!IS_INTRESOURCE(csA->szClass))
428 class_lenA = strlen(csA->szClass) + 1;
429 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
432 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
434 if (title_lenW)
436 csW.szTitle = ptr;
437 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
439 if (class_lenW)
441 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
442 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
443 csA->szClass, class_lenA );
445 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
446 free_buffer( buffer, ptr );
448 break;
450 case WM_GETTEXT:
451 case WM_ASKCBFORMATNAME:
453 WCHAR *ptr, buffer[512];
454 LPSTR str = (LPSTR)lParam;
455 DWORD len = wParam * sizeof(WCHAR);
457 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
458 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
459 if (wParam)
461 len = 0;
462 if (*result)
463 RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, strlenW(ptr) * sizeof(WCHAR) );
464 str[len] = 0;
465 *result = len;
467 free_buffer( buffer, ptr );
469 break;
471 case LB_ADDSTRING:
472 case LB_INSERTSTRING:
473 case LB_FINDSTRING:
474 case LB_FINDSTRINGEXACT:
475 case LB_SELECTSTRING:
476 case CB_ADDSTRING:
477 case CB_INSERTSTRING:
478 case CB_FINDSTRING:
479 case CB_FINDSTRINGEXACT:
480 case CB_SELECTSTRING:
481 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
483 ret = callback( hwnd, msg, wParam, lParam, result, arg );
484 break;
486 /* fall through */
487 case WM_SETTEXT:
488 case WM_WININICHANGE:
489 case WM_DEVMODECHANGE:
490 case CB_DIR:
491 case LB_DIR:
492 case LB_ADDFILE:
493 case EM_REPLACESEL:
494 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
495 else
497 WCHAR *ptr, buffer[512];
498 LPCSTR strA = (LPCSTR)lParam;
499 DWORD lenW, lenA = strlen(strA) + 1;
501 RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
502 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
504 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
505 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
506 free_buffer( buffer, ptr );
509 break;
511 case LB_GETTEXT:
512 case CB_GETLBTEXT:
513 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
515 WCHAR buffer[512]; /* FIXME: fixed sized buffer */
517 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
518 if (*result >= 0)
520 DWORD len;
521 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
522 buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
523 *result = len - 1;
526 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
527 break;
529 case EM_GETLINE:
531 WCHAR *ptr, buffer[512];
532 WORD len = *(WORD *)lParam;
534 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
535 *((WORD *)ptr) = len; /* store the length */
536 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
537 if (*result)
539 DWORD reslen;
540 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
541 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
542 *result = reslen;
544 free_buffer( buffer, ptr );
546 break;
548 case WM_GETDLGCODE:
549 if (lParam)
551 MSG newmsg = *(MSG *)lParam;
552 if (map_wparam_AtoW( newmsg.message, &newmsg.wParam, WMCHAR_MAP_NOMAPPING ))
553 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
555 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
556 break;
558 case WM_CHARTOITEM:
559 case WM_MENUCHAR:
560 case WM_CHAR:
561 case WM_DEADCHAR:
562 case WM_SYSCHAR:
563 case WM_SYSDEADCHAR:
564 case EM_SETPASSWORDCHAR:
565 case WM_IME_CHAR:
566 if (map_wparam_AtoW( msg, &wParam, mapping ))
567 ret = callback( hwnd, msg, wParam, lParam, result, arg );
568 break;
570 case WM_GETTEXTLENGTH:
571 case CB_GETLBTEXTLEN:
572 case LB_GETTEXTLEN:
573 ret = callback( hwnd, msg, wParam, lParam, result, arg );
574 if (*result >= 0)
576 WCHAR *ptr, buffer[512];
577 LRESULT tmp;
578 DWORD len = *result + 1;
579 /* Determine respective GETTEXT message */
580 UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
581 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
582 /* wParam differs between the messages */
583 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
585 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
587 if (callback == call_window_proc) /* FIXME: hack */
588 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
589 else
590 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
591 RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
592 *result = len;
593 free_buffer( buffer, ptr );
595 break;
597 case WM_PAINTCLIPBOARD:
598 case WM_SIZECLIPBOARD:
599 FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
600 SPY_GetMsgName(msg, hwnd), msg );
601 break;
603 default:
604 ret = callback( hwnd, msg, wParam, lParam, result, arg );
605 break;
607 return ret;
611 /**********************************************************************
612 * WINPROC_CallProcWtoA
614 * Call a window procedure, translating args from Unicode to Ansi.
616 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
617 LPARAM lParam, LRESULT *result, void *arg )
619 LRESULT ret = 0;
621 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
622 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
624 switch(msg)
626 case WM_NCCREATE:
627 case WM_CREATE:
629 char buffer[1024], *cls;
630 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
631 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
632 MDICREATESTRUCTA mdi_cs;
633 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
635 if (!IS_INTRESOURCE(csW->lpszClass))
637 class_lenW = (strlenW(csW->lpszClass) + 1) * sizeof(WCHAR);
638 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
640 if (!IS_INTRESOURCE(csW->lpszName))
642 name_lenW = (strlenW(csW->lpszName) + 1) * sizeof(WCHAR);
643 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
646 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA ))) break;
648 if (class_lenA)
650 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
651 csA.lpszClass = cls;
653 if (name_lenA)
655 char *name = cls + class_lenA;
656 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
657 csA.lpszName = name;
660 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
662 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
663 mdi_cs.szTitle = csA.lpszName;
664 mdi_cs.szClass = csA.lpszClass;
665 csA.lpCreateParams = &mdi_cs;
668 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
669 free_buffer( buffer, cls );
671 break;
673 case WM_GETTEXT:
674 case WM_ASKCBFORMATNAME:
676 char *ptr, buffer[512];
677 DWORD len = wParam * 2;
679 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
680 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
681 if (len)
683 if (*result)
685 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
686 *result = len/sizeof(WCHAR) - 1; /* do not count terminating null */
688 ((LPWSTR)lParam)[*result] = 0;
690 free_buffer( buffer, ptr );
692 break;
694 case LB_ADDSTRING:
695 case LB_INSERTSTRING:
696 case LB_FINDSTRING:
697 case LB_FINDSTRINGEXACT:
698 case LB_SELECTSTRING:
699 case CB_ADDSTRING:
700 case CB_INSERTSTRING:
701 case CB_FINDSTRING:
702 case CB_FINDSTRINGEXACT:
703 case CB_SELECTSTRING:
704 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
706 ret = callback( hwnd, msg, wParam, lParam, result, arg );
707 break;
709 /* fall through */
710 case WM_SETTEXT:
711 case WM_WININICHANGE:
712 case WM_DEVMODECHANGE:
713 case CB_DIR:
714 case LB_DIR:
715 case LB_ADDFILE:
716 case EM_REPLACESEL:
717 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
718 else
720 char *ptr, buffer[512];
721 LPCWSTR strW = (LPCWSTR)lParam;
722 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
724 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
725 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
727 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
728 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
729 free_buffer( buffer, ptr );
732 break;
734 case WM_MDICREATE:
736 char *ptr, buffer[1024];
737 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
738 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
739 MDICREATESTRUCTA csA;
741 memcpy( &csA, csW, sizeof(csA) );
743 if (!IS_INTRESOURCE(csW->szTitle))
745 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
746 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
748 if (!IS_INTRESOURCE(csW->szClass))
750 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
751 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
754 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
756 if (title_lenA)
758 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
759 csA.szTitle = ptr;
761 if (class_lenA)
763 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
764 csA.szClass = ptr + title_lenA;
766 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
767 free_buffer( buffer, ptr );
769 break;
771 case LB_GETTEXT:
772 case CB_GETLBTEXT:
773 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
775 char buffer[512]; /* FIXME: fixed sized buffer */
777 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
778 if (*result >= 0)
780 DWORD len;
781 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
782 *result = len / sizeof(WCHAR) - 1;
785 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
786 break;
788 case EM_GETLINE:
790 char *ptr, buffer[512];
791 WORD len = *(WORD *)lParam;
793 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
794 *((WORD *)ptr) = len * 2; /* store the length */
795 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
796 if (*result)
798 DWORD reslen;
799 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
800 *result = reslen / sizeof(WCHAR);
801 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
803 free_buffer( buffer, ptr );
805 break;
807 case WM_GETDLGCODE:
808 if (lParam)
810 MSG newmsg = *(MSG *)lParam;
811 switch(newmsg.message)
813 case WM_CHAR:
814 case WM_DEADCHAR:
815 case WM_SYSCHAR:
816 case WM_SYSDEADCHAR:
817 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
818 break;
819 case WM_IME_CHAR:
820 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
821 break;
823 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
825 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
826 break;
828 case WM_CHAR:
830 WCHAR wch = wParam;
831 char ch[2];
832 DWORD cp = get_input_codepage();
833 DWORD len = WideCharToMultiByte( cp, 0, &wch, 1, ch, 2, NULL, NULL );
834 ret = callback( hwnd, msg, (BYTE)ch[0], lParam, result, arg );
835 if (len == 2) ret = callback( hwnd, msg, (BYTE)ch[1], lParam, result, arg );
837 break;
839 case WM_CHARTOITEM:
840 case WM_MENUCHAR:
841 case WM_DEADCHAR:
842 case WM_SYSCHAR:
843 case WM_SYSDEADCHAR:
844 case EM_SETPASSWORDCHAR:
845 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
846 break;
848 case WM_IME_CHAR:
849 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
850 break;
852 case WM_PAINTCLIPBOARD:
853 case WM_SIZECLIPBOARD:
854 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
855 SPY_GetMsgName(msg, hwnd), msg );
856 break;
858 default:
859 ret = callback( hwnd, msg, wParam, lParam, result, arg );
860 break;
863 return ret;
867 /**********************************************************************
868 * WINPROC_call_window
870 * Call the window procedure of the specified window.
872 BOOL WINPROC_call_window( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
873 LRESULT *result, BOOL unicode, enum wm_char_mapping mapping )
875 struct user_thread_info *thread_info = get_user_thread_info();
876 WND *wndPtr;
877 WNDPROC func;
878 WINDOWPROC *proc;
880 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
881 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
882 if (wndPtr->tid != GetCurrentThreadId())
884 WIN_ReleasePtr( wndPtr );
885 return FALSE;
887 func = wndPtr->winproc;
888 proc = handle_to_proc( wndPtr->winproc );
889 WIN_ReleasePtr( wndPtr );
891 if (!proc) return TRUE;
893 if (thread_info->recursion_count > MAX_WINPROC_RECURSION) return FALSE;
894 thread_info->recursion_count++;
896 if (unicode)
898 if (proc == WINPROC_PROC16)
899 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, result, func );
900 else if (proc->procW)
901 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procW );
902 else
903 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procA );
905 else
907 if (proc == WINPROC_PROC16)
908 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, result, func );
909 else if (proc->procA)
910 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procA );
911 else
912 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procW, mapping );
914 thread_info->recursion_count--;
915 return TRUE;
919 /**********************************************************************
920 * CallWindowProcA (USER32.@)
922 * The CallWindowProc() function invokes the windows procedure _func_,
923 * with _hwnd_ as the target window, the message specified by _msg_, and
924 * the message parameters _wParam_ and _lParam_.
926 * Some kinds of argument conversion may be done, I'm not sure what.
928 * CallWindowProc() may be used for windows subclassing. Use
929 * SetWindowLong() to set a new windows procedure for windows of the
930 * subclass, and handle subclassed messages in the new windows
931 * procedure. The new windows procedure may then use CallWindowProc()
932 * with _func_ set to the parent class's windows procedure to dispatch
933 * the message to the superclass.
935 * RETURNS
937 * The return value is message dependent.
939 * CONFORMANCE
941 * ECMA-234, Win32
943 LRESULT WINAPI CallWindowProcA(
944 WNDPROC func, /* [in] window procedure */
945 HWND hwnd, /* [in] target window */
946 UINT msg, /* [in] message */
947 WPARAM wParam, /* [in] message dependent parameter */
948 LPARAM lParam /* [in] message dependent parameter */
950 WINDOWPROC *proc;
951 LRESULT result;
953 if (!func) return 0;
955 if (!(proc = handle_to_proc( func )))
956 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
957 else if (proc == WINPROC_PROC16)
958 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, &result, func );
959 else if (proc->procA)
960 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
961 else
962 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result,
963 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
964 return result;
968 /**********************************************************************
969 * CallWindowProcW (USER32.@)
971 * See CallWindowProcA.
973 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
974 WPARAM wParam, LPARAM lParam )
976 WINDOWPROC *proc;
977 LRESULT result;
979 if (!func) return 0;
981 if (!(proc = handle_to_proc( func )))
982 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
983 else if (proc == WINPROC_PROC16)
984 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, &result, func );
985 else if (proc->procW)
986 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
987 else
988 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
989 return result;
993 /**********************************************************************
994 * WINPROC_CallDlgProcA
996 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
998 WINDOWPROC *proc;
999 LRESULT result;
1000 INT_PTR ret;
1002 if (!func) return 0;
1004 if (!(proc = handle_to_proc( func )))
1005 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1006 else if (proc == WINPROC_PROC16)
1008 ret = wow_handlers.call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1009 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1011 else if (proc->procW)
1013 ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result,
1014 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
1015 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1017 else
1018 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
1019 return ret;
1023 /**********************************************************************
1024 * WINPROC_CallDlgProcW
1026 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1028 WINDOWPROC *proc;
1029 LRESULT result;
1030 INT_PTR ret;
1032 if (!func) return 0;
1034 if (!(proc = handle_to_proc( func )))
1035 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1036 else if (proc == WINPROC_PROC16)
1038 ret = WINPROC_CallProcWtoA( wow_handlers.call_dialog_proc, hwnd, msg, wParam, lParam, &result, func );
1039 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1041 else if (proc->procA)
1043 ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1044 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1046 else
1047 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
1048 return ret;
1052 /***********************************************************************
1053 * Window procedures for builtin classes
1056 static LRESULT WINAPI ButtonWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1058 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, FALSE );
1061 static LRESULT WINAPI ButtonWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1063 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, TRUE );
1066 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1068 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, FALSE );
1071 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1073 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, TRUE );
1076 LRESULT WINAPI EditWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1078 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, FALSE );
1081 static LRESULT WINAPI EditWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1083 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, TRUE );
1086 static LRESULT WINAPI ListBoxWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1088 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, FALSE );
1091 static LRESULT WINAPI ListBoxWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1093 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, TRUE );
1096 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1098 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, FALSE );
1101 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1103 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, TRUE );
1106 static LRESULT WINAPI ScrollBarWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1108 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, FALSE );
1111 static LRESULT WINAPI ScrollBarWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1113 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, TRUE );
1116 static LRESULT WINAPI StaticWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1118 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, FALSE );
1121 static LRESULT WINAPI StaticWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1123 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, TRUE );
1126 static DWORD wait_message( DWORD count, const HANDLE *handles, DWORD timeout, DWORD mask, DWORD flags )
1128 DWORD ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
1129 if (ret == WAIT_TIMEOUT && !count && !timeout) NtYieldExecution();
1130 if ((mask & QS_INPUT) == QS_INPUT) get_user_thread_info()->message_count = 0;
1131 return ret;
1134 /**********************************************************************
1135 * UserRegisterWowHandlers (USER32.@)
1137 * NOTE: no attempt has been made to be compatible here,
1138 * the Windows function is most likely completely different.
1140 void WINAPI UserRegisterWowHandlers( const struct wow_handlers16 *new, struct wow_handlers32 *orig )
1142 orig->button_proc = ButtonWndProc_common;
1143 orig->combo_proc = ComboWndProc_common;
1144 orig->edit_proc = EditWndProc_common;
1145 orig->listbox_proc = ListBoxWndProc_common;
1146 orig->mdiclient_proc = MDIClientWndProc_common;
1147 orig->scrollbar_proc = ScrollBarWndProc_common;
1148 orig->static_proc = StaticWndProc_common;
1149 orig->wait_message = wait_message;
1150 orig->create_window = WIN_CreateWindowEx;
1151 orig->get_win_handle = WIN_GetFullHandle;
1152 orig->alloc_winproc = WINPROC_AllocProc;
1153 orig->get_dialog_info = DIALOG_get_info;
1154 orig->dialog_box_loop = DIALOG_DoDialogBox;
1155 orig->get_icon_param = get_icon_param;
1156 orig->set_icon_param = set_icon_param;
1158 wow_handlers = *new;
1161 struct wow_handlers16 wow_handlers =
1163 ButtonWndProc_common,
1164 ComboWndProc_common,
1165 EditWndProc_common,
1166 ListBoxWndProc_common,
1167 MDIClientWndProc_common,
1168 ScrollBarWndProc_common,
1169 StaticWndProc_common,
1170 wait_message,
1171 WIN_CreateWindowEx,
1172 NULL, /* call_window_proc */
1173 NULL, /* call_dialog_proc */
1174 NULL, /* free_icon_param */