user32: Set WM_CONTEXTMENU's wparam to the child window's handle.
[wine.git] / dlls / user32 / defwnd.c
blob44675c7b8e7924ef546b48da5769f178a0ad8d32
1 /*
2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
5 * 1995 Alex Korobka
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 <string.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winnls.h"
29 #include "imm.h"
30 #include "win.h"
31 #include "user_private.h"
32 #include "controls.h"
33 #include "wine/server.h"
34 #include "wine/exception.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
39 /* bits in the dwKeyData */
40 #define KEYDATA_ALT 0x2000
41 #define KEYDATA_PREVSTATE 0x4000
43 #define DRAG_FILE 0x454C4946
45 static short iF10Key = 0;
46 static short iMenuSysKey = 0;
48 /***********************************************************************
49 * DEFWND_HandleWindowPosChanged
51 * Handle the WM_WINDOWPOSCHANGED message.
53 static void DEFWND_HandleWindowPosChanged( HWND hwnd, const WINDOWPOS *winpos )
55 RECT rect;
57 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
58 if (!(winpos->flags & SWP_NOCLIENTMOVE))
59 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG(rect.left, rect.top));
61 if (!(winpos->flags & SWP_NOCLIENTSIZE) || (winpos->flags & SWP_STATECHANGED))
63 if (IsIconic( hwnd ))
65 SendMessageW( hwnd, WM_SIZE, SIZE_MINIMIZED, 0 );
67 else
69 WPARAM wp = IsZoomed( hwnd ) ? SIZE_MAXIMIZED : SIZE_RESTORED;
71 SendMessageW( hwnd, WM_SIZE, wp, MAKELONG(rect.right-rect.left, rect.bottom-rect.top) );
77 /***********************************************************************
78 * DEFWND_SetTextA
80 * Set the window text.
82 static LRESULT DEFWND_SetTextA( HWND hwnd, LPCSTR text )
84 int count;
85 WCHAR *textW;
86 WND *wndPtr;
88 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
89 * may have child window IDs instead of window name */
90 if (text && IS_INTRESOURCE(text))
91 return 0;
93 if (!text) text = "";
94 count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
96 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
97 if ((textW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
99 HeapFree(GetProcessHeap(), 0, wndPtr->text);
100 wndPtr->text = textW;
101 MultiByteToWideChar( CP_ACP, 0, text, -1, textW, count );
102 SERVER_START_REQ( set_window_text )
104 req->handle = wine_server_user_handle( hwnd );
105 wine_server_add_data( req, textW, (count-1) * sizeof(WCHAR) );
106 wine_server_call( req );
108 SERVER_END_REQ;
110 else
111 ERR("Not enough memory for window text\n");
112 WIN_ReleasePtr( wndPtr );
114 USER_Driver->pSetWindowText( hwnd, textW );
116 return 1;
119 /***********************************************************************
120 * DEFWND_SetTextW
122 * Set the window text.
124 static LRESULT DEFWND_SetTextW( HWND hwnd, LPCWSTR text )
126 WND *wndPtr;
127 int count;
129 /* check for string, as static icons, bitmaps (SS_ICON, SS_BITMAP)
130 * may have child window IDs instead of window name */
131 if (text && IS_INTRESOURCE(text))
132 return 0;
134 if (!text) text = L"";
135 count = lstrlenW(text) + 1;
137 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
138 HeapFree(GetProcessHeap(), 0, wndPtr->text);
139 if ((wndPtr->text = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
141 lstrcpyW( wndPtr->text, text );
142 SERVER_START_REQ( set_window_text )
144 req->handle = wine_server_user_handle( hwnd );
145 wine_server_add_data( req, wndPtr->text, (count-1) * sizeof(WCHAR) );
146 wine_server_call( req );
148 SERVER_END_REQ;
150 else
151 ERR("Not enough memory for window text\n");
152 text = wndPtr->text;
153 WIN_ReleasePtr( wndPtr );
155 USER_Driver->pSetWindowText( hwnd, text );
157 return 1;
160 /***********************************************************************
161 * DEFWND_ControlColor
163 * Default colors for control painting.
165 HBRUSH DEFWND_ControlColor( HDC hDC, UINT ctlType )
167 if( ctlType == CTLCOLOR_SCROLLBAR)
169 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
170 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
171 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
172 SetBkColor( hDC, bk);
174 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
175 * we better use 0x55aa bitmap brush to make scrollbar's background
176 * look different from the window background.
178 if (bk == GetSysColor(COLOR_WINDOW))
179 return SYSCOLOR_Get55AABrush();
181 UnrealizeObject( hb );
182 return hb;
185 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
187 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
188 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
189 else {
190 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
191 return GetSysColorBrush(COLOR_3DFACE);
193 return GetSysColorBrush(COLOR_WINDOW);
197 /***********************************************************************
198 * DEFWND_Print
200 * This method handles the default behavior for the WM_PRINT message.
202 static void DEFWND_Print( HWND hwnd, HDC hdc, ULONG uFlags)
205 * Visibility flag.
207 if ( (uFlags & PRF_CHECKVISIBLE) &&
208 !IsWindowVisible(hwnd) )
209 return;
212 * Unimplemented flags.
214 if ( (uFlags & PRF_CHILDREN) ||
215 (uFlags & PRF_OWNED) ||
216 (uFlags & PRF_NONCLIENT) )
218 WARN("WM_PRINT message with unsupported flags\n");
222 * Background
224 if ( uFlags & PRF_ERASEBKGND)
225 SendMessageW(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
228 * Client area
230 if ( uFlags & PRF_CLIENT)
231 SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, uFlags);
236 /***********************************************************************
237 * DEFWND_DefWinProc
239 * Default window procedure for messages that are the same in Ansi and Unicode.
241 static LRESULT DEFWND_DefWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
243 switch(msg)
245 case WM_NCPAINT:
246 return NC_HandleNCPaint( hwnd, (HRGN)wParam );
248 case WM_NCMOUSEMOVE:
250 POINT pt;
251 pt.x = (short)LOWORD(lParam);
252 pt.y = (short)HIWORD(lParam);
253 return NC_HandleNCMouseMove( hwnd, pt );
256 case WM_NCMOUSELEAVE:
257 return NC_HandleNCMouseLeave( hwnd );
259 case WM_NCHITTEST:
261 POINT pt;
262 pt.x = (short)LOWORD(lParam);
263 pt.y = (short)HIWORD(lParam);
264 return NC_HandleNCHitTest( hwnd, pt );
267 case WM_NCCALCSIZE:
268 return NC_HandleNCCalcSize( hwnd, wParam, (RECT *)lParam );
270 case WM_WINDOWPOSCHANGING:
271 return WINPOS_HandleWindowPosChanging( hwnd, (WINDOWPOS *)lParam );
273 case WM_WINDOWPOSCHANGED:
274 DEFWND_HandleWindowPosChanged( hwnd, (const WINDOWPOS *)lParam );
275 break;
277 case WM_LBUTTONDOWN:
278 case WM_RBUTTONDOWN:
279 case WM_MBUTTONDOWN:
280 iF10Key = iMenuSysKey = 0;
281 break;
283 case WM_NCLBUTTONDOWN:
284 return NC_HandleNCLButtonDown( hwnd, wParam, lParam );
286 case WM_LBUTTONDBLCLK:
287 return NC_HandleNCLButtonDblClk( hwnd, HTCLIENT, lParam );
289 case WM_NCLBUTTONDBLCLK:
290 return NC_HandleNCLButtonDblClk( hwnd, wParam, lParam );
292 case WM_NCRBUTTONDOWN:
293 return NC_HandleNCRButtonDown( hwnd, wParam, lParam );
295 case WM_RBUTTONUP:
297 POINT pt;
298 pt.x = (short)LOWORD(lParam);
299 pt.y = (short)HIWORD(lParam);
300 ClientToScreen(hwnd, &pt);
301 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(pt.x, pt.y) );
303 break;
305 case WM_NCRBUTTONUP:
307 * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
308 * in Windows), but what _should_ we do? According to MSDN :
309 * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
310 * message to the window". When is it appropriate?
312 break;
314 case WM_XBUTTONUP:
315 case WM_NCXBUTTONUP:
316 if (HIWORD(wParam) == XBUTTON1 || HIWORD(wParam) == XBUTTON2)
318 SendMessageW(hwnd, WM_APPCOMMAND, (WPARAM)hwnd,
319 MAKELPARAM(LOWORD(wParam), FAPPCOMMAND_MOUSE | HIWORD(wParam)));
321 break;
323 case WM_CONTEXTMENU:
324 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
325 SendMessageW( GetParent(hwnd), msg, (WPARAM)hwnd, lParam );
326 else
328 LONG hitcode;
329 POINT pt;
330 pt.x = (short)LOWORD(lParam);
331 pt.y = (short)HIWORD(lParam);
332 hitcode = NC_HandleNCHitTest(hwnd, pt);
334 /* Track system popup if click was in the caption area. */
335 if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
336 TrackPopupMenu(GetSystemMenu(hwnd, FALSE),
337 TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
338 pt.x, pt.y, 0, hwnd, NULL);
340 break;
342 case WM_POPUPSYSTEMMENU:
343 /* This is an undocumented message used by the windows taskbar to
344 display the system menu of windows that belong to other processes. */
345 TrackPopupMenu( GetSystemMenu(hwnd, FALSE), TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
346 (short)LOWORD(lParam), (short)HIWORD(lParam), 0, hwnd, NULL );
347 return 0;
349 case WM_NCACTIVATE:
350 return NC_HandleNCActivate( hwnd, wParam, lParam );
352 case WM_NCDESTROY:
354 WND *wndPtr = WIN_GetPtr( hwnd );
355 if (!wndPtr) return 0;
356 HeapFree( GetProcessHeap(), 0, wndPtr->text );
357 wndPtr->text = NULL;
358 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
359 wndPtr->pScroll = NULL;
360 WIN_ReleasePtr( wndPtr );
361 return 0;
364 case WM_PRINT:
365 DEFWND_Print(hwnd, (HDC)wParam, lParam);
366 return 0;
368 case WM_PAINTICON:
369 case WM_PAINT:
371 PAINTSTRUCT ps;
372 HDC hdc = BeginPaint( hwnd, &ps );
373 if( hdc )
375 HICON hIcon;
376 if (IsIconic(hwnd) && ((hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON))) )
378 RECT rc;
379 int x, y;
381 GetClientRect( hwnd, &rc );
382 x = (rc.right - rc.left - GetSystemMetrics(SM_CXICON))/2;
383 y = (rc.bottom - rc.top - GetSystemMetrics(SM_CYICON))/2;
384 TRACE("Painting class icon: vis rect=(%s)\n",
385 wine_dbgstr_rect(&ps.rcPaint));
386 DrawIcon( hdc, x, y, hIcon );
388 EndPaint( hwnd, &ps );
390 return 0;
393 case WM_SYNCPAINT:
394 RedrawWindow ( hwnd, NULL, 0, RDW_ERASENOW | RDW_ERASE | RDW_ALLCHILDREN );
395 return 0;
397 case WM_SETREDRAW:
398 if (wParam) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
399 else
401 RedrawWindow( hwnd, NULL, 0, RDW_ALLCHILDREN | RDW_VALIDATE );
402 WIN_SetStyle( hwnd, 0, WS_VISIBLE );
404 return 0;
406 case WM_CLOSE:
407 DestroyWindow( hwnd );
408 return 0;
410 case WM_MOUSEACTIVATE:
411 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
413 LONG ret = SendMessageW( GetParent(hwnd), WM_MOUSEACTIVATE, wParam, lParam );
414 if (ret) return ret;
417 /* Caption clicks are handled by NC_HandleNCLButtonDown() */
418 return ( HIWORD(lParam) == WM_LBUTTONDOWN && LOWORD(lParam) == HTCAPTION ? MA_NOACTIVATE : MA_ACTIVATE );
420 case WM_ACTIVATE:
421 /* The default action in Windows is to set the keyboard focus to
422 * the window, if it's being activated and not minimized */
423 if (LOWORD(wParam) != WA_INACTIVE) {
424 if (!IsIconic(hwnd)) SetFocus(hwnd);
426 break;
428 case WM_MOUSEWHEEL:
429 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
430 return SendMessageW( GetParent(hwnd), WM_MOUSEWHEEL, wParam, lParam );
431 break;
433 case WM_ERASEBKGND:
434 case WM_ICONERASEBKGND:
436 RECT rect;
437 HDC hdc = (HDC)wParam;
438 HBRUSH hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
439 if (!hbr) return 0;
441 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
443 /* can't use GetClipBox with a parent DC or we fill the whole parent */
444 GetClientRect( hwnd, &rect );
445 DPtoLP( hdc, (LPPOINT)&rect, 2 );
447 else GetClipBox( hdc, &rect );
448 FillRect( hdc, &rect, hbr );
449 return 1;
452 case WM_GETDLGCODE:
453 return 0;
455 case WM_CTLCOLORMSGBOX:
456 case WM_CTLCOLOREDIT:
457 case WM_CTLCOLORLISTBOX:
458 case WM_CTLCOLORBTN:
459 case WM_CTLCOLORDLG:
460 case WM_CTLCOLORSTATIC:
461 case WM_CTLCOLORSCROLLBAR:
462 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
464 case WM_CTLCOLOR:
465 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
467 case WM_SETCURSOR:
468 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)
470 /* with the exception of the border around a resizable wnd,
471 * give the parent first chance to set the cursor */
472 if ((LOWORD(lParam) < HTSIZEFIRST) || (LOWORD(lParam) > HTSIZELAST))
474 HWND parent = GetParent( hwnd );
475 if (parent != GetDesktopWindow() &&
476 SendMessageW( parent, WM_SETCURSOR, wParam, lParam )) return TRUE;
479 NC_HandleSetCursor( hwnd, wParam, lParam );
480 break;
482 case WM_SYSCOMMAND:
483 return NC_HandleSysCommand( hwnd, wParam, lParam );
485 case WM_KEYDOWN:
486 if(wParam == VK_F10) iF10Key = VK_F10;
487 break;
489 case WM_SYSKEYDOWN:
490 if( HIWORD(lParam) & KEYDATA_ALT )
492 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
493 if ( (wParam == VK_MENU || wParam == VK_LMENU
494 || wParam == VK_RMENU) && !iMenuSysKey )
495 iMenuSysKey = 1;
496 else
497 iMenuSysKey = 0;
499 iF10Key = 0;
501 if( wParam == VK_F4 ) /* try to close the window */
503 HWND top = GetAncestor( hwnd, GA_ROOT );
504 if (!(GetClassLongW( top, GCL_STYLE ) & CS_NOCLOSE))
505 PostMessageW( top, WM_SYSCOMMAND, SC_CLOSE, 0 );
508 else if( wParam == VK_F10 )
510 if (NtUserGetKeyState(VK_SHIFT) & 0x8000)
511 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, -1 );
512 iF10Key = 1;
514 else if (wParam == VK_ESCAPE && (NtUserGetKeyState(VK_SHIFT) & 0x8000))
515 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ' );
516 break;
518 case WM_KEYUP:
519 case WM_SYSKEYUP:
520 /* Press and release F10 or ALT */
521 if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
522 && iMenuSysKey) || ((wParam == VK_F10) && iF10Key))
523 SendMessageW( GetAncestor( hwnd, GA_ROOT ), WM_SYSCOMMAND, SC_KEYMENU, 0L );
524 iMenuSysKey = iF10Key = 0;
525 break;
527 case WM_SYSCHAR:
529 iMenuSysKey = 0;
530 if (wParam == '\r' && IsIconic(hwnd))
532 PostMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0L );
533 break;
535 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
537 if (wParam == '\t' || wParam == '\x1b') break;
538 if (wParam == ' ' && (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
539 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
540 else
541 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, wParam );
543 else /* check for Ctrl-Esc */
544 if (wParam != '\x1b') MessageBeep(0);
545 break;
548 case WM_SHOWWINDOW:
550 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
551 WND *pWnd;
552 if (!lParam) return 0; /* sent from ShowWindow */
553 if ((style & WS_VISIBLE) && wParam) return 0;
554 if (!(style & WS_VISIBLE) && !wParam) return 0;
555 if (!GetWindow( hwnd, GW_OWNER )) return 0;
556 if (!(pWnd = WIN_GetPtr( hwnd ))) return 0;
557 if (pWnd == WND_OTHER_PROCESS) return 0;
558 if (wParam)
560 if (!(pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP))
562 WIN_ReleasePtr( pWnd );
563 return 0;
565 pWnd->flags &= ~WIN_NEEDS_SHOW_OWNEDPOPUP;
567 else pWnd->flags |= WIN_NEEDS_SHOW_OWNEDPOPUP;
568 WIN_ReleasePtr( pWnd );
569 ShowWindow( hwnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
570 break;
573 case WM_CANCELMODE:
574 iMenuSysKey = 0;
575 MENU_EndMenu( hwnd );
576 if (GetCapture() == hwnd) ReleaseCapture();
577 break;
579 case WM_VKEYTOITEM:
580 case WM_CHARTOITEM:
581 return -1;
583 case WM_DROPOBJECT:
584 return DRAG_FILE;
586 case WM_QUERYDROPOBJECT:
587 return (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) != 0;
589 case WM_QUERYDRAGICON:
591 UINT len;
593 HICON hIcon = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
594 HINSTANCE instance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
595 if (hIcon) return (LRESULT)hIcon;
596 for(len=1; len<64; len++)
597 if((hIcon = LoadIconW(instance, MAKEINTRESOURCEW(len))))
598 return (LRESULT)hIcon;
599 return (LRESULT)LoadIconW(0, (LPWSTR)IDI_APPLICATION);
601 break;
603 case WM_ISACTIVEICON:
604 return (win_get_flags( hwnd ) & WIN_NCACTIVATED) != 0;
606 case WM_NOTIFYFORMAT:
607 if (IsWindowUnicode(hwnd)) return NFR_UNICODE;
608 else return NFR_ANSI;
610 case WM_QUERYOPEN:
611 case WM_QUERYENDSESSION:
612 return 1;
614 case WM_SETICON:
616 HICON ret;
617 WND *wndPtr = WIN_GetPtr( hwnd );
619 switch(wParam)
621 case ICON_SMALL:
622 ret = wndPtr->hIconSmall;
623 if (ret && !lParam && wndPtr->hIcon)
625 wndPtr->hIconSmall2 = CopyImage( wndPtr->hIcon, IMAGE_ICON,
626 GetSystemMetrics( SM_CXSMICON ),
627 GetSystemMetrics( SM_CYSMICON ), 0 );
629 else if (lParam && wndPtr->hIconSmall2)
631 DestroyIcon( wndPtr->hIconSmall2 );
632 wndPtr->hIconSmall2 = NULL;
634 wndPtr->hIconSmall = (HICON)lParam;
635 break;
636 case ICON_BIG:
637 ret = wndPtr->hIcon;
638 if (wndPtr->hIconSmall2)
640 DestroyIcon( wndPtr->hIconSmall2 );
641 wndPtr->hIconSmall2 = NULL;
643 if (lParam && !wndPtr->hIconSmall)
645 wndPtr->hIconSmall2 = CopyImage( (HICON)lParam, IMAGE_ICON,
646 GetSystemMetrics( SM_CXSMICON ),
647 GetSystemMetrics( SM_CYSMICON ), 0 );
649 wndPtr->hIcon = (HICON)lParam;
650 break;
651 default:
652 ret = 0;
653 break;
655 WIN_ReleasePtr( wndPtr );
657 USER_Driver->pSetWindowIcon( hwnd, wParam, (HICON)lParam );
659 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
660 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
662 return (LRESULT)ret;
665 case WM_GETICON:
667 HICON ret;
668 WND *wndPtr = WIN_GetPtr( hwnd );
670 switch(wParam)
672 case ICON_SMALL:
673 ret = wndPtr->hIconSmall;
674 break;
675 case ICON_BIG:
676 ret = wndPtr->hIcon;
677 break;
678 case ICON_SMALL2:
679 ret = wndPtr->hIconSmall ? wndPtr->hIconSmall : wndPtr->hIconSmall2;
680 break;
681 default:
682 ret = 0;
683 break;
685 WIN_ReleasePtr( wndPtr );
686 return (LRESULT)ret;
689 case WM_HELP:
690 SendMessageW( GetParent(hwnd), msg, wParam, lParam );
691 break;
693 case WM_STYLECHANGED:
694 if (wParam == GWL_STYLE && (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED))
696 STYLESTRUCT *style = (STYLESTRUCT *)lParam;
697 if ((style->styleOld ^ style->styleNew) & (WS_CAPTION|WS_THICKFRAME|WS_VSCROLL|WS_HSCROLL))
698 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOZORDER |
699 SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE );
701 break;
703 case WM_APPCOMMAND:
705 HWND parent = GetParent(hwnd);
706 if(!parent)
707 HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam, TRUE);
708 else
709 SendMessageW( parent, msg, wParam, lParam );
710 break;
712 case WM_KEYF1:
714 HELPINFO hi;
716 hi.cbSize = sizeof(HELPINFO);
717 GetCursorPos( &hi.MousePos );
718 if (MENU_IsMenuActive())
720 hi.iContextType = HELPINFO_MENUITEM;
721 hi.hItemHandle = MENU_IsMenuActive();
722 hi.iCtrlId = MenuItemFromPoint( hwnd, hi.hItemHandle, hi.MousePos );
723 hi.dwContextId = GetMenuContextHelpId( hi.hItemHandle );
725 else
727 hi.iContextType = HELPINFO_WINDOW;
728 hi.hItemHandle = hwnd;
729 hi.iCtrlId = GetWindowLongPtrA( hwnd, GWLP_ID );
730 hi.dwContextId = GetWindowContextHelpId( hwnd );
732 SendMessageW( hwnd, WM_HELP, 0, (LPARAM)&hi );
733 break;
736 case WM_INPUTLANGCHANGEREQUEST:
737 NtUserActivateKeyboardLayout( (HKL)lParam, 0 );
738 break;
740 case WM_INPUTLANGCHANGE:
742 struct user_thread_info *info = get_user_thread_info();
743 int count = 0;
744 HWND *win_array = WIN_ListChildren( hwnd );
745 info->kbd_layout = (HKL)lParam;
747 if (!win_array)
748 break;
749 while (win_array[count])
750 SendMessageW( win_array[count++], WM_INPUTLANGCHANGE, wParam, lParam);
751 HeapFree(GetProcessHeap(),0,win_array);
752 break;
757 return 0;
760 static LPARAM DEFWND_GetTextA( WND *wndPtr, LPSTR dest, WPARAM wParam )
762 LPARAM result = 0;
764 __TRY
766 if (wndPtr->text)
768 if (!WideCharToMultiByte( CP_ACP, 0, wndPtr->text, -1,
769 dest, wParam, NULL, NULL )) dest[wParam-1] = 0;
770 result = strlen( dest );
772 else dest[0] = '\0';
774 __EXCEPT_PAGE_FAULT
776 return 0;
778 __ENDTRY
779 return result;
782 /***********************************************************************
783 * DefWindowProcA (USER32.@)
785 * See DefWindowProcW.
787 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
789 LRESULT result = 0;
790 HWND full_handle;
792 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
794 if (!IsWindow( hwnd )) return 0;
795 ERR( "called for other process window %p\n", hwnd );
796 return 0;
798 hwnd = full_handle;
800 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
802 switch(msg)
804 case WM_NCCREATE:
805 if (lParam)
807 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
809 DEFWND_SetTextA( hwnd, cs->lpszName );
810 result = 1;
812 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
814 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
815 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
816 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
819 break;
821 case WM_GETTEXTLENGTH:
823 WND *wndPtr = WIN_GetPtr( hwnd );
824 if (wndPtr && wndPtr->text)
825 result = WideCharToMultiByte( CP_ACP, 0, wndPtr->text, lstrlenW(wndPtr->text),
826 NULL, 0, NULL, NULL );
827 WIN_ReleasePtr( wndPtr );
829 break;
831 case WM_GETTEXT:
832 if (wParam)
834 LPSTR dest = (LPSTR)lParam;
835 WND *wndPtr = WIN_GetPtr( hwnd );
837 if (!wndPtr) break;
838 result = DEFWND_GetTextA( wndPtr, dest, wParam );
840 WIN_ReleasePtr( wndPtr );
842 break;
844 case WM_SETTEXT:
845 if (!DEFWND_SetTextA( hwnd, (LPCSTR)lParam ))
846 break;
847 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
848 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
849 result = 1; /* success. FIXME: check text length */
850 break;
852 case WM_IME_CHAR:
853 if (HIBYTE(wParam)) PostMessageA( hwnd, WM_CHAR, HIBYTE(wParam), lParam );
854 PostMessageA( hwnd, WM_CHAR, LOBYTE(wParam), lParam );
855 break;
857 case WM_IME_KEYDOWN:
858 result = PostMessageA( hwnd, WM_KEYDOWN, wParam, lParam );
859 break;
861 case WM_IME_KEYUP:
862 result = PostMessageA( hwnd, WM_KEYUP, wParam, lParam );
863 break;
865 case WM_IME_COMPOSITION:
866 if (lParam & GCS_RESULTSTR)
868 LONG size, i;
869 unsigned char lead = 0;
870 char *buf = NULL;
871 HIMC himc = ImmGetContext( hwnd );
873 if (himc)
875 if ((size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, NULL, 0 )))
877 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size ))) size = 0;
878 else size = ImmGetCompositionStringA( himc, GCS_RESULTSTR, buf, size );
880 ImmReleaseContext( hwnd, himc );
882 for (i = 0; i < size; i++)
884 unsigned char c = buf[i];
885 if (!lead)
887 if (IsDBCSLeadByte( c ))
888 lead = c;
889 else
890 SendMessageA( hwnd, WM_IME_CHAR, c, 1 );
892 else
894 SendMessageA( hwnd, WM_IME_CHAR, MAKEWORD(c, lead), 1 );
895 lead = 0;
898 HeapFree( GetProcessHeap(), 0, buf );
901 /* fall through */
902 case WM_IME_STARTCOMPOSITION:
903 case WM_IME_ENDCOMPOSITION:
904 case WM_IME_SELECT:
905 case WM_IME_NOTIFY:
906 case WM_IME_CONTROL:
908 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
909 if (hwndIME)
910 result = SendMessageA( hwndIME, msg, wParam, lParam );
912 break;
913 case WM_IME_SETCONTEXT:
915 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
916 if (hwndIME) result = ImmIsUIMessageA( hwndIME, msg, wParam, lParam );
918 break;
920 case WM_SYSCHAR:
922 CHAR ch = LOWORD(wParam);
923 WCHAR wch;
924 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
925 wParam = MAKEWPARAM( wch, HIWORD(wParam) );
927 /* fall through */
928 default:
929 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
930 break;
933 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
934 return result;
938 static LPARAM DEFWND_GetTextW( WND *wndPtr, LPWSTR dest, WPARAM wParam )
940 LPARAM result = 0;
942 __TRY
944 if (wndPtr->text)
946 lstrcpynW( dest, wndPtr->text, wParam );
947 result = lstrlenW( dest );
949 else dest[0] = '\0';
951 __EXCEPT_PAGE_FAULT
953 return 0;
955 __ENDTRY
957 return result;
960 /***********************************************************************
961 * DefWindowProcW (USER32.@) Calls default window message handler
963 * Calls default window procedure for messages not processed
964 * by application.
966 * RETURNS
967 * Return value is dependent upon the message.
969 LRESULT WINAPI DefWindowProcW(
970 HWND hwnd, /* [in] window procedure receiving message */
971 UINT msg, /* [in] message identifier */
972 WPARAM wParam, /* [in] first message parameter */
973 LPARAM lParam ) /* [in] second message parameter */
975 LRESULT result = 0;
976 HWND full_handle;
978 if (!(full_handle = WIN_IsCurrentProcess( hwnd )))
980 if (!IsWindow( hwnd )) return 0;
981 ERR( "called for other process window %p\n", hwnd );
982 return 0;
984 hwnd = full_handle;
985 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
987 switch(msg)
989 case WM_NCCREATE:
990 if (lParam)
992 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
994 DEFWND_SetTextW( hwnd, cs->lpszName );
995 result = 1;
997 if(cs->style & (WS_HSCROLL | WS_VSCROLL))
999 SCROLLINFO si = {sizeof si, SIF_ALL, 0, 100, 0, 0, 0};
1000 SetScrollInfo( hwnd, SB_HORZ, &si, FALSE );
1001 SetScrollInfo( hwnd, SB_VERT, &si, FALSE );
1004 break;
1006 case WM_GETTEXTLENGTH:
1008 WND *wndPtr = WIN_GetPtr( hwnd );
1009 if (wndPtr && wndPtr->text) result = (LRESULT)lstrlenW(wndPtr->text);
1010 WIN_ReleasePtr( wndPtr );
1012 break;
1014 case WM_GETTEXT:
1015 if (wParam)
1017 LPWSTR dest = (LPWSTR)lParam;
1018 WND *wndPtr = WIN_GetPtr( hwnd );
1020 if (!wndPtr) break;
1021 result = DEFWND_GetTextW( wndPtr, dest, wParam );
1022 WIN_ReleasePtr( wndPtr );
1024 break;
1026 case WM_SETTEXT:
1027 if (!DEFWND_SetTextW( hwnd, (LPCWSTR)lParam ))
1028 break;
1029 if( (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CAPTION) == WS_CAPTION )
1030 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
1031 result = 1; /* success. FIXME: check text length */
1032 break;
1034 case WM_IME_CHAR:
1035 PostMessageW( hwnd, WM_CHAR, wParam, lParam );
1036 break;
1038 case WM_IME_KEYDOWN:
1039 result = PostMessageW( hwnd, WM_KEYDOWN, wParam, lParam );
1040 break;
1042 case WM_IME_KEYUP:
1043 result = PostMessageW( hwnd, WM_KEYUP, wParam, lParam );
1044 break;
1046 case WM_IME_SETCONTEXT:
1048 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1049 if (hwndIME) result = ImmIsUIMessageW( hwndIME, msg, wParam, lParam );
1051 break;
1053 case WM_IME_COMPOSITION:
1054 if (lParam & GCS_RESULTSTR)
1056 LONG size, i;
1057 WCHAR *buf = NULL;
1058 HIMC himc = ImmGetContext( hwnd );
1060 if (himc)
1062 if ((size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, NULL, 0 )))
1064 if (!(buf = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) size = 0;
1065 else size = ImmGetCompositionStringW( himc, GCS_RESULTSTR, buf, size * sizeof(WCHAR) );
1067 ImmReleaseContext( hwnd, himc );
1069 for (i = 0; i < size / sizeof(WCHAR); i++)
1070 SendMessageW( hwnd, WM_IME_CHAR, buf[i], 1 );
1071 HeapFree( GetProcessHeap(), 0, buf );
1074 /* fall through */
1075 case WM_IME_STARTCOMPOSITION:
1076 case WM_IME_ENDCOMPOSITION:
1077 case WM_IME_SELECT:
1078 case WM_IME_NOTIFY:
1079 case WM_IME_CONTROL:
1081 HWND hwndIME = ImmGetDefaultIMEWnd( hwnd );
1082 if (hwndIME)
1083 result = SendMessageW( hwndIME, msg, wParam, lParam );
1085 break;
1087 default:
1088 result = DEFWND_DefWinProc( hwnd, msg, wParam, lParam );
1089 break;
1091 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result, wParam, lParam );
1092 return result;