comctl32: Fix a typo in comment.
[wine.git] / dlls / user32 / winproc.c
blob73bbe95028e607cb1239b929abc84f2edc416d4f
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 WARN( "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 TRACE_(relay)( "\1Call window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
242 proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
244 *result = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
246 TRACE_(relay)( "\1Ret window proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx\n",
247 proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, *result );
248 return *result;
251 /* call a 32-bit dialog procedure */
252 static LRESULT call_dialog_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result, void *arg )
254 WNDPROC proc = arg;
255 LRESULT ret;
257 USER_CheckNotLock();
259 hwnd = WIN_GetFullHandle( hwnd );
260 TRACE_(relay)( "\1Call dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
261 proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp );
263 ret = WINPROC_wrapper( proc, hwnd, msg, wp, lp );
264 *result = GetWindowLongPtrW( hwnd, DWLP_MSGRESULT );
266 TRACE_(relay)( "\1Ret dialog proc %p (hwnd=%p,msg=%s,wp=%08lx,lp=%08lx) retval=%08lx result=%08lx\n",
267 proc, hwnd, SPY_GetMsgName(msg, hwnd), wp, lp, ret, *result );
268 return ret;
272 /**********************************************************************
273 * WINPROC_GetProc
275 * Get a window procedure pointer that can be passed to the Windows program.
277 WNDPROC WINPROC_GetProc( WNDPROC proc, BOOL unicode )
279 WINDOWPROC *ptr = handle_to_proc( proc );
281 if (!ptr || ptr == WINPROC_PROC16) return proc;
282 if (unicode)
284 if (ptr->procW) return ptr->procW;
285 return proc;
287 else
289 if (ptr->procA) return ptr->procA;
290 return proc;
295 /**********************************************************************
296 * WINPROC_AllocProc
298 * Allocate a window procedure for a window or class.
300 * Note that allocated winprocs are never freed; the idea is that even if an app creates a
301 * lot of windows, it will usually only have a limited number of window procedures, so the
302 * array won't grow too large, and this way we avoid the need to track allocations per window.
304 WNDPROC WINPROC_AllocProc( WNDPROC func, BOOL unicode )
306 WINDOWPROC *proc;
308 if (!(proc = alloc_winproc( func, unicode ))) return func;
309 if (proc == WINPROC_PROC16) return func;
310 return proc_to_handle( proc );
314 /**********************************************************************
315 * WINPROC_IsUnicode
317 * Return the window procedure type, or the default value if not a winproc handle.
319 BOOL WINPROC_IsUnicode( WNDPROC proc, BOOL def_val )
321 WINDOWPROC *ptr = handle_to_proc( proc );
323 if (!ptr) return def_val;
324 if (ptr == WINPROC_PROC16) return FALSE; /* 16-bit is always A */
325 if (ptr->procA && ptr->procW) return def_val; /* can be both */
326 return (ptr->procW != NULL);
330 /**********************************************************************
331 * WINPROC_TestLBForStr
333 * Return TRUE if the lparam is a string
335 static inline BOOL WINPROC_TestLBForStr( HWND hwnd, UINT msg )
337 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
338 if (msg <= CB_MSGMAX)
339 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
340 else
341 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
346 /**********************************************************************
347 * WINPROC_CallProcAtoW
349 * Call a window procedure, translating args from Ansi to Unicode.
351 LRESULT WINPROC_CallProcAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
352 LPARAM lParam, LRESULT *result, void *arg, enum wm_char_mapping mapping )
354 LRESULT ret = 0;
356 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
357 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
359 switch(msg)
361 case WM_NCCREATE:
362 case WM_CREATE:
364 WCHAR *ptr, buffer[512];
365 CREATESTRUCTA *csA = (CREATESTRUCTA *)lParam;
366 CREATESTRUCTW csW = *(CREATESTRUCTW *)csA;
367 MDICREATESTRUCTW mdi_cs;
368 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
370 if (!IS_INTRESOURCE(csA->lpszClass))
372 class_lenA = strlen(csA->lpszClass) + 1;
373 RtlMultiByteToUnicodeSize( &class_lenW, csA->lpszClass, class_lenA );
375 if (!IS_INTRESOURCE(csA->lpszName))
377 name_lenA = strlen(csA->lpszName) + 1;
378 RtlMultiByteToUnicodeSize( &name_lenW, csA->lpszName, name_lenA );
381 if (!(ptr = get_buffer( buffer, sizeof(buffer), class_lenW + name_lenW ))) break;
383 if (class_lenW)
385 csW.lpszClass = ptr;
386 RtlMultiByteToUnicodeN( ptr, class_lenW, NULL, csA->lpszClass, class_lenA );
388 if (name_lenW)
390 csW.lpszName = ptr + class_lenW/sizeof(WCHAR);
391 RtlMultiByteToUnicodeN( ptr + class_lenW/sizeof(WCHAR), name_lenW, NULL,
392 csA->lpszName, name_lenA );
395 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
397 mdi_cs = *(MDICREATESTRUCTW *)csA->lpCreateParams;
398 mdi_cs.szTitle = csW.lpszName;
399 mdi_cs.szClass = csW.lpszClass;
400 csW.lpCreateParams = &mdi_cs;
403 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
404 free_buffer( buffer, ptr );
406 break;
408 case WM_MDICREATE:
410 WCHAR *ptr, buffer[512];
411 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
412 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
413 MDICREATESTRUCTW csW;
415 memcpy( &csW, csA, sizeof(csW) );
417 if (!IS_INTRESOURCE(csA->szTitle))
419 title_lenA = strlen(csA->szTitle) + 1;
420 RtlMultiByteToUnicodeSize( &title_lenW, csA->szTitle, title_lenA );
422 if (!IS_INTRESOURCE(csA->szClass))
424 class_lenA = strlen(csA->szClass) + 1;
425 RtlMultiByteToUnicodeSize( &class_lenW, csA->szClass, class_lenA );
428 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenW + class_lenW ))) break;
430 if (title_lenW)
432 csW.szTitle = ptr;
433 RtlMultiByteToUnicodeN( ptr, title_lenW, NULL, csA->szTitle, title_lenA );
435 if (class_lenW)
437 csW.szClass = ptr + title_lenW/sizeof(WCHAR);
438 RtlMultiByteToUnicodeN( ptr + title_lenW/sizeof(WCHAR), class_lenW, NULL,
439 csA->szClass, class_lenA );
441 ret = callback( hwnd, msg, wParam, (LPARAM)&csW, result, arg );
442 free_buffer( buffer, ptr );
444 break;
446 case WM_GETTEXT:
447 case WM_ASKCBFORMATNAME:
449 WCHAR *ptr, buffer[512];
450 LPSTR str = (LPSTR)lParam;
451 DWORD len = wParam * sizeof(WCHAR);
453 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
454 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
455 if (wParam)
457 len = 0;
458 if (*result)
459 RtlUnicodeToMultiByteN( str, wParam - 1, &len, ptr, ret * sizeof(WCHAR) );
460 str[len] = 0;
461 *result = len;
463 free_buffer( buffer, ptr );
465 break;
467 case LB_ADDSTRING:
468 case LB_INSERTSTRING:
469 case LB_FINDSTRING:
470 case LB_FINDSTRINGEXACT:
471 case LB_SELECTSTRING:
472 case CB_ADDSTRING:
473 case CB_INSERTSTRING:
474 case CB_FINDSTRING:
475 case CB_FINDSTRINGEXACT:
476 case CB_SELECTSTRING:
477 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
479 ret = callback( hwnd, msg, wParam, lParam, result, arg );
480 break;
482 /* fall through */
483 case WM_SETTEXT:
484 case WM_WININICHANGE:
485 case WM_DEVMODECHANGE:
486 case CB_DIR:
487 case LB_DIR:
488 case LB_ADDFILE:
489 case EM_REPLACESEL:
490 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
491 else
493 WCHAR *ptr, buffer[512];
494 LPCSTR strA = (LPCSTR)lParam;
495 DWORD lenW, lenA = strlen(strA) + 1;
497 RtlMultiByteToUnicodeSize( &lenW, strA, lenA );
498 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW )))
500 RtlMultiByteToUnicodeN( ptr, lenW, NULL, strA, lenA );
501 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
502 free_buffer( buffer, ptr );
505 break;
507 case LB_GETTEXT:
508 case CB_GETLBTEXT:
509 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
511 WCHAR buffer[512]; /* FIXME: fixed sized buffer */
513 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
514 if (*result >= 0)
516 DWORD len;
517 RtlUnicodeToMultiByteN( (LPSTR)lParam, ~0u, &len,
518 buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
519 *result = len - 1;
522 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
523 break;
525 case EM_GETLINE:
527 WCHAR *ptr, buffer[512];
528 WORD len = *(WORD *)lParam;
530 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
531 *((WORD *)ptr) = len; /* store the length */
532 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
533 if (*result)
535 DWORD reslen;
536 RtlUnicodeToMultiByteN( (LPSTR)lParam, len, &reslen, ptr, *result * sizeof(WCHAR) );
537 if (reslen < len) ((LPSTR)lParam)[reslen] = 0;
538 *result = reslen;
540 free_buffer( buffer, ptr );
542 break;
544 case WM_GETDLGCODE:
545 if (lParam)
547 MSG newmsg = *(MSG *)lParam;
548 if (map_wparam_AtoW( newmsg.message, &newmsg.wParam, WMCHAR_MAP_NOMAPPING ))
549 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
551 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
552 break;
554 case WM_CHARTOITEM:
555 case WM_MENUCHAR:
556 case WM_CHAR:
557 case WM_DEADCHAR:
558 case WM_SYSCHAR:
559 case WM_SYSDEADCHAR:
560 case EM_SETPASSWORDCHAR:
561 case WM_IME_CHAR:
562 if (map_wparam_AtoW( msg, &wParam, mapping ))
563 ret = callback( hwnd, msg, wParam, lParam, result, arg );
564 break;
566 case WM_GETTEXTLENGTH:
567 case CB_GETLBTEXTLEN:
568 case LB_GETTEXTLEN:
569 ret = callback( hwnd, msg, wParam, lParam, result, arg );
570 if (*result >= 0)
572 WCHAR *ptr, buffer[512];
573 LRESULT tmp;
574 DWORD len = *result + 1;
575 /* Determine respective GETTEXT message */
576 UINT msgGetText = (msg == WM_GETTEXTLENGTH) ? WM_GETTEXT :
577 ((msg == CB_GETLBTEXTLEN) ? CB_GETLBTEXT : LB_GETTEXT);
578 /* wParam differs between the messages */
579 WPARAM wp = (msg == WM_GETTEXTLENGTH) ? len : wParam;
581 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
583 if (callback == call_window_proc) /* FIXME: hack */
584 callback( hwnd, msgGetText, wp, (LPARAM)ptr, &tmp, arg );
585 else
586 tmp = SendMessageW( hwnd, msgGetText, wp, (LPARAM)ptr );
587 RtlUnicodeToMultiByteSize( &len, ptr, tmp * sizeof(WCHAR) );
588 *result = len;
589 free_buffer( buffer, ptr );
591 break;
593 case WM_PAINTCLIPBOARD:
594 case WM_SIZECLIPBOARD:
595 FIXME_(msg)( "message %s (0x%x) needs translation, please report\n",
596 SPY_GetMsgName(msg, hwnd), msg );
597 break;
599 default:
600 ret = callback( hwnd, msg, wParam, lParam, result, arg );
601 break;
603 return ret;
607 /**********************************************************************
608 * WINPROC_CallProcWtoA
610 * Call a window procedure, translating args from Unicode to Ansi.
612 static LRESULT WINPROC_CallProcWtoA( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wParam,
613 LPARAM lParam, LRESULT *result, void *arg )
615 LRESULT ret = 0;
617 TRACE_(msg)("(hwnd=%p,msg=%s,wp=%08lx,lp=%08lx)\n",
618 hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
620 switch(msg)
622 case WM_NCCREATE:
623 case WM_CREATE:
625 char buffer[1024], *cls;
626 CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
627 CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
628 MDICREATESTRUCTA mdi_cs;
629 DWORD name_lenA = 0, name_lenW = 0, class_lenA = 0, class_lenW = 0;
631 if (!IS_INTRESOURCE(csW->lpszClass))
633 class_lenW = (strlenW(csW->lpszClass) + 1) * sizeof(WCHAR);
634 RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
636 if (!IS_INTRESOURCE(csW->lpszName))
638 name_lenW = (strlenW(csW->lpszName) + 1) * sizeof(WCHAR);
639 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
642 if (!(cls = get_buffer( buffer, sizeof(buffer), class_lenA + name_lenA ))) break;
644 if (class_lenA)
646 RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
647 csA.lpszClass = cls;
649 if (name_lenA)
651 char *name = cls + class_lenA;
652 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
653 csA.lpszName = name;
656 if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
658 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
659 mdi_cs.szTitle = csA.lpszName;
660 mdi_cs.szClass = csA.lpszClass;
661 csA.lpCreateParams = &mdi_cs;
664 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
665 free_buffer( buffer, cls );
667 break;
669 case WM_GETTEXT:
670 case WM_ASKCBFORMATNAME:
672 char *ptr, buffer[512];
673 DWORD len = wParam * 2;
675 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
676 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
677 if (len)
679 if (*result)
681 RtlMultiByteToUnicodeN( (LPWSTR)lParam, wParam*sizeof(WCHAR), &len, ptr, ret + 1 );
682 *result = len/sizeof(WCHAR) - 1; /* do not count terminating null */
684 ((LPWSTR)lParam)[*result] = 0;
686 free_buffer( buffer, ptr );
688 break;
690 case LB_ADDSTRING:
691 case LB_INSERTSTRING:
692 case LB_FINDSTRING:
693 case LB_FINDSTRINGEXACT:
694 case LB_SELECTSTRING:
695 case CB_ADDSTRING:
696 case CB_INSERTSTRING:
697 case CB_FINDSTRING:
698 case CB_FINDSTRINGEXACT:
699 case CB_SELECTSTRING:
700 if (!lParam || !WINPROC_TestLBForStr( hwnd, msg ))
702 ret = callback( hwnd, msg, wParam, lParam, result, arg );
703 break;
705 /* fall through */
706 case WM_SETTEXT:
707 case WM_WININICHANGE:
708 case WM_DEVMODECHANGE:
709 case CB_DIR:
710 case LB_DIR:
711 case LB_ADDFILE:
712 case EM_REPLACESEL:
713 if (!lParam) ret = callback( hwnd, msg, wParam, lParam, result, arg );
714 else
716 char *ptr, buffer[512];
717 LPCWSTR strW = (LPCWSTR)lParam;
718 DWORD lenA, lenW = (strlenW(strW) + 1) * sizeof(WCHAR);
720 RtlUnicodeToMultiByteSize( &lenA, strW, lenW );
721 if ((ptr = get_buffer( buffer, sizeof(buffer), lenA )))
723 RtlUnicodeToMultiByteN( ptr, lenA, NULL, strW, lenW );
724 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
725 free_buffer( buffer, ptr );
728 break;
730 case WM_MDICREATE:
732 char *ptr, buffer[1024];
733 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
734 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
735 MDICREATESTRUCTA csA;
737 memcpy( &csA, csW, sizeof(csA) );
739 if (!IS_INTRESOURCE(csW->szTitle))
741 title_lenW = (strlenW(csW->szTitle) + 1) * sizeof(WCHAR);
742 RtlUnicodeToMultiByteSize( &title_lenA, csW->szTitle, title_lenW );
744 if (!IS_INTRESOURCE(csW->szClass))
746 class_lenW = (strlenW(csW->szClass) + 1) * sizeof(WCHAR);
747 RtlUnicodeToMultiByteSize( &class_lenA, csW->szClass, class_lenW );
750 if (!(ptr = get_buffer( buffer, sizeof(buffer), title_lenA + class_lenA ))) break;
752 if (title_lenA)
754 RtlUnicodeToMultiByteN( ptr, title_lenA, NULL, csW->szTitle, title_lenW );
755 csA.szTitle = ptr;
757 if (class_lenA)
759 RtlUnicodeToMultiByteN( ptr + title_lenA, class_lenA, NULL, csW->szClass, class_lenW );
760 csA.szClass = ptr + title_lenA;
762 ret = callback( hwnd, msg, wParam, (LPARAM)&csA, result, arg );
763 free_buffer( buffer, ptr );
765 break;
767 case LB_GETTEXT:
768 case CB_GETLBTEXT:
769 if (lParam && WINPROC_TestLBForStr( hwnd, msg ))
771 char buffer[512]; /* FIXME: fixed sized buffer */
773 ret = callback( hwnd, msg, wParam, (LPARAM)buffer, result, arg );
774 if (*result >= 0)
776 DWORD len;
777 RtlMultiByteToUnicodeN( (LPWSTR)lParam, ~0u, &len, buffer, strlen(buffer) + 1 );
778 *result = len / sizeof(WCHAR) - 1;
781 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
782 break;
784 case EM_GETLINE:
786 char *ptr, buffer[512];
787 WORD len = *(WORD *)lParam;
789 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * 2 ))) break;
790 *((WORD *)ptr) = len * 2; /* store the length */
791 ret = callback( hwnd, msg, wParam, (LPARAM)ptr, result, arg );
792 if (*result)
794 DWORD reslen;
795 RtlMultiByteToUnicodeN( (LPWSTR)lParam, len*sizeof(WCHAR), &reslen, ptr, *result );
796 *result = reslen / sizeof(WCHAR);
797 if (*result < len) ((LPWSTR)lParam)[*result] = 0;
799 free_buffer( buffer, ptr );
801 break;
803 case WM_GETDLGCODE:
804 if (lParam)
806 MSG newmsg = *(MSG *)lParam;
807 switch(newmsg.message)
809 case WM_CHAR:
810 case WM_DEADCHAR:
811 case WM_SYSCHAR:
812 case WM_SYSDEADCHAR:
813 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 1 );
814 break;
815 case WM_IME_CHAR:
816 newmsg.wParam = map_wparam_char_WtoA( newmsg.wParam, 2 );
817 break;
819 ret = callback( hwnd, msg, wParam, (LPARAM)&newmsg, result, arg );
821 else ret = callback( hwnd, msg, wParam, lParam, result, arg );
822 break;
824 case WM_CHAR:
826 WCHAR wch = wParam;
827 char ch[2];
828 DWORD cp = get_input_codepage();
829 DWORD len = WideCharToMultiByte( cp, 0, &wch, 1, ch, 2, NULL, NULL );
830 ret = callback( hwnd, msg, (BYTE)ch[0], lParam, result, arg );
831 if (len == 2) ret = callback( hwnd, msg, (BYTE)ch[1], lParam, result, arg );
833 break;
835 case WM_CHARTOITEM:
836 case WM_MENUCHAR:
837 case WM_DEADCHAR:
838 case WM_SYSCHAR:
839 case WM_SYSDEADCHAR:
840 case EM_SETPASSWORDCHAR:
841 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,1), lParam, result, arg );
842 break;
844 case WM_IME_CHAR:
845 ret = callback( hwnd, msg, map_wparam_char_WtoA(wParam,2), lParam, result, arg );
846 break;
848 case WM_PAINTCLIPBOARD:
849 case WM_SIZECLIPBOARD:
850 FIXME_(msg)( "message %s (%04x) needs translation, please report\n",
851 SPY_GetMsgName(msg, hwnd), msg );
852 break;
854 default:
855 ret = callback( hwnd, msg, wParam, lParam, result, arg );
856 break;
859 return ret;
863 /**********************************************************************
864 * WINPROC_call_window
866 * Call the window procedure of the specified window.
868 BOOL WINPROC_call_window( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
869 LRESULT *result, BOOL unicode, enum wm_char_mapping mapping )
871 struct user_thread_info *thread_info = get_user_thread_info();
872 WND *wndPtr;
873 WNDPROC func;
874 WINDOWPROC *proc;
875 BOOL unicode_win;
877 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
878 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
879 if (wndPtr->tid != GetCurrentThreadId())
881 WIN_ReleasePtr( wndPtr );
882 return FALSE;
884 func = wndPtr->winproc;
885 proc = handle_to_proc( wndPtr->winproc );
886 unicode_win = wndPtr->flags & WIN_ISUNICODE;
887 WIN_ReleasePtr( wndPtr );
889 if (thread_info->recursion_count > MAX_WINPROC_RECURSION) return FALSE;
890 thread_info->recursion_count++;
892 if (unicode)
894 if (proc == WINPROC_PROC16)
895 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, result, func );
896 else if (proc && proc->procW)
897 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procW );
898 else if (proc)
899 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procA );
900 else if (unicode_win)
901 call_window_proc( hwnd, msg, wParam, lParam, result, func );
902 else
903 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, result, func );
905 else
907 if (proc == WINPROC_PROC16)
908 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, result, func );
909 else if (proc && proc->procA)
910 call_window_proc( hwnd, msg, wParam, lParam, result, proc->procA );
911 else if (proc)
912 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, result, proc->procW, mapping );
913 else if (unicode_win)
914 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, result, func, mapping );
915 else
916 call_window_proc( hwnd, msg, wParam, lParam, result, func );
918 thread_info->recursion_count--;
919 return TRUE;
923 /**********************************************************************
924 * CallWindowProcA (USER32.@)
926 * The CallWindowProc() function invokes the windows procedure _func_,
927 * with _hwnd_ as the target window, the message specified by _msg_, and
928 * the message parameters _wParam_ and _lParam_.
930 * Some kinds of argument conversion may be done, I'm not sure what.
932 * CallWindowProc() may be used for windows subclassing. Use
933 * SetWindowLong() to set a new windows procedure for windows of the
934 * subclass, and handle subclassed messages in the new windows
935 * procedure. The new windows procedure may then use CallWindowProc()
936 * with _func_ set to the parent class's windows procedure to dispatch
937 * the message to the superclass.
939 * RETURNS
941 * The return value is message dependent.
943 * CONFORMANCE
945 * ECMA-234, Win32
947 LRESULT WINAPI CallWindowProcA(
948 WNDPROC func, /* [in] window procedure */
949 HWND hwnd, /* [in] target window */
950 UINT msg, /* [in] message */
951 WPARAM wParam, /* [in] message dependent parameter */
952 LPARAM lParam /* [in] message dependent parameter */
954 WINDOWPROC *proc;
955 LRESULT result;
957 if (!func) return 0;
959 if (!(proc = handle_to_proc( func )))
960 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
961 else if (proc == WINPROC_PROC16)
962 wow_handlers.call_window_proc( hwnd, msg, wParam, lParam, &result, func );
963 else if (proc->procA)
964 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
965 else
966 WINPROC_CallProcAtoW( call_window_proc, hwnd, msg, wParam, lParam, &result,
967 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
968 return result;
972 /**********************************************************************
973 * CallWindowProcW (USER32.@)
975 * See CallWindowProcA.
977 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
978 WPARAM wParam, LPARAM lParam )
980 WINDOWPROC *proc;
981 LRESULT result;
983 if (!func) return 0;
985 if (!(proc = handle_to_proc( func )))
986 call_window_proc( hwnd, msg, wParam, lParam, &result, func );
987 else if (proc == WINPROC_PROC16)
988 WINPROC_CallProcWtoA( wow_handlers.call_window_proc, hwnd, msg, wParam, lParam, &result, func );
989 else if (proc->procW)
990 call_window_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
991 else
992 WINPROC_CallProcWtoA( call_window_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
993 return result;
997 /**********************************************************************
998 * WINPROC_CallDlgProcA
1000 INT_PTR WINPROC_CallDlgProcA( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1002 WINDOWPROC *proc;
1003 LRESULT result;
1004 INT_PTR ret;
1006 if (!func) return 0;
1008 if (!(proc = handle_to_proc( func )))
1009 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1010 else if (proc == WINPROC_PROC16)
1012 ret = wow_handlers.call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1013 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1015 else if (proc->procW)
1017 ret = WINPROC_CallProcAtoW( call_dialog_proc, hwnd, msg, wParam, lParam, &result,
1018 proc->procW, WMCHAR_MAP_CALLWINDOWPROC );
1019 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1021 else
1022 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procA );
1023 return ret;
1027 /**********************************************************************
1028 * WINPROC_CallDlgProcW
1030 INT_PTR WINPROC_CallDlgProcW( DLGPROC func, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1032 WINDOWPROC *proc;
1033 LRESULT result;
1034 INT_PTR ret;
1036 if (!func) return 0;
1038 if (!(proc = handle_to_proc( func )))
1039 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, func );
1040 else if (proc == WINPROC_PROC16)
1042 ret = WINPROC_CallProcWtoA( wow_handlers.call_dialog_proc, hwnd, msg, wParam, lParam, &result, func );
1043 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1045 else if (proc->procA)
1047 ret = WINPROC_CallProcWtoA( call_dialog_proc, hwnd, msg, wParam, lParam, &result, proc->procA );
1048 SetWindowLongPtrW( hwnd, DWLP_MSGRESULT, result );
1050 else
1051 ret = call_dialog_proc( hwnd, msg, wParam, lParam, &result, proc->procW );
1052 return ret;
1056 /***********************************************************************
1057 * Window procedures for builtin classes
1060 static LRESULT WINAPI ButtonWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1062 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, FALSE );
1065 static LRESULT WINAPI ButtonWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1067 return wow_handlers.button_proc( hwnd, msg, wParam, lParam, TRUE );
1070 static LRESULT WINAPI ComboWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1072 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, FALSE );
1075 static LRESULT WINAPI ComboWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1077 return wow_handlers.combo_proc( hwnd, message, wParam, lParam, TRUE );
1080 LRESULT WINAPI EditWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1082 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, FALSE );
1085 static LRESULT WINAPI EditWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1087 return wow_handlers.edit_proc( hwnd, msg, wParam, lParam, TRUE );
1090 static LRESULT WINAPI ListBoxWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1092 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, FALSE );
1095 static LRESULT WINAPI ListBoxWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1097 return wow_handlers.listbox_proc( hwnd, msg, wParam, lParam, TRUE );
1100 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1102 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, FALSE );
1105 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1107 return wow_handlers.mdiclient_proc( hwnd, msg, wParam, lParam, TRUE );
1110 static LRESULT WINAPI ScrollBarWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1112 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, FALSE );
1115 static LRESULT WINAPI ScrollBarWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1117 return wow_handlers.scrollbar_proc( hwnd, msg, wParam, lParam, TRUE );
1120 static LRESULT WINAPI StaticWndProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1122 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, FALSE );
1125 static LRESULT WINAPI StaticWndProcW( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
1127 return wow_handlers.static_proc( hwnd, msg, wParam, lParam, TRUE );
1130 static DWORD wait_message( DWORD count, const HANDLE *handles, DWORD timeout, DWORD mask, DWORD flags )
1132 DWORD ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count, handles, timeout, mask, flags );
1133 if (ret == WAIT_TIMEOUT && !count && !timeout) NtYieldExecution();
1134 if ((mask & QS_INPUT) == QS_INPUT) get_user_thread_info()->message_count = 0;
1135 return ret;
1138 /**********************************************************************
1139 * UserRegisterWowHandlers (USER32.@)
1141 * NOTE: no attempt has been made to be compatible here,
1142 * the Windows function is most likely completely different.
1144 void WINAPI UserRegisterWowHandlers( const struct wow_handlers16 *new, struct wow_handlers32 *orig )
1146 orig->button_proc = ButtonWndProc_common;
1147 orig->combo_proc = ComboWndProc_common;
1148 orig->edit_proc = EditWndProc_common;
1149 orig->listbox_proc = ListBoxWndProc_common;
1150 orig->mdiclient_proc = MDIClientWndProc_common;
1151 orig->scrollbar_proc = ScrollBarWndProc_common;
1152 orig->static_proc = StaticWndProc_common;
1153 orig->wait_message = wait_message;
1154 orig->create_window = WIN_CreateWindowEx;
1155 orig->get_win_handle = WIN_GetFullHandle;
1156 orig->alloc_winproc = WINPROC_AllocProc;
1157 orig->get_dialog_info = DIALOG_get_info;
1158 orig->dialog_box_loop = DIALOG_DoDialogBox;
1159 orig->get_icon_param = get_icon_param;
1160 orig->set_icon_param = set_icon_param;
1162 wow_handlers = *new;
1165 struct wow_handlers16 wow_handlers =
1167 ButtonWndProc_common,
1168 ComboWndProc_common,
1169 EditWndProc_common,
1170 ListBoxWndProc_common,
1171 MDIClientWndProc_common,
1172 ScrollBarWndProc_common,
1173 StaticWndProc_common,
1174 wait_message,
1175 WIN_CreateWindowEx,
1176 NULL, /* call_window_proc */
1177 NULL, /* call_dialog_proc */
1178 NULL, /* free_icon_param */