wrc: Remove non-bison compatibility code.
[wine.git] / dlls / user32 / winproc.c
blobebccbe40f3c77013da1d7ca7a61524f6aa53fb66
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 { NULL, DesktopWndProc }, /* WINPROC_DESKTOP */
79 { NULL, IconTitleWndProc }, /* WINPROC_ICONTITLE */
80 { NULL, PopupMenuWndProc }, /* WINPROC_MENU */
81 { NULL, MessageWndProc }, /* WINPROC_MESSAGE */
84 static UINT winproc_used = NB_BUILTIN_WINPROCS;
86 static CRITICAL_SECTION winproc_cs;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
89 0, 0, &winproc_cs,
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": winproc_cs") }
93 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
95 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
97 if (size >= need) return static_buffer;
98 return HeapAlloc( GetProcessHeap(), 0, need );
101 static inline void free_buffer( void *static_buffer, void *buffer )
103 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
106 /* find an existing winproc for a given function and type */
107 /* FIXME: probably should do something more clever than a linear search */
108 static inline WINDOWPROC *find_winproc( WNDPROC func, BOOL unicode )
110 unsigned int i;
112 for (i = 0; i < NB_BUILTIN_AW_WINPROCS; i++)
114 /* match either proc, some apps confuse A and W */
115 if (winproc_array[i].procA != func && winproc_array[i].procW != func) continue;
116 return &winproc_array[i];
118 for (i = NB_BUILTIN_AW_WINPROCS; i < winproc_used; i++)
120 if (!unicode && winproc_array[i].procA != func) continue;
121 if (unicode && winproc_array[i].procW != func) continue;
122 return &winproc_array[i];
124 return NULL;
127 /* return the window proc for a given handle, or NULL for an invalid handle,
128 * or WINPROC_PROC16 for a handle to a 16-bit proc. */
129 static inline WINDOWPROC *handle_to_proc( WNDPROC handle )
131 UINT index = LOWORD(handle);
132 if ((ULONG_PTR)handle >> 16 != WINPROC_HANDLE) return NULL;
133 if (index >= MAX_WINPROCS) return WINPROC_PROC16;
134 if (index >= winproc_used) return NULL;
135 return &winproc_array[index];
138 /* create a handle for a given window proc */
139 static inline WNDPROC proc_to_handle( WINDOWPROC *proc )
141 return (WNDPROC)(ULONG_PTR)((proc - winproc_array) | (WINPROC_HANDLE << 16));
144 /* allocate and initialize a new winproc */
145 static inline WINDOWPROC *alloc_winproc( WNDPROC func, BOOL unicode )
147 WINDOWPROC *proc;
149 /* check if the function is already a win proc */
150 if (!func) return NULL;
151 if ((proc = handle_to_proc( func ))) return proc;
153 EnterCriticalSection( &winproc_cs );
155 /* check if we already have a winproc for that function */
156 if (!(proc = find_winproc( func, unicode )))
158 if (winproc_used < MAX_WINPROCS)
160 proc = &winproc_array[winproc_used++];
161 if (unicode) proc->procW = func;
162 else proc->procA = func;
163 TRACE( "allocated %p for %c %p (%d/%d used)\n",
164 proc_to_handle(proc), unicode ? 'W' : 'A', func,
165 winproc_used, MAX_WINPROCS );
167 else FIXME( "too many winprocs, cannot allocate one for %p\n", func );
169 else TRACE( "reusing %p for %p\n", proc_to_handle(proc), func );
171 LeaveCriticalSection( &winproc_cs );
172 return proc;
175 #ifdef __i386__
176 /* Some window procedures modify register they shouldn't, or are not
177 * properly declared stdcall; so we need a small assembly wrapper to
178 * call them. */
179 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
180 WPARAM wParam, LPARAM lParam );
181 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
182 "pushl %ebp\n\t"
183 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
184 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
185 "movl %esp,%ebp\n\t"
186 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
187 "pushl %edi\n\t"
188 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
189 "pushl %esi\n\t"
190 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
191 "pushl %ebx\n\t"
192 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
193 "subl $12,%esp\n\t"
194 "pushl 24(%ebp)\n\t"
195 "pushl 20(%ebp)\n\t"
196 "pushl 16(%ebp)\n\t"
197 "pushl 12(%ebp)\n\t"
198 "movl 8(%ebp),%eax\n\t"
199 "call *%eax\n\t"
200 "leal -12(%ebp),%esp\n\t"
201 "popl %ebx\n\t"
202 __ASM_CFI(".cfi_same_value %ebx\n\t")
203 "popl %esi\n\t"
204 __ASM_CFI(".cfi_same_value %esi\n\t")
205 "popl %edi\n\t"
206 __ASM_CFI(".cfi_same_value %edi\n\t")
207 "leave\n\t"
208 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
209 __ASM_CFI(".cfi_same_value %ebp\n\t")
210 "ret" )
211 #else
212 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
213 WPARAM wParam, LPARAM lParam )
215 return proc( hwnd, msg, wParam, lParam );
217 #endif /* __i386__ */
219 static WPARAM map_wparam_char_WtoA( WPARAM wParam, DWORD len )
221 WCHAR wch = wParam;
222 BYTE ch[2];
223 DWORD cp = get_input_codepage();
225 len = WideCharToMultiByte( cp, 0, &wch, 1, (LPSTR)ch, len, NULL, NULL );
226 if (len == 2)
227 return MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wParam) );
228 else
229 return MAKEWPARAM( ch[0], HIWORD(wParam) );
232 /* call a 32-bit window procedure */
233 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
235 WNDPROC proc = arg;
237 USER_CheckNotLock();
239 hwnd = WIN_GetFullHandle( hwnd );
240 if (TRACE_ON(relay))
241 DPRINTF( "%04x:Call window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
242 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
244 *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
246 if (TRACE_ON(relay))
247 DPRINTF( "%04x:Ret window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx\n",
248 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
249 return *result;
252 /* call a 32-bit dialog procedure */
253 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
255 WNDPROC proc = arg;
256 LRESULT ret;
258 USER_CheckNotLock();
260 hwnd = WIN_GetFullHandle( hwnd );
261 if (TRACE_ON(relay))
262 DPRINTF( "%04x:Call dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
263 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
265 ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
266 *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
268 if (TRACE_ON(relay))
269 DPRINTF( "%04x:Ret dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx result=%08lx\n",
270 GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
271 return ret;
275 /**********************************************************************
276 * WINPROC_GetProc
278 * Get a window procedure pointer that can be passed to the Windows program.
280 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
282 WINDOWPROC *ptr = handle_to_proc( proc );
284 if (!ptr || ptr == WINPROC_PROC16) return proc;
285 if (unicode)
287 if (ptr->procW) return ptr->procW;
288 return proc;
290 else
292 if (ptr->procA) return ptr->procA;
293 return proc;
298 /**********************************************************************
299 * WINPROC_AllocProc
301 * Allocate a window procedure for a window or class.
303 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
304 * lot of windows, it will usually only have a limited number of window procedures, so the
305 * array won't grow too large, and this way we avoid the need to track allocations per window.
307 WNDPROC WINPROC_AllocProc( WNDPROC func, BOOL unicode )
309 WINDOWPROC *proc;
311 if (!(proc = alloc_winproc( func, unicode ))) return NULL;
312 if (proc == WINPROC_PROC16) return func;
313 return proc_to_handle( proc );
317 /**********************************************************************
318 * WINPROC_IsUnicode
320 * Return the window procedure type, or the default value if not a winproc handle.
322 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
324 WINDOWPROC *ptr = handle_to_proc( proc );
326 if (!ptr) return def_val;
327 if (ptr == WINPROC_PROC16) return FALSE; /* 16-bit is always A */
328 if (ptr->procA && ptr->procW) return def_val; /* can be both */
329 return (ptr->procW != NULL);
333 /**********************************************************************
334 * WINPROC_TestLBForStr
336 * Return TRUE if the lparam is a string
338 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
340 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
341 if (msg <= CB_MSGMAX)
342 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
343 else
344 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
349 /**********************************************************************
350 * WINPROC_CallProcAtoW
352 * Call a window procedure, translating args from Ansi to Unicode.
354 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
355 LPARAM lParam, LRESULT *result, void *arg, enum wm_char_mapping mapping )
357 LRESULT ret = 0;
359 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
360 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
362 switch(msg)
364 case WM_NCCREATE:
365 case WM_CREATE:
367 WCHAR *ptr, buffer[512];
368 CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
369 CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
370 MDICREATESTRUCTW mdi_cs;
371 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
373 if (!IS_INTRESOURCE(csA->lpszClass))
375 class_lenA = strlen(csA->lpszClass) + 1;
376 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
378 if (!IS_INTRESOURCE(csA->lpszName))
380 name_lenA = strlen(csA->lpszName) + 1;
381 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
384 if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
386 if (class_lenW)
388 csW.lpszClass = ptr;
389 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
391 if (name_lenW)
393 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
394 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
395 csA->lpszName, name_lenA );
398 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
400 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
401 mdi_cs.szTitle = csW.lpszName;
402 mdi_cs.szClass = csW.lpszClass;
403 csW.lpCreateParams = &mdi_cs;
406 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
407 free_buffer( buffer, ptr );
409 break;
411 case WM_MDICREATE:
413 WCHAR *ptr, buffer[512];
414 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
415 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
416 MDICREATESTRUCTW csW;
418 memcpy( &csW, csA, sizeof(csW) );
420 if (!IS_INTRESOURCE(csA->szTitle))
422 title_lenA = strlen(csA->szTitle) + 1;
423 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
425 if (!IS_INTRESOURCE(csA->szClass))
427 class_lenA = strlen(csA->szClass) + 1;
428 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
431 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
433 if (title_lenW)
435 csW.szTitle = ptr;
436 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
438 if (class_lenW)
440 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
441 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
442 csA->szClass, class_lenA );
444 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
445 free_buffer( buffer, ptr );
447 break;
449 case WM_GETTEXT:
450 case WM_ASKCBFORMATNAME:
452 WCHAR *ptr, buffer[512];
453 LPSTR str = (LPSTR)lParam;
454 DWORD len = wParam * sizeof(WCHAR);
456 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
457 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
458 if (wParam)
460 len = 0;
461 if (*result)
462 RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, strlenW(ptr) * sizeof(WCHAR) );
463 str[len] = 0;
464 *result = len;
466 free_buffer( buffer, ptr );
468 break;
470 case LB_ADDSTRING:
471 case LB_INSERTSTRING:
472 case LB_FINDSTRING:
473 case LB_FINDSTRINGEXACT:
474 case LB_SELECTSTRING:
475 case CB_ADDSTRING:
476 case CB_INSERTSTRING:
477 case CB_FINDSTRING:
478 case CB_FINDSTRINGEXACT:
479 case CB_SELECTSTRING:
480 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
482 ret = callback( hwnd, msg, wParam, lParam, result, arg );
483 break;
485 /* fall through */
486 case WM_SETTEXT:
487 case WM_WININICHANGE:
488 case WM_DEVMODECHANGE:
489 case CB_DIR:
490 case LB_DIR:
491 case LB_ADDFILE:
492 case EM_REPLACESEL:
493 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
494 else
496 WCHAR *ptr, buffer[512];
497 LPCSTR strA = (LPCSTR)lParam;
498 DWORD lenW, lenA = strlen(strA) + 1;
500 RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
501 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
503 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
504 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
505 free_buffer( buffer, ptr );
508 break;
510 case LB_GETTEXT:
511 case CB_GETLBTEXT:
512 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
514 WCHAR buffer[512]; /* FIXME: fixed sized buffer */
516 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
517 if (*result >= 0)
519 DWORD len;
520 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
521 buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
522 *result = len - 1;
525 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
526 break;
528 case EM_GETLINE:
530 WCHAR *ptr, buffer[512];
531 WORD len = *(WORD *)lParam;
533 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
534 *((WORD *)ptr) = len; /* store the length */
535 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
536 if (*result)
538 DWORD reslen;
539 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
540 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
541 *result = reslen;
543 free_buffer( buffer, ptr );
545 break;
547 case WM_GETDLGCODE:
548 if (lParam)
550 MSG newmsg = *(MSG *)lParam;
551 if (map_wparam_AtoW( newmsg.message, &newmsg.wParam, WMCHAR_MAP_NOMAPPING ))
552 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
554 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
555 break;
557 case WM_CHARTOITEM:
558 case WM_MENUCHAR:
559 case WM_CHAR:
560 case WM_DEADCHAR:
561 case WM_SYSCHAR:
562 case WM_SYSDEADCHAR:
563 case EM_SETPASSWORDCHAR:
564 case WM_IME_CHAR:
565 if (map_wparam_AtoW( msg, &wParam, mapping ))
566 ret = callback( hwnd, msg, wParam, lParam, result, arg );
567 break;
569 case WM_GETTEXTLENGTH:
570 case CB_GETLBTEXTLEN:
571 case LB_GETTEXTLEN:
572 ret = callback( hwnd, msg, wParam, lParam, result, arg );
573 if (*result >= 0)
575 WCHAR *ptr, buffer[512];
576 LRESULT tmp;
577 DWORD len = *result + 1;
578 /* Determine respective GETTEXT message */
579 UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
580 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
581 /* wParam differs between the messages */
582 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
584 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
586 if (callback == call_window_proc) /* FIXME: hack */
587 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
588 else
589 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
590 RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
591 *result = len;
592 free_buffer( buffer, ptr );
594 break;
596 case WM_PAINTCLIPBOARD:
597 case WM_SIZECLIPBOARD:
598 FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
599 SPY_GetMsgName(msg, hwnd), msg );
600 break;
602 default:
603 ret = callback( hwnd, msg, wParam, lParam, result, arg );
604 break;
606 return ret;
610 /**********************************************************************
611 * WINPROC_CallProcWtoA
613 * Call a window procedure, translating args from Unicode to Ansi.
615 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
616 LPARAM lParam, LRESULT *result, void *arg )
618 LRESULT ret = 0;
620 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
621 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
623 switch(msg)
625 case WM_NCCREATE:
626 case WM_CREATE:
628 char buffer[1024], *cls;
629 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
630 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
631 MDICREATESTRUCTA mdi_cs;
632 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
634 if (!IS_INTRESOURCE(csW->lpszClass))
636 class_lenW = (strlenW(csW->lpszClass) + 1) * sizeof(WCHAR);
637 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
639 if (!IS_INTRESOURCE(csW->lpszName))
641 name_lenW = (strlenW(csW->lpszName) + 1) * sizeof(WCHAR);
642 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
645 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA ))) break;
647 if (class_lenA)
649 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
650 csA.lpszClass = cls;
652 if (name_lenA)
654 char *name = cls + class_lenA;
655 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
656 csA.lpszName = name;
659 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
661 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
662 mdi_cs.szTitle = csA.lpszName;
663 mdi_cs.szClass = csA.lpszClass;
664 csA.lpCreateParams = &mdi_cs;
667 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
668 free_buffer( buffer, cls );
670 break;
672 case WM_GETTEXT:
673 case WM_ASKCBFORMATNAME:
675 char *ptr, buffer[512];
676 DWORD len = wParam * 2;
678 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
679 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
680 if (len)
682 if (*result)
684 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, strlen(ptr)+1 );
685 *result = len/sizeof(WCHAR) - 1; /* do not count terminating null */
687 ((LPWSTR)lParam)[*result] = 0;
689 free_buffer( buffer, ptr );
691 break;
693 case LB_ADDSTRING:
694 case LB_INSERTSTRING:
695 case LB_FINDSTRING:
696 case LB_FINDSTRINGEXACT:
697 case LB_SELECTSTRING:
698 case CB_ADDSTRING:
699 case CB_INSERTSTRING:
700 case CB_FINDSTRING:
701 case CB_FINDSTRINGEXACT:
702 case CB_SELECTSTRING:
703 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
705 ret = callback( hwnd, msg, wParam, lParam, result, arg );
706 break;
708 /* fall through */
709 case WM_SETTEXT:
710 case WM_WININICHANGE:
711 case WM_DEVMODECHANGE:
712 case CB_DIR:
713 case LB_DIR:
714 case LB_ADDFILE:
715 case EM_REPLACESEL:
716 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
717 else
719 char *ptr, buffer[512];
720 LPCWSTR strW = (LPCWSTR)lParam;
721 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
723 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
724 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
726 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
727 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
728 free_buffer( buffer, ptr );
731 break;
733 case WM_MDICREATE:
735 char *ptr, buffer[1024];
736 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
737 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
738 MDICREATESTRUCTA csA;
740 memcpy( &csA, csW, sizeof(csA) );
742 if (!IS_INTRESOURCE(csW->szTitle))
744 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
745 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
747 if (!IS_INTRESOURCE(csW->szClass))
749 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
750 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
753 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
755 if (title_lenA)
757 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
758 csA.szTitle = ptr;
760 if (class_lenA)
762 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
763 csA.szClass = ptr + title_lenA;
765 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
766 free_buffer( buffer, ptr );
768 break;
770 case LB_GETTEXT:
771 case CB_GETLBTEXT:
772 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
774 char buffer[512]; /* FIXME: fixed sized buffer */
776 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
777 if (*result >= 0)
779 DWORD len;
780 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
781 *result = len / sizeof(WCHAR) - 1;
784 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
785 break;
787 case EM_GETLINE:
789 char *ptr, buffer[512];
790 WORD len = *(WORD *)lParam;
792 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
793 *((WORD *)ptr) = len * 2; /* store the length */
794 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
795 if (*result)
797 DWORD reslen;
798 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
799 *result = reslen / sizeof(WCHAR);
800 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
802 free_buffer( buffer, ptr );
804 break;
806 case WM_GETDLGCODE:
807 if (lParam)
809 MSG newmsg = *(MSG *)lParam;
810 switch(newmsg.message)
812 case WM_CHAR:
813 case WM_DEADCHAR:
814 case WM_SYSCHAR:
815 case WM_SYSDEADCHAR:
816 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
817 break;
818 case WM_IME_CHAR:
819 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
820 break;
822 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
824 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
825 break;
827 case WM_CHAR:
829 WCHAR wch = wParam;
830 char ch[2];
831 DWORD cp = get_input_codepage();
832 DWORD len = WideCharToMultiByte( cp, 0, &wch, 1, ch, 2, NULL, NULL );
833 ret = callback( hwnd, msg, (BYTE)ch[0], lParam, result, arg );
834 if (len == 2) ret = callback( hwnd, msg, (BYTE)ch[1], lParam, result, arg );
836 break;
838 case WM_CHARTOITEM:
839 case WM_MENUCHAR:
840 case WM_DEADCHAR:
841 case WM_SYSCHAR:
842 case WM_SYSDEADCHAR:
843 case EM_SETPASSWORDCHAR:
844 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
845 break;
847 case WM_IME_CHAR:
848 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
849 break;
851 case WM_PAINTCLIPBOARD:
852 case WM_SIZECLIPBOARD:
853 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
854 SPY_GetMsgName(msg, hwnd), msg );
855 break;
857 default:
858 ret = callback( hwnd, msg, wParam, lParam, result, arg );
859 break;
862 return ret;
866 /**********************************************************************
867 * WINPROC_call_window
869 * Call the window procedure of the specified window.
871 BOOL WINPROC_call_window( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
872 LRESULT *result, BOOL unicode, enum wm_char_mapping mapping )
874 struct user_thread_info *thread_info = get_user_thread_info();
875 WND *wndPtr;
876 WNDPROC func;
877 WINDOWPROC *proc;
879 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
880 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
881 if (wndPtr->tid != GetCurrentThreadId())
883 WIN_ReleasePtr( wndPtr );
884 return FALSE;
886 func = wndPtr->winproc;
887 proc = handle_to_proc( wndPtr->winproc );
888 WIN_ReleasePtr( wndPtr );
890 if (!proc) return TRUE;
892 if (thread_info->recursion_count > MAX_WINPROC_RECURSION) return FALSE;
893 thread_info->recursion_count++;
895 if (unicode)
897 if (proc == WINPROC_PROC16)
898 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, result, func );
899 else if (proc->procW)
900 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procW );
901 else
902 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procA );
904 else
906 if (proc == WINPROC_PROC16)
907 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, result, func );
908 else if (proc->procA)
909 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procA );
910 else
911 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procW, mapping );
913 thread_info->recursion_count--;
914 return TRUE;
918 /**********************************************************************
919 * CallWindowProcA (USER32.@)
921 * The CallWindowProc() function invokes the windows procedure _func_,
922 * with _hwnd_ as the target window, the message specified by _msg_, and
923 * the message parameters _wParam_ and _lParam_.
925 * Some kinds of argument conversion may be done, I'm not sure what.
927 * CallWindowProc() may be used for windows subclassing. Use
928 * SetWindowLong() to set a new windows procedure for windows of the
929 * subclass, and handle subclassed messages in the new windows
930 * procedure. The new windows procedure may then use CallWindowProc()
931 * with _func_ set to the parent class's windows procedure to dispatch
932 * the message to the superclass.
934 * RETURNS
936 * The return value is message dependent.
938 * CONFORMANCE
940 * ECMA-234, Win32
942 LRESULT WINAPI CallWindowProcA(
943 WNDPROC func, /* [in] window procedure */
944 HWND hwnd, /* [in] target window */
945 UINT msg, /* [in] message */
946 WPARAM wParam, /* [in] message dependent parameter */
947 LPARAM lParam /* [in] message dependent parameter */
949 WINDOWPROC *proc;
950 LRESULT result;
952 if (!func) return 0;
954 if (!(proc = handle_to_proc( func )))
955 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
956 else if (proc == WINPROC_PROC16)
957 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, &result, func );
958 else if (proc->procA)
959 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
960 else
961 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result,
962 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
963 return result;
967 /**********************************************************************
968 * CallWindowProcW (USER32.@)
970 * See CallWindowProcA.
972 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
973 WPARAM wParam, LPARAM lParam )
975 WINDOWPROC *proc;
976 LRESULT result;
978 if (!func) return 0;
980 if (!(proc = handle_to_proc( func )))
981 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
982 else if (proc == WINPROC_PROC16)
983 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, &result, func );
984 else if (proc->procW)
985 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
986 else
987 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
988 return result;
992 /**********************************************************************
993 * WINPROC_CallDlgProcA
995 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
997 WINDOWPROC *proc;
998 LRESULT result;
999 INT_PTR ret;
1001 if (!func) return 0;
1003 if (!(proc = handle_to_proc( func )))
1004 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1005 else if (proc == WINPROC_PROC16)
1007 ret = wow_handlers.call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1008 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1010 else if (proc->procW)
1012 ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result,
1013 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
1014 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1016 else
1017 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
1018 return ret;
1022 /**********************************************************************
1023 * WINPROC_CallDlgProcW
1025 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1027 WINDOWPROC *proc;
1028 LRESULT result;
1029 INT_PTR ret;
1031 if (!func) return 0;
1033 if (!(proc = handle_to_proc( func )))
1034 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1035 else if (proc == WINPROC_PROC16)
1037 ret = WINPROC_CallProcWtoA( wow_handlers.call_dialog_proc, hwnd, msg, wParam, lParam, &result, func );
1038 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1040 else if (proc->procA)
1042 ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1043 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1045 else
1046 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
1047 return ret;
1051 /***********************************************************************
1052 * Window procedures for builtin classes
1055 static LRESULT WINAPI ButtonWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1057 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, FALSE );
1060 static LRESULT WINAPI ButtonWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1062 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, TRUE );
1065 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1067 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, FALSE );
1070 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1072 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, TRUE );
1075 LRESULT WINAPI EditWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1077 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, FALSE );
1080 static LRESULT WINAPI EditWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1082 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, TRUE );
1085 static LRESULT WINAPI ListBoxWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1087 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, FALSE );
1090 static LRESULT WINAPI ListBoxWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1092 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, TRUE );
1095 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1097 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, FALSE );
1100 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1102 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, TRUE );
1105 static LRESULT WINAPI ScrollBarWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1107 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, FALSE );
1110 static LRESULT WINAPI ScrollBarWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1112 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, TRUE );
1115 static LRESULT WINAPI StaticWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1117 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, FALSE );
1120 static LRESULT WINAPI StaticWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1122 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, TRUE );
1125 static DWORD wait_message( DWORD count, CONST HANDLE *handles, DWORD timeout, DWORD mask, DWORD flags )
1127 DWORD ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
1128 if (ret == WAIT_TIMEOUT && !count && !timeout) NtYieldExecution();
1129 if ((mask & QS_INPUT) == QS_INPUT) get_user_thread_info()->message_count = 0;
1130 return ret;
1133 /**********************************************************************
1134 * UserRegisterWowHandlers (USER32.@)
1136 * NOTE: no attempt has been made to be compatible here,
1137 * the Windows function is most likely completely different.
1139 void WINAPI UserRegisterWowHandlers( const struct wow_handlers16 *new, struct wow_handlers32 *orig )
1141 orig->button_proc = ButtonWndProc_common;
1142 orig->combo_proc = ComboWndProc_common;
1143 orig->edit_proc = EditWndProc_common;
1144 orig->listbox_proc = ListBoxWndProc_common;
1145 orig->mdiclient_proc = MDIClientWndProc_common;
1146 orig->scrollbar_proc = ScrollBarWndProc_common;
1147 orig->static_proc = StaticWndProc_common;
1148 orig->wait_message = wait_message;
1149 orig->create_window = WIN_CreateWindowEx;
1150 orig->get_win_handle = WIN_GetFullHandle;
1151 orig->alloc_winproc = WINPROC_AllocProc;
1152 orig->get_dialog_info = DIALOG_get_info;
1153 orig->dialog_box_loop = DIALOG_DoDialogBox;
1154 orig->get_icon_param = get_icon_param;
1155 orig->set_icon_param = set_icon_param;
1157 wow_handlers = *new;
1160 struct wow_handlers16 wow_handlers =
1162 ButtonWndProc_common,
1163 ComboWndProc_common,
1164 EditWndProc_common,
1165 ListBoxWndProc_common,
1166 MDIClientWndProc_common,
1167 ScrollBarWndProc_common,
1168 StaticWndProc_common,
1169 wait_message,
1170 WIN_CreateWindowEx,
1171 NULL, /* call_window_proc */
1172 NULL, /* call_dialog_proc */
1173 NULL, /* free_icon_param */